blob: 94a2edf9cfd5e10c46253e9a3e94ac5f3ec4b06d [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
Axel Lin708fafb2010-08-27 10:34:27 +0800252 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 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
Mark Brownc3c5a192010-09-15 18:15:14 +0100273static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
274 size_t count, loff_t *ppos)
275{
276 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
277 ssize_t ret = 0;
278 struct snd_soc_codec *codec;
279
280 if (!buf)
281 return -ENOMEM;
282
283 list_for_each_entry(codec, &codec_list, list)
284 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
285 codec->name);
286
287 if (ret >= 0)
288 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
289
290 kfree(buf);
291
292 return ret;
293}
294
295static const struct file_operations codec_list_fops = {
296 .read = codec_list_read_file,
297 .llseek = default_llseek,/* read accesses f_pos */
298};
299
Mark Brown2624d5f2009-11-03 21:56:13 +0000300#else
301
302static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
303{
304}
305
306static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
307{
308}
309#endif
310
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200311#ifdef CONFIG_SND_SOC_AC97_BUS
312/* unregister ac97 codec */
313static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
314{
315 if (codec->ac97->dev.bus)
316 device_unregister(&codec->ac97->dev);
317 return 0;
318}
319
320/* stop no dev release warning */
321static void soc_ac97_device_release(struct device *dev){}
322
323/* register ac97 codec to bus */
324static int soc_ac97_dev_register(struct snd_soc_codec *codec)
325{
326 int err;
327
328 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100329 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200330 codec->ac97->dev.release = soc_ac97_device_release;
331
Kay Sieversbb072bf2008-11-02 03:50:35 +0100332 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200334 err = device_register(&codec->ac97->dev);
335 if (err < 0) {
336 snd_printk(KERN_ERR "Can't register ac97 bus\n");
337 codec->ac97->dev.bus = NULL;
338 return err;
339 }
340 return 0;
341}
342#endif
343
Mark Brown06f409d2009-04-07 18:10:13 +0100344static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
345{
346 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000347 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
348 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100349 int ret;
350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000351 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
352 rtd->dai_link->symmetric_rates) {
353 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
354 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100355
356 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
357 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000358 rtd->rate,
359 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100360 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000361 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100362 "Unable to apply rate symmetry constraint: %d\n", ret);
363 return ret;
364 }
365 }
366
367 return 0;
368}
369
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200370/*
371 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
372 * then initialized and any private data can be allocated. This also calls
373 * startup for the cpu DAI, platform, machine and codec DAI.
374 */
375static int soc_pcm_open(struct snd_pcm_substream *substream)
376{
377 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200378 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000379 struct snd_soc_platform *platform = rtd->platform;
380 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
381 struct snd_soc_dai *codec_dai = rtd->codec_dai;
382 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
383 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200384 int ret = 0;
385
386 mutex_lock(&pcm_mutex);
387
388 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000389 if (cpu_dai->driver->ops->startup) {
390 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200391 if (ret < 0) {
392 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100393 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200394 goto out;
395 }
396 }
397
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000398 if (platform->driver->ops->open) {
399 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200400 if (ret < 0) {
401 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
402 goto platform_err;
403 }
404 }
405
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000406 if (codec_dai->driver->ops->startup) {
407 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100408 if (ret < 0) {
409 printk(KERN_ERR "asoc: can't open codec %s\n",
410 codec_dai->name);
411 goto codec_dai_err;
412 }
413 }
414
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
416 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200419 goto machine_err;
420 }
421 }
422
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200423 /* Check that the codec and cpu DAI's are compatible */
424 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
425 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000426 max(codec_dai_drv->playback.rate_min,
427 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200428 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000429 min(codec_dai_drv->playback.rate_max,
430 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200431 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 max(codec_dai_drv->playback.channels_min,
433 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200434 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435 min(codec_dai_drv->playback.channels_max,
436 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100437 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100439 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
441 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900442 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000443 runtime->hw.rates |= cpu_dai_drv->playback.rates;
444 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900445 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447 } else {
448 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 max(codec_dai_drv->capture.rate_min,
450 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 min(codec_dai_drv->capture.rate_max,
453 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 max(codec_dai_drv->capture.channels_min,
456 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 min(codec_dai_drv->capture.channels_max,
459 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100460 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100462 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
464 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900465 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 runtime->hw.rates |= cpu_dai_drv->capture.rates;
467 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900468 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200470 }
471
472 snd_pcm_limit_hw_rates(runtime);
473 if (!runtime->hw.rates) {
474 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100475 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900476 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 }
478 if (!runtime->hw.formats) {
479 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100480 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900481 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200482 }
483 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
484 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900486 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 }
488
Mark Brown06f409d2009-04-07 18:10:13 +0100489 /* Symmetry only applies if we've already got an active stream. */
490 if (cpu_dai->active || codec_dai->active) {
491 ret = soc_pcm_apply_symmetry(substream);
492 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900493 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100494 }
495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 pr_debug("asoc: %s <-> %s info:\n",
497 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100498 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
499 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
500 runtime->hw.channels_max);
501 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
502 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
Jassi Brar14dc5732010-02-26 09:12:32 +0900504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 cpu_dai->playback_active++;
506 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900507 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 cpu_dai->capture_active++;
509 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900510 }
511 cpu_dai->active++;
512 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 mutex_unlock(&pcm_mutex);
515 return 0;
516
Jassi Brarbb1c0472010-02-25 11:24:53 +0900517config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
519 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520
Jassi Brarbb1c0472010-02-25 11:24:53 +0900521machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 if (codec_dai->driver->ops->shutdown)
523 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900524
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100525codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 if (platform->driver->ops->close)
527 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528
529platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 if (cpu_dai->driver->ops->shutdown)
531 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200532out:
533 mutex_unlock(&pcm_mutex);
534 return ret;
535}
536
537/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200538 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 * This is to ensure there are no pops or clicks in between any music tracks
540 * due to DAPM power cycling.
541 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100542static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 struct snd_soc_pcm_runtime *rtd =
545 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
546 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547
548 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
551 codec_dai->driver->playback.stream_name,
552 codec_dai->playback_active ? "active" : "inactive",
553 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 /* are we waiting on this codec DAI stream */
556 if (codec_dai->pop_wait == 1) {
557 codec_dai->pop_wait = 0;
558 snd_soc_dapm_stream_event(rtd,
559 codec_dai->driver->playback.stream_name,
560 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563 mutex_unlock(&pcm_mutex);
564}
565
566/*
567 * Called by ALSA when a PCM substream is closed. Private data can be
568 * freed here. The cpu DAI, codec DAI, machine and platform are also
569 * shutdown.
570 */
571static int soc_codec_close(struct snd_pcm_substream *substream)
572{
573 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 struct snd_soc_platform *platform = rtd->platform;
575 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
576 struct snd_soc_dai *codec_dai = rtd->codec_dai;
577 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578
579 mutex_lock(&pcm_mutex);
580
Jassi Brar14dc5732010-02-26 09:12:32 +0900581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582 cpu_dai->playback_active--;
583 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900584 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 cpu_dai->capture_active--;
586 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900588
589 cpu_dai->active--;
590 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591 codec->active--;
592
Mark Brown6010b2d2008-09-06 18:33:24 +0100593 /* Muting the DAC suppresses artifacts caused during digital
594 * shutdown, for example from stopping clocks.
595 */
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
597 snd_soc_dai_digital_mute(codec_dai, 1);
598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 if (cpu_dai->driver->ops->shutdown)
600 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (codec_dai->driver->ops->shutdown)
603 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
606 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 if (platform->driver->ops->close)
609 platform->driver->ops->close(substream);
610 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
612 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
613 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100614 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 schedule_delayed_work(&rtd->delayed_work,
616 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617 } else {
618 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 snd_soc_dapm_stream_event(rtd,
620 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100621 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622 }
623
624 mutex_unlock(&pcm_mutex);
625 return 0;
626}
627
628/*
629 * Called by ALSA when the PCM substream is prepared, can set format, sample
630 * rate, etc. This function is non atomic and can be called multiple times,
631 * it can refer to the runtime info.
632 */
633static int soc_pcm_prepare(struct snd_pcm_substream *substream)
634{
635 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 struct snd_soc_platform *platform = rtd->platform;
637 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
638 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 int ret = 0;
640
641 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100642
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
644 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100645 if (ret < 0) {
646 printk(KERN_ERR "asoc: machine prepare error\n");
647 goto out;
648 }
649 }
650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 if (platform->driver->ops->prepare) {
652 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200653 if (ret < 0) {
654 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200656 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657 }
658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (codec_dai->driver->ops->prepare) {
660 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200661 if (ret < 0) {
662 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200664 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665 }
666
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 if (cpu_dai->driver->ops->prepare) {
668 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100669 if (ret < 0) {
670 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
671 goto out;
672 }
673 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674
Mark Brownd45f6212008-10-14 13:58:36 +0100675 /* cancel any delayed stream shutdown that is pending */
676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
677 codec_dai->pop_wait) {
678 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100680 }
681
Mark Brown452c5ea2009-05-17 21:41:23 +0100682 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 snd_soc_dapm_stream_event(rtd,
684 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100685 SND_SOC_DAPM_STREAM_START);
686 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 snd_soc_dapm_stream_event(rtd,
688 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100689 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100690
Mark Brown452c5ea2009-05-17 21:41:23 +0100691 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
693out:
694 mutex_unlock(&pcm_mutex);
695 return ret;
696}
697
698/*
699 * Called by ALSA when the hardware params are set by application. This
700 * function can also be called multiple times and can allocate buffers
701 * (using snd_pcm_lib_* ). It's non-atomic.
702 */
703static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
704 struct snd_pcm_hw_params *params)
705{
706 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 struct snd_soc_platform *platform = rtd->platform;
708 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
709 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 int ret = 0;
711
712 mutex_lock(&pcm_mutex);
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
715 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100717 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 goto out;
719 }
720 }
721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 if (codec_dai->driver->ops->hw_params) {
723 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100724 if (ret < 0) {
725 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
726 codec_dai->name);
727 goto codec_err;
728 }
729 }
730
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 if (cpu_dai->driver->ops->hw_params) {
732 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200734 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100735 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736 goto interface_err;
737 }
738 }
739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 if (platform->driver->ops->hw_params) {
741 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200743 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 platform->name);
745 goto platform_err;
746 }
747 }
748
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100750
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751out:
752 mutex_unlock(&pcm_mutex);
753 return ret;
754
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 if (cpu_dai->driver->ops->hw_free)
757 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758
759interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 if (codec_dai->driver->ops->hw_free)
761 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100762
763codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
765 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766
767 mutex_unlock(&pcm_mutex);
768 return ret;
769}
770
771/*
772 * Free's resources allocated by hw_params, can be called multiple times
773 */
774static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
775{
776 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 struct snd_soc_platform *platform = rtd->platform;
778 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
779 struct snd_soc_dai *codec_dai = rtd->codec_dai;
780 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
782 mutex_lock(&pcm_mutex);
783
784 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100785 if (!codec->active)
786 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787
788 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
790 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791
792 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 if (platform->driver->ops->hw_free)
794 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
796 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 if (codec_dai->driver->ops->hw_free)
798 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 if (cpu_dai->driver->ops->hw_free)
801 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802
803 mutex_unlock(&pcm_mutex);
804 return 0;
805}
806
807static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
808{
809 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000810 struct snd_soc_platform *platform = rtd->platform;
811 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
812 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 int ret;
814
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000815 if (codec_dai->driver->ops->trigger) {
816 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817 if (ret < 0)
818 return ret;
819 }
820
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000821 if (platform->driver->ops->trigger) {
822 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200823 if (ret < 0)
824 return ret;
825 }
826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000827 if (cpu_dai->driver->ops->trigger) {
828 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200829 if (ret < 0)
830 return ret;
831 }
832 return 0;
833}
834
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200835/*
836 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200837 * If cpu_dai, codec_dai, platform driver has the delay callback, than
838 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200839 */
840static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
841{
842 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 struct snd_soc_platform *platform = rtd->platform;
844 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
845 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200846 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200847 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200848 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 if (platform->driver->ops->pointer)
851 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853 if (cpu_dai->driver->ops->delay)
854 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200855
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 if (codec_dai->driver->ops->delay)
857 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200858
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 if (platform->driver->delay)
860 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200861
862 runtime->delay = delay;
863
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200864 return offset;
865}
866
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867/* ASoC PCM operations */
868static struct snd_pcm_ops soc_pcm_ops = {
869 .open = soc_pcm_open,
870 .close = soc_codec_close,
871 .hw_params = soc_pcm_hw_params,
872 .hw_free = soc_pcm_hw_free,
873 .prepare = soc_pcm_prepare,
874 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200875 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876};
877
878#ifdef CONFIG_PM
879/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100880static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881{
Mark Brown416356f2009-06-30 19:05:15 +0100882 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000883 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884 int i;
885
Daniel Macke3509ff2009-06-03 17:44:49 +0200886 /* If the initialization of this soc device failed, there is no codec
887 * associated with it. Just bail out in this case.
888 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200890 return 0;
891
Andy Green6ed25972008-06-13 16:24:05 +0100892 /* Due to the resume being scheduled into a workqueue we could
893 * suspend before that's finished - wait for it to complete.
894 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895 snd_power_lock(card->snd_card);
896 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
897 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100898
899 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100901
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903 for (i = 0; i < card->num_rtd; i++) {
904 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
905 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100908 continue;
909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (drv->ops->digital_mute && dai->playback_active)
911 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912 }
913
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100914 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 for (i = 0; i < card->num_rtd; i++) {
916 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100917 continue;
918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100920 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100921
Mark Brown87506542008-11-18 20:50:34 +0000922 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100923 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925 for (i = 0; i < card->num_rtd; i++) {
926 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
927 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100928
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100930 continue;
931
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
933 cpu_dai->driver->suspend(cpu_dai);
934 if (platform->driver->suspend && !platform->suspended) {
935 platform->driver->suspend(cpu_dai);
936 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100937 }
938 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200939
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000940 /* close any waiting streams and save state */
941 for (i = 0; i < card->num_rtd; i++) {
942 run_delayed_work(&card->rtd[i].delayed_work);
943 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
944 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100945
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 for (i = 0; i < card->num_rtd; i++) {
947 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
948
949 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100950 continue;
951
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000952 if (driver->playback.stream_name != NULL)
953 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
954 SND_SOC_DAPM_STREAM_SUSPEND);
955
956 if (driver->capture.stream_name != NULL)
957 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
958 SND_SOC_DAPM_STREAM_SUSPEND);
959 }
960
961 /* suspend all CODECs */
962 for (i = 0; i < card->num_rtd; i++) {
963 struct snd_soc_codec *codec = card->rtd[i].codec;
964 /* If there are paths active then the CODEC will be held with
965 * bias _ON and should not be suspended. */
966 if (!codec->suspended && codec->driver->suspend) {
967 switch (codec->bias_level) {
968 case SND_SOC_BIAS_STANDBY:
969 case SND_SOC_BIAS_OFF:
970 codec->driver->suspend(codec, PMSG_SUSPEND);
971 codec->suspended = 1;
972 break;
973 default:
974 dev_dbg(codec->dev, "CODEC is on over suspend\n");
975 break;
976 }
977 }
978 }
979
980 for (i = 0; i < card->num_rtd; i++) {
981 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
982
983 if (card->rtd[i].dai_link->ignore_suspend)
984 continue;
985
986 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
987 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200988 }
989
Mark Brown87506542008-11-18 20:50:34 +0000990 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100991 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200992
993 return 0;
994}
995
Andy Green6ed25972008-06-13 16:24:05 +0100996/* deferred resume work, so resume can complete before we finished
997 * setting our codec back up, which can be very slow on I2C
998 */
999static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 struct snd_soc_card *card =
1002 container_of(work, struct snd_soc_card, deferred_resume_work);
1003 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001004 int i;
1005
Andy Green6ed25972008-06-13 16:24:05 +01001006 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1007 * so userspace apps are blocked from touching us
1008 */
1009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001011
Mark Brown9949788b2010-05-07 20:24:05 +01001012 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +01001014
Mark Brown87506542008-11-18 20:50:34 +00001015 if (card->resume_pre)
1016 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001017
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001018 /* resume AC97 DAIs */
1019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001023 continue;
1024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1026 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001027 }
1028
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001029 for (i = 0; i < card->num_rtd; i++) {
1030 struct snd_soc_codec *codec = card->rtd[i].codec;
1031 /* If the CODEC was idle over suspend then it will have been
1032 * left with bias OFF or STANDBY and suspended so we must now
1033 * resume. Otherwise the suspend was suppressed.
1034 */
1035 if (codec->driver->resume && codec->suspended) {
1036 switch (codec->bias_level) {
1037 case SND_SOC_BIAS_STANDBY:
1038 case SND_SOC_BIAS_OFF:
1039 codec->driver->resume(codec);
1040 codec->suspended = 0;
1041 break;
1042 default:
1043 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1044 break;
1045 }
Mark Brown1547aba2010-05-07 21:11:40 +01001046 }
1047 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001048
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 for (i = 0; i < card->num_rtd; i++) {
1050 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001051
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001052 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001053 continue;
1054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 if (driver->playback.stream_name != NULL)
1056 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001057 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058
1059 if (driver->capture.stream_name != NULL)
1060 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001061 SND_SOC_DAPM_STREAM_RESUME);
1062 }
1063
Mark Brown3ff3f642008-05-19 12:32:25 +02001064 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 for (i = 0; i < card->num_rtd; i++) {
1066 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1067 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001068
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001069 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001070 continue;
1071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072 if (drv->ops->digital_mute && dai->playback_active)
1073 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001074 }
1075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 for (i = 0; i < card->num_rtd; i++) {
1077 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1078 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001079
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001080 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001081 continue;
1082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1084 cpu_dai->driver->resume(cpu_dai);
1085 if (platform->driver->resume && platform->suspended) {
1086 platform->driver->resume(cpu_dai);
1087 platform->suspended = 0;
1088 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001089 }
1090
Mark Brown87506542008-11-18 20:50:34 +00001091 if (card->resume_post)
1092 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001094 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001095
1096 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001097 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001098}
1099
1100/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001101static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001102{
Mark Brown416356f2009-06-30 19:05:15 +01001103 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001104 struct snd_soc_card *card = platform_get_drvdata(pdev);
1105 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001106
Mark Brown64ab9ba2009-03-31 11:27:03 +01001107 /* AC97 devices might have other drivers hanging off them so
1108 * need to resume immediately. Other drivers don't have that
1109 * problem and may take a substantial amount of time to resume
1110 * due to I/O costs and anti-pop so handle them out of line.
1111 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001112 for (i = 0; i < card->num_rtd; i++) {
1113 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1114 if (cpu_dai->driver->ac97_control) {
1115 dev_dbg(dev, "Resuming AC97 immediately\n");
1116 soc_resume_deferred(&card->deferred_resume_work);
1117 } else {
1118 dev_dbg(dev, "Scheduling resume work\n");
1119 if (!schedule_work(&card->deferred_resume_work))
1120 dev_err(dev, "resume work item may be lost\n");
1121 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001122 }
Andy Green6ed25972008-06-13 16:24:05 +01001123
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001124 return 0;
1125}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001126#else
1127#define soc_suspend NULL
1128#define soc_resume NULL
1129#endif
1130
Barry Song02a06d32009-10-16 18:13:38 +08001131static struct snd_soc_dai_ops null_dai_ops = {
1132};
1133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001135{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1137 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001138 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001139 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001140 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 if (rtd->complete)
1143 return 1;
1144 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 /* do we already have the CPU DAI for this link ? */
1147 if (rtd->cpu_dai) {
1148 goto find_codec;
1149 }
1150 /* no, then find CPU DAI from registered DAIs*/
1151 list_for_each_entry(cpu_dai, &dai_list, list) {
1152 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1153
1154 if (!try_module_get(cpu_dai->dev->driver->owner))
1155 return -ENODEV;
1156
1157 rtd->cpu_dai = cpu_dai;
1158 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001159 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 }
1161 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1162 dai_link->cpu_dai_name);
1163
1164find_codec:
1165 /* do we already have the CODEC for this link ? */
1166 if (rtd->codec) {
1167 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001168 }
1169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 /* no, then find CODEC from registered CODECs*/
1171 list_for_each_entry(codec, &codec_list, list) {
1172 if (!strcmp(codec->name, dai_link->codec_name)) {
1173 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 if (!try_module_get(codec->dev->driver->owner))
1176 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001177
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001178 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1179 list_for_each_entry(codec_dai, &dai_list, list) {
1180 if (codec->dev == codec_dai->dev &&
1181 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1182 rtd->codec_dai = codec_dai;
1183 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001184 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 }
1186 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1187 dai_link->codec_dai_name);
1188
1189 goto find_platform;
1190 }
1191 }
1192 dev_dbg(card->dev, "CODEC %s not registered\n",
1193 dai_link->codec_name);
1194
1195find_platform:
1196 /* do we already have the CODEC DAI for this link ? */
1197 if (rtd->platform) {
1198 goto out;
1199 }
1200 /* no, then find CPU DAI from registered DAIs*/
1201 list_for_each_entry(platform, &platform_list, list) {
1202 if (!strcmp(platform->name, dai_link->platform_name)) {
1203
1204 if (!try_module_get(platform->dev->driver->owner))
1205 return -ENODEV;
1206
1207 rtd->platform = platform;
1208 goto out;
1209 }
1210 }
1211
1212 dev_dbg(card->dev, "platform %s not registered\n",
1213 dai_link->platform_name);
1214 return 0;
1215
1216out:
1217 /* mark rtd as complete if we found all 4 of our client devices */
1218 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1219 rtd->complete = 1;
1220 card->num_rtd++;
1221 }
1222 return 1;
1223}
1224
1225static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1226{
1227 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1228 struct snd_soc_codec *codec = rtd->codec;
1229 struct snd_soc_platform *platform = rtd->platform;
1230 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1231 int err;
1232
1233 /* unregister the rtd device */
1234 if (rtd->dev_registered) {
1235 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1236 device_unregister(&rtd->dev);
1237 rtd->dev_registered = 0;
1238 }
1239
1240 /* remove the CODEC DAI */
1241 if (codec_dai && codec_dai->probed) {
1242 if (codec_dai->driver->remove) {
1243 err = codec_dai->driver->remove(codec_dai);
1244 if (err < 0)
1245 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1246 }
1247 codec_dai->probed = 0;
1248 list_del(&codec_dai->card_list);
1249 }
1250
1251 /* remove the platform */
1252 if (platform && platform->probed) {
1253 if (platform->driver->remove) {
1254 err = platform->driver->remove(platform);
1255 if (err < 0)
1256 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1257 }
1258 platform->probed = 0;
1259 list_del(&platform->card_list);
1260 module_put(platform->dev->driver->owner);
1261 }
1262
1263 /* remove the CODEC */
1264 if (codec && codec->probed) {
1265 if (codec->driver->remove) {
1266 err = codec->driver->remove(codec);
1267 if (err < 0)
1268 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1269 }
1270
1271 /* Make sure all DAPM widgets are freed */
1272 snd_soc_dapm_free(codec);
1273
1274 soc_cleanup_codec_debugfs(codec);
1275 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1276 codec->probed = 0;
1277 list_del(&codec->card_list);
1278 module_put(codec->dev->driver->owner);
1279 }
1280
1281 /* remove the cpu_dai */
1282 if (cpu_dai && cpu_dai->probed) {
1283 if (cpu_dai->driver->remove) {
1284 err = cpu_dai->driver->remove(cpu_dai);
1285 if (err < 0)
1286 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1287 }
1288 cpu_dai->probed = 0;
1289 list_del(&cpu_dai->card_list);
1290 module_put(cpu_dai->dev->driver->owner);
1291 }
1292}
1293
1294static void rtd_release(struct device *dev) {}
1295
1296static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1297{
1298 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1299 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1300 struct snd_soc_codec *codec = rtd->codec;
1301 struct snd_soc_platform *platform = rtd->platform;
1302 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1303 int ret;
1304
1305 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1306
1307 /* config components */
1308 codec_dai->codec = codec;
1309 codec->card = card;
1310 cpu_dai->platform = platform;
1311 rtd->card = card;
1312 rtd->dev.parent = card->dev;
1313 codec_dai->card = card;
1314 cpu_dai->card = card;
1315
1316 /* set default power off timeout */
1317 rtd->pmdown_time = pmdown_time;
1318
1319 /* probe the cpu_dai */
1320 if (!cpu_dai->probed) {
1321 if (cpu_dai->driver->probe) {
1322 ret = cpu_dai->driver->probe(cpu_dai);
1323 if (ret < 0) {
1324 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1325 cpu_dai->name);
1326 return ret;
1327 }
1328 }
1329 cpu_dai->probed = 1;
1330 /* mark cpu_dai as probed and add to card cpu_dai list */
1331 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1332 }
1333
1334 /* probe the CODEC */
1335 if (!codec->probed) {
1336 if (codec->driver->probe) {
1337 ret = codec->driver->probe(codec);
1338 if (ret < 0) {
1339 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1340 codec->name);
1341 return ret;
1342 }
1343 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001344
1345 soc_init_codec_debugfs(codec);
1346
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347 /* mark codec as probed and add to card codec list */
1348 codec->probed = 1;
1349 list_add(&codec->card_list, &card->codec_dev_list);
1350 }
1351
1352 /* probe the platform */
1353 if (!platform->probed) {
1354 if (platform->driver->probe) {
1355 ret = platform->driver->probe(platform);
1356 if (ret < 0) {
1357 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1358 platform->name);
1359 return ret;
1360 }
1361 }
1362 /* mark platform as probed and add to card platform list */
1363 platform->probed = 1;
1364 list_add(&platform->card_list, &card->platform_dev_list);
1365 }
1366
1367 /* probe the CODEC DAI */
1368 if (!codec_dai->probed) {
1369 if (codec_dai->driver->probe) {
1370 ret = codec_dai->driver->probe(codec_dai);
1371 if (ret < 0) {
1372 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1373 codec_dai->name);
1374 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001375 }
1376 }
1377
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 /* mark cpu_dai as probed and add to card cpu_dai list */
1379 codec_dai->probed = 1;
1380 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001381 }
1382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 /* DAPM dai link stream work */
1384 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1385
1386 /* now that all clients have probed, initialise the DAI link */
1387 if (dai_link->init) {
1388 ret = dai_link->init(rtd);
1389 if (ret < 0) {
1390 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1391 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001392 }
1393 }
1394
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 /* Make sure all DAPM widgets are instantiated */
1396 snd_soc_dapm_new_widgets(codec);
1397 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001398
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001399 /* register the rtd device */
1400 rtd->dev.init_name = rtd->dai_link->stream_name;
1401 rtd->dev.release = rtd_release;
1402 rtd->dev.init_name = dai_link->name;
1403 ret = device_register(&rtd->dev);
1404 if (ret < 0) {
1405 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1406 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407 }
1408
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 rtd->dev_registered = 1;
1410 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1411 if (ret < 0)
1412 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1413
1414 /* add DAPM sysfs entries for this codec */
1415 ret = snd_soc_dapm_sys_add(&rtd->dev);
1416 if (ret < 0)
1417 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1418
1419 /* add codec sysfs entries */
1420 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1421 if (ret < 0)
1422 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 /* create the pcm */
1425 ret = soc_new_pcm(rtd, num);
1426 if (ret < 0) {
1427 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1428 return ret;
1429 }
1430
1431 /* add platform data for AC97 devices */
1432 if (rtd->codec_dai->driver->ac97_control)
1433 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1434
1435 return 0;
1436}
1437
1438#ifdef CONFIG_SND_SOC_AC97_BUS
1439static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1440{
1441 int ret;
1442
1443 /* Only instantiate AC97 if not already done by the adaptor
1444 * for the generic AC97 subsystem.
1445 */
1446 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1447
1448 ret = soc_ac97_dev_register(rtd->codec);
1449 if (ret < 0) {
1450 printk(KERN_ERR "asoc: AC97 device register failed\n");
1451 return ret;
1452 }
1453
1454 rtd->codec->ac97_registered = 1;
1455 }
1456 return 0;
1457}
1458
1459static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1460{
1461 if (codec->ac97_registered) {
1462 soc_ac97_dev_unregister(codec);
1463 codec->ac97_registered = 0;
1464 }
1465}
1466#endif
1467
1468static void snd_soc_instantiate_card(struct snd_soc_card *card)
1469{
1470 struct platform_device *pdev = to_platform_device(card->dev);
1471 int ret, i;
1472
1473 mutex_lock(&card->mutex);
1474
1475 if (card->instantiated) {
1476 mutex_unlock(&card->mutex);
1477 return;
1478 }
1479
1480 /* bind DAIs */
1481 for (i = 0; i < card->num_links; i++)
1482 soc_bind_dai_link(card, i);
1483
1484 /* bind completed ? */
1485 if (card->num_rtd != card->num_links) {
1486 mutex_unlock(&card->mutex);
1487 return;
1488 }
1489
1490 /* card bind complete so register a sound card */
1491 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1492 card->owner, 0, &card->snd_card);
1493 if (ret < 0) {
1494 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1495 card->name);
1496 mutex_unlock(&card->mutex);
1497 return;
1498 }
1499 card->snd_card->dev = card->dev;
1500
Randy Dunlap1301a962008-06-17 19:19:34 +01001501#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001502 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001503 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001504#endif
Andy Green6ed25972008-06-13 16:24:05 +01001505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001506 /* initialise the sound card only once */
1507 if (card->probe) {
1508 ret = card->probe(pdev);
1509 if (ret < 0)
1510 goto card_probe_error;
1511 }
1512
Mark Brownfe3e78e2009-11-03 22:13:13 +00001513 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514 ret = soc_probe_dai_link(card, i);
1515 if (ret < 0) {
1516 printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name);
1517 goto probe_dai_err;
1518 }
1519 }
1520
1521 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1522 "%s", card->name);
1523 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1524 "%s", card->name);
1525
1526 ret = snd_card_register(card->snd_card);
1527 if (ret < 0) {
1528 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1529 goto probe_dai_err;
1530 }
1531
1532#ifdef CONFIG_SND_SOC_AC97_BUS
1533 /* register any AC97 codecs */
1534 for (i = 0; i < card->num_rtd; i++) {
1535 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001536 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1538 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001539 }
1540 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001541#endif
1542
Mark Brown435c5e22008-12-04 15:32:53 +00001543 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001544 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001545 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547probe_dai_err:
1548 for (i = 0; i < card->num_links; i++)
1549 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001550
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001551card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001552 if (card->remove)
1553 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554
1555 snd_card_free(card->snd_card);
1556
1557 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001558}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001559
Mark Brown435c5e22008-12-04 15:32:53 +00001560/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001561 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001562 * client_mutex.
1563 */
1564static void snd_soc_instantiate_cards(void)
1565{
1566 struct snd_soc_card *card;
1567 list_for_each_entry(card, &card_list, list)
1568 snd_soc_instantiate_card(card);
1569}
1570
1571/* probes a new socdev */
1572static int soc_probe(struct platform_device *pdev)
1573{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001574 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001575 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001576
1577 /* Bodge while we unpick instantiation */
1578 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001579 INIT_LIST_HEAD(&card->dai_dev_list);
1580 INIT_LIST_HEAD(&card->codec_dev_list);
1581 INIT_LIST_HEAD(&card->platform_dev_list);
1582
Mark Brown435c5e22008-12-04 15:32:53 +00001583 ret = snd_soc_register_card(card);
1584 if (ret != 0) {
1585 dev_err(&pdev->dev, "Failed to register card\n");
1586 return ret;
1587 }
1588
1589 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001590}
1591
1592/* removes a socdev */
1593static int soc_remove(struct platform_device *pdev)
1594{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001596 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001598 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600 /* make sure any delayed work runs */
1601 for (i = 0; i < card->num_rtd; i++) {
1602 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1603 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001604 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001605
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 /* remove and free each DAI */
1607 for (i = 0; i < card->num_rtd; i++)
1608 soc_remove_dai_link(card, i);
1609
1610 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001611 if (card->remove)
1612 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001613
1614 kfree(card->rtd);
1615 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001616 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001617 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001618 return 0;
1619}
1620
Mark Brown416356f2009-06-30 19:05:15 +01001621static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001622{
Mark Brown416356f2009-06-30 19:05:15 +01001623 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624 struct snd_soc_card *card = platform_get_drvdata(pdev);
1625 int i;
Mark Brown51737472009-06-22 13:16:51 +01001626
1627 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001628 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001629
1630 /* Flush out pmdown_time work - we actually do want to run it
1631 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632 for (i = 0; i < card->num_rtd; i++) {
1633 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1634 run_delayed_work(&rtd->delayed_work);
1635 }
Mark Brown51737472009-06-22 13:16:51 +01001636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001638
1639 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001640}
1641
Alexey Dobriyan47145212009-12-14 18:00:08 -08001642static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001643 .suspend = soc_suspend,
1644 .resume = soc_resume,
1645 .poweroff = soc_poweroff,
1646};
1647
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648/* ASoC platform driver */
1649static struct platform_driver soc_driver = {
1650 .driver = {
1651 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001652 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001653 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001654 },
1655 .probe = soc_probe,
1656 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001657};
1658
1659/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001661{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662 struct snd_soc_codec *codec = rtd->codec;
1663 struct snd_soc_platform *platform = rtd->platform;
1664 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001666 struct snd_pcm *pcm;
1667 char new_name[64];
1668 int ret = 0, playback = 0, capture = 0;
1669
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001671 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001672 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001675 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677 capture = 1;
1678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001679 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1680 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1681 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001682 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001683 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001684 return ret;
1685 }
1686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001687 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001688 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001689 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1690 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1691 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1692 soc_pcm_ops.copy = platform->driver->ops->copy;
1693 soc_pcm_ops.silence = platform->driver->ops->silence;
1694 soc_pcm_ops.ack = platform->driver->ops->ack;
1695 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001696
1697 if (playback)
1698 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1699
1700 if (capture)
1701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704 if (ret < 0) {
1705 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706 return ret;
1707 }
1708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1711 cpu_dai->name);
1712 return ret;
1713}
1714
Mark Brown096e49d2009-07-05 15:12:22 +01001715/**
1716 * snd_soc_codec_volatile_register: Report if a register is volatile.
1717 *
1718 * @codec: CODEC to query.
1719 * @reg: Register to query.
1720 *
1721 * Boolean function indiciating if a CODEC register is volatile.
1722 */
1723int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1724{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725 if (codec->driver->volatile_register)
1726 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001727 else
1728 return 0;
1729}
1730EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1731
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001732/**
1733 * snd_soc_new_ac97_codec - initailise AC97 device
1734 * @codec: audio codec
1735 * @ops: AC97 bus operations
1736 * @num: AC97 codec number
1737 *
1738 * Initialises AC97 codec resources for use by ad-hoc devices only.
1739 */
1740int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1741 struct snd_ac97_bus_ops *ops, int num)
1742{
1743 mutex_lock(&codec->mutex);
1744
1745 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1746 if (codec->ac97 == NULL) {
1747 mutex_unlock(&codec->mutex);
1748 return -ENOMEM;
1749 }
1750
1751 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1752 if (codec->ac97->bus == NULL) {
1753 kfree(codec->ac97);
1754 codec->ac97 = NULL;
1755 mutex_unlock(&codec->mutex);
1756 return -ENOMEM;
1757 }
1758
1759 codec->ac97->bus->ops = ops;
1760 codec->ac97->num = num;
1761 mutex_unlock(&codec->mutex);
1762 return 0;
1763}
1764EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1765
1766/**
1767 * snd_soc_free_ac97_codec - free AC97 codec device
1768 * @codec: audio codec
1769 *
1770 * Frees AC97 codec device resources.
1771 */
1772void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1773{
1774 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775#ifdef CONFIG_SND_SOC_AC97_BUS
1776 soc_unregister_ac97_dai_link(codec);
1777#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001778 kfree(codec->ac97->bus);
1779 kfree(codec->ac97);
1780 codec->ac97 = NULL;
1781 mutex_unlock(&codec->mutex);
1782}
1783EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1784
1785/**
1786 * snd_soc_update_bits - 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.
1793 *
1794 * Returns 1 for change else 0.
1795 */
1796int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001797 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001798{
1799 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001800 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001801
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001802 old = snd_soc_read(codec, reg);
1803 new = (old & ~mask) | value;
1804 change = old != new;
1805 if (change)
1806 snd_soc_write(codec, reg, new);
1807
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001808 return change;
1809}
1810EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1811
1812/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001813 * snd_soc_update_bits_locked - update codec register bits
1814 * @codec: audio codec
1815 * @reg: codec register
1816 * @mask: register mask
1817 * @value: new value
1818 *
1819 * Writes new register value, and takes the codec mutex.
1820 *
1821 * Returns 1 for change else 0.
1822 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001823int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1824 unsigned short reg, unsigned int mask,
1825 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001826{
1827 int change;
1828
1829 mutex_lock(&codec->mutex);
1830 change = snd_soc_update_bits(codec, reg, mask, value);
1831 mutex_unlock(&codec->mutex);
1832
1833 return change;
1834}
Mark Browndd1b3d52009-12-04 14:22:03 +00001835EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001836
1837/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838 * snd_soc_test_bits - test register for change
1839 * @codec: audio codec
1840 * @reg: codec register
1841 * @mask: register mask
1842 * @value: new value
1843 *
1844 * Tests a register with a new value and checks if the new value is
1845 * different from the old value.
1846 *
1847 * Returns 1 for change else 0.
1848 */
1849int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001850 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851{
1852 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001853 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001855 old = snd_soc_read(codec, reg);
1856 new = (old & ~mask) | value;
1857 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001858
1859 return change;
1860}
1861EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1862
1863/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1865 * @substream: the pcm substream
1866 * @hw: the hardware parameters
1867 *
1868 * Sets the substream runtime hardware parameters.
1869 */
1870int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1871 const struct snd_pcm_hardware *hw)
1872{
1873 struct snd_pcm_runtime *runtime = substream->runtime;
1874 runtime->hw.info = hw->info;
1875 runtime->hw.formats = hw->formats;
1876 runtime->hw.period_bytes_min = hw->period_bytes_min;
1877 runtime->hw.period_bytes_max = hw->period_bytes_max;
1878 runtime->hw.periods_min = hw->periods_min;
1879 runtime->hw.periods_max = hw->periods_max;
1880 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1881 runtime->hw.fifo_size = hw->fifo_size;
1882 return 0;
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1885
1886/**
1887 * snd_soc_cnew - create new control
1888 * @_template: control template
1889 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001890 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001891 *
1892 * Create a new mixer control from a template control.
1893 *
1894 * Returns 0 for success, else error.
1895 */
1896struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1897 void *data, char *long_name)
1898{
1899 struct snd_kcontrol_new template;
1900
1901 memcpy(&template, _template, sizeof(template));
1902 if (long_name)
1903 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001904 template.index = 0;
1905
1906 return snd_ctl_new1(&template, data);
1907}
1908EXPORT_SYMBOL_GPL(snd_soc_cnew);
1909
1910/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001911 * snd_soc_add_controls - add an array of controls to a codec.
1912 * Convienience function to add a list of controls. Many codecs were
1913 * duplicating this code.
1914 *
1915 * @codec: codec to add controls to
1916 * @controls: array of controls to add
1917 * @num_controls: number of elements in the array
1918 *
1919 * Return 0 for success, else error.
1920 */
1921int snd_soc_add_controls(struct snd_soc_codec *codec,
1922 const struct snd_kcontrol_new *controls, int num_controls)
1923{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001924 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001925 int err, i;
1926
1927 for (i = 0; i < num_controls; i++) {
1928 const struct snd_kcontrol_new *control = &controls[i];
1929 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1930 if (err < 0) {
1931 dev_err(codec->dev, "%s: Failed to add %s\n",
1932 codec->name, control->name);
1933 return err;
1934 }
1935 }
1936
1937 return 0;
1938}
1939EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1940
1941/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942 * snd_soc_info_enum_double - enumerated double mixer info callback
1943 * @kcontrol: mixer control
1944 * @uinfo: control element information
1945 *
1946 * Callback to provide information about a double enumerated
1947 * mixer control.
1948 *
1949 * Returns 0 for success.
1950 */
1951int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_info *uinfo)
1953{
1954 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1955
1956 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1957 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001958 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001959
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001960 if (uinfo->value.enumerated.item > e->max - 1)
1961 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001962 strcpy(uinfo->value.enumerated.name,
1963 e->texts[uinfo->value.enumerated.item]);
1964 return 0;
1965}
1966EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1967
1968/**
1969 * snd_soc_get_enum_double - enumerated double mixer get callback
1970 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001971 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972 *
1973 * Callback to get the value of a double enumerated mixer.
1974 *
1975 * Returns 0 for success.
1976 */
1977int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_value *ucontrol)
1979{
1980 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1981 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001982 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001984 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985 ;
1986 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001987 ucontrol->value.enumerated.item[0]
1988 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001989 if (e->shift_l != e->shift_r)
1990 ucontrol->value.enumerated.item[1] =
1991 (val >> e->shift_r) & (bitmask - 1);
1992
1993 return 0;
1994}
1995EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1996
1997/**
1998 * snd_soc_put_enum_double - enumerated double mixer put callback
1999 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002000 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 *
2002 * Callback to set the value of a double enumerated mixer.
2003 *
2004 * Returns 0 for success.
2005 */
2006int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2007 struct snd_ctl_elem_value *ucontrol)
2008{
2009 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2010 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002011 unsigned int val;
2012 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002013
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002014 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002016 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002017 return -EINVAL;
2018 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2019 mask = (bitmask - 1) << e->shift_l;
2020 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002021 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 return -EINVAL;
2023 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2024 mask |= (bitmask - 1) << e->shift_r;
2025 }
2026
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002027 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028}
2029EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2030
2031/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002032 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2033 * @kcontrol: mixer control
2034 * @ucontrol: control element information
2035 *
2036 * Callback to get the value of a double semi enumerated mixer.
2037 *
2038 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2039 * used for handling bitfield coded enumeration for example.
2040 *
2041 * Returns 0 for success.
2042 */
2043int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol)
2045{
2046 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002047 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002048 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002049
2050 reg_val = snd_soc_read(codec, e->reg);
2051 val = (reg_val >> e->shift_l) & e->mask;
2052 for (mux = 0; mux < e->max; mux++) {
2053 if (val == e->values[mux])
2054 break;
2055 }
2056 ucontrol->value.enumerated.item[0] = mux;
2057 if (e->shift_l != e->shift_r) {
2058 val = (reg_val >> e->shift_r) & e->mask;
2059 for (mux = 0; mux < e->max; mux++) {
2060 if (val == e->values[mux])
2061 break;
2062 }
2063 ucontrol->value.enumerated.item[1] = mux;
2064 }
2065
2066 return 0;
2067}
2068EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2069
2070/**
2071 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2072 * @kcontrol: mixer control
2073 * @ucontrol: control element information
2074 *
2075 * Callback to set the value of a double semi enumerated mixer.
2076 *
2077 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2078 * used for handling bitfield coded enumeration for example.
2079 *
2080 * Returns 0 for success.
2081 */
2082int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_value *ucontrol)
2084{
2085 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002086 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002087 unsigned int val;
2088 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002089
2090 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2091 return -EINVAL;
2092 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2093 mask = e->mask << e->shift_l;
2094 if (e->shift_l != e->shift_r) {
2095 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2096 return -EINVAL;
2097 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2098 mask |= e->mask << e->shift_r;
2099 }
2100
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002101 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002102}
2103EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2104
2105/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2107 * @kcontrol: mixer control
2108 * @uinfo: control element information
2109 *
2110 * Callback to provide information about an external enumerated
2111 * single mixer.
2112 *
2113 * Returns 0 for success.
2114 */
2115int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_info *uinfo)
2117{
2118 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2119
2120 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2121 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002122 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002123
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002124 if (uinfo->value.enumerated.item > e->max - 1)
2125 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126 strcpy(uinfo->value.enumerated.name,
2127 e->texts[uinfo->value.enumerated.item]);
2128 return 0;
2129}
2130EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2131
2132/**
2133 * snd_soc_info_volsw_ext - external single mixer info callback
2134 * @kcontrol: mixer control
2135 * @uinfo: control element information
2136 *
2137 * Callback to provide information about a single external mixer control.
2138 *
2139 * Returns 0 for success.
2140 */
2141int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002144 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145
Mark Brownfd5dfad2009-04-15 21:37:46 +01002146 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002147 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2148 else
2149 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2150
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151 uinfo->count = 1;
2152 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002153 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002154 return 0;
2155}
2156EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2157
2158/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159 * snd_soc_info_volsw - single mixer info callback
2160 * @kcontrol: mixer control
2161 * @uinfo: control element information
2162 *
2163 * Callback to provide information about a single mixer control.
2164 *
2165 * Returns 0 for success.
2166 */
2167int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_info *uinfo)
2169{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002170 struct soc_mixer_control *mc =
2171 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002172 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002173 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002174 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002175
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002176 if (!mc->platform_max)
2177 mc->platform_max = mc->max;
2178 platform_max = mc->platform_max;
2179
2180 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002181 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2182 else
2183 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2184
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185 uinfo->count = shift == rshift ? 1 : 2;
2186 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002187 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002188 return 0;
2189}
2190EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2191
2192/**
2193 * snd_soc_get_volsw - single mixer get callback
2194 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002195 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196 *
2197 * Callback to get the value of a single mixer control.
2198 *
2199 * Returns 0 for success.
2200 */
2201int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2203{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002204 struct soc_mixer_control *mc =
2205 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002207 unsigned int reg = mc->reg;
2208 unsigned int shift = mc->shift;
2209 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002210 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002211 unsigned int mask = (1 << fls(max)) - 1;
2212 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213
2214 ucontrol->value.integer.value[0] =
2215 (snd_soc_read(codec, reg) >> shift) & mask;
2216 if (shift != rshift)
2217 ucontrol->value.integer.value[1] =
2218 (snd_soc_read(codec, reg) >> rshift) & mask;
2219 if (invert) {
2220 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002221 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 if (shift != rshift)
2223 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002224 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002225 }
2226
2227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2230
2231/**
2232 * snd_soc_put_volsw - single mixer put callback
2233 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002234 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 *
2236 * Callback to set the value of a single mixer control.
2237 *
2238 * Returns 0 for success.
2239 */
2240int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002243 struct soc_mixer_control *mc =
2244 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002246 unsigned int reg = mc->reg;
2247 unsigned int shift = mc->shift;
2248 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002249 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002250 unsigned int mask = (1 << fls(max)) - 1;
2251 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002252 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253
2254 val = (ucontrol->value.integer.value[0] & mask);
2255 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002256 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257 val_mask = mask << shift;
2258 val = val << shift;
2259 if (shift != rshift) {
2260 val2 = (ucontrol->value.integer.value[1] & mask);
2261 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002262 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263 val_mask |= mask << rshift;
2264 val |= val2 << rshift;
2265 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002266 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267}
2268EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2269
2270/**
2271 * snd_soc_info_volsw_2r - double mixer info callback
2272 * @kcontrol: mixer control
2273 * @uinfo: control element information
2274 *
2275 * Callback to provide information about a double mixer control that
2276 * spans 2 codec registers.
2277 *
2278 * Returns 0 for success.
2279 */
2280int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_info *uinfo)
2282{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002283 struct soc_mixer_control *mc =
2284 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002285 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002287 if (!mc->platform_max)
2288 mc->platform_max = mc->max;
2289 platform_max = mc->platform_max;
2290
2291 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002292 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2293 else
2294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2295
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002296 uinfo->count = 2;
2297 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002298 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2302
2303/**
2304 * snd_soc_get_volsw_2r - double mixer get callback
2305 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002306 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307 *
2308 * Callback to get the value of a double mixer control that spans 2 registers.
2309 *
2310 * Returns 0 for success.
2311 */
2312int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
2314{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002315 struct soc_mixer_control *mc =
2316 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002318 unsigned int reg = mc->reg;
2319 unsigned int reg2 = mc->rreg;
2320 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002321 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002322 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002323 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324
2325 ucontrol->value.integer.value[0] =
2326 (snd_soc_read(codec, reg) >> shift) & mask;
2327 ucontrol->value.integer.value[1] =
2328 (snd_soc_read(codec, reg2) >> shift) & mask;
2329 if (invert) {
2330 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002331 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002333 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 }
2335
2336 return 0;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2339
2340/**
2341 * snd_soc_put_volsw_2r - double mixer set callback
2342 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002343 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002344 *
2345 * Callback to set the value of a double mixer control that spans 2 registers.
2346 *
2347 * Returns 0 for success.
2348 */
2349int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
2351{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002352 struct soc_mixer_control *mc =
2353 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002355 unsigned int reg = mc->reg;
2356 unsigned int reg2 = mc->rreg;
2357 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002358 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002359 unsigned int mask = (1 << fls(max)) - 1;
2360 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002361 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002362 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363
2364 val_mask = mask << shift;
2365 val = (ucontrol->value.integer.value[0] & mask);
2366 val2 = (ucontrol->value.integer.value[1] & mask);
2367
2368 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002369 val = max - val;
2370 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371 }
2372
2373 val = val << shift;
2374 val2 = val2 << shift;
2375
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002376 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002377 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 return err;
2379
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002380 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381 return err;
2382}
2383EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2384
Mark Browne13ac2e2008-05-28 17:58:05 +01002385/**
2386 * snd_soc_info_volsw_s8 - signed mixer info callback
2387 * @kcontrol: mixer control
2388 * @uinfo: control element information
2389 *
2390 * Callback to provide information about a signed mixer control.
2391 *
2392 * Returns 0 for success.
2393 */
2394int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_info *uinfo)
2396{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002397 struct soc_mixer_control *mc =
2398 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002399 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002400 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002401
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002402 if (!mc->platform_max)
2403 mc->platform_max = mc->max;
2404 platform_max = mc->platform_max;
2405
Mark Browne13ac2e2008-05-28 17:58:05 +01002406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2407 uinfo->count = 2;
2408 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002409 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002410 return 0;
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2413
2414/**
2415 * snd_soc_get_volsw_s8 - signed mixer get 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 get the value of a signed mixer control.
2420 *
2421 * Returns 0 for success.
2422 */
2423int snd_soc_get_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;
Mark Browne13ac2e2008-05-28 17:58:05 +01002431 int val = snd_soc_read(codec, reg);
2432
2433 ucontrol->value.integer.value[0] =
2434 ((signed char)(val & 0xff))-min;
2435 ucontrol->value.integer.value[1] =
2436 ((signed char)((val >> 8) & 0xff))-min;
2437 return 0;
2438}
2439EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2440
2441/**
2442 * snd_soc_put_volsw_sgn - signed mixer put callback
2443 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002444 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002445 *
2446 * Callback to set the value of a signed mixer control.
2447 *
2448 * Returns 0 for success.
2449 */
2450int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2452{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002453 struct soc_mixer_control *mc =
2454 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002455 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002456 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002457 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002458 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002459
2460 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2461 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2462
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002463 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002464}
2465EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2466
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002467/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002468 * snd_soc_limit_volume - Set new limit to an existing volume control.
2469 *
2470 * @codec: where to look for the control
2471 * @name: Name of the control
2472 * @max: new maximum limit
2473 *
2474 * Return 0 for success, else error.
2475 */
2476int snd_soc_limit_volume(struct snd_soc_codec *codec,
2477 const char *name, int max)
2478{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002479 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002480 struct snd_kcontrol *kctl;
2481 struct soc_mixer_control *mc;
2482 int found = 0;
2483 int ret = -EINVAL;
2484
2485 /* Sanity check for name and max */
2486 if (unlikely(!name || max <= 0))
2487 return -EINVAL;
2488
2489 list_for_each_entry(kctl, &card->controls, list) {
2490 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2491 found = 1;
2492 break;
2493 }
2494 }
2495 if (found) {
2496 mc = (struct soc_mixer_control *)kctl->private_value;
2497 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002498 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002499 ret = 0;
2500 }
2501 }
2502 return ret;
2503}
2504EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2505
2506/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002507 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2508 * mixer info callback
2509 * @kcontrol: mixer control
2510 * @uinfo: control element information
2511 *
2512 * Returns 0 for success.
2513 */
2514int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_info *uinfo)
2516{
2517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
2519 int max = mc->max;
2520 int min = mc->min;
2521
2522 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2523 uinfo->count = 2;
2524 uinfo->value.integer.min = 0;
2525 uinfo->value.integer.max = max-min;
2526
2527 return 0;
2528}
2529EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2530
2531/**
2532 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2533 * mixer get callback
2534 * @kcontrol: mixer control
2535 * @uinfo: control element information
2536 *
2537 * Returns 0 for success.
2538 */
2539int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2541{
2542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
2544 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2545 unsigned int mask = (1<<mc->shift)-1;
2546 int min = mc->min;
2547 int val = snd_soc_read(codec, mc->reg) & mask;
2548 int valr = snd_soc_read(codec, mc->rreg) & mask;
2549
Stuart Longland20630c72010-06-18 12:56:10 +10002550 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2551 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002552 return 0;
2553}
2554EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2555
2556/**
2557 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2558 * mixer put callback
2559 * @kcontrol: mixer control
2560 * @uinfo: control element information
2561 *
2562 * Returns 0 for success.
2563 */
2564int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_value *ucontrol)
2566{
2567 struct soc_mixer_control *mc =
2568 (struct soc_mixer_control *)kcontrol->private_value;
2569 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2570 unsigned int mask = (1<<mc->shift)-1;
2571 int min = mc->min;
2572 int ret;
2573 unsigned int val, valr, oval, ovalr;
2574
2575 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2576 val &= mask;
2577 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2578 valr &= mask;
2579
2580 oval = snd_soc_read(codec, mc->reg) & mask;
2581 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2582
2583 ret = 0;
2584 if (oval != val) {
2585 ret = snd_soc_write(codec, mc->reg, val);
2586 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002587 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002588 }
2589 if (ovalr != valr) {
2590 ret = snd_soc_write(codec, mc->rreg, valr);
2591 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002592 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002593 }
2594
2595 return 0;
2596}
2597EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2598
2599/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002600 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2601 * @dai: DAI
2602 * @clk_id: DAI specific clock ID
2603 * @freq: new clock frequency in Hz
2604 * @dir: new clock direction - input/output.
2605 *
2606 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2607 */
2608int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2609 unsigned int freq, int dir)
2610{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002611 if (dai->driver && dai->driver->ops->set_sysclk)
2612 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002613 else
2614 return -EINVAL;
2615}
2616EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2617
2618/**
2619 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2620 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002621 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002622 * @div: new clock divisor.
2623 *
2624 * Configures the clock dividers. This is used to derive the best DAI bit and
2625 * frame clocks from the system or master clock. It's best to set the DAI bit
2626 * and frame clocks as low as possible to save system power.
2627 */
2628int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2629 int div_id, int div)
2630{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002631 if (dai->driver && dai->driver->ops->set_clkdiv)
2632 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002633 else
2634 return -EINVAL;
2635}
2636EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2637
2638/**
2639 * snd_soc_dai_set_pll - configure DAI PLL.
2640 * @dai: DAI
2641 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002642 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002643 * @freq_in: PLL input clock frequency in Hz
2644 * @freq_out: requested PLL output clock frequency in Hz
2645 *
2646 * Configures and enables PLL to generate output clock based on input clock.
2647 */
Mark Brown85488032009-09-05 18:52:16 +01002648int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2649 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002650{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002651 if (dai->driver && dai->driver->ops->set_pll)
2652 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002653 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002654 else
2655 return -EINVAL;
2656}
2657EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2658
2659/**
2660 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2661 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002662 * @fmt: SND_SOC_DAIFMT_ format value.
2663 *
2664 * Configures the DAI hardware format and clocking.
2665 */
2666int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2667{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002668 if (dai->driver && dai->driver->ops->set_fmt)
2669 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002670 else
2671 return -EINVAL;
2672}
2673EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2674
2675/**
2676 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2677 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002678 * @tx_mask: bitmask representing active TX slots.
2679 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002680 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002681 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002682 *
2683 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2684 * specific.
2685 */
2686int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002687 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002688{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002689 if (dai->driver && dai->driver->ops->set_tdm_slot)
2690 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002691 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002692 else
2693 return -EINVAL;
2694}
2695EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2696
2697/**
Barry Song472df3c2009-09-12 01:16:29 +08002698 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2699 * @dai: DAI
2700 * @tx_num: how many TX channels
2701 * @tx_slot: pointer to an array which imply the TX slot number channel
2702 * 0~num-1 uses
2703 * @rx_num: how many RX channels
2704 * @rx_slot: pointer to an array which imply the RX slot number channel
2705 * 0~num-1 uses
2706 *
2707 * configure the relationship between channel number and TDM slot number.
2708 */
2709int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2710 unsigned int tx_num, unsigned int *tx_slot,
2711 unsigned int rx_num, unsigned int *rx_slot)
2712{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002713 if (dai->driver && dai->driver->ops->set_channel_map)
2714 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002715 rx_num, rx_slot);
2716 else
2717 return -EINVAL;
2718}
2719EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2720
2721/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002722 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2723 * @dai: DAI
2724 * @tristate: tristate enable
2725 *
2726 * Tristates the DAI so that others can use it.
2727 */
2728int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2729{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730 if (dai->driver && dai->driver->ops->set_tristate)
2731 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002732 else
2733 return -EINVAL;
2734}
2735EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2736
2737/**
2738 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2739 * @dai: DAI
2740 * @mute: mute enable
2741 *
2742 * Mutes the DAI DAC.
2743 */
2744int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2745{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002746 if (dai->driver && dai->driver->ops->digital_mute)
2747 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002748 else
2749 return -EINVAL;
2750}
2751EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2752
Mark Brownc5af3a22008-11-28 13:29:45 +00002753/**
2754 * snd_soc_register_card - Register a card with the ASoC core
2755 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002756 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002757 *
2758 * Note that currently this is an internal only function: it will be
2759 * exposed to machine drivers after further backporting of ASoC v2
2760 * registration APIs.
2761 */
2762static int snd_soc_register_card(struct snd_soc_card *card)
2763{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002764 int i;
2765
Mark Brownc5af3a22008-11-28 13:29:45 +00002766 if (!card->name || !card->dev)
2767 return -EINVAL;
2768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002769 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2770 GFP_KERNEL);
2771 if (card->rtd == NULL)
2772 return -ENOMEM;
2773
2774 for (i = 0; i < card->num_links; i++)
2775 card->rtd[i].dai_link = &card->dai_link[i];
2776
Mark Brownc5af3a22008-11-28 13:29:45 +00002777 INIT_LIST_HEAD(&card->list);
2778 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002779 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002780
2781 mutex_lock(&client_mutex);
2782 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002783 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002784 mutex_unlock(&client_mutex);
2785
2786 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2787
2788 return 0;
2789}
2790
2791/**
2792 * snd_soc_unregister_card - Unregister a card with the ASoC core
2793 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002794 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002795 *
2796 * Note that currently this is an internal only function: it will be
2797 * exposed to machine drivers after further backporting of ASoC v2
2798 * registration APIs.
2799 */
2800static int snd_soc_unregister_card(struct snd_soc_card *card)
2801{
2802 mutex_lock(&client_mutex);
2803 list_del(&card->list);
2804 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002805 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2806
2807 return 0;
2808}
2809
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002810/*
2811 * Simplify DAI link configuration by removing ".-1" from device names
2812 * and sanitizing names.
2813 */
2814static inline char *fmt_single_name(struct device *dev, int *id)
2815{
2816 char *found, name[NAME_SIZE];
2817 int id1, id2;
2818
2819 if (dev_name(dev) == NULL)
2820 return NULL;
2821
2822 strncpy(name, dev_name(dev), NAME_SIZE);
2823
2824 /* are we a "%s.%d" name (platform and SPI components) */
2825 found = strstr(name, dev->driver->name);
2826 if (found) {
2827 /* get ID */
2828 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2829
2830 /* discard ID from name if ID == -1 */
2831 if (*id == -1)
2832 found[strlen(dev->driver->name)] = '\0';
2833 }
2834
2835 } else {
2836 /* I2C component devices are named "bus-addr" */
2837 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2838 char tmp[NAME_SIZE];
2839
2840 /* create unique ID number from I2C addr and bus */
2841 *id = ((id1 && 0xffff) << 16) + id2;
2842
2843 /* sanitize component name for DAI link creation */
2844 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2845 strncpy(name, tmp, NAME_SIZE);
2846 } else
2847 *id = 0;
2848 }
2849
2850 return kstrdup(name, GFP_KERNEL);
2851}
2852
2853/*
2854 * Simplify DAI link naming for single devices with multiple DAIs by removing
2855 * any ".-1" and using the DAI name (instead of device name).
2856 */
2857static inline char *fmt_multiple_name(struct device *dev,
2858 struct snd_soc_dai_driver *dai_drv)
2859{
2860 if (dai_drv->name == NULL) {
2861 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2862 dev_name(dev));
2863 return NULL;
2864 }
2865
2866 return kstrdup(dai_drv->name, GFP_KERNEL);
2867}
2868
Mark Brown91151712008-11-30 23:31:24 +00002869/**
2870 * snd_soc_register_dai - Register a DAI with the ASoC core
2871 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002872 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002873 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002874int snd_soc_register_dai(struct device *dev,
2875 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002876{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002877 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002879 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002880
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002881 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2882 if (dai == NULL)
2883 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002884
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002885 /* create DAI component name */
2886 dai->name = fmt_single_name(dev, &dai->id);
2887 if (dai->name == NULL) {
2888 kfree(dai);
2889 return -ENOMEM;
2890 }
2891
2892 dai->dev = dev;
2893 dai->driver = dai_drv;
2894 if (!dai->driver->ops)
2895 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002896
2897 mutex_lock(&client_mutex);
2898 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002899 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002900 mutex_unlock(&client_mutex);
2901
2902 pr_debug("Registered DAI '%s'\n", dai->name);
2903
2904 return 0;
2905}
2906EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2907
2908/**
2909 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2910 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002911 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002912 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002913void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002914{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002915 struct snd_soc_dai *dai;
2916
2917 list_for_each_entry(dai, &dai_list, list) {
2918 if (dev == dai->dev)
2919 goto found;
2920 }
2921 return;
2922
2923found:
Mark Brown91151712008-11-30 23:31:24 +00002924 mutex_lock(&client_mutex);
2925 list_del(&dai->list);
2926 mutex_unlock(&client_mutex);
2927
2928 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002929 kfree(dai->name);
2930 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002931}
2932EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2933
2934/**
2935 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2936 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002937 * @dai: Array of DAIs to register
2938 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002939 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002940int snd_soc_register_dais(struct device *dev,
2941 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002942{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002943 struct snd_soc_dai *dai;
2944 int i, ret = 0;
2945
Liam Girdwood720ffa42010-08-18 00:30:30 +01002946 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002947
2948 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949
2950 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2951 if (dai == NULL)
2952 return -ENOMEM;
2953
2954 /* create DAI component name */
2955 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2956 if (dai->name == NULL) {
2957 kfree(dai);
2958 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00002959 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002960 }
2961
2962 dai->dev = dev;
2963 dai->id = i;
2964 dai->driver = &dai_drv[i];
2965 if (!dai->driver->ops)
2966 dai->driver->ops = &null_dai_ops;
2967
2968 mutex_lock(&client_mutex);
2969 list_add(&dai->list, &dai_list);
2970 mutex_unlock(&client_mutex);
2971
2972 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002973 }
2974
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002975 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002976 return 0;
2977
2978err:
2979 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002981
2982 return ret;
2983}
2984EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2985
2986/**
2987 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2988 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002989 * @dai: Array of DAIs to unregister
2990 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002991 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002992void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002993{
2994 int i;
2995
2996 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002997 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002998}
2999EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3000
Mark Brown12a48a8c2008-12-03 19:40:30 +00003001/**
3002 * snd_soc_register_platform - Register a platform with the ASoC core
3003 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003004 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003005 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003006int snd_soc_register_platform(struct device *dev,
3007 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003008{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3012
3013 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3014 if (platform == NULL)
3015 return -ENOMEM;
3016
3017 /* create platform component name */
3018 platform->name = fmt_single_name(dev, &platform->id);
3019 if (platform->name == NULL) {
3020 kfree(platform);
3021 return -ENOMEM;
3022 }
3023
3024 platform->dev = dev;
3025 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003026
3027 mutex_lock(&client_mutex);
3028 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003029 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003030 mutex_unlock(&client_mutex);
3031
3032 pr_debug("Registered platform '%s'\n", platform->name);
3033
3034 return 0;
3035}
3036EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3037
3038/**
3039 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3040 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003041 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003042 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003043void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003044{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003045 struct snd_soc_platform *platform;
3046
3047 list_for_each_entry(platform, &platform_list, list) {
3048 if (dev == platform->dev)
3049 goto found;
3050 }
3051 return;
3052
3053found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003054 mutex_lock(&client_mutex);
3055 list_del(&platform->list);
3056 mutex_unlock(&client_mutex);
3057
3058 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003059 kfree(platform->name);
3060 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003061}
3062EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3063
Mark Brown151ab222009-05-09 16:22:58 +01003064static u64 codec_format_map[] = {
3065 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3066 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3067 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3068 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3069 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3070 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3071 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3072 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3073 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3074 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3075 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3076 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3077 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3078 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3079 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3080 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3081};
3082
3083/* Fix up the DAI formats for endianness: codecs don't actually see
3084 * the endianness of the data but we're using the CPU format
3085 * definitions which do need to include endianness so we ensure that
3086 * codec DAIs always have both big and little endian variants set.
3087 */
3088static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3089{
3090 int i;
3091
3092 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3093 if (stream->formats & codec_format_map[i])
3094 stream->formats |= codec_format_map[i];
3095}
3096
Mark Brown0d0cf002008-12-10 14:32:45 +00003097/**
3098 * snd_soc_register_codec - Register a codec with the ASoC core
3099 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003100 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003101 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003102int snd_soc_register_codec(struct device *dev,
3103 struct snd_soc_codec_driver *codec_drv,
3104 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003105{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003106 struct snd_soc_codec *codec;
3107 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003109 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003110
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003111 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3112 if (codec == NULL)
3113 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 /* create CODEC component name */
3116 codec->name = fmt_single_name(dev, &codec->id);
3117 if (codec->name == NULL) {
3118 kfree(codec);
3119 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003120 }
3121
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122 /* allocate CODEC register cache */
3123 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3124
3125 if (codec_drv->reg_cache_default)
3126 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3127 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3128 else
3129 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3130 codec_drv->reg_word_size, GFP_KERNEL);
3131
3132 if (codec->reg_cache == NULL) {
3133 kfree(codec->name);
3134 kfree(codec);
3135 return -ENOMEM;
3136 }
3137 }
3138
3139 codec->dev = dev;
3140 codec->driver = codec_drv;
3141 codec->bias_level = SND_SOC_BIAS_OFF;
3142 codec->num_dai = num_dai;
3143 mutex_init(&codec->mutex);
3144 INIT_LIST_HEAD(&codec->dapm_widgets);
3145 INIT_LIST_HEAD(&codec->dapm_paths);
3146
3147 for (i = 0; i < num_dai; i++) {
3148 fixup_codec_formats(&dai_drv[i].playback);
3149 fixup_codec_formats(&dai_drv[i].capture);
3150 }
3151
Mark Brown26b01cc2010-08-18 20:20:55 +01003152 /* register any DAIs */
3153 if (num_dai) {
3154 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3155 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003156 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003157 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003158
Mark Brown0d0cf002008-12-10 14:32:45 +00003159 mutex_lock(&client_mutex);
3160 list_add(&codec->list, &codec_list);
3161 snd_soc_instantiate_cards();
3162 mutex_unlock(&client_mutex);
3163
3164 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003165 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003166
3167error:
3168 for (i--; i >= 0; i--)
3169 snd_soc_unregister_dai(dev);
3170
3171 if (codec->reg_cache)
3172 kfree(codec->reg_cache);
3173 kfree(codec->name);
3174 kfree(codec);
3175 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003176}
3177EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3178
3179/**
3180 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3181 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003182 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003183 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003185{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003186 struct snd_soc_codec *codec;
3187 int i;
3188
3189 list_for_each_entry(codec, &codec_list, list) {
3190 if (dev == codec->dev)
3191 goto found;
3192 }
3193 return;
3194
3195found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003196 if (codec->num_dai)
3197 for (i = 0; i < codec->num_dai; i++)
3198 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003199
Mark Brown0d0cf002008-12-10 14:32:45 +00003200 mutex_lock(&client_mutex);
3201 list_del(&codec->list);
3202 mutex_unlock(&client_mutex);
3203
3204 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205
3206 if (codec->reg_cache)
3207 kfree(codec->reg_cache);
3208 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003209}
3210EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3211
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003212static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003213{
Mark Brown384c89e2008-12-03 17:34:03 +00003214#ifdef CONFIG_DEBUG_FS
3215 debugfs_root = debugfs_create_dir("asoc", NULL);
3216 if (IS_ERR(debugfs_root) || !debugfs_root) {
3217 printk(KERN_WARNING
3218 "ASoC: Failed to create debugfs directory\n");
3219 debugfs_root = NULL;
3220 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003221
3222 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3223 &codec_list_fops))
3224 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3225
Mark Brown384c89e2008-12-03 17:34:03 +00003226#endif
3227
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003228 return platform_driver_register(&soc_driver);
3229}
3230
Mark Brown7d8c16a2008-11-30 22:11:24 +00003231static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003232{
Mark Brown384c89e2008-12-03 17:34:03 +00003233#ifdef CONFIG_DEBUG_FS
3234 debugfs_remove_recursive(debugfs_root);
3235#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003236 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003237}
3238
3239module_init(snd_soc_init);
3240module_exit(snd_soc_exit);
3241
3242/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003243MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003244MODULE_DESCRIPTION("ALSA SoC Core");
3245MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003246MODULE_ALIAS("platform:soc-audio");