blob: cea0fe488623b52328fdae67c50a5743eac72910 [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{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100237 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 debugfs_root);
239 if (!codec->debugfs_codec_root) {
240 printk(KERN_WARNING
241 "ASoC: Failed to create codec debugfs directory\n");
242 return;
243 }
244
245 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
246 codec->debugfs_codec_root,
247 codec, &codec_reg_fops);
248 if (!codec->debugfs_reg)
249 printk(KERN_WARNING
250 "ASoC: Failed to create codec register debugfs file\n");
251
252 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
253 codec->debugfs_codec_root,
254 &codec->pop_time);
255 if (!codec->debugfs_pop_time)
256 printk(KERN_WARNING
257 "Failed to create pop time debugfs file\n");
258
259 codec->debugfs_dapm = debugfs_create_dir("dapm",
260 codec->debugfs_codec_root);
261 if (!codec->debugfs_dapm)
262 printk(KERN_WARNING
263 "Failed to create DAPM debugfs directory\n");
264
265 snd_soc_dapm_debugfs_init(codec);
266}
267
268static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
269{
270 debugfs_remove_recursive(codec->debugfs_codec_root);
271}
272
273#else
274
275static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
276{
277}
278
279static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
280{
281}
282#endif
283
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200284#ifdef CONFIG_SND_SOC_AC97_BUS
285/* unregister ac97 codec */
286static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
287{
288 if (codec->ac97->dev.bus)
289 device_unregister(&codec->ac97->dev);
290 return 0;
291}
292
293/* stop no dev release warning */
294static void soc_ac97_device_release(struct device *dev){}
295
296/* register ac97 codec to bus */
297static int soc_ac97_dev_register(struct snd_soc_codec *codec)
298{
299 int err;
300
301 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100302 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200303 codec->ac97->dev.release = soc_ac97_device_release;
304
Kay Sieversbb072bf2008-11-02 03:50:35 +0100305 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000306 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200307 err = device_register(&codec->ac97->dev);
308 if (err < 0) {
309 snd_printk(KERN_ERR "Can't register ac97 bus\n");
310 codec->ac97->dev.bus = NULL;
311 return err;
312 }
313 return 0;
314}
315#endif
316
Mark Brown06f409d2009-04-07 18:10:13 +0100317static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
318{
319 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
321 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100322 int ret;
323
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000324 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
325 rtd->dai_link->symmetric_rates) {
326 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
327 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100328
329 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
330 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 rtd->rate,
332 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100333 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000334 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100335 "Unable to apply rate symmetry constraint: %d\n", ret);
336 return ret;
337 }
338 }
339
340 return 0;
341}
342
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200343/*
344 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
345 * then initialized and any private data can be allocated. This also calls
346 * startup for the cpu DAI, platform, machine and codec DAI.
347 */
348static int soc_pcm_open(struct snd_pcm_substream *substream)
349{
350 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000352 struct snd_soc_platform *platform = rtd->platform;
353 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
354 struct snd_soc_dai *codec_dai = rtd->codec_dai;
355 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
356 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200357 int ret = 0;
358
359 mutex_lock(&pcm_mutex);
360
361 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362 if (cpu_dai->driver->ops->startup) {
363 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200364 if (ret < 0) {
365 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100366 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200367 goto out;
368 }
369 }
370
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000371 if (platform->driver->ops->open) {
372 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200373 if (ret < 0) {
374 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
375 goto platform_err;
376 }
377 }
378
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000379 if (codec_dai->driver->ops->startup) {
380 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100381 if (ret < 0) {
382 printk(KERN_ERR "asoc: can't open codec %s\n",
383 codec_dai->name);
384 goto codec_dai_err;
385 }
386 }
387
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
389 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200390 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200392 goto machine_err;
393 }
394 }
395
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200396 /* Check that the codec and cpu DAI's are compatible */
397 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
398 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000399 max(codec_dai_drv->playback.rate_min,
400 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200401 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000402 min(codec_dai_drv->playback.rate_max,
403 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200404 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 max(codec_dai_drv->playback.channels_min,
406 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408 min(codec_dai_drv->playback.channels_max,
409 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100410 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000411 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100412 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000413 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
414 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900415 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000416 runtime->hw.rates |= cpu_dai_drv->playback.rates;
417 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900418 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200420 } else {
421 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000422 max(codec_dai_drv->capture.rate_min,
423 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200424 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 min(codec_dai_drv->capture.rate_max,
426 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200427 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 max(codec_dai_drv->capture.channels_min,
429 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200430 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431 min(codec_dai_drv->capture.channels_max,
432 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100433 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100435 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000436 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
437 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900438 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000439 runtime->hw.rates |= cpu_dai_drv->capture.rates;
440 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900441 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443 }
444
445 snd_pcm_limit_hw_rates(runtime);
446 if (!runtime->hw.rates) {
447 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100448 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900449 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 }
451 if (!runtime->hw.formats) {
452 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100453 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900454 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200455 }
456 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
457 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900459 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 }
461
Mark Brown06f409d2009-04-07 18:10:13 +0100462 /* Symmetry only applies if we've already got an active stream. */
463 if (cpu_dai->active || codec_dai->active) {
464 ret = soc_pcm_apply_symmetry(substream);
465 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900466 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100467 }
468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 pr_debug("asoc: %s <-> %s info:\n",
470 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100471 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
472 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
473 runtime->hw.channels_max);
474 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
475 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200476
Jassi Brar14dc5732010-02-26 09:12:32 +0900477 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 cpu_dai->playback_active++;
479 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900480 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000481 cpu_dai->capture_active++;
482 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900483 }
484 cpu_dai->active++;
485 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 mutex_unlock(&pcm_mutex);
488 return 0;
489
Jassi Brarbb1c0472010-02-25 11:24:53 +0900490config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
492 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493
Jassi Brarbb1c0472010-02-25 11:24:53 +0900494machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 if (codec_dai->driver->ops->shutdown)
496 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900497
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100498codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499 if (platform->driver->ops->close)
500 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501
502platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 if (cpu_dai->driver->ops->shutdown)
504 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505out:
506 mutex_unlock(&pcm_mutex);
507 return ret;
508}
509
510/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200511 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 * This is to ensure there are no pops or clicks in between any music tracks
513 * due to DAPM power cycling.
514 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100515static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 struct snd_soc_pcm_runtime *rtd =
518 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
519 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520
521 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
524 codec_dai->driver->playback.stream_name,
525 codec_dai->playback_active ? "active" : "inactive",
526 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 /* are we waiting on this codec DAI stream */
529 if (codec_dai->pop_wait == 1) {
530 codec_dai->pop_wait = 0;
531 snd_soc_dapm_stream_event(rtd,
532 codec_dai->driver->playback.stream_name,
533 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 mutex_unlock(&pcm_mutex);
537}
538
539/*
540 * Called by ALSA when a PCM substream is closed. Private data can be
541 * freed here. The cpu DAI, codec DAI, machine and platform are also
542 * shutdown.
543 */
544static int soc_codec_close(struct snd_pcm_substream *substream)
545{
546 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 struct snd_soc_platform *platform = rtd->platform;
548 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
549 struct snd_soc_dai *codec_dai = rtd->codec_dai;
550 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200551
552 mutex_lock(&pcm_mutex);
553
Jassi Brar14dc5732010-02-26 09:12:32 +0900554 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 cpu_dai->playback_active--;
556 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900557 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 cpu_dai->capture_active--;
559 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900561
562 cpu_dai->active--;
563 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 codec->active--;
565
Mark Brown6010b2d2008-09-06 18:33:24 +0100566 /* Muting the DAC suppresses artifacts caused during digital
567 * shutdown, for example from stopping clocks.
568 */
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
570 snd_soc_dai_digital_mute(codec_dai, 1);
571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 if (cpu_dai->driver->ops->shutdown)
573 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 if (codec_dai->driver->ops->shutdown)
576 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
579 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (platform->driver->ops->close)
582 platform->driver->ops->close(substream);
583 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584
585 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
586 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100587 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 schedule_delayed_work(&rtd->delayed_work,
589 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590 } else {
591 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 snd_soc_dapm_stream_event(rtd,
593 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100594 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 }
596
597 mutex_unlock(&pcm_mutex);
598 return 0;
599}
600
601/*
602 * Called by ALSA when the PCM substream is prepared, can set format, sample
603 * rate, etc. This function is non atomic and can be called multiple times,
604 * it can refer to the runtime info.
605 */
606static int soc_pcm_prepare(struct snd_pcm_substream *substream)
607{
608 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 struct snd_soc_platform *platform = rtd->platform;
610 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
611 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 int ret = 0;
613
614 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100615
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
617 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100618 if (ret < 0) {
619 printk(KERN_ERR "asoc: machine prepare error\n");
620 goto out;
621 }
622 }
623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 if (platform->driver->ops->prepare) {
625 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200626 if (ret < 0) {
627 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200629 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 }
631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 if (codec_dai->driver->ops->prepare) {
633 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200634 if (ret < 0) {
635 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200637 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638 }
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 if (cpu_dai->driver->ops->prepare) {
641 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100642 if (ret < 0) {
643 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
644 goto out;
645 }
646 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647
Mark Brownd45f6212008-10-14 13:58:36 +0100648 /* cancel any delayed stream shutdown that is pending */
649 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
650 codec_dai->pop_wait) {
651 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100653 }
654
Mark Brown452c5ea2009-05-17 21:41:23 +0100655 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 snd_soc_dapm_stream_event(rtd,
657 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100658 SND_SOC_DAPM_STREAM_START);
659 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 snd_soc_dapm_stream_event(rtd,
661 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100662 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100663
Mark Brown452c5ea2009-05-17 21:41:23 +0100664 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
666out:
667 mutex_unlock(&pcm_mutex);
668 return ret;
669}
670
671/*
672 * Called by ALSA when the hardware params are set by application. This
673 * function can also be called multiple times and can allocate buffers
674 * (using snd_pcm_lib_* ). It's non-atomic.
675 */
676static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
677 struct snd_pcm_hw_params *params)
678{
679 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 struct snd_soc_platform *platform = rtd->platform;
681 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
682 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 int ret = 0;
684
685 mutex_lock(&pcm_mutex);
686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
688 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200689 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100690 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691 goto out;
692 }
693 }
694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 if (codec_dai->driver->ops->hw_params) {
696 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100697 if (ret < 0) {
698 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
699 codec_dai->name);
700 goto codec_err;
701 }
702 }
703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 if (cpu_dai->driver->ops->hw_params) {
705 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200707 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100708 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 goto interface_err;
710 }
711 }
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (platform->driver->ops->hw_params) {
714 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200716 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 platform->name);
718 goto platform_err;
719 }
720 }
721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100723
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724out:
725 mutex_unlock(&pcm_mutex);
726 return ret;
727
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 if (cpu_dai->driver->ops->hw_free)
730 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731
732interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (codec_dai->driver->ops->hw_free)
734 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100735
736codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
738 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739
740 mutex_unlock(&pcm_mutex);
741 return ret;
742}
743
744/*
745 * Free's resources allocated by hw_params, can be called multiple times
746 */
747static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
748{
749 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 struct snd_soc_platform *platform = rtd->platform;
751 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
752 struct snd_soc_dai *codec_dai = rtd->codec_dai;
753 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
755 mutex_lock(&pcm_mutex);
756
757 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100758 if (!codec->active)
759 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760
761 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
763 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764
765 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766 if (platform->driver->ops->hw_free)
767 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768
769 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 if (codec_dai->driver->ops->hw_free)
771 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773 if (cpu_dai->driver->ops->hw_free)
774 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775
776 mutex_unlock(&pcm_mutex);
777 return 0;
778}
779
780static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
781{
782 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 struct snd_soc_platform *platform = rtd->platform;
784 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
785 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 int ret;
787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788 if (codec_dai->driver->ops->trigger) {
789 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 if (ret < 0)
791 return ret;
792 }
793
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 if (platform->driver->ops->trigger) {
795 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796 if (ret < 0)
797 return ret;
798 }
799
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 if (cpu_dai->driver->ops->trigger) {
801 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 if (ret < 0)
803 return ret;
804 }
805 return 0;
806}
807
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200808/*
809 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200810 * If cpu_dai, codec_dai, platform driver has the delay callback, than
811 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200812 */
813static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
814{
815 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000816 struct snd_soc_platform *platform = rtd->platform;
817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
818 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200819 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200820 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200821 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200822
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 if (platform->driver->ops->pointer)
824 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 if (cpu_dai->driver->ops->delay)
827 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200828
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 if (codec_dai->driver->ops->delay)
830 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200831
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000832 if (platform->driver->delay)
833 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200834
835 runtime->delay = delay;
836
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200837 return offset;
838}
839
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200840/* ASoC PCM operations */
841static struct snd_pcm_ops soc_pcm_ops = {
842 .open = soc_pcm_open,
843 .close = soc_codec_close,
844 .hw_params = soc_pcm_hw_params,
845 .hw_free = soc_pcm_hw_free,
846 .prepare = soc_pcm_prepare,
847 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200848 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849};
850
851#ifdef CONFIG_PM
852/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100853static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854{
Mark Brown416356f2009-06-30 19:05:15 +0100855 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857 int i;
858
Daniel Macke3509ff2009-06-03 17:44:49 +0200859 /* If the initialization of this soc device failed, there is no codec
860 * associated with it. Just bail out in this case.
861 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000862 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200863 return 0;
864
Andy Green6ed25972008-06-13 16:24:05 +0100865 /* Due to the resume being scheduled into a workqueue we could
866 * suspend before that's finished - wait for it to complete.
867 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 snd_power_lock(card->snd_card);
869 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
870 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100871
872 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100874
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000876 for (i = 0; i < card->num_rtd; i++) {
877 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
878 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100879
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100881 continue;
882
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000883 if (drv->ops->digital_mute && dai->playback_active)
884 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885 }
886
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100887 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 for (i = 0; i < card->num_rtd; i++) {
889 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 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100893 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100894
Mark Brown87506542008-11-18 20:50:34 +0000895 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100896 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 for (i = 0; i < card->num_rtd; i++) {
899 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
900 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100901
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000902 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100903 continue;
904
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
906 cpu_dai->driver->suspend(cpu_dai);
907 if (platform->driver->suspend && !platform->suspended) {
908 platform->driver->suspend(cpu_dai);
909 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100910 }
911 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 /* close any waiting streams and save state */
914 for (i = 0; i < card->num_rtd; i++) {
915 run_delayed_work(&card->rtd[i].delayed_work);
916 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
917 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919 for (i = 0; i < card->num_rtd; i++) {
920 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
921
922 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100923 continue;
924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925 if (driver->playback.stream_name != NULL)
926 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
927 SND_SOC_DAPM_STREAM_SUSPEND);
928
929 if (driver->capture.stream_name != NULL)
930 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
931 SND_SOC_DAPM_STREAM_SUSPEND);
932 }
933
934 /* suspend all CODECs */
935 for (i = 0; i < card->num_rtd; i++) {
936 struct snd_soc_codec *codec = card->rtd[i].codec;
937 /* If there are paths active then the CODEC will be held with
938 * bias _ON and should not be suspended. */
939 if (!codec->suspended && codec->driver->suspend) {
940 switch (codec->bias_level) {
941 case SND_SOC_BIAS_STANDBY:
942 case SND_SOC_BIAS_OFF:
943 codec->driver->suspend(codec, PMSG_SUSPEND);
944 codec->suspended = 1;
945 break;
946 default:
947 dev_dbg(codec->dev, "CODEC is on over suspend\n");
948 break;
949 }
950 }
951 }
952
953 for (i = 0; i < card->num_rtd; i++) {
954 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
955
956 if (card->rtd[i].dai_link->ignore_suspend)
957 continue;
958
959 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
960 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200961 }
962
Mark Brown87506542008-11-18 20:50:34 +0000963 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100964 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200965
966 return 0;
967}
968
Andy Green6ed25972008-06-13 16:24:05 +0100969/* deferred resume work, so resume can complete before we finished
970 * setting our codec back up, which can be very slow on I2C
971 */
972static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200973{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 struct snd_soc_card *card =
975 container_of(work, struct snd_soc_card, deferred_resume_work);
976 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200977 int i;
978
Andy Green6ed25972008-06-13 16:24:05 +0100979 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
980 * so userspace apps are blocked from touching us
981 */
982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100984
Mark Brown99497882010-05-07 20:24:05 +0100985 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100987
Mark Brown87506542008-11-18 20:50:34 +0000988 if (card->resume_pre)
989 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 /* resume AC97 DAIs */
992 for (i = 0; i < card->num_rtd; i++) {
993 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100994
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100996 continue;
997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
999 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000 }
1001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 for (i = 0; i < card->num_rtd; i++) {
1003 struct snd_soc_codec *codec = card->rtd[i].codec;
1004 /* If the CODEC was idle over suspend then it will have been
1005 * left with bias OFF or STANDBY and suspended so we must now
1006 * resume. Otherwise the suspend was suppressed.
1007 */
1008 if (codec->driver->resume && codec->suspended) {
1009 switch (codec->bias_level) {
1010 case SND_SOC_BIAS_STANDBY:
1011 case SND_SOC_BIAS_OFF:
1012 codec->driver->resume(codec);
1013 codec->suspended = 0;
1014 break;
1015 default:
1016 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1017 break;
1018 }
Mark Brown1547aba2010-05-07 21:11:40 +01001019 }
1020 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 for (i = 0; i < card->num_rtd; i++) {
1023 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001026 continue;
1027
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 if (driver->playback.stream_name != NULL)
1029 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001031
1032 if (driver->capture.stream_name != NULL)
1033 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001034 SND_SOC_DAPM_STREAM_RESUME);
1035 }
1036
Mark Brown3ff3f642008-05-19 12:32:25 +02001037 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1040 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001041
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001043 continue;
1044
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 if (drv->ops->digital_mute && dai->playback_active)
1046 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001047 }
1048
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 for (i = 0; i < card->num_rtd; i++) {
1050 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1051 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001052
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001053 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001054 continue;
1055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1057 cpu_dai->driver->resume(cpu_dai);
1058 if (platform->driver->resume && platform->suspended) {
1059 platform->driver->resume(cpu_dai);
1060 platform->suspended = 0;
1061 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001062 }
1063
Mark Brown87506542008-11-18 20:50:34 +00001064 if (card->resume_post)
1065 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001066
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001067 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001068
1069 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001070 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001071}
1072
1073/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001074static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001075{
Mark Brown416356f2009-06-30 19:05:15 +01001076 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001077 struct snd_soc_card *card = platform_get_drvdata(pdev);
1078 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001079
Mark Brown64ab9ba2009-03-31 11:27:03 +01001080 /* AC97 devices might have other drivers hanging off them so
1081 * need to resume immediately. Other drivers don't have that
1082 * problem and may take a substantial amount of time to resume
1083 * due to I/O costs and anti-pop so handle them out of line.
1084 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001085 for (i = 0; i < card->num_rtd; i++) {
1086 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1087 if (cpu_dai->driver->ac97_control) {
1088 dev_dbg(dev, "Resuming AC97 immediately\n");
1089 soc_resume_deferred(&card->deferred_resume_work);
1090 } else {
1091 dev_dbg(dev, "Scheduling resume work\n");
1092 if (!schedule_work(&card->deferred_resume_work))
1093 dev_err(dev, "resume work item may be lost\n");
1094 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001095 }
Andy Green6ed25972008-06-13 16:24:05 +01001096
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001097 return 0;
1098}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001099#else
1100#define soc_suspend NULL
1101#define soc_resume NULL
1102#endif
1103
Barry Song02a06d32009-10-16 18:13:38 +08001104static struct snd_soc_dai_ops null_dai_ops = {
1105};
1106
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001108{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001109 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1110 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001111 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001112 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001115 if (rtd->complete)
1116 return 1;
1117 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001118
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001119 /* do we already have the CPU DAI for this link ? */
1120 if (rtd->cpu_dai) {
1121 goto find_codec;
1122 }
1123 /* no, then find CPU DAI from registered DAIs*/
1124 list_for_each_entry(cpu_dai, &dai_list, list) {
1125 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1126
1127 if (!try_module_get(cpu_dai->dev->driver->owner))
1128 return -ENODEV;
1129
1130 rtd->cpu_dai = cpu_dai;
1131 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001132 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001133 }
1134 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1135 dai_link->cpu_dai_name);
1136
1137find_codec:
1138 /* do we already have the CODEC for this link ? */
1139 if (rtd->codec) {
1140 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001141 }
1142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001143 /* no, then find CODEC from registered CODECs*/
1144 list_for_each_entry(codec, &codec_list, list) {
1145 if (!strcmp(codec->name, dai_link->codec_name)) {
1146 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148 if (!try_module_get(codec->dev->driver->owner))
1149 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001150
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1152 list_for_each_entry(codec_dai, &dai_list, list) {
1153 if (codec->dev == codec_dai->dev &&
1154 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1155 rtd->codec_dai = codec_dai;
1156 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001157 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001158 }
1159 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1160 dai_link->codec_dai_name);
1161
1162 goto find_platform;
1163 }
1164 }
1165 dev_dbg(card->dev, "CODEC %s not registered\n",
1166 dai_link->codec_name);
1167
1168find_platform:
1169 /* do we already have the CODEC DAI for this link ? */
1170 if (rtd->platform) {
1171 goto out;
1172 }
1173 /* no, then find CPU DAI from registered DAIs*/
1174 list_for_each_entry(platform, &platform_list, list) {
1175 if (!strcmp(platform->name, dai_link->platform_name)) {
1176
1177 if (!try_module_get(platform->dev->driver->owner))
1178 return -ENODEV;
1179
1180 rtd->platform = platform;
1181 goto out;
1182 }
1183 }
1184
1185 dev_dbg(card->dev, "platform %s not registered\n",
1186 dai_link->platform_name);
1187 return 0;
1188
1189out:
1190 /* mark rtd as complete if we found all 4 of our client devices */
1191 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1192 rtd->complete = 1;
1193 card->num_rtd++;
1194 }
1195 return 1;
1196}
1197
1198static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1199{
1200 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1201 struct snd_soc_codec *codec = rtd->codec;
1202 struct snd_soc_platform *platform = rtd->platform;
1203 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1204 int err;
1205
1206 /* unregister the rtd device */
1207 if (rtd->dev_registered) {
1208 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1209 device_unregister(&rtd->dev);
1210 rtd->dev_registered = 0;
1211 }
1212
1213 /* remove the CODEC DAI */
1214 if (codec_dai && codec_dai->probed) {
1215 if (codec_dai->driver->remove) {
1216 err = codec_dai->driver->remove(codec_dai);
1217 if (err < 0)
1218 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1219 }
1220 codec_dai->probed = 0;
1221 list_del(&codec_dai->card_list);
1222 }
1223
1224 /* remove the platform */
1225 if (platform && platform->probed) {
1226 if (platform->driver->remove) {
1227 err = platform->driver->remove(platform);
1228 if (err < 0)
1229 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1230 }
1231 platform->probed = 0;
1232 list_del(&platform->card_list);
1233 module_put(platform->dev->driver->owner);
1234 }
1235
1236 /* remove the CODEC */
1237 if (codec && codec->probed) {
1238 if (codec->driver->remove) {
1239 err = codec->driver->remove(codec);
1240 if (err < 0)
1241 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1242 }
1243
1244 /* Make sure all DAPM widgets are freed */
1245 snd_soc_dapm_free(codec);
1246
1247 soc_cleanup_codec_debugfs(codec);
1248 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1249 codec->probed = 0;
1250 list_del(&codec->card_list);
1251 module_put(codec->dev->driver->owner);
1252 }
1253
1254 /* remove the cpu_dai */
1255 if (cpu_dai && cpu_dai->probed) {
1256 if (cpu_dai->driver->remove) {
1257 err = cpu_dai->driver->remove(cpu_dai);
1258 if (err < 0)
1259 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1260 }
1261 cpu_dai->probed = 0;
1262 list_del(&cpu_dai->card_list);
1263 module_put(cpu_dai->dev->driver->owner);
1264 }
1265}
1266
1267static void rtd_release(struct device *dev) {}
1268
1269static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1270{
1271 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1272 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1273 struct snd_soc_codec *codec = rtd->codec;
1274 struct snd_soc_platform *platform = rtd->platform;
1275 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1276 int ret;
1277
1278 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1279
1280 /* config components */
1281 codec_dai->codec = codec;
1282 codec->card = card;
1283 cpu_dai->platform = platform;
1284 rtd->card = card;
1285 rtd->dev.parent = card->dev;
1286 codec_dai->card = card;
1287 cpu_dai->card = card;
1288
1289 /* set default power off timeout */
1290 rtd->pmdown_time = pmdown_time;
1291
1292 /* probe the cpu_dai */
1293 if (!cpu_dai->probed) {
1294 if (cpu_dai->driver->probe) {
1295 ret = cpu_dai->driver->probe(cpu_dai);
1296 if (ret < 0) {
1297 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1298 cpu_dai->name);
1299 return ret;
1300 }
1301 }
1302 cpu_dai->probed = 1;
1303 /* mark cpu_dai as probed and add to card cpu_dai list */
1304 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1305 }
1306
1307 /* probe the CODEC */
1308 if (!codec->probed) {
1309 if (codec->driver->probe) {
1310 ret = codec->driver->probe(codec);
1311 if (ret < 0) {
1312 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1313 codec->name);
1314 return ret;
1315 }
1316 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001317
1318 soc_init_codec_debugfs(codec);
1319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001320 /* mark codec as probed and add to card codec list */
1321 codec->probed = 1;
1322 list_add(&codec->card_list, &card->codec_dev_list);
1323 }
1324
1325 /* probe the platform */
1326 if (!platform->probed) {
1327 if (platform->driver->probe) {
1328 ret = platform->driver->probe(platform);
1329 if (ret < 0) {
1330 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1331 platform->name);
1332 return ret;
1333 }
1334 }
1335 /* mark platform as probed and add to card platform list */
1336 platform->probed = 1;
1337 list_add(&platform->card_list, &card->platform_dev_list);
1338 }
1339
1340 /* probe the CODEC DAI */
1341 if (!codec_dai->probed) {
1342 if (codec_dai->driver->probe) {
1343 ret = codec_dai->driver->probe(codec_dai);
1344 if (ret < 0) {
1345 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1346 codec_dai->name);
1347 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001348 }
1349 }
1350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001351 /* mark cpu_dai as probed and add to card cpu_dai list */
1352 codec_dai->probed = 1;
1353 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001354 }
1355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001356 /* DAPM dai link stream work */
1357 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1358
1359 /* now that all clients have probed, initialise the DAI link */
1360 if (dai_link->init) {
1361 ret = dai_link->init(rtd);
1362 if (ret < 0) {
1363 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1364 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001365 }
1366 }
1367
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001368 /* Make sure all DAPM widgets are instantiated */
1369 snd_soc_dapm_new_widgets(codec);
1370 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001371
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372 /* register the rtd device */
1373 rtd->dev.init_name = rtd->dai_link->stream_name;
1374 rtd->dev.release = rtd_release;
1375 rtd->dev.init_name = dai_link->name;
1376 ret = device_register(&rtd->dev);
1377 if (ret < 0) {
1378 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1379 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001380 }
1381
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001382 rtd->dev_registered = 1;
1383 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1384 if (ret < 0)
1385 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1386
1387 /* add DAPM sysfs entries for this codec */
1388 ret = snd_soc_dapm_sys_add(&rtd->dev);
1389 if (ret < 0)
1390 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1391
1392 /* add codec sysfs entries */
1393 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1394 if (ret < 0)
1395 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1396
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 /* create the pcm */
1398 ret = soc_new_pcm(rtd, num);
1399 if (ret < 0) {
1400 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1401 return ret;
1402 }
1403
1404 /* add platform data for AC97 devices */
1405 if (rtd->codec_dai->driver->ac97_control)
1406 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1407
1408 return 0;
1409}
1410
1411#ifdef CONFIG_SND_SOC_AC97_BUS
1412static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1413{
1414 int ret;
1415
1416 /* Only instantiate AC97 if not already done by the adaptor
1417 * for the generic AC97 subsystem.
1418 */
1419 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1420
1421 ret = soc_ac97_dev_register(rtd->codec);
1422 if (ret < 0) {
1423 printk(KERN_ERR "asoc: AC97 device register failed\n");
1424 return ret;
1425 }
1426
1427 rtd->codec->ac97_registered = 1;
1428 }
1429 return 0;
1430}
1431
1432static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1433{
1434 if (codec->ac97_registered) {
1435 soc_ac97_dev_unregister(codec);
1436 codec->ac97_registered = 0;
1437 }
1438}
1439#endif
1440
1441static void snd_soc_instantiate_card(struct snd_soc_card *card)
1442{
1443 struct platform_device *pdev = to_platform_device(card->dev);
1444 int ret, i;
1445
1446 mutex_lock(&card->mutex);
1447
1448 if (card->instantiated) {
1449 mutex_unlock(&card->mutex);
1450 return;
1451 }
1452
1453 /* bind DAIs */
1454 for (i = 0; i < card->num_links; i++)
1455 soc_bind_dai_link(card, i);
1456
1457 /* bind completed ? */
1458 if (card->num_rtd != card->num_links) {
1459 mutex_unlock(&card->mutex);
1460 return;
1461 }
1462
1463 /* card bind complete so register a sound card */
1464 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1465 card->owner, 0, &card->snd_card);
1466 if (ret < 0) {
1467 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1468 card->name);
1469 mutex_unlock(&card->mutex);
1470 return;
1471 }
1472 card->snd_card->dev = card->dev;
1473
Randy Dunlap1301a962008-06-17 19:19:34 +01001474#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001475 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001476 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001477#endif
Andy Green6ed25972008-06-13 16:24:05 +01001478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001479 /* initialise the sound card only once */
1480 if (card->probe) {
1481 ret = card->probe(pdev);
1482 if (ret < 0)
1483 goto card_probe_error;
1484 }
1485
Mark Brownfe3e78e2009-11-03 22:13:13 +00001486 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 ret = soc_probe_dai_link(card, i);
1488 if (ret < 0) {
1489 printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name);
1490 goto probe_dai_err;
1491 }
1492 }
1493
1494 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1495 "%s", card->name);
1496 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1497 "%s", card->name);
1498
1499 ret = snd_card_register(card->snd_card);
1500 if (ret < 0) {
1501 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1502 goto probe_dai_err;
1503 }
1504
1505#ifdef CONFIG_SND_SOC_AC97_BUS
1506 /* register any AC97 codecs */
1507 for (i = 0; i < card->num_rtd; i++) {
1508 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001509 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1511 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001512 }
1513 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001514#endif
1515
Mark Brown435c5e22008-12-04 15:32:53 +00001516 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001518 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001519
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520probe_dai_err:
1521 for (i = 0; i < card->num_links; i++)
1522 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001525 if (card->remove)
1526 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527
1528 snd_card_free(card->snd_card);
1529
1530 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001531}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001532
Mark Brown435c5e22008-12-04 15:32:53 +00001533/*
1534 * Attempt to initialise any uninitalised cards. Must be called with
1535 * client_mutex.
1536 */
1537static void snd_soc_instantiate_cards(void)
1538{
1539 struct snd_soc_card *card;
1540 list_for_each_entry(card, &card_list, list)
1541 snd_soc_instantiate_card(card);
1542}
1543
1544/* probes a new socdev */
1545static int soc_probe(struct platform_device *pdev)
1546{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001548 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001549
1550 /* Bodge while we unpick instantiation */
1551 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001552 INIT_LIST_HEAD(&card->dai_dev_list);
1553 INIT_LIST_HEAD(&card->codec_dev_list);
1554 INIT_LIST_HEAD(&card->platform_dev_list);
1555
Mark Brown435c5e22008-12-04 15:32:53 +00001556 ret = snd_soc_register_card(card);
1557 if (ret != 0) {
1558 dev_err(&pdev->dev, "Failed to register card\n");
1559 return ret;
1560 }
1561
1562 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001563}
1564
1565/* removes a socdev */
1566static int soc_remove(struct platform_device *pdev)
1567{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001568 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001569 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001571 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 /* make sure any delayed work runs */
1574 for (i = 0; i < card->num_rtd; i++) {
1575 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1576 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001577 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001579 /* remove and free each DAI */
1580 for (i = 0; i < card->num_rtd; i++)
1581 soc_remove_dai_link(card, i);
1582
1583 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001584 if (card->remove)
1585 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001586
1587 kfree(card->rtd);
1588 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001589 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001590 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001591 return 0;
1592}
1593
Mark Brown416356f2009-06-30 19:05:15 +01001594static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001595{
Mark Brown416356f2009-06-30 19:05:15 +01001596 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001597 struct snd_soc_card *card = platform_get_drvdata(pdev);
1598 int i;
Mark Brown51737472009-06-22 13:16:51 +01001599
1600 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001601 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001602
1603 /* Flush out pmdown_time work - we actually do want to run it
1604 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 for (i = 0; i < card->num_rtd; i++) {
1606 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1607 run_delayed_work(&rtd->delayed_work);
1608 }
Mark Brown51737472009-06-22 13:16:51 +01001609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001611
1612 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001613}
1614
Alexey Dobriyan47145212009-12-14 18:00:08 -08001615static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001616 .suspend = soc_suspend,
1617 .resume = soc_resume,
1618 .poweroff = soc_poweroff,
1619};
1620
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001621/* ASoC platform driver */
1622static struct platform_driver soc_driver = {
1623 .driver = {
1624 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001625 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001626 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627 },
1628 .probe = soc_probe,
1629 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630};
1631
1632/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 struct snd_soc_codec *codec = rtd->codec;
1636 struct snd_soc_platform *platform = rtd->platform;
1637 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1638 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001639 struct snd_pcm *pcm;
1640 char new_name[64];
1641 int ret = 0, playback = 0, capture = 0;
1642
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001643 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001644 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001645 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001646
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001650 capture = 1;
1651
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1653 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1654 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001655 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001657 return ret;
1658 }
1659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001661 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1663 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1664 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1665 soc_pcm_ops.copy = platform->driver->ops->copy;
1666 soc_pcm_ops.silence = platform->driver->ops->silence;
1667 soc_pcm_ops.ack = platform->driver->ops->ack;
1668 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001669
1670 if (playback)
1671 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1672
1673 if (capture)
1674 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677 if (ret < 0) {
1678 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679 return ret;
1680 }
1681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001682 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1684 cpu_dai->name);
1685 return ret;
1686}
1687
Mark Brown096e49d2009-07-05 15:12:22 +01001688/**
1689 * snd_soc_codec_volatile_register: Report if a register is volatile.
1690 *
1691 * @codec: CODEC to query.
1692 * @reg: Register to query.
1693 *
1694 * Boolean function indiciating if a CODEC register is volatile.
1695 */
1696int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1697{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 if (codec->driver->volatile_register)
1699 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001700 else
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1704
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705/**
1706 * snd_soc_new_ac97_codec - initailise AC97 device
1707 * @codec: audio codec
1708 * @ops: AC97 bus operations
1709 * @num: AC97 codec number
1710 *
1711 * Initialises AC97 codec resources for use by ad-hoc devices only.
1712 */
1713int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1714 struct snd_ac97_bus_ops *ops, int num)
1715{
1716 mutex_lock(&codec->mutex);
1717
1718 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1719 if (codec->ac97 == NULL) {
1720 mutex_unlock(&codec->mutex);
1721 return -ENOMEM;
1722 }
1723
1724 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1725 if (codec->ac97->bus == NULL) {
1726 kfree(codec->ac97);
1727 codec->ac97 = NULL;
1728 mutex_unlock(&codec->mutex);
1729 return -ENOMEM;
1730 }
1731
1732 codec->ac97->bus->ops = ops;
1733 codec->ac97->num = num;
1734 mutex_unlock(&codec->mutex);
1735 return 0;
1736}
1737EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1738
1739/**
1740 * snd_soc_free_ac97_codec - free AC97 codec device
1741 * @codec: audio codec
1742 *
1743 * Frees AC97 codec device resources.
1744 */
1745void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1746{
1747 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001748#ifdef CONFIG_SND_SOC_AC97_BUS
1749 soc_unregister_ac97_dai_link(codec);
1750#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001751 kfree(codec->ac97->bus);
1752 kfree(codec->ac97);
1753 codec->ac97 = NULL;
1754 mutex_unlock(&codec->mutex);
1755}
1756EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1757
1758/**
1759 * snd_soc_update_bits - update codec register bits
1760 * @codec: audio codec
1761 * @reg: codec register
1762 * @mask: register mask
1763 * @value: new value
1764 *
1765 * Writes new register value.
1766 *
1767 * Returns 1 for change else 0.
1768 */
1769int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001770 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771{
1772 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001773 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001774
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775 old = snd_soc_read(codec, reg);
1776 new = (old & ~mask) | value;
1777 change = old != new;
1778 if (change)
1779 snd_soc_write(codec, reg, new);
1780
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001781 return change;
1782}
1783EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1784
1785/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001786 * snd_soc_update_bits_locked - update codec register bits
1787 * @codec: audio codec
1788 * @reg: codec register
1789 * @mask: register mask
1790 * @value: new value
1791 *
1792 * Writes new register value, and takes the codec mutex.
1793 *
1794 * Returns 1 for change else 0.
1795 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001796int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1797 unsigned short reg, unsigned int mask,
1798 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001799{
1800 int change;
1801
1802 mutex_lock(&codec->mutex);
1803 change = snd_soc_update_bits(codec, reg, mask, value);
1804 mutex_unlock(&codec->mutex);
1805
1806 return change;
1807}
Mark Browndd1b3d52009-12-04 14:22:03 +00001808EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001809
1810/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811 * snd_soc_test_bits - test register for change
1812 * @codec: audio codec
1813 * @reg: codec register
1814 * @mask: register mask
1815 * @value: new value
1816 *
1817 * Tests a register with a new value and checks if the new value is
1818 * different from the old value.
1819 *
1820 * Returns 1 for change else 0.
1821 */
1822int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001823 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001824{
1825 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001826 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001827
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828 old = snd_soc_read(codec, reg);
1829 new = (old & ~mask) | value;
1830 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001831
1832 return change;
1833}
1834EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1835
1836/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1838 * @substream: the pcm substream
1839 * @hw: the hardware parameters
1840 *
1841 * Sets the substream runtime hardware parameters.
1842 */
1843int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1844 const struct snd_pcm_hardware *hw)
1845{
1846 struct snd_pcm_runtime *runtime = substream->runtime;
1847 runtime->hw.info = hw->info;
1848 runtime->hw.formats = hw->formats;
1849 runtime->hw.period_bytes_min = hw->period_bytes_min;
1850 runtime->hw.period_bytes_max = hw->period_bytes_max;
1851 runtime->hw.periods_min = hw->periods_min;
1852 runtime->hw.periods_max = hw->periods_max;
1853 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1854 runtime->hw.fifo_size = hw->fifo_size;
1855 return 0;
1856}
1857EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1858
1859/**
1860 * snd_soc_cnew - create new control
1861 * @_template: control template
1862 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001863 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 *
1865 * Create a new mixer control from a template control.
1866 *
1867 * Returns 0 for success, else error.
1868 */
1869struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1870 void *data, char *long_name)
1871{
1872 struct snd_kcontrol_new template;
1873
1874 memcpy(&template, _template, sizeof(template));
1875 if (long_name)
1876 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001877 template.index = 0;
1878
1879 return snd_ctl_new1(&template, data);
1880}
1881EXPORT_SYMBOL_GPL(snd_soc_cnew);
1882
1883/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001884 * snd_soc_add_controls - add an array of controls to a codec.
1885 * Convienience function to add a list of controls. Many codecs were
1886 * duplicating this code.
1887 *
1888 * @codec: codec to add controls to
1889 * @controls: array of controls to add
1890 * @num_controls: number of elements in the array
1891 *
1892 * Return 0 for success, else error.
1893 */
1894int snd_soc_add_controls(struct snd_soc_codec *codec,
1895 const struct snd_kcontrol_new *controls, int num_controls)
1896{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001897 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001898 int err, i;
1899
1900 for (i = 0; i < num_controls; i++) {
1901 const struct snd_kcontrol_new *control = &controls[i];
1902 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1903 if (err < 0) {
1904 dev_err(codec->dev, "%s: Failed to add %s\n",
1905 codec->name, control->name);
1906 return err;
1907 }
1908 }
1909
1910 return 0;
1911}
1912EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1913
1914/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001915 * snd_soc_info_enum_double - enumerated double mixer info callback
1916 * @kcontrol: mixer control
1917 * @uinfo: control element information
1918 *
1919 * Callback to provide information about a double enumerated
1920 * mixer control.
1921 *
1922 * Returns 0 for success.
1923 */
1924int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_info *uinfo)
1926{
1927 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1928
1929 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1930 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001931 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001933 if (uinfo->value.enumerated.item > e->max - 1)
1934 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935 strcpy(uinfo->value.enumerated.name,
1936 e->texts[uinfo->value.enumerated.item]);
1937 return 0;
1938}
1939EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1940
1941/**
1942 * snd_soc_get_enum_double - enumerated double mixer get callback
1943 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001944 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945 *
1946 * Callback to get the value of a double enumerated mixer.
1947 *
1948 * Returns 0 for success.
1949 */
1950int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1951 struct snd_ctl_elem_value *ucontrol)
1952{
1953 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1954 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001955 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001956
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001957 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001958 ;
1959 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001960 ucontrol->value.enumerated.item[0]
1961 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001962 if (e->shift_l != e->shift_r)
1963 ucontrol->value.enumerated.item[1] =
1964 (val >> e->shift_r) & (bitmask - 1);
1965
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1969
1970/**
1971 * snd_soc_put_enum_double - enumerated double mixer put callback
1972 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001973 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974 *
1975 * Callback to set the value of a double enumerated mixer.
1976 *
1977 * Returns 0 for success.
1978 */
1979int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1983 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001984 unsigned int val;
1985 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001987 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001989 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990 return -EINVAL;
1991 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1992 mask = (bitmask - 1) << e->shift_l;
1993 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001994 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995 return -EINVAL;
1996 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1997 mask |= (bitmask - 1) << e->shift_r;
1998 }
1999
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002000 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001}
2002EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2003
2004/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002005 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2006 * @kcontrol: mixer control
2007 * @ucontrol: control element information
2008 *
2009 * Callback to get the value of a double semi enumerated mixer.
2010 *
2011 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2012 * used for handling bitfield coded enumeration for example.
2013 *
2014 * Returns 0 for success.
2015 */
2016int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
2018{
2019 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002020 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002021 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002022
2023 reg_val = snd_soc_read(codec, e->reg);
2024 val = (reg_val >> e->shift_l) & e->mask;
2025 for (mux = 0; mux < e->max; mux++) {
2026 if (val == e->values[mux])
2027 break;
2028 }
2029 ucontrol->value.enumerated.item[0] = mux;
2030 if (e->shift_l != e->shift_r) {
2031 val = (reg_val >> e->shift_r) & e->mask;
2032 for (mux = 0; mux < e->max; mux++) {
2033 if (val == e->values[mux])
2034 break;
2035 }
2036 ucontrol->value.enumerated.item[1] = mux;
2037 }
2038
2039 return 0;
2040}
2041EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2042
2043/**
2044 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2045 * @kcontrol: mixer control
2046 * @ucontrol: control element information
2047 *
2048 * Callback to set the value of a double semi enumerated mixer.
2049 *
2050 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2051 * used for handling bitfield coded enumeration for example.
2052 *
2053 * Returns 0 for success.
2054 */
2055int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002059 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002060 unsigned int val;
2061 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002062
2063 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2064 return -EINVAL;
2065 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2066 mask = e->mask << e->shift_l;
2067 if (e->shift_l != e->shift_r) {
2068 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2069 return -EINVAL;
2070 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2071 mask |= e->mask << e->shift_r;
2072 }
2073
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002074 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002075}
2076EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2077
2078/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2080 * @kcontrol: mixer control
2081 * @uinfo: control element information
2082 *
2083 * Callback to provide information about an external enumerated
2084 * single mixer.
2085 *
2086 * Returns 0 for success.
2087 */
2088int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *uinfo)
2090{
2091 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2092
2093 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2094 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002095 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002097 if (uinfo->value.enumerated.item > e->max - 1)
2098 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 strcpy(uinfo->value.enumerated.name,
2100 e->texts[uinfo->value.enumerated.item]);
2101 return 0;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2104
2105/**
2106 * snd_soc_info_volsw_ext - external single mixer info callback
2107 * @kcontrol: mixer control
2108 * @uinfo: control element information
2109 *
2110 * Callback to provide information about a single external mixer control.
2111 *
2112 * Returns 0 for success.
2113 */
2114int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_info *uinfo)
2116{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002117 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002118
Mark Brownfd5dfad2009-04-15 21:37:46 +01002119 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002120 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2121 else
2122 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2123
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002124 uinfo->count = 1;
2125 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002126 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002127 return 0;
2128}
2129EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2130
2131/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002132 * snd_soc_info_volsw - single mixer info callback
2133 * @kcontrol: mixer control
2134 * @uinfo: control element information
2135 *
2136 * Callback to provide information about a single mixer control.
2137 *
2138 * Returns 0 for success.
2139 */
2140int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_info *uinfo)
2142{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002143 struct soc_mixer_control *mc =
2144 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002145 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002146 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002147 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002149 if (!mc->platform_max)
2150 mc->platform_max = mc->max;
2151 platform_max = mc->platform_max;
2152
2153 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002154 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2155 else
2156 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2157
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158 uinfo->count = shift == rshift ? 1 : 2;
2159 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002160 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2164
2165/**
2166 * snd_soc_get_volsw - single mixer get callback
2167 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002168 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002169 *
2170 * Callback to get the value of a single mixer control.
2171 *
2172 * Returns 0 for success.
2173 */
2174int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002177 struct soc_mixer_control *mc =
2178 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002180 unsigned int reg = mc->reg;
2181 unsigned int shift = mc->shift;
2182 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002183 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002184 unsigned int mask = (1 << fls(max)) - 1;
2185 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002186
2187 ucontrol->value.integer.value[0] =
2188 (snd_soc_read(codec, reg) >> shift) & mask;
2189 if (shift != rshift)
2190 ucontrol->value.integer.value[1] =
2191 (snd_soc_read(codec, reg) >> rshift) & mask;
2192 if (invert) {
2193 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002194 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195 if (shift != rshift)
2196 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002197 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198 }
2199
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2203
2204/**
2205 * snd_soc_put_volsw - single mixer put callback
2206 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002207 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208 *
2209 * Callback to set the value of a single mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002216 struct soc_mixer_control *mc =
2217 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002219 unsigned int reg = mc->reg;
2220 unsigned int shift = mc->shift;
2221 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002222 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002223 unsigned int mask = (1 << fls(max)) - 1;
2224 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002225 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226
2227 val = (ucontrol->value.integer.value[0] & mask);
2228 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002229 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230 val_mask = mask << shift;
2231 val = val << shift;
2232 if (shift != rshift) {
2233 val2 = (ucontrol->value.integer.value[1] & mask);
2234 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002235 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236 val_mask |= mask << rshift;
2237 val |= val2 << rshift;
2238 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002239 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240}
2241EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2242
2243/**
2244 * snd_soc_info_volsw_2r - double mixer info callback
2245 * @kcontrol: mixer control
2246 * @uinfo: control element information
2247 *
2248 * Callback to provide information about a double mixer control that
2249 * spans 2 codec registers.
2250 *
2251 * Returns 0 for success.
2252 */
2253int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_info *uinfo)
2255{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002256 struct soc_mixer_control *mc =
2257 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002258 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002260 if (!mc->platform_max)
2261 mc->platform_max = mc->max;
2262 platform_max = mc->platform_max;
2263
2264 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002265 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2266 else
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002269 uinfo->count = 2;
2270 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002271 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272 return 0;
2273}
2274EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2275
2276/**
2277 * snd_soc_get_volsw_2r - double mixer get callback
2278 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002279 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 *
2281 * Callback to get the value of a double mixer control that spans 2 registers.
2282 *
2283 * Returns 0 for success.
2284 */
2285int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002288 struct soc_mixer_control *mc =
2289 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002291 unsigned int reg = mc->reg;
2292 unsigned int reg2 = mc->rreg;
2293 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002294 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002295 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002296 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297
2298 ucontrol->value.integer.value[0] =
2299 (snd_soc_read(codec, reg) >> shift) & mask;
2300 ucontrol->value.integer.value[1] =
2301 (snd_soc_read(codec, reg2) >> shift) & mask;
2302 if (invert) {
2303 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002304 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002306 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307 }
2308
2309 return 0;
2310}
2311EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2312
2313/**
2314 * snd_soc_put_volsw_2r - double mixer set callback
2315 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002316 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 *
2318 * Callback to set the value of a double mixer control that spans 2 registers.
2319 *
2320 * Returns 0 for success.
2321 */
2322int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002325 struct soc_mixer_control *mc =
2326 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002327 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002328 unsigned int reg = mc->reg;
2329 unsigned int reg2 = mc->rreg;
2330 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002331 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002332 unsigned int mask = (1 << fls(max)) - 1;
2333 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002335 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336
2337 val_mask = mask << shift;
2338 val = (ucontrol->value.integer.value[0] & mask);
2339 val2 = (ucontrol->value.integer.value[1] & mask);
2340
2341 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002342 val = max - val;
2343 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002344 }
2345
2346 val = val << shift;
2347 val2 = val2 << shift;
2348
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002349 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002350 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351 return err;
2352
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002353 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 return err;
2355}
2356EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2357
Mark Browne13ac2e2008-05-28 17:58:05 +01002358/**
2359 * snd_soc_info_volsw_s8 - signed mixer info callback
2360 * @kcontrol: mixer control
2361 * @uinfo: control element information
2362 *
2363 * Callback to provide information about a signed mixer control.
2364 *
2365 * Returns 0 for success.
2366 */
2367int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2369{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002370 struct soc_mixer_control *mc =
2371 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002372 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002373 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002374
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002375 if (!mc->platform_max)
2376 mc->platform_max = mc->max;
2377 platform_max = mc->platform_max;
2378
Mark Browne13ac2e2008-05-28 17:58:05 +01002379 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2380 uinfo->count = 2;
2381 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002382 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002383 return 0;
2384}
2385EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2386
2387/**
2388 * snd_soc_get_volsw_s8 - signed mixer get callback
2389 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002390 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002391 *
2392 * Callback to get the value of a signed mixer control.
2393 *
2394 * Returns 0 for success.
2395 */
2396int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2398{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002399 struct soc_mixer_control *mc =
2400 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002401 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002402 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002403 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002404 int val = snd_soc_read(codec, reg);
2405
2406 ucontrol->value.integer.value[0] =
2407 ((signed char)(val & 0xff))-min;
2408 ucontrol->value.integer.value[1] =
2409 ((signed char)((val >> 8) & 0xff))-min;
2410 return 0;
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2413
2414/**
2415 * snd_soc_put_volsw_sgn - signed mixer put callback
2416 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002417 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002418 *
2419 * Callback to set the value of a signed mixer control.
2420 *
2421 * Returns 0 for success.
2422 */
2423int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2425{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002426 struct soc_mixer_control *mc =
2427 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002429 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002430 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002431 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002432
2433 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2434 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2435
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002436 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002437}
2438EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2439
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002440/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002441 * snd_soc_limit_volume - Set new limit to an existing volume control.
2442 *
2443 * @codec: where to look for the control
2444 * @name: Name of the control
2445 * @max: new maximum limit
2446 *
2447 * Return 0 for success, else error.
2448 */
2449int snd_soc_limit_volume(struct snd_soc_codec *codec,
2450 const char *name, int max)
2451{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002452 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002453 struct snd_kcontrol *kctl;
2454 struct soc_mixer_control *mc;
2455 int found = 0;
2456 int ret = -EINVAL;
2457
2458 /* Sanity check for name and max */
2459 if (unlikely(!name || max <= 0))
2460 return -EINVAL;
2461
2462 list_for_each_entry(kctl, &card->controls, list) {
2463 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (found) {
2469 mc = (struct soc_mixer_control *)kctl->private_value;
2470 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002471 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002472 ret = 0;
2473 }
2474 }
2475 return ret;
2476}
2477EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2478
2479/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002480 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2481 * mixer info callback
2482 * @kcontrol: mixer control
2483 * @uinfo: control element information
2484 *
2485 * Returns 0 for success.
2486 */
2487int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_info *uinfo)
2489{
2490 struct soc_mixer_control *mc =
2491 (struct soc_mixer_control *)kcontrol->private_value;
2492 int max = mc->max;
2493 int min = mc->min;
2494
2495 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2496 uinfo->count = 2;
2497 uinfo->value.integer.min = 0;
2498 uinfo->value.integer.max = max-min;
2499
2500 return 0;
2501}
2502EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2503
2504/**
2505 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2506 * mixer get callback
2507 * @kcontrol: mixer control
2508 * @uinfo: control element information
2509 *
2510 * Returns 0 for success.
2511 */
2512int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_value *ucontrol)
2514{
2515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
2517 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2518 unsigned int mask = (1<<mc->shift)-1;
2519 int min = mc->min;
2520 int val = snd_soc_read(codec, mc->reg) & mask;
2521 int valr = snd_soc_read(codec, mc->rreg) & mask;
2522
Stuart Longland20630c72010-06-18 12:56:10 +10002523 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2524 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002525 return 0;
2526}
2527EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2528
2529/**
2530 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2531 * mixer put callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2534 *
2535 * Returns 0 for success.
2536 */
2537int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2538 struct snd_ctl_elem_value *ucontrol)
2539{
2540 struct soc_mixer_control *mc =
2541 (struct soc_mixer_control *)kcontrol->private_value;
2542 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2543 unsigned int mask = (1<<mc->shift)-1;
2544 int min = mc->min;
2545 int ret;
2546 unsigned int val, valr, oval, ovalr;
2547
2548 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2549 val &= mask;
2550 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2551 valr &= mask;
2552
2553 oval = snd_soc_read(codec, mc->reg) & mask;
2554 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2555
2556 ret = 0;
2557 if (oval != val) {
2558 ret = snd_soc_write(codec, mc->reg, val);
2559 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002560 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002561 }
2562 if (ovalr != valr) {
2563 ret = snd_soc_write(codec, mc->rreg, valr);
2564 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002565 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002566 }
2567
2568 return 0;
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2571
2572/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002573 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2574 * @dai: DAI
2575 * @clk_id: DAI specific clock ID
2576 * @freq: new clock frequency in Hz
2577 * @dir: new clock direction - input/output.
2578 *
2579 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2580 */
2581int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2582 unsigned int freq, int dir)
2583{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002584 if (dai->driver && dai->driver->ops->set_sysclk)
2585 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002586 else
2587 return -EINVAL;
2588}
2589EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2590
2591/**
2592 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2593 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002594 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002595 * @div: new clock divisor.
2596 *
2597 * Configures the clock dividers. This is used to derive the best DAI bit and
2598 * frame clocks from the system or master clock. It's best to set the DAI bit
2599 * and frame clocks as low as possible to save system power.
2600 */
2601int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2602 int div_id, int div)
2603{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002604 if (dai->driver && dai->driver->ops->set_clkdiv)
2605 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002606 else
2607 return -EINVAL;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2610
2611/**
2612 * snd_soc_dai_set_pll - configure DAI PLL.
2613 * @dai: DAI
2614 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002615 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002616 * @freq_in: PLL input clock frequency in Hz
2617 * @freq_out: requested PLL output clock frequency in Hz
2618 *
2619 * Configures and enables PLL to generate output clock based on input clock.
2620 */
Mark Brown85488032009-09-05 18:52:16 +01002621int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2622 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002623{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002624 if (dai->driver && dai->driver->ops->set_pll)
2625 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002626 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002627 else
2628 return -EINVAL;
2629}
2630EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2631
2632/**
2633 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2634 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002635 * @fmt: SND_SOC_DAIFMT_ format value.
2636 *
2637 * Configures the DAI hardware format and clocking.
2638 */
2639int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2640{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002641 if (dai->driver && dai->driver->ops->set_fmt)
2642 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002643 else
2644 return -EINVAL;
2645}
2646EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2647
2648/**
2649 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2650 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002651 * @tx_mask: bitmask representing active TX slots.
2652 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002653 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002654 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002655 *
2656 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2657 * specific.
2658 */
2659int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002660 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002661{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002662 if (dai->driver && dai->driver->ops->set_tdm_slot)
2663 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002664 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002665 else
2666 return -EINVAL;
2667}
2668EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2669
2670/**
Barry Song472df3c2009-09-12 01:16:29 +08002671 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2672 * @dai: DAI
2673 * @tx_num: how many TX channels
2674 * @tx_slot: pointer to an array which imply the TX slot number channel
2675 * 0~num-1 uses
2676 * @rx_num: how many RX channels
2677 * @rx_slot: pointer to an array which imply the RX slot number channel
2678 * 0~num-1 uses
2679 *
2680 * configure the relationship between channel number and TDM slot number.
2681 */
2682int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2683 unsigned int tx_num, unsigned int *tx_slot,
2684 unsigned int rx_num, unsigned int *rx_slot)
2685{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002686 if (dai->driver && dai->driver->ops->set_channel_map)
2687 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002688 rx_num, rx_slot);
2689 else
2690 return -EINVAL;
2691}
2692EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2693
2694/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002695 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2696 * @dai: DAI
2697 * @tristate: tristate enable
2698 *
2699 * Tristates the DAI so that others can use it.
2700 */
2701int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2702{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002703 if (dai->driver && dai->driver->ops->set_tristate)
2704 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002705 else
2706 return -EINVAL;
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2709
2710/**
2711 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2712 * @dai: DAI
2713 * @mute: mute enable
2714 *
2715 * Mutes the DAI DAC.
2716 */
2717int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2718{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002719 if (dai->driver && dai->driver->ops->digital_mute)
2720 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002721 else
2722 return -EINVAL;
2723}
2724EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2725
Mark Brownc5af3a22008-11-28 13:29:45 +00002726/**
2727 * snd_soc_register_card - Register a card with the ASoC core
2728 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002729 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002730 *
2731 * Note that currently this is an internal only function: it will be
2732 * exposed to machine drivers after further backporting of ASoC v2
2733 * registration APIs.
2734 */
2735static int snd_soc_register_card(struct snd_soc_card *card)
2736{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002737 int i;
2738
Mark Brownc5af3a22008-11-28 13:29:45 +00002739 if (!card->name || !card->dev)
2740 return -EINVAL;
2741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002742 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2743 GFP_KERNEL);
2744 if (card->rtd == NULL)
2745 return -ENOMEM;
2746
2747 for (i = 0; i < card->num_links; i++)
2748 card->rtd[i].dai_link = &card->dai_link[i];
2749
Mark Brownc5af3a22008-11-28 13:29:45 +00002750 INIT_LIST_HEAD(&card->list);
2751 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002752 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002753
2754 mutex_lock(&client_mutex);
2755 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002756 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002757 mutex_unlock(&client_mutex);
2758
2759 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2760
2761 return 0;
2762}
2763
2764/**
2765 * snd_soc_unregister_card - Unregister a card with the ASoC core
2766 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002767 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002768 *
2769 * Note that currently this is an internal only function: it will be
2770 * exposed to machine drivers after further backporting of ASoC v2
2771 * registration APIs.
2772 */
2773static int snd_soc_unregister_card(struct snd_soc_card *card)
2774{
2775 mutex_lock(&client_mutex);
2776 list_del(&card->list);
2777 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002778 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2779
2780 return 0;
2781}
2782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002783/*
2784 * Simplify DAI link configuration by removing ".-1" from device names
2785 * and sanitizing names.
2786 */
2787static inline char *fmt_single_name(struct device *dev, int *id)
2788{
2789 char *found, name[NAME_SIZE];
2790 int id1, id2;
2791
2792 if (dev_name(dev) == NULL)
2793 return NULL;
2794
2795 strncpy(name, dev_name(dev), NAME_SIZE);
2796
2797 /* are we a "%s.%d" name (platform and SPI components) */
2798 found = strstr(name, dev->driver->name);
2799 if (found) {
2800 /* get ID */
2801 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2802
2803 /* discard ID from name if ID == -1 */
2804 if (*id == -1)
2805 found[strlen(dev->driver->name)] = '\0';
2806 }
2807
2808 } else {
2809 /* I2C component devices are named "bus-addr" */
2810 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2811 char tmp[NAME_SIZE];
2812
2813 /* create unique ID number from I2C addr and bus */
2814 *id = ((id1 && 0xffff) << 16) + id2;
2815
2816 /* sanitize component name for DAI link creation */
2817 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2818 strncpy(name, tmp, NAME_SIZE);
2819 } else
2820 *id = 0;
2821 }
2822
2823 return kstrdup(name, GFP_KERNEL);
2824}
2825
2826/*
2827 * Simplify DAI link naming for single devices with multiple DAIs by removing
2828 * any ".-1" and using the DAI name (instead of device name).
2829 */
2830static inline char *fmt_multiple_name(struct device *dev,
2831 struct snd_soc_dai_driver *dai_drv)
2832{
2833 if (dai_drv->name == NULL) {
2834 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2835 dev_name(dev));
2836 return NULL;
2837 }
2838
2839 return kstrdup(dai_drv->name, GFP_KERNEL);
2840}
2841
Mark Brown91151712008-11-30 23:31:24 +00002842/**
2843 * snd_soc_register_dai - Register a DAI with the ASoC core
2844 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002845 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002846 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002847int snd_soc_register_dai(struct device *dev,
2848 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002849{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002850 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002852 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002853
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002854 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2855 if (dai == NULL)
2856 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002857
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002858 /* create DAI component name */
2859 dai->name = fmt_single_name(dev, &dai->id);
2860 if (dai->name == NULL) {
2861 kfree(dai);
2862 return -ENOMEM;
2863 }
2864
2865 dai->dev = dev;
2866 dai->driver = dai_drv;
2867 if (!dai->driver->ops)
2868 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002869
2870 mutex_lock(&client_mutex);
2871 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002872 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002873 mutex_unlock(&client_mutex);
2874
2875 pr_debug("Registered DAI '%s'\n", dai->name);
2876
2877 return 0;
2878}
2879EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2880
2881/**
2882 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2883 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002884 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002885 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002886void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002887{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002888 struct snd_soc_dai *dai;
2889
2890 list_for_each_entry(dai, &dai_list, list) {
2891 if (dev == dai->dev)
2892 goto found;
2893 }
2894 return;
2895
2896found:
Mark Brown91151712008-11-30 23:31:24 +00002897 mutex_lock(&client_mutex);
2898 list_del(&dai->list);
2899 mutex_unlock(&client_mutex);
2900
2901 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002902 kfree(dai->name);
2903 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002904}
2905EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2906
2907/**
2908 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2909 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002910 * @dai: Array of DAIs to register
2911 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002912 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002913int snd_soc_register_dais(struct device *dev,
2914 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002915{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002916 struct snd_soc_dai *dai;
2917 int i, ret = 0;
2918
2919 dev_dbg(dev, "dai register %s #%d\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002920
2921 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002922
2923 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2924 if (dai == NULL)
2925 return -ENOMEM;
2926
2927 /* create DAI component name */
2928 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2929 if (dai->name == NULL) {
2930 kfree(dai);
2931 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00002932 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002933 }
2934
2935 dai->dev = dev;
2936 dai->id = i;
2937 dai->driver = &dai_drv[i];
2938 if (!dai->driver->ops)
2939 dai->driver->ops = &null_dai_ops;
2940
2941 mutex_lock(&client_mutex);
2942 list_add(&dai->list, &dai_list);
2943 mutex_unlock(&client_mutex);
2944
2945 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002946 }
2947
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002948 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002949 return 0;
2950
2951err:
2952 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002954
2955 return ret;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2958
2959/**
2960 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2961 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002962 * @dai: Array of DAIs to unregister
2963 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002964 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002965void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002966{
2967 int i;
2968
2969 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002970 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002971}
2972EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2973
Mark Brown12a48a8c2008-12-03 19:40:30 +00002974/**
2975 * snd_soc_register_platform - Register a platform with the ASoC core
2976 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002977 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002978 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002979int snd_soc_register_platform(struct device *dev,
2980 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002981{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002982 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002984 dev_dbg(dev, "platform register %s\n", dev_name(dev));
2985
2986 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2987 if (platform == NULL)
2988 return -ENOMEM;
2989
2990 /* create platform component name */
2991 platform->name = fmt_single_name(dev, &platform->id);
2992 if (platform->name == NULL) {
2993 kfree(platform);
2994 return -ENOMEM;
2995 }
2996
2997 platform->dev = dev;
2998 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002999
3000 mutex_lock(&client_mutex);
3001 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003002 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003003 mutex_unlock(&client_mutex);
3004
3005 pr_debug("Registered platform '%s'\n", platform->name);
3006
3007 return 0;
3008}
3009EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3010
3011/**
3012 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3013 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003014 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003015 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003016void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003017{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018 struct snd_soc_platform *platform;
3019
3020 list_for_each_entry(platform, &platform_list, list) {
3021 if (dev == platform->dev)
3022 goto found;
3023 }
3024 return;
3025
3026found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003027 mutex_lock(&client_mutex);
3028 list_del(&platform->list);
3029 mutex_unlock(&client_mutex);
3030
3031 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003032 kfree(platform->name);
3033 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003034}
3035EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3036
Mark Brown151ab222009-05-09 16:22:58 +01003037static u64 codec_format_map[] = {
3038 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3039 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3040 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3041 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3042 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3043 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3044 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3045 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3046 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3047 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3048 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3049 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3050 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3051 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3052 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3053 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3054};
3055
3056/* Fix up the DAI formats for endianness: codecs don't actually see
3057 * the endianness of the data but we're using the CPU format
3058 * definitions which do need to include endianness so we ensure that
3059 * codec DAIs always have both big and little endian variants set.
3060 */
3061static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3062{
3063 int i;
3064
3065 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3066 if (stream->formats & codec_format_map[i])
3067 stream->formats |= codec_format_map[i];
3068}
3069
Mark Brown0d0cf002008-12-10 14:32:45 +00003070/**
3071 * snd_soc_register_codec - Register a codec with the ASoC core
3072 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003073 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003074 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003075int snd_soc_register_codec(struct device *dev,
3076 struct snd_soc_codec_driver *codec_drv,
3077 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003078{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079 struct snd_soc_codec *codec;
3080 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003081
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003082 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003083
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3085 if (codec == NULL)
3086 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003087
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088 /* create CODEC component name */
3089 codec->name = fmt_single_name(dev, &codec->id);
3090 if (codec->name == NULL) {
3091 kfree(codec);
3092 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003093 }
3094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003095 /* allocate CODEC register cache */
3096 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3097
3098 if (codec_drv->reg_cache_default)
3099 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3100 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3101 else
3102 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3103 codec_drv->reg_word_size, GFP_KERNEL);
3104
3105 if (codec->reg_cache == NULL) {
3106 kfree(codec->name);
3107 kfree(codec);
3108 return -ENOMEM;
3109 }
3110 }
3111
3112 codec->dev = dev;
3113 codec->driver = codec_drv;
3114 codec->bias_level = SND_SOC_BIAS_OFF;
3115 codec->num_dai = num_dai;
3116 mutex_init(&codec->mutex);
3117 INIT_LIST_HEAD(&codec->dapm_widgets);
3118 INIT_LIST_HEAD(&codec->dapm_paths);
3119
3120 for (i = 0; i < num_dai; i++) {
3121 fixup_codec_formats(&dai_drv[i].playback);
3122 fixup_codec_formats(&dai_drv[i].capture);
3123 }
3124
3125 /* register DAIs */
3126 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3127 if (ret < 0)
3128 goto error;
3129
Mark Brown0d0cf002008-12-10 14:32:45 +00003130 mutex_lock(&client_mutex);
3131 list_add(&codec->list, &codec_list);
3132 snd_soc_instantiate_cards();
3133 mutex_unlock(&client_mutex);
3134
3135 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003136 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003137
3138error:
3139 for (i--; i >= 0; i--)
3140 snd_soc_unregister_dai(dev);
3141
3142 if (codec->reg_cache)
3143 kfree(codec->reg_cache);
3144 kfree(codec->name);
3145 kfree(codec);
3146 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003147}
3148EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3149
3150/**
3151 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3152 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003153 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003154 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003156{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003157 struct snd_soc_codec *codec;
3158 int i;
3159
3160 list_for_each_entry(codec, &codec_list, list) {
3161 if (dev == codec->dev)
3162 goto found;
3163 }
3164 return;
3165
3166found:
3167 for (i = 0; i < codec->num_dai; i++)
3168 snd_soc_unregister_dai(dev);
3169
Mark Brown0d0cf002008-12-10 14:32:45 +00003170 mutex_lock(&client_mutex);
3171 list_del(&codec->list);
3172 mutex_unlock(&client_mutex);
3173
3174 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003175
3176 if (codec->reg_cache)
3177 kfree(codec->reg_cache);
3178 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003179}
3180EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3181
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003182static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003183{
Mark Brown384c89e2008-12-03 17:34:03 +00003184#ifdef CONFIG_DEBUG_FS
3185 debugfs_root = debugfs_create_dir("asoc", NULL);
3186 if (IS_ERR(debugfs_root) || !debugfs_root) {
3187 printk(KERN_WARNING
3188 "ASoC: Failed to create debugfs directory\n");
3189 debugfs_root = NULL;
3190 }
3191#endif
3192
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003193 return platform_driver_register(&soc_driver);
3194}
3195
Mark Brown7d8c16a2008-11-30 22:11:24 +00003196static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003197{
Mark Brown384c89e2008-12-03 17:34:03 +00003198#ifdef CONFIG_DEBUG_FS
3199 debugfs_remove_recursive(debugfs_root);
3200#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003201 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003202}
3203
3204module_init(snd_soc_init);
3205module_exit(snd_soc_exit);
3206
3207/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003208MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003209MODULE_DESCRIPTION("ALSA SoC Core");
3210MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003211MODULE_ALIAS("platform:soc-audio");