blob: 7b4179eca8937887d0f503217920ff57505100d5 [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);
430 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200431 }
432 if (!runtime->hw.formats) {
433 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100434 codec_dai->name, cpu_dai->name);
435 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200436 }
437 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
438 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100439 codec_dai->name, cpu_dai->name);
440 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200441 }
442
Mark Brown06f409d2009-04-07 18:10:13 +0100443 /* Symmetry only applies if we've already got an active stream. */
444 if (cpu_dai->active || codec_dai->active) {
445 ret = soc_pcm_apply_symmetry(substream);
446 if (ret != 0)
447 goto machine_err;
448 }
449
Mark Brownf24368c2008-10-21 21:45:08 +0100450 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
451 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
452 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
453 runtime->hw.channels_max);
454 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
455 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200456
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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100470machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 if (machine->ops && machine->ops->shutdown)
472 machine->ops->shutdown(substream);
473
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100474codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 if (platform->pcm_ops->close)
476 platform->pcm_ops->close(substream);
477
478platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800479 if (cpu_dai->ops->shutdown)
480 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481out:
482 mutex_unlock(&pcm_mutex);
483 return ret;
484}
485
486/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200487 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488 * This is to ensure there are no pops or clicks in between any music tracks
489 * due to DAPM power cycling.
490 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100491static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492{
Mark Brown63084192008-12-02 15:08:03 +0000493 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
494 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000495 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100496 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497 int i;
498
499 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200500 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501 codec_dai = &codec->dai[i];
502
Mark Brownf24368c2008-10-21 21:45:08 +0100503 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
504 codec_dai->playback.stream_name,
505 codec_dai->playback.active ? "active" : "inactive",
506 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507
508 /* are we waiting on this codec DAI stream */
509 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100511 snd_soc_dapm_stream_event(codec,
512 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 }
515 }
516 mutex_unlock(&pcm_mutex);
517}
518
519/*
520 * Called by ALSA when a PCM substream is closed. Private data can be
521 * freed here. The cpu DAI, codec DAI, machine and platform are also
522 * shutdown.
523 */
524static int soc_codec_close(struct snd_pcm_substream *substream)
525{
526 struct snd_soc_pcm_runtime *rtd = substream->private_data;
527 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000528 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100529 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000530 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100531 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
532 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000533 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534
535 mutex_lock(&pcm_mutex);
536
Jassi Brar14dc5732010-02-26 09:12:32 +0900537 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
538 cpu_dai->playback.active--;
539 codec_dai->playback.active--;
540 } else {
541 cpu_dai->capture.active--;
542 codec_dai->capture.active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900544
545 cpu_dai->active--;
546 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 codec->active--;
548
Mark Brown6010b2d2008-09-06 18:33:24 +0100549 /* Muting the DAC suppresses artifacts caused during digital
550 * shutdown, for example from stopping clocks.
551 */
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
553 snd_soc_dai_digital_mute(codec_dai, 1);
554
Eric Miao6335d052009-03-03 09:41:00 +0800555 if (cpu_dai->ops->shutdown)
556 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557
Eric Miao6335d052009-03-03 09:41:00 +0800558 if (codec_dai->ops->shutdown)
559 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560
561 if (machine->ops && machine->ops->shutdown)
562 machine->ops->shutdown(substream);
563
564 if (platform->pcm_ops->close)
565 platform->pcm_ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200566
567 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
568 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100569 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000570 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000571 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 } else {
573 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100574 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100575 codec_dai->capture.stream_name,
576 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577 }
578
579 mutex_unlock(&pcm_mutex);
580 return 0;
581}
582
583/*
584 * Called by ALSA when the PCM substream is prepared, can set format, sample
585 * rate, etc. This function is non atomic and can be called multiple times,
586 * it can refer to the runtime info.
587 */
588static int soc_pcm_prepare(struct snd_pcm_substream *substream)
589{
590 struct snd_soc_pcm_runtime *rtd = substream->private_data;
591 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000592 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100593 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000594 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100595 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
596 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000597 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598 int ret = 0;
599
600 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100601
602 if (machine->ops && machine->ops->prepare) {
603 ret = machine->ops->prepare(substream);
604 if (ret < 0) {
605 printk(KERN_ERR "asoc: machine prepare error\n");
606 goto out;
607 }
608 }
609
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200610 if (platform->pcm_ops->prepare) {
611 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200612 if (ret < 0) {
613 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200614 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200615 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200616 }
617
Eric Miao6335d052009-03-03 09:41:00 +0800618 if (codec_dai->ops->prepare) {
619 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200620 if (ret < 0) {
621 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200623 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624 }
625
Eric Miao6335d052009-03-03 09:41:00 +0800626 if (cpu_dai->ops->prepare) {
627 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100628 if (ret < 0) {
629 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
630 goto out;
631 }
632 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633
Mark Brownd45f6212008-10-14 13:58:36 +0100634 /* cancel any delayed stream shutdown that is pending */
635 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
636 codec_dai->pop_wait) {
637 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000638 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100639 }
640
Mark Brown452c5ea2009-05-17 21:41:23 +0100641 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
642 snd_soc_dapm_stream_event(codec,
643 codec_dai->playback.stream_name,
644 SND_SOC_DAPM_STREAM_START);
645 else
646 snd_soc_dapm_stream_event(codec,
647 codec_dai->capture.stream_name,
648 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100649
Mark Brown452c5ea2009-05-17 21:41:23 +0100650 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200651
652out:
653 mutex_unlock(&pcm_mutex);
654 return ret;
655}
656
657/*
658 * Called by ALSA when the hardware params are set by application. This
659 * function can also be called multiple times and can allocate buffers
660 * (using snd_pcm_lib_* ). It's non-atomic.
661 */
662static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
663 struct snd_pcm_hw_params *params)
664{
665 struct snd_soc_pcm_runtime *rtd = substream->private_data;
666 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100667 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000668 struct snd_soc_card *card = socdev->card;
669 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100670 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
671 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200672 int ret = 0;
673
674 mutex_lock(&pcm_mutex);
675
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100676 if (machine->ops && machine->ops->hw_params) {
677 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100679 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680 goto out;
681 }
682 }
683
Eric Miao6335d052009-03-03 09:41:00 +0800684 if (codec_dai->ops->hw_params) {
685 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100686 if (ret < 0) {
687 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
688 codec_dai->name);
689 goto codec_err;
690 }
691 }
692
Eric Miao6335d052009-03-03 09:41:00 +0800693 if (cpu_dai->ops->hw_params) {
694 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200696 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100697 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698 goto interface_err;
699 }
700 }
701
702 if (platform->pcm_ops->hw_params) {
703 ret = platform->pcm_ops->hw_params(substream, params);
704 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200705 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706 platform->name);
707 goto platform_err;
708 }
709 }
710
Mark Brown06f409d2009-04-07 18:10:13 +0100711 machine->rate = params_rate(params);
712
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713out:
714 mutex_unlock(&pcm_mutex);
715 return ret;
716
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800718 if (cpu_dai->ops->hw_free)
719 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
721interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800722 if (codec_dai->ops->hw_free)
723 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100724
725codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200726 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100727 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728
729 mutex_unlock(&pcm_mutex);
730 return ret;
731}
732
733/*
734 * Free's resources allocated by hw_params, can be called multiple times
735 */
736static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
737{
738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
739 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100740 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000741 struct snd_soc_card *card = socdev->card;
742 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100743 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
744 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000745 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746
747 mutex_lock(&pcm_mutex);
748
749 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100750 if (!codec->active)
751 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752
753 /* free any machine hw params */
754 if (machine->ops && machine->ops->hw_free)
755 machine->ops->hw_free(substream);
756
757 /* free any DMA resources */
758 if (platform->pcm_ops->hw_free)
759 platform->pcm_ops->hw_free(substream);
760
761 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800762 if (codec_dai->ops->hw_free)
763 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764
Eric Miao6335d052009-03-03 09:41:00 +0800765 if (cpu_dai->ops->hw_free)
766 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767
768 mutex_unlock(&pcm_mutex);
769 return 0;
770}
771
772static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
773{
774 struct snd_soc_pcm_runtime *rtd = substream->private_data;
775 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000776 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100777 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000778 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100779 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
780 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781 int ret;
782
Eric Miao6335d052009-03-03 09:41:00 +0800783 if (codec_dai->ops->trigger) {
784 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785 if (ret < 0)
786 return ret;
787 }
788
789 if (platform->pcm_ops->trigger) {
790 ret = platform->pcm_ops->trigger(substream, cmd);
791 if (ret < 0)
792 return ret;
793 }
794
Eric Miao6335d052009-03-03 09:41:00 +0800795 if (cpu_dai->ops->trigger) {
796 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797 if (ret < 0)
798 return ret;
799 }
800 return 0;
801}
802
803/* ASoC PCM operations */
804static struct snd_pcm_ops soc_pcm_ops = {
805 .open = soc_pcm_open,
806 .close = soc_codec_close,
807 .hw_params = soc_pcm_hw_params,
808 .hw_free = soc_pcm_hw_free,
809 .prepare = soc_pcm_prepare,
810 .trigger = soc_pcm_trigger,
811};
812
813#ifdef CONFIG_PM
814/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100815static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200816{
Mark Brown416356f2009-06-30 19:05:15 +0100817 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200818 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000819 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000820 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200821 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000822 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200823 int i;
824
Daniel Macke3509ff2009-06-03 17:44:49 +0200825 /* If the initialization of this soc device failed, there is no codec
826 * associated with it. Just bail out in this case.
827 */
828 if (!codec)
829 return 0;
830
Andy Green6ed25972008-06-13 16:24:05 +0100831 /* Due to the resume being scheduled into a workqueue we could
832 * suspend before that's finished - wait for it to complete.
833 */
834 snd_power_lock(codec->card);
835 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
836 snd_power_unlock(codec->card);
837
838 /* we're going to block userspace touching us until resume completes */
839 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
840
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000842 for (i = 0; i < card->num_links; i++) {
843 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800844 if (dai->ops->digital_mute && dai->playback.active)
845 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 }
847
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100848 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000849 for (i = 0; i < card->num_links; i++)
850 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100851
Mark Brown87506542008-11-18 20:50:34 +0000852 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100853 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854
Mark Brown87506542008-11-18 20:50:34 +0000855 for (i = 0; i < card->num_links; i++) {
856 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000857 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000858 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900860 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 }
862
863 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000864 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200865 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866
Mark Brown3ff3f642008-05-19 12:32:25 +0200867 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868 char *stream = codec->dai[i].playback.stream_name;
869 if (stream != NULL)
870 snd_soc_dapm_stream_event(codec, stream,
871 SND_SOC_DAPM_STREAM_SUSPEND);
872 stream = codec->dai[i].capture.stream_name;
873 if (stream != NULL)
874 snd_soc_dapm_stream_event(codec, stream,
875 SND_SOC_DAPM_STREAM_SUSPEND);
876 }
877
878 if (codec_dev->suspend)
Mark Brown416356f2009-06-30 19:05:15 +0100879 codec_dev->suspend(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200880
Mark Brown87506542008-11-18 20:50:34 +0000881 for (i = 0; i < card->num_links; i++) {
882 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000883 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000884 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885 }
886
Mark Brown87506542008-11-18 20:50:34 +0000887 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100888 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889
890 return 0;
891}
892
Andy Green6ed25972008-06-13 16:24:05 +0100893/* deferred resume work, so resume can complete before we finished
894 * setting our codec back up, which can be very slow on I2C
895 */
896static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897{
Mark Brown63084192008-12-02 15:08:03 +0000898 struct snd_soc_card *card = container_of(work,
899 struct snd_soc_card,
900 deferred_resume_work);
901 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000902 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200903 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000904 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100905 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 int i;
907
Andy Green6ed25972008-06-13 16:24:05 +0100908 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
909 * so userspace apps are blocked from touching us
910 */
911
Mark Brownfde22f22008-11-24 18:08:18 +0000912 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100913
Mark Brown87506542008-11-18 20:50:34 +0000914 if (card->resume_pre)
915 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916
Mark Brown87506542008-11-18 20:50:34 +0000917 for (i = 0; i < card->num_links; i++) {
918 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000919 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000920 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200921 }
922
923 if (codec_dev->resume)
924 codec_dev->resume(pdev);
925
Mark Brown3ff3f642008-05-19 12:32:25 +0200926 for (i = 0; i < codec->num_dai; i++) {
927 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200928 if (stream != NULL)
929 snd_soc_dapm_stream_event(codec, stream,
930 SND_SOC_DAPM_STREAM_RESUME);
931 stream = codec->dai[i].capture.stream_name;
932 if (stream != NULL)
933 snd_soc_dapm_stream_event(codec, stream,
934 SND_SOC_DAPM_STREAM_RESUME);
935 }
936
Mark Brown3ff3f642008-05-19 12:32:25 +0200937 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000938 for (i = 0; i < card->num_links; i++) {
939 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800940 if (dai->ops->digital_mute && dai->playback.active)
941 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200942 }
943
Mark Brown87506542008-11-18 20:50:34 +0000944 for (i = 0; i < card->num_links; i++) {
945 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000946 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000947 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200948 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +0900949 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950 }
951
Mark Brown87506542008-11-18 20:50:34 +0000952 if (card->resume_post)
953 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200954
Mark Brownfde22f22008-11-24 18:08:18 +0000955 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100956
957 /* userspace can access us now we are back as we were before */
958 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
959}
960
961/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100962static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100963{
Mark Brown416356f2009-06-30 19:05:15 +0100964 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +0100965 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000966 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +0100967 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +0100968
Mark Brown64ab9ba2009-03-31 11:27:03 +0100969 /* AC97 devices might have other drivers hanging off them so
970 * need to resume immediately. Other drivers don't have that
971 * problem and may take a substantial amount of time to resume
972 * due to I/O costs and anti-pop so handle them out of line.
973 */
974 if (cpu_dai->ac97_control) {
975 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
976 soc_resume_deferred(&card->deferred_resume_work);
977 } else {
978 dev_dbg(socdev->dev, "Scheduling resume work\n");
979 if (!schedule_work(&card->deferred_resume_work))
980 dev_err(socdev->dev, "resume work item may be lost\n");
981 }
Andy Green6ed25972008-06-13 16:24:05 +0100982
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200983 return 0;
984}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200985#else
986#define soc_suspend NULL
987#define soc_resume NULL
988#endif
989
Barry Song02a06d32009-10-16 18:13:38 +0800990static struct snd_soc_dai_ops null_dai_ops = {
991};
992
Mark Brown435c5e22008-12-04 15:32:53 +0000993static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200994{
Mark Brown435c5e22008-12-04 15:32:53 +0000995 struct platform_device *pdev = container_of(card->dev,
996 struct platform_device,
997 dev);
998 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +0000999 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001000 struct snd_soc_platform *platform;
1001 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001002 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001003
Mark Brown435c5e22008-12-04 15:32:53 +00001004 if (card->instantiated)
1005 return;
Mark Brown63084192008-12-02 15:08:03 +00001006
Mark Brown435c5e22008-12-04 15:32:53 +00001007 found = 0;
1008 list_for_each_entry(platform, &platform_list, list)
1009 if (card->platform == platform) {
1010 found = 1;
1011 break;
1012 }
1013 if (!found) {
1014 dev_dbg(card->dev, "Platform %s not registered\n",
1015 card->platform->name);
1016 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001017 }
1018
Mark Brown6b05eda2008-12-08 19:26:48 +00001019 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001020 for (i = 0; i < card->num_links; i++) {
1021 found = 0;
1022 list_for_each_entry(dai, &dai_list, list)
1023 if (card->dai_link[i].cpu_dai == dai) {
1024 found = 1;
1025 break;
1026 }
1027 if (!found) {
1028 dev_dbg(card->dev, "DAI %s not registered\n",
1029 card->dai_link[i].cpu_dai->name);
1030 return;
1031 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001032
1033 if (card->dai_link[i].cpu_dai->ac97_control)
1034 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001035 }
1036
Barry Song02a06d32009-10-16 18:13:38 +08001037 for (i = 0; i < card->num_links; i++) {
1038 if (!card->dai_link[i].codec_dai->ops)
1039 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1040 }
1041
Mark Brown6b05eda2008-12-08 19:26:48 +00001042 /* If we have AC97 in the system then don't wait for the
1043 * codec. This will need revisiting if we have to handle
1044 * systems with mixed AC97 and non-AC97 parts. Only check for
1045 * DAIs currently; we can't do this per link since some AC97
1046 * codecs have non-AC97 DAIs.
1047 */
1048 if (!ac97)
1049 for (i = 0; i < card->num_links; i++) {
1050 found = 0;
1051 list_for_each_entry(dai, &dai_list, list)
1052 if (card->dai_link[i].codec_dai == dai) {
1053 found = 1;
1054 break;
1055 }
1056 if (!found) {
1057 dev_dbg(card->dev, "DAI %s not registered\n",
1058 card->dai_link[i].codec_dai->name);
1059 return;
1060 }
1061 }
1062
Mark Brown435c5e22008-12-04 15:32:53 +00001063 /* Note that we do not current check for codec components */
1064
1065 dev_dbg(card->dev, "All components present, instantiating\n");
1066
1067 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001068 card->pmdown_time = pmdown_time;
1069
Mark Brown87506542008-11-18 20:50:34 +00001070 if (card->probe) {
1071 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001072 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001073 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001074 }
1075
Mark Brown87506542008-11-18 20:50:34 +00001076 for (i = 0; i < card->num_links; i++) {
1077 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001078 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001079 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001080 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001081 goto cpu_dai_err;
1082 }
1083 }
1084
1085 if (codec_dev->probe) {
1086 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001087 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001088 goto cpu_dai_err;
1089 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001090 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001091
1092 if (platform->probe) {
1093 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001094 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095 goto platform_err;
1096 }
1097
1098 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001099 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001100#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001101 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001102 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001103#endif
Andy Green6ed25972008-06-13 16:24:05 +01001104
Mark Brownfe3e78e2009-11-03 22:13:13 +00001105 for (i = 0; i < card->num_links; i++) {
1106 if (card->dai_link[i].init) {
1107 ret = card->dai_link[i].init(codec);
1108 if (ret < 0) {
1109 printk(KERN_ERR "asoc: failed to init %s\n",
1110 card->dai_link[i].stream_name);
1111 continue;
1112 }
1113 }
Barry Songf7732052009-11-12 12:01:47 +08001114 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001115 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001116 }
1117
1118 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1119 "%s", card->name);
1120 snprintf(codec->card->longname, sizeof(codec->card->longname),
1121 "%s (%s)", card->name, codec->name);
1122
1123 /* Make sure all DAPM widgets are instantiated */
1124 snd_soc_dapm_new_widgets(codec);
1125
1126 ret = snd_card_register(codec->card);
1127 if (ret < 0) {
1128 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1129 codec->name);
1130 goto card_err;
1131 }
1132
1133 mutex_lock(&codec->mutex);
1134#ifdef CONFIG_SND_SOC_AC97_BUS
1135 /* Only instantiate AC97 if not already done by the adaptor
1136 * for the generic AC97 subsystem.
1137 */
1138 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1139 ret = soc_ac97_dev_register(codec);
1140 if (ret < 0) {
1141 printk(KERN_ERR "asoc: AC97 device register failed\n");
1142 snd_card_free(codec->card);
1143 mutex_unlock(&codec->mutex);
1144 goto card_err;
1145 }
1146 }
1147#endif
1148
1149 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1150 if (ret < 0)
1151 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1152
Mark Browndbe21402010-02-12 11:37:24 +00001153 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1154 if (ret < 0)
1155 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1156
Mark Brownfe3e78e2009-11-03 22:13:13 +00001157 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1158 if (ret < 0)
1159 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1160
1161 soc_init_codec_debugfs(codec);
1162 mutex_unlock(&codec->mutex);
1163
Mark Brown435c5e22008-12-04 15:32:53 +00001164 card->instantiated = 1;
1165
1166 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001167
Mark Brownfe3e78e2009-11-03 22:13:13 +00001168card_err:
1169 if (platform->remove)
1170 platform->remove(pdev);
1171
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001172platform_err:
1173 if (codec_dev->remove)
1174 codec_dev->remove(pdev);
1175
1176cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001177 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001178 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001179 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001180 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001181 }
1182
Mark Brown87506542008-11-18 20:50:34 +00001183 if (card->remove)
1184 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001185}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001186
Mark Brown435c5e22008-12-04 15:32:53 +00001187/*
1188 * Attempt to initialise any uninitalised cards. Must be called with
1189 * client_mutex.
1190 */
1191static void snd_soc_instantiate_cards(void)
1192{
1193 struct snd_soc_card *card;
1194 list_for_each_entry(card, &card_list, list)
1195 snd_soc_instantiate_card(card);
1196}
1197
1198/* probes a new socdev */
1199static int soc_probe(struct platform_device *pdev)
1200{
1201 int ret = 0;
1202 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1203 struct snd_soc_card *card = socdev->card;
1204
1205 /* Bodge while we push things out of socdev */
1206 card->socdev = socdev;
1207
1208 /* Bodge while we unpick instantiation */
1209 card->dev = &pdev->dev;
1210 ret = snd_soc_register_card(card);
1211 if (ret != 0) {
1212 dev_err(&pdev->dev, "Failed to register card\n");
1213 return ret;
1214 }
1215
1216 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217}
1218
1219/* removes a socdev */
1220static int soc_remove(struct platform_device *pdev)
1221{
1222 int i;
1223 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001224 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001225 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001226 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1227
Mike Rapoport914dc182009-05-11 13:04:55 +03001228 if (!card->instantiated)
1229 return 0;
1230
Mark Brown63084192008-12-02 15:08:03 +00001231 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +01001232
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001233 if (platform->remove)
1234 platform->remove(pdev);
1235
1236 if (codec_dev->remove)
1237 codec_dev->remove(pdev);
1238
Mark Brown87506542008-11-18 20:50:34 +00001239 for (i = 0; i < card->num_links; i++) {
1240 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001241 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001242 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001243 }
1244
Mark Brown87506542008-11-18 20:50:34 +00001245 if (card->remove)
1246 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001247
Mark Brownc5af3a22008-11-28 13:29:45 +00001248 snd_soc_unregister_card(card);
1249
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001250 return 0;
1251}
1252
Mark Brown416356f2009-06-30 19:05:15 +01001253static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001254{
Mark Brown416356f2009-06-30 19:05:15 +01001255 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001256 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1257 struct snd_soc_card *card = socdev->card;
1258
1259 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001260 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001261
1262 /* Flush out pmdown_time work - we actually do want to run it
1263 * now, we're shutting down so no imminent restart. */
1264 run_delayed_work(&card->delayed_work);
1265
1266 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001267
1268 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001269}
1270
Alexey Dobriyan47145212009-12-14 18:00:08 -08001271static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001272 .suspend = soc_suspend,
1273 .resume = soc_resume,
1274 .poweroff = soc_poweroff,
1275};
1276
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001277/* ASoC platform driver */
1278static struct platform_driver soc_driver = {
1279 .driver = {
1280 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001281 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001282 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001283 },
1284 .probe = soc_probe,
1285 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001286};
1287
1288/* create a new pcm */
1289static int soc_new_pcm(struct snd_soc_device *socdev,
1290 struct snd_soc_dai_link *dai_link, int num)
1291{
Mark Brown87689d52008-12-02 16:01:14 +00001292 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001293 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001294 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001295 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1296 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001297 struct snd_soc_pcm_runtime *rtd;
1298 struct snd_pcm *pcm;
1299 char new_name[64];
1300 int ret = 0, playback = 0, capture = 0;
1301
1302 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1303 if (rtd == NULL)
1304 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001305
1306 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001307 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001308 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001309
1310 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001311 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1312 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001313
1314 if (codec_dai->playback.channels_min)
1315 playback = 1;
1316 if (codec_dai->capture.channels_min)
1317 capture = 1;
1318
1319 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1320 capture, &pcm);
1321 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001322 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1323 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001324 kfree(rtd);
1325 return ret;
1326 }
1327
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001328 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001329 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001330 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1331 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1332 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1333 soc_pcm_ops.copy = platform->pcm_ops->copy;
1334 soc_pcm_ops.silence = platform->pcm_ops->silence;
1335 soc_pcm_ops.ack = platform->pcm_ops->ack;
1336 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001337
1338 if (playback)
1339 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1340
1341 if (capture)
1342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1343
Mark Brown87689d52008-12-02 16:01:14 +00001344 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001345 if (ret < 0) {
1346 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1347 kfree(rtd);
1348 return ret;
1349 }
1350
Mark Brown87689d52008-12-02 16:01:14 +00001351 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001352 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1353 cpu_dai->name);
1354 return ret;
1355}
1356
Mark Brown096e49d2009-07-05 15:12:22 +01001357/**
1358 * snd_soc_codec_volatile_register: Report if a register is volatile.
1359 *
1360 * @codec: CODEC to query.
1361 * @reg: Register to query.
1362 *
1363 * Boolean function indiciating if a CODEC register is volatile.
1364 */
1365int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1366{
1367 if (codec->volatile_register)
1368 return codec->volatile_register(reg);
1369 else
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1373
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001374/**
1375 * snd_soc_new_ac97_codec - initailise AC97 device
1376 * @codec: audio codec
1377 * @ops: AC97 bus operations
1378 * @num: AC97 codec number
1379 *
1380 * Initialises AC97 codec resources for use by ad-hoc devices only.
1381 */
1382int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1383 struct snd_ac97_bus_ops *ops, int num)
1384{
1385 mutex_lock(&codec->mutex);
1386
1387 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1388 if (codec->ac97 == NULL) {
1389 mutex_unlock(&codec->mutex);
1390 return -ENOMEM;
1391 }
1392
1393 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1394 if (codec->ac97->bus == NULL) {
1395 kfree(codec->ac97);
1396 codec->ac97 = NULL;
1397 mutex_unlock(&codec->mutex);
1398 return -ENOMEM;
1399 }
1400
1401 codec->ac97->bus->ops = ops;
1402 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001403 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001404 mutex_unlock(&codec->mutex);
1405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1408
1409/**
1410 * snd_soc_free_ac97_codec - free AC97 codec device
1411 * @codec: audio codec
1412 *
1413 * Frees AC97 codec device resources.
1414 */
1415void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1416{
1417 mutex_lock(&codec->mutex);
1418 kfree(codec->ac97->bus);
1419 kfree(codec->ac97);
1420 codec->ac97 = NULL;
1421 mutex_unlock(&codec->mutex);
1422}
1423EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1424
1425/**
1426 * snd_soc_update_bits - update codec register bits
1427 * @codec: audio codec
1428 * @reg: codec register
1429 * @mask: register mask
1430 * @value: new value
1431 *
1432 * Writes new register value.
1433 *
1434 * Returns 1 for change else 0.
1435 */
1436int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001437 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001438{
1439 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001440 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001441
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001442 old = snd_soc_read(codec, reg);
1443 new = (old & ~mask) | value;
1444 change = old != new;
1445 if (change)
1446 snd_soc_write(codec, reg, new);
1447
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001448 return change;
1449}
1450EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1451
1452/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001453 * snd_soc_update_bits_locked - update codec register bits
1454 * @codec: audio codec
1455 * @reg: codec register
1456 * @mask: register mask
1457 * @value: new value
1458 *
1459 * Writes new register value, and takes the codec mutex.
1460 *
1461 * Returns 1 for change else 0.
1462 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001463int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1464 unsigned short reg, unsigned int mask,
1465 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001466{
1467 int change;
1468
1469 mutex_lock(&codec->mutex);
1470 change = snd_soc_update_bits(codec, reg, mask, value);
1471 mutex_unlock(&codec->mutex);
1472
1473 return change;
1474}
Mark Browndd1b3d52009-12-04 14:22:03 +00001475EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001476
1477/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001478 * snd_soc_test_bits - test register for change
1479 * @codec: audio codec
1480 * @reg: codec register
1481 * @mask: register mask
1482 * @value: new value
1483 *
1484 * Tests a register with a new value and checks if the new value is
1485 * different from the old value.
1486 *
1487 * Returns 1 for change else 0.
1488 */
1489int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001490 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001491{
1492 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001493 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001494
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001495 old = snd_soc_read(codec, reg);
1496 new = (old & ~mask) | value;
1497 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001498
1499 return change;
1500}
1501EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1502
1503/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001504 * snd_soc_new_pcms - create new sound card and pcms
1505 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001506 * @idx: ALSA card index
1507 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001508 *
1509 * Create a new sound card based upon the codec and interface pcms.
1510 *
1511 * Returns 0 for success, else error.
1512 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001513int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001514{
Mark Brown87506542008-11-18 20:50:34 +00001515 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001516 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001517 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001518
1519 mutex_lock(&codec->mutex);
1520
1521 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001522 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1523 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001524 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1525 codec->name);
1526 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001527 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001528 }
1529
Mark Brown452c5ea2009-05-17 21:41:23 +01001530 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001531 codec->card->dev = socdev->dev;
1532 codec->card->private_data = codec;
1533 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1534
1535 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001536 for (i = 0; i < card->num_links; i++) {
1537 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001538 if (ret < 0) {
1539 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001540 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001541 mutex_unlock(&codec->mutex);
1542 return ret;
1543 }
Barry Songf7732052009-11-12 12:01:47 +08001544 if (card->dai_link[i].codec_dai->ac97_control) {
1545 snd_ac97_dev_add_pdata(codec->ac97,
1546 card->dai_link[i].cpu_dai->ac97_pdata);
1547 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001548 }
1549
1550 mutex_unlock(&codec->mutex);
1551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1554
1555/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001556 * snd_soc_free_pcms - free sound card and pcms
1557 * @socdev: the SoC audio device
1558 *
1559 * Frees sound card and pcms associated with the socdev.
1560 * Also unregister the codec if it is an AC97 device.
1561 */
1562void snd_soc_free_pcms(struct snd_soc_device *socdev)
1563{
Mark Brown6627a652009-01-23 22:55:23 +00001564 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001565#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001566 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001567 int i;
1568#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001569
1570 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001571 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001572#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001573 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001574 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001575 if (codec_dai->ac97_control && codec->ac97 &&
1576 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001577 soc_ac97_dev_unregister(codec);
1578 goto free_card;
1579 }
1580 }
1581free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001582#endif
1583
1584 if (codec->card)
1585 snd_card_free(codec->card);
1586 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1587 mutex_unlock(&codec->mutex);
1588}
1589EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1590
1591/**
1592 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1593 * @substream: the pcm substream
1594 * @hw: the hardware parameters
1595 *
1596 * Sets the substream runtime hardware parameters.
1597 */
1598int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1599 const struct snd_pcm_hardware *hw)
1600{
1601 struct snd_pcm_runtime *runtime = substream->runtime;
1602 runtime->hw.info = hw->info;
1603 runtime->hw.formats = hw->formats;
1604 runtime->hw.period_bytes_min = hw->period_bytes_min;
1605 runtime->hw.period_bytes_max = hw->period_bytes_max;
1606 runtime->hw.periods_min = hw->periods_min;
1607 runtime->hw.periods_max = hw->periods_max;
1608 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1609 runtime->hw.fifo_size = hw->fifo_size;
1610 return 0;
1611}
1612EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1613
1614/**
1615 * snd_soc_cnew - create new control
1616 * @_template: control template
1617 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001618 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001619 *
1620 * Create a new mixer control from a template control.
1621 *
1622 * Returns 0 for success, else error.
1623 */
1624struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1625 void *data, char *long_name)
1626{
1627 struct snd_kcontrol_new template;
1628
1629 memcpy(&template, _template, sizeof(template));
1630 if (long_name)
1631 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001632 template.index = 0;
1633
1634 return snd_ctl_new1(&template, data);
1635}
1636EXPORT_SYMBOL_GPL(snd_soc_cnew);
1637
1638/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001639 * snd_soc_add_controls - add an array of controls to a codec.
1640 * Convienience function to add a list of controls. Many codecs were
1641 * duplicating this code.
1642 *
1643 * @codec: codec to add controls to
1644 * @controls: array of controls to add
1645 * @num_controls: number of elements in the array
1646 *
1647 * Return 0 for success, else error.
1648 */
1649int snd_soc_add_controls(struct snd_soc_codec *codec,
1650 const struct snd_kcontrol_new *controls, int num_controls)
1651{
1652 struct snd_card *card = codec->card;
1653 int err, i;
1654
1655 for (i = 0; i < num_controls; i++) {
1656 const struct snd_kcontrol_new *control = &controls[i];
1657 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1658 if (err < 0) {
1659 dev_err(codec->dev, "%s: Failed to add %s\n",
1660 codec->name, control->name);
1661 return err;
1662 }
1663 }
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1668
1669/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670 * snd_soc_info_enum_double - enumerated double mixer info callback
1671 * @kcontrol: mixer control
1672 * @uinfo: control element information
1673 *
1674 * Callback to provide information about a double enumerated
1675 * mixer control.
1676 *
1677 * Returns 0 for success.
1678 */
1679int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1680 struct snd_ctl_elem_info *uinfo)
1681{
1682 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1683
1684 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1685 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001686 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001687
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001688 if (uinfo->value.enumerated.item > e->max - 1)
1689 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001690 strcpy(uinfo->value.enumerated.name,
1691 e->texts[uinfo->value.enumerated.item]);
1692 return 0;
1693}
1694EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1695
1696/**
1697 * snd_soc_get_enum_double - enumerated double mixer get callback
1698 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001699 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001700 *
1701 * Callback to get the value of a double enumerated mixer.
1702 *
1703 * Returns 0 for success.
1704 */
1705int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
1707{
1708 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1709 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001710 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001711
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001712 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001713 ;
1714 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001715 ucontrol->value.enumerated.item[0]
1716 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001717 if (e->shift_l != e->shift_r)
1718 ucontrol->value.enumerated.item[1] =
1719 (val >> e->shift_r) & (bitmask - 1);
1720
1721 return 0;
1722}
1723EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1724
1725/**
1726 * snd_soc_put_enum_double - enumerated double mixer put callback
1727 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001728 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001729 *
1730 * Callback to set the value of a double enumerated mixer.
1731 *
1732 * Returns 0 for success.
1733 */
1734int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1735 struct snd_ctl_elem_value *ucontrol)
1736{
1737 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1738 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001739 unsigned int val;
1740 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001741
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001742 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001744 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001745 return -EINVAL;
1746 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1747 mask = (bitmask - 1) << e->shift_l;
1748 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001749 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001750 return -EINVAL;
1751 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1752 mask |= (bitmask - 1) << e->shift_r;
1753 }
1754
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001755 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001756}
1757EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1758
1759/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001760 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1761 * @kcontrol: mixer control
1762 * @ucontrol: control element information
1763 *
1764 * Callback to get the value of a double semi enumerated mixer.
1765 *
1766 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1767 * used for handling bitfield coded enumeration for example.
1768 *
1769 * Returns 0 for success.
1770 */
1771int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_value *ucontrol)
1773{
1774 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001775 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001776 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001777
1778 reg_val = snd_soc_read(codec, e->reg);
1779 val = (reg_val >> e->shift_l) & e->mask;
1780 for (mux = 0; mux < e->max; mux++) {
1781 if (val == e->values[mux])
1782 break;
1783 }
1784 ucontrol->value.enumerated.item[0] = mux;
1785 if (e->shift_l != e->shift_r) {
1786 val = (reg_val >> e->shift_r) & e->mask;
1787 for (mux = 0; mux < e->max; mux++) {
1788 if (val == e->values[mux])
1789 break;
1790 }
1791 ucontrol->value.enumerated.item[1] = mux;
1792 }
1793
1794 return 0;
1795}
1796EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1797
1798/**
1799 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1800 * @kcontrol: mixer control
1801 * @ucontrol: control element information
1802 *
1803 * Callback to set the value of a double semi enumerated mixer.
1804 *
1805 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1806 * used for handling bitfield coded enumeration for example.
1807 *
1808 * Returns 0 for success.
1809 */
1810int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_value *ucontrol)
1812{
1813 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001814 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001815 unsigned int val;
1816 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001817
1818 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1819 return -EINVAL;
1820 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1821 mask = e->mask << e->shift_l;
1822 if (e->shift_l != e->shift_r) {
1823 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1824 return -EINVAL;
1825 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1826 mask |= e->mask << e->shift_r;
1827 }
1828
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001829 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001830}
1831EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1832
1833/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001834 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1835 * @kcontrol: mixer control
1836 * @uinfo: control element information
1837 *
1838 * Callback to provide information about an external enumerated
1839 * single mixer.
1840 *
1841 * Returns 0 for success.
1842 */
1843int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_info *uinfo)
1845{
1846 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1847
1848 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1849 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001850 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001852 if (uinfo->value.enumerated.item > e->max - 1)
1853 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854 strcpy(uinfo->value.enumerated.name,
1855 e->texts[uinfo->value.enumerated.item]);
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1859
1860/**
1861 * snd_soc_info_volsw_ext - external single mixer info callback
1862 * @kcontrol: mixer control
1863 * @uinfo: control element information
1864 *
1865 * Callback to provide information about a single external mixer control.
1866 *
1867 * Returns 0 for success.
1868 */
1869int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_info *uinfo)
1871{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001872 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873
Mark Brownfd5dfad2009-04-15 21:37:46 +01001874 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001875 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1876 else
1877 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1878
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001879 uinfo->count = 1;
1880 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001881 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882 return 0;
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1885
1886/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001887 * snd_soc_info_volsw - single mixer info callback
1888 * @kcontrol: mixer control
1889 * @uinfo: control element information
1890 *
1891 * Callback to provide information about a single mixer control.
1892 *
1893 * Returns 0 for success.
1894 */
1895int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_info *uinfo)
1897{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001898 struct soc_mixer_control *mc =
1899 (struct soc_mixer_control *)kcontrol->private_value;
1900 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001901 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001902 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001903
Mark Brownfd5dfad2009-04-15 21:37:46 +01001904 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001905 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1906 else
1907 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1908
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001909 uinfo->count = shift == rshift ? 1 : 2;
1910 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001911 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001912 return 0;
1913}
1914EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1915
1916/**
1917 * snd_soc_get_volsw - single mixer get callback
1918 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001919 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001920 *
1921 * Callback to get the value of a single mixer control.
1922 *
1923 * Returns 0 for success.
1924 */
1925int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1926 struct snd_ctl_elem_value *ucontrol)
1927{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001928 struct soc_mixer_control *mc =
1929 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001931 unsigned int reg = mc->reg;
1932 unsigned int shift = mc->shift;
1933 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001934 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001935 unsigned int mask = (1 << fls(max)) - 1;
1936 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937
1938 ucontrol->value.integer.value[0] =
1939 (snd_soc_read(codec, reg) >> shift) & mask;
1940 if (shift != rshift)
1941 ucontrol->value.integer.value[1] =
1942 (snd_soc_read(codec, reg) >> rshift) & mask;
1943 if (invert) {
1944 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001945 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946 if (shift != rshift)
1947 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001948 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949 }
1950
1951 return 0;
1952}
1953EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1954
1955/**
1956 * snd_soc_put_volsw - single mixer put callback
1957 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001958 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001959 *
1960 * Callback to set the value of a single mixer control.
1961 *
1962 * Returns 0 for success.
1963 */
1964int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1966{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001967 struct soc_mixer_control *mc =
1968 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001969 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001970 unsigned int reg = mc->reg;
1971 unsigned int shift = mc->shift;
1972 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001973 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001974 unsigned int mask = (1 << fls(max)) - 1;
1975 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001976 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001977
1978 val = (ucontrol->value.integer.value[0] & mask);
1979 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001980 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981 val_mask = mask << shift;
1982 val = val << shift;
1983 if (shift != rshift) {
1984 val2 = (ucontrol->value.integer.value[1] & mask);
1985 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001986 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987 val_mask |= mask << rshift;
1988 val |= val2 << rshift;
1989 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001990 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991}
1992EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1993
1994/**
1995 * snd_soc_info_volsw_2r - double mixer info callback
1996 * @kcontrol: mixer control
1997 * @uinfo: control element information
1998 *
1999 * Callback to provide information about a double mixer control that
2000 * spans 2 codec registers.
2001 *
2002 * Returns 0 for success.
2003 */
2004int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2005 struct snd_ctl_elem_info *uinfo)
2006{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002007 struct soc_mixer_control *mc =
2008 (struct soc_mixer_control *)kcontrol->private_value;
2009 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010
Mark Brownfd5dfad2009-04-15 21:37:46 +01002011 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002012 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2013 else
2014 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2015
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016 uinfo->count = 2;
2017 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002018 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2022
2023/**
2024 * snd_soc_get_volsw_2r - double mixer get callback
2025 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002026 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002027 *
2028 * Callback to get the value of a double mixer control that spans 2 registers.
2029 *
2030 * Returns 0 for success.
2031 */
2032int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
2034{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002035 struct soc_mixer_control *mc =
2036 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002038 unsigned int reg = mc->reg;
2039 unsigned int reg2 = mc->rreg;
2040 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002041 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002042 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002043 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002044
2045 ucontrol->value.integer.value[0] =
2046 (snd_soc_read(codec, reg) >> shift) & mask;
2047 ucontrol->value.integer.value[1] =
2048 (snd_soc_read(codec, reg2) >> shift) & mask;
2049 if (invert) {
2050 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002051 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002052 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002053 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054 }
2055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2059
2060/**
2061 * snd_soc_put_volsw_2r - double mixer set callback
2062 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002063 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002064 *
2065 * Callback to set the value of a double mixer control that spans 2 registers.
2066 *
2067 * Returns 0 for success.
2068 */
2069int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
2071{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002072 struct soc_mixer_control *mc =
2073 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002075 unsigned int reg = mc->reg;
2076 unsigned int reg2 = mc->rreg;
2077 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002078 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002079 unsigned int mask = (1 << fls(max)) - 1;
2080 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002081 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002082 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083
2084 val_mask = mask << shift;
2085 val = (ucontrol->value.integer.value[0] & mask);
2086 val2 = (ucontrol->value.integer.value[1] & mask);
2087
2088 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002089 val = max - val;
2090 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 }
2092
2093 val = val << shift;
2094 val2 = val2 << shift;
2095
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002096 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002097 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098 return err;
2099
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002100 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002101 return err;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2104
Mark Browne13ac2e2008-05-28 17:58:05 +01002105/**
2106 * snd_soc_info_volsw_s8 - signed mixer info callback
2107 * @kcontrol: mixer control
2108 * @uinfo: control element information
2109 *
2110 * Callback to provide information about a signed mixer control.
2111 *
2112 * Returns 0 for success.
2113 */
2114int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_info *uinfo)
2116{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002117 struct soc_mixer_control *mc =
2118 (struct soc_mixer_control *)kcontrol->private_value;
2119 int max = mc->max;
2120 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002121
2122 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2123 uinfo->count = 2;
2124 uinfo->value.integer.min = 0;
2125 uinfo->value.integer.max = max-min;
2126 return 0;
2127}
2128EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2129
2130/**
2131 * snd_soc_get_volsw_s8 - signed mixer get callback
2132 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002133 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002134 *
2135 * Callback to get the value of a signed mixer control.
2136 *
2137 * Returns 0 for success.
2138 */
2139int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_value *ucontrol)
2141{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002142 struct soc_mixer_control *mc =
2143 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002144 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002145 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002146 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002147 int val = snd_soc_read(codec, reg);
2148
2149 ucontrol->value.integer.value[0] =
2150 ((signed char)(val & 0xff))-min;
2151 ucontrol->value.integer.value[1] =
2152 ((signed char)((val >> 8) & 0xff))-min;
2153 return 0;
2154}
2155EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2156
2157/**
2158 * snd_soc_put_volsw_sgn - signed mixer put callback
2159 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002160 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002161 *
2162 * Callback to set the value of a signed mixer control.
2163 *
2164 * Returns 0 for success.
2165 */
2166int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
2168{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002169 struct soc_mixer_control *mc =
2170 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002171 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002172 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002173 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002174 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002175
2176 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2177 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2178
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002179 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002180}
2181EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2182
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002183/**
2184 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2185 * @dai: DAI
2186 * @clk_id: DAI specific clock ID
2187 * @freq: new clock frequency in Hz
2188 * @dir: new clock direction - input/output.
2189 *
2190 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2191 */
2192int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2193 unsigned int freq, int dir)
2194{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002195 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002196 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002197 else
2198 return -EINVAL;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2201
2202/**
2203 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2204 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002205 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002206 * @div: new clock divisor.
2207 *
2208 * Configures the clock dividers. This is used to derive the best DAI bit and
2209 * frame clocks from the system or master clock. It's best to set the DAI bit
2210 * and frame clocks as low as possible to save system power.
2211 */
2212int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2213 int div_id, int div)
2214{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002215 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002216 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002217 else
2218 return -EINVAL;
2219}
2220EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2221
2222/**
2223 * snd_soc_dai_set_pll - configure DAI PLL.
2224 * @dai: DAI
2225 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002226 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002227 * @freq_in: PLL input clock frequency in Hz
2228 * @freq_out: requested PLL output clock frequency in Hz
2229 *
2230 * Configures and enables PLL to generate output clock based on input clock.
2231 */
Mark Brown85488032009-09-05 18:52:16 +01002232int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2233 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002234{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002235 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002236 return dai->ops->set_pll(dai, pll_id, source,
2237 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002238 else
2239 return -EINVAL;
2240}
2241EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2242
2243/**
2244 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2245 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002246 * @fmt: SND_SOC_DAIFMT_ format value.
2247 *
2248 * Configures the DAI hardware format and clocking.
2249 */
2250int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2251{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002252 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002253 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002254 else
2255 return -EINVAL;
2256}
2257EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2258
2259/**
2260 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2261 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002262 * @tx_mask: bitmask representing active TX slots.
2263 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002264 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002265 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002266 *
2267 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2268 * specific.
2269 */
2270int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002271 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002272{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002273 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002274 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2275 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002276 else
2277 return -EINVAL;
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2280
2281/**
Barry Song472df3c2009-09-12 01:16:29 +08002282 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2283 * @dai: DAI
2284 * @tx_num: how many TX channels
2285 * @tx_slot: pointer to an array which imply the TX slot number channel
2286 * 0~num-1 uses
2287 * @rx_num: how many RX channels
2288 * @rx_slot: pointer to an array which imply the RX slot number channel
2289 * 0~num-1 uses
2290 *
2291 * configure the relationship between channel number and TDM slot number.
2292 */
2293int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2294 unsigned int tx_num, unsigned int *tx_slot,
2295 unsigned int rx_num, unsigned int *rx_slot)
2296{
2297 if (dai->ops && dai->ops->set_channel_map)
2298 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2299 rx_num, rx_slot);
2300 else
2301 return -EINVAL;
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2304
2305/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002306 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2307 * @dai: DAI
2308 * @tristate: tristate enable
2309 *
2310 * Tristates the DAI so that others can use it.
2311 */
2312int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2313{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002314 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002315 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002316 else
2317 return -EINVAL;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2320
2321/**
2322 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2323 * @dai: DAI
2324 * @mute: mute enable
2325 *
2326 * Mutes the DAI DAC.
2327 */
2328int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2329{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002330 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002331 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002332 else
2333 return -EINVAL;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2336
Mark Brownc5af3a22008-11-28 13:29:45 +00002337/**
2338 * snd_soc_register_card - Register a card with the ASoC core
2339 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002340 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002341 *
2342 * Note that currently this is an internal only function: it will be
2343 * exposed to machine drivers after further backporting of ASoC v2
2344 * registration APIs.
2345 */
2346static int snd_soc_register_card(struct snd_soc_card *card)
2347{
2348 if (!card->name || !card->dev)
2349 return -EINVAL;
2350
2351 INIT_LIST_HEAD(&card->list);
2352 card->instantiated = 0;
2353
2354 mutex_lock(&client_mutex);
2355 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002356 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002357 mutex_unlock(&client_mutex);
2358
2359 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2360
2361 return 0;
2362}
2363
2364/**
2365 * snd_soc_unregister_card - Unregister a card with the ASoC core
2366 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002367 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002368 *
2369 * Note that currently this is an internal only function: it will be
2370 * exposed to machine drivers after further backporting of ASoC v2
2371 * registration APIs.
2372 */
2373static int snd_soc_unregister_card(struct snd_soc_card *card)
2374{
2375 mutex_lock(&client_mutex);
2376 list_del(&card->list);
2377 mutex_unlock(&client_mutex);
2378
2379 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2380
2381 return 0;
2382}
2383
Mark Brown91151712008-11-30 23:31:24 +00002384/**
2385 * snd_soc_register_dai - Register a DAI with the ASoC core
2386 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002387 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002388 */
2389int snd_soc_register_dai(struct snd_soc_dai *dai)
2390{
2391 if (!dai->name)
2392 return -EINVAL;
2393
2394 /* The device should become mandatory over time */
2395 if (!dai->dev)
2396 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2397
Eric Miao6335d052009-03-03 09:41:00 +08002398 if (!dai->ops)
2399 dai->ops = &null_dai_ops;
2400
Mark Brown91151712008-11-30 23:31:24 +00002401 INIT_LIST_HEAD(&dai->list);
2402
2403 mutex_lock(&client_mutex);
2404 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002405 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002406 mutex_unlock(&client_mutex);
2407
2408 pr_debug("Registered DAI '%s'\n", dai->name);
2409
2410 return 0;
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2413
2414/**
2415 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2416 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002417 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002418 */
2419void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2420{
2421 mutex_lock(&client_mutex);
2422 list_del(&dai->list);
2423 mutex_unlock(&client_mutex);
2424
2425 pr_debug("Unregistered DAI '%s'\n", dai->name);
2426}
2427EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2428
2429/**
2430 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2431 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002432 * @dai: Array of DAIs to register
2433 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002434 */
2435int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2436{
2437 int i, ret;
2438
2439 for (i = 0; i < count; i++) {
2440 ret = snd_soc_register_dai(&dai[i]);
2441 if (ret != 0)
2442 goto err;
2443 }
2444
2445 return 0;
2446
2447err:
2448 for (i--; i >= 0; i--)
2449 snd_soc_unregister_dai(&dai[i]);
2450
2451 return ret;
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2454
2455/**
2456 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2457 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002458 * @dai: Array of DAIs to unregister
2459 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002460 */
2461void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2462{
2463 int i;
2464
2465 for (i = 0; i < count; i++)
2466 snd_soc_unregister_dai(&dai[i]);
2467}
2468EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2469
Mark Brown12a48a8c2008-12-03 19:40:30 +00002470/**
2471 * snd_soc_register_platform - Register a platform with the ASoC core
2472 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002473 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002474 */
2475int snd_soc_register_platform(struct snd_soc_platform *platform)
2476{
2477 if (!platform->name)
2478 return -EINVAL;
2479
2480 INIT_LIST_HEAD(&platform->list);
2481
2482 mutex_lock(&client_mutex);
2483 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002484 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002485 mutex_unlock(&client_mutex);
2486
2487 pr_debug("Registered platform '%s'\n", platform->name);
2488
2489 return 0;
2490}
2491EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2492
2493/**
2494 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2495 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002496 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002497 */
2498void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2499{
2500 mutex_lock(&client_mutex);
2501 list_del(&platform->list);
2502 mutex_unlock(&client_mutex);
2503
2504 pr_debug("Unregistered platform '%s'\n", platform->name);
2505}
2506EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2507
Mark Brown151ab222009-05-09 16:22:58 +01002508static u64 codec_format_map[] = {
2509 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2510 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2511 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2512 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2513 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2514 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2515 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2516 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2517 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2518 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2519 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2520 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2521 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2522 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2523 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2524 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2525};
2526
2527/* Fix up the DAI formats for endianness: codecs don't actually see
2528 * the endianness of the data but we're using the CPU format
2529 * definitions which do need to include endianness so we ensure that
2530 * codec DAIs always have both big and little endian variants set.
2531 */
2532static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2533{
2534 int i;
2535
2536 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2537 if (stream->formats & codec_format_map[i])
2538 stream->formats |= codec_format_map[i];
2539}
2540
Mark Brown0d0cf002008-12-10 14:32:45 +00002541/**
2542 * snd_soc_register_codec - Register a codec with the ASoC core
2543 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002544 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002545 */
2546int snd_soc_register_codec(struct snd_soc_codec *codec)
2547{
Mark Brown151ab222009-05-09 16:22:58 +01002548 int i;
2549
Mark Brown0d0cf002008-12-10 14:32:45 +00002550 if (!codec->name)
2551 return -EINVAL;
2552
2553 /* The device should become mandatory over time */
2554 if (!codec->dev)
2555 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2556
2557 INIT_LIST_HEAD(&codec->list);
2558
Mark Brown151ab222009-05-09 16:22:58 +01002559 for (i = 0; i < codec->num_dai; i++) {
2560 fixup_codec_formats(&codec->dai[i].playback);
2561 fixup_codec_formats(&codec->dai[i].capture);
2562 }
2563
Mark Brown0d0cf002008-12-10 14:32:45 +00002564 mutex_lock(&client_mutex);
2565 list_add(&codec->list, &codec_list);
2566 snd_soc_instantiate_cards();
2567 mutex_unlock(&client_mutex);
2568
2569 pr_debug("Registered codec '%s'\n", codec->name);
2570
2571 return 0;
2572}
2573EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2574
2575/**
2576 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2577 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002578 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002579 */
2580void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2581{
2582 mutex_lock(&client_mutex);
2583 list_del(&codec->list);
2584 mutex_unlock(&client_mutex);
2585
2586 pr_debug("Unregistered codec '%s'\n", codec->name);
2587}
2588EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2589
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002590static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002591{
Mark Brown384c89e2008-12-03 17:34:03 +00002592#ifdef CONFIG_DEBUG_FS
2593 debugfs_root = debugfs_create_dir("asoc", NULL);
2594 if (IS_ERR(debugfs_root) || !debugfs_root) {
2595 printk(KERN_WARNING
2596 "ASoC: Failed to create debugfs directory\n");
2597 debugfs_root = NULL;
2598 }
2599#endif
2600
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002601 return platform_driver_register(&soc_driver);
2602}
2603
Mark Brown7d8c16a2008-11-30 22:11:24 +00002604static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605{
Mark Brown384c89e2008-12-03 17:34:03 +00002606#ifdef CONFIG_DEBUG_FS
2607 debugfs_remove_recursive(debugfs_root);
2608#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002609 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610}
2611
2612module_init(snd_soc_init);
2613module_exit(snd_soc_exit);
2614
2615/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002616MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617MODULE_DESCRIPTION("ALSA SoC Core");
2618MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002619MODULE_ALIAS("platform:soc-audio");