blob: 4011ad3dc57a492d8519cb948e83eba445c73f4a [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
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200803/*
804 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200805 * If cpu_dai, codec_dai, platform driver has the delay callback, than
806 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200807 */
808static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
809{
810 struct snd_soc_pcm_runtime *rtd = substream->private_data;
811 struct snd_soc_device *socdev = rtd->socdev;
812 struct snd_soc_card *card = socdev->card;
813 struct snd_soc_platform *platform = card->platform;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200814 struct snd_soc_dai_link *machine = rtd->dai;
815 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
816 struct snd_soc_dai *codec_dai = machine->codec_dai;
817 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200818 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200819 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200820
821 if (platform->pcm_ops->pointer)
822 offset = platform->pcm_ops->pointer(substream);
823
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200824 if (cpu_dai->ops->delay)
825 delay += cpu_dai->ops->delay(substream, cpu_dai);
826
827 if (codec_dai->ops->delay)
828 delay += codec_dai->ops->delay(substream, codec_dai);
829
830 if (platform->delay)
831 delay += platform->delay(substream, codec_dai);
832
833 runtime->delay = delay;
834
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200835 return offset;
836}
837
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200838/* ASoC PCM operations */
839static struct snd_pcm_ops soc_pcm_ops = {
840 .open = soc_pcm_open,
841 .close = soc_codec_close,
842 .hw_params = soc_pcm_hw_params,
843 .hw_free = soc_pcm_hw_free,
844 .prepare = soc_pcm_prepare,
845 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200846 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847};
848
849#ifdef CONFIG_PM
850/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100851static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852{
Mark Brown416356f2009-06-30 19:05:15 +0100853 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200854 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000855 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000856 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200857 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000858 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859 int i;
860
Daniel Macke3509ff2009-06-03 17:44:49 +0200861 /* If the initialization of this soc device failed, there is no codec
862 * associated with it. Just bail out in this case.
863 */
864 if (!codec)
865 return 0;
866
Andy Green6ed25972008-06-13 16:24:05 +0100867 /* Due to the resume being scheduled into a workqueue we could
868 * suspend before that's finished - wait for it to complete.
869 */
870 snd_power_lock(codec->card);
871 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
872 snd_power_unlock(codec->card);
873
874 /* we're going to block userspace touching us until resume completes */
875 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
876
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200877 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000878 for (i = 0; i < card->num_links; i++) {
879 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800880 if (dai->ops->digital_mute && dai->playback.active)
881 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882 }
883
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100884 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000885 for (i = 0; i < card->num_links; i++)
886 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100887
Mark Brown87506542008-11-18 20:50:34 +0000888 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100889 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
Mark Brown87506542008-11-18 20:50:34 +0000891 for (i = 0; i < card->num_links; i++) {
892 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000893 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000894 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200895 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900896 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897 }
898
899 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000900 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200901 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902
Mark Brown3ff3f642008-05-19 12:32:25 +0200903 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 char *stream = codec->dai[i].playback.stream_name;
905 if (stream != NULL)
906 snd_soc_dapm_stream_event(codec, stream,
907 SND_SOC_DAPM_STREAM_SUSPEND);
908 stream = codec->dai[i].capture.stream_name;
909 if (stream != NULL)
910 snd_soc_dapm_stream_event(codec, stream,
911 SND_SOC_DAPM_STREAM_SUSPEND);
912 }
913
914 if (codec_dev->suspend)
Mark Brown416356f2009-06-30 19:05:15 +0100915 codec_dev->suspend(pdev, PMSG_SUSPEND);
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 Brown3ba9e102008-11-24 18:01:05 +0000919 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000920 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200921 }
922
Mark Brown87506542008-11-18 20:50:34 +0000923 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100924 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200925
926 return 0;
927}
928
Andy Green6ed25972008-06-13 16:24:05 +0100929/* deferred resume work, so resume can complete before we finished
930 * setting our codec back up, which can be very slow on I2C
931 */
932static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200933{
Mark Brown63084192008-12-02 15:08:03 +0000934 struct snd_soc_card *card = container_of(work,
935 struct snd_soc_card,
936 deferred_resume_work);
937 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000938 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200939 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000940 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100941 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200942 int i;
943
Andy Green6ed25972008-06-13 16:24:05 +0100944 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
945 * so userspace apps are blocked from touching us
946 */
947
Mark Brownfde22f22008-11-24 18:08:18 +0000948 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100949
Mark Brown87506542008-11-18 20:50:34 +0000950 if (card->resume_pre)
951 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200952
Mark Brown87506542008-11-18 20:50:34 +0000953 for (i = 0; i < card->num_links; i++) {
954 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000955 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000956 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200957 }
958
959 if (codec_dev->resume)
960 codec_dev->resume(pdev);
961
Mark Brown3ff3f642008-05-19 12:32:25 +0200962 for (i = 0; i < codec->num_dai; i++) {
963 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200964 if (stream != NULL)
965 snd_soc_dapm_stream_event(codec, stream,
966 SND_SOC_DAPM_STREAM_RESUME);
967 stream = codec->dai[i].capture.stream_name;
968 if (stream != NULL)
969 snd_soc_dapm_stream_event(codec, stream,
970 SND_SOC_DAPM_STREAM_RESUME);
971 }
972
Mark Brown3ff3f642008-05-19 12:32:25 +0200973 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000974 for (i = 0; i < card->num_links; i++) {
975 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800976 if (dai->ops->digital_mute && dai->playback.active)
977 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978 }
979
Mark Brown87506542008-11-18 20:50:34 +0000980 for (i = 0; i < card->num_links; i++) {
981 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000982 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000983 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200984 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +0900985 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986 }
987
Mark Brown87506542008-11-18 20:50:34 +0000988 if (card->resume_post)
989 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990
Mark Brownfde22f22008-11-24 18:08:18 +0000991 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100992
993 /* userspace can access us now we are back as we were before */
994 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
995}
996
997/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100998static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100999{
Mark Brown416356f2009-06-30 19:05:15 +01001000 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +01001001 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +00001002 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +01001003 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +01001004
Mark Brown64ab9ba2009-03-31 11:27:03 +01001005 /* AC97 devices might have other drivers hanging off them so
1006 * need to resume immediately. Other drivers don't have that
1007 * problem and may take a substantial amount of time to resume
1008 * due to I/O costs and anti-pop so handle them out of line.
1009 */
1010 if (cpu_dai->ac97_control) {
1011 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1012 soc_resume_deferred(&card->deferred_resume_work);
1013 } else {
1014 dev_dbg(socdev->dev, "Scheduling resume work\n");
1015 if (!schedule_work(&card->deferred_resume_work))
1016 dev_err(socdev->dev, "resume work item may be lost\n");
1017 }
Andy Green6ed25972008-06-13 16:24:05 +01001018
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001019 return 0;
1020}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001021#else
1022#define soc_suspend NULL
1023#define soc_resume NULL
1024#endif
1025
Barry Song02a06d32009-10-16 18:13:38 +08001026static struct snd_soc_dai_ops null_dai_ops = {
1027};
1028
Mark Brown435c5e22008-12-04 15:32:53 +00001029static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030{
Mark Brown435c5e22008-12-04 15:32:53 +00001031 struct platform_device *pdev = container_of(card->dev,
1032 struct platform_device,
1033 dev);
1034 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001035 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001036 struct snd_soc_platform *platform;
1037 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001038 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001039
Mark Brown435c5e22008-12-04 15:32:53 +00001040 if (card->instantiated)
1041 return;
Mark Brown63084192008-12-02 15:08:03 +00001042
Mark Brown435c5e22008-12-04 15:32:53 +00001043 found = 0;
1044 list_for_each_entry(platform, &platform_list, list)
1045 if (card->platform == platform) {
1046 found = 1;
1047 break;
1048 }
1049 if (!found) {
1050 dev_dbg(card->dev, "Platform %s not registered\n",
1051 card->platform->name);
1052 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001053 }
1054
Mark Brown6b05eda2008-12-08 19:26:48 +00001055 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001056 for (i = 0; i < card->num_links; i++) {
1057 found = 0;
1058 list_for_each_entry(dai, &dai_list, list)
1059 if (card->dai_link[i].cpu_dai == dai) {
1060 found = 1;
1061 break;
1062 }
1063 if (!found) {
1064 dev_dbg(card->dev, "DAI %s not registered\n",
1065 card->dai_link[i].cpu_dai->name);
1066 return;
1067 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001068
1069 if (card->dai_link[i].cpu_dai->ac97_control)
1070 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001071 }
1072
Barry Song02a06d32009-10-16 18:13:38 +08001073 for (i = 0; i < card->num_links; i++) {
1074 if (!card->dai_link[i].codec_dai->ops)
1075 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1076 }
1077
Mark Brown6b05eda2008-12-08 19:26:48 +00001078 /* If we have AC97 in the system then don't wait for the
1079 * codec. This will need revisiting if we have to handle
1080 * systems with mixed AC97 and non-AC97 parts. Only check for
1081 * DAIs currently; we can't do this per link since some AC97
1082 * codecs have non-AC97 DAIs.
1083 */
1084 if (!ac97)
1085 for (i = 0; i < card->num_links; i++) {
1086 found = 0;
1087 list_for_each_entry(dai, &dai_list, list)
1088 if (card->dai_link[i].codec_dai == dai) {
1089 found = 1;
1090 break;
1091 }
1092 if (!found) {
1093 dev_dbg(card->dev, "DAI %s not registered\n",
1094 card->dai_link[i].codec_dai->name);
1095 return;
1096 }
1097 }
1098
Mark Brown435c5e22008-12-04 15:32:53 +00001099 /* Note that we do not current check for codec components */
1100
1101 dev_dbg(card->dev, "All components present, instantiating\n");
1102
1103 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001104 card->pmdown_time = pmdown_time;
1105
Mark Brown87506542008-11-18 20:50:34 +00001106 if (card->probe) {
1107 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001108 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001109 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001110 }
1111
Mark Brown87506542008-11-18 20:50:34 +00001112 for (i = 0; i < card->num_links; i++) {
1113 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001114 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001115 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001116 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001117 goto cpu_dai_err;
1118 }
1119 }
1120
1121 if (codec_dev->probe) {
1122 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001123 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001124 goto cpu_dai_err;
1125 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001126 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001127
1128 if (platform->probe) {
1129 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001130 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001131 goto platform_err;
1132 }
1133
1134 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001135 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001136#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001137 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001138 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001139#endif
Andy Green6ed25972008-06-13 16:24:05 +01001140
Mark Brownfe3e78e2009-11-03 22:13:13 +00001141 for (i = 0; i < card->num_links; i++) {
1142 if (card->dai_link[i].init) {
1143 ret = card->dai_link[i].init(codec);
1144 if (ret < 0) {
1145 printk(KERN_ERR "asoc: failed to init %s\n",
1146 card->dai_link[i].stream_name);
1147 continue;
1148 }
1149 }
Barry Songf7732052009-11-12 12:01:47 +08001150 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001151 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001152 }
1153
1154 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1155 "%s", card->name);
1156 snprintf(codec->card->longname, sizeof(codec->card->longname),
1157 "%s (%s)", card->name, codec->name);
1158
1159 /* Make sure all DAPM widgets are instantiated */
1160 snd_soc_dapm_new_widgets(codec);
1161
1162 ret = snd_card_register(codec->card);
1163 if (ret < 0) {
1164 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1165 codec->name);
1166 goto card_err;
1167 }
1168
1169 mutex_lock(&codec->mutex);
1170#ifdef CONFIG_SND_SOC_AC97_BUS
1171 /* Only instantiate AC97 if not already done by the adaptor
1172 * for the generic AC97 subsystem.
1173 */
1174 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1175 ret = soc_ac97_dev_register(codec);
1176 if (ret < 0) {
1177 printk(KERN_ERR "asoc: AC97 device register failed\n");
1178 snd_card_free(codec->card);
1179 mutex_unlock(&codec->mutex);
1180 goto card_err;
1181 }
1182 }
1183#endif
1184
1185 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1186 if (ret < 0)
1187 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1188
Mark Browndbe21402010-02-12 11:37:24 +00001189 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1190 if (ret < 0)
1191 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1192
Mark Brownfe3e78e2009-11-03 22:13:13 +00001193 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1194 if (ret < 0)
1195 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1196
1197 soc_init_codec_debugfs(codec);
1198 mutex_unlock(&codec->mutex);
1199
Mark Brown435c5e22008-12-04 15:32:53 +00001200 card->instantiated = 1;
1201
1202 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001203
Mark Brownfe3e78e2009-11-03 22:13:13 +00001204card_err:
1205 if (platform->remove)
1206 platform->remove(pdev);
1207
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001208platform_err:
1209 if (codec_dev->remove)
1210 codec_dev->remove(pdev);
1211
1212cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001213 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001214 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001215 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001216 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217 }
1218
Mark Brown87506542008-11-18 20:50:34 +00001219 if (card->remove)
1220 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001221}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001222
Mark Brown435c5e22008-12-04 15:32:53 +00001223/*
1224 * Attempt to initialise any uninitalised cards. Must be called with
1225 * client_mutex.
1226 */
1227static void snd_soc_instantiate_cards(void)
1228{
1229 struct snd_soc_card *card;
1230 list_for_each_entry(card, &card_list, list)
1231 snd_soc_instantiate_card(card);
1232}
1233
1234/* probes a new socdev */
1235static int soc_probe(struct platform_device *pdev)
1236{
1237 int ret = 0;
1238 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1239 struct snd_soc_card *card = socdev->card;
1240
1241 /* Bodge while we push things out of socdev */
1242 card->socdev = socdev;
1243
1244 /* Bodge while we unpick instantiation */
1245 card->dev = &pdev->dev;
1246 ret = snd_soc_register_card(card);
1247 if (ret != 0) {
1248 dev_err(&pdev->dev, "Failed to register card\n");
1249 return ret;
1250 }
1251
1252 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001253}
1254
1255/* removes a socdev */
1256static int soc_remove(struct platform_device *pdev)
1257{
1258 int i;
1259 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001260 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001261 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001262 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1263
Mike Rapoport914dc182009-05-11 13:04:55 +03001264 if (!card->instantiated)
1265 return 0;
1266
Mark Brown63084192008-12-02 15:08:03 +00001267 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +01001268
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001269 if (platform->remove)
1270 platform->remove(pdev);
1271
1272 if (codec_dev->remove)
1273 codec_dev->remove(pdev);
1274
Mark Brown87506542008-11-18 20:50:34 +00001275 for (i = 0; i < card->num_links; i++) {
1276 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001277 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001278 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001279 }
1280
Mark Brown87506542008-11-18 20:50:34 +00001281 if (card->remove)
1282 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001283
Mark Brownc5af3a22008-11-28 13:29:45 +00001284 snd_soc_unregister_card(card);
1285
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001286 return 0;
1287}
1288
Mark Brown416356f2009-06-30 19:05:15 +01001289static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001290{
Mark Brown416356f2009-06-30 19:05:15 +01001291 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001292 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1293 struct snd_soc_card *card = socdev->card;
1294
1295 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001296 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001297
1298 /* Flush out pmdown_time work - we actually do want to run it
1299 * now, we're shutting down so no imminent restart. */
1300 run_delayed_work(&card->delayed_work);
1301
1302 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001303
1304 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001305}
1306
Alexey Dobriyan47145212009-12-14 18:00:08 -08001307static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001308 .suspend = soc_suspend,
1309 .resume = soc_resume,
1310 .poweroff = soc_poweroff,
1311};
1312
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001313/* ASoC platform driver */
1314static struct platform_driver soc_driver = {
1315 .driver = {
1316 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001317 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001318 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001319 },
1320 .probe = soc_probe,
1321 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001322};
1323
1324/* create a new pcm */
1325static int soc_new_pcm(struct snd_soc_device *socdev,
1326 struct snd_soc_dai_link *dai_link, int num)
1327{
Mark Brown87689d52008-12-02 16:01:14 +00001328 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001329 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001330 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001331 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1332 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001333 struct snd_soc_pcm_runtime *rtd;
1334 struct snd_pcm *pcm;
1335 char new_name[64];
1336 int ret = 0, playback = 0, capture = 0;
1337
1338 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1339 if (rtd == NULL)
1340 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001341
1342 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001343 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001344 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001345
1346 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001347 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1348 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001349
1350 if (codec_dai->playback.channels_min)
1351 playback = 1;
1352 if (codec_dai->capture.channels_min)
1353 capture = 1;
1354
1355 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1356 capture, &pcm);
1357 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001358 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1359 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001360 kfree(rtd);
1361 return ret;
1362 }
1363
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001364 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001365 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001366 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
Mark Brown87689d52008-12-02 16:01:14 +00001367 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1368 soc_pcm_ops.copy = platform->pcm_ops->copy;
1369 soc_pcm_ops.silence = platform->pcm_ops->silence;
1370 soc_pcm_ops.ack = platform->pcm_ops->ack;
1371 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001372
1373 if (playback)
1374 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1375
1376 if (capture)
1377 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1378
Mark Brown87689d52008-12-02 16:01:14 +00001379 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001380 if (ret < 0) {
1381 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1382 kfree(rtd);
1383 return ret;
1384 }
1385
Mark Brown87689d52008-12-02 16:01:14 +00001386 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001387 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1388 cpu_dai->name);
1389 return ret;
1390}
1391
Mark Brown096e49d2009-07-05 15:12:22 +01001392/**
1393 * snd_soc_codec_volatile_register: Report if a register is volatile.
1394 *
1395 * @codec: CODEC to query.
1396 * @reg: Register to query.
1397 *
1398 * Boolean function indiciating if a CODEC register is volatile.
1399 */
1400int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1401{
1402 if (codec->volatile_register)
1403 return codec->volatile_register(reg);
1404 else
1405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1408
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001409/**
1410 * snd_soc_new_ac97_codec - initailise AC97 device
1411 * @codec: audio codec
1412 * @ops: AC97 bus operations
1413 * @num: AC97 codec number
1414 *
1415 * Initialises AC97 codec resources for use by ad-hoc devices only.
1416 */
1417int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1418 struct snd_ac97_bus_ops *ops, int num)
1419{
1420 mutex_lock(&codec->mutex);
1421
1422 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1423 if (codec->ac97 == NULL) {
1424 mutex_unlock(&codec->mutex);
1425 return -ENOMEM;
1426 }
1427
1428 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1429 if (codec->ac97->bus == NULL) {
1430 kfree(codec->ac97);
1431 codec->ac97 = NULL;
1432 mutex_unlock(&codec->mutex);
1433 return -ENOMEM;
1434 }
1435
1436 codec->ac97->bus->ops = ops;
1437 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001438 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001439 mutex_unlock(&codec->mutex);
1440 return 0;
1441}
1442EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1443
1444/**
1445 * snd_soc_free_ac97_codec - free AC97 codec device
1446 * @codec: audio codec
1447 *
1448 * Frees AC97 codec device resources.
1449 */
1450void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1451{
1452 mutex_lock(&codec->mutex);
1453 kfree(codec->ac97->bus);
1454 kfree(codec->ac97);
1455 codec->ac97 = NULL;
1456 mutex_unlock(&codec->mutex);
1457}
1458EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1459
1460/**
1461 * snd_soc_update_bits - update codec register bits
1462 * @codec: audio codec
1463 * @reg: codec register
1464 * @mask: register mask
1465 * @value: new value
1466 *
1467 * Writes new register value.
1468 *
1469 * Returns 1 for change else 0.
1470 */
1471int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001472 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001473{
1474 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001475 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001476
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001477 old = snd_soc_read(codec, reg);
1478 new = (old & ~mask) | value;
1479 change = old != new;
1480 if (change)
1481 snd_soc_write(codec, reg, new);
1482
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001483 return change;
1484}
1485EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1486
1487/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001488 * snd_soc_update_bits_locked - update codec register bits
1489 * @codec: audio codec
1490 * @reg: codec register
1491 * @mask: register mask
1492 * @value: new value
1493 *
1494 * Writes new register value, and takes the codec mutex.
1495 *
1496 * Returns 1 for change else 0.
1497 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001498int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1499 unsigned short reg, unsigned int mask,
1500 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001501{
1502 int change;
1503
1504 mutex_lock(&codec->mutex);
1505 change = snd_soc_update_bits(codec, reg, mask, value);
1506 mutex_unlock(&codec->mutex);
1507
1508 return change;
1509}
Mark Browndd1b3d52009-12-04 14:22:03 +00001510EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001511
1512/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001513 * snd_soc_test_bits - test register for change
1514 * @codec: audio codec
1515 * @reg: codec register
1516 * @mask: register mask
1517 * @value: new value
1518 *
1519 * Tests a register with a new value and checks if the new value is
1520 * different from the old value.
1521 *
1522 * Returns 1 for change else 0.
1523 */
1524int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001525 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526{
1527 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001528 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001529
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001530 old = snd_soc_read(codec, reg);
1531 new = (old & ~mask) | value;
1532 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001533
1534 return change;
1535}
1536EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1537
1538/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539 * snd_soc_new_pcms - create new sound card and pcms
1540 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001541 * @idx: ALSA card index
1542 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543 *
1544 * Create a new sound card based upon the codec and interface pcms.
1545 *
1546 * Returns 0 for success, else error.
1547 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001548int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549{
Mark Brown87506542008-11-18 20:50:34 +00001550 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001551 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001552 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001553
1554 mutex_lock(&codec->mutex);
1555
1556 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001557 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1558 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001559 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1560 codec->name);
1561 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001562 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001563 }
1564
Mark Brown452c5ea2009-05-17 21:41:23 +01001565 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001566 codec->card->dev = socdev->dev;
1567 codec->card->private_data = codec;
1568 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1569
1570 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001571 for (i = 0; i < card->num_links; i++) {
1572 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001573 if (ret < 0) {
1574 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001575 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001576 mutex_unlock(&codec->mutex);
1577 return ret;
1578 }
Barry Songf7732052009-11-12 12:01:47 +08001579 if (card->dai_link[i].codec_dai->ac97_control) {
1580 snd_ac97_dev_add_pdata(codec->ac97,
1581 card->dai_link[i].cpu_dai->ac97_pdata);
1582 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583 }
1584
1585 mutex_unlock(&codec->mutex);
1586 return ret;
1587}
1588EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1589
1590/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001591 * snd_soc_free_pcms - free sound card and pcms
1592 * @socdev: the SoC audio device
1593 *
1594 * Frees sound card and pcms associated with the socdev.
1595 * Also unregister the codec if it is an AC97 device.
1596 */
1597void snd_soc_free_pcms(struct snd_soc_device *socdev)
1598{
Mark Brown6627a652009-01-23 22:55:23 +00001599 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001600#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001601 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001602 int i;
1603#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001604
1605 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001606 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001607#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001608 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001609 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001610 if (codec_dai->ac97_control && codec->ac97 &&
1611 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001612 soc_ac97_dev_unregister(codec);
1613 goto free_card;
1614 }
1615 }
1616free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001617#endif
1618
1619 if (codec->card)
1620 snd_card_free(codec->card);
1621 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1622 mutex_unlock(&codec->mutex);
1623}
1624EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1625
1626/**
1627 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1628 * @substream: the pcm substream
1629 * @hw: the hardware parameters
1630 *
1631 * Sets the substream runtime hardware parameters.
1632 */
1633int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1634 const struct snd_pcm_hardware *hw)
1635{
1636 struct snd_pcm_runtime *runtime = substream->runtime;
1637 runtime->hw.info = hw->info;
1638 runtime->hw.formats = hw->formats;
1639 runtime->hw.period_bytes_min = hw->period_bytes_min;
1640 runtime->hw.period_bytes_max = hw->period_bytes_max;
1641 runtime->hw.periods_min = hw->periods_min;
1642 runtime->hw.periods_max = hw->periods_max;
1643 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1644 runtime->hw.fifo_size = hw->fifo_size;
1645 return 0;
1646}
1647EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1648
1649/**
1650 * snd_soc_cnew - create new control
1651 * @_template: control template
1652 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001653 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001654 *
1655 * Create a new mixer control from a template control.
1656 *
1657 * Returns 0 for success, else error.
1658 */
1659struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1660 void *data, char *long_name)
1661{
1662 struct snd_kcontrol_new template;
1663
1664 memcpy(&template, _template, sizeof(template));
1665 if (long_name)
1666 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001667 template.index = 0;
1668
1669 return snd_ctl_new1(&template, data);
1670}
1671EXPORT_SYMBOL_GPL(snd_soc_cnew);
1672
1673/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001674 * snd_soc_add_controls - add an array of controls to a codec.
1675 * Convienience function to add a list of controls. Many codecs were
1676 * duplicating this code.
1677 *
1678 * @codec: codec to add controls to
1679 * @controls: array of controls to add
1680 * @num_controls: number of elements in the array
1681 *
1682 * Return 0 for success, else error.
1683 */
1684int snd_soc_add_controls(struct snd_soc_codec *codec,
1685 const struct snd_kcontrol_new *controls, int num_controls)
1686{
1687 struct snd_card *card = codec->card;
1688 int err, i;
1689
1690 for (i = 0; i < num_controls; i++) {
1691 const struct snd_kcontrol_new *control = &controls[i];
1692 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1693 if (err < 0) {
1694 dev_err(codec->dev, "%s: Failed to add %s\n",
1695 codec->name, control->name);
1696 return err;
1697 }
1698 }
1699
1700 return 0;
1701}
1702EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1703
1704/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705 * snd_soc_info_enum_double - enumerated double mixer info callback
1706 * @kcontrol: mixer control
1707 * @uinfo: control element information
1708 *
1709 * Callback to provide information about a double enumerated
1710 * mixer control.
1711 *
1712 * Returns 0 for success.
1713 */
1714int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_info *uinfo)
1716{
1717 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1718
1719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1720 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001721 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001722
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001723 if (uinfo->value.enumerated.item > e->max - 1)
1724 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001725 strcpy(uinfo->value.enumerated.name,
1726 e->texts[uinfo->value.enumerated.item]);
1727 return 0;
1728}
1729EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1730
1731/**
1732 * snd_soc_get_enum_double - enumerated double mixer get callback
1733 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001734 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001735 *
1736 * Callback to get the value of a double enumerated mixer.
1737 *
1738 * Returns 0 for success.
1739 */
1740int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol)
1742{
1743 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1744 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001745 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001747 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748 ;
1749 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001750 ucontrol->value.enumerated.item[0]
1751 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 if (e->shift_l != e->shift_r)
1753 ucontrol->value.enumerated.item[1] =
1754 (val >> e->shift_r) & (bitmask - 1);
1755
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1759
1760/**
1761 * snd_soc_put_enum_double - enumerated double mixer put callback
1762 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001763 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764 *
1765 * Callback to set the value of a double enumerated mixer.
1766 *
1767 * Returns 0 for success.
1768 */
1769int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_value *ucontrol)
1771{
1772 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1773 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001774 unsigned int val;
1775 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001776
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001777 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001778 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001779 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001780 return -EINVAL;
1781 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1782 mask = (bitmask - 1) << e->shift_l;
1783 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001784 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001785 return -EINVAL;
1786 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1787 mask |= (bitmask - 1) << e->shift_r;
1788 }
1789
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001790 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791}
1792EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1793
1794/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001795 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1796 * @kcontrol: mixer control
1797 * @ucontrol: control element information
1798 *
1799 * Callback to get the value of a double semi enumerated mixer.
1800 *
1801 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1802 * used for handling bitfield coded enumeration for example.
1803 *
1804 * Returns 0 for success.
1805 */
1806int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1807 struct snd_ctl_elem_value *ucontrol)
1808{
1809 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001810 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001811 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001812
1813 reg_val = snd_soc_read(codec, e->reg);
1814 val = (reg_val >> e->shift_l) & e->mask;
1815 for (mux = 0; mux < e->max; mux++) {
1816 if (val == e->values[mux])
1817 break;
1818 }
1819 ucontrol->value.enumerated.item[0] = mux;
1820 if (e->shift_l != e->shift_r) {
1821 val = (reg_val >> e->shift_r) & e->mask;
1822 for (mux = 0; mux < e->max; mux++) {
1823 if (val == e->values[mux])
1824 break;
1825 }
1826 ucontrol->value.enumerated.item[1] = mux;
1827 }
1828
1829 return 0;
1830}
1831EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1832
1833/**
1834 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1835 * @kcontrol: mixer control
1836 * @ucontrol: control element information
1837 *
1838 * Callback to set the value of a double semi enumerated mixer.
1839 *
1840 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1841 * used for handling bitfield coded enumeration for example.
1842 *
1843 * Returns 0 for success.
1844 */
1845int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_value *ucontrol)
1847{
1848 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001849 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001850 unsigned int val;
1851 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001852
1853 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1854 return -EINVAL;
1855 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1856 mask = e->mask << e->shift_l;
1857 if (e->shift_l != e->shift_r) {
1858 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1859 return -EINVAL;
1860 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1861 mask |= e->mask << e->shift_r;
1862 }
1863
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001864 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001865}
1866EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1867
1868/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001869 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1870 * @kcontrol: mixer control
1871 * @uinfo: control element information
1872 *
1873 * Callback to provide information about an external enumerated
1874 * single mixer.
1875 *
1876 * Returns 0 for success.
1877 */
1878int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1879 struct snd_ctl_elem_info *uinfo)
1880{
1881 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1882
1883 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1884 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001885 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001887 if (uinfo->value.enumerated.item > e->max - 1)
1888 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889 strcpy(uinfo->value.enumerated.name,
1890 e->texts[uinfo->value.enumerated.item]);
1891 return 0;
1892}
1893EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1894
1895/**
1896 * snd_soc_info_volsw_ext - external single mixer info callback
1897 * @kcontrol: mixer control
1898 * @uinfo: control element information
1899 *
1900 * Callback to provide information about a single external mixer control.
1901 *
1902 * Returns 0 for success.
1903 */
1904int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
1906{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001907 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001908
Mark Brownfd5dfad2009-04-15 21:37:46 +01001909 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001910 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1911 else
1912 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1913
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914 uinfo->count = 1;
1915 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001916 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917 return 0;
1918}
1919EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1920
1921/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922 * snd_soc_info_volsw - single mixer info callback
1923 * @kcontrol: mixer control
1924 * @uinfo: control element information
1925 *
1926 * Callback to provide information about a single mixer control.
1927 *
1928 * Returns 0 for success.
1929 */
1930int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1931 struct snd_ctl_elem_info *uinfo)
1932{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001933 struct soc_mixer_control *mc =
1934 (struct soc_mixer_control *)kcontrol->private_value;
1935 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001936 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04001937 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001938
Mark Brownfd5dfad2009-04-15 21:37:46 +01001939 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001940 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1941 else
1942 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1943
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944 uinfo->count = shift == rshift ? 1 : 2;
1945 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001946 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 return 0;
1948}
1949EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1950
1951/**
1952 * snd_soc_get_volsw - single mixer get callback
1953 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001954 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955 *
1956 * Callback to get the value of a single mixer control.
1957 *
1958 * Returns 0 for success.
1959 */
1960int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1961 struct snd_ctl_elem_value *ucontrol)
1962{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001963 struct soc_mixer_control *mc =
1964 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001966 unsigned int reg = mc->reg;
1967 unsigned int shift = mc->shift;
1968 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001969 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001970 unsigned int mask = (1 << fls(max)) - 1;
1971 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972
1973 ucontrol->value.integer.value[0] =
1974 (snd_soc_read(codec, reg) >> shift) & mask;
1975 if (shift != rshift)
1976 ucontrol->value.integer.value[1] =
1977 (snd_soc_read(codec, reg) >> rshift) & mask;
1978 if (invert) {
1979 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001980 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981 if (shift != rshift)
1982 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001983 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984 }
1985
1986 return 0;
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1989
1990/**
1991 * snd_soc_put_volsw - single mixer put callback
1992 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001993 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001994 *
1995 * Callback to set the value of a single mixer control.
1996 *
1997 * Returns 0 for success.
1998 */
1999int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2000 struct snd_ctl_elem_value *ucontrol)
2001{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002002 struct soc_mixer_control *mc =
2003 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002005 unsigned int reg = mc->reg;
2006 unsigned int shift = mc->shift;
2007 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002008 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002009 unsigned int mask = (1 << fls(max)) - 1;
2010 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002011 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002012
2013 val = (ucontrol->value.integer.value[0] & mask);
2014 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002015 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016 val_mask = mask << shift;
2017 val = val << shift;
2018 if (shift != rshift) {
2019 val2 = (ucontrol->value.integer.value[1] & mask);
2020 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002021 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 val_mask |= mask << rshift;
2023 val |= val2 << rshift;
2024 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002025 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026}
2027EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2028
2029/**
2030 * snd_soc_info_volsw_2r - double mixer info callback
2031 * @kcontrol: mixer control
2032 * @uinfo: control element information
2033 *
2034 * Callback to provide information about a double mixer control that
2035 * spans 2 codec registers.
2036 *
2037 * Returns 0 for success.
2038 */
2039int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_info *uinfo)
2041{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002042 struct soc_mixer_control *mc =
2043 (struct soc_mixer_control *)kcontrol->private_value;
2044 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002045
Mark Brownfd5dfad2009-04-15 21:37:46 +01002046 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002047 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2048 else
2049 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2050
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002051 uinfo->count = 2;
2052 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002053 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054 return 0;
2055}
2056EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2057
2058/**
2059 * snd_soc_get_volsw_2r - double mixer get callback
2060 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002061 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002062 *
2063 * Callback to get the value of a double mixer control that spans 2 registers.
2064 *
2065 * Returns 0 for success.
2066 */
2067int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2068 struct snd_ctl_elem_value *ucontrol)
2069{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002070 struct soc_mixer_control *mc =
2071 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002072 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002073 unsigned int reg = mc->reg;
2074 unsigned int reg2 = mc->rreg;
2075 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002076 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002077 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002078 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079
2080 ucontrol->value.integer.value[0] =
2081 (snd_soc_read(codec, reg) >> shift) & mask;
2082 ucontrol->value.integer.value[1] =
2083 (snd_soc_read(codec, reg2) >> shift) & mask;
2084 if (invert) {
2085 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002086 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002088 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002089 }
2090
2091 return 0;
2092}
2093EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2094
2095/**
2096 * snd_soc_put_volsw_2r - double mixer set callback
2097 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002098 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 *
2100 * Callback to set the value of a double mixer control that spans 2 registers.
2101 *
2102 * Returns 0 for success.
2103 */
2104int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2105 struct snd_ctl_elem_value *ucontrol)
2106{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002107 struct soc_mixer_control *mc =
2108 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002110 unsigned int reg = mc->reg;
2111 unsigned int reg2 = mc->rreg;
2112 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002113 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002114 unsigned int mask = (1 << fls(max)) - 1;
2115 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002117 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002118
2119 val_mask = mask << shift;
2120 val = (ucontrol->value.integer.value[0] & mask);
2121 val2 = (ucontrol->value.integer.value[1] & mask);
2122
2123 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002124 val = max - val;
2125 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126 }
2127
2128 val = val << shift;
2129 val2 = val2 << shift;
2130
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002131 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002132 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133 return err;
2134
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002135 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136 return err;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2139
Mark Browne13ac2e2008-05-28 17:58:05 +01002140/**
2141 * snd_soc_info_volsw_s8 - signed mixer info callback
2142 * @kcontrol: mixer control
2143 * @uinfo: control element information
2144 *
2145 * Callback to provide information about a signed mixer control.
2146 *
2147 * Returns 0 for success.
2148 */
2149int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_info *uinfo)
2151{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002152 struct soc_mixer_control *mc =
2153 (struct soc_mixer_control *)kcontrol->private_value;
2154 int max = mc->max;
2155 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002156
2157 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2158 uinfo->count = 2;
2159 uinfo->value.integer.min = 0;
2160 uinfo->value.integer.max = max-min;
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2164
2165/**
2166 * snd_soc_get_volsw_s8 - signed mixer get callback
2167 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002168 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002169 *
2170 * Callback to get the value of a signed mixer control.
2171 *
2172 * Returns 0 for success.
2173 */
2174int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002177 struct soc_mixer_control *mc =
2178 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002180 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002181 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002182 int val = snd_soc_read(codec, reg);
2183
2184 ucontrol->value.integer.value[0] =
2185 ((signed char)(val & 0xff))-min;
2186 ucontrol->value.integer.value[1] =
2187 ((signed char)((val >> 8) & 0xff))-min;
2188 return 0;
2189}
2190EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2191
2192/**
2193 * snd_soc_put_volsw_sgn - signed mixer put callback
2194 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002195 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002196 *
2197 * Callback to set the value of a signed mixer control.
2198 *
2199 * Returns 0 for success.
2200 */
2201int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2203{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002204 struct soc_mixer_control *mc =
2205 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002206 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002207 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002208 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002209 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002210
2211 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2212 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2213
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002214 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002215}
2216EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2217
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002218/**
2219 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2220 * @dai: DAI
2221 * @clk_id: DAI specific clock ID
2222 * @freq: new clock frequency in Hz
2223 * @dir: new clock direction - input/output.
2224 *
2225 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2226 */
2227int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2228 unsigned int freq, int dir)
2229{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002230 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002231 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002232 else
2233 return -EINVAL;
2234}
2235EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2236
2237/**
2238 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2239 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002240 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002241 * @div: new clock divisor.
2242 *
2243 * Configures the clock dividers. This is used to derive the best DAI bit and
2244 * frame clocks from the system or master clock. It's best to set the DAI bit
2245 * and frame clocks as low as possible to save system power.
2246 */
2247int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2248 int div_id, int div)
2249{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002250 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002251 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002252 else
2253 return -EINVAL;
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2256
2257/**
2258 * snd_soc_dai_set_pll - configure DAI PLL.
2259 * @dai: DAI
2260 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002261 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002262 * @freq_in: PLL input clock frequency in Hz
2263 * @freq_out: requested PLL output clock frequency in Hz
2264 *
2265 * Configures and enables PLL to generate output clock based on input clock.
2266 */
Mark Brown85488032009-09-05 18:52:16 +01002267int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2268 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002269{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002270 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002271 return dai->ops->set_pll(dai, pll_id, source,
2272 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002273 else
2274 return -EINVAL;
2275}
2276EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2277
2278/**
2279 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2280 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002281 * @fmt: SND_SOC_DAIFMT_ format value.
2282 *
2283 * Configures the DAI hardware format and clocking.
2284 */
2285int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2286{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002287 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002288 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002289 else
2290 return -EINVAL;
2291}
2292EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2293
2294/**
2295 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2296 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002297 * @tx_mask: bitmask representing active TX slots.
2298 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002299 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002300 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002301 *
2302 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2303 * specific.
2304 */
2305int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002306 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002307{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002308 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002309 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2310 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002311 else
2312 return -EINVAL;
2313}
2314EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2315
2316/**
Barry Song472df3c2009-09-12 01:16:29 +08002317 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2318 * @dai: DAI
2319 * @tx_num: how many TX channels
2320 * @tx_slot: pointer to an array which imply the TX slot number channel
2321 * 0~num-1 uses
2322 * @rx_num: how many RX channels
2323 * @rx_slot: pointer to an array which imply the RX slot number channel
2324 * 0~num-1 uses
2325 *
2326 * configure the relationship between channel number and TDM slot number.
2327 */
2328int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2329 unsigned int tx_num, unsigned int *tx_slot,
2330 unsigned int rx_num, unsigned int *rx_slot)
2331{
2332 if (dai->ops && dai->ops->set_channel_map)
2333 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2334 rx_num, rx_slot);
2335 else
2336 return -EINVAL;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2339
2340/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002341 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2342 * @dai: DAI
2343 * @tristate: tristate enable
2344 *
2345 * Tristates the DAI so that others can use it.
2346 */
2347int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2348{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002349 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002350 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002351 else
2352 return -EINVAL;
2353}
2354EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2355
2356/**
2357 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2358 * @dai: DAI
2359 * @mute: mute enable
2360 *
2361 * Mutes the DAI DAC.
2362 */
2363int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2364{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002365 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002366 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002367 else
2368 return -EINVAL;
2369}
2370EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2371
Mark Brownc5af3a22008-11-28 13:29:45 +00002372/**
2373 * snd_soc_register_card - Register a card with the ASoC core
2374 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002375 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002376 *
2377 * Note that currently this is an internal only function: it will be
2378 * exposed to machine drivers after further backporting of ASoC v2
2379 * registration APIs.
2380 */
2381static int snd_soc_register_card(struct snd_soc_card *card)
2382{
2383 if (!card->name || !card->dev)
2384 return -EINVAL;
2385
2386 INIT_LIST_HEAD(&card->list);
2387 card->instantiated = 0;
2388
2389 mutex_lock(&client_mutex);
2390 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002391 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002392 mutex_unlock(&client_mutex);
2393
2394 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2395
2396 return 0;
2397}
2398
2399/**
2400 * snd_soc_unregister_card - Unregister a card with the ASoC core
2401 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002402 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002403 *
2404 * Note that currently this is an internal only function: it will be
2405 * exposed to machine drivers after further backporting of ASoC v2
2406 * registration APIs.
2407 */
2408static int snd_soc_unregister_card(struct snd_soc_card *card)
2409{
2410 mutex_lock(&client_mutex);
2411 list_del(&card->list);
2412 mutex_unlock(&client_mutex);
2413
2414 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2415
2416 return 0;
2417}
2418
Mark Brown91151712008-11-30 23:31:24 +00002419/**
2420 * snd_soc_register_dai - Register a DAI with the ASoC core
2421 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002422 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002423 */
2424int snd_soc_register_dai(struct snd_soc_dai *dai)
2425{
2426 if (!dai->name)
2427 return -EINVAL;
2428
2429 /* The device should become mandatory over time */
2430 if (!dai->dev)
2431 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2432
Eric Miao6335d052009-03-03 09:41:00 +08002433 if (!dai->ops)
2434 dai->ops = &null_dai_ops;
2435
Mark Brown91151712008-11-30 23:31:24 +00002436 INIT_LIST_HEAD(&dai->list);
2437
2438 mutex_lock(&client_mutex);
2439 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002440 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002441 mutex_unlock(&client_mutex);
2442
2443 pr_debug("Registered DAI '%s'\n", dai->name);
2444
2445 return 0;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2448
2449/**
2450 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2451 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002452 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002453 */
2454void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2455{
2456 mutex_lock(&client_mutex);
2457 list_del(&dai->list);
2458 mutex_unlock(&client_mutex);
2459
2460 pr_debug("Unregistered DAI '%s'\n", dai->name);
2461}
2462EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2463
2464/**
2465 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2466 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002467 * @dai: Array of DAIs to register
2468 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002469 */
2470int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2471{
2472 int i, ret;
2473
2474 for (i = 0; i < count; i++) {
2475 ret = snd_soc_register_dai(&dai[i]);
2476 if (ret != 0)
2477 goto err;
2478 }
2479
2480 return 0;
2481
2482err:
2483 for (i--; i >= 0; i--)
2484 snd_soc_unregister_dai(&dai[i]);
2485
2486 return ret;
2487}
2488EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2489
2490/**
2491 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2492 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002493 * @dai: Array of DAIs to unregister
2494 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002495 */
2496void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2497{
2498 int i;
2499
2500 for (i = 0; i < count; i++)
2501 snd_soc_unregister_dai(&dai[i]);
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2504
Mark Brown12a48a8c2008-12-03 19:40:30 +00002505/**
2506 * snd_soc_register_platform - Register a platform with the ASoC core
2507 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002508 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002509 */
2510int snd_soc_register_platform(struct snd_soc_platform *platform)
2511{
2512 if (!platform->name)
2513 return -EINVAL;
2514
2515 INIT_LIST_HEAD(&platform->list);
2516
2517 mutex_lock(&client_mutex);
2518 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002519 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002520 mutex_unlock(&client_mutex);
2521
2522 pr_debug("Registered platform '%s'\n", platform->name);
2523
2524 return 0;
2525}
2526EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2527
2528/**
2529 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2530 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002531 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002532 */
2533void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2534{
2535 mutex_lock(&client_mutex);
2536 list_del(&platform->list);
2537 mutex_unlock(&client_mutex);
2538
2539 pr_debug("Unregistered platform '%s'\n", platform->name);
2540}
2541EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2542
Mark Brown151ab222009-05-09 16:22:58 +01002543static u64 codec_format_map[] = {
2544 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2545 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2546 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2547 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2548 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2549 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2550 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2551 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2552 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2553 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2554 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2555 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2556 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2557 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2558 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2559 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2560};
2561
2562/* Fix up the DAI formats for endianness: codecs don't actually see
2563 * the endianness of the data but we're using the CPU format
2564 * definitions which do need to include endianness so we ensure that
2565 * codec DAIs always have both big and little endian variants set.
2566 */
2567static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2568{
2569 int i;
2570
2571 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2572 if (stream->formats & codec_format_map[i])
2573 stream->formats |= codec_format_map[i];
2574}
2575
Mark Brown0d0cf002008-12-10 14:32:45 +00002576/**
2577 * snd_soc_register_codec - Register a codec with the ASoC core
2578 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002579 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002580 */
2581int snd_soc_register_codec(struct snd_soc_codec *codec)
2582{
Mark Brown151ab222009-05-09 16:22:58 +01002583 int i;
2584
Mark Brown0d0cf002008-12-10 14:32:45 +00002585 if (!codec->name)
2586 return -EINVAL;
2587
2588 /* The device should become mandatory over time */
2589 if (!codec->dev)
2590 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2591
2592 INIT_LIST_HEAD(&codec->list);
2593
Mark Brown151ab222009-05-09 16:22:58 +01002594 for (i = 0; i < codec->num_dai; i++) {
2595 fixup_codec_formats(&codec->dai[i].playback);
2596 fixup_codec_formats(&codec->dai[i].capture);
2597 }
2598
Mark Brown0d0cf002008-12-10 14:32:45 +00002599 mutex_lock(&client_mutex);
2600 list_add(&codec->list, &codec_list);
2601 snd_soc_instantiate_cards();
2602 mutex_unlock(&client_mutex);
2603
2604 pr_debug("Registered codec '%s'\n", codec->name);
2605
2606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2609
2610/**
2611 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2612 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002613 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002614 */
2615void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2616{
2617 mutex_lock(&client_mutex);
2618 list_del(&codec->list);
2619 mutex_unlock(&client_mutex);
2620
2621 pr_debug("Unregistered codec '%s'\n", codec->name);
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2624
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002625static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626{
Mark Brown384c89e2008-12-03 17:34:03 +00002627#ifdef CONFIG_DEBUG_FS
2628 debugfs_root = debugfs_create_dir("asoc", NULL);
2629 if (IS_ERR(debugfs_root) || !debugfs_root) {
2630 printk(KERN_WARNING
2631 "ASoC: Failed to create debugfs directory\n");
2632 debugfs_root = NULL;
2633 }
2634#endif
2635
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636 return platform_driver_register(&soc_driver);
2637}
2638
Mark Brown7d8c16a2008-11-30 22:11:24 +00002639static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640{
Mark Brown384c89e2008-12-03 17:34:03 +00002641#ifdef CONFIG_DEBUG_FS
2642 debugfs_remove_recursive(debugfs_root);
2643#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002644 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645}
2646
2647module_init(snd_soc_init);
2648module_exit(snd_soc_exit);
2649
2650/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002651MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652MODULE_DESCRIPTION("ALSA SoC Core");
2653MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002654MODULE_ALIAS("platform:soc-audio");