blob: ac0fa228c7b33c9ec8f43f0f7da8a1cdeba7e6f6 [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.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020034#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/soc-dapm.h>
40#include <sound/initval.h>
41
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042#define NAME_SIZE 32
43
Frank Mandarinodb2a4162006-10-06 18:31:09 +020044static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020045static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46
Mark Brown384c89e2008-12-03 17:34:03 +000047#ifdef CONFIG_DEBUG_FS
48static struct dentry *debugfs_root;
49#endif
50
Mark Brownc5af3a22008-11-28 13:29:45 +000051static DEFINE_MUTEX(client_mutex);
52static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000053static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000054static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000055static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
57static int snd_soc_register_card(struct snd_soc_card *card);
58static int snd_soc_unregister_card(struct snd_soc_card *card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000059static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Liam Girdwood965ac422007-01-31 14:14:57 +010070/*
71 * This function forces any delayed work to be queued and run.
72 */
73static int run_delayed_work(struct delayed_work *dwork)
74{
75 int ret;
76
77 /* cancel any work waiting to be queued. */
78 ret = cancel_delayed_work(dwork);
79
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
82 if (ret) {
83 schedule_delayed_work(dwork, 0);
84 flush_scheduled_work();
85 }
86 return ret;
87}
88
Mark Brown2624d5f2009-11-03 21:56:13 +000089/* codec register dump */
90static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
91{
Mark Brown5164d742010-07-14 16:14:33 +010092 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000094 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000095 return 0;
96
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000097 if (codec->driver->reg_cache_step)
98 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000099
100 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000101 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000103 continue;
104
105 count += sprintf(buf + count, "%2x: ", i);
106 if (count >= PAGE_SIZE - 1)
107 break;
108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000109 if (codec->driver->display_register) {
110 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000111 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100112 } else {
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
115 * powered off.
116 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000117 ret = codec->driver->read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100118 if (ret >= 0)
119 count += snprintf(buf + count,
120 PAGE_SIZE - count,
121 "%4x", ret);
122 else
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
125 "<no data: %d>", ret);
126 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000127
128 if (count >= PAGE_SIZE - 1)
129 break;
130
131 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
132 if (count >= PAGE_SIZE - 1)
133 break;
134 }
135
136 /* Truncate count; min() would cause a warning */
137 if (count >= PAGE_SIZE)
138 count = PAGE_SIZE - 1;
139
140 return count;
141}
142static ssize_t codec_reg_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 struct snd_soc_pcm_runtime *rtd =
146 container_of(dev, struct snd_soc_pcm_runtime, dev);
147
148 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000149}
150
151static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
152
Mark Browndbe21402010-02-12 11:37:24 +0000153static ssize_t pmdown_time_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 struct snd_soc_pcm_runtime *rtd =
157 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000160}
161
162static ssize_t pmdown_time_set(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t count)
165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 struct snd_soc_pcm_runtime *rtd =
167 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 strict_strtol(buf, 10, &rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000170
171 return count;
172}
173
174static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
175
Mark Brown2624d5f2009-11-03 21:56:13 +0000176#ifdef CONFIG_DEBUG_FS
177static int codec_reg_open_file(struct inode *inode, struct file *file)
178{
179 file->private_data = inode->i_private;
180 return 0;
181}
182
183static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
185{
186 ssize_t ret;
187 struct snd_soc_codec *codec = file->private_data;
188 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
189 if (!buf)
190 return -ENOMEM;
191 ret = soc_codec_reg_show(codec, buf);
192 if (ret >= 0)
193 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
194 kfree(buf);
195 return ret;
196}
197
198static ssize_t codec_reg_write_file(struct file *file,
199 const char __user *user_buf, size_t count, loff_t *ppos)
200{
201 char buf[32];
202 int buf_size;
203 char *start = buf;
204 unsigned long reg, value;
205 int step = 1;
206 struct snd_soc_codec *codec = file->private_data;
207
208 buf_size = min(count, (sizeof(buf)-1));
209 if (copy_from_user(buf, user_buf, buf_size))
210 return -EFAULT;
211 buf[buf_size] = 0;
212
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000213 if (codec->driver->reg_cache_step)
214 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000215
216 while (*start == ' ')
217 start++;
218 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000220 return -EINVAL;
221 while (*start == ' ')
222 start++;
223 if (strict_strtoul(start, 16, &value))
224 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000225 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 return buf_size;
227}
228
229static const struct file_operations codec_reg_fops = {
230 .open = codec_reg_open_file,
231 .read = codec_reg_read_file,
232 .write = codec_reg_write_file,
233};
234
235static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
236{
237 char codec_root[128];
238
239 if (codec->dev)
240 snprintf(codec_root, sizeof(codec_root),
241 "%s.%s", codec->name, dev_name(codec->dev));
242 else
243 snprintf(codec_root, sizeof(codec_root),
244 "%s", codec->name);
245
246 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
247 debugfs_root);
248 if (!codec->debugfs_codec_root) {
249 printk(KERN_WARNING
250 "ASoC: Failed to create codec debugfs directory\n");
251 return;
252 }
253
254 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
255 codec->debugfs_codec_root,
256 codec, &codec_reg_fops);
257 if (!codec->debugfs_reg)
258 printk(KERN_WARNING
259 "ASoC: Failed to create codec register debugfs file\n");
260
261 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
262 codec->debugfs_codec_root,
263 &codec->pop_time);
264 if (!codec->debugfs_pop_time)
265 printk(KERN_WARNING
266 "Failed to create pop time debugfs file\n");
267
268 codec->debugfs_dapm = debugfs_create_dir("dapm",
269 codec->debugfs_codec_root);
270 if (!codec->debugfs_dapm)
271 printk(KERN_WARNING
272 "Failed to create DAPM debugfs directory\n");
273
274 snd_soc_dapm_debugfs_init(codec);
275}
276
277static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
278{
279 debugfs_remove_recursive(codec->debugfs_codec_root);
280}
281
282#else
283
284static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
285{
286}
287
288static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
289{
290}
291#endif
292
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293#ifdef CONFIG_SND_SOC_AC97_BUS
294/* unregister ac97 codec */
295static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
296{
297 if (codec->ac97->dev.bus)
298 device_unregister(&codec->ac97->dev);
299 return 0;
300}
301
302/* stop no dev release warning */
303static void soc_ac97_device_release(struct device *dev){}
304
305/* register ac97 codec to bus */
306static int soc_ac97_dev_register(struct snd_soc_codec *codec)
307{
308 int err;
309
310 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100311 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200312 codec->ac97->dev.release = soc_ac97_device_release;
313
Kay Sieversbb072bf2008-11-02 03:50:35 +0100314 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000315 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200316 err = device_register(&codec->ac97->dev);
317 if (err < 0) {
318 snd_printk(KERN_ERR "Can't register ac97 bus\n");
319 codec->ac97->dev.bus = NULL;
320 return err;
321 }
322 return 0;
323}
324#endif
325
Mark Brown06f409d2009-04-07 18:10:13 +0100326static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
327{
328 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000329 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
330 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100331 int ret;
332
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
334 rtd->dai_link->symmetric_rates) {
335 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
336 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100337
338 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
339 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000340 rtd->rate,
341 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100342 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000343 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100344 "Unable to apply rate symmetry constraint: %d\n", ret);
345 return ret;
346 }
347 }
348
349 return 0;
350}
351
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200352/*
353 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
354 * then initialized and any private data can be allocated. This also calls
355 * startup for the cpu DAI, platform, machine and codec DAI.
356 */
357static int soc_pcm_open(struct snd_pcm_substream *substream)
358{
359 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000361 struct snd_soc_platform *platform = rtd->platform;
362 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
363 struct snd_soc_dai *codec_dai = rtd->codec_dai;
364 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
365 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366 int ret = 0;
367
368 mutex_lock(&pcm_mutex);
369
370 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000371 if (cpu_dai->driver->ops->startup) {
372 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200373 if (ret < 0) {
374 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100375 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200376 goto out;
377 }
378 }
379
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000380 if (platform->driver->ops->open) {
381 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200382 if (ret < 0) {
383 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
384 goto platform_err;
385 }
386 }
387
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 if (codec_dai->driver->ops->startup) {
389 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100390 if (ret < 0) {
391 printk(KERN_ERR "asoc: can't open codec %s\n",
392 codec_dai->name);
393 goto codec_dai_err;
394 }
395 }
396
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000397 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
398 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200399 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000400 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200401 goto machine_err;
402 }
403 }
404
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200405 /* Check that the codec and cpu DAI's are compatible */
406 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
407 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408 max(codec_dai_drv->playback.rate_min,
409 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200410 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000411 min(codec_dai_drv->playback.rate_max,
412 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200413 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000414 max(codec_dai_drv->playback.channels_min,
415 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200416 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000417 min(codec_dai_drv->playback.channels_max,
418 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100419 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100421 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000422 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
423 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900424 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 runtime->hw.rates |= cpu_dai_drv->playback.rates;
426 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900427 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 } else {
430 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431 max(codec_dai_drv->capture.rate_min,
432 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 min(codec_dai_drv->capture.rate_max,
435 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200436 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000437 max(codec_dai_drv->capture.channels_min,
438 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 min(codec_dai_drv->capture.channels_max,
441 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100442 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000443 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100444 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000445 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
446 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900447 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448 runtime->hw.rates |= cpu_dai_drv->capture.rates;
449 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900450 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200452 }
453
454 snd_pcm_limit_hw_rates(runtime);
455 if (!runtime->hw.rates) {
456 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100457 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900458 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200459 }
460 if (!runtime->hw.formats) {
461 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100462 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900463 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200464 }
465 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
466 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000467 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900468 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 }
470
Mark Brown06f409d2009-04-07 18:10:13 +0100471 /* Symmetry only applies if we've already got an active stream. */
472 if (cpu_dai->active || codec_dai->active) {
473 ret = soc_pcm_apply_symmetry(substream);
474 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900475 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100476 }
477
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 pr_debug("asoc: %s <-> %s info:\n",
479 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100480 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
481 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
482 runtime->hw.channels_max);
483 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
484 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485
Jassi Brar14dc5732010-02-26 09:12:32 +0900486 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 cpu_dai->playback_active++;
488 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900489 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 cpu_dai->capture_active++;
491 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900492 }
493 cpu_dai->active++;
494 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 mutex_unlock(&pcm_mutex);
497 return 0;
498
Jassi Brarbb1c0472010-02-25 11:24:53 +0900499config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
501 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502
Jassi Brarbb1c0472010-02-25 11:24:53 +0900503machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 if (codec_dai->driver->ops->shutdown)
505 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900506
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100507codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 if (platform->driver->ops->close)
509 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510
511platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 if (cpu_dai->driver->ops->shutdown)
513 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514out:
515 mutex_unlock(&pcm_mutex);
516 return ret;
517}
518
519/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200520 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 * This is to ensure there are no pops or clicks in between any music tracks
522 * due to DAPM power cycling.
523 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100524static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200525{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 struct snd_soc_pcm_runtime *rtd =
527 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
528 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529
530 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
533 codec_dai->driver->playback.stream_name,
534 codec_dai->playback_active ? "active" : "inactive",
535 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 /* are we waiting on this codec DAI stream */
538 if (codec_dai->pop_wait == 1) {
539 codec_dai->pop_wait = 0;
540 snd_soc_dapm_stream_event(rtd,
541 codec_dai->driver->playback.stream_name,
542 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545 mutex_unlock(&pcm_mutex);
546}
547
548/*
549 * Called by ALSA when a PCM substream is closed. Private data can be
550 * freed here. The cpu DAI, codec DAI, machine and platform are also
551 * shutdown.
552 */
553static int soc_codec_close(struct snd_pcm_substream *substream)
554{
555 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 struct snd_soc_platform *platform = rtd->platform;
557 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
558 struct snd_soc_dai *codec_dai = rtd->codec_dai;
559 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560
561 mutex_lock(&pcm_mutex);
562
Jassi Brar14dc5732010-02-26 09:12:32 +0900563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 cpu_dai->playback_active--;
565 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900566 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 cpu_dai->capture_active--;
568 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900570
571 cpu_dai->active--;
572 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573 codec->active--;
574
Mark Brown6010b2d2008-09-06 18:33:24 +0100575 /* Muting the DAC suppresses artifacts caused during digital
576 * shutdown, for example from stopping clocks.
577 */
578 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
579 snd_soc_dai_digital_mute(codec_dai, 1);
580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (cpu_dai->driver->ops->shutdown)
582 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (codec_dai->driver->ops->shutdown)
585 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
588 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200589
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 if (platform->driver->ops->close)
591 platform->driver->ops->close(substream);
592 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593
594 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
595 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100596 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 schedule_delayed_work(&rtd->delayed_work,
598 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599 } else {
600 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 snd_soc_dapm_stream_event(rtd,
602 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100603 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604 }
605
606 mutex_unlock(&pcm_mutex);
607 return 0;
608}
609
610/*
611 * Called by ALSA when the PCM substream is prepared, can set format, sample
612 * rate, etc. This function is non atomic and can be called multiple times,
613 * it can refer to the runtime info.
614 */
615static int soc_pcm_prepare(struct snd_pcm_substream *substream)
616{
617 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 struct snd_soc_platform *platform = rtd->platform;
619 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
620 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200621 int ret = 0;
622
623 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
626 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100627 if (ret < 0) {
628 printk(KERN_ERR "asoc: machine prepare error\n");
629 goto out;
630 }
631 }
632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 if (platform->driver->ops->prepare) {
634 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200635 if (ret < 0) {
636 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200638 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 }
640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 if (codec_dai->driver->ops->prepare) {
642 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200643 if (ret < 0) {
644 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200646 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 }
648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 if (cpu_dai->driver->ops->prepare) {
650 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100651 if (ret < 0) {
652 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
653 goto out;
654 }
655 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656
Mark Brownd45f6212008-10-14 13:58:36 +0100657 /* cancel any delayed stream shutdown that is pending */
658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
659 codec_dai->pop_wait) {
660 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100662 }
663
Mark Brown452c5ea2009-05-17 21:41:23 +0100664 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 snd_soc_dapm_stream_event(rtd,
666 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100667 SND_SOC_DAPM_STREAM_START);
668 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 snd_soc_dapm_stream_event(rtd,
670 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100671 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100672
Mark Brown452c5ea2009-05-17 21:41:23 +0100673 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674
675out:
676 mutex_unlock(&pcm_mutex);
677 return ret;
678}
679
680/*
681 * Called by ALSA when the hardware params are set by application. This
682 * function can also be called multiple times and can allocate buffers
683 * (using snd_pcm_lib_* ). It's non-atomic.
684 */
685static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
686 struct snd_pcm_hw_params *params)
687{
688 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 struct snd_soc_platform *platform = rtd->platform;
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
691 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692 int ret = 0;
693
694 mutex_lock(&pcm_mutex);
695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
697 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100699 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 goto out;
701 }
702 }
703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 if (codec_dai->driver->ops->hw_params) {
705 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100706 if (ret < 0) {
707 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
708 codec_dai->name);
709 goto codec_err;
710 }
711 }
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (cpu_dai->driver->ops->hw_params) {
714 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200716 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100717 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 goto interface_err;
719 }
720 }
721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 if (platform->driver->ops->hw_params) {
723 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 platform->name);
727 goto platform_err;
728 }
729 }
730
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100732
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733out:
734 mutex_unlock(&pcm_mutex);
735 return ret;
736
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200737platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 if (cpu_dai->driver->ops->hw_free)
739 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740
741interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 if (codec_dai->driver->ops->hw_free)
743 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100744
745codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
747 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748
749 mutex_unlock(&pcm_mutex);
750 return ret;
751}
752
753/*
754 * Free's resources allocated by hw_params, can be called multiple times
755 */
756static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
757{
758 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 struct snd_soc_platform *platform = rtd->platform;
760 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
761 struct snd_soc_dai *codec_dai = rtd->codec_dai;
762 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200763
764 mutex_lock(&pcm_mutex);
765
766 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100767 if (!codec->active)
768 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
770 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
772 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773
774 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 if (platform->driver->ops->hw_free)
776 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200777
778 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 if (codec_dai->driver->ops->hw_free)
780 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 if (cpu_dai->driver->ops->hw_free)
783 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784
785 mutex_unlock(&pcm_mutex);
786 return 0;
787}
788
789static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
790{
791 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 struct snd_soc_platform *platform = rtd->platform;
793 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
794 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795 int ret;
796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 if (codec_dai->driver->ops->trigger) {
798 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799 if (ret < 0)
800 return ret;
801 }
802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 if (platform->driver->ops->trigger) {
804 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 if (ret < 0)
806 return ret;
807 }
808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 if (cpu_dai->driver->ops->trigger) {
810 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 if (ret < 0)
812 return ret;
813 }
814 return 0;
815}
816
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200817/*
818 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200819 * If cpu_dai, codec_dai, platform driver has the delay callback, than
820 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200821 */
822static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
823{
824 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 struct snd_soc_platform *platform = rtd->platform;
826 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
827 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200828 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200829 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200830 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200831
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000832 if (platform->driver->ops->pointer)
833 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200834
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 if (cpu_dai->driver->ops->delay)
836 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200837
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000838 if (codec_dai->driver->ops->delay)
839 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 if (platform->driver->delay)
842 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200843
844 runtime->delay = delay;
845
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200846 return offset;
847}
848
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849/* ASoC PCM operations */
850static struct snd_pcm_ops soc_pcm_ops = {
851 .open = soc_pcm_open,
852 .close = soc_codec_close,
853 .hw_params = soc_pcm_hw_params,
854 .hw_free = soc_pcm_hw_free,
855 .prepare = soc_pcm_prepare,
856 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200857 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858};
859
860#ifdef CONFIG_PM
861/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100862static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863{
Mark Brown416356f2009-06-30 19:05:15 +0100864 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866 int i;
867
Daniel Macke3509ff2009-06-03 17:44:49 +0200868 /* If the initialization of this soc device failed, there is no codec
869 * associated with it. Just bail out in this case.
870 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000871 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200872 return 0;
873
Andy Green6ed25972008-06-13 16:24:05 +0100874 /* Due to the resume being scheduled into a workqueue we could
875 * suspend before that's finished - wait for it to complete.
876 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877 snd_power_lock(card->snd_card);
878 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
879 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100880
881 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100883
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000885 for (i = 0; i < card->num_rtd; i++) {
886 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
887 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100888
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100890 continue;
891
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 if (drv->ops->digital_mute && dai->playback_active)
893 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894 }
895
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100896 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 for (i = 0; i < card->num_rtd; i++) {
898 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100899 continue;
900
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100902 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100903
Mark Brown87506542008-11-18 20:50:34 +0000904 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100905 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 for (i = 0; i < card->num_rtd; i++) {
908 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
909 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100910
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100912 continue;
913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
915 cpu_dai->driver->suspend(cpu_dai);
916 if (platform->driver->suspend && !platform->suspended) {
917 platform->driver->suspend(cpu_dai);
918 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100919 }
920 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200921
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 /* close any waiting streams and save state */
923 for (i = 0; i < card->num_rtd; i++) {
924 run_delayed_work(&card->rtd[i].delayed_work);
925 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
926 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100927
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000928 for (i = 0; i < card->num_rtd; i++) {
929 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
930
931 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100932 continue;
933
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 if (driver->playback.stream_name != NULL)
935 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
936 SND_SOC_DAPM_STREAM_SUSPEND);
937
938 if (driver->capture.stream_name != NULL)
939 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
940 SND_SOC_DAPM_STREAM_SUSPEND);
941 }
942
943 /* suspend all CODECs */
944 for (i = 0; i < card->num_rtd; i++) {
945 struct snd_soc_codec *codec = card->rtd[i].codec;
946 /* If there are paths active then the CODEC will be held with
947 * bias _ON and should not be suspended. */
948 if (!codec->suspended && codec->driver->suspend) {
949 switch (codec->bias_level) {
950 case SND_SOC_BIAS_STANDBY:
951 case SND_SOC_BIAS_OFF:
952 codec->driver->suspend(codec, PMSG_SUSPEND);
953 codec->suspended = 1;
954 break;
955 default:
956 dev_dbg(codec->dev, "CODEC is on over suspend\n");
957 break;
958 }
959 }
960 }
961
962 for (i = 0; i < card->num_rtd; i++) {
963 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
964
965 if (card->rtd[i].dai_link->ignore_suspend)
966 continue;
967
968 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
969 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200970 }
971
Mark Brown87506542008-11-18 20:50:34 +0000972 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100973 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200974
975 return 0;
976}
977
Andy Green6ed25972008-06-13 16:24:05 +0100978/* deferred resume work, so resume can complete before we finished
979 * setting our codec back up, which can be very slow on I2C
980 */
981static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200982{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 struct snd_soc_card *card =
984 container_of(work, struct snd_soc_card, deferred_resume_work);
985 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986 int i;
987
Andy Green6ed25972008-06-13 16:24:05 +0100988 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
989 * so userspace apps are blocked from touching us
990 */
991
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100993
Mark Brown99497882010-05-07 20:24:05 +0100994 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100996
Mark Brown87506542008-11-18 20:50:34 +0000997 if (card->resume_pre)
998 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200999
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000 /* resume AC97 DAIs */
1001 for (i = 0; i < card->num_rtd; i++) {
1002 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001005 continue;
1006
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001007 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1008 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001009 }
1010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 for (i = 0; i < card->num_rtd; i++) {
1012 struct snd_soc_codec *codec = card->rtd[i].codec;
1013 /* If the CODEC was idle over suspend then it will have been
1014 * left with bias OFF or STANDBY and suspended so we must now
1015 * resume. Otherwise the suspend was suppressed.
1016 */
1017 if (codec->driver->resume && codec->suspended) {
1018 switch (codec->bias_level) {
1019 case SND_SOC_BIAS_STANDBY:
1020 case SND_SOC_BIAS_OFF:
1021 codec->driver->resume(codec);
1022 codec->suspended = 0;
1023 break;
1024 default:
1025 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1026 break;
1027 }
Mark Brown1547aba2010-05-07 21:11:40 +01001028 }
1029 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001031 for (i = 0; i < card->num_rtd; i++) {
1032 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001033
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001035 continue;
1036
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001037 if (driver->playback.stream_name != NULL)
1038 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001039 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001040
1041 if (driver->capture.stream_name != NULL)
1042 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001043 SND_SOC_DAPM_STREAM_RESUME);
1044 }
1045
Mark Brown3ff3f642008-05-19 12:32:25 +02001046 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047 for (i = 0; i < card->num_rtd; i++) {
1048 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1049 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001050
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001052 continue;
1053
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001054 if (drv->ops->digital_mute && dai->playback_active)
1055 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001056 }
1057
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058 for (i = 0; i < card->num_rtd; i++) {
1059 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1060 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001063 continue;
1064
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1066 cpu_dai->driver->resume(cpu_dai);
1067 if (platform->driver->resume && platform->suspended) {
1068 platform->driver->resume(cpu_dai);
1069 platform->suspended = 0;
1070 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001071 }
1072
Mark Brown87506542008-11-18 20:50:34 +00001073 if (card->resume_post)
1074 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001077
1078 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001079 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001080}
1081
1082/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001083static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001084{
Mark Brown416356f2009-06-30 19:05:15 +01001085 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001086 struct snd_soc_card *card = platform_get_drvdata(pdev);
1087 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001088
Mark Brown64ab9ba2009-03-31 11:27:03 +01001089 /* AC97 devices might have other drivers hanging off them so
1090 * need to resume immediately. Other drivers don't have that
1091 * problem and may take a substantial amount of time to resume
1092 * due to I/O costs and anti-pop so handle them out of line.
1093 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001094 for (i = 0; i < card->num_rtd; i++) {
1095 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1096 if (cpu_dai->driver->ac97_control) {
1097 dev_dbg(dev, "Resuming AC97 immediately\n");
1098 soc_resume_deferred(&card->deferred_resume_work);
1099 } else {
1100 dev_dbg(dev, "Scheduling resume work\n");
1101 if (!schedule_work(&card->deferred_resume_work))
1102 dev_err(dev, "resume work item may be lost\n");
1103 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001104 }
Andy Green6ed25972008-06-13 16:24:05 +01001105
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001106 return 0;
1107}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001108#else
1109#define soc_suspend NULL
1110#define soc_resume NULL
1111#endif
1112
Barry Song02a06d32009-10-16 18:13:38 +08001113static struct snd_soc_dai_ops null_dai_ops = {
1114};
1115
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001117{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001118 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1119 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001120 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001121 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124 if (rtd->complete)
1125 return 1;
1126 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 /* do we already have the CPU DAI for this link ? */
1129 if (rtd->cpu_dai) {
1130 goto find_codec;
1131 }
1132 /* no, then find CPU DAI from registered DAIs*/
1133 list_for_each_entry(cpu_dai, &dai_list, list) {
1134 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1135
1136 if (!try_module_get(cpu_dai->dev->driver->owner))
1137 return -ENODEV;
1138
1139 rtd->cpu_dai = cpu_dai;
1140 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001141 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 }
1143 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1144 dai_link->cpu_dai_name);
1145
1146find_codec:
1147 /* do we already have the CODEC for this link ? */
1148 if (rtd->codec) {
1149 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001150 }
1151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 /* no, then find CODEC from registered CODECs*/
1153 list_for_each_entry(codec, &codec_list, list) {
1154 if (!strcmp(codec->name, dai_link->codec_name)) {
1155 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001157 if (!try_module_get(codec->dev->driver->owner))
1158 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1161 list_for_each_entry(codec_dai, &dai_list, list) {
1162 if (codec->dev == codec_dai->dev &&
1163 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1164 rtd->codec_dai = codec_dai;
1165 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001166 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 }
1168 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1169 dai_link->codec_dai_name);
1170
1171 goto find_platform;
1172 }
1173 }
1174 dev_dbg(card->dev, "CODEC %s not registered\n",
1175 dai_link->codec_name);
1176
1177find_platform:
1178 /* do we already have the CODEC DAI for this link ? */
1179 if (rtd->platform) {
1180 goto out;
1181 }
1182 /* no, then find CPU DAI from registered DAIs*/
1183 list_for_each_entry(platform, &platform_list, list) {
1184 if (!strcmp(platform->name, dai_link->platform_name)) {
1185
1186 if (!try_module_get(platform->dev->driver->owner))
1187 return -ENODEV;
1188
1189 rtd->platform = platform;
1190 goto out;
1191 }
1192 }
1193
1194 dev_dbg(card->dev, "platform %s not registered\n",
1195 dai_link->platform_name);
1196 return 0;
1197
1198out:
1199 /* mark rtd as complete if we found all 4 of our client devices */
1200 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1201 rtd->complete = 1;
1202 card->num_rtd++;
1203 }
1204 return 1;
1205}
1206
1207static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1208{
1209 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1210 struct snd_soc_codec *codec = rtd->codec;
1211 struct snd_soc_platform *platform = rtd->platform;
1212 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1213 int err;
1214
1215 /* unregister the rtd device */
1216 if (rtd->dev_registered) {
1217 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1218 device_unregister(&rtd->dev);
1219 rtd->dev_registered = 0;
1220 }
1221
1222 /* remove the CODEC DAI */
1223 if (codec_dai && codec_dai->probed) {
1224 if (codec_dai->driver->remove) {
1225 err = codec_dai->driver->remove(codec_dai);
1226 if (err < 0)
1227 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1228 }
1229 codec_dai->probed = 0;
1230 list_del(&codec_dai->card_list);
1231 }
1232
1233 /* remove the platform */
1234 if (platform && platform->probed) {
1235 if (platform->driver->remove) {
1236 err = platform->driver->remove(platform);
1237 if (err < 0)
1238 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1239 }
1240 platform->probed = 0;
1241 list_del(&platform->card_list);
1242 module_put(platform->dev->driver->owner);
1243 }
1244
1245 /* remove the CODEC */
1246 if (codec && codec->probed) {
1247 if (codec->driver->remove) {
1248 err = codec->driver->remove(codec);
1249 if (err < 0)
1250 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1251 }
1252
1253 /* Make sure all DAPM widgets are freed */
1254 snd_soc_dapm_free(codec);
1255
1256 soc_cleanup_codec_debugfs(codec);
1257 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1258 codec->probed = 0;
1259 list_del(&codec->card_list);
1260 module_put(codec->dev->driver->owner);
1261 }
1262
1263 /* remove the cpu_dai */
1264 if (cpu_dai && cpu_dai->probed) {
1265 if (cpu_dai->driver->remove) {
1266 err = cpu_dai->driver->remove(cpu_dai);
1267 if (err < 0)
1268 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1269 }
1270 cpu_dai->probed = 0;
1271 list_del(&cpu_dai->card_list);
1272 module_put(cpu_dai->dev->driver->owner);
1273 }
1274}
1275
1276static void rtd_release(struct device *dev) {}
1277
1278static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1279{
1280 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1281 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1282 struct snd_soc_codec *codec = rtd->codec;
1283 struct snd_soc_platform *platform = rtd->platform;
1284 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1285 int ret;
1286
1287 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1288
1289 /* config components */
1290 codec_dai->codec = codec;
1291 codec->card = card;
1292 cpu_dai->platform = platform;
1293 rtd->card = card;
1294 rtd->dev.parent = card->dev;
1295 codec_dai->card = card;
1296 cpu_dai->card = card;
1297
1298 /* set default power off timeout */
1299 rtd->pmdown_time = pmdown_time;
1300
1301 /* probe the cpu_dai */
1302 if (!cpu_dai->probed) {
1303 if (cpu_dai->driver->probe) {
1304 ret = cpu_dai->driver->probe(cpu_dai);
1305 if (ret < 0) {
1306 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1307 cpu_dai->name);
1308 return ret;
1309 }
1310 }
1311 cpu_dai->probed = 1;
1312 /* mark cpu_dai as probed and add to card cpu_dai list */
1313 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1314 }
1315
1316 /* probe the CODEC */
1317 if (!codec->probed) {
1318 if (codec->driver->probe) {
1319 ret = codec->driver->probe(codec);
1320 if (ret < 0) {
1321 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1322 codec->name);
1323 return ret;
1324 }
1325 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001326
1327 soc_init_codec_debugfs(codec);
1328
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001329 /* mark codec as probed and add to card codec list */
1330 codec->probed = 1;
1331 list_add(&codec->card_list, &card->codec_dev_list);
1332 }
1333
1334 /* probe the platform */
1335 if (!platform->probed) {
1336 if (platform->driver->probe) {
1337 ret = platform->driver->probe(platform);
1338 if (ret < 0) {
1339 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1340 platform->name);
1341 return ret;
1342 }
1343 }
1344 /* mark platform as probed and add to card platform list */
1345 platform->probed = 1;
1346 list_add(&platform->card_list, &card->platform_dev_list);
1347 }
1348
1349 /* probe the CODEC DAI */
1350 if (!codec_dai->probed) {
1351 if (codec_dai->driver->probe) {
1352 ret = codec_dai->driver->probe(codec_dai);
1353 if (ret < 0) {
1354 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1355 codec_dai->name);
1356 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001357 }
1358 }
1359
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001360 /* mark cpu_dai as probed and add to card cpu_dai list */
1361 codec_dai->probed = 1;
1362 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001363 }
1364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365 /* DAPM dai link stream work */
1366 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1367
1368 /* now that all clients have probed, initialise the DAI link */
1369 if (dai_link->init) {
1370 ret = dai_link->init(rtd);
1371 if (ret < 0) {
1372 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1373 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001374 }
1375 }
1376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377 /* Make sure all DAPM widgets are instantiated */
1378 snd_soc_dapm_new_widgets(codec);
1379 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001380
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381 /* register the rtd device */
1382 rtd->dev.init_name = rtd->dai_link->stream_name;
1383 rtd->dev.release = rtd_release;
1384 rtd->dev.init_name = dai_link->name;
1385 ret = device_register(&rtd->dev);
1386 if (ret < 0) {
1387 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1388 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001389 }
1390
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001391 rtd->dev_registered = 1;
1392 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1393 if (ret < 0)
1394 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1395
1396 /* add DAPM sysfs entries for this codec */
1397 ret = snd_soc_dapm_sys_add(&rtd->dev);
1398 if (ret < 0)
1399 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1400
1401 /* add codec sysfs entries */
1402 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1403 if (ret < 0)
1404 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1405
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 /* create the pcm */
1407 ret = soc_new_pcm(rtd, num);
1408 if (ret < 0) {
1409 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1410 return ret;
1411 }
1412
1413 /* add platform data for AC97 devices */
1414 if (rtd->codec_dai->driver->ac97_control)
1415 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1416
1417 return 0;
1418}
1419
1420#ifdef CONFIG_SND_SOC_AC97_BUS
1421static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1422{
1423 int ret;
1424
1425 /* Only instantiate AC97 if not already done by the adaptor
1426 * for the generic AC97 subsystem.
1427 */
1428 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1429
1430 ret = soc_ac97_dev_register(rtd->codec);
1431 if (ret < 0) {
1432 printk(KERN_ERR "asoc: AC97 device register failed\n");
1433 return ret;
1434 }
1435
1436 rtd->codec->ac97_registered = 1;
1437 }
1438 return 0;
1439}
1440
1441static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1442{
1443 if (codec->ac97_registered) {
1444 soc_ac97_dev_unregister(codec);
1445 codec->ac97_registered = 0;
1446 }
1447}
1448#endif
1449
1450static void snd_soc_instantiate_card(struct snd_soc_card *card)
1451{
1452 struct platform_device *pdev = to_platform_device(card->dev);
1453 int ret, i;
1454
1455 mutex_lock(&card->mutex);
1456
1457 if (card->instantiated) {
1458 mutex_unlock(&card->mutex);
1459 return;
1460 }
1461
1462 /* bind DAIs */
1463 for (i = 0; i < card->num_links; i++)
1464 soc_bind_dai_link(card, i);
1465
1466 /* bind completed ? */
1467 if (card->num_rtd != card->num_links) {
1468 mutex_unlock(&card->mutex);
1469 return;
1470 }
1471
1472 /* card bind complete so register a sound card */
1473 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1474 card->owner, 0, &card->snd_card);
1475 if (ret < 0) {
1476 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1477 card->name);
1478 mutex_unlock(&card->mutex);
1479 return;
1480 }
1481 card->snd_card->dev = card->dev;
1482
Randy Dunlap1301a962008-06-17 19:19:34 +01001483#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001484 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001485 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001486#endif
Andy Green6ed25972008-06-13 16:24:05 +01001487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488 /* initialise the sound card only once */
1489 if (card->probe) {
1490 ret = card->probe(pdev);
1491 if (ret < 0)
1492 goto card_probe_error;
1493 }
1494
Mark Brownfe3e78e2009-11-03 22:13:13 +00001495 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 ret = soc_probe_dai_link(card, i);
1497 if (ret < 0) {
1498 printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name);
1499 goto probe_dai_err;
1500 }
1501 }
1502
1503 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1504 "%s", card->name);
1505 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1506 "%s", card->name);
1507
1508 ret = snd_card_register(card->snd_card);
1509 if (ret < 0) {
1510 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1511 goto probe_dai_err;
1512 }
1513
1514#ifdef CONFIG_SND_SOC_AC97_BUS
1515 /* register any AC97 codecs */
1516 for (i = 0; i < card->num_rtd; i++) {
1517 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001518 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1520 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001521 }
1522 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001523#endif
1524
Mark Brown435c5e22008-12-04 15:32:53 +00001525 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001527 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529probe_dai_err:
1530 for (i = 0; i < card->num_links; i++)
1531 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001532
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001533card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001534 if (card->remove)
1535 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001536
1537 snd_card_free(card->snd_card);
1538
1539 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001540}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001541
Mark Brown435c5e22008-12-04 15:32:53 +00001542/*
1543 * Attempt to initialise any uninitalised cards. Must be called with
1544 * client_mutex.
1545 */
1546static void snd_soc_instantiate_cards(void)
1547{
1548 struct snd_soc_card *card;
1549 list_for_each_entry(card, &card_list, list)
1550 snd_soc_instantiate_card(card);
1551}
1552
1553/* probes a new socdev */
1554static int soc_probe(struct platform_device *pdev)
1555{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001556 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001557 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001558
1559 /* Bodge while we unpick instantiation */
1560 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001561 INIT_LIST_HEAD(&card->dai_dev_list);
1562 INIT_LIST_HEAD(&card->codec_dev_list);
1563 INIT_LIST_HEAD(&card->platform_dev_list);
1564
Mark Brown435c5e22008-12-04 15:32:53 +00001565 ret = snd_soc_register_card(card);
1566 if (ret != 0) {
1567 dev_err(&pdev->dev, "Failed to register card\n");
1568 return ret;
1569 }
1570
1571 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001572}
1573
1574/* removes a socdev */
1575static int soc_remove(struct platform_device *pdev)
1576{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582 /* make sure any delayed work runs */
1583 for (i = 0; i < card->num_rtd; i++) {
1584 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1585 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001586 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001587
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 /* remove and free each DAI */
1589 for (i = 0; i < card->num_rtd; i++)
1590 soc_remove_dai_link(card, i);
1591
1592 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001593 if (card->remove)
1594 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595
1596 kfree(card->rtd);
1597 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001598 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001599 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001600 return 0;
1601}
1602
Mark Brown416356f2009-06-30 19:05:15 +01001603static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001604{
Mark Brown416356f2009-06-30 19:05:15 +01001605 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 struct snd_soc_card *card = platform_get_drvdata(pdev);
1607 int i;
Mark Brown51737472009-06-22 13:16:51 +01001608
1609 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001610 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001611
1612 /* Flush out pmdown_time work - we actually do want to run it
1613 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 for (i = 0; i < card->num_rtd; i++) {
1615 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1616 run_delayed_work(&rtd->delayed_work);
1617 }
Mark Brown51737472009-06-22 13:16:51 +01001618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001620
1621 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001622}
1623
Alexey Dobriyan47145212009-12-14 18:00:08 -08001624static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001625 .suspend = soc_suspend,
1626 .resume = soc_resume,
1627 .poweroff = soc_poweroff,
1628};
1629
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630/* ASoC platform driver */
1631static struct platform_driver soc_driver = {
1632 .driver = {
1633 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001634 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001635 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001636 },
1637 .probe = soc_probe,
1638 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001639};
1640
1641/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001643{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 struct snd_soc_codec *codec = rtd->codec;
1645 struct snd_soc_platform *platform = rtd->platform;
1646 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1647 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648 struct snd_pcm *pcm;
1649 char new_name[64];
1650 int ret = 0, playback = 0, capture = 0;
1651
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001652 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001653 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001657 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001658 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001659 capture = 1;
1660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1662 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1663 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001664 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001666 return ret;
1667 }
1668
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1672 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1673 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1674 soc_pcm_ops.copy = platform->driver->ops->copy;
1675 soc_pcm_ops.silence = platform->driver->ops->silence;
1676 soc_pcm_ops.ack = platform->driver->ops->ack;
1677 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001678
1679 if (playback)
1680 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1681
1682 if (capture)
1683 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686 if (ret < 0) {
1687 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001688 return ret;
1689 }
1690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001691 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001692 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1693 cpu_dai->name);
1694 return ret;
1695}
1696
Mark Brown096e49d2009-07-05 15:12:22 +01001697/**
1698 * snd_soc_codec_volatile_register: Report if a register is volatile.
1699 *
1700 * @codec: CODEC to query.
1701 * @reg: Register to query.
1702 *
1703 * Boolean function indiciating if a CODEC register is volatile.
1704 */
1705int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1706{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 if (codec->driver->volatile_register)
1708 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001709 else
1710 return 0;
1711}
1712EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1713
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001714/**
1715 * snd_soc_new_ac97_codec - initailise AC97 device
1716 * @codec: audio codec
1717 * @ops: AC97 bus operations
1718 * @num: AC97 codec number
1719 *
1720 * Initialises AC97 codec resources for use by ad-hoc devices only.
1721 */
1722int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1723 struct snd_ac97_bus_ops *ops, int num)
1724{
1725 mutex_lock(&codec->mutex);
1726
1727 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1728 if (codec->ac97 == NULL) {
1729 mutex_unlock(&codec->mutex);
1730 return -ENOMEM;
1731 }
1732
1733 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1734 if (codec->ac97->bus == NULL) {
1735 kfree(codec->ac97);
1736 codec->ac97 = NULL;
1737 mutex_unlock(&codec->mutex);
1738 return -ENOMEM;
1739 }
1740
1741 codec->ac97->bus->ops = ops;
1742 codec->ac97->num = num;
1743 mutex_unlock(&codec->mutex);
1744 return 0;
1745}
1746EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1747
1748/**
1749 * snd_soc_free_ac97_codec - free AC97 codec device
1750 * @codec: audio codec
1751 *
1752 * Frees AC97 codec device resources.
1753 */
1754void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1755{
1756 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757#ifdef CONFIG_SND_SOC_AC97_BUS
1758 soc_unregister_ac97_dai_link(codec);
1759#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760 kfree(codec->ac97->bus);
1761 kfree(codec->ac97);
1762 codec->ac97 = NULL;
1763 mutex_unlock(&codec->mutex);
1764}
1765EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1766
1767/**
1768 * snd_soc_update_bits - update codec register bits
1769 * @codec: audio codec
1770 * @reg: codec register
1771 * @mask: register mask
1772 * @value: new value
1773 *
1774 * Writes new register value.
1775 *
1776 * Returns 1 for change else 0.
1777 */
1778int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001779 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001780{
1781 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001782 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001783
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001784 old = snd_soc_read(codec, reg);
1785 new = (old & ~mask) | value;
1786 change = old != new;
1787 if (change)
1788 snd_soc_write(codec, reg, new);
1789
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001790 return change;
1791}
1792EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1793
1794/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001795 * snd_soc_update_bits_locked - update codec register bits
1796 * @codec: audio codec
1797 * @reg: codec register
1798 * @mask: register mask
1799 * @value: new value
1800 *
1801 * Writes new register value, and takes the codec mutex.
1802 *
1803 * Returns 1 for change else 0.
1804 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001805int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1806 unsigned short reg, unsigned int mask,
1807 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001808{
1809 int change;
1810
1811 mutex_lock(&codec->mutex);
1812 change = snd_soc_update_bits(codec, reg, mask, value);
1813 mutex_unlock(&codec->mutex);
1814
1815 return change;
1816}
Mark Browndd1b3d52009-12-04 14:22:03 +00001817EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001818
1819/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001820 * snd_soc_test_bits - test register for change
1821 * @codec: audio codec
1822 * @reg: codec register
1823 * @mask: register mask
1824 * @value: new value
1825 *
1826 * Tests a register with a new value and checks if the new value is
1827 * different from the old value.
1828 *
1829 * Returns 1 for change else 0.
1830 */
1831int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001832 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833{
1834 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001835 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001836
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837 old = snd_soc_read(codec, reg);
1838 new = (old & ~mask) | value;
1839 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001840
1841 return change;
1842}
1843EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1844
1845/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1847 * @substream: the pcm substream
1848 * @hw: the hardware parameters
1849 *
1850 * Sets the substream runtime hardware parameters.
1851 */
1852int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1853 const struct snd_pcm_hardware *hw)
1854{
1855 struct snd_pcm_runtime *runtime = substream->runtime;
1856 runtime->hw.info = hw->info;
1857 runtime->hw.formats = hw->formats;
1858 runtime->hw.period_bytes_min = hw->period_bytes_min;
1859 runtime->hw.period_bytes_max = hw->period_bytes_max;
1860 runtime->hw.periods_min = hw->periods_min;
1861 runtime->hw.periods_max = hw->periods_max;
1862 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1863 runtime->hw.fifo_size = hw->fifo_size;
1864 return 0;
1865}
1866EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1867
1868/**
1869 * snd_soc_cnew - create new control
1870 * @_template: control template
1871 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001872 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873 *
1874 * Create a new mixer control from a template control.
1875 *
1876 * Returns 0 for success, else error.
1877 */
1878struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1879 void *data, char *long_name)
1880{
1881 struct snd_kcontrol_new template;
1882
1883 memcpy(&template, _template, sizeof(template));
1884 if (long_name)
1885 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886 template.index = 0;
1887
1888 return snd_ctl_new1(&template, data);
1889}
1890EXPORT_SYMBOL_GPL(snd_soc_cnew);
1891
1892/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001893 * snd_soc_add_controls - add an array of controls to a codec.
1894 * Convienience function to add a list of controls. Many codecs were
1895 * duplicating this code.
1896 *
1897 * @codec: codec to add controls to
1898 * @controls: array of controls to add
1899 * @num_controls: number of elements in the array
1900 *
1901 * Return 0 for success, else error.
1902 */
1903int snd_soc_add_controls(struct snd_soc_codec *codec,
1904 const struct snd_kcontrol_new *controls, int num_controls)
1905{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001906 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001907 int err, i;
1908
1909 for (i = 0; i < num_controls; i++) {
1910 const struct snd_kcontrol_new *control = &controls[i];
1911 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1912 if (err < 0) {
1913 dev_err(codec->dev, "%s: Failed to add %s\n",
1914 codec->name, control->name);
1915 return err;
1916 }
1917 }
1918
1919 return 0;
1920}
1921EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1922
1923/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001924 * snd_soc_info_enum_double - enumerated double mixer info callback
1925 * @kcontrol: mixer control
1926 * @uinfo: control element information
1927 *
1928 * Callback to provide information about a double enumerated
1929 * mixer control.
1930 *
1931 * Returns 0 for success.
1932 */
1933int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_info *uinfo)
1935{
1936 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1937
1938 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1939 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001940 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001942 if (uinfo->value.enumerated.item > e->max - 1)
1943 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944 strcpy(uinfo->value.enumerated.name,
1945 e->texts[uinfo->value.enumerated.item]);
1946 return 0;
1947}
1948EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1949
1950/**
1951 * snd_soc_get_enum_double - enumerated double mixer get callback
1952 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001953 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001954 *
1955 * Callback to get the value of a double enumerated mixer.
1956 *
1957 * Returns 0 for success.
1958 */
1959int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_value *ucontrol)
1961{
1962 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1963 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001964 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001966 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001967 ;
1968 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001969 ucontrol->value.enumerated.item[0]
1970 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971 if (e->shift_l != e->shift_r)
1972 ucontrol->value.enumerated.item[1] =
1973 (val >> e->shift_r) & (bitmask - 1);
1974
1975 return 0;
1976}
1977EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1978
1979/**
1980 * snd_soc_put_enum_double - enumerated double mixer put callback
1981 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001982 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 *
1984 * Callback to set the value of a double enumerated mixer.
1985 *
1986 * Returns 0 for success.
1987 */
1988int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
1990{
1991 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1992 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001993 unsigned int val;
1994 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001996 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001997 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001998 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999 return -EINVAL;
2000 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2001 mask = (bitmask - 1) << e->shift_l;
2002 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002003 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004 return -EINVAL;
2005 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2006 mask |= (bitmask - 1) << e->shift_r;
2007 }
2008
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002009 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010}
2011EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2012
2013/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002014 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2015 * @kcontrol: mixer control
2016 * @ucontrol: control element information
2017 *
2018 * Callback to get the value of a double semi enumerated mixer.
2019 *
2020 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2021 * used for handling bitfield coded enumeration for example.
2022 *
2023 * Returns 0 for success.
2024 */
2025int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2026 struct snd_ctl_elem_value *ucontrol)
2027{
2028 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002029 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002030 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002031
2032 reg_val = snd_soc_read(codec, e->reg);
2033 val = (reg_val >> e->shift_l) & e->mask;
2034 for (mux = 0; mux < e->max; mux++) {
2035 if (val == e->values[mux])
2036 break;
2037 }
2038 ucontrol->value.enumerated.item[0] = mux;
2039 if (e->shift_l != e->shift_r) {
2040 val = (reg_val >> e->shift_r) & e->mask;
2041 for (mux = 0; mux < e->max; mux++) {
2042 if (val == e->values[mux])
2043 break;
2044 }
2045 ucontrol->value.enumerated.item[1] = mux;
2046 }
2047
2048 return 0;
2049}
2050EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2051
2052/**
2053 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2054 * @kcontrol: mixer control
2055 * @ucontrol: control element information
2056 *
2057 * Callback to set the value of a double semi enumerated mixer.
2058 *
2059 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2060 * used for handling bitfield coded enumeration for example.
2061 *
2062 * Returns 0 for success.
2063 */
2064int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_value *ucontrol)
2066{
2067 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002068 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002069 unsigned int val;
2070 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002071
2072 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2073 return -EINVAL;
2074 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2075 mask = e->mask << e->shift_l;
2076 if (e->shift_l != e->shift_r) {
2077 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2078 return -EINVAL;
2079 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2080 mask |= e->mask << e->shift_r;
2081 }
2082
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002083 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002084}
2085EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2086
2087/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002088 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2089 * @kcontrol: mixer control
2090 * @uinfo: control element information
2091 *
2092 * Callback to provide information about an external enumerated
2093 * single mixer.
2094 *
2095 * Returns 0 for success.
2096 */
2097int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_info *uinfo)
2099{
2100 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2101
2102 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2103 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002104 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002105
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002106 if (uinfo->value.enumerated.item > e->max - 1)
2107 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108 strcpy(uinfo->value.enumerated.name,
2109 e->texts[uinfo->value.enumerated.item]);
2110 return 0;
2111}
2112EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2113
2114/**
2115 * snd_soc_info_volsw_ext - external single mixer info callback
2116 * @kcontrol: mixer control
2117 * @uinfo: control element information
2118 *
2119 * Callback to provide information about a single external mixer control.
2120 *
2121 * Returns 0 for success.
2122 */
2123int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_info *uinfo)
2125{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002126 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002127
Mark Brownfd5dfad2009-04-15 21:37:46 +01002128 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002129 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2130 else
2131 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2132
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133 uinfo->count = 1;
2134 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002135 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136 return 0;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2139
2140/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141 * snd_soc_info_volsw - single mixer info callback
2142 * @kcontrol: mixer control
2143 * @uinfo: control element information
2144 *
2145 * Callback to provide information about a single mixer control.
2146 *
2147 * Returns 0 for success.
2148 */
2149int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_info *uinfo)
2151{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002152 struct soc_mixer_control *mc =
2153 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002154 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002155 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002156 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002158 if (!mc->platform_max)
2159 mc->platform_max = mc->max;
2160 platform_max = mc->platform_max;
2161
2162 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002163 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2164 else
2165 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2166
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002167 uinfo->count = shift == rshift ? 1 : 2;
2168 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002169 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2173
2174/**
2175 * snd_soc_get_volsw - single mixer get callback
2176 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002177 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 *
2179 * Callback to get the value of a single mixer control.
2180 *
2181 * Returns 0 for success.
2182 */
2183int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2185{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002186 struct soc_mixer_control *mc =
2187 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002188 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002189 unsigned int reg = mc->reg;
2190 unsigned int shift = mc->shift;
2191 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002192 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002193 unsigned int mask = (1 << fls(max)) - 1;
2194 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195
2196 ucontrol->value.integer.value[0] =
2197 (snd_soc_read(codec, reg) >> shift) & mask;
2198 if (shift != rshift)
2199 ucontrol->value.integer.value[1] =
2200 (snd_soc_read(codec, reg) >> rshift) & mask;
2201 if (invert) {
2202 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002203 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002204 if (shift != rshift)
2205 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002206 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002207 }
2208
2209 return 0;
2210}
2211EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2212
2213/**
2214 * snd_soc_put_volsw - single mixer put callback
2215 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002216 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217 *
2218 * Callback to set the value of a single mixer control.
2219 *
2220 * Returns 0 for success.
2221 */
2222int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
2224{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002225 struct soc_mixer_control *mc =
2226 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002228 unsigned int reg = mc->reg;
2229 unsigned int shift = mc->shift;
2230 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002231 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002232 unsigned int mask = (1 << fls(max)) - 1;
2233 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002234 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235
2236 val = (ucontrol->value.integer.value[0] & mask);
2237 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002238 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239 val_mask = mask << shift;
2240 val = val << shift;
2241 if (shift != rshift) {
2242 val2 = (ucontrol->value.integer.value[1] & mask);
2243 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002244 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 val_mask |= mask << rshift;
2246 val |= val2 << rshift;
2247 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002248 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249}
2250EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2251
2252/**
2253 * snd_soc_info_volsw_2r - double mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2256 *
2257 * Callback to provide information about a double mixer control that
2258 * spans 2 codec registers.
2259 *
2260 * Returns 0 for success.
2261 */
2262int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_info *uinfo)
2264{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002265 struct soc_mixer_control *mc =
2266 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002267 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002269 if (!mc->platform_max)
2270 mc->platform_max = mc->max;
2271 platform_max = mc->platform_max;
2272
2273 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002274 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2275 else
2276 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2277
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278 uinfo->count = 2;
2279 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002280 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281 return 0;
2282}
2283EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2284
2285/**
2286 * snd_soc_get_volsw_2r - double mixer get callback
2287 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002288 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 *
2290 * Callback to get the value of a double mixer control that spans 2 registers.
2291 *
2292 * Returns 0 for success.
2293 */
2294int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
2296{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002297 struct soc_mixer_control *mc =
2298 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002300 unsigned int reg = mc->reg;
2301 unsigned int reg2 = mc->rreg;
2302 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002303 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002304 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002305 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306
2307 ucontrol->value.integer.value[0] =
2308 (snd_soc_read(codec, reg) >> shift) & mask;
2309 ucontrol->value.integer.value[1] =
2310 (snd_soc_read(codec, reg2) >> shift) & mask;
2311 if (invert) {
2312 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002313 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002314 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002315 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 }
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2321
2322/**
2323 * snd_soc_put_volsw_2r - double mixer set callback
2324 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002325 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 *
2327 * Callback to set the value of a double mixer control that spans 2 registers.
2328 *
2329 * Returns 0 for success.
2330 */
2331int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2333{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002334 struct soc_mixer_control *mc =
2335 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002337 unsigned int reg = mc->reg;
2338 unsigned int reg2 = mc->rreg;
2339 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002340 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002341 unsigned int mask = (1 << fls(max)) - 1;
2342 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002344 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345
2346 val_mask = mask << shift;
2347 val = (ucontrol->value.integer.value[0] & mask);
2348 val2 = (ucontrol->value.integer.value[1] & mask);
2349
2350 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002351 val = max - val;
2352 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002353 }
2354
2355 val = val << shift;
2356 val2 = val2 << shift;
2357
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002358 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002359 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 return err;
2361
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002362 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363 return err;
2364}
2365EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2366
Mark Browne13ac2e2008-05-28 17:58:05 +01002367/**
2368 * snd_soc_info_volsw_s8 - signed mixer info callback
2369 * @kcontrol: mixer control
2370 * @uinfo: control element information
2371 *
2372 * Callback to provide information about a signed mixer control.
2373 *
2374 * Returns 0 for success.
2375 */
2376int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_info *uinfo)
2378{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002379 struct soc_mixer_control *mc =
2380 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002381 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002382 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002383
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002384 if (!mc->platform_max)
2385 mc->platform_max = mc->max;
2386 platform_max = mc->platform_max;
2387
Mark Browne13ac2e2008-05-28 17:58:05 +01002388 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2389 uinfo->count = 2;
2390 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002391 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002392 return 0;
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2395
2396/**
2397 * snd_soc_get_volsw_s8 - signed mixer get callback
2398 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002399 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002400 *
2401 * Callback to get the value of a signed mixer control.
2402 *
2403 * Returns 0 for success.
2404 */
2405int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_value *ucontrol)
2407{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002408 struct soc_mixer_control *mc =
2409 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002410 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002411 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002412 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002413 int val = snd_soc_read(codec, reg);
2414
2415 ucontrol->value.integer.value[0] =
2416 ((signed char)(val & 0xff))-min;
2417 ucontrol->value.integer.value[1] =
2418 ((signed char)((val >> 8) & 0xff))-min;
2419 return 0;
2420}
2421EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2422
2423/**
2424 * snd_soc_put_volsw_sgn - signed mixer put callback
2425 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002426 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002427 *
2428 * Callback to set the value of a signed mixer control.
2429 *
2430 * Returns 0 for success.
2431 */
2432int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2434{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002435 struct soc_mixer_control *mc =
2436 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002437 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002438 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002439 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002440 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002441
2442 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2443 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2444
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002445 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002446}
2447EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2448
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002449/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002450 * snd_soc_limit_volume - Set new limit to an existing volume control.
2451 *
2452 * @codec: where to look for the control
2453 * @name: Name of the control
2454 * @max: new maximum limit
2455 *
2456 * Return 0 for success, else error.
2457 */
2458int snd_soc_limit_volume(struct snd_soc_codec *codec,
2459 const char *name, int max)
2460{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002461 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002462 struct snd_kcontrol *kctl;
2463 struct soc_mixer_control *mc;
2464 int found = 0;
2465 int ret = -EINVAL;
2466
2467 /* Sanity check for name and max */
2468 if (unlikely(!name || max <= 0))
2469 return -EINVAL;
2470
2471 list_for_each_entry(kctl, &card->controls, list) {
2472 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2473 found = 1;
2474 break;
2475 }
2476 }
2477 if (found) {
2478 mc = (struct soc_mixer_control *)kctl->private_value;
2479 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002480 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002481 ret = 0;
2482 }
2483 }
2484 return ret;
2485}
2486EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2487
2488/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002489 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2490 * mixer info callback
2491 * @kcontrol: mixer control
2492 * @uinfo: control element information
2493 *
2494 * Returns 0 for success.
2495 */
2496int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_info *uinfo)
2498{
2499 struct soc_mixer_control *mc =
2500 (struct soc_mixer_control *)kcontrol->private_value;
2501 int max = mc->max;
2502 int min = mc->min;
2503
2504 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2505 uinfo->count = 2;
2506 uinfo->value.integer.min = 0;
2507 uinfo->value.integer.max = max-min;
2508
2509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2512
2513/**
2514 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2515 * mixer get callback
2516 * @kcontrol: mixer control
2517 * @uinfo: control element information
2518 *
2519 * Returns 0 for success.
2520 */
2521int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_value *ucontrol)
2523{
2524 struct soc_mixer_control *mc =
2525 (struct soc_mixer_control *)kcontrol->private_value;
2526 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2527 unsigned int mask = (1<<mc->shift)-1;
2528 int min = mc->min;
2529 int val = snd_soc_read(codec, mc->reg) & mask;
2530 int valr = snd_soc_read(codec, mc->rreg) & mask;
2531
Stuart Longland20630c72010-06-18 12:56:10 +10002532 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2533 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002534 return 0;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2537
2538/**
2539 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2540 * mixer put callback
2541 * @kcontrol: mixer control
2542 * @uinfo: control element information
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
2549 struct soc_mixer_control *mc =
2550 (struct soc_mixer_control *)kcontrol->private_value;
2551 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2552 unsigned int mask = (1<<mc->shift)-1;
2553 int min = mc->min;
2554 int ret;
2555 unsigned int val, valr, oval, ovalr;
2556
2557 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2558 val &= mask;
2559 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2560 valr &= mask;
2561
2562 oval = snd_soc_read(codec, mc->reg) & mask;
2563 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2564
2565 ret = 0;
2566 if (oval != val) {
2567 ret = snd_soc_write(codec, mc->reg, val);
2568 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002569 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002570 }
2571 if (ovalr != valr) {
2572 ret = snd_soc_write(codec, mc->rreg, valr);
2573 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002574 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002575 }
2576
2577 return 0;
2578}
2579EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2580
2581/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002582 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2583 * @dai: DAI
2584 * @clk_id: DAI specific clock ID
2585 * @freq: new clock frequency in Hz
2586 * @dir: new clock direction - input/output.
2587 *
2588 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2589 */
2590int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2591 unsigned int freq, int dir)
2592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002593 if (dai->driver && dai->driver->ops->set_sysclk)
2594 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002595 else
2596 return -EINVAL;
2597}
2598EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2599
2600/**
2601 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2602 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002603 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002604 * @div: new clock divisor.
2605 *
2606 * Configures the clock dividers. This is used to derive the best DAI bit and
2607 * frame clocks from the system or master clock. It's best to set the DAI bit
2608 * and frame clocks as low as possible to save system power.
2609 */
2610int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2611 int div_id, int div)
2612{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002613 if (dai->driver && dai->driver->ops->set_clkdiv)
2614 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002615 else
2616 return -EINVAL;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2619
2620/**
2621 * snd_soc_dai_set_pll - configure DAI PLL.
2622 * @dai: DAI
2623 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002624 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002625 * @freq_in: PLL input clock frequency in Hz
2626 * @freq_out: requested PLL output clock frequency in Hz
2627 *
2628 * Configures and enables PLL to generate output clock based on input clock.
2629 */
Mark Brown85488032009-09-05 18:52:16 +01002630int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2631 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002633 if (dai->driver && dai->driver->ops->set_pll)
2634 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002635 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002636 else
2637 return -EINVAL;
2638}
2639EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2640
2641/**
2642 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2643 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002644 * @fmt: SND_SOC_DAIFMT_ format value.
2645 *
2646 * Configures the DAI hardware format and clocking.
2647 */
2648int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2649{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002650 if (dai->driver && dai->driver->ops->set_fmt)
2651 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002652 else
2653 return -EINVAL;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2656
2657/**
2658 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2659 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002660 * @tx_mask: bitmask representing active TX slots.
2661 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002662 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002663 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002664 *
2665 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2666 * specific.
2667 */
2668int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002669 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002670{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002671 if (dai->driver && dai->driver->ops->set_tdm_slot)
2672 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002673 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002674 else
2675 return -EINVAL;
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2678
2679/**
Barry Song472df3c2009-09-12 01:16:29 +08002680 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2681 * @dai: DAI
2682 * @tx_num: how many TX channels
2683 * @tx_slot: pointer to an array which imply the TX slot number channel
2684 * 0~num-1 uses
2685 * @rx_num: how many RX channels
2686 * @rx_slot: pointer to an array which imply the RX slot number channel
2687 * 0~num-1 uses
2688 *
2689 * configure the relationship between channel number and TDM slot number.
2690 */
2691int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2692 unsigned int tx_num, unsigned int *tx_slot,
2693 unsigned int rx_num, unsigned int *rx_slot)
2694{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002695 if (dai->driver && dai->driver->ops->set_channel_map)
2696 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002697 rx_num, rx_slot);
2698 else
2699 return -EINVAL;
2700}
2701EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2702
2703/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002704 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2705 * @dai: DAI
2706 * @tristate: tristate enable
2707 *
2708 * Tristates the DAI so that others can use it.
2709 */
2710int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2711{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002712 if (dai->driver && dai->driver->ops->set_tristate)
2713 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002714 else
2715 return -EINVAL;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2718
2719/**
2720 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2721 * @dai: DAI
2722 * @mute: mute enable
2723 *
2724 * Mutes the DAI DAC.
2725 */
2726int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2727{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002728 if (dai->driver && dai->driver->ops->digital_mute)
2729 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002730 else
2731 return -EINVAL;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2734
Mark Brownc5af3a22008-11-28 13:29:45 +00002735/**
2736 * snd_soc_register_card - Register a card with the ASoC core
2737 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002738 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002739 *
2740 * Note that currently this is an internal only function: it will be
2741 * exposed to machine drivers after further backporting of ASoC v2
2742 * registration APIs.
2743 */
2744static int snd_soc_register_card(struct snd_soc_card *card)
2745{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002746 int i;
2747
Mark Brownc5af3a22008-11-28 13:29:45 +00002748 if (!card->name || !card->dev)
2749 return -EINVAL;
2750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002751 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2752 GFP_KERNEL);
2753 if (card->rtd == NULL)
2754 return -ENOMEM;
2755
2756 for (i = 0; i < card->num_links; i++)
2757 card->rtd[i].dai_link = &card->dai_link[i];
2758
Mark Brownc5af3a22008-11-28 13:29:45 +00002759 INIT_LIST_HEAD(&card->list);
2760 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002761 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002762
2763 mutex_lock(&client_mutex);
2764 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002765 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002766 mutex_unlock(&client_mutex);
2767
2768 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2769
2770 return 0;
2771}
2772
2773/**
2774 * snd_soc_unregister_card - Unregister a card with the ASoC core
2775 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002776 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002777 *
2778 * Note that currently this is an internal only function: it will be
2779 * exposed to machine drivers after further backporting of ASoC v2
2780 * registration APIs.
2781 */
2782static int snd_soc_unregister_card(struct snd_soc_card *card)
2783{
2784 mutex_lock(&client_mutex);
2785 list_del(&card->list);
2786 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002787 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2788
2789 return 0;
2790}
2791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002792/*
2793 * Simplify DAI link configuration by removing ".-1" from device names
2794 * and sanitizing names.
2795 */
2796static inline char *fmt_single_name(struct device *dev, int *id)
2797{
2798 char *found, name[NAME_SIZE];
2799 int id1, id2;
2800
2801 if (dev_name(dev) == NULL)
2802 return NULL;
2803
2804 strncpy(name, dev_name(dev), NAME_SIZE);
2805
2806 /* are we a "%s.%d" name (platform and SPI components) */
2807 found = strstr(name, dev->driver->name);
2808 if (found) {
2809 /* get ID */
2810 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2811
2812 /* discard ID from name if ID == -1 */
2813 if (*id == -1)
2814 found[strlen(dev->driver->name)] = '\0';
2815 }
2816
2817 } else {
2818 /* I2C component devices are named "bus-addr" */
2819 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2820 char tmp[NAME_SIZE];
2821
2822 /* create unique ID number from I2C addr and bus */
2823 *id = ((id1 && 0xffff) << 16) + id2;
2824
2825 /* sanitize component name for DAI link creation */
2826 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2827 strncpy(name, tmp, NAME_SIZE);
2828 } else
2829 *id = 0;
2830 }
2831
2832 return kstrdup(name, GFP_KERNEL);
2833}
2834
2835/*
2836 * Simplify DAI link naming for single devices with multiple DAIs by removing
2837 * any ".-1" and using the DAI name (instead of device name).
2838 */
2839static inline char *fmt_multiple_name(struct device *dev,
2840 struct snd_soc_dai_driver *dai_drv)
2841{
2842 if (dai_drv->name == NULL) {
2843 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2844 dev_name(dev));
2845 return NULL;
2846 }
2847
2848 return kstrdup(dai_drv->name, GFP_KERNEL);
2849}
2850
Mark Brown91151712008-11-30 23:31:24 +00002851/**
2852 * snd_soc_register_dai - Register a DAI with the ASoC core
2853 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002854 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002855 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002856int snd_soc_register_dai(struct device *dev,
2857 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002858{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002859 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002860
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002861 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002863 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2864 if (dai == NULL)
2865 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002866
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002867 /* create DAI component name */
2868 dai->name = fmt_single_name(dev, &dai->id);
2869 if (dai->name == NULL) {
2870 kfree(dai);
2871 return -ENOMEM;
2872 }
2873
2874 dai->dev = dev;
2875 dai->driver = dai_drv;
2876 if (!dai->driver->ops)
2877 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002878
2879 mutex_lock(&client_mutex);
2880 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002881 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002882 mutex_unlock(&client_mutex);
2883
2884 pr_debug("Registered DAI '%s'\n", dai->name);
2885
2886 return 0;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2889
2890/**
2891 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2892 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002893 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002894 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002895void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002896{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002897 struct snd_soc_dai *dai;
2898
2899 list_for_each_entry(dai, &dai_list, list) {
2900 if (dev == dai->dev)
2901 goto found;
2902 }
2903 return;
2904
2905found:
Mark Brown91151712008-11-30 23:31:24 +00002906 mutex_lock(&client_mutex);
2907 list_del(&dai->list);
2908 mutex_unlock(&client_mutex);
2909
2910 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002911 kfree(dai->name);
2912 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002913}
2914EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2915
2916/**
2917 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2918 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002919 * @dai: Array of DAIs to register
2920 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002921 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002922int snd_soc_register_dais(struct device *dev,
2923 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002924{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002925 struct snd_soc_dai *dai;
2926 int i, ret = 0;
2927
2928 dev_dbg(dev, "dai register %s #%d\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002929
2930 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002931
2932 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2933 if (dai == NULL)
2934 return -ENOMEM;
2935
2936 /* create DAI component name */
2937 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2938 if (dai->name == NULL) {
2939 kfree(dai);
2940 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00002941 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002942 }
2943
2944 dai->dev = dev;
2945 dai->id = i;
2946 dai->driver = &dai_drv[i];
2947 if (!dai->driver->ops)
2948 dai->driver->ops = &null_dai_ops;
2949
2950 mutex_lock(&client_mutex);
2951 list_add(&dai->list, &dai_list);
2952 mutex_unlock(&client_mutex);
2953
2954 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002955 }
2956
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002957 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002958 return 0;
2959
2960err:
2961 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002962 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002963
2964 return ret;
2965}
2966EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2967
2968/**
2969 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2970 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002971 * @dai: Array of DAIs to unregister
2972 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002973 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002974void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002975{
2976 int i;
2977
2978 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002979 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002980}
2981EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2982
Mark Brown12a48a8c2008-12-03 19:40:30 +00002983/**
2984 * snd_soc_register_platform - Register a platform with the ASoC core
2985 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002986 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002987 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002988int snd_soc_register_platform(struct device *dev,
2989 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002990{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002991 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002992
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002993 dev_dbg(dev, "platform register %s\n", dev_name(dev));
2994
2995 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2996 if (platform == NULL)
2997 return -ENOMEM;
2998
2999 /* create platform component name */
3000 platform->name = fmt_single_name(dev, &platform->id);
3001 if (platform->name == NULL) {
3002 kfree(platform);
3003 return -ENOMEM;
3004 }
3005
3006 platform->dev = dev;
3007 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003008
3009 mutex_lock(&client_mutex);
3010 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003011 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003012 mutex_unlock(&client_mutex);
3013
3014 pr_debug("Registered platform '%s'\n", platform->name);
3015
3016 return 0;
3017}
3018EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3019
3020/**
3021 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3022 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003023 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003024 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003025void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003026{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003027 struct snd_soc_platform *platform;
3028
3029 list_for_each_entry(platform, &platform_list, list) {
3030 if (dev == platform->dev)
3031 goto found;
3032 }
3033 return;
3034
3035found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003036 mutex_lock(&client_mutex);
3037 list_del(&platform->list);
3038 mutex_unlock(&client_mutex);
3039
3040 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003041 kfree(platform->name);
3042 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003043}
3044EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3045
Mark Brown151ab222009-05-09 16:22:58 +01003046static u64 codec_format_map[] = {
3047 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3048 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3049 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3050 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3051 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3052 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3053 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3054 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3055 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3056 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3057 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3058 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3059 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3060 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3061 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3062 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3063};
3064
3065/* Fix up the DAI formats for endianness: codecs don't actually see
3066 * the endianness of the data but we're using the CPU format
3067 * definitions which do need to include endianness so we ensure that
3068 * codec DAIs always have both big and little endian variants set.
3069 */
3070static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3071{
3072 int i;
3073
3074 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3075 if (stream->formats & codec_format_map[i])
3076 stream->formats |= codec_format_map[i];
3077}
3078
Mark Brown0d0cf002008-12-10 14:32:45 +00003079/**
3080 * snd_soc_register_codec - Register a codec with the ASoC core
3081 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003082 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003083 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084int snd_soc_register_codec(struct device *dev,
3085 struct snd_soc_codec_driver *codec_drv,
3086 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003087{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088 struct snd_soc_codec *codec;
3089 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003090
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003092
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003093 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3094 if (codec == NULL)
3095 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003096
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003097 /* create CODEC component name */
3098 codec->name = fmt_single_name(dev, &codec->id);
3099 if (codec->name == NULL) {
3100 kfree(codec);
3101 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003102 }
3103
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003104 /* allocate CODEC register cache */
3105 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3106
3107 if (codec_drv->reg_cache_default)
3108 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3109 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3110 else
3111 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3112 codec_drv->reg_word_size, GFP_KERNEL);
3113
3114 if (codec->reg_cache == NULL) {
3115 kfree(codec->name);
3116 kfree(codec);
3117 return -ENOMEM;
3118 }
3119 }
3120
3121 codec->dev = dev;
3122 codec->driver = codec_drv;
3123 codec->bias_level = SND_SOC_BIAS_OFF;
3124 codec->num_dai = num_dai;
3125 mutex_init(&codec->mutex);
3126 INIT_LIST_HEAD(&codec->dapm_widgets);
3127 INIT_LIST_HEAD(&codec->dapm_paths);
3128
3129 for (i = 0; i < num_dai; i++) {
3130 fixup_codec_formats(&dai_drv[i].playback);
3131 fixup_codec_formats(&dai_drv[i].capture);
3132 }
3133
3134 /* register DAIs */
3135 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3136 if (ret < 0)
3137 goto error;
3138
Mark Brown0d0cf002008-12-10 14:32:45 +00003139 mutex_lock(&client_mutex);
3140 list_add(&codec->list, &codec_list);
3141 snd_soc_instantiate_cards();
3142 mutex_unlock(&client_mutex);
3143
3144 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003145 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003146
3147error:
3148 for (i--; i >= 0; i--)
3149 snd_soc_unregister_dai(dev);
3150
3151 if (codec->reg_cache)
3152 kfree(codec->reg_cache);
3153 kfree(codec->name);
3154 kfree(codec);
3155 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003156}
3157EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3158
3159/**
3160 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3161 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003162 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003163 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003164void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003166 struct snd_soc_codec *codec;
3167 int i;
3168
3169 list_for_each_entry(codec, &codec_list, list) {
3170 if (dev == codec->dev)
3171 goto found;
3172 }
3173 return;
3174
3175found:
3176 for (i = 0; i < codec->num_dai; i++)
3177 snd_soc_unregister_dai(dev);
3178
Mark Brown0d0cf002008-12-10 14:32:45 +00003179 mutex_lock(&client_mutex);
3180 list_del(&codec->list);
3181 mutex_unlock(&client_mutex);
3182
3183 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184
3185 if (codec->reg_cache)
3186 kfree(codec->reg_cache);
3187 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003188}
3189EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3190
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003191static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003192{
Mark Brown384c89e2008-12-03 17:34:03 +00003193#ifdef CONFIG_DEBUG_FS
3194 debugfs_root = debugfs_create_dir("asoc", NULL);
3195 if (IS_ERR(debugfs_root) || !debugfs_root) {
3196 printk(KERN_WARNING
3197 "ASoC: Failed to create debugfs directory\n");
3198 debugfs_root = NULL;
3199 }
3200#endif
3201
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003202 return platform_driver_register(&soc_driver);
3203}
3204
Mark Brown7d8c16a2008-11-30 22:11:24 +00003205static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003206{
Mark Brown384c89e2008-12-03 17:34:03 +00003207#ifdef CONFIG_DEBUG_FS
3208 debugfs_remove_recursive(debugfs_root);
3209#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003210 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003211}
3212
3213module_init(snd_soc_init);
3214module_exit(snd_soc_exit);
3215
3216/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003217MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003218MODULE_DESCRIPTION("ALSA SoC Core");
3219MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003220MODULE_ALIAS("platform:soc-audio");