blob: 7d22b5d5bc0dc7a9496a30c05855371c62c08139 [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 Brownf3208782010-09-15 18:19:07 +0100300static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
301 size_t count, loff_t *ppos)
302{
303 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
304 ssize_t ret = 0;
305 struct snd_soc_dai *dai;
306
307 if (!buf)
308 return -ENOMEM;
309
310 list_for_each_entry(dai, &dai_list, list)
311 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
312
313 if (ret >= 0)
314 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
315
316 kfree(buf);
317
318 return ret;
319}
320
321static const struct file_operations dai_list_fops = {
322 .read = dai_list_read_file,
323 .llseek = default_llseek,/* read accesses f_pos */
324};
325
Mark Brown19c7ac22010-09-15 18:22:40 +0100326static ssize_t platform_list_read_file(struct file *file,
327 char __user *user_buf,
328 size_t count, loff_t *ppos)
329{
330 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
331 ssize_t ret = 0;
332 struct snd_soc_platform *platform;
333
334 if (!buf)
335 return -ENOMEM;
336
337 list_for_each_entry(platform, &platform_list, list)
338 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
339 platform->name);
340
341 if (ret >= 0)
342 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
343
344 kfree(buf);
345
346 return ret;
347}
348
349static const struct file_operations platform_list_fops = {
350 .read = platform_list_read_file,
351 .llseek = default_llseek,/* read accesses f_pos */
352};
353
Mark Brown2624d5f2009-11-03 21:56:13 +0000354#else
355
356static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
357{
358}
359
360static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
361{
362}
363#endif
364
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200365#ifdef CONFIG_SND_SOC_AC97_BUS
366/* unregister ac97 codec */
367static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
368{
369 if (codec->ac97->dev.bus)
370 device_unregister(&codec->ac97->dev);
371 return 0;
372}
373
374/* stop no dev release warning */
375static void soc_ac97_device_release(struct device *dev){}
376
377/* register ac97 codec to bus */
378static int soc_ac97_dev_register(struct snd_soc_codec *codec)
379{
380 int err;
381
382 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100383 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200384 codec->ac97->dev.release = soc_ac97_device_release;
385
Kay Sieversbb072bf2008-11-02 03:50:35 +0100386 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200388 err = device_register(&codec->ac97->dev);
389 if (err < 0) {
390 snd_printk(KERN_ERR "Can't register ac97 bus\n");
391 codec->ac97->dev.bus = NULL;
392 return err;
393 }
394 return 0;
395}
396#endif
397
Mark Brown06f409d2009-04-07 18:10:13 +0100398static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
399{
400 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000401 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
402 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100403 int ret;
404
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
406 rtd->dai_link->symmetric_rates) {
407 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
408 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100409
410 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
411 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000412 rtd->rate,
413 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100414 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100416 "Unable to apply rate symmetry constraint: %d\n", ret);
417 return ret;
418 }
419 }
420
421 return 0;
422}
423
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200424/*
425 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
426 * then initialized and any private data can be allocated. This also calls
427 * startup for the cpu DAI, platform, machine and codec DAI.
428 */
429static int soc_pcm_open(struct snd_pcm_substream *substream)
430{
431 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200432 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000433 struct snd_soc_platform *platform = rtd->platform;
434 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
435 struct snd_soc_dai *codec_dai = rtd->codec_dai;
436 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
437 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200438 int ret = 0;
439
440 mutex_lock(&pcm_mutex);
441
442 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000443 if (cpu_dai->driver->ops->startup) {
444 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200445 if (ret < 0) {
446 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100447 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200448 goto out;
449 }
450 }
451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 if (platform->driver->ops->open) {
453 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 if (ret < 0) {
455 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
456 goto platform_err;
457 }
458 }
459
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000460 if (codec_dai->driver->ops->startup) {
461 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100462 if (ret < 0) {
463 printk(KERN_ERR "asoc: can't open codec %s\n",
464 codec_dai->name);
465 goto codec_dai_err;
466 }
467 }
468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
470 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 goto machine_err;
474 }
475 }
476
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 /* Check that the codec and cpu DAI's are compatible */
478 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
479 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 max(codec_dai_drv->playback.rate_min,
481 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200482 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483 min(codec_dai_drv->playback.rate_max,
484 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486 max(codec_dai_drv->playback.channels_min,
487 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 min(codec_dai_drv->playback.channels_max,
490 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100491 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100493 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
495 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900496 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 runtime->hw.rates |= cpu_dai_drv->playback.rates;
498 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900499 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501 } else {
502 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 max(codec_dai_drv->capture.rate_min,
504 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 min(codec_dai_drv->capture.rate_max,
507 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 max(codec_dai_drv->capture.channels_min,
510 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200511 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 min(codec_dai_drv->capture.channels_max,
513 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100514 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100516 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
518 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900519 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 runtime->hw.rates |= cpu_dai_drv->capture.rates;
521 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900522 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 }
525
526 snd_pcm_limit_hw_rates(runtime);
527 if (!runtime->hw.rates) {
528 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100529 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900530 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 }
532 if (!runtime->hw.formats) {
533 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900535 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 }
537 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
538 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900540 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 }
542
Mark Brown06f409d2009-04-07 18:10:13 +0100543 /* Symmetry only applies if we've already got an active stream. */
544 if (cpu_dai->active || codec_dai->active) {
545 ret = soc_pcm_apply_symmetry(substream);
546 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900547 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100548 }
549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 pr_debug("asoc: %s <-> %s info:\n",
551 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100552 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
553 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
554 runtime->hw.channels_max);
555 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
556 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557
Jassi Brar14dc5732010-02-26 09:12:32 +0900558 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 cpu_dai->playback_active++;
560 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900561 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 cpu_dai->capture_active++;
563 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900564 }
565 cpu_dai->active++;
566 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568 mutex_unlock(&pcm_mutex);
569 return 0;
570
Jassi Brarbb1c0472010-02-25 11:24:53 +0900571config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
573 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
Jassi Brarbb1c0472010-02-25 11:24:53 +0900575machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 if (codec_dai->driver->ops->shutdown)
577 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900578
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100579codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 if (platform->driver->ops->close)
581 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
583platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (cpu_dai->driver->ops->shutdown)
585 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586out:
587 mutex_unlock(&pcm_mutex);
588 return ret;
589}
590
591/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200592 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593 * This is to ensure there are no pops or clicks in between any music tracks
594 * due to DAPM power cycling.
595 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100596static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 struct snd_soc_pcm_runtime *rtd =
599 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
600 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601
602 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
605 codec_dai->driver->playback.stream_name,
606 codec_dai->playback_active ? "active" : "inactive",
607 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 /* are we waiting on this codec DAI stream */
610 if (codec_dai->pop_wait == 1) {
611 codec_dai->pop_wait = 0;
612 snd_soc_dapm_stream_event(rtd,
613 codec_dai->driver->playback.stream_name,
614 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617 mutex_unlock(&pcm_mutex);
618}
619
620/*
621 * Called by ALSA when a PCM substream is closed. Private data can be
622 * freed here. The cpu DAI, codec DAI, machine and platform are also
623 * shutdown.
624 */
625static int soc_codec_close(struct snd_pcm_substream *substream)
626{
627 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 struct snd_soc_platform *platform = rtd->platform;
629 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
630 struct snd_soc_dai *codec_dai = rtd->codec_dai;
631 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632
633 mutex_lock(&pcm_mutex);
634
Jassi Brar14dc5732010-02-26 09:12:32 +0900635 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 cpu_dai->playback_active--;
637 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900638 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 cpu_dai->capture_active--;
640 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900642
643 cpu_dai->active--;
644 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 codec->active--;
646
Mark Brown6010b2d2008-09-06 18:33:24 +0100647 /* Muting the DAC suppresses artifacts caused during digital
648 * shutdown, for example from stopping clocks.
649 */
650 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
651 snd_soc_dai_digital_mute(codec_dai, 1);
652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 if (cpu_dai->driver->ops->shutdown)
654 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 if (codec_dai->driver->ops->shutdown)
657 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
660 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 if (platform->driver->ops->close)
663 platform->driver->ops->close(substream);
664 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
666 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
667 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100668 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 schedule_delayed_work(&rtd->delayed_work,
670 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671 } else {
672 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 snd_soc_dapm_stream_event(rtd,
674 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100675 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676 }
677
678 mutex_unlock(&pcm_mutex);
679 return 0;
680}
681
682/*
683 * Called by ALSA when the PCM substream is prepared, can set format, sample
684 * rate, etc. This function is non atomic and can be called multiple times,
685 * it can refer to the runtime info.
686 */
687static int soc_pcm_prepare(struct snd_pcm_substream *substream)
688{
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 struct snd_soc_platform *platform = rtd->platform;
691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 int ret = 0;
694
695 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
698 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100699 if (ret < 0) {
700 printk(KERN_ERR "asoc: machine prepare error\n");
701 goto out;
702 }
703 }
704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 if (platform->driver->ops->prepare) {
706 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200707 if (ret < 0) {
708 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200710 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200711 }
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (codec_dai->driver->ops->prepare) {
714 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200715 if (ret < 0) {
716 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200718 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719 }
720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (cpu_dai->driver->ops->prepare) {
722 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100723 if (ret < 0) {
724 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
725 goto out;
726 }
727 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728
Mark Brownd45f6212008-10-14 13:58:36 +0100729 /* cancel any delayed stream shutdown that is pending */
730 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
731 codec_dai->pop_wait) {
732 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100734 }
735
Mark Brown452c5ea2009-05-17 21:41:23 +0100736 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 snd_soc_dapm_stream_event(rtd,
738 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100739 SND_SOC_DAPM_STREAM_START);
740 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 snd_soc_dapm_stream_event(rtd,
742 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100743 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100744
Mark Brown452c5ea2009-05-17 21:41:23 +0100745 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746
747out:
748 mutex_unlock(&pcm_mutex);
749 return ret;
750}
751
752/*
753 * Called by ALSA when the hardware params are set by application. This
754 * function can also be called multiple times and can allocate buffers
755 * (using snd_pcm_lib_* ). It's non-atomic.
756 */
757static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
758 struct snd_pcm_hw_params *params)
759{
760 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 struct snd_soc_platform *platform = rtd->platform;
762 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
763 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764 int ret = 0;
765
766 mutex_lock(&pcm_mutex);
767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000768 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
769 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200770 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100771 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772 goto out;
773 }
774 }
775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 if (codec_dai->driver->ops->hw_params) {
777 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100778 if (ret < 0) {
779 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
780 codec_dai->name);
781 goto codec_err;
782 }
783 }
784
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 if (cpu_dai->driver->ops->hw_params) {
786 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200788 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100789 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 goto interface_err;
791 }
792 }
793
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 if (platform->driver->ops->hw_params) {
795 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200797 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200798 platform->name);
799 goto platform_err;
800 }
801 }
802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100804
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805out:
806 mutex_unlock(&pcm_mutex);
807 return ret;
808
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000810 if (cpu_dai->driver->ops->hw_free)
811 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200812
813interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 if (codec_dai->driver->ops->hw_free)
815 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100816
817codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000818 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
819 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820
821 mutex_unlock(&pcm_mutex);
822 return ret;
823}
824
825/*
826 * Free's resources allocated by hw_params, can be called multiple times
827 */
828static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
829{
830 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 struct snd_soc_platform *platform = rtd->platform;
832 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
833 struct snd_soc_dai *codec_dai = rtd->codec_dai;
834 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835
836 mutex_lock(&pcm_mutex);
837
838 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100839 if (!codec->active)
840 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841
842 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
844 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845
846 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (platform->driver->ops->hw_free)
848 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
850 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (codec_dai->driver->ops->hw_free)
852 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000854 if (cpu_dai->driver->ops->hw_free)
855 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856
857 mutex_unlock(&pcm_mutex);
858 return 0;
859}
860
861static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
862{
863 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 struct snd_soc_platform *platform = rtd->platform;
865 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
866 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867 int ret;
868
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 if (codec_dai->driver->ops->trigger) {
870 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871 if (ret < 0)
872 return ret;
873 }
874
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875 if (platform->driver->ops->trigger) {
876 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200877 if (ret < 0)
878 return ret;
879 }
880
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881 if (cpu_dai->driver->ops->trigger) {
882 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200883 if (ret < 0)
884 return ret;
885 }
886 return 0;
887}
888
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200889/*
890 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200891 * If cpu_dai, codec_dai, platform driver has the delay callback, than
892 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200893 */
894static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
895{
896 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 struct snd_soc_platform *platform = rtd->platform;
898 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
899 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200900 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200901 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200902 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 if (platform->driver->ops->pointer)
905 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 if (cpu_dai->driver->ops->delay)
908 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (codec_dai->driver->ops->delay)
911 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200912
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 if (platform->driver->delay)
914 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200915
916 runtime->delay = delay;
917
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200918 return offset;
919}
920
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200921/* ASoC PCM operations */
922static struct snd_pcm_ops soc_pcm_ops = {
923 .open = soc_pcm_open,
924 .close = soc_codec_close,
925 .hw_params = soc_pcm_hw_params,
926 .hw_free = soc_pcm_hw_free,
927 .prepare = soc_pcm_prepare,
928 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200929 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200930};
931
932#ifdef CONFIG_PM
933/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100934static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200935{
Mark Brown416356f2009-06-30 19:05:15 +0100936 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000937 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200938 int i;
939
Daniel Macke3509ff2009-06-03 17:44:49 +0200940 /* If the initialization of this soc device failed, there is no codec
941 * associated with it. Just bail out in this case.
942 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200944 return 0;
945
Andy Green6ed25972008-06-13 16:24:05 +0100946 /* Due to the resume being scheduled into a workqueue we could
947 * suspend before that's finished - wait for it to complete.
948 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000949 snd_power_lock(card->snd_card);
950 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
951 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100952
953 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000954 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100955
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200956 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 for (i = 0; i < card->num_rtd; i++) {
958 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
959 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100960
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100962 continue;
963
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 if (drv->ops->digital_mute && dai->playback_active)
965 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200966 }
967
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100968 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 for (i = 0; i < card->num_rtd; i++) {
970 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100971 continue;
972
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100974 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100975
Mark Brown87506542008-11-18 20:50:34 +0000976 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100977 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 for (i = 0; i < card->num_rtd; i++) {
980 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
981 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100984 continue;
985
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
987 cpu_dai->driver->suspend(cpu_dai);
988 if (platform->driver->suspend && !platform->suspended) {
989 platform->driver->suspend(cpu_dai);
990 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100991 }
992 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200993
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 /* close any waiting streams and save state */
995 for (i = 0; i < card->num_rtd; i++) {
996 run_delayed_work(&card->rtd[i].delayed_work);
997 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
998 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100999
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000 for (i = 0; i < card->num_rtd; i++) {
1001 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1002
1003 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001004 continue;
1005
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 if (driver->playback.stream_name != NULL)
1007 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1008 SND_SOC_DAPM_STREAM_SUSPEND);
1009
1010 if (driver->capture.stream_name != NULL)
1011 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1012 SND_SOC_DAPM_STREAM_SUSPEND);
1013 }
1014
1015 /* suspend all CODECs */
1016 for (i = 0; i < card->num_rtd; i++) {
1017 struct snd_soc_codec *codec = card->rtd[i].codec;
1018 /* If there are paths active then the CODEC will be held with
1019 * bias _ON and should not be suspended. */
1020 if (!codec->suspended && codec->driver->suspend) {
1021 switch (codec->bias_level) {
1022 case SND_SOC_BIAS_STANDBY:
1023 case SND_SOC_BIAS_OFF:
1024 codec->driver->suspend(codec, PMSG_SUSPEND);
1025 codec->suspended = 1;
1026 break;
1027 default:
1028 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1029 break;
1030 }
1031 }
1032 }
1033
1034 for (i = 0; i < card->num_rtd; i++) {
1035 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1036
1037 if (card->rtd[i].dai_link->ignore_suspend)
1038 continue;
1039
1040 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1041 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001042 }
1043
Mark Brown87506542008-11-18 20:50:34 +00001044 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001045 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001046
1047 return 0;
1048}
1049
Andy Green6ed25972008-06-13 16:24:05 +01001050/* deferred resume work, so resume can complete before we finished
1051 * setting our codec back up, which can be very slow on I2C
1052 */
1053static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001054{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 struct snd_soc_card *card =
1056 container_of(work, struct snd_soc_card, deferred_resume_work);
1057 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001058 int i;
1059
Andy Green6ed25972008-06-13 16:24:05 +01001060 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1061 * so userspace apps are blocked from touching us
1062 */
1063
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001064 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001065
Mark Brown99497882010-05-07 20:24:05 +01001066 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001067 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001068
Mark Brown87506542008-11-18 20:50:34 +00001069 if (card->resume_pre)
1070 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072 /* resume AC97 DAIs */
1073 for (i = 0; i < card->num_rtd; i++) {
1074 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001077 continue;
1078
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001079 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1080 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001081 }
1082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 for (i = 0; i < card->num_rtd; i++) {
1084 struct snd_soc_codec *codec = card->rtd[i].codec;
1085 /* If the CODEC was idle over suspend then it will have been
1086 * left with bias OFF or STANDBY and suspended so we must now
1087 * resume. Otherwise the suspend was suppressed.
1088 */
1089 if (codec->driver->resume && codec->suspended) {
1090 switch (codec->bias_level) {
1091 case SND_SOC_BIAS_STANDBY:
1092 case SND_SOC_BIAS_OFF:
1093 codec->driver->resume(codec);
1094 codec->suspended = 0;
1095 break;
1096 default:
1097 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1098 break;
1099 }
Mark Brown1547aba2010-05-07 21:11:40 +01001100 }
1101 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001102
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001103 for (i = 0; i < card->num_rtd; i++) {
1104 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001105
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001107 continue;
1108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001109 if (driver->playback.stream_name != NULL)
1110 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001111 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001112
1113 if (driver->capture.stream_name != NULL)
1114 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001115 SND_SOC_DAPM_STREAM_RESUME);
1116 }
1117
Mark Brown3ff3f642008-05-19 12:32:25 +02001118 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001119 for (i = 0; i < card->num_rtd; i++) {
1120 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1121 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001122
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001123 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001124 continue;
1125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001126 if (drv->ops->digital_mute && dai->playback_active)
1127 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001128 }
1129
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001130 for (i = 0; i < card->num_rtd; i++) {
1131 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1132 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001135 continue;
1136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001137 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1138 cpu_dai->driver->resume(cpu_dai);
1139 if (platform->driver->resume && platform->suspended) {
1140 platform->driver->resume(cpu_dai);
1141 platform->suspended = 0;
1142 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001143 }
1144
Mark Brown87506542008-11-18 20:50:34 +00001145 if (card->resume_post)
1146 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001149
1150 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001152}
1153
1154/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001155static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001156{
Mark Brown416356f2009-06-30 19:05:15 +01001157 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001158 struct snd_soc_card *card = platform_get_drvdata(pdev);
1159 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001160
Mark Brown64ab9ba2009-03-31 11:27:03 +01001161 /* AC97 devices might have other drivers hanging off them so
1162 * need to resume immediately. Other drivers don't have that
1163 * problem and may take a substantial amount of time to resume
1164 * due to I/O costs and anti-pop so handle them out of line.
1165 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166 for (i = 0; i < card->num_rtd; i++) {
1167 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1168 if (cpu_dai->driver->ac97_control) {
1169 dev_dbg(dev, "Resuming AC97 immediately\n");
1170 soc_resume_deferred(&card->deferred_resume_work);
1171 } else {
1172 dev_dbg(dev, "Scheduling resume work\n");
1173 if (!schedule_work(&card->deferred_resume_work))
1174 dev_err(dev, "resume work item may be lost\n");
1175 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001176 }
Andy Green6ed25972008-06-13 16:24:05 +01001177
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001178 return 0;
1179}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001180#else
1181#define soc_suspend NULL
1182#define soc_resume NULL
1183#endif
1184
Barry Song02a06d32009-10-16 18:13:38 +08001185static struct snd_soc_dai_ops null_dai_ops = {
1186};
1187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001189{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001190 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1191 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001192 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001193 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001194 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001196 if (rtd->complete)
1197 return 1;
1198 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001199
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 /* do we already have the CPU DAI for this link ? */
1201 if (rtd->cpu_dai) {
1202 goto find_codec;
1203 }
1204 /* no, then find CPU DAI from registered DAIs*/
1205 list_for_each_entry(cpu_dai, &dai_list, list) {
1206 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1207
1208 if (!try_module_get(cpu_dai->dev->driver->owner))
1209 return -ENODEV;
1210
1211 rtd->cpu_dai = cpu_dai;
1212 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001213 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001214 }
1215 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1216 dai_link->cpu_dai_name);
1217
1218find_codec:
1219 /* do we already have the CODEC for this link ? */
1220 if (rtd->codec) {
1221 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001222 }
1223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 /* no, then find CODEC from registered CODECs*/
1225 list_for_each_entry(codec, &codec_list, list) {
1226 if (!strcmp(codec->name, dai_link->codec_name)) {
1227 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001228
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001229 if (!try_module_get(codec->dev->driver->owner))
1230 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001232 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1233 list_for_each_entry(codec_dai, &dai_list, list) {
1234 if (codec->dev == codec_dai->dev &&
1235 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1236 rtd->codec_dai = codec_dai;
1237 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001238 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239 }
1240 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1241 dai_link->codec_dai_name);
1242
1243 goto find_platform;
1244 }
1245 }
1246 dev_dbg(card->dev, "CODEC %s not registered\n",
1247 dai_link->codec_name);
1248
1249find_platform:
1250 /* do we already have the CODEC DAI for this link ? */
1251 if (rtd->platform) {
1252 goto out;
1253 }
1254 /* no, then find CPU DAI from registered DAIs*/
1255 list_for_each_entry(platform, &platform_list, list) {
1256 if (!strcmp(platform->name, dai_link->platform_name)) {
1257
1258 if (!try_module_get(platform->dev->driver->owner))
1259 return -ENODEV;
1260
1261 rtd->platform = platform;
1262 goto out;
1263 }
1264 }
1265
1266 dev_dbg(card->dev, "platform %s not registered\n",
1267 dai_link->platform_name);
1268 return 0;
1269
1270out:
1271 /* mark rtd as complete if we found all 4 of our client devices */
1272 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1273 rtd->complete = 1;
1274 card->num_rtd++;
1275 }
1276 return 1;
1277}
1278
1279static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1280{
1281 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1282 struct snd_soc_codec *codec = rtd->codec;
1283 struct snd_soc_platform *platform = rtd->platform;
1284 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1285 int err;
1286
1287 /* unregister the rtd device */
1288 if (rtd->dev_registered) {
1289 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1290 device_unregister(&rtd->dev);
1291 rtd->dev_registered = 0;
1292 }
1293
1294 /* remove the CODEC DAI */
1295 if (codec_dai && codec_dai->probed) {
1296 if (codec_dai->driver->remove) {
1297 err = codec_dai->driver->remove(codec_dai);
1298 if (err < 0)
1299 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1300 }
1301 codec_dai->probed = 0;
1302 list_del(&codec_dai->card_list);
1303 }
1304
1305 /* remove the platform */
1306 if (platform && platform->probed) {
1307 if (platform->driver->remove) {
1308 err = platform->driver->remove(platform);
1309 if (err < 0)
1310 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1311 }
1312 platform->probed = 0;
1313 list_del(&platform->card_list);
1314 module_put(platform->dev->driver->owner);
1315 }
1316
1317 /* remove the CODEC */
1318 if (codec && codec->probed) {
1319 if (codec->driver->remove) {
1320 err = codec->driver->remove(codec);
1321 if (err < 0)
1322 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1323 }
1324
1325 /* Make sure all DAPM widgets are freed */
1326 snd_soc_dapm_free(codec);
1327
1328 soc_cleanup_codec_debugfs(codec);
1329 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1330 codec->probed = 0;
1331 list_del(&codec->card_list);
1332 module_put(codec->dev->driver->owner);
1333 }
1334
1335 /* remove the cpu_dai */
1336 if (cpu_dai && cpu_dai->probed) {
1337 if (cpu_dai->driver->remove) {
1338 err = cpu_dai->driver->remove(cpu_dai);
1339 if (err < 0)
1340 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1341 }
1342 cpu_dai->probed = 0;
1343 list_del(&cpu_dai->card_list);
1344 module_put(cpu_dai->dev->driver->owner);
1345 }
1346}
1347
1348static void rtd_release(struct device *dev) {}
1349
1350static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1351{
1352 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1353 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1354 struct snd_soc_codec *codec = rtd->codec;
1355 struct snd_soc_platform *platform = rtd->platform;
1356 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1357 int ret;
1358
1359 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1360
1361 /* config components */
1362 codec_dai->codec = codec;
1363 codec->card = card;
1364 cpu_dai->platform = platform;
1365 rtd->card = card;
1366 rtd->dev.parent = card->dev;
1367 codec_dai->card = card;
1368 cpu_dai->card = card;
1369
1370 /* set default power off timeout */
1371 rtd->pmdown_time = pmdown_time;
1372
1373 /* probe the cpu_dai */
1374 if (!cpu_dai->probed) {
1375 if (cpu_dai->driver->probe) {
1376 ret = cpu_dai->driver->probe(cpu_dai);
1377 if (ret < 0) {
1378 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1379 cpu_dai->name);
1380 return ret;
1381 }
1382 }
1383 cpu_dai->probed = 1;
1384 /* mark cpu_dai as probed and add to card cpu_dai list */
1385 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1386 }
1387
1388 /* probe the CODEC */
1389 if (!codec->probed) {
1390 if (codec->driver->probe) {
1391 ret = codec->driver->probe(codec);
1392 if (ret < 0) {
1393 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1394 codec->name);
1395 return ret;
1396 }
1397 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001398
1399 soc_init_codec_debugfs(codec);
1400
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001401 /* mark codec as probed and add to card codec list */
1402 codec->probed = 1;
1403 list_add(&codec->card_list, &card->codec_dev_list);
1404 }
1405
1406 /* probe the platform */
1407 if (!platform->probed) {
1408 if (platform->driver->probe) {
1409 ret = platform->driver->probe(platform);
1410 if (ret < 0) {
1411 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1412 platform->name);
1413 return ret;
1414 }
1415 }
1416 /* mark platform as probed and add to card platform list */
1417 platform->probed = 1;
1418 list_add(&platform->card_list, &card->platform_dev_list);
1419 }
1420
1421 /* probe the CODEC DAI */
1422 if (!codec_dai->probed) {
1423 if (codec_dai->driver->probe) {
1424 ret = codec_dai->driver->probe(codec_dai);
1425 if (ret < 0) {
1426 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1427 codec_dai->name);
1428 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001429 }
1430 }
1431
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001432 /* mark cpu_dai as probed and add to card cpu_dai list */
1433 codec_dai->probed = 1;
1434 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001435 }
1436
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437 /* DAPM dai link stream work */
1438 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1439
1440 /* now that all clients have probed, initialise the DAI link */
1441 if (dai_link->init) {
1442 ret = dai_link->init(rtd);
1443 if (ret < 0) {
1444 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1445 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001446 }
1447 }
1448
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001449 /* Make sure all DAPM widgets are instantiated */
1450 snd_soc_dapm_new_widgets(codec);
1451 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 rtd->dev.release = rtd_release;
1455 rtd->dev.init_name = dai_link->name;
1456 ret = device_register(&rtd->dev);
1457 if (ret < 0) {
1458 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1459 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001460 }
1461
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001462 rtd->dev_registered = 1;
1463 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1464 if (ret < 0)
1465 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1466
1467 /* add DAPM sysfs entries for this codec */
1468 ret = snd_soc_dapm_sys_add(&rtd->dev);
1469 if (ret < 0)
1470 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1471
1472 /* add codec sysfs entries */
1473 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1474 if (ret < 0)
1475 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1476
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477 /* create the pcm */
1478 ret = soc_new_pcm(rtd, num);
1479 if (ret < 0) {
1480 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1481 return ret;
1482 }
1483
1484 /* add platform data for AC97 devices */
1485 if (rtd->codec_dai->driver->ac97_control)
1486 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1487
1488 return 0;
1489}
1490
1491#ifdef CONFIG_SND_SOC_AC97_BUS
1492static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1493{
1494 int ret;
1495
1496 /* Only instantiate AC97 if not already done by the adaptor
1497 * for the generic AC97 subsystem.
1498 */
1499 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001500 /*
1501 * It is possible that the AC97 device is already registered to
1502 * the device subsystem. This happens when the device is created
1503 * via snd_ac97_mixer(). Currently only SoC codec that does so
1504 * is the generic AC97 glue but others migh emerge.
1505 *
1506 * In those cases we don't try to register the device again.
1507 */
1508 if (!rtd->codec->ac97_created)
1509 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510
1511 ret = soc_ac97_dev_register(rtd->codec);
1512 if (ret < 0) {
1513 printk(KERN_ERR "asoc: AC97 device register failed\n");
1514 return ret;
1515 }
1516
1517 rtd->codec->ac97_registered = 1;
1518 }
1519 return 0;
1520}
1521
1522static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1523{
1524 if (codec->ac97_registered) {
1525 soc_ac97_dev_unregister(codec);
1526 codec->ac97_registered = 0;
1527 }
1528}
1529#endif
1530
1531static void snd_soc_instantiate_card(struct snd_soc_card *card)
1532{
1533 struct platform_device *pdev = to_platform_device(card->dev);
1534 int ret, i;
1535
1536 mutex_lock(&card->mutex);
1537
1538 if (card->instantiated) {
1539 mutex_unlock(&card->mutex);
1540 return;
1541 }
1542
1543 /* bind DAIs */
1544 for (i = 0; i < card->num_links; i++)
1545 soc_bind_dai_link(card, i);
1546
1547 /* bind completed ? */
1548 if (card->num_rtd != card->num_links) {
1549 mutex_unlock(&card->mutex);
1550 return;
1551 }
1552
1553 /* card bind complete so register a sound card */
1554 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1555 card->owner, 0, &card->snd_card);
1556 if (ret < 0) {
1557 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1558 card->name);
1559 mutex_unlock(&card->mutex);
1560 return;
1561 }
1562 card->snd_card->dev = card->dev;
1563
Randy Dunlap1301a962008-06-17 19:19:34 +01001564#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001565 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001566 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001567#endif
Andy Green6ed25972008-06-13 16:24:05 +01001568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001569 /* initialise the sound card only once */
1570 if (card->probe) {
1571 ret = card->probe(pdev);
1572 if (ret < 0)
1573 goto card_probe_error;
1574 }
1575
Mark Brownfe3e78e2009-11-03 22:13:13 +00001576 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 ret = soc_probe_dai_link(card, i);
1578 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001579 pr_err("asoc: failed to instantiate card %s: %d\n",
1580 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 goto probe_dai_err;
1582 }
1583 }
1584
1585 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1586 "%s", card->name);
1587 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1588 "%s", card->name);
1589
1590 ret = snd_card_register(card->snd_card);
1591 if (ret < 0) {
1592 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1593 goto probe_dai_err;
1594 }
1595
1596#ifdef CONFIG_SND_SOC_AC97_BUS
1597 /* register any AC97 codecs */
1598 for (i = 0; i < card->num_rtd; i++) {
1599 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001600 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1602 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001603 }
1604 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001605#endif
1606
Mark Brown435c5e22008-12-04 15:32:53 +00001607 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001609 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001610
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611probe_dai_err:
1612 for (i = 0; i < card->num_links; i++)
1613 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001614
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001616 if (card->remove)
1617 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618
1619 snd_card_free(card->snd_card);
1620
1621 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001622}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623
Mark Brown435c5e22008-12-04 15:32:53 +00001624/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001625 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001626 * client_mutex.
1627 */
1628static void snd_soc_instantiate_cards(void)
1629{
1630 struct snd_soc_card *card;
1631 list_for_each_entry(card, &card_list, list)
1632 snd_soc_instantiate_card(card);
1633}
1634
1635/* probes a new socdev */
1636static int soc_probe(struct platform_device *pdev)
1637{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001639 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001640
1641 /* Bodge while we unpick instantiation */
1642 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643 INIT_LIST_HEAD(&card->dai_dev_list);
1644 INIT_LIST_HEAD(&card->codec_dev_list);
1645 INIT_LIST_HEAD(&card->platform_dev_list);
1646
Mark Brown435c5e22008-12-04 15:32:53 +00001647 ret = snd_soc_register_card(card);
1648 if (ret != 0) {
1649 dev_err(&pdev->dev, "Failed to register card\n");
1650 return ret;
1651 }
1652
1653 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001654}
1655
1656/* removes a socdev */
1657static int soc_remove(struct platform_device *pdev)
1658{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001659 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001663
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001664 /* make sure any delayed work runs */
1665 for (i = 0; i < card->num_rtd; i++) {
1666 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1667 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001668 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001669
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001670 /* remove and free each DAI */
1671 for (i = 0; i < card->num_rtd; i++)
1672 soc_remove_dai_link(card, i);
1673
1674 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001675 if (card->remove)
1676 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677
1678 kfree(card->rtd);
1679 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001680 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001681 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001682 return 0;
1683}
1684
Mark Brown416356f2009-06-30 19:05:15 +01001685static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001686{
Mark Brown416356f2009-06-30 19:05:15 +01001687 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 struct snd_soc_card *card = platform_get_drvdata(pdev);
1689 int i;
Mark Brown51737472009-06-22 13:16:51 +01001690
1691 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001692 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001693
1694 /* Flush out pmdown_time work - we actually do want to run it
1695 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 for (i = 0; i < card->num_rtd; i++) {
1697 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1698 run_delayed_work(&rtd->delayed_work);
1699 }
Mark Brown51737472009-06-22 13:16:51 +01001700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001701 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001702
1703 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001704}
1705
Alexey Dobriyan47145212009-12-14 18:00:08 -08001706static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001707 .suspend = soc_suspend,
1708 .resume = soc_resume,
1709 .poweroff = soc_poweroff,
1710};
1711
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001712/* ASoC platform driver */
1713static struct platform_driver soc_driver = {
1714 .driver = {
1715 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001716 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001717 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001718 },
1719 .probe = soc_probe,
1720 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001721};
1722
1723/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001725{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 struct snd_soc_codec *codec = rtd->codec;
1727 struct snd_soc_platform *platform = rtd->platform;
1728 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1729 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001730 struct snd_pcm *pcm;
1731 char new_name[64];
1732 int ret = 0, playback = 0, capture = 0;
1733
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001735 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001736 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001738 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001739 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001741 capture = 1;
1742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001743 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1744 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1745 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748 return ret;
1749 }
1750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1754 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1755 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1756 soc_pcm_ops.copy = platform->driver->ops->copy;
1757 soc_pcm_ops.silence = platform->driver->ops->silence;
1758 soc_pcm_ops.ack = platform->driver->ops->ack;
1759 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760
1761 if (playback)
1762 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1763
1764 if (capture)
1765 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001767 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001768 if (ret < 0) {
1769 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001770 return ret;
1771 }
1772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001774 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1775 cpu_dai->name);
1776 return ret;
1777}
1778
Mark Brown096e49d2009-07-05 15:12:22 +01001779/**
1780 * snd_soc_codec_volatile_register: Report if a register is volatile.
1781 *
1782 * @codec: CODEC to query.
1783 * @reg: Register to query.
1784 *
1785 * Boolean function indiciating if a CODEC register is volatile.
1786 */
1787int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1788{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789 if (codec->driver->volatile_register)
1790 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001791 else
1792 return 0;
1793}
1794EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1795
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001796/**
1797 * snd_soc_new_ac97_codec - initailise AC97 device
1798 * @codec: audio codec
1799 * @ops: AC97 bus operations
1800 * @num: AC97 codec number
1801 *
1802 * Initialises AC97 codec resources for use by ad-hoc devices only.
1803 */
1804int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1805 struct snd_ac97_bus_ops *ops, int num)
1806{
1807 mutex_lock(&codec->mutex);
1808
1809 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1810 if (codec->ac97 == NULL) {
1811 mutex_unlock(&codec->mutex);
1812 return -ENOMEM;
1813 }
1814
1815 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1816 if (codec->ac97->bus == NULL) {
1817 kfree(codec->ac97);
1818 codec->ac97 = NULL;
1819 mutex_unlock(&codec->mutex);
1820 return -ENOMEM;
1821 }
1822
1823 codec->ac97->bus->ops = ops;
1824 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001825
1826 /*
1827 * Mark the AC97 device to be created by us. This way we ensure that the
1828 * device will be registered with the device subsystem later on.
1829 */
1830 codec->ac97_created = 1;
1831
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832 mutex_unlock(&codec->mutex);
1833 return 0;
1834}
1835EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1836
1837/**
1838 * snd_soc_free_ac97_codec - free AC97 codec device
1839 * @codec: audio codec
1840 *
1841 * Frees AC97 codec device resources.
1842 */
1843void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1844{
1845 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846#ifdef CONFIG_SND_SOC_AC97_BUS
1847 soc_unregister_ac97_dai_link(codec);
1848#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849 kfree(codec->ac97->bus);
1850 kfree(codec->ac97);
1851 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001852 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001853 mutex_unlock(&codec->mutex);
1854}
1855EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1856
1857/**
1858 * snd_soc_update_bits - update codec register bits
1859 * @codec: audio codec
1860 * @reg: codec register
1861 * @mask: register mask
1862 * @value: new value
1863 *
1864 * Writes new register value.
1865 *
1866 * Returns 1 for change else 0.
1867 */
1868int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001869 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001870{
1871 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001872 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 old = snd_soc_read(codec, reg);
1875 new = (old & ~mask) | value;
1876 change = old != new;
1877 if (change)
1878 snd_soc_write(codec, reg, new);
1879
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880 return change;
1881}
1882EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1883
1884/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001885 * snd_soc_update_bits_locked - update codec register bits
1886 * @codec: audio codec
1887 * @reg: codec register
1888 * @mask: register mask
1889 * @value: new value
1890 *
1891 * Writes new register value, and takes the codec mutex.
1892 *
1893 * Returns 1 for change else 0.
1894 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001895int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1896 unsigned short reg, unsigned int mask,
1897 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001898{
1899 int change;
1900
1901 mutex_lock(&codec->mutex);
1902 change = snd_soc_update_bits(codec, reg, mask, value);
1903 mutex_unlock(&codec->mutex);
1904
1905 return change;
1906}
Mark Browndd1b3d52009-12-04 14:22:03 +00001907EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001908
1909/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001910 * snd_soc_test_bits - test register for change
1911 * @codec: audio codec
1912 * @reg: codec register
1913 * @mask: register mask
1914 * @value: new value
1915 *
1916 * Tests a register with a new value and checks if the new value is
1917 * different from the old value.
1918 *
1919 * Returns 1 for change else 0.
1920 */
1921int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001922 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001923{
1924 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001925 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001926
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927 old = snd_soc_read(codec, reg);
1928 new = (old & ~mask) | value;
1929 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930
1931 return change;
1932}
1933EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1934
1935/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1937 * @substream: the pcm substream
1938 * @hw: the hardware parameters
1939 *
1940 * Sets the substream runtime hardware parameters.
1941 */
1942int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1943 const struct snd_pcm_hardware *hw)
1944{
1945 struct snd_pcm_runtime *runtime = substream->runtime;
1946 runtime->hw.info = hw->info;
1947 runtime->hw.formats = hw->formats;
1948 runtime->hw.period_bytes_min = hw->period_bytes_min;
1949 runtime->hw.period_bytes_max = hw->period_bytes_max;
1950 runtime->hw.periods_min = hw->periods_min;
1951 runtime->hw.periods_max = hw->periods_max;
1952 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1953 runtime->hw.fifo_size = hw->fifo_size;
1954 return 0;
1955}
1956EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1957
1958/**
1959 * snd_soc_cnew - create new control
1960 * @_template: control template
1961 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001962 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963 *
1964 * Create a new mixer control from a template control.
1965 *
1966 * Returns 0 for success, else error.
1967 */
1968struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1969 void *data, char *long_name)
1970{
1971 struct snd_kcontrol_new template;
1972
1973 memcpy(&template, _template, sizeof(template));
1974 if (long_name)
1975 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976 template.index = 0;
1977
1978 return snd_ctl_new1(&template, data);
1979}
1980EXPORT_SYMBOL_GPL(snd_soc_cnew);
1981
1982/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001983 * snd_soc_add_controls - add an array of controls to a codec.
1984 * Convienience function to add a list of controls. Many codecs were
1985 * duplicating this code.
1986 *
1987 * @codec: codec to add controls to
1988 * @controls: array of controls to add
1989 * @num_controls: number of elements in the array
1990 *
1991 * Return 0 for success, else error.
1992 */
1993int snd_soc_add_controls(struct snd_soc_codec *codec,
1994 const struct snd_kcontrol_new *controls, int num_controls)
1995{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001997 int err, i;
1998
1999 for (i = 0; i < num_controls; i++) {
2000 const struct snd_kcontrol_new *control = &controls[i];
2001 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2002 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002003 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2004 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002005 return err;
2006 }
2007 }
2008
2009 return 0;
2010}
2011EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2012
2013/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 * snd_soc_info_enum_double - enumerated double mixer info callback
2015 * @kcontrol: mixer control
2016 * @uinfo: control element information
2017 *
2018 * Callback to provide information about a double enumerated
2019 * mixer control.
2020 *
2021 * Returns 0 for success.
2022 */
2023int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2024 struct snd_ctl_elem_info *uinfo)
2025{
2026 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2027
2028 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2029 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002030 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002032 if (uinfo->value.enumerated.item > e->max - 1)
2033 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034 strcpy(uinfo->value.enumerated.name,
2035 e->texts[uinfo->value.enumerated.item]);
2036 return 0;
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2039
2040/**
2041 * snd_soc_get_enum_double - enumerated double mixer get callback
2042 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002043 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002044 *
2045 * Callback to get the value of a double enumerated mixer.
2046 *
2047 * Returns 0 for success.
2048 */
2049int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_value *ucontrol)
2051{
2052 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2053 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002054 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002055
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002056 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057 ;
2058 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002059 ucontrol->value.enumerated.item[0]
2060 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 if (e->shift_l != e->shift_r)
2062 ucontrol->value.enumerated.item[1] =
2063 (val >> e->shift_r) & (bitmask - 1);
2064
2065 return 0;
2066}
2067EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2068
2069/**
2070 * snd_soc_put_enum_double - enumerated double mixer put callback
2071 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002072 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002073 *
2074 * Callback to set the value of a double enumerated mixer.
2075 *
2076 * Returns 0 for success.
2077 */
2078int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2079 struct snd_ctl_elem_value *ucontrol)
2080{
2081 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2082 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002083 unsigned int val;
2084 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002085
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002086 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002088 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002089 return -EINVAL;
2090 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2091 mask = (bitmask - 1) << e->shift_l;
2092 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002093 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094 return -EINVAL;
2095 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2096 mask |= (bitmask - 1) << e->shift_r;
2097 }
2098
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002099 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100}
2101EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2102
2103/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002104 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2105 * @kcontrol: mixer control
2106 * @ucontrol: control element information
2107 *
2108 * Callback to get the value of a double semi enumerated mixer.
2109 *
2110 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2111 * used for handling bitfield coded enumeration for example.
2112 *
2113 * Returns 0 for success.
2114 */
2115int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
2117{
2118 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002119 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002120 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002121
2122 reg_val = snd_soc_read(codec, e->reg);
2123 val = (reg_val >> e->shift_l) & e->mask;
2124 for (mux = 0; mux < e->max; mux++) {
2125 if (val == e->values[mux])
2126 break;
2127 }
2128 ucontrol->value.enumerated.item[0] = mux;
2129 if (e->shift_l != e->shift_r) {
2130 val = (reg_val >> e->shift_r) & e->mask;
2131 for (mux = 0; mux < e->max; mux++) {
2132 if (val == e->values[mux])
2133 break;
2134 }
2135 ucontrol->value.enumerated.item[1] = mux;
2136 }
2137
2138 return 0;
2139}
2140EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2141
2142/**
2143 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2144 * @kcontrol: mixer control
2145 * @ucontrol: control element information
2146 *
2147 * Callback to set the value of a double semi enumerated mixer.
2148 *
2149 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2150 * used for handling bitfield coded enumeration for example.
2151 *
2152 * Returns 0 for success.
2153 */
2154int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
2157 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002158 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002159 unsigned int val;
2160 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002161
2162 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2163 return -EINVAL;
2164 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2165 mask = e->mask << e->shift_l;
2166 if (e->shift_l != e->shift_r) {
2167 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2168 return -EINVAL;
2169 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2170 mask |= e->mask << e->shift_r;
2171 }
2172
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002173 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002174}
2175EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2176
2177/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2179 * @kcontrol: mixer control
2180 * @uinfo: control element information
2181 *
2182 * Callback to provide information about an external enumerated
2183 * single mixer.
2184 *
2185 * Returns 0 for success.
2186 */
2187int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_info *uinfo)
2189{
2190 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2191
2192 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2193 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002194 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002196 if (uinfo->value.enumerated.item > e->max - 1)
2197 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198 strcpy(uinfo->value.enumerated.name,
2199 e->texts[uinfo->value.enumerated.item]);
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2203
2204/**
2205 * snd_soc_info_volsw_ext - external single mixer info callback
2206 * @kcontrol: mixer control
2207 * @uinfo: control element information
2208 *
2209 * Callback to provide information about a single external mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_info *uinfo)
2215{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002216 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217
Mark Brownfd5dfad2009-04-15 21:37:46 +01002218 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002219 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2220 else
2221 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2222
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002223 uinfo->count = 1;
2224 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002225 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 return 0;
2227}
2228EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2229
2230/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231 * snd_soc_info_volsw - single mixer info callback
2232 * @kcontrol: mixer control
2233 * @uinfo: control element information
2234 *
2235 * Callback to provide information about a single mixer control.
2236 *
2237 * Returns 0 for success.
2238 */
2239int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_info *uinfo)
2241{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002242 struct soc_mixer_control *mc =
2243 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002244 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002245 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002246 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002248 if (!mc->platform_max)
2249 mc->platform_max = mc->max;
2250 platform_max = mc->platform_max;
2251
2252 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002253 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2254 else
2255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2256
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257 uinfo->count = shift == rshift ? 1 : 2;
2258 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002259 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2263
2264/**
2265 * snd_soc_get_volsw - single mixer get callback
2266 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002267 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268 *
2269 * Callback to get the value of a single mixer control.
2270 *
2271 * Returns 0 for success.
2272 */
2273int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2274 struct snd_ctl_elem_value *ucontrol)
2275{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002276 struct soc_mixer_control *mc =
2277 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002279 unsigned int reg = mc->reg;
2280 unsigned int shift = mc->shift;
2281 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002282 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002283 unsigned int mask = (1 << fls(max)) - 1;
2284 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002285
2286 ucontrol->value.integer.value[0] =
2287 (snd_soc_read(codec, reg) >> shift) & mask;
2288 if (shift != rshift)
2289 ucontrol->value.integer.value[1] =
2290 (snd_soc_read(codec, reg) >> rshift) & mask;
2291 if (invert) {
2292 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002293 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002294 if (shift != rshift)
2295 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002296 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 }
2298
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2302
2303/**
2304 * snd_soc_put_volsw - single mixer put 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 set the value of a single mixer control.
2309 *
2310 * Returns 0 for success.
2311 */
2312int snd_soc_put_volsw(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 shift = mc->shift;
2320 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002321 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002322 unsigned int mask = (1 << fls(max)) - 1;
2323 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002324 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325
2326 val = (ucontrol->value.integer.value[0] & mask);
2327 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002328 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329 val_mask = mask << shift;
2330 val = val << shift;
2331 if (shift != rshift) {
2332 val2 = (ucontrol->value.integer.value[1] & mask);
2333 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002334 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335 val_mask |= mask << rshift;
2336 val |= val2 << rshift;
2337 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002338 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339}
2340EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2341
2342/**
2343 * snd_soc_info_volsw_2r - double mixer info callback
2344 * @kcontrol: mixer control
2345 * @uinfo: control element information
2346 *
2347 * Callback to provide information about a double mixer control that
2348 * spans 2 codec registers.
2349 *
2350 * Returns 0 for success.
2351 */
2352int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2354{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002355 struct soc_mixer_control *mc =
2356 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002357 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002359 if (!mc->platform_max)
2360 mc->platform_max = mc->max;
2361 platform_max = mc->platform_max;
2362
2363 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002364 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2365 else
2366 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2367
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 uinfo->count = 2;
2369 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002370 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371 return 0;
2372}
2373EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2374
2375/**
2376 * snd_soc_get_volsw_2r - double mixer get callback
2377 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002378 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002379 *
2380 * Callback to get the value of a double mixer control that spans 2 registers.
2381 *
2382 * Returns 0 for success.
2383 */
2384int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2386{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002387 struct soc_mixer_control *mc =
2388 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002390 unsigned int reg = mc->reg;
2391 unsigned int reg2 = mc->rreg;
2392 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002393 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002394 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002395 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396
2397 ucontrol->value.integer.value[0] =
2398 (snd_soc_read(codec, reg) >> shift) & mask;
2399 ucontrol->value.integer.value[1] =
2400 (snd_soc_read(codec, reg2) >> shift) & mask;
2401 if (invert) {
2402 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002403 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002405 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 }
2407
2408 return 0;
2409}
2410EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2411
2412/**
2413 * snd_soc_put_volsw_2r - double mixer set callback
2414 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002415 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 *
2417 * Callback to set the value of a double mixer control that spans 2 registers.
2418 *
2419 * Returns 0 for success.
2420 */
2421int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002424 struct soc_mixer_control *mc =
2425 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002426 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002427 unsigned int reg = mc->reg;
2428 unsigned int reg2 = mc->rreg;
2429 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002430 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002431 unsigned int mask = (1 << fls(max)) - 1;
2432 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002434 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435
2436 val_mask = mask << shift;
2437 val = (ucontrol->value.integer.value[0] & mask);
2438 val2 = (ucontrol->value.integer.value[1] & mask);
2439
2440 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002441 val = max - val;
2442 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 }
2444
2445 val = val << shift;
2446 val2 = val2 << shift;
2447
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002448 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002449 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450 return err;
2451
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002452 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002453 return err;
2454}
2455EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2456
Mark Browne13ac2e2008-05-28 17:58:05 +01002457/**
2458 * snd_soc_info_volsw_s8 - signed mixer info callback
2459 * @kcontrol: mixer control
2460 * @uinfo: control element information
2461 *
2462 * Callback to provide information about a signed mixer control.
2463 *
2464 * Returns 0 for success.
2465 */
2466int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_info *uinfo)
2468{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002469 struct soc_mixer_control *mc =
2470 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002471 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002472 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002473
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002474 if (!mc->platform_max)
2475 mc->platform_max = mc->max;
2476 platform_max = mc->platform_max;
2477
Mark Browne13ac2e2008-05-28 17:58:05 +01002478 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2479 uinfo->count = 2;
2480 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002481 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002482 return 0;
2483}
2484EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2485
2486/**
2487 * snd_soc_get_volsw_s8 - signed mixer get callback
2488 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002489 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002490 *
2491 * Callback to get the value of a signed mixer control.
2492 *
2493 * Returns 0 for success.
2494 */
2495int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
2497{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002498 struct soc_mixer_control *mc =
2499 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002500 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002501 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002502 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002503 int val = snd_soc_read(codec, reg);
2504
2505 ucontrol->value.integer.value[0] =
2506 ((signed char)(val & 0xff))-min;
2507 ucontrol->value.integer.value[1] =
2508 ((signed char)((val >> 8) & 0xff))-min;
2509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2512
2513/**
2514 * snd_soc_put_volsw_sgn - signed mixer put callback
2515 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002516 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002517 *
2518 * Callback to set the value of a signed mixer control.
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002525 struct soc_mixer_control *mc =
2526 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002527 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002528 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002529 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002530 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002531
2532 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2533 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2534
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002535 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002536}
2537EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2538
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002539/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002540 * snd_soc_limit_volume - Set new limit to an existing volume control.
2541 *
2542 * @codec: where to look for the control
2543 * @name: Name of the control
2544 * @max: new maximum limit
2545 *
2546 * Return 0 for success, else error.
2547 */
2548int snd_soc_limit_volume(struct snd_soc_codec *codec,
2549 const char *name, int max)
2550{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002551 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002552 struct snd_kcontrol *kctl;
2553 struct soc_mixer_control *mc;
2554 int found = 0;
2555 int ret = -EINVAL;
2556
2557 /* Sanity check for name and max */
2558 if (unlikely(!name || max <= 0))
2559 return -EINVAL;
2560
2561 list_for_each_entry(kctl, &card->controls, list) {
2562 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2563 found = 1;
2564 break;
2565 }
2566 }
2567 if (found) {
2568 mc = (struct soc_mixer_control *)kctl->private_value;
2569 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002570 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002571 ret = 0;
2572 }
2573 }
2574 return ret;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2577
2578/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002579 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2580 * mixer info callback
2581 * @kcontrol: mixer control
2582 * @uinfo: control element information
2583 *
2584 * Returns 0 for success.
2585 */
2586int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_info *uinfo)
2588{
2589 struct soc_mixer_control *mc =
2590 (struct soc_mixer_control *)kcontrol->private_value;
2591 int max = mc->max;
2592 int min = mc->min;
2593
2594 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2595 uinfo->count = 2;
2596 uinfo->value.integer.min = 0;
2597 uinfo->value.integer.max = max-min;
2598
2599 return 0;
2600}
2601EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2602
2603/**
2604 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2605 * mixer get callback
2606 * @kcontrol: mixer control
2607 * @uinfo: control element information
2608 *
2609 * Returns 0 for success.
2610 */
2611int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
2613{
2614 struct soc_mixer_control *mc =
2615 (struct soc_mixer_control *)kcontrol->private_value;
2616 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2617 unsigned int mask = (1<<mc->shift)-1;
2618 int min = mc->min;
2619 int val = snd_soc_read(codec, mc->reg) & mask;
2620 int valr = snd_soc_read(codec, mc->rreg) & mask;
2621
Stuart Longland20630c72010-06-18 12:56:10 +10002622 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2623 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002624 return 0;
2625}
2626EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2627
2628/**
2629 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2630 * mixer put callback
2631 * @kcontrol: mixer control
2632 * @uinfo: control element information
2633 *
2634 * Returns 0 for success.
2635 */
2636int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2638{
2639 struct soc_mixer_control *mc =
2640 (struct soc_mixer_control *)kcontrol->private_value;
2641 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2642 unsigned int mask = (1<<mc->shift)-1;
2643 int min = mc->min;
2644 int ret;
2645 unsigned int val, valr, oval, ovalr;
2646
2647 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2648 val &= mask;
2649 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2650 valr &= mask;
2651
2652 oval = snd_soc_read(codec, mc->reg) & mask;
2653 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2654
2655 ret = 0;
2656 if (oval != val) {
2657 ret = snd_soc_write(codec, mc->reg, val);
2658 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002659 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002660 }
2661 if (ovalr != valr) {
2662 ret = snd_soc_write(codec, mc->rreg, valr);
2663 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002664 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002665 }
2666
2667 return 0;
2668}
2669EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2670
2671/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002672 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2673 * @dai: DAI
2674 * @clk_id: DAI specific clock ID
2675 * @freq: new clock frequency in Hz
2676 * @dir: new clock direction - input/output.
2677 *
2678 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2679 */
2680int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2681 unsigned int freq, int dir)
2682{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002683 if (dai->driver && dai->driver->ops->set_sysclk)
2684 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002685 else
2686 return -EINVAL;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2689
2690/**
2691 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2692 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002693 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002694 * @div: new clock divisor.
2695 *
2696 * Configures the clock dividers. This is used to derive the best DAI bit and
2697 * frame clocks from the system or master clock. It's best to set the DAI bit
2698 * and frame clocks as low as possible to save system power.
2699 */
2700int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2701 int div_id, int div)
2702{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002703 if (dai->driver && dai->driver->ops->set_clkdiv)
2704 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002705 else
2706 return -EINVAL;
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2709
2710/**
2711 * snd_soc_dai_set_pll - configure DAI PLL.
2712 * @dai: DAI
2713 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002714 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002715 * @freq_in: PLL input clock frequency in Hz
2716 * @freq_out: requested PLL output clock frequency in Hz
2717 *
2718 * Configures and enables PLL to generate output clock based on input clock.
2719 */
Mark Brown85488032009-09-05 18:52:16 +01002720int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2721 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002722{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002723 if (dai->driver && dai->driver->ops->set_pll)
2724 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002725 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726 else
2727 return -EINVAL;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2730
2731/**
2732 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2733 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002734 * @fmt: SND_SOC_DAIFMT_ format value.
2735 *
2736 * Configures the DAI hardware format and clocking.
2737 */
2738int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2739{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002740 if (dai->driver && dai->driver->ops->set_fmt)
2741 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002742 else
2743 return -EINVAL;
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2746
2747/**
2748 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2749 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002750 * @tx_mask: bitmask representing active TX slots.
2751 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002752 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002753 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002754 *
2755 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2756 * specific.
2757 */
2758int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002759 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002760{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002761 if (dai->driver && dai->driver->ops->set_tdm_slot)
2762 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002763 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002764 else
2765 return -EINVAL;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2768
2769/**
Barry Song472df3c2009-09-12 01:16:29 +08002770 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2771 * @dai: DAI
2772 * @tx_num: how many TX channels
2773 * @tx_slot: pointer to an array which imply the TX slot number channel
2774 * 0~num-1 uses
2775 * @rx_num: how many RX channels
2776 * @rx_slot: pointer to an array which imply the RX slot number channel
2777 * 0~num-1 uses
2778 *
2779 * configure the relationship between channel number and TDM slot number.
2780 */
2781int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2782 unsigned int tx_num, unsigned int *tx_slot,
2783 unsigned int rx_num, unsigned int *rx_slot)
2784{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002785 if (dai->driver && dai->driver->ops->set_channel_map)
2786 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002787 rx_num, rx_slot);
2788 else
2789 return -EINVAL;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2792
2793/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002794 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2795 * @dai: DAI
2796 * @tristate: tristate enable
2797 *
2798 * Tristates the DAI so that others can use it.
2799 */
2800int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2801{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002802 if (dai->driver && dai->driver->ops->set_tristate)
2803 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002804 else
2805 return -EINVAL;
2806}
2807EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2808
2809/**
2810 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2811 * @dai: DAI
2812 * @mute: mute enable
2813 *
2814 * Mutes the DAI DAC.
2815 */
2816int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2817{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002818 if (dai->driver && dai->driver->ops->digital_mute)
2819 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002820 else
2821 return -EINVAL;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2824
Mark Brownc5af3a22008-11-28 13:29:45 +00002825/**
2826 * snd_soc_register_card - Register a card with the ASoC core
2827 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002828 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002829 *
2830 * Note that currently this is an internal only function: it will be
2831 * exposed to machine drivers after further backporting of ASoC v2
2832 * registration APIs.
2833 */
2834static int snd_soc_register_card(struct snd_soc_card *card)
2835{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002836 int i;
2837
Mark Brownc5af3a22008-11-28 13:29:45 +00002838 if (!card->name || !card->dev)
2839 return -EINVAL;
2840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002841 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2842 GFP_KERNEL);
2843 if (card->rtd == NULL)
2844 return -ENOMEM;
2845
2846 for (i = 0; i < card->num_links; i++)
2847 card->rtd[i].dai_link = &card->dai_link[i];
2848
Mark Brownc5af3a22008-11-28 13:29:45 +00002849 INIT_LIST_HEAD(&card->list);
2850 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002851 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002852
2853 mutex_lock(&client_mutex);
2854 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002855 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002856 mutex_unlock(&client_mutex);
2857
2858 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2859
2860 return 0;
2861}
2862
2863/**
2864 * snd_soc_unregister_card - Unregister a card with the ASoC core
2865 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002866 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002867 *
2868 * Note that currently this is an internal only function: it will be
2869 * exposed to machine drivers after further backporting of ASoC v2
2870 * registration APIs.
2871 */
2872static int snd_soc_unregister_card(struct snd_soc_card *card)
2873{
2874 mutex_lock(&client_mutex);
2875 list_del(&card->list);
2876 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002877 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2878
2879 return 0;
2880}
2881
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002882/*
2883 * Simplify DAI link configuration by removing ".-1" from device names
2884 * and sanitizing names.
2885 */
2886static inline char *fmt_single_name(struct device *dev, int *id)
2887{
2888 char *found, name[NAME_SIZE];
2889 int id1, id2;
2890
2891 if (dev_name(dev) == NULL)
2892 return NULL;
2893
2894 strncpy(name, dev_name(dev), NAME_SIZE);
2895
2896 /* are we a "%s.%d" name (platform and SPI components) */
2897 found = strstr(name, dev->driver->name);
2898 if (found) {
2899 /* get ID */
2900 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2901
2902 /* discard ID from name if ID == -1 */
2903 if (*id == -1)
2904 found[strlen(dev->driver->name)] = '\0';
2905 }
2906
2907 } else {
2908 /* I2C component devices are named "bus-addr" */
2909 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2910 char tmp[NAME_SIZE];
2911
2912 /* create unique ID number from I2C addr and bus */
2913 *id = ((id1 && 0xffff) << 16) + id2;
2914
2915 /* sanitize component name for DAI link creation */
2916 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2917 strncpy(name, tmp, NAME_SIZE);
2918 } else
2919 *id = 0;
2920 }
2921
2922 return kstrdup(name, GFP_KERNEL);
2923}
2924
2925/*
2926 * Simplify DAI link naming for single devices with multiple DAIs by removing
2927 * any ".-1" and using the DAI name (instead of device name).
2928 */
2929static inline char *fmt_multiple_name(struct device *dev,
2930 struct snd_soc_dai_driver *dai_drv)
2931{
2932 if (dai_drv->name == NULL) {
2933 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2934 dev_name(dev));
2935 return NULL;
2936 }
2937
2938 return kstrdup(dai_drv->name, GFP_KERNEL);
2939}
2940
Mark Brown91151712008-11-30 23:31:24 +00002941/**
2942 * snd_soc_register_dai - Register a DAI with the ASoC core
2943 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002944 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002945 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002946int snd_soc_register_dai(struct device *dev,
2947 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002948{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002951 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2954 if (dai == NULL)
2955 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002956
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002957 /* create DAI component name */
2958 dai->name = fmt_single_name(dev, &dai->id);
2959 if (dai->name == NULL) {
2960 kfree(dai);
2961 return -ENOMEM;
2962 }
2963
2964 dai->dev = dev;
2965 dai->driver = dai_drv;
2966 if (!dai->driver->ops)
2967 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002968
2969 mutex_lock(&client_mutex);
2970 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002971 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002972 mutex_unlock(&client_mutex);
2973
2974 pr_debug("Registered DAI '%s'\n", dai->name);
2975
2976 return 0;
2977}
2978EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2979
2980/**
2981 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2982 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002983 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002984 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002985void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002986{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002987 struct snd_soc_dai *dai;
2988
2989 list_for_each_entry(dai, &dai_list, list) {
2990 if (dev == dai->dev)
2991 goto found;
2992 }
2993 return;
2994
2995found:
Mark Brown91151712008-11-30 23:31:24 +00002996 mutex_lock(&client_mutex);
2997 list_del(&dai->list);
2998 mutex_unlock(&client_mutex);
2999
3000 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003001 kfree(dai->name);
3002 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003003}
3004EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3005
3006/**
3007 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3008 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003009 * @dai: Array of DAIs to register
3010 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003011 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003012int snd_soc_register_dais(struct device *dev,
3013 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003014{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015 struct snd_soc_dai *dai;
3016 int i, ret = 0;
3017
Liam Girdwood720ffa42010-08-18 00:30:30 +01003018 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003019
3020 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003021
3022 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3023 if (dai == NULL)
3024 return -ENOMEM;
3025
3026 /* create DAI component name */
3027 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3028 if (dai->name == NULL) {
3029 kfree(dai);
3030 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003031 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003032 }
3033
3034 dai->dev = dev;
3035 dai->id = i;
3036 dai->driver = &dai_drv[i];
3037 if (!dai->driver->ops)
3038 dai->driver->ops = &null_dai_ops;
3039
3040 mutex_lock(&client_mutex);
3041 list_add(&dai->list, &dai_list);
3042 mutex_unlock(&client_mutex);
3043
3044 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003045 }
3046
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003047 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003048 return 0;
3049
3050err:
3051 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003052 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003053
3054 return ret;
3055}
3056EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3057
3058/**
3059 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3060 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003061 * @dai: Array of DAIs to unregister
3062 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003063 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003064void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003065{
3066 int i;
3067
3068 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003069 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003070}
3071EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3072
Mark Brown12a48a8c2008-12-03 19:40:30 +00003073/**
3074 * snd_soc_register_platform - Register a platform with the ASoC core
3075 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003076 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003077 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003078int snd_soc_register_platform(struct device *dev,
3079 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003080{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003081 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003083 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3084
3085 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3086 if (platform == NULL)
3087 return -ENOMEM;
3088
3089 /* create platform component name */
3090 platform->name = fmt_single_name(dev, &platform->id);
3091 if (platform->name == NULL) {
3092 kfree(platform);
3093 return -ENOMEM;
3094 }
3095
3096 platform->dev = dev;
3097 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003098
3099 mutex_lock(&client_mutex);
3100 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003101 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003102 mutex_unlock(&client_mutex);
3103
3104 pr_debug("Registered platform '%s'\n", platform->name);
3105
3106 return 0;
3107}
3108EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3109
3110/**
3111 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3112 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003113 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003114 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003116{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003117 struct snd_soc_platform *platform;
3118
3119 list_for_each_entry(platform, &platform_list, list) {
3120 if (dev == platform->dev)
3121 goto found;
3122 }
3123 return;
3124
3125found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003126 mutex_lock(&client_mutex);
3127 list_del(&platform->list);
3128 mutex_unlock(&client_mutex);
3129
3130 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003131 kfree(platform->name);
3132 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003133}
3134EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3135
Mark Brown151ab222009-05-09 16:22:58 +01003136static u64 codec_format_map[] = {
3137 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3138 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3139 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3140 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3141 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3142 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3143 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3144 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3145 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3146 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3147 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3148 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3149 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3150 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3151 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3152 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3153};
3154
3155/* Fix up the DAI formats for endianness: codecs don't actually see
3156 * the endianness of the data but we're using the CPU format
3157 * definitions which do need to include endianness so we ensure that
3158 * codec DAIs always have both big and little endian variants set.
3159 */
3160static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3161{
3162 int i;
3163
3164 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3165 if (stream->formats & codec_format_map[i])
3166 stream->formats |= codec_format_map[i];
3167}
3168
Mark Brown0d0cf002008-12-10 14:32:45 +00003169/**
3170 * snd_soc_register_codec - Register a codec with the ASoC core
3171 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003172 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003173 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003174int snd_soc_register_codec(struct device *dev,
3175 struct snd_soc_codec_driver *codec_drv,
3176 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003177{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003178 struct snd_soc_codec *codec;
3179 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003180
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003181 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003183 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3184 if (codec == NULL)
3185 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187 /* create CODEC component name */
3188 codec->name = fmt_single_name(dev, &codec->id);
3189 if (codec->name == NULL) {
3190 kfree(codec);
3191 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003192 }
3193
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003194 /* allocate CODEC register cache */
3195 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3196
3197 if (codec_drv->reg_cache_default)
3198 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3199 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3200 else
3201 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3202 codec_drv->reg_word_size, GFP_KERNEL);
3203
3204 if (codec->reg_cache == NULL) {
3205 kfree(codec->name);
3206 kfree(codec);
3207 return -ENOMEM;
3208 }
3209 }
3210
3211 codec->dev = dev;
3212 codec->driver = codec_drv;
3213 codec->bias_level = SND_SOC_BIAS_OFF;
3214 codec->num_dai = num_dai;
3215 mutex_init(&codec->mutex);
3216 INIT_LIST_HEAD(&codec->dapm_widgets);
3217 INIT_LIST_HEAD(&codec->dapm_paths);
3218
3219 for (i = 0; i < num_dai; i++) {
3220 fixup_codec_formats(&dai_drv[i].playback);
3221 fixup_codec_formats(&dai_drv[i].capture);
3222 }
3223
Mark Brown26b01cc2010-08-18 20:20:55 +01003224 /* register any DAIs */
3225 if (num_dai) {
3226 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3227 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003228 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003229 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003230
Mark Brown0d0cf002008-12-10 14:32:45 +00003231 mutex_lock(&client_mutex);
3232 list_add(&codec->list, &codec_list);
3233 snd_soc_instantiate_cards();
3234 mutex_unlock(&client_mutex);
3235
3236 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003237 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003238
3239error:
3240 for (i--; i >= 0; i--)
3241 snd_soc_unregister_dai(dev);
3242
3243 if (codec->reg_cache)
3244 kfree(codec->reg_cache);
3245 kfree(codec->name);
3246 kfree(codec);
3247 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003248}
3249EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3250
3251/**
3252 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3253 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003254 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003255 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003256void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003257{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258 struct snd_soc_codec *codec;
3259 int i;
3260
3261 list_for_each_entry(codec, &codec_list, list) {
3262 if (dev == codec->dev)
3263 goto found;
3264 }
3265 return;
3266
3267found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003268 if (codec->num_dai)
3269 for (i = 0; i < codec->num_dai; i++)
3270 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271
Mark Brown0d0cf002008-12-10 14:32:45 +00003272 mutex_lock(&client_mutex);
3273 list_del(&codec->list);
3274 mutex_unlock(&client_mutex);
3275
3276 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003277
3278 if (codec->reg_cache)
3279 kfree(codec->reg_cache);
3280 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003281}
3282EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3283
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003284static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003285{
Mark Brown384c89e2008-12-03 17:34:03 +00003286#ifdef CONFIG_DEBUG_FS
3287 debugfs_root = debugfs_create_dir("asoc", NULL);
3288 if (IS_ERR(debugfs_root) || !debugfs_root) {
3289 printk(KERN_WARNING
3290 "ASoC: Failed to create debugfs directory\n");
3291 debugfs_root = NULL;
3292 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003293
3294 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3295 &codec_list_fops))
3296 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3297
Mark Brownf3208782010-09-15 18:19:07 +01003298 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3299 &dai_list_fops))
3300 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003301
3302 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3303 &platform_list_fops))
3304 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003305#endif
3306
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003307 return platform_driver_register(&soc_driver);
3308}
3309
Mark Brown7d8c16a2008-11-30 22:11:24 +00003310static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003311{
Mark Brown384c89e2008-12-03 17:34:03 +00003312#ifdef CONFIG_DEBUG_FS
3313 debugfs_remove_recursive(debugfs_root);
3314#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003315 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003316}
3317
3318module_init(snd_soc_init);
3319module_exit(snd_soc_exit);
3320
3321/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003322MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003323MODULE_DESCRIPTION("ALSA SoC Core");
3324MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003325MODULE_ALIAS("platform:soc-audio");