blob: 06c38d1502b76510f26cb89cdda51004f70b9304 [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) {
Peter Ujfalusi50831452010-03-03 15:08:04 +0200318 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
Mark Brown06f409d2009-04-07 18:10:13 +0100319 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);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900430 goto config_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);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900435 goto config_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);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900440 goto config_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)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900447 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100448 }
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
Jassi Brar14dc5732010-02-26 09:12:32 +0900457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
458 cpu_dai->playback.active++;
459 codec_dai->playback.active++;
460 } else {
461 cpu_dai->capture.active++;
462 codec_dai->capture.active++;
463 }
464 cpu_dai->active++;
465 codec_dai->active++;
Mark Brown6627a652009-01-23 22:55:23 +0000466 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467 mutex_unlock(&pcm_mutex);
468 return 0;
469
Jassi Brarbb1c0472010-02-25 11:24:53 +0900470config_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 if (machine->ops && machine->ops->shutdown)
472 machine->ops->shutdown(substream);
473
Jassi Brarbb1c0472010-02-25 11:24:53 +0900474machine_err:
475 if (codec_dai->ops->shutdown)
476 codec_dai->ops->shutdown(substream, codec_dai);
477
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100478codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200479 if (platform->pcm_ops->close)
480 platform->pcm_ops->close(substream);
481
482platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800483 if (cpu_dai->ops->shutdown)
484 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485out:
486 mutex_unlock(&pcm_mutex);
487 return ret;
488}
489
490/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200491 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 * This is to ensure there are no pops or clicks in between any music tracks
493 * due to DAPM power cycling.
494 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100495static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496{
Mark Brown63084192008-12-02 15:08:03 +0000497 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
498 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000499 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100500 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501 int i;
502
503 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200504 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 codec_dai = &codec->dai[i];
506
Mark Brownf24368c2008-10-21 21:45:08 +0100507 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
508 codec_dai->playback.stream_name,
509 codec_dai->playback.active ? "active" : "inactive",
510 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200511
512 /* are we waiting on this codec DAI stream */
513 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100515 snd_soc_dapm_stream_event(codec,
516 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200517 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 }
519 }
520 mutex_unlock(&pcm_mutex);
521}
522
523/*
524 * Called by ALSA when a PCM substream is closed. Private data can be
525 * freed here. The cpu DAI, codec DAI, machine and platform are also
526 * shutdown.
527 */
528static int soc_codec_close(struct snd_pcm_substream *substream)
529{
530 struct snd_soc_pcm_runtime *rtd = substream->private_data;
531 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000532 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100533 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000534 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100535 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
536 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000537 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538
539 mutex_lock(&pcm_mutex);
540
Jassi Brar14dc5732010-02-26 09:12:32 +0900541 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
542 cpu_dai->playback.active--;
543 codec_dai->playback.active--;
544 } else {
545 cpu_dai->capture.active--;
546 codec_dai->capture.active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900548
549 cpu_dai->active--;
550 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200551 codec->active--;
552
Mark Brown6010b2d2008-09-06 18:33:24 +0100553 /* Muting the DAC suppresses artifacts caused during digital
554 * shutdown, for example from stopping clocks.
555 */
556 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
557 snd_soc_dai_digital_mute(codec_dai, 1);
558
Eric Miao6335d052009-03-03 09:41:00 +0800559 if (cpu_dai->ops->shutdown)
560 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
Eric Miao6335d052009-03-03 09:41:00 +0800562 if (codec_dai->ops->shutdown)
563 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564
565 if (machine->ops && machine->ops->shutdown)
566 machine->ops->shutdown(substream);
567
568 if (platform->pcm_ops->close)
569 platform->pcm_ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200570
571 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
572 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100573 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000574 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000575 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576 } else {
577 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100578 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100579 codec_dai->capture.stream_name,
580 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200581 }
582
583 mutex_unlock(&pcm_mutex);
584 return 0;
585}
586
587/*
588 * Called by ALSA when the PCM substream is prepared, can set format, sample
589 * rate, etc. This function is non atomic and can be called multiple times,
590 * it can refer to the runtime info.
591 */
592static int soc_pcm_prepare(struct snd_pcm_substream *substream)
593{
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000596 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100597 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000598 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100599 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
600 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000601 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602 int ret = 0;
603
604 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100605
606 if (machine->ops && machine->ops->prepare) {
607 ret = machine->ops->prepare(substream);
608 if (ret < 0) {
609 printk(KERN_ERR "asoc: machine prepare error\n");
610 goto out;
611 }
612 }
613
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200614 if (platform->pcm_ops->prepare) {
615 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200616 if (ret < 0) {
617 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200618 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200619 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620 }
621
Eric Miao6335d052009-03-03 09:41:00 +0800622 if (codec_dai->ops->prepare) {
623 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200624 if (ret < 0) {
625 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200627 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 }
629
Eric Miao6335d052009-03-03 09:41:00 +0800630 if (cpu_dai->ops->prepare) {
631 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100632 if (ret < 0) {
633 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
634 goto out;
635 }
636 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637
Mark Brownd45f6212008-10-14 13:58:36 +0100638 /* cancel any delayed stream shutdown that is pending */
639 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
640 codec_dai->pop_wait) {
641 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000642 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100643 }
644
Mark Brown452c5ea2009-05-17 21:41:23 +0100645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
646 snd_soc_dapm_stream_event(codec,
647 codec_dai->playback.stream_name,
648 SND_SOC_DAPM_STREAM_START);
649 else
650 snd_soc_dapm_stream_event(codec,
651 codec_dai->capture.stream_name,
652 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100653
Mark Brown452c5ea2009-05-17 21:41:23 +0100654 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
656out:
657 mutex_unlock(&pcm_mutex);
658 return ret;
659}
660
661/*
662 * Called by ALSA when the hardware params are set by application. This
663 * function can also be called multiple times and can allocate buffers
664 * (using snd_pcm_lib_* ). It's non-atomic.
665 */
666static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
667 struct snd_pcm_hw_params *params)
668{
669 struct snd_soc_pcm_runtime *rtd = substream->private_data;
670 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100671 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000672 struct snd_soc_card *card = socdev->card;
673 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100674 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
675 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676 int ret = 0;
677
678 mutex_lock(&pcm_mutex);
679
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100680 if (machine->ops && machine->ops->hw_params) {
681 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100683 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684 goto out;
685 }
686 }
687
Eric Miao6335d052009-03-03 09:41:00 +0800688 if (codec_dai->ops->hw_params) {
689 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100690 if (ret < 0) {
691 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
692 codec_dai->name);
693 goto codec_err;
694 }
695 }
696
Eric Miao6335d052009-03-03 09:41:00 +0800697 if (cpu_dai->ops->hw_params) {
698 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200700 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100701 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702 goto interface_err;
703 }
704 }
705
706 if (platform->pcm_ops->hw_params) {
707 ret = platform->pcm_ops->hw_params(substream, params);
708 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200709 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 platform->name);
711 goto platform_err;
712 }
713 }
714
Mark Brown06f409d2009-04-07 18:10:13 +0100715 machine->rate = params_rate(params);
716
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717out:
718 mutex_unlock(&pcm_mutex);
719 return ret;
720
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800722 if (cpu_dai->ops->hw_free)
723 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724
725interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800726 if (codec_dai->ops->hw_free)
727 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100728
729codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200730 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100731 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732
733 mutex_unlock(&pcm_mutex);
734 return ret;
735}
736
737/*
738 * Free's resources allocated by hw_params, can be called multiple times
739 */
740static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
741{
742 struct snd_soc_pcm_runtime *rtd = substream->private_data;
743 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100744 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000745 struct snd_soc_card *card = socdev->card;
746 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100747 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
748 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000749 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750
751 mutex_lock(&pcm_mutex);
752
753 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100754 if (!codec->active)
755 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756
757 /* free any machine hw params */
758 if (machine->ops && machine->ops->hw_free)
759 machine->ops->hw_free(substream);
760
761 /* free any DMA resources */
762 if (platform->pcm_ops->hw_free)
763 platform->pcm_ops->hw_free(substream);
764
765 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800766 if (codec_dai->ops->hw_free)
767 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768
Eric Miao6335d052009-03-03 09:41:00 +0800769 if (cpu_dai->ops->hw_free)
770 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771
772 mutex_unlock(&pcm_mutex);
773 return 0;
774}
775
776static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
777{
778 struct snd_soc_pcm_runtime *rtd = substream->private_data;
779 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000780 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100781 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000782 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100783 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
784 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785 int ret;
786
Eric Miao6335d052009-03-03 09:41:00 +0800787 if (codec_dai->ops->trigger) {
788 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200789 if (ret < 0)
790 return ret;
791 }
792
793 if (platform->pcm_ops->trigger) {
794 ret = platform->pcm_ops->trigger(substream, cmd);
795 if (ret < 0)
796 return ret;
797 }
798
Eric Miao6335d052009-03-03 09:41:00 +0800799 if (cpu_dai->ops->trigger) {
800 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801 if (ret < 0)
802 return ret;
803 }
804 return 0;
805}
806
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200807/*
808 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200809 * If cpu_dai, codec_dai, platform driver has the delay callback, than
810 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200811 */
812static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
813{
814 struct snd_soc_pcm_runtime *rtd = substream->private_data;
815 struct snd_soc_device *socdev = rtd->socdev;
816 struct snd_soc_card *card = socdev->card;
817 struct snd_soc_platform *platform = card->platform;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200818 struct snd_soc_dai_link *machine = rtd->dai;
819 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
820 struct snd_soc_dai *codec_dai = machine->codec_dai;
821 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200822 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200823 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200824
825 if (platform->pcm_ops->pointer)
826 offset = platform->pcm_ops->pointer(substream);
827
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200828 if (cpu_dai->ops->delay)
829 delay += cpu_dai->ops->delay(substream, cpu_dai);
830
831 if (codec_dai->ops->delay)
832 delay += codec_dai->ops->delay(substream, codec_dai);
833
834 if (platform->delay)
835 delay += platform->delay(substream, codec_dai);
836
837 runtime->delay = delay;
838
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200839 return offset;
840}
841
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842/* ASoC PCM operations */
843static struct snd_pcm_ops soc_pcm_ops = {
844 .open = soc_pcm_open,
845 .close = soc_codec_close,
846 .hw_params = soc_pcm_hw_params,
847 .hw_free = soc_pcm_hw_free,
848 .prepare = soc_pcm_prepare,
849 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200850 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851};
852
853#ifdef CONFIG_PM
854/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100855static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856{
Mark Brown416356f2009-06-30 19:05:15 +0100857 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200858 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000859 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000860 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200861 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000862 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 int i;
864
Daniel Macke3509ff2009-06-03 17:44:49 +0200865 /* If the initialization of this soc device failed, there is no codec
866 * associated with it. Just bail out in this case.
867 */
868 if (!codec)
869 return 0;
870
Andy Green6ed25972008-06-13 16:24:05 +0100871 /* Due to the resume being scheduled into a workqueue we could
872 * suspend before that's finished - wait for it to complete.
873 */
874 snd_power_lock(codec->card);
875 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
876 snd_power_unlock(codec->card);
877
878 /* we're going to block userspace touching us until resume completes */
879 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
880
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000882 for (i = 0; i < card->num_links; i++) {
883 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800884 if (dai->ops->digital_mute && dai->playback.active)
885 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886 }
887
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100888 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000889 for (i = 0; i < card->num_links; i++)
890 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100891
Mark Brown87506542008-11-18 20:50:34 +0000892 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100893 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894
Mark Brown87506542008-11-18 20:50:34 +0000895 for (i = 0; i < card->num_links; i++) {
896 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000897 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000898 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900900 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200901 }
902
903 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000904 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200905 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906
Mark Brown3ff3f642008-05-19 12:32:25 +0200907 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 char *stream = codec->dai[i].playback.stream_name;
909 if (stream != NULL)
910 snd_soc_dapm_stream_event(codec, stream,
911 SND_SOC_DAPM_STREAM_SUSPEND);
912 stream = codec->dai[i].capture.stream_name;
913 if (stream != NULL)
914 snd_soc_dapm_stream_event(codec, stream,
915 SND_SOC_DAPM_STREAM_SUSPEND);
916 }
917
918 if (codec_dev->suspend)
Mark Brown416356f2009-06-30 19:05:15 +0100919 codec_dev->suspend(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200920
Mark Brown87506542008-11-18 20:50:34 +0000921 for (i = 0; i < card->num_links; i++) {
922 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000923 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000924 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200925 }
926
Mark Brown87506542008-11-18 20:50:34 +0000927 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100928 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200929
930 return 0;
931}
932
Andy Green6ed25972008-06-13 16:24:05 +0100933/* deferred resume work, so resume can complete before we finished
934 * setting our codec back up, which can be very slow on I2C
935 */
936static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200937{
Mark Brown63084192008-12-02 15:08:03 +0000938 struct snd_soc_card *card = container_of(work,
939 struct snd_soc_card,
940 deferred_resume_work);
941 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000942 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200943 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000944 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100945 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200946 int i;
947
Andy Green6ed25972008-06-13 16:24:05 +0100948 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
949 * so userspace apps are blocked from touching us
950 */
951
Mark Brownfde22f22008-11-24 18:08:18 +0000952 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100953
Mark Brown87506542008-11-18 20:50:34 +0000954 if (card->resume_pre)
955 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200956
Mark Brown87506542008-11-18 20:50:34 +0000957 for (i = 0; i < card->num_links; i++) {
958 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000959 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000960 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200961 }
962
963 if (codec_dev->resume)
964 codec_dev->resume(pdev);
965
Mark Brown3ff3f642008-05-19 12:32:25 +0200966 for (i = 0; i < codec->num_dai; i++) {
967 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200968 if (stream != NULL)
969 snd_soc_dapm_stream_event(codec, stream,
970 SND_SOC_DAPM_STREAM_RESUME);
971 stream = codec->dai[i].capture.stream_name;
972 if (stream != NULL)
973 snd_soc_dapm_stream_event(codec, stream,
974 SND_SOC_DAPM_STREAM_RESUME);
975 }
976
Mark Brown3ff3f642008-05-19 12:32:25 +0200977 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000978 for (i = 0; i < card->num_links; i++) {
979 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800980 if (dai->ops->digital_mute && dai->playback.active)
981 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200982 }
983
Mark Brown87506542008-11-18 20:50:34 +0000984 for (i = 0; i < card->num_links; i++) {
985 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000986 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000987 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200988 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +0900989 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990 }
991
Mark Brown87506542008-11-18 20:50:34 +0000992 if (card->resume_post)
993 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200994
Mark Brownfde22f22008-11-24 18:08:18 +0000995 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100996
997 /* userspace can access us now we are back as we were before */
998 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
999}
1000
1001/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001002static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001003{
Mark Brown416356f2009-06-30 19:05:15 +01001004 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +01001005 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +00001006 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +01001007 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +01001008
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001009 /* If the initialization of this soc device failed, there is no codec
1010 * associated with it. Just bail out in this case.
1011 */
1012 if (!card->codec)
1013 return 0;
1014
Mark Brown64ab9ba2009-03-31 11:27:03 +01001015 /* AC97 devices might have other drivers hanging off them so
1016 * need to resume immediately. Other drivers don't have that
1017 * problem and may take a substantial amount of time to resume
1018 * due to I/O costs and anti-pop so handle them out of line.
1019 */
1020 if (cpu_dai->ac97_control) {
1021 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1022 soc_resume_deferred(&card->deferred_resume_work);
1023 } else {
1024 dev_dbg(socdev->dev, "Scheduling resume work\n");
1025 if (!schedule_work(&card->deferred_resume_work))
1026 dev_err(socdev->dev, "resume work item may be lost\n");
1027 }
Andy Green6ed25972008-06-13 16:24:05 +01001028
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001029 return 0;
1030}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001031#else
1032#define soc_suspend NULL
1033#define soc_resume NULL
1034#endif
1035
Barry Song02a06d32009-10-16 18:13:38 +08001036static struct snd_soc_dai_ops null_dai_ops = {
1037};
1038
Mark Brown435c5e22008-12-04 15:32:53 +00001039static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001040{
Mark Brown435c5e22008-12-04 15:32:53 +00001041 struct platform_device *pdev = container_of(card->dev,
1042 struct platform_device,
1043 dev);
1044 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001045 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001046 struct snd_soc_platform *platform;
1047 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001048 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001049
Mark Brown435c5e22008-12-04 15:32:53 +00001050 if (card->instantiated)
1051 return;
Mark Brown63084192008-12-02 15:08:03 +00001052
Mark Brown435c5e22008-12-04 15:32:53 +00001053 found = 0;
1054 list_for_each_entry(platform, &platform_list, list)
1055 if (card->platform == platform) {
1056 found = 1;
1057 break;
1058 }
1059 if (!found) {
1060 dev_dbg(card->dev, "Platform %s not registered\n",
1061 card->platform->name);
1062 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001063 }
1064
Mark Brown6b05eda2008-12-08 19:26:48 +00001065 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001066 for (i = 0; i < card->num_links; i++) {
1067 found = 0;
1068 list_for_each_entry(dai, &dai_list, list)
1069 if (card->dai_link[i].cpu_dai == dai) {
1070 found = 1;
1071 break;
1072 }
1073 if (!found) {
1074 dev_dbg(card->dev, "DAI %s not registered\n",
1075 card->dai_link[i].cpu_dai->name);
1076 return;
1077 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001078
1079 if (card->dai_link[i].cpu_dai->ac97_control)
1080 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001081 }
1082
Barry Song02a06d32009-10-16 18:13:38 +08001083 for (i = 0; i < card->num_links; i++) {
1084 if (!card->dai_link[i].codec_dai->ops)
1085 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1086 }
1087
Mark Brown6b05eda2008-12-08 19:26:48 +00001088 /* If we have AC97 in the system then don't wait for the
1089 * codec. This will need revisiting if we have to handle
1090 * systems with mixed AC97 and non-AC97 parts. Only check for
1091 * DAIs currently; we can't do this per link since some AC97
1092 * codecs have non-AC97 DAIs.
1093 */
1094 if (!ac97)
1095 for (i = 0; i < card->num_links; i++) {
1096 found = 0;
1097 list_for_each_entry(dai, &dai_list, list)
1098 if (card->dai_link[i].codec_dai == dai) {
1099 found = 1;
1100 break;
1101 }
1102 if (!found) {
1103 dev_dbg(card->dev, "DAI %s not registered\n",
1104 card->dai_link[i].codec_dai->name);
1105 return;
1106 }
1107 }
1108
Mark Brown435c5e22008-12-04 15:32:53 +00001109 /* Note that we do not current check for codec components */
1110
1111 dev_dbg(card->dev, "All components present, instantiating\n");
1112
1113 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001114 card->pmdown_time = pmdown_time;
1115
Mark Brown87506542008-11-18 20:50:34 +00001116 if (card->probe) {
1117 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001118 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001119 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001120 }
1121
Mark Brown87506542008-11-18 20:50:34 +00001122 for (i = 0; i < card->num_links; i++) {
1123 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001124 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001125 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001126 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001127 goto cpu_dai_err;
1128 }
1129 }
1130
1131 if (codec_dev->probe) {
1132 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001133 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001134 goto cpu_dai_err;
1135 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001136 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001137
1138 if (platform->probe) {
1139 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001140 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001141 goto platform_err;
1142 }
1143
1144 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001145 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001146#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001147 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001148 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001149#endif
Andy Green6ed25972008-06-13 16:24:05 +01001150
Mark Brownfe3e78e2009-11-03 22:13:13 +00001151 for (i = 0; i < card->num_links; i++) {
1152 if (card->dai_link[i].init) {
1153 ret = card->dai_link[i].init(codec);
1154 if (ret < 0) {
1155 printk(KERN_ERR "asoc: failed to init %s\n",
1156 card->dai_link[i].stream_name);
1157 continue;
1158 }
1159 }
Barry Songf7732052009-11-12 12:01:47 +08001160 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001161 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001162 }
1163
1164 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1165 "%s", card->name);
1166 snprintf(codec->card->longname, sizeof(codec->card->longname),
1167 "%s (%s)", card->name, codec->name);
1168
1169 /* Make sure all DAPM widgets are instantiated */
1170 snd_soc_dapm_new_widgets(codec);
1171
1172 ret = snd_card_register(codec->card);
1173 if (ret < 0) {
1174 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1175 codec->name);
1176 goto card_err;
1177 }
1178
1179 mutex_lock(&codec->mutex);
1180#ifdef CONFIG_SND_SOC_AC97_BUS
1181 /* Only instantiate AC97 if not already done by the adaptor
1182 * for the generic AC97 subsystem.
1183 */
1184 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1185 ret = soc_ac97_dev_register(codec);
1186 if (ret < 0) {
1187 printk(KERN_ERR "asoc: AC97 device register failed\n");
1188 snd_card_free(codec->card);
1189 mutex_unlock(&codec->mutex);
1190 goto card_err;
1191 }
1192 }
1193#endif
1194
1195 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1196 if (ret < 0)
1197 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1198
Mark Browndbe21402010-02-12 11:37:24 +00001199 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1200 if (ret < 0)
1201 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1202
Mark Brownfe3e78e2009-11-03 22:13:13 +00001203 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1204 if (ret < 0)
1205 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1206
1207 soc_init_codec_debugfs(codec);
1208 mutex_unlock(&codec->mutex);
1209
Mark Brown435c5e22008-12-04 15:32:53 +00001210 card->instantiated = 1;
1211
1212 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001213
Mark Brownfe3e78e2009-11-03 22:13:13 +00001214card_err:
1215 if (platform->remove)
1216 platform->remove(pdev);
1217
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001218platform_err:
1219 if (codec_dev->remove)
1220 codec_dev->remove(pdev);
1221
1222cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001223 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001224 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001225 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001226 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001227 }
1228
Mark Brown87506542008-11-18 20:50:34 +00001229 if (card->remove)
1230 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001231}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001232
Mark Brown435c5e22008-12-04 15:32:53 +00001233/*
1234 * Attempt to initialise any uninitalised cards. Must be called with
1235 * client_mutex.
1236 */
1237static void snd_soc_instantiate_cards(void)
1238{
1239 struct snd_soc_card *card;
1240 list_for_each_entry(card, &card_list, list)
1241 snd_soc_instantiate_card(card);
1242}
1243
1244/* probes a new socdev */
1245static int soc_probe(struct platform_device *pdev)
1246{
1247 int ret = 0;
1248 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1249 struct snd_soc_card *card = socdev->card;
1250
1251 /* Bodge while we push things out of socdev */
1252 card->socdev = socdev;
1253
1254 /* Bodge while we unpick instantiation */
1255 card->dev = &pdev->dev;
1256 ret = snd_soc_register_card(card);
1257 if (ret != 0) {
1258 dev_err(&pdev->dev, "Failed to register card\n");
1259 return ret;
1260 }
1261
1262 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001263}
1264
1265/* removes a socdev */
1266static int soc_remove(struct platform_device *pdev)
1267{
1268 int i;
1269 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001270 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001271 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001272 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1273
Mike Rapoport914dc182009-05-11 13:04:55 +03001274 if (!card->instantiated)
1275 return 0;
1276
Mark Brown63084192008-12-02 15:08:03 +00001277 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +01001278
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001279 if (platform->remove)
1280 platform->remove(pdev);
1281
1282 if (codec_dev->remove)
1283 codec_dev->remove(pdev);
1284
Mark Brown87506542008-11-18 20:50:34 +00001285 for (i = 0; i < card->num_links; i++) {
1286 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001287 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001288 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001289 }
1290
Mark Brown87506542008-11-18 20:50:34 +00001291 if (card->remove)
1292 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001293
Mark Brownc5af3a22008-11-28 13:29:45 +00001294 snd_soc_unregister_card(card);
1295
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001296 return 0;
1297}
1298
Mark Brown416356f2009-06-30 19:05:15 +01001299static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001300{
Mark Brown416356f2009-06-30 19:05:15 +01001301 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001302 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1303 struct snd_soc_card *card = socdev->card;
1304
1305 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001306 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001307
1308 /* Flush out pmdown_time work - we actually do want to run it
1309 * now, we're shutting down so no imminent restart. */
1310 run_delayed_work(&card->delayed_work);
1311
1312 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001313
1314 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001315}
1316
Alexey Dobriyan47145212009-12-14 18:00:08 -08001317static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001318 .suspend = soc_suspend,
1319 .resume = soc_resume,
1320 .poweroff = soc_poweroff,
1321};
1322
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001323/* ASoC platform driver */
1324static struct platform_driver soc_driver = {
1325 .driver = {
1326 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001327 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001328 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001329 },
1330 .probe = soc_probe,
1331 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332};
1333
1334/* create a new pcm */
1335static int soc_new_pcm(struct snd_soc_device *socdev,
1336 struct snd_soc_dai_link *dai_link, int num)
1337{
Mark Brown87689d52008-12-02 16:01:14 +00001338 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001339 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001340 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001341 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1342 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001343 struct snd_soc_pcm_runtime *rtd;
1344 struct snd_pcm *pcm;
1345 char new_name[64];
1346 int ret = 0, playback = 0, capture = 0;
1347
1348 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1349 if (rtd == NULL)
1350 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001351
1352 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001353 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001354 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001355
1356 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001357 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1358 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001359
1360 if (codec_dai->playback.channels_min)
1361 playback = 1;
1362 if (codec_dai->capture.channels_min)
1363 capture = 1;
1364
1365 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1366 capture, &pcm);
1367 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001368 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1369 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001370 kfree(rtd);
1371 return ret;
1372 }
1373
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001374 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001375 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001376 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
Mark Brown87689d52008-12-02 16:01:14 +00001377 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1378 soc_pcm_ops.copy = platform->pcm_ops->copy;
1379 soc_pcm_ops.silence = platform->pcm_ops->silence;
1380 soc_pcm_ops.ack = platform->pcm_ops->ack;
1381 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001382
1383 if (playback)
1384 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1385
1386 if (capture)
1387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1388
Mark Brown87689d52008-12-02 16:01:14 +00001389 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001390 if (ret < 0) {
1391 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1392 kfree(rtd);
1393 return ret;
1394 }
1395
Mark Brown87689d52008-12-02 16:01:14 +00001396 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001397 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1398 cpu_dai->name);
1399 return ret;
1400}
1401
Mark Brown096e49d2009-07-05 15:12:22 +01001402/**
1403 * snd_soc_codec_volatile_register: Report if a register is volatile.
1404 *
1405 * @codec: CODEC to query.
1406 * @reg: Register to query.
1407 *
1408 * Boolean function indiciating if a CODEC register is volatile.
1409 */
1410int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1411{
1412 if (codec->volatile_register)
1413 return codec->volatile_register(reg);
1414 else
1415 return 0;
1416}
1417EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1418
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001419/**
1420 * snd_soc_new_ac97_codec - initailise AC97 device
1421 * @codec: audio codec
1422 * @ops: AC97 bus operations
1423 * @num: AC97 codec number
1424 *
1425 * Initialises AC97 codec resources for use by ad-hoc devices only.
1426 */
1427int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1428 struct snd_ac97_bus_ops *ops, int num)
1429{
1430 mutex_lock(&codec->mutex);
1431
1432 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1433 if (codec->ac97 == NULL) {
1434 mutex_unlock(&codec->mutex);
1435 return -ENOMEM;
1436 }
1437
1438 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1439 if (codec->ac97->bus == NULL) {
1440 kfree(codec->ac97);
1441 codec->ac97 = NULL;
1442 mutex_unlock(&codec->mutex);
1443 return -ENOMEM;
1444 }
1445
1446 codec->ac97->bus->ops = ops;
1447 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001448 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449 mutex_unlock(&codec->mutex);
1450 return 0;
1451}
1452EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1453
1454/**
1455 * snd_soc_free_ac97_codec - free AC97 codec device
1456 * @codec: audio codec
1457 *
1458 * Frees AC97 codec device resources.
1459 */
1460void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1461{
1462 mutex_lock(&codec->mutex);
1463 kfree(codec->ac97->bus);
1464 kfree(codec->ac97);
1465 codec->ac97 = NULL;
1466 mutex_unlock(&codec->mutex);
1467}
1468EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1469
1470/**
1471 * snd_soc_update_bits - update codec register bits
1472 * @codec: audio codec
1473 * @reg: codec register
1474 * @mask: register mask
1475 * @value: new value
1476 *
1477 * Writes new register value.
1478 *
1479 * Returns 1 for change else 0.
1480 */
1481int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001482 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001483{
1484 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001485 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001486
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001487 old = snd_soc_read(codec, reg);
1488 new = (old & ~mask) | value;
1489 change = old != new;
1490 if (change)
1491 snd_soc_write(codec, reg, new);
1492
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001493 return change;
1494}
1495EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1496
1497/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001498 * snd_soc_update_bits_locked - update codec register bits
1499 * @codec: audio codec
1500 * @reg: codec register
1501 * @mask: register mask
1502 * @value: new value
1503 *
1504 * Writes new register value, and takes the codec mutex.
1505 *
1506 * Returns 1 for change else 0.
1507 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001508int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1509 unsigned short reg, unsigned int mask,
1510 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001511{
1512 int change;
1513
1514 mutex_lock(&codec->mutex);
1515 change = snd_soc_update_bits(codec, reg, mask, value);
1516 mutex_unlock(&codec->mutex);
1517
1518 return change;
1519}
Mark Browndd1b3d52009-12-04 14:22:03 +00001520EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001521
1522/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001523 * snd_soc_test_bits - test register for change
1524 * @codec: audio codec
1525 * @reg: codec register
1526 * @mask: register mask
1527 * @value: new value
1528 *
1529 * Tests a register with a new value and checks if the new value is
1530 * different from the old value.
1531 *
1532 * Returns 1 for change else 0.
1533 */
1534int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001535 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536{
1537 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001538 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001540 old = snd_soc_read(codec, reg);
1541 new = (old & ~mask) | value;
1542 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543
1544 return change;
1545}
1546EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1547
1548/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549 * snd_soc_new_pcms - create new sound card and pcms
1550 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001551 * @idx: ALSA card index
1552 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001553 *
1554 * Create a new sound card based upon the codec and interface pcms.
1555 *
1556 * Returns 0 for success, else error.
1557 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001558int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001559{
Mark Brown87506542008-11-18 20:50:34 +00001560 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001561 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001562 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001563
1564 mutex_lock(&codec->mutex);
1565
1566 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001567 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1568 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001569 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1570 codec->name);
1571 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001572 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001573 }
1574
Mark Brown452c5ea2009-05-17 21:41:23 +01001575 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001576 codec->card->dev = socdev->dev;
1577 codec->card->private_data = codec;
1578 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1579
1580 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001581 for (i = 0; i < card->num_links; i++) {
1582 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583 if (ret < 0) {
1584 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001585 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001586 mutex_unlock(&codec->mutex);
1587 return ret;
1588 }
Barry Songf7732052009-11-12 12:01:47 +08001589 if (card->dai_link[i].codec_dai->ac97_control) {
1590 snd_ac97_dev_add_pdata(codec->ac97,
1591 card->dai_link[i].cpu_dai->ac97_pdata);
1592 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001593 }
1594
1595 mutex_unlock(&codec->mutex);
1596 return ret;
1597}
1598EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1599
1600/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001601 * snd_soc_free_pcms - free sound card and pcms
1602 * @socdev: the SoC audio device
1603 *
1604 * Frees sound card and pcms associated with the socdev.
1605 * Also unregister the codec if it is an AC97 device.
1606 */
1607void snd_soc_free_pcms(struct snd_soc_device *socdev)
1608{
Mark Brown6627a652009-01-23 22:55:23 +00001609 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001610#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001611 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001612 int i;
1613#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001614
1615 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001616 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001617#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001618 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001619 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001620 if (codec_dai->ac97_control && codec->ac97 &&
1621 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001622 soc_ac97_dev_unregister(codec);
1623 goto free_card;
1624 }
1625 }
1626free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627#endif
1628
1629 if (codec->card)
1630 snd_card_free(codec->card);
1631 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1632 mutex_unlock(&codec->mutex);
1633}
1634EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1635
1636/**
1637 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1638 * @substream: the pcm substream
1639 * @hw: the hardware parameters
1640 *
1641 * Sets the substream runtime hardware parameters.
1642 */
1643int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1644 const struct snd_pcm_hardware *hw)
1645{
1646 struct snd_pcm_runtime *runtime = substream->runtime;
1647 runtime->hw.info = hw->info;
1648 runtime->hw.formats = hw->formats;
1649 runtime->hw.period_bytes_min = hw->period_bytes_min;
1650 runtime->hw.period_bytes_max = hw->period_bytes_max;
1651 runtime->hw.periods_min = hw->periods_min;
1652 runtime->hw.periods_max = hw->periods_max;
1653 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1654 runtime->hw.fifo_size = hw->fifo_size;
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1658
1659/**
1660 * snd_soc_cnew - create new control
1661 * @_template: control template
1662 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001663 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001664 *
1665 * Create a new mixer control from a template control.
1666 *
1667 * Returns 0 for success, else error.
1668 */
1669struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1670 void *data, char *long_name)
1671{
1672 struct snd_kcontrol_new template;
1673
1674 memcpy(&template, _template, sizeof(template));
1675 if (long_name)
1676 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677 template.index = 0;
1678
1679 return snd_ctl_new1(&template, data);
1680}
1681EXPORT_SYMBOL_GPL(snd_soc_cnew);
1682
1683/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001684 * snd_soc_add_controls - add an array of controls to a codec.
1685 * Convienience function to add a list of controls. Many codecs were
1686 * duplicating this code.
1687 *
1688 * @codec: codec to add controls to
1689 * @controls: array of controls to add
1690 * @num_controls: number of elements in the array
1691 *
1692 * Return 0 for success, else error.
1693 */
1694int snd_soc_add_controls(struct snd_soc_codec *codec,
1695 const struct snd_kcontrol_new *controls, int num_controls)
1696{
1697 struct snd_card *card = codec->card;
1698 int err, i;
1699
1700 for (i = 0; i < num_controls; i++) {
1701 const struct snd_kcontrol_new *control = &controls[i];
1702 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1703 if (err < 0) {
1704 dev_err(codec->dev, "%s: Failed to add %s\n",
1705 codec->name, control->name);
1706 return err;
1707 }
1708 }
1709
1710 return 0;
1711}
1712EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1713
1714/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001715 * snd_soc_info_enum_double - enumerated double mixer info callback
1716 * @kcontrol: mixer control
1717 * @uinfo: control element information
1718 *
1719 * Callback to provide information about a double enumerated
1720 * mixer control.
1721 *
1722 * Returns 0 for success.
1723 */
1724int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_info *uinfo)
1726{
1727 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1728
1729 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1730 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001731 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001732
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001733 if (uinfo->value.enumerated.item > e->max - 1)
1734 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001735 strcpy(uinfo->value.enumerated.name,
1736 e->texts[uinfo->value.enumerated.item]);
1737 return 0;
1738}
1739EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1740
1741/**
1742 * snd_soc_get_enum_double - enumerated double mixer get callback
1743 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001744 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001745 *
1746 * Callback to get the value of a double enumerated mixer.
1747 *
1748 * Returns 0 for success.
1749 */
1750int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *ucontrol)
1752{
1753 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1754 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001755 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001756
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001757 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001758 ;
1759 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001760 ucontrol->value.enumerated.item[0]
1761 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001762 if (e->shift_l != e->shift_r)
1763 ucontrol->value.enumerated.item[1] =
1764 (val >> e->shift_r) & (bitmask - 1);
1765
1766 return 0;
1767}
1768EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1769
1770/**
1771 * snd_soc_put_enum_double - enumerated double mixer put callback
1772 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001773 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001774 *
1775 * Callback to set the value of a double enumerated mixer.
1776 *
1777 * Returns 0 for success.
1778 */
1779int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
1781{
1782 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1783 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001784 unsigned int val;
1785 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001786
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001787 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001789 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001790 return -EINVAL;
1791 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1792 mask = (bitmask - 1) << e->shift_l;
1793 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001794 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795 return -EINVAL;
1796 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1797 mask |= (bitmask - 1) << e->shift_r;
1798 }
1799
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001800 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001801}
1802EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1803
1804/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001805 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1806 * @kcontrol: mixer control
1807 * @ucontrol: control element information
1808 *
1809 * Callback to get the value of a double semi enumerated mixer.
1810 *
1811 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1812 * used for handling bitfield coded enumeration for example.
1813 *
1814 * Returns 0 for success.
1815 */
1816int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001820 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001821 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001822
1823 reg_val = snd_soc_read(codec, e->reg);
1824 val = (reg_val >> e->shift_l) & e->mask;
1825 for (mux = 0; mux < e->max; mux++) {
1826 if (val == e->values[mux])
1827 break;
1828 }
1829 ucontrol->value.enumerated.item[0] = mux;
1830 if (e->shift_l != e->shift_r) {
1831 val = (reg_val >> e->shift_r) & e->mask;
1832 for (mux = 0; mux < e->max; mux++) {
1833 if (val == e->values[mux])
1834 break;
1835 }
1836 ucontrol->value.enumerated.item[1] = mux;
1837 }
1838
1839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1842
1843/**
1844 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1845 * @kcontrol: mixer control
1846 * @ucontrol: control element information
1847 *
1848 * Callback to set the value of a double semi enumerated mixer.
1849 *
1850 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1851 * used for handling bitfield coded enumeration for example.
1852 *
1853 * Returns 0 for success.
1854 */
1855int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_value *ucontrol)
1857{
1858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001859 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001860 unsigned int val;
1861 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001862
1863 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1864 return -EINVAL;
1865 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1866 mask = e->mask << e->shift_l;
1867 if (e->shift_l != e->shift_r) {
1868 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1869 return -EINVAL;
1870 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1871 mask |= e->mask << e->shift_r;
1872 }
1873
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001874 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001875}
1876EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1877
1878/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001879 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1880 * @kcontrol: mixer control
1881 * @uinfo: control element information
1882 *
1883 * Callback to provide information about an external enumerated
1884 * single mixer.
1885 *
1886 * Returns 0 for success.
1887 */
1888int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_info *uinfo)
1890{
1891 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1892
1893 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1894 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001895 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001897 if (uinfo->value.enumerated.item > e->max - 1)
1898 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899 strcpy(uinfo->value.enumerated.name,
1900 e->texts[uinfo->value.enumerated.item]);
1901 return 0;
1902}
1903EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1904
1905/**
1906 * snd_soc_info_volsw_ext - external single mixer info callback
1907 * @kcontrol: mixer control
1908 * @uinfo: control element information
1909 *
1910 * Callback to provide information about a single external mixer control.
1911 *
1912 * Returns 0 for success.
1913 */
1914int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_info *uinfo)
1916{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001917 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001918
Mark Brownfd5dfad2009-04-15 21:37:46 +01001919 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001920 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1921 else
1922 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1923
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001924 uinfo->count = 1;
1925 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001926 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1930
1931/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932 * snd_soc_info_volsw - single mixer info callback
1933 * @kcontrol: mixer control
1934 * @uinfo: control element information
1935 *
1936 * Callback to provide information about a single mixer control.
1937 *
1938 * Returns 0 for success.
1939 */
1940int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1941 struct snd_ctl_elem_info *uinfo)
1942{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001943 struct soc_mixer_control *mc =
1944 (struct soc_mixer_control *)kcontrol->private_value;
1945 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001946 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001947 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001948
Mark Brownfd5dfad2009-04-15 21:37:46 +01001949 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001950 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1951 else
1952 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1953
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001954 uinfo->count = shift == rshift ? 1 : 2;
1955 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001956 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957 return 0;
1958}
1959EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1960
1961/**
1962 * snd_soc_get_volsw - single mixer get callback
1963 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001964 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965 *
1966 * Callback to get the value of a single mixer control.
1967 *
1968 * Returns 0 for success.
1969 */
1970int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1971 struct snd_ctl_elem_value *ucontrol)
1972{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001973 struct soc_mixer_control *mc =
1974 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001976 unsigned int reg = mc->reg;
1977 unsigned int shift = mc->shift;
1978 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001979 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001980 unsigned int mask = (1 << fls(max)) - 1;
1981 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982
1983 ucontrol->value.integer.value[0] =
1984 (snd_soc_read(codec, reg) >> shift) & mask;
1985 if (shift != rshift)
1986 ucontrol->value.integer.value[1] =
1987 (snd_soc_read(codec, reg) >> rshift) & mask;
1988 if (invert) {
1989 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001990 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991 if (shift != rshift)
1992 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001993 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001994 }
1995
1996 return 0;
1997}
1998EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1999
2000/**
2001 * snd_soc_put_volsw - single mixer put callback
2002 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002003 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004 *
2005 * Callback to set the value of a single mixer control.
2006 *
2007 * Returns 0 for success.
2008 */
2009int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2011{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002012 struct soc_mixer_control *mc =
2013 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002015 unsigned int reg = mc->reg;
2016 unsigned int shift = mc->shift;
2017 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002018 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002019 unsigned int mask = (1 << fls(max)) - 1;
2020 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002021 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022
2023 val = (ucontrol->value.integer.value[0] & mask);
2024 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002025 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 val_mask = mask << shift;
2027 val = val << shift;
2028 if (shift != rshift) {
2029 val2 = (ucontrol->value.integer.value[1] & mask);
2030 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002031 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002032 val_mask |= mask << rshift;
2033 val |= val2 << rshift;
2034 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002035 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036}
2037EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2038
2039/**
2040 * snd_soc_info_volsw_2r - double mixer info callback
2041 * @kcontrol: mixer control
2042 * @uinfo: control element information
2043 *
2044 * Callback to provide information about a double mixer control that
2045 * spans 2 codec registers.
2046 *
2047 * Returns 0 for success.
2048 */
2049int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_info *uinfo)
2051{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002052 struct soc_mixer_control *mc =
2053 (struct soc_mixer_control *)kcontrol->private_value;
2054 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002055
Mark Brownfd5dfad2009-04-15 21:37:46 +01002056 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002057 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2058 else
2059 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2060
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 uinfo->count = 2;
2062 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002063 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002064 return 0;
2065}
2066EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2067
2068/**
2069 * snd_soc_get_volsw_2r - double mixer get callback
2070 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002071 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002072 *
2073 * Callback to get the value of a double mixer control that spans 2 registers.
2074 *
2075 * Returns 0 for success.
2076 */
2077int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2079{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002080 struct soc_mixer_control *mc =
2081 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002083 unsigned int reg = mc->reg;
2084 unsigned int reg2 = mc->rreg;
2085 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002086 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002087 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002088 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002089
2090 ucontrol->value.integer.value[0] =
2091 (snd_soc_read(codec, reg) >> shift) & mask;
2092 ucontrol->value.integer.value[1] =
2093 (snd_soc_read(codec, reg2) >> shift) & mask;
2094 if (invert) {
2095 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002096 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002098 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 }
2100
2101 return 0;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2104
2105/**
2106 * snd_soc_put_volsw_2r - double mixer set callback
2107 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002108 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 *
2110 * Callback to set the value of a double mixer control that spans 2 registers.
2111 *
2112 * Returns 0 for success.
2113 */
2114int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2116{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002117 struct soc_mixer_control *mc =
2118 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002120 unsigned int reg = mc->reg;
2121 unsigned int reg2 = mc->rreg;
2122 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002123 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002124 unsigned int mask = (1 << fls(max)) - 1;
2125 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002127 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128
2129 val_mask = mask << shift;
2130 val = (ucontrol->value.integer.value[0] & mask);
2131 val2 = (ucontrol->value.integer.value[1] & mask);
2132
2133 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002134 val = max - val;
2135 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136 }
2137
2138 val = val << shift;
2139 val2 = val2 << shift;
2140
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002141 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002142 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002143 return err;
2144
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002145 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146 return err;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2149
Mark Browne13ac2e2008-05-28 17:58:05 +01002150/**
2151 * snd_soc_info_volsw_s8 - signed mixer info callback
2152 * @kcontrol: mixer control
2153 * @uinfo: control element information
2154 *
2155 * Callback to provide information about a signed mixer control.
2156 *
2157 * Returns 0 for success.
2158 */
2159int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_info *uinfo)
2161{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002162 struct soc_mixer_control *mc =
2163 (struct soc_mixer_control *)kcontrol->private_value;
2164 int max = mc->max;
2165 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002166
2167 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2168 uinfo->count = 2;
2169 uinfo->value.integer.min = 0;
2170 uinfo->value.integer.max = max-min;
2171 return 0;
2172}
2173EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2174
2175/**
2176 * snd_soc_get_volsw_s8 - signed mixer get callback
2177 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002178 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002179 *
2180 * Callback to get the value of a signed mixer control.
2181 *
2182 * Returns 0 for success.
2183 */
2184int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002187 struct soc_mixer_control *mc =
2188 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002189 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002190 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002191 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002192 int val = snd_soc_read(codec, reg);
2193
2194 ucontrol->value.integer.value[0] =
2195 ((signed char)(val & 0xff))-min;
2196 ucontrol->value.integer.value[1] =
2197 ((signed char)((val >> 8) & 0xff))-min;
2198 return 0;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2201
2202/**
2203 * snd_soc_put_volsw_sgn - signed mixer put callback
2204 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002205 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002206 *
2207 * Callback to set the value of a signed mixer control.
2208 *
2209 * Returns 0 for success.
2210 */
2211int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2213{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002214 struct soc_mixer_control *mc =
2215 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002217 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002218 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002219 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002220
2221 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2222 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2223
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002224 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002225}
2226EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2227
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002228/**
2229 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2230 * @dai: DAI
2231 * @clk_id: DAI specific clock ID
2232 * @freq: new clock frequency in Hz
2233 * @dir: new clock direction - input/output.
2234 *
2235 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2236 */
2237int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2238 unsigned int freq, int dir)
2239{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002240 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002241 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002242 else
2243 return -EINVAL;
2244}
2245EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2246
2247/**
2248 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2249 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002250 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002251 * @div: new clock divisor.
2252 *
2253 * Configures the clock dividers. This is used to derive the best DAI bit and
2254 * frame clocks from the system or master clock. It's best to set the DAI bit
2255 * and frame clocks as low as possible to save system power.
2256 */
2257int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2258 int div_id, int div)
2259{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002260 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002261 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002262 else
2263 return -EINVAL;
2264}
2265EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2266
2267/**
2268 * snd_soc_dai_set_pll - configure DAI PLL.
2269 * @dai: DAI
2270 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002271 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002272 * @freq_in: PLL input clock frequency in Hz
2273 * @freq_out: requested PLL output clock frequency in Hz
2274 *
2275 * Configures and enables PLL to generate output clock based on input clock.
2276 */
Mark Brown85488032009-09-05 18:52:16 +01002277int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2278 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002279{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002280 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002281 return dai->ops->set_pll(dai, pll_id, source,
2282 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002283 else
2284 return -EINVAL;
2285}
2286EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2287
2288/**
2289 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2290 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002291 * @fmt: SND_SOC_DAIFMT_ format value.
2292 *
2293 * Configures the DAI hardware format and clocking.
2294 */
2295int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2296{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002297 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002298 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002299 else
2300 return -EINVAL;
2301}
2302EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2303
2304/**
2305 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2306 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002307 * @tx_mask: bitmask representing active TX slots.
2308 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002309 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002310 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002311 *
2312 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2313 * specific.
2314 */
2315int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002316 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002317{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002318 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002319 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2320 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002321 else
2322 return -EINVAL;
2323}
2324EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2325
2326/**
Barry Song472df3c2009-09-12 01:16:29 +08002327 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2328 * @dai: DAI
2329 * @tx_num: how many TX channels
2330 * @tx_slot: pointer to an array which imply the TX slot number channel
2331 * 0~num-1 uses
2332 * @rx_num: how many RX channels
2333 * @rx_slot: pointer to an array which imply the RX slot number channel
2334 * 0~num-1 uses
2335 *
2336 * configure the relationship between channel number and TDM slot number.
2337 */
2338int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2339 unsigned int tx_num, unsigned int *tx_slot,
2340 unsigned int rx_num, unsigned int *rx_slot)
2341{
2342 if (dai->ops && dai->ops->set_channel_map)
2343 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2344 rx_num, rx_slot);
2345 else
2346 return -EINVAL;
2347}
2348EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2349
2350/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002351 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2352 * @dai: DAI
2353 * @tristate: tristate enable
2354 *
2355 * Tristates the DAI so that others can use it.
2356 */
2357int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2358{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002359 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002360 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002361 else
2362 return -EINVAL;
2363}
2364EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2365
2366/**
2367 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2368 * @dai: DAI
2369 * @mute: mute enable
2370 *
2371 * Mutes the DAI DAC.
2372 */
2373int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2374{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002375 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002376 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002377 else
2378 return -EINVAL;
2379}
2380EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2381
Mark Brownc5af3a22008-11-28 13:29:45 +00002382/**
2383 * snd_soc_register_card - Register a card with the ASoC core
2384 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002385 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002386 *
2387 * Note that currently this is an internal only function: it will be
2388 * exposed to machine drivers after further backporting of ASoC v2
2389 * registration APIs.
2390 */
2391static int snd_soc_register_card(struct snd_soc_card *card)
2392{
2393 if (!card->name || !card->dev)
2394 return -EINVAL;
2395
2396 INIT_LIST_HEAD(&card->list);
2397 card->instantiated = 0;
2398
2399 mutex_lock(&client_mutex);
2400 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002401 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002402 mutex_unlock(&client_mutex);
2403
2404 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2405
2406 return 0;
2407}
2408
2409/**
2410 * snd_soc_unregister_card - Unregister a card with the ASoC core
2411 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002412 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002413 *
2414 * Note that currently this is an internal only function: it will be
2415 * exposed to machine drivers after further backporting of ASoC v2
2416 * registration APIs.
2417 */
2418static int snd_soc_unregister_card(struct snd_soc_card *card)
2419{
2420 mutex_lock(&client_mutex);
2421 list_del(&card->list);
2422 mutex_unlock(&client_mutex);
2423
2424 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2425
2426 return 0;
2427}
2428
Mark Brown91151712008-11-30 23:31:24 +00002429/**
2430 * snd_soc_register_dai - Register a DAI with the ASoC core
2431 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002432 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002433 */
2434int snd_soc_register_dai(struct snd_soc_dai *dai)
2435{
2436 if (!dai->name)
2437 return -EINVAL;
2438
2439 /* The device should become mandatory over time */
2440 if (!dai->dev)
2441 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2442
Eric Miao6335d052009-03-03 09:41:00 +08002443 if (!dai->ops)
2444 dai->ops = &null_dai_ops;
2445
Mark Brown91151712008-11-30 23:31:24 +00002446 INIT_LIST_HEAD(&dai->list);
2447
2448 mutex_lock(&client_mutex);
2449 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002450 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002451 mutex_unlock(&client_mutex);
2452
2453 pr_debug("Registered DAI '%s'\n", dai->name);
2454
2455 return 0;
2456}
2457EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2458
2459/**
2460 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2461 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002462 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002463 */
2464void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2465{
2466 mutex_lock(&client_mutex);
2467 list_del(&dai->list);
2468 mutex_unlock(&client_mutex);
2469
2470 pr_debug("Unregistered DAI '%s'\n", dai->name);
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2473
2474/**
2475 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2476 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002477 * @dai: Array of DAIs to register
2478 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002479 */
2480int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2481{
2482 int i, ret;
2483
2484 for (i = 0; i < count; i++) {
2485 ret = snd_soc_register_dai(&dai[i]);
2486 if (ret != 0)
2487 goto err;
2488 }
2489
2490 return 0;
2491
2492err:
2493 for (i--; i >= 0; i--)
2494 snd_soc_unregister_dai(&dai[i]);
2495
2496 return ret;
2497}
2498EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2499
2500/**
2501 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2502 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002503 * @dai: Array of DAIs to unregister
2504 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002505 */
2506void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2507{
2508 int i;
2509
2510 for (i = 0; i < count; i++)
2511 snd_soc_unregister_dai(&dai[i]);
2512}
2513EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2514
Mark Brown12a48a8c2008-12-03 19:40:30 +00002515/**
2516 * snd_soc_register_platform - Register a platform with the ASoC core
2517 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002518 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002519 */
2520int snd_soc_register_platform(struct snd_soc_platform *platform)
2521{
2522 if (!platform->name)
2523 return -EINVAL;
2524
2525 INIT_LIST_HEAD(&platform->list);
2526
2527 mutex_lock(&client_mutex);
2528 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002529 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002530 mutex_unlock(&client_mutex);
2531
2532 pr_debug("Registered platform '%s'\n", platform->name);
2533
2534 return 0;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2537
2538/**
2539 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2540 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002541 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002542 */
2543void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2544{
2545 mutex_lock(&client_mutex);
2546 list_del(&platform->list);
2547 mutex_unlock(&client_mutex);
2548
2549 pr_debug("Unregistered platform '%s'\n", platform->name);
2550}
2551EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2552
Mark Brown151ab222009-05-09 16:22:58 +01002553static u64 codec_format_map[] = {
2554 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2555 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2556 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2557 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2558 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2559 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2560 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2561 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2562 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2563 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2564 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2565 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2566 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2567 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2568 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2569 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2570};
2571
2572/* Fix up the DAI formats for endianness: codecs don't actually see
2573 * the endianness of the data but we're using the CPU format
2574 * definitions which do need to include endianness so we ensure that
2575 * codec DAIs always have both big and little endian variants set.
2576 */
2577static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2578{
2579 int i;
2580
2581 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2582 if (stream->formats & codec_format_map[i])
2583 stream->formats |= codec_format_map[i];
2584}
2585
Mark Brown0d0cf002008-12-10 14:32:45 +00002586/**
2587 * snd_soc_register_codec - Register a codec with the ASoC core
2588 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002589 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002590 */
2591int snd_soc_register_codec(struct snd_soc_codec *codec)
2592{
Mark Brown151ab222009-05-09 16:22:58 +01002593 int i;
2594
Mark Brown0d0cf002008-12-10 14:32:45 +00002595 if (!codec->name)
2596 return -EINVAL;
2597
2598 /* The device should become mandatory over time */
2599 if (!codec->dev)
2600 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2601
2602 INIT_LIST_HEAD(&codec->list);
2603
Mark Brown151ab222009-05-09 16:22:58 +01002604 for (i = 0; i < codec->num_dai; i++) {
2605 fixup_codec_formats(&codec->dai[i].playback);
2606 fixup_codec_formats(&codec->dai[i].capture);
2607 }
2608
Mark Brown0d0cf002008-12-10 14:32:45 +00002609 mutex_lock(&client_mutex);
2610 list_add(&codec->list, &codec_list);
2611 snd_soc_instantiate_cards();
2612 mutex_unlock(&client_mutex);
2613
2614 pr_debug("Registered codec '%s'\n", codec->name);
2615
2616 return 0;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2619
2620/**
2621 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2622 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002623 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002624 */
2625void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2626{
2627 mutex_lock(&client_mutex);
2628 list_del(&codec->list);
2629 mutex_unlock(&client_mutex);
2630
2631 pr_debug("Unregistered codec '%s'\n", codec->name);
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2634
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002635static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636{
Mark Brown384c89e2008-12-03 17:34:03 +00002637#ifdef CONFIG_DEBUG_FS
2638 debugfs_root = debugfs_create_dir("asoc", NULL);
2639 if (IS_ERR(debugfs_root) || !debugfs_root) {
2640 printk(KERN_WARNING
2641 "ASoC: Failed to create debugfs directory\n");
2642 debugfs_root = NULL;
2643 }
2644#endif
2645
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 return platform_driver_register(&soc_driver);
2647}
2648
Mark Brown7d8c16a2008-11-30 22:11:24 +00002649static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650{
Mark Brown384c89e2008-12-03 17:34:03 +00002651#ifdef CONFIG_DEBUG_FS
2652 debugfs_remove_recursive(debugfs_root);
2653#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002654 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002655}
2656
2657module_init(snd_soc_init);
2658module_exit(snd_soc_exit);
2659
2660/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002661MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002662MODULE_DESCRIPTION("ALSA SoC Core");
2663MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002664MODULE_ALIAS("platform:soc-audio");