blob: 63d93b3b7b7ad9e66951fe18568b63c41ceda0d1 [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 Brown2624d5f2009-11-03 21:56:13 +0000326#else
327
328static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
329{
330}
331
332static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
333{
334}
335#endif
336
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200337#ifdef CONFIG_SND_SOC_AC97_BUS
338/* unregister ac97 codec */
339static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
340{
341 if (codec->ac97->dev.bus)
342 device_unregister(&codec->ac97->dev);
343 return 0;
344}
345
346/* stop no dev release warning */
347static void soc_ac97_device_release(struct device *dev){}
348
349/* register ac97 codec to bus */
350static int soc_ac97_dev_register(struct snd_soc_codec *codec)
351{
352 int err;
353
354 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100355 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200356 codec->ac97->dev.release = soc_ac97_device_release;
357
Kay Sieversbb072bf2008-11-02 03:50:35 +0100358 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000359 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 err = device_register(&codec->ac97->dev);
361 if (err < 0) {
362 snd_printk(KERN_ERR "Can't register ac97 bus\n");
363 codec->ac97->dev.bus = NULL;
364 return err;
365 }
366 return 0;
367}
368#endif
369
Mark Brown06f409d2009-04-07 18:10:13 +0100370static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000373 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
374 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100375 int ret;
376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
378 rtd->dai_link->symmetric_rates) {
379 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
380 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100381
382 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
383 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000384 rtd->rate,
385 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100386 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100388 "Unable to apply rate symmetry constraint: %d\n", ret);
389 return ret;
390 }
391 }
392
393 return 0;
394}
395
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200396/*
397 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
398 * then initialized and any private data can be allocated. This also calls
399 * startup for the cpu DAI, platform, machine and codec DAI.
400 */
401static int soc_pcm_open(struct snd_pcm_substream *substream)
402{
403 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200404 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 struct snd_soc_platform *platform = rtd->platform;
406 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
407 struct snd_soc_dai *codec_dai = rtd->codec_dai;
408 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
409 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200410 int ret = 0;
411
412 mutex_lock(&pcm_mutex);
413
414 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415 if (cpu_dai->driver->ops->startup) {
416 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417 if (ret < 0) {
418 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100419 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200420 goto out;
421 }
422 }
423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 if (platform->driver->ops->open) {
425 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200426 if (ret < 0) {
427 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
428 goto platform_err;
429 }
430 }
431
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 if (codec_dai->driver->ops->startup) {
433 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100434 if (ret < 0) {
435 printk(KERN_ERR "asoc: can't open codec %s\n",
436 codec_dai->name);
437 goto codec_dai_err;
438 }
439 }
440
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000441 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
442 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000444 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200445 goto machine_err;
446 }
447 }
448
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 /* Check that the codec and cpu DAI's are compatible */
450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
451 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 max(codec_dai_drv->playback.rate_min,
453 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 min(codec_dai_drv->playback.rate_max,
456 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 max(codec_dai_drv->playback.channels_min,
459 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461 min(codec_dai_drv->playback.channels_max,
462 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100463 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100465 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
467 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900468 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 runtime->hw.rates |= cpu_dai_drv->playback.rates;
470 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900471 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 } else {
474 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 max(codec_dai_drv->capture.rate_min,
476 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 min(codec_dai_drv->capture.rate_max,
479 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200480 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000481 max(codec_dai_drv->capture.channels_min,
482 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 min(codec_dai_drv->capture.channels_max,
485 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100486 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
490 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900491 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 runtime->hw.rates |= cpu_dai_drv->capture.rates;
493 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900494 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 }
497
498 snd_pcm_limit_hw_rates(runtime);
499 if (!runtime->hw.rates) {
500 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100501 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900502 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503 }
504 if (!runtime->hw.formats) {
505 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100506 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900507 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 }
509 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
510 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900512 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 }
514
Mark Brown06f409d2009-04-07 18:10:13 +0100515 /* Symmetry only applies if we've already got an active stream. */
516 if (cpu_dai->active || codec_dai->active) {
517 ret = soc_pcm_apply_symmetry(substream);
518 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900519 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100520 }
521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 pr_debug("asoc: %s <-> %s info:\n",
523 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100524 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
525 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
526 runtime->hw.channels_max);
527 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
528 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529
Jassi Brar14dc5732010-02-26 09:12:32 +0900530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 cpu_dai->playback_active++;
532 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900533 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 cpu_dai->capture_active++;
535 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900536 }
537 cpu_dai->active++;
538 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 mutex_unlock(&pcm_mutex);
541 return 0;
542
Jassi Brarbb1c0472010-02-25 11:24:53 +0900543config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
545 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546
Jassi Brarbb1c0472010-02-25 11:24:53 +0900547machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 if (codec_dai->driver->ops->shutdown)
549 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900550
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 if (platform->driver->ops->close)
553 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
555platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 if (cpu_dai->driver->ops->shutdown)
557 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558out:
559 mutex_unlock(&pcm_mutex);
560 return ret;
561}
562
563/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200564 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565 * This is to ensure there are no pops or clicks in between any music tracks
566 * due to DAPM power cycling.
567 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100568static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 struct snd_soc_pcm_runtime *rtd =
571 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
572 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573
574 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
577 codec_dai->driver->playback.stream_name,
578 codec_dai->playback_active ? "active" : "inactive",
579 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 /* are we waiting on this codec DAI stream */
582 if (codec_dai->pop_wait == 1) {
583 codec_dai->pop_wait = 0;
584 snd_soc_dapm_stream_event(rtd,
585 codec_dai->driver->playback.stream_name,
586 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200589 mutex_unlock(&pcm_mutex);
590}
591
592/*
593 * Called by ALSA when a PCM substream is closed. Private data can be
594 * freed here. The cpu DAI, codec DAI, machine and platform are also
595 * shutdown.
596 */
597static int soc_codec_close(struct snd_pcm_substream *substream)
598{
599 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 struct snd_soc_platform *platform = rtd->platform;
601 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
602 struct snd_soc_dai *codec_dai = rtd->codec_dai;
603 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
605 mutex_lock(&pcm_mutex);
606
Jassi Brar14dc5732010-02-26 09:12:32 +0900607 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 cpu_dai->playback_active--;
609 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900610 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 cpu_dai->capture_active--;
612 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200613 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900614
615 cpu_dai->active--;
616 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617 codec->active--;
618
Mark Brown6010b2d2008-09-06 18:33:24 +0100619 /* Muting the DAC suppresses artifacts caused during digital
620 * shutdown, for example from stopping clocks.
621 */
622 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
623 snd_soc_dai_digital_mute(codec_dai, 1);
624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 if (cpu_dai->driver->ops->shutdown)
626 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 if (codec_dai->driver->ops->shutdown)
629 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
632 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 if (platform->driver->ops->close)
635 platform->driver->ops->close(substream);
636 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637
638 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
639 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100640 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 schedule_delayed_work(&rtd->delayed_work,
642 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 } else {
644 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 snd_soc_dapm_stream_event(rtd,
646 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100647 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200648 }
649
650 mutex_unlock(&pcm_mutex);
651 return 0;
652}
653
654/*
655 * Called by ALSA when the PCM substream is prepared, can set format, sample
656 * rate, etc. This function is non atomic and can be called multiple times,
657 * it can refer to the runtime info.
658 */
659static int soc_pcm_prepare(struct snd_pcm_substream *substream)
660{
661 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 struct snd_soc_platform *platform = rtd->platform;
663 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
664 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665 int ret = 0;
666
667 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100668
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
670 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100671 if (ret < 0) {
672 printk(KERN_ERR "asoc: machine prepare error\n");
673 goto out;
674 }
675 }
676
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 if (platform->driver->ops->prepare) {
678 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200679 if (ret < 0) {
680 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200682 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 }
684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (codec_dai->driver->ops->prepare) {
686 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200687 if (ret < 0) {
688 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200689 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200690 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691 }
692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 if (cpu_dai->driver->ops->prepare) {
694 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100695 if (ret < 0) {
696 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
697 goto out;
698 }
699 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Mark Brownd45f6212008-10-14 13:58:36 +0100701 /* cancel any delayed stream shutdown that is pending */
702 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
703 codec_dai->pop_wait) {
704 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100706 }
707
Mark Brown452c5ea2009-05-17 21:41:23 +0100708 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 snd_soc_dapm_stream_event(rtd,
710 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100711 SND_SOC_DAPM_STREAM_START);
712 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 snd_soc_dapm_stream_event(rtd,
714 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100715 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100716
Mark Brown452c5ea2009-05-17 21:41:23 +0100717 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718
719out:
720 mutex_unlock(&pcm_mutex);
721 return ret;
722}
723
724/*
725 * Called by ALSA when the hardware params are set by application. This
726 * function can also be called multiple times and can allocate buffers
727 * (using snd_pcm_lib_* ). It's non-atomic.
728 */
729static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
730 struct snd_pcm_hw_params *params)
731{
732 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 struct snd_soc_platform *platform = rtd->platform;
734 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
735 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736 int ret = 0;
737
738 mutex_lock(&pcm_mutex);
739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
741 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100743 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 goto out;
745 }
746 }
747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 if (codec_dai->driver->ops->hw_params) {
749 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100750 if (ret < 0) {
751 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
752 codec_dai->name);
753 goto codec_err;
754 }
755 }
756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 if (cpu_dai->driver->ops->hw_params) {
758 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200759 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200760 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100761 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762 goto interface_err;
763 }
764 }
765
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766 if (platform->driver->ops->hw_params) {
767 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200769 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200770 platform->name);
771 goto platform_err;
772 }
773 }
774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100776
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200777out:
778 mutex_unlock(&pcm_mutex);
779 return ret;
780
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 if (cpu_dai->driver->ops->hw_free)
783 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784
785interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 if (codec_dai->driver->ops->hw_free)
787 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100788
789codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
791 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792
793 mutex_unlock(&pcm_mutex);
794 return ret;
795}
796
797/*
798 * Free's resources allocated by hw_params, can be called multiple times
799 */
800static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
801{
802 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 struct snd_soc_platform *platform = rtd->platform;
804 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
805 struct snd_soc_dai *codec_dai = rtd->codec_dai;
806 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807
808 mutex_lock(&pcm_mutex);
809
810 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100811 if (!codec->active)
812 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813
814 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000815 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
816 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817
818 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 if (platform->driver->ops->hw_free)
820 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821
822 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 if (codec_dai->driver->ops->hw_free)
824 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 if (cpu_dai->driver->ops->hw_free)
827 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828
829 mutex_unlock(&pcm_mutex);
830 return 0;
831}
832
833static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
834{
835 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000836 struct snd_soc_platform *platform = rtd->platform;
837 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
838 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 int ret;
840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 if (codec_dai->driver->ops->trigger) {
842 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 if (ret < 0)
844 return ret;
845 }
846
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (platform->driver->ops->trigger) {
848 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849 if (ret < 0)
850 return ret;
851 }
852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853 if (cpu_dai->driver->ops->trigger) {
854 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855 if (ret < 0)
856 return ret;
857 }
858 return 0;
859}
860
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200861/*
862 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200863 * If cpu_dai, codec_dai, platform driver has the delay callback, than
864 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200865 */
866static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
867{
868 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 struct snd_soc_platform *platform = rtd->platform;
870 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
871 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200872 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200873 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200874 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200875
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000876 if (platform->driver->ops->pointer)
877 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 if (cpu_dai->driver->ops->delay)
880 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200881
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 if (codec_dai->driver->ops->delay)
883 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200884
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000885 if (platform->driver->delay)
886 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200887
888 runtime->delay = delay;
889
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200890 return offset;
891}
892
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200893/* ASoC PCM operations */
894static struct snd_pcm_ops soc_pcm_ops = {
895 .open = soc_pcm_open,
896 .close = soc_codec_close,
897 .hw_params = soc_pcm_hw_params,
898 .hw_free = soc_pcm_hw_free,
899 .prepare = soc_pcm_prepare,
900 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200901 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902};
903
904#ifdef CONFIG_PM
905/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100906static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907{
Mark Brown416356f2009-06-30 19:05:15 +0100908 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200910 int i;
911
Daniel Macke3509ff2009-06-03 17:44:49 +0200912 /* If the initialization of this soc device failed, there is no codec
913 * associated with it. Just bail out in this case.
914 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200916 return 0;
917
Andy Green6ed25972008-06-13 16:24:05 +0100918 /* Due to the resume being scheduled into a workqueue we could
919 * suspend before that's finished - wait for it to complete.
920 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 snd_power_lock(card->snd_card);
922 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
923 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100924
925 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100927
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200928 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 for (i = 0; i < card->num_rtd; i++) {
930 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
931 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100932
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100934 continue;
935
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936 if (drv->ops->digital_mute && dai->playback_active)
937 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200938 }
939
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100940 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 for (i = 0; i < card->num_rtd; i++) {
942 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100943 continue;
944
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100946 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100947
Mark Brown87506542008-11-18 20:50:34 +0000948 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100949 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 for (i = 0; i < card->num_rtd; i++) {
952 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
953 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100956 continue;
957
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
959 cpu_dai->driver->suspend(cpu_dai);
960 if (platform->driver->suspend && !platform->suspended) {
961 platform->driver->suspend(cpu_dai);
962 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100963 }
964 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200965
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 /* close any waiting streams and save state */
967 for (i = 0; i < card->num_rtd; i++) {
968 run_delayed_work(&card->rtd[i].delayed_work);
969 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
970 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100971
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000972 for (i = 0; i < card->num_rtd; i++) {
973 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
974
975 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100976 continue;
977
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000978 if (driver->playback.stream_name != NULL)
979 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
980 SND_SOC_DAPM_STREAM_SUSPEND);
981
982 if (driver->capture.stream_name != NULL)
983 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
984 SND_SOC_DAPM_STREAM_SUSPEND);
985 }
986
987 /* suspend all CODECs */
988 for (i = 0; i < card->num_rtd; i++) {
989 struct snd_soc_codec *codec = card->rtd[i].codec;
990 /* If there are paths active then the CODEC will be held with
991 * bias _ON and should not be suspended. */
992 if (!codec->suspended && codec->driver->suspend) {
993 switch (codec->bias_level) {
994 case SND_SOC_BIAS_STANDBY:
995 case SND_SOC_BIAS_OFF:
996 codec->driver->suspend(codec, PMSG_SUSPEND);
997 codec->suspended = 1;
998 break;
999 default:
1000 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1001 break;
1002 }
1003 }
1004 }
1005
1006 for (i = 0; i < card->num_rtd; i++) {
1007 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1008
1009 if (card->rtd[i].dai_link->ignore_suspend)
1010 continue;
1011
1012 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1013 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001014 }
1015
Mark Brown87506542008-11-18 20:50:34 +00001016 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001017 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001018
1019 return 0;
1020}
1021
Andy Green6ed25972008-06-13 16:24:05 +01001022/* deferred resume work, so resume can complete before we finished
1023 * setting our codec back up, which can be very slow on I2C
1024 */
1025static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001026{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001027 struct snd_soc_card *card =
1028 container_of(work, struct snd_soc_card, deferred_resume_work);
1029 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030 int i;
1031
Andy Green6ed25972008-06-13 16:24:05 +01001032 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1033 * so userspace apps are blocked from touching us
1034 */
1035
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001036 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001037
Mark Brown99497882010-05-07 20:24:05 +01001038 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001040
Mark Brown87506542008-11-18 20:50:34 +00001041 if (card->resume_pre)
1042 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001043
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001044 /* resume AC97 DAIs */
1045 for (i = 0; i < card->num_rtd; i++) {
1046 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001049 continue;
1050
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1052 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001053 }
1054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 for (i = 0; i < card->num_rtd; i++) {
1056 struct snd_soc_codec *codec = card->rtd[i].codec;
1057 /* If the CODEC was idle over suspend then it will have been
1058 * left with bias OFF or STANDBY and suspended so we must now
1059 * resume. Otherwise the suspend was suppressed.
1060 */
1061 if (codec->driver->resume && codec->suspended) {
1062 switch (codec->bias_level) {
1063 case SND_SOC_BIAS_STANDBY:
1064 case SND_SOC_BIAS_OFF:
1065 codec->driver->resume(codec);
1066 codec->suspended = 0;
1067 break;
1068 default:
1069 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1070 break;
1071 }
Mark Brown1547aba2010-05-07 21:11:40 +01001072 }
1073 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001074
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001075 for (i = 0; i < card->num_rtd; i++) {
1076 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001077
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001078 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001079 continue;
1080
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001081 if (driver->playback.stream_name != NULL)
1082 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001083 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001084
1085 if (driver->capture.stream_name != NULL)
1086 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001087 SND_SOC_DAPM_STREAM_RESUME);
1088 }
1089
Mark Brown3ff3f642008-05-19 12:32:25 +02001090 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001091 for (i = 0; i < card->num_rtd; i++) {
1092 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1093 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001095 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001096 continue;
1097
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001098 if (drv->ops->digital_mute && dai->playback_active)
1099 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001100 }
1101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 for (i = 0; i < card->num_rtd; i++) {
1103 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1104 struct snd_soc_platform *platform = card->rtd[i].platform;
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 (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1110 cpu_dai->driver->resume(cpu_dai);
1111 if (platform->driver->resume && platform->suspended) {
1112 platform->driver->resume(cpu_dai);
1113 platform->suspended = 0;
1114 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001115 }
1116
Mark Brown87506542008-11-18 20:50:34 +00001117 if (card->resume_post)
1118 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001121
1122 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001123 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001124}
1125
1126/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001127static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001128{
Mark Brown416356f2009-06-30 19:05:15 +01001129 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001130 struct snd_soc_card *card = platform_get_drvdata(pdev);
1131 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001132
Mark Brown64ab9ba2009-03-31 11:27:03 +01001133 /* AC97 devices might have other drivers hanging off them so
1134 * need to resume immediately. Other drivers don't have that
1135 * problem and may take a substantial amount of time to resume
1136 * due to I/O costs and anti-pop so handle them out of line.
1137 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138 for (i = 0; i < card->num_rtd; i++) {
1139 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1140 if (cpu_dai->driver->ac97_control) {
1141 dev_dbg(dev, "Resuming AC97 immediately\n");
1142 soc_resume_deferred(&card->deferred_resume_work);
1143 } else {
1144 dev_dbg(dev, "Scheduling resume work\n");
1145 if (!schedule_work(&card->deferred_resume_work))
1146 dev_err(dev, "resume work item may be lost\n");
1147 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001148 }
Andy Green6ed25972008-06-13 16:24:05 +01001149
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001150 return 0;
1151}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001152#else
1153#define soc_suspend NULL
1154#define soc_resume NULL
1155#endif
1156
Barry Song02a06d32009-10-16 18:13:38 +08001157static struct snd_soc_dai_ops null_dai_ops = {
1158};
1159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001161{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1163 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001164 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001165 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001167
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001168 if (rtd->complete)
1169 return 1;
1170 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172 /* do we already have the CPU DAI for this link ? */
1173 if (rtd->cpu_dai) {
1174 goto find_codec;
1175 }
1176 /* no, then find CPU DAI from registered DAIs*/
1177 list_for_each_entry(cpu_dai, &dai_list, list) {
1178 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1179
1180 if (!try_module_get(cpu_dai->dev->driver->owner))
1181 return -ENODEV;
1182
1183 rtd->cpu_dai = cpu_dai;
1184 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001185 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001186 }
1187 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1188 dai_link->cpu_dai_name);
1189
1190find_codec:
1191 /* do we already have the CODEC for this link ? */
1192 if (rtd->codec) {
1193 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001194 }
1195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001196 /* no, then find CODEC from registered CODECs*/
1197 list_for_each_entry(codec, &codec_list, list) {
1198 if (!strcmp(codec->name, dai_link->codec_name)) {
1199 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001200
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001201 if (!try_module_get(codec->dev->driver->owner))
1202 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1205 list_for_each_entry(codec_dai, &dai_list, list) {
1206 if (codec->dev == codec_dai->dev &&
1207 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1208 rtd->codec_dai = codec_dai;
1209 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001210 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211 }
1212 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1213 dai_link->codec_dai_name);
1214
1215 goto find_platform;
1216 }
1217 }
1218 dev_dbg(card->dev, "CODEC %s not registered\n",
1219 dai_link->codec_name);
1220
1221find_platform:
1222 /* do we already have the CODEC DAI for this link ? */
1223 if (rtd->platform) {
1224 goto out;
1225 }
1226 /* no, then find CPU DAI from registered DAIs*/
1227 list_for_each_entry(platform, &platform_list, list) {
1228 if (!strcmp(platform->name, dai_link->platform_name)) {
1229
1230 if (!try_module_get(platform->dev->driver->owner))
1231 return -ENODEV;
1232
1233 rtd->platform = platform;
1234 goto out;
1235 }
1236 }
1237
1238 dev_dbg(card->dev, "platform %s not registered\n",
1239 dai_link->platform_name);
1240 return 0;
1241
1242out:
1243 /* mark rtd as complete if we found all 4 of our client devices */
1244 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1245 rtd->complete = 1;
1246 card->num_rtd++;
1247 }
1248 return 1;
1249}
1250
1251static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1252{
1253 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1254 struct snd_soc_codec *codec = rtd->codec;
1255 struct snd_soc_platform *platform = rtd->platform;
1256 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1257 int err;
1258
1259 /* unregister the rtd device */
1260 if (rtd->dev_registered) {
1261 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1262 device_unregister(&rtd->dev);
1263 rtd->dev_registered = 0;
1264 }
1265
1266 /* remove the CODEC DAI */
1267 if (codec_dai && codec_dai->probed) {
1268 if (codec_dai->driver->remove) {
1269 err = codec_dai->driver->remove(codec_dai);
1270 if (err < 0)
1271 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1272 }
1273 codec_dai->probed = 0;
1274 list_del(&codec_dai->card_list);
1275 }
1276
1277 /* remove the platform */
1278 if (platform && platform->probed) {
1279 if (platform->driver->remove) {
1280 err = platform->driver->remove(platform);
1281 if (err < 0)
1282 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1283 }
1284 platform->probed = 0;
1285 list_del(&platform->card_list);
1286 module_put(platform->dev->driver->owner);
1287 }
1288
1289 /* remove the CODEC */
1290 if (codec && codec->probed) {
1291 if (codec->driver->remove) {
1292 err = codec->driver->remove(codec);
1293 if (err < 0)
1294 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1295 }
1296
1297 /* Make sure all DAPM widgets are freed */
1298 snd_soc_dapm_free(codec);
1299
1300 soc_cleanup_codec_debugfs(codec);
1301 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1302 codec->probed = 0;
1303 list_del(&codec->card_list);
1304 module_put(codec->dev->driver->owner);
1305 }
1306
1307 /* remove the cpu_dai */
1308 if (cpu_dai && cpu_dai->probed) {
1309 if (cpu_dai->driver->remove) {
1310 err = cpu_dai->driver->remove(cpu_dai);
1311 if (err < 0)
1312 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1313 }
1314 cpu_dai->probed = 0;
1315 list_del(&cpu_dai->card_list);
1316 module_put(cpu_dai->dev->driver->owner);
1317 }
1318}
1319
1320static void rtd_release(struct device *dev) {}
1321
1322static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1323{
1324 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1325 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1326 struct snd_soc_codec *codec = rtd->codec;
1327 struct snd_soc_platform *platform = rtd->platform;
1328 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1329 int ret;
1330
1331 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1332
1333 /* config components */
1334 codec_dai->codec = codec;
1335 codec->card = card;
1336 cpu_dai->platform = platform;
1337 rtd->card = card;
1338 rtd->dev.parent = card->dev;
1339 codec_dai->card = card;
1340 cpu_dai->card = card;
1341
1342 /* set default power off timeout */
1343 rtd->pmdown_time = pmdown_time;
1344
1345 /* probe the cpu_dai */
1346 if (!cpu_dai->probed) {
1347 if (cpu_dai->driver->probe) {
1348 ret = cpu_dai->driver->probe(cpu_dai);
1349 if (ret < 0) {
1350 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1351 cpu_dai->name);
1352 return ret;
1353 }
1354 }
1355 cpu_dai->probed = 1;
1356 /* mark cpu_dai as probed and add to card cpu_dai list */
1357 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1358 }
1359
1360 /* probe the CODEC */
1361 if (!codec->probed) {
1362 if (codec->driver->probe) {
1363 ret = codec->driver->probe(codec);
1364 if (ret < 0) {
1365 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1366 codec->name);
1367 return ret;
1368 }
1369 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001370
1371 soc_init_codec_debugfs(codec);
1372
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 /* mark codec as probed and add to card codec list */
1374 codec->probed = 1;
1375 list_add(&codec->card_list, &card->codec_dev_list);
1376 }
1377
1378 /* probe the platform */
1379 if (!platform->probed) {
1380 if (platform->driver->probe) {
1381 ret = platform->driver->probe(platform);
1382 if (ret < 0) {
1383 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1384 platform->name);
1385 return ret;
1386 }
1387 }
1388 /* mark platform as probed and add to card platform list */
1389 platform->probed = 1;
1390 list_add(&platform->card_list, &card->platform_dev_list);
1391 }
1392
1393 /* probe the CODEC DAI */
1394 if (!codec_dai->probed) {
1395 if (codec_dai->driver->probe) {
1396 ret = codec_dai->driver->probe(codec_dai);
1397 if (ret < 0) {
1398 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1399 codec_dai->name);
1400 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001401 }
1402 }
1403
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001404 /* mark cpu_dai as probed and add to card cpu_dai list */
1405 codec_dai->probed = 1;
1406 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407 }
1408
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 /* DAPM dai link stream work */
1410 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1411
1412 /* now that all clients have probed, initialise the DAI link */
1413 if (dai_link->init) {
1414 ret = dai_link->init(rtd);
1415 if (ret < 0) {
1416 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1417 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001418 }
1419 }
1420
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001421 /* Make sure all DAPM widgets are instantiated */
1422 snd_soc_dapm_new_widgets(codec);
1423 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 /* register the rtd device */
1426 rtd->dev.init_name = rtd->dai_link->stream_name;
1427 rtd->dev.release = rtd_release;
1428 rtd->dev.init_name = dai_link->name;
1429 ret = device_register(&rtd->dev);
1430 if (ret < 0) {
1431 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1432 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001433 }
1434
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435 rtd->dev_registered = 1;
1436 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1437 if (ret < 0)
1438 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1439
1440 /* add DAPM sysfs entries for this codec */
1441 ret = snd_soc_dapm_sys_add(&rtd->dev);
1442 if (ret < 0)
1443 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1444
1445 /* add codec sysfs entries */
1446 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1447 if (ret < 0)
1448 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1449
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450 /* create the pcm */
1451 ret = soc_new_pcm(rtd, num);
1452 if (ret < 0) {
1453 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1454 return ret;
1455 }
1456
1457 /* add platform data for AC97 devices */
1458 if (rtd->codec_dai->driver->ac97_control)
1459 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1460
1461 return 0;
1462}
1463
1464#ifdef CONFIG_SND_SOC_AC97_BUS
1465static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1466{
1467 int ret;
1468
1469 /* Only instantiate AC97 if not already done by the adaptor
1470 * for the generic AC97 subsystem.
1471 */
1472 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1473
1474 ret = soc_ac97_dev_register(rtd->codec);
1475 if (ret < 0) {
1476 printk(KERN_ERR "asoc: AC97 device register failed\n");
1477 return ret;
1478 }
1479
1480 rtd->codec->ac97_registered = 1;
1481 }
1482 return 0;
1483}
1484
1485static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1486{
1487 if (codec->ac97_registered) {
1488 soc_ac97_dev_unregister(codec);
1489 codec->ac97_registered = 0;
1490 }
1491}
1492#endif
1493
1494static void snd_soc_instantiate_card(struct snd_soc_card *card)
1495{
1496 struct platform_device *pdev = to_platform_device(card->dev);
1497 int ret, i;
1498
1499 mutex_lock(&card->mutex);
1500
1501 if (card->instantiated) {
1502 mutex_unlock(&card->mutex);
1503 return;
1504 }
1505
1506 /* bind DAIs */
1507 for (i = 0; i < card->num_links; i++)
1508 soc_bind_dai_link(card, i);
1509
1510 /* bind completed ? */
1511 if (card->num_rtd != card->num_links) {
1512 mutex_unlock(&card->mutex);
1513 return;
1514 }
1515
1516 /* card bind complete so register a sound card */
1517 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1518 card->owner, 0, &card->snd_card);
1519 if (ret < 0) {
1520 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1521 card->name);
1522 mutex_unlock(&card->mutex);
1523 return;
1524 }
1525 card->snd_card->dev = card->dev;
1526
Randy Dunlap1301a962008-06-17 19:19:34 +01001527#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001528 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001529 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001530#endif
Andy Green6ed25972008-06-13 16:24:05 +01001531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532 /* initialise the sound card only once */
1533 if (card->probe) {
1534 ret = card->probe(pdev);
1535 if (ret < 0)
1536 goto card_probe_error;
1537 }
1538
Mark Brownfe3e78e2009-11-03 22:13:13 +00001539 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001540 ret = soc_probe_dai_link(card, i);
1541 if (ret < 0) {
1542 printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name);
1543 goto probe_dai_err;
1544 }
1545 }
1546
1547 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1548 "%s", card->name);
1549 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1550 "%s", card->name);
1551
1552 ret = snd_card_register(card->snd_card);
1553 if (ret < 0) {
1554 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1555 goto probe_dai_err;
1556 }
1557
1558#ifdef CONFIG_SND_SOC_AC97_BUS
1559 /* register any AC97 codecs */
1560 for (i = 0; i < card->num_rtd; i++) {
1561 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001562 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1564 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001565 }
1566 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001567#endif
1568
Mark Brown435c5e22008-12-04 15:32:53 +00001569 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001571 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573probe_dai_err:
1574 for (i = 0; i < card->num_links; i++)
1575 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001578 if (card->remove)
1579 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580
1581 snd_card_free(card->snd_card);
1582
1583 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001584}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001585
Mark Brown435c5e22008-12-04 15:32:53 +00001586/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001587 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001588 * client_mutex.
1589 */
1590static void snd_soc_instantiate_cards(void)
1591{
1592 struct snd_soc_card *card;
1593 list_for_each_entry(card, &card_list, list)
1594 snd_soc_instantiate_card(card);
1595}
1596
1597/* probes a new socdev */
1598static int soc_probe(struct platform_device *pdev)
1599{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001601 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001602
1603 /* Bodge while we unpick instantiation */
1604 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 INIT_LIST_HEAD(&card->dai_dev_list);
1606 INIT_LIST_HEAD(&card->codec_dev_list);
1607 INIT_LIST_HEAD(&card->platform_dev_list);
1608
Mark Brown435c5e22008-12-04 15:32:53 +00001609 ret = snd_soc_register_card(card);
1610 if (ret != 0) {
1611 dev_err(&pdev->dev, "Failed to register card\n");
1612 return ret;
1613 }
1614
1615 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001616}
1617
1618/* removes a socdev */
1619static int soc_remove(struct platform_device *pdev)
1620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001622 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 /* make sure any delayed work runs */
1627 for (i = 0; i < card->num_rtd; i++) {
1628 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1629 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001630 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632 /* remove and free each DAI */
1633 for (i = 0; i < card->num_rtd; i++)
1634 soc_remove_dai_link(card, i);
1635
1636 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001637 if (card->remove)
1638 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639
1640 kfree(card->rtd);
1641 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001642 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001643 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001644 return 0;
1645}
1646
Mark Brown416356f2009-06-30 19:05:15 +01001647static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001648{
Mark Brown416356f2009-06-30 19:05:15 +01001649 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650 struct snd_soc_card *card = platform_get_drvdata(pdev);
1651 int i;
Mark Brown51737472009-06-22 13:16:51 +01001652
1653 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001654 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001655
1656 /* Flush out pmdown_time work - we actually do want to run it
1657 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001658 for (i = 0; i < card->num_rtd; i++) {
1659 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1660 run_delayed_work(&rtd->delayed_work);
1661 }
Mark Brown51737472009-06-22 13:16:51 +01001662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001664
1665 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001666}
1667
Alexey Dobriyan47145212009-12-14 18:00:08 -08001668static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001669 .suspend = soc_suspend,
1670 .resume = soc_resume,
1671 .poweroff = soc_poweroff,
1672};
1673
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001674/* ASoC platform driver */
1675static struct platform_driver soc_driver = {
1676 .driver = {
1677 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001678 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001679 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001680 },
1681 .probe = soc_probe,
1682 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683};
1684
1685/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001687{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 struct snd_soc_codec *codec = rtd->codec;
1689 struct snd_soc_platform *platform = rtd->platform;
1690 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001692 struct snd_pcm *pcm;
1693 char new_name[64];
1694 int ret = 0, playback = 0, capture = 0;
1695
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001696 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001697 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001701 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001703 capture = 1;
1704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001705 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1706 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1707 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001708 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710 return ret;
1711 }
1712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001714 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1716 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1717 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1718 soc_pcm_ops.copy = platform->driver->ops->copy;
1719 soc_pcm_ops.silence = platform->driver->ops->silence;
1720 soc_pcm_ops.ack = platform->driver->ops->ack;
1721 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001722
1723 if (playback)
1724 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1725
1726 if (capture)
1727 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1728
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001729 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001730 if (ret < 0) {
1731 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001732 return ret;
1733 }
1734
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001735 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001736 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1737 cpu_dai->name);
1738 return ret;
1739}
1740
Mark Brown096e49d2009-07-05 15:12:22 +01001741/**
1742 * snd_soc_codec_volatile_register: Report if a register is volatile.
1743 *
1744 * @codec: CODEC to query.
1745 * @reg: Register to query.
1746 *
1747 * Boolean function indiciating if a CODEC register is volatile.
1748 */
1749int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1750{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 if (codec->driver->volatile_register)
1752 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001753 else
1754 return 0;
1755}
1756EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1757
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001758/**
1759 * snd_soc_new_ac97_codec - initailise AC97 device
1760 * @codec: audio codec
1761 * @ops: AC97 bus operations
1762 * @num: AC97 codec number
1763 *
1764 * Initialises AC97 codec resources for use by ad-hoc devices only.
1765 */
1766int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1767 struct snd_ac97_bus_ops *ops, int num)
1768{
1769 mutex_lock(&codec->mutex);
1770
1771 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1772 if (codec->ac97 == NULL) {
1773 mutex_unlock(&codec->mutex);
1774 return -ENOMEM;
1775 }
1776
1777 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1778 if (codec->ac97->bus == NULL) {
1779 kfree(codec->ac97);
1780 codec->ac97 = NULL;
1781 mutex_unlock(&codec->mutex);
1782 return -ENOMEM;
1783 }
1784
1785 codec->ac97->bus->ops = ops;
1786 codec->ac97->num = num;
1787 mutex_unlock(&codec->mutex);
1788 return 0;
1789}
1790EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1791
1792/**
1793 * snd_soc_free_ac97_codec - free AC97 codec device
1794 * @codec: audio codec
1795 *
1796 * Frees AC97 codec device resources.
1797 */
1798void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1799{
1800 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001801#ifdef CONFIG_SND_SOC_AC97_BUS
1802 soc_unregister_ac97_dai_link(codec);
1803#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001804 kfree(codec->ac97->bus);
1805 kfree(codec->ac97);
1806 codec->ac97 = NULL;
1807 mutex_unlock(&codec->mutex);
1808}
1809EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1810
1811/**
1812 * snd_soc_update_bits - update codec register bits
1813 * @codec: audio codec
1814 * @reg: codec register
1815 * @mask: register mask
1816 * @value: new value
1817 *
1818 * Writes new register value.
1819 *
1820 * Returns 1 for change else 0.
1821 */
1822int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001823 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001824{
1825 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001826 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001827
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828 old = snd_soc_read(codec, reg);
1829 new = (old & ~mask) | value;
1830 change = old != new;
1831 if (change)
1832 snd_soc_write(codec, reg, new);
1833
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001834 return change;
1835}
1836EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1837
1838/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001839 * snd_soc_update_bits_locked - update codec register bits
1840 * @codec: audio codec
1841 * @reg: codec register
1842 * @mask: register mask
1843 * @value: new value
1844 *
1845 * Writes new register value, and takes the codec mutex.
1846 *
1847 * Returns 1 for change else 0.
1848 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001849int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1850 unsigned short reg, unsigned int mask,
1851 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001852{
1853 int change;
1854
1855 mutex_lock(&codec->mutex);
1856 change = snd_soc_update_bits(codec, reg, mask, value);
1857 mutex_unlock(&codec->mutex);
1858
1859 return change;
1860}
Mark Browndd1b3d52009-12-04 14:22:03 +00001861EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001862
1863/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 * snd_soc_test_bits - test register for change
1865 * @codec: audio codec
1866 * @reg: codec register
1867 * @mask: register mask
1868 * @value: new value
1869 *
1870 * Tests a register with a new value and checks if the new value is
1871 * different from the old value.
1872 *
1873 * Returns 1 for change else 0.
1874 */
1875int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001876 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001877{
1878 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001879 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001881 old = snd_soc_read(codec, reg);
1882 new = (old & ~mask) | value;
1883 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001884
1885 return change;
1886}
1887EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1888
1889/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001890 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1891 * @substream: the pcm substream
1892 * @hw: the hardware parameters
1893 *
1894 * Sets the substream runtime hardware parameters.
1895 */
1896int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1897 const struct snd_pcm_hardware *hw)
1898{
1899 struct snd_pcm_runtime *runtime = substream->runtime;
1900 runtime->hw.info = hw->info;
1901 runtime->hw.formats = hw->formats;
1902 runtime->hw.period_bytes_min = hw->period_bytes_min;
1903 runtime->hw.period_bytes_max = hw->period_bytes_max;
1904 runtime->hw.periods_min = hw->periods_min;
1905 runtime->hw.periods_max = hw->periods_max;
1906 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1907 runtime->hw.fifo_size = hw->fifo_size;
1908 return 0;
1909}
1910EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1911
1912/**
1913 * snd_soc_cnew - create new control
1914 * @_template: control template
1915 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001916 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917 *
1918 * Create a new mixer control from a template control.
1919 *
1920 * Returns 0 for success, else error.
1921 */
1922struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1923 void *data, char *long_name)
1924{
1925 struct snd_kcontrol_new template;
1926
1927 memcpy(&template, _template, sizeof(template));
1928 if (long_name)
1929 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930 template.index = 0;
1931
1932 return snd_ctl_new1(&template, data);
1933}
1934EXPORT_SYMBOL_GPL(snd_soc_cnew);
1935
1936/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001937 * snd_soc_add_controls - add an array of controls to a codec.
1938 * Convienience function to add a list of controls. Many codecs were
1939 * duplicating this code.
1940 *
1941 * @codec: codec to add controls to
1942 * @controls: array of controls to add
1943 * @num_controls: number of elements in the array
1944 *
1945 * Return 0 for success, else error.
1946 */
1947int snd_soc_add_controls(struct snd_soc_codec *codec,
1948 const struct snd_kcontrol_new *controls, int num_controls)
1949{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001950 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001951 int err, i;
1952
1953 for (i = 0; i < num_controls; i++) {
1954 const struct snd_kcontrol_new *control = &controls[i];
1955 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1956 if (err < 0) {
1957 dev_err(codec->dev, "%s: Failed to add %s\n",
1958 codec->name, control->name);
1959 return err;
1960 }
1961 }
1962
1963 return 0;
1964}
1965EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1966
1967/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968 * snd_soc_info_enum_double - enumerated double mixer info callback
1969 * @kcontrol: mixer control
1970 * @uinfo: control element information
1971 *
1972 * Callback to provide information about a double enumerated
1973 * mixer control.
1974 *
1975 * Returns 0 for success.
1976 */
1977int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_info *uinfo)
1979{
1980 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1981
1982 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1983 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001984 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001986 if (uinfo->value.enumerated.item > e->max - 1)
1987 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 strcpy(uinfo->value.enumerated.name,
1989 e->texts[uinfo->value.enumerated.item]);
1990 return 0;
1991}
1992EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1993
1994/**
1995 * snd_soc_get_enum_double - enumerated double mixer get callback
1996 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001997 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 *
1999 * Callback to get the value of a double enumerated mixer.
2000 *
2001 * Returns 0 for success.
2002 */
2003int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_value *ucontrol)
2005{
2006 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2007 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002008 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002010 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002011 ;
2012 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002013 ucontrol->value.enumerated.item[0]
2014 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 if (e->shift_l != e->shift_r)
2016 ucontrol->value.enumerated.item[1] =
2017 (val >> e->shift_r) & (bitmask - 1);
2018
2019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2022
2023/**
2024 * snd_soc_put_enum_double - enumerated double mixer put callback
2025 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002026 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002027 *
2028 * Callback to set the value of a double enumerated mixer.
2029 *
2030 * Returns 0 for success.
2031 */
2032int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
2034{
2035 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2036 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002037 unsigned int val;
2038 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002040 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002042 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043 return -EINVAL;
2044 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2045 mask = (bitmask - 1) << e->shift_l;
2046 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002047 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002048 return -EINVAL;
2049 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2050 mask |= (bitmask - 1) << e->shift_r;
2051 }
2052
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002053 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054}
2055EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2056
2057/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002058 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2059 * @kcontrol: mixer control
2060 * @ucontrol: control element information
2061 *
2062 * Callback to get the value of a double semi enumerated mixer.
2063 *
2064 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2065 * used for handling bitfield coded enumeration for example.
2066 *
2067 * Returns 0 for success.
2068 */
2069int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
2071{
2072 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002073 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002074 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002075
2076 reg_val = snd_soc_read(codec, e->reg);
2077 val = (reg_val >> e->shift_l) & e->mask;
2078 for (mux = 0; mux < e->max; mux++) {
2079 if (val == e->values[mux])
2080 break;
2081 }
2082 ucontrol->value.enumerated.item[0] = mux;
2083 if (e->shift_l != e->shift_r) {
2084 val = (reg_val >> e->shift_r) & e->mask;
2085 for (mux = 0; mux < e->max; mux++) {
2086 if (val == e->values[mux])
2087 break;
2088 }
2089 ucontrol->value.enumerated.item[1] = mux;
2090 }
2091
2092 return 0;
2093}
2094EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2095
2096/**
2097 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2098 * @kcontrol: mixer control
2099 * @ucontrol: control element information
2100 *
2101 * Callback to set the value of a double semi enumerated mixer.
2102 *
2103 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2104 * used for handling bitfield coded enumeration for example.
2105 *
2106 * Returns 0 for success.
2107 */
2108int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2109 struct snd_ctl_elem_value *ucontrol)
2110{
2111 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002112 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002113 unsigned int val;
2114 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002115
2116 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2117 return -EINVAL;
2118 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2119 mask = e->mask << e->shift_l;
2120 if (e->shift_l != e->shift_r) {
2121 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2122 return -EINVAL;
2123 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2124 mask |= e->mask << e->shift_r;
2125 }
2126
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002127 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002128}
2129EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2130
2131/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002132 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2133 * @kcontrol: mixer control
2134 * @uinfo: control element information
2135 *
2136 * Callback to provide information about an external enumerated
2137 * single mixer.
2138 *
2139 * Returns 0 for success.
2140 */
2141int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
2144 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2145
2146 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2147 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002148 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002149
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002150 if (uinfo->value.enumerated.item > e->max - 1)
2151 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002152 strcpy(uinfo->value.enumerated.name,
2153 e->texts[uinfo->value.enumerated.item]);
2154 return 0;
2155}
2156EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2157
2158/**
2159 * snd_soc_info_volsw_ext - external single mixer info callback
2160 * @kcontrol: mixer control
2161 * @uinfo: control element information
2162 *
2163 * Callback to provide information about a single external mixer control.
2164 *
2165 * Returns 0 for success.
2166 */
2167int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_info *uinfo)
2169{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002170 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002171
Mark Brownfd5dfad2009-04-15 21:37:46 +01002172 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002173 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2174 else
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2176
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002177 uinfo->count = 1;
2178 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002179 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180 return 0;
2181}
2182EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2183
2184/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185 * snd_soc_info_volsw - single mixer info callback
2186 * @kcontrol: mixer control
2187 * @uinfo: control element information
2188 *
2189 * Callback to provide information about a single mixer control.
2190 *
2191 * Returns 0 for success.
2192 */
2193int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_info *uinfo)
2195{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002196 struct soc_mixer_control *mc =
2197 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002198 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002199 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04002200 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002202 if (!mc->platform_max)
2203 mc->platform_max = mc->max;
2204 platform_max = mc->platform_max;
2205
2206 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002207 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2208 else
2209 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2210
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211 uinfo->count = shift == rshift ? 1 : 2;
2212 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002213 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 return 0;
2215}
2216EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2217
2218/**
2219 * snd_soc_get_volsw - single mixer get callback
2220 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002221 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 *
2223 * Callback to get the value of a single mixer control.
2224 *
2225 * Returns 0 for success.
2226 */
2227int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002230 struct soc_mixer_control *mc =
2231 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002232 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002233 unsigned int reg = mc->reg;
2234 unsigned int shift = mc->shift;
2235 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002236 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002237 unsigned int mask = (1 << fls(max)) - 1;
2238 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239
2240 ucontrol->value.integer.value[0] =
2241 (snd_soc_read(codec, reg) >> shift) & mask;
2242 if (shift != rshift)
2243 ucontrol->value.integer.value[1] =
2244 (snd_soc_read(codec, reg) >> rshift) & mask;
2245 if (invert) {
2246 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002247 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 if (shift != rshift)
2249 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002250 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251 }
2252
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2256
2257/**
2258 * snd_soc_put_volsw - single mixer put callback
2259 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002260 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 *
2262 * Callback to set the value of a single mixer control.
2263 *
2264 * Returns 0 for success.
2265 */
2266int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
2268{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002269 struct soc_mixer_control *mc =
2270 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002272 unsigned int reg = mc->reg;
2273 unsigned int shift = mc->shift;
2274 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002275 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002276 unsigned int mask = (1 << fls(max)) - 1;
2277 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002278 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279
2280 val = (ucontrol->value.integer.value[0] & mask);
2281 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002282 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002283 val_mask = mask << shift;
2284 val = val << shift;
2285 if (shift != rshift) {
2286 val2 = (ucontrol->value.integer.value[1] & mask);
2287 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002288 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 val_mask |= mask << rshift;
2290 val |= val2 << rshift;
2291 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002292 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293}
2294EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2295
2296/**
2297 * snd_soc_info_volsw_2r - double mixer info callback
2298 * @kcontrol: mixer control
2299 * @uinfo: control element information
2300 *
2301 * Callback to provide information about a double mixer control that
2302 * spans 2 codec registers.
2303 *
2304 * Returns 0 for success.
2305 */
2306int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_info *uinfo)
2308{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002309 struct soc_mixer_control *mc =
2310 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002311 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002313 if (!mc->platform_max)
2314 mc->platform_max = mc->max;
2315 platform_max = mc->platform_max;
2316
2317 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002318 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2319 else
2320 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2321
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 uinfo->count = 2;
2323 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002324 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325 return 0;
2326}
2327EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2328
2329/**
2330 * snd_soc_get_volsw_2r - double mixer get callback
2331 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002332 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333 *
2334 * Callback to get the value of a double mixer control that spans 2 registers.
2335 *
2336 * Returns 0 for success.
2337 */
2338int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2340{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002341 struct soc_mixer_control *mc =
2342 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002344 unsigned int reg = mc->reg;
2345 unsigned int reg2 = mc->rreg;
2346 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002347 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002348 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002349 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350
2351 ucontrol->value.integer.value[0] =
2352 (snd_soc_read(codec, reg) >> shift) & mask;
2353 ucontrol->value.integer.value[1] =
2354 (snd_soc_read(codec, reg2) >> shift) & mask;
2355 if (invert) {
2356 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002357 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002359 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 }
2361
2362 return 0;
2363}
2364EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2365
2366/**
2367 * snd_soc_put_volsw_2r - double mixer set callback
2368 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002369 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 *
2371 * Callback to set the value of a double mixer control that spans 2 registers.
2372 *
2373 * Returns 0 for success.
2374 */
2375int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_value *ucontrol)
2377{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002378 struct soc_mixer_control *mc =
2379 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002381 unsigned int reg = mc->reg;
2382 unsigned int reg2 = mc->rreg;
2383 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002384 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002385 unsigned int mask = (1 << fls(max)) - 1;
2386 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002388 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389
2390 val_mask = mask << shift;
2391 val = (ucontrol->value.integer.value[0] & mask);
2392 val2 = (ucontrol->value.integer.value[1] & mask);
2393
2394 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002395 val = max - val;
2396 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397 }
2398
2399 val = val << shift;
2400 val2 = val2 << shift;
2401
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002402 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002403 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404 return err;
2405
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002406 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407 return err;
2408}
2409EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2410
Mark Browne13ac2e2008-05-28 17:58:05 +01002411/**
2412 * snd_soc_info_volsw_s8 - signed mixer info callback
2413 * @kcontrol: mixer control
2414 * @uinfo: control element information
2415 *
2416 * Callback to provide information about a signed mixer control.
2417 *
2418 * Returns 0 for success.
2419 */
2420int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_info *uinfo)
2422{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002423 struct soc_mixer_control *mc =
2424 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002425 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002426 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002427
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002428 if (!mc->platform_max)
2429 mc->platform_max = mc->max;
2430 platform_max = mc->platform_max;
2431
Mark Browne13ac2e2008-05-28 17:58:05 +01002432 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2433 uinfo->count = 2;
2434 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002435 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002436 return 0;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2439
2440/**
2441 * snd_soc_get_volsw_s8 - signed mixer get callback
2442 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002443 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002444 *
2445 * Callback to get the value of a signed mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_value *ucontrol)
2451{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002452 struct soc_mixer_control *mc =
2453 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002454 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002455 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002456 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002457 int val = snd_soc_read(codec, reg);
2458
2459 ucontrol->value.integer.value[0] =
2460 ((signed char)(val & 0xff))-min;
2461 ucontrol->value.integer.value[1] =
2462 ((signed char)((val >> 8) & 0xff))-min;
2463 return 0;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2466
2467/**
2468 * snd_soc_put_volsw_sgn - signed mixer put callback
2469 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002470 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002471 *
2472 * Callback to set the value of a signed mixer control.
2473 *
2474 * Returns 0 for success.
2475 */
2476int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002479 struct soc_mixer_control *mc =
2480 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002481 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002482 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002483 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002484 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002485
2486 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2487 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2488
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002489 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002490}
2491EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2492
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002493/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002494 * snd_soc_limit_volume - Set new limit to an existing volume control.
2495 *
2496 * @codec: where to look for the control
2497 * @name: Name of the control
2498 * @max: new maximum limit
2499 *
2500 * Return 0 for success, else error.
2501 */
2502int snd_soc_limit_volume(struct snd_soc_codec *codec,
2503 const char *name, int max)
2504{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002505 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002506 struct snd_kcontrol *kctl;
2507 struct soc_mixer_control *mc;
2508 int found = 0;
2509 int ret = -EINVAL;
2510
2511 /* Sanity check for name and max */
2512 if (unlikely(!name || max <= 0))
2513 return -EINVAL;
2514
2515 list_for_each_entry(kctl, &card->controls, list) {
2516 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2517 found = 1;
2518 break;
2519 }
2520 }
2521 if (found) {
2522 mc = (struct soc_mixer_control *)kctl->private_value;
2523 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002524 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002525 ret = 0;
2526 }
2527 }
2528 return ret;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2531
2532/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002533 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2534 * mixer info callback
2535 * @kcontrol: mixer control
2536 * @uinfo: control element information
2537 *
2538 * Returns 0 for success.
2539 */
2540int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_info *uinfo)
2542{
2543 struct soc_mixer_control *mc =
2544 (struct soc_mixer_control *)kcontrol->private_value;
2545 int max = mc->max;
2546 int min = mc->min;
2547
2548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2549 uinfo->count = 2;
2550 uinfo->value.integer.min = 0;
2551 uinfo->value.integer.max = max-min;
2552
2553 return 0;
2554}
2555EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2556
2557/**
2558 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2559 * mixer get callback
2560 * @kcontrol: mixer control
2561 * @uinfo: control element information
2562 *
2563 * Returns 0 for success.
2564 */
2565int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2567{
2568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
2570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2571 unsigned int mask = (1<<mc->shift)-1;
2572 int min = mc->min;
2573 int val = snd_soc_read(codec, mc->reg) & mask;
2574 int valr = snd_soc_read(codec, mc->rreg) & mask;
2575
Stuart Longland20630c72010-06-18 12:56:10 +10002576 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2577 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002578 return 0;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2581
2582/**
2583 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2584 * mixer put callback
2585 * @kcontrol: mixer control
2586 * @uinfo: control element information
2587 *
2588 * Returns 0 for success.
2589 */
2590int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_value *ucontrol)
2592{
2593 struct soc_mixer_control *mc =
2594 (struct soc_mixer_control *)kcontrol->private_value;
2595 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2596 unsigned int mask = (1<<mc->shift)-1;
2597 int min = mc->min;
2598 int ret;
2599 unsigned int val, valr, oval, ovalr;
2600
2601 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2602 val &= mask;
2603 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2604 valr &= mask;
2605
2606 oval = snd_soc_read(codec, mc->reg) & mask;
2607 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2608
2609 ret = 0;
2610 if (oval != val) {
2611 ret = snd_soc_write(codec, mc->reg, val);
2612 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002613 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002614 }
2615 if (ovalr != valr) {
2616 ret = snd_soc_write(codec, mc->rreg, valr);
2617 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002618 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002619 }
2620
2621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2624
2625/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002626 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2627 * @dai: DAI
2628 * @clk_id: DAI specific clock ID
2629 * @freq: new clock frequency in Hz
2630 * @dir: new clock direction - input/output.
2631 *
2632 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2633 */
2634int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2635 unsigned int freq, int dir)
2636{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002637 if (dai->driver && dai->driver->ops->set_sysclk)
2638 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002639 else
2640 return -EINVAL;
2641}
2642EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2643
2644/**
2645 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2646 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002647 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002648 * @div: new clock divisor.
2649 *
2650 * Configures the clock dividers. This is used to derive the best DAI bit and
2651 * frame clocks from the system or master clock. It's best to set the DAI bit
2652 * and frame clocks as low as possible to save system power.
2653 */
2654int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2655 int div_id, int div)
2656{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002657 if (dai->driver && dai->driver->ops->set_clkdiv)
2658 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002659 else
2660 return -EINVAL;
2661}
2662EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2663
2664/**
2665 * snd_soc_dai_set_pll - configure DAI PLL.
2666 * @dai: DAI
2667 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002668 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002669 * @freq_in: PLL input clock frequency in Hz
2670 * @freq_out: requested PLL output clock frequency in Hz
2671 *
2672 * Configures and enables PLL to generate output clock based on input clock.
2673 */
Mark Brown85488032009-09-05 18:52:16 +01002674int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2675 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002676{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002677 if (dai->driver && dai->driver->ops->set_pll)
2678 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002679 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002680 else
2681 return -EINVAL;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2684
2685/**
2686 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2687 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002688 * @fmt: SND_SOC_DAIFMT_ format value.
2689 *
2690 * Configures the DAI hardware format and clocking.
2691 */
2692int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2693{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002694 if (dai->driver && dai->driver->ops->set_fmt)
2695 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002696 else
2697 return -EINVAL;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2700
2701/**
2702 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2703 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002704 * @tx_mask: bitmask representing active TX slots.
2705 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002706 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002707 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002708 *
2709 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2710 * specific.
2711 */
2712int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002713 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002714{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002715 if (dai->driver && dai->driver->ops->set_tdm_slot)
2716 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002717 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002718 else
2719 return -EINVAL;
2720}
2721EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2722
2723/**
Barry Song472df3c2009-09-12 01:16:29 +08002724 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2725 * @dai: DAI
2726 * @tx_num: how many TX channels
2727 * @tx_slot: pointer to an array which imply the TX slot number channel
2728 * 0~num-1 uses
2729 * @rx_num: how many RX channels
2730 * @rx_slot: pointer to an array which imply the RX slot number channel
2731 * 0~num-1 uses
2732 *
2733 * configure the relationship between channel number and TDM slot number.
2734 */
2735int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2736 unsigned int tx_num, unsigned int *tx_slot,
2737 unsigned int rx_num, unsigned int *rx_slot)
2738{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002739 if (dai->driver && dai->driver->ops->set_channel_map)
2740 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002741 rx_num, rx_slot);
2742 else
2743 return -EINVAL;
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2746
2747/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002748 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2749 * @dai: DAI
2750 * @tristate: tristate enable
2751 *
2752 * Tristates the DAI so that others can use it.
2753 */
2754int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002756 if (dai->driver && dai->driver->ops->set_tristate)
2757 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002758 else
2759 return -EINVAL;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2762
2763/**
2764 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2765 * @dai: DAI
2766 * @mute: mute enable
2767 *
2768 * Mutes the DAI DAC.
2769 */
2770int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2771{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002772 if (dai->driver && dai->driver->ops->digital_mute)
2773 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002774 else
2775 return -EINVAL;
2776}
2777EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2778
Mark Brownc5af3a22008-11-28 13:29:45 +00002779/**
2780 * snd_soc_register_card - Register a card with the ASoC core
2781 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002782 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002783 *
2784 * Note that currently this is an internal only function: it will be
2785 * exposed to machine drivers after further backporting of ASoC v2
2786 * registration APIs.
2787 */
2788static int snd_soc_register_card(struct snd_soc_card *card)
2789{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002790 int i;
2791
Mark Brownc5af3a22008-11-28 13:29:45 +00002792 if (!card->name || !card->dev)
2793 return -EINVAL;
2794
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002795 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2796 GFP_KERNEL);
2797 if (card->rtd == NULL)
2798 return -ENOMEM;
2799
2800 for (i = 0; i < card->num_links; i++)
2801 card->rtd[i].dai_link = &card->dai_link[i];
2802
Mark Brownc5af3a22008-11-28 13:29:45 +00002803 INIT_LIST_HEAD(&card->list);
2804 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002805 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002806
2807 mutex_lock(&client_mutex);
2808 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002809 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002810 mutex_unlock(&client_mutex);
2811
2812 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2813
2814 return 0;
2815}
2816
2817/**
2818 * snd_soc_unregister_card - Unregister a card with the ASoC core
2819 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002820 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002821 *
2822 * Note that currently this is an internal only function: it will be
2823 * exposed to machine drivers after further backporting of ASoC v2
2824 * registration APIs.
2825 */
2826static int snd_soc_unregister_card(struct snd_soc_card *card)
2827{
2828 mutex_lock(&client_mutex);
2829 list_del(&card->list);
2830 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002831 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2832
2833 return 0;
2834}
2835
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002836/*
2837 * Simplify DAI link configuration by removing ".-1" from device names
2838 * and sanitizing names.
2839 */
2840static inline char *fmt_single_name(struct device *dev, int *id)
2841{
2842 char *found, name[NAME_SIZE];
2843 int id1, id2;
2844
2845 if (dev_name(dev) == NULL)
2846 return NULL;
2847
2848 strncpy(name, dev_name(dev), NAME_SIZE);
2849
2850 /* are we a "%s.%d" name (platform and SPI components) */
2851 found = strstr(name, dev->driver->name);
2852 if (found) {
2853 /* get ID */
2854 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2855
2856 /* discard ID from name if ID == -1 */
2857 if (*id == -1)
2858 found[strlen(dev->driver->name)] = '\0';
2859 }
2860
2861 } else {
2862 /* I2C component devices are named "bus-addr" */
2863 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2864 char tmp[NAME_SIZE];
2865
2866 /* create unique ID number from I2C addr and bus */
2867 *id = ((id1 && 0xffff) << 16) + id2;
2868
2869 /* sanitize component name for DAI link creation */
2870 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2871 strncpy(name, tmp, NAME_SIZE);
2872 } else
2873 *id = 0;
2874 }
2875
2876 return kstrdup(name, GFP_KERNEL);
2877}
2878
2879/*
2880 * Simplify DAI link naming for single devices with multiple DAIs by removing
2881 * any ".-1" and using the DAI name (instead of device name).
2882 */
2883static inline char *fmt_multiple_name(struct device *dev,
2884 struct snd_soc_dai_driver *dai_drv)
2885{
2886 if (dai_drv->name == NULL) {
2887 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2888 dev_name(dev));
2889 return NULL;
2890 }
2891
2892 return kstrdup(dai_drv->name, GFP_KERNEL);
2893}
2894
Mark Brown91151712008-11-30 23:31:24 +00002895/**
2896 * snd_soc_register_dai - Register a DAI with the ASoC core
2897 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002898 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002899 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002900int snd_soc_register_dai(struct device *dev,
2901 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002902{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002903 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002904
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002905 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002907 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2908 if (dai == NULL)
2909 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002910
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002911 /* create DAI component name */
2912 dai->name = fmt_single_name(dev, &dai->id);
2913 if (dai->name == NULL) {
2914 kfree(dai);
2915 return -ENOMEM;
2916 }
2917
2918 dai->dev = dev;
2919 dai->driver = dai_drv;
2920 if (!dai->driver->ops)
2921 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002922
2923 mutex_lock(&client_mutex);
2924 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002925 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002926 mutex_unlock(&client_mutex);
2927
2928 pr_debug("Registered DAI '%s'\n", dai->name);
2929
2930 return 0;
2931}
2932EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2933
2934/**
2935 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2936 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002937 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002938 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002940{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002941 struct snd_soc_dai *dai;
2942
2943 list_for_each_entry(dai, &dai_list, list) {
2944 if (dev == dai->dev)
2945 goto found;
2946 }
2947 return;
2948
2949found:
Mark Brown91151712008-11-30 23:31:24 +00002950 mutex_lock(&client_mutex);
2951 list_del(&dai->list);
2952 mutex_unlock(&client_mutex);
2953
2954 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002955 kfree(dai->name);
2956 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002957}
2958EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2959
2960/**
2961 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2962 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002963 * @dai: Array of DAIs to register
2964 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002965 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002966int snd_soc_register_dais(struct device *dev,
2967 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002968{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002969 struct snd_soc_dai *dai;
2970 int i, ret = 0;
2971
Liam Girdwood720ffa42010-08-18 00:30:30 +01002972 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002973
2974 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002975
2976 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2977 if (dai == NULL)
2978 return -ENOMEM;
2979
2980 /* create DAI component name */
2981 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2982 if (dai->name == NULL) {
2983 kfree(dai);
2984 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00002985 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002986 }
2987
2988 dai->dev = dev;
2989 dai->id = i;
2990 dai->driver = &dai_drv[i];
2991 if (!dai->driver->ops)
2992 dai->driver->ops = &null_dai_ops;
2993
2994 mutex_lock(&client_mutex);
2995 list_add(&dai->list, &dai_list);
2996 mutex_unlock(&client_mutex);
2997
2998 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002999 }
3000
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003001 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003002 return 0;
3003
3004err:
3005 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003006 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003007
3008 return ret;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3011
3012/**
3013 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3014 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003015 * @dai: Array of DAIs to unregister
3016 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003017 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003019{
3020 int i;
3021
3022 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003023 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003024}
3025EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3026
Mark Brown12a48a8c2008-12-03 19:40:30 +00003027/**
3028 * snd_soc_register_platform - Register a platform with the ASoC core
3029 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003030 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003031 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003032int snd_soc_register_platform(struct device *dev,
3033 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003034{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003035 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003036
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3038
3039 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3040 if (platform == NULL)
3041 return -ENOMEM;
3042
3043 /* create platform component name */
3044 platform->name = fmt_single_name(dev, &platform->id);
3045 if (platform->name == NULL) {
3046 kfree(platform);
3047 return -ENOMEM;
3048 }
3049
3050 platform->dev = dev;
3051 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003052
3053 mutex_lock(&client_mutex);
3054 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003055 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003056 mutex_unlock(&client_mutex);
3057
3058 pr_debug("Registered platform '%s'\n", platform->name);
3059
3060 return 0;
3061}
3062EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3063
3064/**
3065 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3066 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003067 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003068 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003069void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003070{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003071 struct snd_soc_platform *platform;
3072
3073 list_for_each_entry(platform, &platform_list, list) {
3074 if (dev == platform->dev)
3075 goto found;
3076 }
3077 return;
3078
3079found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003080 mutex_lock(&client_mutex);
3081 list_del(&platform->list);
3082 mutex_unlock(&client_mutex);
3083
3084 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003085 kfree(platform->name);
3086 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003087}
3088EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3089
Mark Brown151ab222009-05-09 16:22:58 +01003090static u64 codec_format_map[] = {
3091 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3092 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3093 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3094 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3095 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3096 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3097 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3098 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3099 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3100 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3101 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3102 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3103 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3104 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3105 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3106 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3107};
3108
3109/* Fix up the DAI formats for endianness: codecs don't actually see
3110 * the endianness of the data but we're using the CPU format
3111 * definitions which do need to include endianness so we ensure that
3112 * codec DAIs always have both big and little endian variants set.
3113 */
3114static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3115{
3116 int i;
3117
3118 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3119 if (stream->formats & codec_format_map[i])
3120 stream->formats |= codec_format_map[i];
3121}
3122
Mark Brown0d0cf002008-12-10 14:32:45 +00003123/**
3124 * snd_soc_register_codec - Register a codec with the ASoC core
3125 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003126 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003127 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003128int snd_soc_register_codec(struct device *dev,
3129 struct snd_soc_codec_driver *codec_drv,
3130 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003131{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003132 struct snd_soc_codec *codec;
3133 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003134
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003135 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003137 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3138 if (codec == NULL)
3139 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003141 /* create CODEC component name */
3142 codec->name = fmt_single_name(dev, &codec->id);
3143 if (codec->name == NULL) {
3144 kfree(codec);
3145 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003146 }
3147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148 /* allocate CODEC register cache */
3149 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3150
3151 if (codec_drv->reg_cache_default)
3152 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3153 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3154 else
3155 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3156 codec_drv->reg_word_size, GFP_KERNEL);
3157
3158 if (codec->reg_cache == NULL) {
3159 kfree(codec->name);
3160 kfree(codec);
3161 return -ENOMEM;
3162 }
3163 }
3164
3165 codec->dev = dev;
3166 codec->driver = codec_drv;
3167 codec->bias_level = SND_SOC_BIAS_OFF;
3168 codec->num_dai = num_dai;
3169 mutex_init(&codec->mutex);
3170 INIT_LIST_HEAD(&codec->dapm_widgets);
3171 INIT_LIST_HEAD(&codec->dapm_paths);
3172
3173 for (i = 0; i < num_dai; i++) {
3174 fixup_codec_formats(&dai_drv[i].playback);
3175 fixup_codec_formats(&dai_drv[i].capture);
3176 }
3177
Mark Brown26b01cc2010-08-18 20:20:55 +01003178 /* register any DAIs */
3179 if (num_dai) {
3180 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3181 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003182 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003183 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184
Mark Brown0d0cf002008-12-10 14:32:45 +00003185 mutex_lock(&client_mutex);
3186 list_add(&codec->list, &codec_list);
3187 snd_soc_instantiate_cards();
3188 mutex_unlock(&client_mutex);
3189
3190 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003191 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003192
3193error:
3194 for (i--; i >= 0; i--)
3195 snd_soc_unregister_dai(dev);
3196
3197 if (codec->reg_cache)
3198 kfree(codec->reg_cache);
3199 kfree(codec->name);
3200 kfree(codec);
3201 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003202}
3203EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3204
3205/**
3206 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3207 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003208 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003209 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003210void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003211{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003212 struct snd_soc_codec *codec;
3213 int i;
3214
3215 list_for_each_entry(codec, &codec_list, list) {
3216 if (dev == codec->dev)
3217 goto found;
3218 }
3219 return;
3220
3221found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003222 if (codec->num_dai)
3223 for (i = 0; i < codec->num_dai; i++)
3224 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225
Mark Brown0d0cf002008-12-10 14:32:45 +00003226 mutex_lock(&client_mutex);
3227 list_del(&codec->list);
3228 mutex_unlock(&client_mutex);
3229
3230 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003231
3232 if (codec->reg_cache)
3233 kfree(codec->reg_cache);
3234 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003235}
3236EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3237
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003238static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003239{
Mark Brown384c89e2008-12-03 17:34:03 +00003240#ifdef CONFIG_DEBUG_FS
3241 debugfs_root = debugfs_create_dir("asoc", NULL);
3242 if (IS_ERR(debugfs_root) || !debugfs_root) {
3243 printk(KERN_WARNING
3244 "ASoC: Failed to create debugfs directory\n");
3245 debugfs_root = NULL;
3246 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003247
3248 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3249 &codec_list_fops))
3250 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3251
Mark Brownf3208782010-09-15 18:19:07 +01003252 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3253 &dai_list_fops))
3254 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003255#endif
3256
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003257 return platform_driver_register(&soc_driver);
3258}
3259
Mark Brown7d8c16a2008-11-30 22:11:24 +00003260static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003261{
Mark Brown384c89e2008-12-03 17:34:03 +00003262#ifdef CONFIG_DEBUG_FS
3263 debugfs_remove_recursive(debugfs_root);
3264#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003265 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003266}
3267
3268module_init(snd_soc_init);
3269module_exit(snd_soc_exit);
3270
3271/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003272MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003273MODULE_DESCRIPTION("ALSA SoC Core");
3274MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003275MODULE_ALIAS("platform:soc-audio");