blob: ad7f9528d751a95e93eca755d225d92ba9cea701 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +01008 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020016 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070029#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020030#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020032#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020033#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/soc.h>
37#include <sound/soc-dapm.h>
38#include <sound/initval.h>
39
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42
Mark Brown384c89e2008-12-03 17:34:03 +000043#ifdef CONFIG_DEBUG_FS
44static struct dentry *debugfs_root;
45#endif
46
Mark Brownc5af3a22008-11-28 13:29:45 +000047static DEFINE_MUTEX(client_mutex);
48static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000049static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000050static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000051static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000052
53static int snd_soc_register_card(struct snd_soc_card *card);
54static int snd_soc_unregister_card(struct snd_soc_card *card);
55
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056/*
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
60 */
61static int pmdown_time = 5000;
62module_param(pmdown_time, int, 0);
63MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64
Liam Girdwood965ac422007-01-31 14:14:57 +010065/*
66 * This function forces any delayed work to be queued and run.
67 */
68static int run_delayed_work(struct delayed_work *dwork)
69{
70 int ret;
71
72 /* cancel any work waiting to be queued. */
73 ret = cancel_delayed_work(dwork);
74
75 /* if there was any work waiting then we run it now and
76 * wait for it's completion */
77 if (ret) {
78 schedule_delayed_work(dwork, 0);
79 flush_scheduled_work();
80 }
81 return ret;
82}
83
Mark Brown2624d5f2009-11-03 21:56:13 +000084/* codec register dump */
85static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
86{
87 int i, step = 1, count = 0;
88
89 if (!codec->reg_cache_size)
90 return 0;
91
92 if (codec->reg_cache_step)
93 step = codec->reg_cache_step;
94
95 count += sprintf(buf, "%s registers\n", codec->name);
96 for (i = 0; i < codec->reg_cache_size; i += step) {
97 if (codec->readable_register && !codec->readable_register(i))
98 continue;
99
100 count += sprintf(buf + count, "%2x: ", i);
101 if (count >= PAGE_SIZE - 1)
102 break;
103
104 if (codec->display_register)
105 count += codec->display_register(codec, buf + count,
106 PAGE_SIZE - count, i);
107 else
108 count += snprintf(buf + count, PAGE_SIZE - count,
109 "%4x", codec->read(codec, i));
110
111 if (count >= PAGE_SIZE - 1)
112 break;
113
114 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
115 if (count >= PAGE_SIZE - 1)
116 break;
117 }
118
119 /* Truncate count; min() would cause a warning */
120 if (count >= PAGE_SIZE)
121 count = PAGE_SIZE - 1;
122
123 return count;
124}
125static ssize_t codec_reg_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
127{
128 struct snd_soc_device *devdata = dev_get_drvdata(dev);
129 return soc_codec_reg_show(devdata->card->codec, buf);
130}
131
132static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
133
Mark Browndbe21402010-02-12 11:37:24 +0000134static ssize_t pmdown_time_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
136{
137 struct snd_soc_device *socdev = dev_get_drvdata(dev);
138 struct snd_soc_card *card = socdev->card;
139
Mark Brown6c5f1fe2010-02-17 14:30:44 +0000140 return sprintf(buf, "%ld\n", card->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000141}
142
143static ssize_t pmdown_time_set(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
146{
147 struct snd_soc_device *socdev = dev_get_drvdata(dev);
148 struct snd_soc_card *card = socdev->card;
149
150 strict_strtol(buf, 10, &card->pmdown_time);
151
152 return count;
153}
154
155static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
156
Mark Brown2624d5f2009-11-03 21:56:13 +0000157#ifdef CONFIG_DEBUG_FS
158static int codec_reg_open_file(struct inode *inode, struct file *file)
159{
160 file->private_data = inode->i_private;
161 return 0;
162}
163
164static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
165 size_t count, loff_t *ppos)
166{
167 ssize_t ret;
168 struct snd_soc_codec *codec = file->private_data;
169 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
170 if (!buf)
171 return -ENOMEM;
172 ret = soc_codec_reg_show(codec, buf);
173 if (ret >= 0)
174 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
175 kfree(buf);
176 return ret;
177}
178
179static ssize_t codec_reg_write_file(struct file *file,
180 const char __user *user_buf, size_t count, loff_t *ppos)
181{
182 char buf[32];
183 int buf_size;
184 char *start = buf;
185 unsigned long reg, value;
186 int step = 1;
187 struct snd_soc_codec *codec = file->private_data;
188
189 buf_size = min(count, (sizeof(buf)-1));
190 if (copy_from_user(buf, user_buf, buf_size))
191 return -EFAULT;
192 buf[buf_size] = 0;
193
194 if (codec->reg_cache_step)
195 step = codec->reg_cache_step;
196
197 while (*start == ' ')
198 start++;
199 reg = simple_strtoul(start, &start, 16);
200 if ((reg >= codec->reg_cache_size) || (reg % step))
201 return -EINVAL;
202 while (*start == ' ')
203 start++;
204 if (strict_strtoul(start, 16, &value))
205 return -EINVAL;
206 codec->write(codec, reg, value);
207 return buf_size;
208}
209
210static const struct file_operations codec_reg_fops = {
211 .open = codec_reg_open_file,
212 .read = codec_reg_read_file,
213 .write = codec_reg_write_file,
214};
215
216static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
217{
218 char codec_root[128];
219
220 if (codec->dev)
221 snprintf(codec_root, sizeof(codec_root),
222 "%s.%s", codec->name, dev_name(codec->dev));
223 else
224 snprintf(codec_root, sizeof(codec_root),
225 "%s", codec->name);
226
227 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
228 debugfs_root);
229 if (!codec->debugfs_codec_root) {
230 printk(KERN_WARNING
231 "ASoC: Failed to create codec debugfs directory\n");
232 return;
233 }
234
235 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
236 codec->debugfs_codec_root,
237 codec, &codec_reg_fops);
238 if (!codec->debugfs_reg)
239 printk(KERN_WARNING
240 "ASoC: Failed to create codec register debugfs file\n");
241
242 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
243 codec->debugfs_codec_root,
244 &codec->pop_time);
245 if (!codec->debugfs_pop_time)
246 printk(KERN_WARNING
247 "Failed to create pop time debugfs file\n");
248
249 codec->debugfs_dapm = debugfs_create_dir("dapm",
250 codec->debugfs_codec_root);
251 if (!codec->debugfs_dapm)
252 printk(KERN_WARNING
253 "Failed to create DAPM debugfs directory\n");
254
255 snd_soc_dapm_debugfs_init(codec);
256}
257
258static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
259{
260 debugfs_remove_recursive(codec->debugfs_codec_root);
261}
262
263#else
264
265static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
266{
267}
268
269static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
270{
271}
272#endif
273
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200274#ifdef CONFIG_SND_SOC_AC97_BUS
275/* unregister ac97 codec */
276static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
277{
278 if (codec->ac97->dev.bus)
279 device_unregister(&codec->ac97->dev);
280 return 0;
281}
282
283/* stop no dev release warning */
284static void soc_ac97_device_release(struct device *dev){}
285
286/* register ac97 codec to bus */
287static int soc_ac97_dev_register(struct snd_soc_codec *codec)
288{
289 int err;
290
291 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100292 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293 codec->ac97->dev.release = soc_ac97_device_release;
294
Kay Sieversbb072bf2008-11-02 03:50:35 +0100295 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
296 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200297 err = device_register(&codec->ac97->dev);
298 if (err < 0) {
299 snd_printk(KERN_ERR "Can't register ac97 bus\n");
300 codec->ac97->dev.bus = NULL;
301 return err;
302 }
303 return 0;
304}
305#endif
306
Mark Brown06f409d2009-04-07 18:10:13 +0100307static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
308{
309 struct snd_soc_pcm_runtime *rtd = substream->private_data;
310 struct snd_soc_device *socdev = rtd->socdev;
311 struct snd_soc_card *card = socdev->card;
312 struct snd_soc_dai_link *machine = rtd->dai;
313 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
314 struct snd_soc_dai *codec_dai = machine->codec_dai;
315 int ret;
316
317 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
318 machine->symmetric_rates) {
319 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
320 machine->rate);
321
322 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
323 SNDRV_PCM_HW_PARAM_RATE,
324 machine->rate,
325 machine->rate);
326 if (ret < 0) {
327 dev_err(card->dev,
328 "Unable to apply rate symmetry constraint: %d\n", ret);
329 return ret;
330 }
331 }
332
333 return 0;
334}
335
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200336/*
337 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
338 * then initialized and any private data can be allocated. This also calls
339 * startup for the cpu DAI, platform, machine and codec DAI.
340 */
341static int soc_pcm_open(struct snd_pcm_substream *substream)
342{
343 struct snd_soc_pcm_runtime *rtd = substream->private_data;
344 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000345 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100347 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000348 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100349 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
350 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351 int ret = 0;
352
353 mutex_lock(&pcm_mutex);
354
355 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800356 if (cpu_dai->ops->startup) {
357 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200358 if (ret < 0) {
359 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100360 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200361 goto out;
362 }
363 }
364
365 if (platform->pcm_ops->open) {
366 ret = platform->pcm_ops->open(substream);
367 if (ret < 0) {
368 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
369 goto platform_err;
370 }
371 }
372
Eric Miao6335d052009-03-03 09:41:00 +0800373 if (codec_dai->ops->startup) {
374 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100375 if (ret < 0) {
376 printk(KERN_ERR "asoc: can't open codec %s\n",
377 codec_dai->name);
378 goto codec_dai_err;
379 }
380 }
381
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200382 if (machine->ops && machine->ops->startup) {
383 ret = machine->ops->startup(substream);
384 if (ret < 0) {
385 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
386 goto machine_err;
387 }
388 }
389
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200390 /* Check that the codec and cpu DAI's are compatible */
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
392 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200393 max(codec_dai->playback.rate_min,
394 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200395 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200396 min(codec_dai->playback.rate_max,
397 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200398 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100399 max(codec_dai->playback.channels_min,
400 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200401 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100402 min(codec_dai->playback.channels_max,
403 cpu_dai->playback.channels_max);
404 runtime->hw.formats =
405 codec_dai->playback.formats & cpu_dai->playback.formats;
406 runtime->hw.rates =
407 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200408 } else {
409 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200410 max(codec_dai->capture.rate_min,
411 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200412 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200413 min(codec_dai->capture.rate_max,
414 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200415 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100416 max(codec_dai->capture.channels_min,
417 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200418 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100419 min(codec_dai->capture.channels_max,
420 cpu_dai->capture.channels_max);
421 runtime->hw.formats =
422 codec_dai->capture.formats & cpu_dai->capture.formats;
423 runtime->hw.rates =
424 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 }
426
427 snd_pcm_limit_hw_rates(runtime);
428 if (!runtime->hw.rates) {
429 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100430 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900431 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200432 }
433 if (!runtime->hw.formats) {
434 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100435 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900436 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200437 }
438 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
439 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100440 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900441 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442 }
443
Mark Brown06f409d2009-04-07 18:10:13 +0100444 /* Symmetry only applies if we've already got an active stream. */
445 if (cpu_dai->active || codec_dai->active) {
446 ret = soc_pcm_apply_symmetry(substream);
447 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900448 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100449 }
450
Mark Brownf24368c2008-10-21 21:45:08 +0100451 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
452 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
453 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
454 runtime->hw.channels_max);
455 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
456 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100459 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100461 cpu_dai->capture.active = codec_dai->capture.active = 1;
462 cpu_dai->active = codec_dai->active = 1;
463 cpu_dai->runtime = runtime;
Mark Brown6627a652009-01-23 22:55:23 +0000464 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465 mutex_unlock(&pcm_mutex);
466 return 0;
467
Jassi Brarbb1c0472010-02-25 11:24:53 +0900468config_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 if (machine->ops && machine->ops->shutdown)
470 machine->ops->shutdown(substream);
471
Jassi Brarbb1c0472010-02-25 11:24:53 +0900472machine_err:
473 if (codec_dai->ops->shutdown)
474 codec_dai->ops->shutdown(substream, codec_dai);
475
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100476codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 if (platform->pcm_ops->close)
478 platform->pcm_ops->close(substream);
479
480platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800481 if (cpu_dai->ops->shutdown)
482 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483out:
484 mutex_unlock(&pcm_mutex);
485 return ret;
486}
487
488/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200489 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 * This is to ensure there are no pops or clicks in between any music tracks
491 * due to DAPM power cycling.
492 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100493static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494{
Mark Brown63084192008-12-02 15:08:03 +0000495 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
496 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000497 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100498 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499 int i;
500
501 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200502 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503 codec_dai = &codec->dai[i];
504
Mark Brownf24368c2008-10-21 21:45:08 +0100505 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
506 codec_dai->playback.stream_name,
507 codec_dai->playback.active ? "active" : "inactive",
508 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509
510 /* are we waiting on this codec DAI stream */
511 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100513 snd_soc_dapm_stream_event(codec,
514 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 }
517 }
518 mutex_unlock(&pcm_mutex);
519}
520
521/*
522 * Called by ALSA when a PCM substream is closed. Private data can be
523 * freed here. The cpu DAI, codec DAI, machine and platform are also
524 * shutdown.
525 */
526static int soc_codec_close(struct snd_pcm_substream *substream)
527{
528 struct snd_soc_pcm_runtime *rtd = substream->private_data;
529 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000530 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100531 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000532 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100533 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
534 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000535 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536
537 mutex_lock(&pcm_mutex);
538
539 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100540 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100544 if (codec_dai->playback.active == 0 &&
545 codec_dai->capture.active == 0) {
546 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 }
548 codec->active--;
549
Mark Brown6010b2d2008-09-06 18:33:24 +0100550 /* Muting the DAC suppresses artifacts caused during digital
551 * shutdown, for example from stopping clocks.
552 */
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
554 snd_soc_dai_digital_mute(codec_dai, 1);
555
Eric Miao6335d052009-03-03 09:41:00 +0800556 if (cpu_dai->ops->shutdown)
557 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558
Eric Miao6335d052009-03-03 09:41:00 +0800559 if (codec_dai->ops->shutdown)
560 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
562 if (machine->ops && machine->ops->shutdown)
563 machine->ops->shutdown(substream);
564
565 if (platform->pcm_ops->close)
566 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100567 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
570 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100571 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000572 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000573 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574 } else {
575 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100576 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100577 codec_dai->capture.stream_name,
578 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579 }
580
581 mutex_unlock(&pcm_mutex);
582 return 0;
583}
584
585/*
586 * Called by ALSA when the PCM substream is prepared, can set format, sample
587 * rate, etc. This function is non atomic and can be called multiple times,
588 * it can refer to the runtime info.
589 */
590static int soc_pcm_prepare(struct snd_pcm_substream *substream)
591{
592 struct snd_soc_pcm_runtime *rtd = substream->private_data;
593 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000594 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100595 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000596 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100597 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
598 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000599 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200600 int ret = 0;
601
602 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100603
604 if (machine->ops && machine->ops->prepare) {
605 ret = machine->ops->prepare(substream);
606 if (ret < 0) {
607 printk(KERN_ERR "asoc: machine prepare error\n");
608 goto out;
609 }
610 }
611
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 if (platform->pcm_ops->prepare) {
613 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200614 if (ret < 0) {
615 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200616 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200617 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200618 }
619
Eric Miao6335d052009-03-03 09:41:00 +0800620 if (codec_dai->ops->prepare) {
621 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200622 if (ret < 0) {
623 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200625 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626 }
627
Eric Miao6335d052009-03-03 09:41:00 +0800628 if (cpu_dai->ops->prepare) {
629 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100630 if (ret < 0) {
631 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
632 goto out;
633 }
634 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635
Mark Brownd45f6212008-10-14 13:58:36 +0100636 /* cancel any delayed stream shutdown that is pending */
637 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
638 codec_dai->pop_wait) {
639 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000640 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100641 }
642
Mark Brown452c5ea2009-05-17 21:41:23 +0100643 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
644 snd_soc_dapm_stream_event(codec,
645 codec_dai->playback.stream_name,
646 SND_SOC_DAPM_STREAM_START);
647 else
648 snd_soc_dapm_stream_event(codec,
649 codec_dai->capture.stream_name,
650 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100651
Mark Brown452c5ea2009-05-17 21:41:23 +0100652 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653
654out:
655 mutex_unlock(&pcm_mutex);
656 return ret;
657}
658
659/*
660 * Called by ALSA when the hardware params are set by application. This
661 * function can also be called multiple times and can allocate buffers
662 * (using snd_pcm_lib_* ). It's non-atomic.
663 */
664static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
665 struct snd_pcm_hw_params *params)
666{
667 struct snd_soc_pcm_runtime *rtd = substream->private_data;
668 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100669 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000670 struct snd_soc_card *card = socdev->card;
671 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100672 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
673 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674 int ret = 0;
675
676 mutex_lock(&pcm_mutex);
677
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100678 if (machine->ops && machine->ops->hw_params) {
679 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100681 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 goto out;
683 }
684 }
685
Eric Miao6335d052009-03-03 09:41:00 +0800686 if (codec_dai->ops->hw_params) {
687 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100688 if (ret < 0) {
689 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
690 codec_dai->name);
691 goto codec_err;
692 }
693 }
694
Eric Miao6335d052009-03-03 09:41:00 +0800695 if (cpu_dai->ops->hw_params) {
696 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200698 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100699 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 goto interface_err;
701 }
702 }
703
704 if (platform->pcm_ops->hw_params) {
705 ret = platform->pcm_ops->hw_params(substream, params);
706 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200707 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708 platform->name);
709 goto platform_err;
710 }
711 }
712
Mark Brown06f409d2009-04-07 18:10:13 +0100713 machine->rate = params_rate(params);
714
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715out:
716 mutex_unlock(&pcm_mutex);
717 return ret;
718
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800720 if (cpu_dai->ops->hw_free)
721 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722
723interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800724 if (codec_dai->ops->hw_free)
725 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100726
727codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200728 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100729 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730
731 mutex_unlock(&pcm_mutex);
732 return ret;
733}
734
735/*
736 * Free's resources allocated by hw_params, can be called multiple times
737 */
738static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
739{
740 struct snd_soc_pcm_runtime *rtd = substream->private_data;
741 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100742 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000743 struct snd_soc_card *card = socdev->card;
744 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100745 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
746 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000747 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748
749 mutex_lock(&pcm_mutex);
750
751 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100752 if (!codec->active)
753 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
755 /* free any machine hw params */
756 if (machine->ops && machine->ops->hw_free)
757 machine->ops->hw_free(substream);
758
759 /* free any DMA resources */
760 if (platform->pcm_ops->hw_free)
761 platform->pcm_ops->hw_free(substream);
762
763 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800764 if (codec_dai->ops->hw_free)
765 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766
Eric Miao6335d052009-03-03 09:41:00 +0800767 if (cpu_dai->ops->hw_free)
768 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
770 mutex_unlock(&pcm_mutex);
771 return 0;
772}
773
774static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
775{
776 struct snd_soc_pcm_runtime *rtd = substream->private_data;
777 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000778 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100779 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000780 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100781 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
782 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783 int ret;
784
Eric Miao6335d052009-03-03 09:41:00 +0800785 if (codec_dai->ops->trigger) {
786 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 if (ret < 0)
788 return ret;
789 }
790
791 if (platform->pcm_ops->trigger) {
792 ret = platform->pcm_ops->trigger(substream, cmd);
793 if (ret < 0)
794 return ret;
795 }
796
Eric Miao6335d052009-03-03 09:41:00 +0800797 if (cpu_dai->ops->trigger) {
798 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799 if (ret < 0)
800 return ret;
801 }
802 return 0;
803}
804
805/* ASoC PCM operations */
806static struct snd_pcm_ops soc_pcm_ops = {
807 .open = soc_pcm_open,
808 .close = soc_codec_close,
809 .hw_params = soc_pcm_hw_params,
810 .hw_free = soc_pcm_hw_free,
811 .prepare = soc_pcm_prepare,
812 .trigger = soc_pcm_trigger,
813};
814
815#ifdef CONFIG_PM
816/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100817static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818{
Mark Brown416356f2009-06-30 19:05:15 +0100819 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200820 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000821 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000822 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200823 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000824 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200825 int i;
826
Daniel Macke3509ff2009-06-03 17:44:49 +0200827 /* If the initialization of this soc device failed, there is no codec
828 * associated with it. Just bail out in this case.
829 */
830 if (!codec)
831 return 0;
832
Andy Green6ed25972008-06-13 16:24:05 +0100833 /* Due to the resume being scheduled into a workqueue we could
834 * suspend before that's finished - wait for it to complete.
835 */
836 snd_power_lock(codec->card);
837 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
838 snd_power_unlock(codec->card);
839
840 /* we're going to block userspace touching us until resume completes */
841 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
842
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000844 for (i = 0; i < card->num_links; i++) {
845 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800846 if (dai->ops->digital_mute && dai->playback.active)
847 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200848 }
849
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100850 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000851 for (i = 0; i < card->num_links; i++)
852 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100853
Mark Brown87506542008-11-18 20:50:34 +0000854 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100855 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856
Mark Brown87506542008-11-18 20:50:34 +0000857 for (i = 0; i < card->num_links; i++) {
858 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000859 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000860 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 if (platform->suspend)
Mark Brown07c84d02008-12-03 18:17:28 +0000862 platform->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 }
864
865 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000866 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200867 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868
Mark Brown3ff3f642008-05-19 12:32:25 +0200869 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200870 char *stream = codec->dai[i].playback.stream_name;
871 if (stream != NULL)
872 snd_soc_dapm_stream_event(codec, stream,
873 SND_SOC_DAPM_STREAM_SUSPEND);
874 stream = codec->dai[i].capture.stream_name;
875 if (stream != NULL)
876 snd_soc_dapm_stream_event(codec, stream,
877 SND_SOC_DAPM_STREAM_SUSPEND);
878 }
879
880 if (codec_dev->suspend)
Mark Brown416356f2009-06-30 19:05:15 +0100881 codec_dev->suspend(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
Mark Brown87506542008-11-18 20:50:34 +0000883 for (i = 0; i < card->num_links; i++) {
884 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000885 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000886 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887 }
888
Mark Brown87506542008-11-18 20:50:34 +0000889 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100890 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200891
892 return 0;
893}
894
Andy Green6ed25972008-06-13 16:24:05 +0100895/* deferred resume work, so resume can complete before we finished
896 * setting our codec back up, which can be very slow on I2C
897 */
898static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899{
Mark Brown63084192008-12-02 15:08:03 +0000900 struct snd_soc_card *card = container_of(work,
901 struct snd_soc_card,
902 deferred_resume_work);
903 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000904 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200905 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000906 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100907 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 int i;
909
Andy Green6ed25972008-06-13 16:24:05 +0100910 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
911 * so userspace apps are blocked from touching us
912 */
913
Mark Brownfde22f22008-11-24 18:08:18 +0000914 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100915
Mark Brown87506542008-11-18 20:50:34 +0000916 if (card->resume_pre)
917 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200918
Mark Brown87506542008-11-18 20:50:34 +0000919 for (i = 0; i < card->num_links; i++) {
920 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000921 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000922 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200923 }
924
925 if (codec_dev->resume)
926 codec_dev->resume(pdev);
927
Mark Brown3ff3f642008-05-19 12:32:25 +0200928 for (i = 0; i < codec->num_dai; i++) {
929 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200930 if (stream != NULL)
931 snd_soc_dapm_stream_event(codec, stream,
932 SND_SOC_DAPM_STREAM_RESUME);
933 stream = codec->dai[i].capture.stream_name;
934 if (stream != NULL)
935 snd_soc_dapm_stream_event(codec, stream,
936 SND_SOC_DAPM_STREAM_RESUME);
937 }
938
Mark Brown3ff3f642008-05-19 12:32:25 +0200939 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000940 for (i = 0; i < card->num_links; i++) {
941 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800942 if (dai->ops->digital_mute && dai->playback.active)
943 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200944 }
945
Mark Brown87506542008-11-18 20:50:34 +0000946 for (i = 0; i < card->num_links; i++) {
947 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000948 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000949 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950 if (platform->resume)
Mark Brown07c84d02008-12-03 18:17:28 +0000951 platform->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200952 }
953
Mark Brown87506542008-11-18 20:50:34 +0000954 if (card->resume_post)
955 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200956
Mark Brownfde22f22008-11-24 18:08:18 +0000957 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100958
959 /* userspace can access us now we are back as we were before */
960 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
961}
962
963/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100964static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100965{
Mark Brown416356f2009-06-30 19:05:15 +0100966 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +0100967 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000968 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +0100969 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +0100970
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200971 /* If the initialization of this soc device failed, there is no codec
972 * associated with it. Just bail out in this case.
973 */
974 if (!card->codec)
975 return 0;
976
Mark Brown64ab9ba2009-03-31 11:27:03 +0100977 /* AC97 devices might have other drivers hanging off them so
978 * need to resume immediately. Other drivers don't have that
979 * problem and may take a substantial amount of time to resume
980 * due to I/O costs and anti-pop so handle them out of line.
981 */
982 if (cpu_dai->ac97_control) {
983 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
984 soc_resume_deferred(&card->deferred_resume_work);
985 } else {
986 dev_dbg(socdev->dev, "Scheduling resume work\n");
987 if (!schedule_work(&card->deferred_resume_work))
988 dev_err(socdev->dev, "resume work item may be lost\n");
989 }
Andy Green6ed25972008-06-13 16:24:05 +0100990
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200991 return 0;
992}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200993#else
994#define soc_suspend NULL
995#define soc_resume NULL
996#endif
997
Barry Song02a06d32009-10-16 18:13:38 +0800998static struct snd_soc_dai_ops null_dai_ops = {
999};
1000
Mark Brown435c5e22008-12-04 15:32:53 +00001001static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001002{
Mark Brown435c5e22008-12-04 15:32:53 +00001003 struct platform_device *pdev = container_of(card->dev,
1004 struct platform_device,
1005 dev);
1006 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001007 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001008 struct snd_soc_platform *platform;
1009 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001010 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001011
Mark Brown435c5e22008-12-04 15:32:53 +00001012 if (card->instantiated)
1013 return;
Mark Brown63084192008-12-02 15:08:03 +00001014
Mark Brown435c5e22008-12-04 15:32:53 +00001015 found = 0;
1016 list_for_each_entry(platform, &platform_list, list)
1017 if (card->platform == platform) {
1018 found = 1;
1019 break;
1020 }
1021 if (!found) {
1022 dev_dbg(card->dev, "Platform %s not registered\n",
1023 card->platform->name);
1024 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001025 }
1026
Mark Brown6b05eda2008-12-08 19:26:48 +00001027 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001028 for (i = 0; i < card->num_links; i++) {
1029 found = 0;
1030 list_for_each_entry(dai, &dai_list, list)
1031 if (card->dai_link[i].cpu_dai == dai) {
1032 found = 1;
1033 break;
1034 }
1035 if (!found) {
1036 dev_dbg(card->dev, "DAI %s not registered\n",
1037 card->dai_link[i].cpu_dai->name);
1038 return;
1039 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001040
1041 if (card->dai_link[i].cpu_dai->ac97_control)
1042 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001043 }
1044
Barry Song02a06d32009-10-16 18:13:38 +08001045 for (i = 0; i < card->num_links; i++) {
1046 if (!card->dai_link[i].codec_dai->ops)
1047 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1048 }
1049
Mark Brown6b05eda2008-12-08 19:26:48 +00001050 /* If we have AC97 in the system then don't wait for the
1051 * codec. This will need revisiting if we have to handle
1052 * systems with mixed AC97 and non-AC97 parts. Only check for
1053 * DAIs currently; we can't do this per link since some AC97
1054 * codecs have non-AC97 DAIs.
1055 */
1056 if (!ac97)
1057 for (i = 0; i < card->num_links; i++) {
1058 found = 0;
1059 list_for_each_entry(dai, &dai_list, list)
1060 if (card->dai_link[i].codec_dai == dai) {
1061 found = 1;
1062 break;
1063 }
1064 if (!found) {
1065 dev_dbg(card->dev, "DAI %s not registered\n",
1066 card->dai_link[i].codec_dai->name);
1067 return;
1068 }
1069 }
1070
Mark Brown435c5e22008-12-04 15:32:53 +00001071 /* Note that we do not current check for codec components */
1072
1073 dev_dbg(card->dev, "All components present, instantiating\n");
1074
1075 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001076 card->pmdown_time = pmdown_time;
1077
Mark Brown87506542008-11-18 20:50:34 +00001078 if (card->probe) {
1079 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001080 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001081 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001082 }
1083
Mark Brown87506542008-11-18 20:50:34 +00001084 for (i = 0; i < card->num_links; i++) {
1085 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001086 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001087 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001088 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001089 goto cpu_dai_err;
1090 }
1091 }
1092
1093 if (codec_dev->probe) {
1094 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001095 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001096 goto cpu_dai_err;
1097 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001098 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001099
1100 if (platform->probe) {
1101 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001102 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001103 goto platform_err;
1104 }
1105
1106 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001107 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001108#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001109 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001110 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001111#endif
Andy Green6ed25972008-06-13 16:24:05 +01001112
Mark Brownfe3e78e2009-11-03 22:13:13 +00001113 for (i = 0; i < card->num_links; i++) {
1114 if (card->dai_link[i].init) {
1115 ret = card->dai_link[i].init(codec);
1116 if (ret < 0) {
1117 printk(KERN_ERR "asoc: failed to init %s\n",
1118 card->dai_link[i].stream_name);
1119 continue;
1120 }
1121 }
Barry Songf7732052009-11-12 12:01:47 +08001122 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001123 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001124 }
1125
1126 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1127 "%s", card->name);
1128 snprintf(codec->card->longname, sizeof(codec->card->longname),
1129 "%s (%s)", card->name, codec->name);
1130
1131 /* Make sure all DAPM widgets are instantiated */
1132 snd_soc_dapm_new_widgets(codec);
1133
1134 ret = snd_card_register(codec->card);
1135 if (ret < 0) {
1136 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1137 codec->name);
1138 goto card_err;
1139 }
1140
1141 mutex_lock(&codec->mutex);
1142#ifdef CONFIG_SND_SOC_AC97_BUS
1143 /* Only instantiate AC97 if not already done by the adaptor
1144 * for the generic AC97 subsystem.
1145 */
1146 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1147 ret = soc_ac97_dev_register(codec);
1148 if (ret < 0) {
1149 printk(KERN_ERR "asoc: AC97 device register failed\n");
1150 snd_card_free(codec->card);
1151 mutex_unlock(&codec->mutex);
1152 goto card_err;
1153 }
1154 }
1155#endif
1156
1157 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1158 if (ret < 0)
1159 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1160
Mark Browndbe21402010-02-12 11:37:24 +00001161 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1162 if (ret < 0)
1163 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1164
Mark Brownfe3e78e2009-11-03 22:13:13 +00001165 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1166 if (ret < 0)
1167 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1168
1169 soc_init_codec_debugfs(codec);
1170 mutex_unlock(&codec->mutex);
1171
Mark Brown435c5e22008-12-04 15:32:53 +00001172 card->instantiated = 1;
1173
1174 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001175
Mark Brownfe3e78e2009-11-03 22:13:13 +00001176card_err:
1177 if (platform->remove)
1178 platform->remove(pdev);
1179
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001180platform_err:
1181 if (codec_dev->remove)
1182 codec_dev->remove(pdev);
1183
1184cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001185 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001186 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001187 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001188 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001189 }
1190
Mark Brown87506542008-11-18 20:50:34 +00001191 if (card->remove)
1192 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001193}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001194
Mark Brown435c5e22008-12-04 15:32:53 +00001195/*
1196 * Attempt to initialise any uninitalised cards. Must be called with
1197 * client_mutex.
1198 */
1199static void snd_soc_instantiate_cards(void)
1200{
1201 struct snd_soc_card *card;
1202 list_for_each_entry(card, &card_list, list)
1203 snd_soc_instantiate_card(card);
1204}
1205
1206/* probes a new socdev */
1207static int soc_probe(struct platform_device *pdev)
1208{
1209 int ret = 0;
1210 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1211 struct snd_soc_card *card = socdev->card;
1212
1213 /* Bodge while we push things out of socdev */
1214 card->socdev = socdev;
1215
1216 /* Bodge while we unpick instantiation */
1217 card->dev = &pdev->dev;
1218 ret = snd_soc_register_card(card);
1219 if (ret != 0) {
1220 dev_err(&pdev->dev, "Failed to register card\n");
1221 return ret;
1222 }
1223
1224 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001225}
1226
1227/* removes a socdev */
1228static int soc_remove(struct platform_device *pdev)
1229{
1230 int i;
1231 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001232 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001233 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001234 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1235
Mike Rapoport914dc182009-05-11 13:04:55 +03001236 if (!card->instantiated)
1237 return 0;
1238
Mark Brown63084192008-12-02 15:08:03 +00001239 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +01001240
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001241 if (platform->remove)
1242 platform->remove(pdev);
1243
1244 if (codec_dev->remove)
1245 codec_dev->remove(pdev);
1246
Mark Brown87506542008-11-18 20:50:34 +00001247 for (i = 0; i < card->num_links; i++) {
1248 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001249 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001250 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001251 }
1252
Mark Brown87506542008-11-18 20:50:34 +00001253 if (card->remove)
1254 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001255
Mark Brownc5af3a22008-11-28 13:29:45 +00001256 snd_soc_unregister_card(card);
1257
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001258 return 0;
1259}
1260
Mark Brown416356f2009-06-30 19:05:15 +01001261static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001262{
Mark Brown416356f2009-06-30 19:05:15 +01001263 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001264 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1265 struct snd_soc_card *card = socdev->card;
1266
1267 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001268 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001269
1270 /* Flush out pmdown_time work - we actually do want to run it
1271 * now, we're shutting down so no imminent restart. */
1272 run_delayed_work(&card->delayed_work);
1273
1274 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001275
1276 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001277}
1278
Alexey Dobriyan47145212009-12-14 18:00:08 -08001279static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001280 .suspend = soc_suspend,
1281 .resume = soc_resume,
1282 .poweroff = soc_poweroff,
1283};
1284
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001285/* ASoC platform driver */
1286static struct platform_driver soc_driver = {
1287 .driver = {
1288 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001289 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001290 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001291 },
1292 .probe = soc_probe,
1293 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001294};
1295
1296/* create a new pcm */
1297static int soc_new_pcm(struct snd_soc_device *socdev,
1298 struct snd_soc_dai_link *dai_link, int num)
1299{
Mark Brown87689d52008-12-02 16:01:14 +00001300 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001301 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001302 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001303 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1304 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001305 struct snd_soc_pcm_runtime *rtd;
1306 struct snd_pcm *pcm;
1307 char new_name[64];
1308 int ret = 0, playback = 0, capture = 0;
1309
1310 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1311 if (rtd == NULL)
1312 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001313
1314 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001315 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001316 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001317
1318 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001319 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1320 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001321
1322 if (codec_dai->playback.channels_min)
1323 playback = 1;
1324 if (codec_dai->capture.channels_min)
1325 capture = 1;
1326
1327 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1328 capture, &pcm);
1329 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001330 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1331 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332 kfree(rtd);
1333 return ret;
1334 }
1335
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001336 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001337 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001338 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1339 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1340 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1341 soc_pcm_ops.copy = platform->pcm_ops->copy;
1342 soc_pcm_ops.silence = platform->pcm_ops->silence;
1343 soc_pcm_ops.ack = platform->pcm_ops->ack;
1344 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001345
1346 if (playback)
1347 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1348
1349 if (capture)
1350 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1351
Mark Brown87689d52008-12-02 16:01:14 +00001352 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001353 if (ret < 0) {
1354 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1355 kfree(rtd);
1356 return ret;
1357 }
1358
Mark Brown87689d52008-12-02 16:01:14 +00001359 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001360 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1361 cpu_dai->name);
1362 return ret;
1363}
1364
Mark Brown096e49d2009-07-05 15:12:22 +01001365/**
1366 * snd_soc_codec_volatile_register: Report if a register is volatile.
1367 *
1368 * @codec: CODEC to query.
1369 * @reg: Register to query.
1370 *
1371 * Boolean function indiciating if a CODEC register is volatile.
1372 */
1373int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1374{
1375 if (codec->volatile_register)
1376 return codec->volatile_register(reg);
1377 else
1378 return 0;
1379}
1380EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1381
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001382/**
1383 * snd_soc_new_ac97_codec - initailise AC97 device
1384 * @codec: audio codec
1385 * @ops: AC97 bus operations
1386 * @num: AC97 codec number
1387 *
1388 * Initialises AC97 codec resources for use by ad-hoc devices only.
1389 */
1390int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1391 struct snd_ac97_bus_ops *ops, int num)
1392{
1393 mutex_lock(&codec->mutex);
1394
1395 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1396 if (codec->ac97 == NULL) {
1397 mutex_unlock(&codec->mutex);
1398 return -ENOMEM;
1399 }
1400
1401 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1402 if (codec->ac97->bus == NULL) {
1403 kfree(codec->ac97);
1404 codec->ac97 = NULL;
1405 mutex_unlock(&codec->mutex);
1406 return -ENOMEM;
1407 }
1408
1409 codec->ac97->bus->ops = ops;
1410 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001411 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001412 mutex_unlock(&codec->mutex);
1413 return 0;
1414}
1415EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1416
1417/**
1418 * snd_soc_free_ac97_codec - free AC97 codec device
1419 * @codec: audio codec
1420 *
1421 * Frees AC97 codec device resources.
1422 */
1423void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1424{
1425 mutex_lock(&codec->mutex);
1426 kfree(codec->ac97->bus);
1427 kfree(codec->ac97);
1428 codec->ac97 = NULL;
1429 mutex_unlock(&codec->mutex);
1430}
1431EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1432
1433/**
1434 * snd_soc_update_bits - update codec register bits
1435 * @codec: audio codec
1436 * @reg: codec register
1437 * @mask: register mask
1438 * @value: new value
1439 *
1440 * Writes new register value.
1441 *
1442 * Returns 1 for change else 0.
1443 */
1444int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001445 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001446{
1447 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001448 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001450 old = snd_soc_read(codec, reg);
1451 new = (old & ~mask) | value;
1452 change = old != new;
1453 if (change)
1454 snd_soc_write(codec, reg, new);
1455
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001456 return change;
1457}
1458EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1459
1460/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001461 * snd_soc_update_bits_locked - 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, and takes the codec mutex.
1468 *
1469 * Returns 1 for change else 0.
1470 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001471int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1472 unsigned short reg, unsigned int mask,
1473 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001474{
1475 int change;
1476
1477 mutex_lock(&codec->mutex);
1478 change = snd_soc_update_bits(codec, reg, mask, value);
1479 mutex_unlock(&codec->mutex);
1480
1481 return change;
1482}
Mark Browndd1b3d52009-12-04 14:22:03 +00001483EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001484
1485/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001486 * snd_soc_test_bits - test register for change
1487 * @codec: audio codec
1488 * @reg: codec register
1489 * @mask: register mask
1490 * @value: new value
1491 *
1492 * Tests a register with a new value and checks if the new value is
1493 * different from the old value.
1494 *
1495 * Returns 1 for change else 0.
1496 */
1497int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001498 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001499{
1500 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001501 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001502
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503 old = snd_soc_read(codec, reg);
1504 new = (old & ~mask) | value;
1505 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506
1507 return change;
1508}
1509EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1510
1511/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001512 * snd_soc_new_pcms - create new sound card and pcms
1513 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001514 * @idx: ALSA card index
1515 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001516 *
1517 * Create a new sound card based upon the codec and interface pcms.
1518 *
1519 * Returns 0 for success, else error.
1520 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001521int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001522{
Mark Brown87506542008-11-18 20:50:34 +00001523 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001524 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001525 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526
1527 mutex_lock(&codec->mutex);
1528
1529 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001530 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1531 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001532 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1533 codec->name);
1534 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001535 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 }
1537
Mark Brown452c5ea2009-05-17 21:41:23 +01001538 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539 codec->card->dev = socdev->dev;
1540 codec->card->private_data = codec;
1541 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1542
1543 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001544 for (i = 0; i < card->num_links; i++) {
1545 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001546 if (ret < 0) {
1547 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001548 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549 mutex_unlock(&codec->mutex);
1550 return ret;
1551 }
Graham Gowerfb48e3c2010-03-25 10:52:12 +10301552 /* Check for codec->ac97 to handle the ac97.c fun */
1553 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
Barry Songf7732052009-11-12 12:01:47 +08001554 snd_ac97_dev_add_pdata(codec->ac97,
1555 card->dai_link[i].cpu_dai->ac97_pdata);
1556 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001557 }
1558
1559 mutex_unlock(&codec->mutex);
1560 return ret;
1561}
1562EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1563
1564/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001565 * snd_soc_free_pcms - free sound card and pcms
1566 * @socdev: the SoC audio device
1567 *
1568 * Frees sound card and pcms associated with the socdev.
1569 * Also unregister the codec if it is an AC97 device.
1570 */
1571void snd_soc_free_pcms(struct snd_soc_device *socdev)
1572{
Mark Brown6627a652009-01-23 22:55:23 +00001573 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001574#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001575 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001576 int i;
1577#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578
1579 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001580 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001581#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001582 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001583 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001584 if (codec_dai->ac97_control && codec->ac97 &&
1585 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001586 soc_ac97_dev_unregister(codec);
1587 goto free_card;
1588 }
1589 }
1590free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001591#endif
1592
1593 if (codec->card)
1594 snd_card_free(codec->card);
1595 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1596 mutex_unlock(&codec->mutex);
1597}
1598EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1599
1600/**
1601 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1602 * @substream: the pcm substream
1603 * @hw: the hardware parameters
1604 *
1605 * Sets the substream runtime hardware parameters.
1606 */
1607int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1608 const struct snd_pcm_hardware *hw)
1609{
1610 struct snd_pcm_runtime *runtime = substream->runtime;
1611 runtime->hw.info = hw->info;
1612 runtime->hw.formats = hw->formats;
1613 runtime->hw.period_bytes_min = hw->period_bytes_min;
1614 runtime->hw.period_bytes_max = hw->period_bytes_max;
1615 runtime->hw.periods_min = hw->periods_min;
1616 runtime->hw.periods_max = hw->periods_max;
1617 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1618 runtime->hw.fifo_size = hw->fifo_size;
1619 return 0;
1620}
1621EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1622
1623/**
1624 * snd_soc_cnew - create new control
1625 * @_template: control template
1626 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001627 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001628 *
1629 * Create a new mixer control from a template control.
1630 *
1631 * Returns 0 for success, else error.
1632 */
1633struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1634 void *data, char *long_name)
1635{
1636 struct snd_kcontrol_new template;
1637
1638 memcpy(&template, _template, sizeof(template));
1639 if (long_name)
1640 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001641 template.index = 0;
1642
1643 return snd_ctl_new1(&template, data);
1644}
1645EXPORT_SYMBOL_GPL(snd_soc_cnew);
1646
1647/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001648 * snd_soc_add_controls - add an array of controls to a codec.
1649 * Convienience function to add a list of controls. Many codecs were
1650 * duplicating this code.
1651 *
1652 * @codec: codec to add controls to
1653 * @controls: array of controls to add
1654 * @num_controls: number of elements in the array
1655 *
1656 * Return 0 for success, else error.
1657 */
1658int snd_soc_add_controls(struct snd_soc_codec *codec,
1659 const struct snd_kcontrol_new *controls, int num_controls)
1660{
1661 struct snd_card *card = codec->card;
1662 int err, i;
1663
1664 for (i = 0; i < num_controls; i++) {
1665 const struct snd_kcontrol_new *control = &controls[i];
1666 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1667 if (err < 0) {
1668 dev_err(codec->dev, "%s: Failed to add %s\n",
1669 codec->name, control->name);
1670 return err;
1671 }
1672 }
1673
1674 return 0;
1675}
1676EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1677
1678/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679 * snd_soc_info_enum_double - enumerated double mixer info callback
1680 * @kcontrol: mixer control
1681 * @uinfo: control element information
1682 *
1683 * Callback to provide information about a double enumerated
1684 * mixer control.
1685 *
1686 * Returns 0 for success.
1687 */
1688int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_info *uinfo)
1690{
1691 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1692
1693 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1694 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001695 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001696
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001697 if (uinfo->value.enumerated.item > e->max - 1)
1698 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001699 strcpy(uinfo->value.enumerated.name,
1700 e->texts[uinfo->value.enumerated.item]);
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1704
1705/**
1706 * snd_soc_get_enum_double - enumerated double mixer get callback
1707 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001708 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001709 *
1710 * Callback to get the value of a double enumerated mixer.
1711 *
1712 * Returns 0 for success.
1713 */
1714int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
1716{
1717 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1718 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001719 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001720
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001721 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001722 ;
1723 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001724 ucontrol->value.enumerated.item[0]
1725 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001726 if (e->shift_l != e->shift_r)
1727 ucontrol->value.enumerated.item[1] =
1728 (val >> e->shift_r) & (bitmask - 1);
1729
1730 return 0;
1731}
1732EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1733
1734/**
1735 * snd_soc_put_enum_double - enumerated double mixer put callback
1736 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001737 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 *
1739 * Callback to set the value of a double enumerated mixer.
1740 *
1741 * Returns 0 for success.
1742 */
1743int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
1745{
1746 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1747 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001748 unsigned int val;
1749 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001750
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001751 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001753 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001754 return -EINVAL;
1755 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1756 mask = (bitmask - 1) << e->shift_l;
1757 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001758 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001759 return -EINVAL;
1760 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1761 mask |= (bitmask - 1) << e->shift_r;
1762 }
1763
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001764 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001765}
1766EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1767
1768/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001769 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1770 * @kcontrol: mixer control
1771 * @ucontrol: control element information
1772 *
1773 * Callback to get the value of a double semi enumerated mixer.
1774 *
1775 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1776 * used for handling bitfield coded enumeration for example.
1777 *
1778 * Returns 0 for success.
1779 */
1780int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
1782{
1783 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001784 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001785 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001786
1787 reg_val = snd_soc_read(codec, e->reg);
1788 val = (reg_val >> e->shift_l) & e->mask;
1789 for (mux = 0; mux < e->max; mux++) {
1790 if (val == e->values[mux])
1791 break;
1792 }
1793 ucontrol->value.enumerated.item[0] = mux;
1794 if (e->shift_l != e->shift_r) {
1795 val = (reg_val >> e->shift_r) & e->mask;
1796 for (mux = 0; mux < e->max; mux++) {
1797 if (val == e->values[mux])
1798 break;
1799 }
1800 ucontrol->value.enumerated.item[1] = mux;
1801 }
1802
1803 return 0;
1804}
1805EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1806
1807/**
1808 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1809 * @kcontrol: mixer control
1810 * @ucontrol: control element information
1811 *
1812 * Callback to set the value of a double semi enumerated mixer.
1813 *
1814 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1815 * used for handling bitfield coded enumeration for example.
1816 *
1817 * Returns 0 for success.
1818 */
1819int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1821{
1822 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001823 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001824 unsigned int val;
1825 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001826
1827 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1828 return -EINVAL;
1829 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1830 mask = e->mask << e->shift_l;
1831 if (e->shift_l != e->shift_r) {
1832 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1833 return -EINVAL;
1834 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1835 mask |= e->mask << e->shift_r;
1836 }
1837
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001838 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001839}
1840EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1841
1842/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001843 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1844 * @kcontrol: mixer control
1845 * @uinfo: control element information
1846 *
1847 * Callback to provide information about an external enumerated
1848 * single mixer.
1849 *
1850 * Returns 0 for success.
1851 */
1852int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_info *uinfo)
1854{
1855 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1856
1857 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1858 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001859 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001860
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001861 if (uinfo->value.enumerated.item > e->max - 1)
1862 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001863 strcpy(uinfo->value.enumerated.name,
1864 e->texts[uinfo->value.enumerated.item]);
1865 return 0;
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1868
1869/**
1870 * snd_soc_info_volsw_ext - external single mixer info callback
1871 * @kcontrol: mixer control
1872 * @uinfo: control element information
1873 *
1874 * Callback to provide information about a single external mixer control.
1875 *
1876 * Returns 0 for success.
1877 */
1878int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1879 struct snd_ctl_elem_info *uinfo)
1880{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001881 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882
Mark Brownfd5dfad2009-04-15 21:37:46 +01001883 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001884 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1885 else
1886 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1887
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001888 uinfo->count = 1;
1889 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001890 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001891 return 0;
1892}
1893EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1894
1895/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896 * snd_soc_info_volsw - single mixer info callback
1897 * @kcontrol: mixer control
1898 * @uinfo: control element information
1899 *
1900 * Callback to provide information about a single mixer control.
1901 *
1902 * Returns 0 for success.
1903 */
1904int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
1906{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001907 struct soc_mixer_control *mc =
1908 (struct soc_mixer_control *)kcontrol->private_value;
1909 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001910 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001911 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001912
Mark Brownfd5dfad2009-04-15 21:37:46 +01001913 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001914 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1915 else
1916 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1917
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001918 uinfo->count = shift == rshift ? 1 : 2;
1919 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001920 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001921 return 0;
1922}
1923EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1924
1925/**
1926 * snd_soc_get_volsw - single mixer get callback
1927 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001928 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929 *
1930 * Callback to get the value of a single mixer control.
1931 *
1932 * Returns 0 for success.
1933 */
1934int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001937 struct soc_mixer_control *mc =
1938 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001939 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001940 unsigned int reg = mc->reg;
1941 unsigned int shift = mc->shift;
1942 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001943 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001944 unsigned int mask = (1 << fls(max)) - 1;
1945 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946
1947 ucontrol->value.integer.value[0] =
1948 (snd_soc_read(codec, reg) >> shift) & mask;
1949 if (shift != rshift)
1950 ucontrol->value.integer.value[1] =
1951 (snd_soc_read(codec, reg) >> rshift) & mask;
1952 if (invert) {
1953 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001954 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955 if (shift != rshift)
1956 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001957 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001958 }
1959
1960 return 0;
1961}
1962EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1963
1964/**
1965 * snd_soc_put_volsw - single mixer put callback
1966 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001967 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968 *
1969 * Callback to set the value of a single mixer control.
1970 *
1971 * Returns 0 for success.
1972 */
1973int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1974 struct snd_ctl_elem_value *ucontrol)
1975{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001976 struct soc_mixer_control *mc =
1977 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001979 unsigned int reg = mc->reg;
1980 unsigned int shift = mc->shift;
1981 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001982 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001983 unsigned int mask = (1 << fls(max)) - 1;
1984 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001985 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986
1987 val = (ucontrol->value.integer.value[0] & mask);
1988 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001989 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990 val_mask = mask << shift;
1991 val = val << shift;
1992 if (shift != rshift) {
1993 val2 = (ucontrol->value.integer.value[1] & mask);
1994 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001995 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996 val_mask |= mask << rshift;
1997 val |= val2 << rshift;
1998 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001999 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000}
2001EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2002
2003/**
2004 * snd_soc_info_volsw_2r - double mixer info callback
2005 * @kcontrol: mixer control
2006 * @uinfo: control element information
2007 *
2008 * Callback to provide information about a double mixer control that
2009 * spans 2 codec registers.
2010 *
2011 * Returns 0 for success.
2012 */
2013int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_info *uinfo)
2015{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002016 struct soc_mixer_control *mc =
2017 (struct soc_mixer_control *)kcontrol->private_value;
2018 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019
Mark Brownfd5dfad2009-04-15 21:37:46 +01002020 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002021 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2022 else
2023 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2024
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002025 uinfo->count = 2;
2026 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002027 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2031
2032/**
2033 * snd_soc_get_volsw_2r - double mixer get callback
2034 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002035 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 *
2037 * Callback to get the value of a double mixer control that spans 2 registers.
2038 *
2039 * Returns 0 for success.
2040 */
2041int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_value *ucontrol)
2043{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002044 struct soc_mixer_control *mc =
2045 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002047 unsigned int reg = mc->reg;
2048 unsigned int reg2 = mc->rreg;
2049 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002050 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002051 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002052 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002053
2054 ucontrol->value.integer.value[0] =
2055 (snd_soc_read(codec, reg) >> shift) & mask;
2056 ucontrol->value.integer.value[1] =
2057 (snd_soc_read(codec, reg2) >> shift) & mask;
2058 if (invert) {
2059 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002060 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002062 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002063 }
2064
2065 return 0;
2066}
2067EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2068
2069/**
2070 * snd_soc_put_volsw_2r - double mixer set callback
2071 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002072 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002073 *
2074 * Callback to set the value of a double mixer control that spans 2 registers.
2075 *
2076 * Returns 0 for success.
2077 */
2078int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2079 struct snd_ctl_elem_value *ucontrol)
2080{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002081 struct soc_mixer_control *mc =
2082 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002084 unsigned int reg = mc->reg;
2085 unsigned int reg2 = mc->rreg;
2086 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002087 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002088 unsigned int mask = (1 << fls(max)) - 1;
2089 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002091 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002092
2093 val_mask = mask << shift;
2094 val = (ucontrol->value.integer.value[0] & mask);
2095 val2 = (ucontrol->value.integer.value[1] & mask);
2096
2097 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002098 val = max - val;
2099 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100 }
2101
2102 val = val << shift;
2103 val2 = val2 << shift;
2104
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002105 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002106 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107 return err;
2108
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002109 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110 return err;
2111}
2112EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2113
Mark Browne13ac2e2008-05-28 17:58:05 +01002114/**
2115 * snd_soc_info_volsw_s8 - signed mixer info callback
2116 * @kcontrol: mixer control
2117 * @uinfo: control element information
2118 *
2119 * Callback to provide information about a signed mixer control.
2120 *
2121 * Returns 0 for success.
2122 */
2123int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_info *uinfo)
2125{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002126 struct soc_mixer_control *mc =
2127 (struct soc_mixer_control *)kcontrol->private_value;
2128 int max = mc->max;
2129 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002130
2131 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2132 uinfo->count = 2;
2133 uinfo->value.integer.min = 0;
2134 uinfo->value.integer.max = max-min;
2135 return 0;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2138
2139/**
2140 * snd_soc_get_volsw_s8 - signed mixer get callback
2141 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002142 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002143 *
2144 * Callback to get the value of a signed mixer control.
2145 *
2146 * Returns 0 for success.
2147 */
2148int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2149 struct snd_ctl_elem_value *ucontrol)
2150{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002151 struct soc_mixer_control *mc =
2152 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002153 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002154 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002155 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002156 int val = snd_soc_read(codec, reg);
2157
2158 ucontrol->value.integer.value[0] =
2159 ((signed char)(val & 0xff))-min;
2160 ucontrol->value.integer.value[1] =
2161 ((signed char)((val >> 8) & 0xff))-min;
2162 return 0;
2163}
2164EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2165
2166/**
2167 * snd_soc_put_volsw_sgn - signed mixer put callback
2168 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002169 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002170 *
2171 * Callback to set the value of a signed mixer control.
2172 *
2173 * Returns 0 for success.
2174 */
2175int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2177{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002178 struct soc_mixer_control *mc =
2179 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002180 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002181 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002182 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002183 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002184
2185 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2186 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2187
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002188 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002189}
2190EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2191
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002192/**
2193 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2194 * @dai: DAI
2195 * @clk_id: DAI specific clock ID
2196 * @freq: new clock frequency in Hz
2197 * @dir: new clock direction - input/output.
2198 *
2199 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2200 */
2201int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2202 unsigned int freq, int dir)
2203{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002204 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002205 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002206 else
2207 return -EINVAL;
2208}
2209EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2210
2211/**
2212 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2213 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002214 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002215 * @div: new clock divisor.
2216 *
2217 * Configures the clock dividers. This is used to derive the best DAI bit and
2218 * frame clocks from the system or master clock. It's best to set the DAI bit
2219 * and frame clocks as low as possible to save system power.
2220 */
2221int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2222 int div_id, int div)
2223{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002224 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002225 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002226 else
2227 return -EINVAL;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2230
2231/**
2232 * snd_soc_dai_set_pll - configure DAI PLL.
2233 * @dai: DAI
2234 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002235 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002236 * @freq_in: PLL input clock frequency in Hz
2237 * @freq_out: requested PLL output clock frequency in Hz
2238 *
2239 * Configures and enables PLL to generate output clock based on input clock.
2240 */
Mark Brown85488032009-09-05 18:52:16 +01002241int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2242 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002243{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002244 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002245 return dai->ops->set_pll(dai, pll_id, source,
2246 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002247 else
2248 return -EINVAL;
2249}
2250EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2251
2252/**
2253 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2254 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002255 * @fmt: SND_SOC_DAIFMT_ format value.
2256 *
2257 * Configures the DAI hardware format and clocking.
2258 */
2259int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2260{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002261 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002262 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002263 else
2264 return -EINVAL;
2265}
2266EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2267
2268/**
2269 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2270 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002271 * @tx_mask: bitmask representing active TX slots.
2272 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002273 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002274 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002275 *
2276 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2277 * specific.
2278 */
2279int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002280 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002281{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002282 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002283 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2284 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002285 else
2286 return -EINVAL;
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2289
2290/**
Barry Song472df3c2009-09-12 01:16:29 +08002291 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2292 * @dai: DAI
2293 * @tx_num: how many TX channels
2294 * @tx_slot: pointer to an array which imply the TX slot number channel
2295 * 0~num-1 uses
2296 * @rx_num: how many RX channels
2297 * @rx_slot: pointer to an array which imply the RX slot number channel
2298 * 0~num-1 uses
2299 *
2300 * configure the relationship between channel number and TDM slot number.
2301 */
2302int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2303 unsigned int tx_num, unsigned int *tx_slot,
2304 unsigned int rx_num, unsigned int *rx_slot)
2305{
2306 if (dai->ops && dai->ops->set_channel_map)
2307 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2308 rx_num, rx_slot);
2309 else
2310 return -EINVAL;
2311}
2312EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2313
2314/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002315 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2316 * @dai: DAI
2317 * @tristate: tristate enable
2318 *
2319 * Tristates the DAI so that others can use it.
2320 */
2321int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2322{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002323 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002324 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002325 else
2326 return -EINVAL;
2327}
2328EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2329
2330/**
2331 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2332 * @dai: DAI
2333 * @mute: mute enable
2334 *
2335 * Mutes the DAI DAC.
2336 */
2337int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2338{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002339 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002340 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002341 else
2342 return -EINVAL;
2343}
2344EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2345
Mark Brownc5af3a22008-11-28 13:29:45 +00002346/**
2347 * snd_soc_register_card - Register a card with the ASoC core
2348 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002349 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002350 *
2351 * Note that currently this is an internal only function: it will be
2352 * exposed to machine drivers after further backporting of ASoC v2
2353 * registration APIs.
2354 */
2355static int snd_soc_register_card(struct snd_soc_card *card)
2356{
2357 if (!card->name || !card->dev)
2358 return -EINVAL;
2359
2360 INIT_LIST_HEAD(&card->list);
2361 card->instantiated = 0;
2362
2363 mutex_lock(&client_mutex);
2364 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002365 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002366 mutex_unlock(&client_mutex);
2367
2368 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2369
2370 return 0;
2371}
2372
2373/**
2374 * snd_soc_unregister_card - Unregister a card with the ASoC core
2375 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002376 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002377 *
2378 * Note that currently this is an internal only function: it will be
2379 * exposed to machine drivers after further backporting of ASoC v2
2380 * registration APIs.
2381 */
2382static int snd_soc_unregister_card(struct snd_soc_card *card)
2383{
2384 mutex_lock(&client_mutex);
2385 list_del(&card->list);
2386 mutex_unlock(&client_mutex);
2387
2388 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2389
2390 return 0;
2391}
2392
Mark Brown91151712008-11-30 23:31:24 +00002393/**
2394 * snd_soc_register_dai - Register a DAI with the ASoC core
2395 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002396 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002397 */
2398int snd_soc_register_dai(struct snd_soc_dai *dai)
2399{
2400 if (!dai->name)
2401 return -EINVAL;
2402
2403 /* The device should become mandatory over time */
2404 if (!dai->dev)
2405 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2406
Eric Miao6335d052009-03-03 09:41:00 +08002407 if (!dai->ops)
2408 dai->ops = &null_dai_ops;
2409
Mark Brown91151712008-11-30 23:31:24 +00002410 INIT_LIST_HEAD(&dai->list);
2411
2412 mutex_lock(&client_mutex);
2413 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002414 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002415 mutex_unlock(&client_mutex);
2416
2417 pr_debug("Registered DAI '%s'\n", dai->name);
2418
2419 return 0;
2420}
2421EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2422
2423/**
2424 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2425 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002426 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002427 */
2428void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2429{
2430 mutex_lock(&client_mutex);
2431 list_del(&dai->list);
2432 mutex_unlock(&client_mutex);
2433
2434 pr_debug("Unregistered DAI '%s'\n", dai->name);
2435}
2436EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2437
2438/**
2439 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2440 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002441 * @dai: Array of DAIs to register
2442 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002443 */
2444int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2445{
2446 int i, ret;
2447
2448 for (i = 0; i < count; i++) {
2449 ret = snd_soc_register_dai(&dai[i]);
2450 if (ret != 0)
2451 goto err;
2452 }
2453
2454 return 0;
2455
2456err:
2457 for (i--; i >= 0; i--)
2458 snd_soc_unregister_dai(&dai[i]);
2459
2460 return ret;
2461}
2462EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2463
2464/**
2465 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2466 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002467 * @dai: Array of DAIs to unregister
2468 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002469 */
2470void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2471{
2472 int i;
2473
2474 for (i = 0; i < count; i++)
2475 snd_soc_unregister_dai(&dai[i]);
2476}
2477EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2478
Mark Brown12a48a8c2008-12-03 19:40:30 +00002479/**
2480 * snd_soc_register_platform - Register a platform with the ASoC core
2481 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002482 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002483 */
2484int snd_soc_register_platform(struct snd_soc_platform *platform)
2485{
2486 if (!platform->name)
2487 return -EINVAL;
2488
2489 INIT_LIST_HEAD(&platform->list);
2490
2491 mutex_lock(&client_mutex);
2492 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002493 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002494 mutex_unlock(&client_mutex);
2495
2496 pr_debug("Registered platform '%s'\n", platform->name);
2497
2498 return 0;
2499}
2500EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2501
2502/**
2503 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2504 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002505 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002506 */
2507void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2508{
2509 mutex_lock(&client_mutex);
2510 list_del(&platform->list);
2511 mutex_unlock(&client_mutex);
2512
2513 pr_debug("Unregistered platform '%s'\n", platform->name);
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2516
Mark Brown151ab222009-05-09 16:22:58 +01002517static u64 codec_format_map[] = {
2518 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2519 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2520 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2521 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2522 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2523 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2524 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2525 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2526 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2527 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2528 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2529 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2530 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2531 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2532 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2533 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2534};
2535
2536/* Fix up the DAI formats for endianness: codecs don't actually see
2537 * the endianness of the data but we're using the CPU format
2538 * definitions which do need to include endianness so we ensure that
2539 * codec DAIs always have both big and little endian variants set.
2540 */
2541static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2542{
2543 int i;
2544
2545 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2546 if (stream->formats & codec_format_map[i])
2547 stream->formats |= codec_format_map[i];
2548}
2549
Mark Brown0d0cf002008-12-10 14:32:45 +00002550/**
2551 * snd_soc_register_codec - Register a codec with the ASoC core
2552 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002553 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002554 */
2555int snd_soc_register_codec(struct snd_soc_codec *codec)
2556{
Mark Brown151ab222009-05-09 16:22:58 +01002557 int i;
2558
Mark Brown0d0cf002008-12-10 14:32:45 +00002559 if (!codec->name)
2560 return -EINVAL;
2561
2562 /* The device should become mandatory over time */
2563 if (!codec->dev)
2564 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2565
2566 INIT_LIST_HEAD(&codec->list);
2567
Mark Brown151ab222009-05-09 16:22:58 +01002568 for (i = 0; i < codec->num_dai; i++) {
2569 fixup_codec_formats(&codec->dai[i].playback);
2570 fixup_codec_formats(&codec->dai[i].capture);
2571 }
2572
Mark Brown0d0cf002008-12-10 14:32:45 +00002573 mutex_lock(&client_mutex);
2574 list_add(&codec->list, &codec_list);
2575 snd_soc_instantiate_cards();
2576 mutex_unlock(&client_mutex);
2577
2578 pr_debug("Registered codec '%s'\n", codec->name);
2579
2580 return 0;
2581}
2582EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2583
2584/**
2585 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2586 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002587 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002588 */
2589void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2590{
2591 mutex_lock(&client_mutex);
2592 list_del(&codec->list);
2593 mutex_unlock(&client_mutex);
2594
2595 pr_debug("Unregistered codec '%s'\n", codec->name);
2596}
2597EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2598
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002599static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600{
Mark Brown384c89e2008-12-03 17:34:03 +00002601#ifdef CONFIG_DEBUG_FS
2602 debugfs_root = debugfs_create_dir("asoc", NULL);
2603 if (IS_ERR(debugfs_root) || !debugfs_root) {
2604 printk(KERN_WARNING
2605 "ASoC: Failed to create debugfs directory\n");
2606 debugfs_root = NULL;
2607 }
2608#endif
2609
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 return platform_driver_register(&soc_driver);
2611}
2612
Mark Brown7d8c16a2008-11-30 22:11:24 +00002613static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614{
Mark Brown384c89e2008-12-03 17:34:03 +00002615#ifdef CONFIG_DEBUG_FS
2616 debugfs_remove_recursive(debugfs_root);
2617#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002618 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619}
2620
2621module_init(snd_soc_init);
2622module_exit(snd_soc_exit);
2623
2624/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002625MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626MODULE_DESCRIPTION("ALSA SoC Core");
2627MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002628MODULE_ALIAS("platform:soc-audio");