blob: f030521c48d16b85a50b8358c6db31e583a198c8 [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 Brownc593b522010-10-27 20:11:17 -0700168 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000169
Mark Brownc593b522010-10-27 20:11:17 -0700170 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
171 if (ret)
172 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000173
174 return count;
175}
176
177static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
178
Mark Brown2624d5f2009-11-03 21:56:13 +0000179#ifdef CONFIG_DEBUG_FS
180static int codec_reg_open_file(struct inode *inode, struct file *file)
181{
182 file->private_data = inode->i_private;
183 return 0;
184}
185
186static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
187 size_t count, loff_t *ppos)
188{
189 ssize_t ret;
190 struct snd_soc_codec *codec = file->private_data;
191 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
192 if (!buf)
193 return -ENOMEM;
194 ret = soc_codec_reg_show(codec, buf);
195 if (ret >= 0)
196 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
197 kfree(buf);
198 return ret;
199}
200
201static ssize_t codec_reg_write_file(struct file *file,
202 const char __user *user_buf, size_t count, loff_t *ppos)
203{
204 char buf[32];
205 int buf_size;
206 char *start = buf;
207 unsigned long reg, value;
208 int step = 1;
209 struct snd_soc_codec *codec = file->private_data;
210
211 buf_size = min(count, (sizeof(buf)-1));
212 if (copy_from_user(buf, user_buf, buf_size))
213 return -EFAULT;
214 buf[buf_size] = 0;
215
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 if (codec->driver->reg_cache_step)
217 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000218
219 while (*start == ' ')
220 start++;
221 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000222 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000223 return -EINVAL;
224 while (*start == ' ')
225 start++;
226 if (strict_strtoul(start, 16, &value))
227 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000228 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000229 return buf_size;
230}
231
232static const struct file_operations codec_reg_fops = {
233 .open = codec_reg_open_file,
234 .read = codec_reg_read_file,
235 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200236 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000237};
238
239static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
240{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200241 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
242
243 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
244 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 if (!codec->debugfs_codec_root) {
246 printk(KERN_WARNING
247 "ASoC: Failed to create codec debugfs directory\n");
248 return;
249 }
250
251 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
252 codec->debugfs_codec_root,
253 codec, &codec_reg_fops);
254 if (!codec->debugfs_reg)
255 printk(KERN_WARNING
256 "ASoC: Failed to create codec register debugfs file\n");
257
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200258 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200260 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 printk(KERN_WARNING
262 "Failed to create DAPM debugfs directory\n");
263
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200264 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000265}
266
267static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
268{
269 debugfs_remove_recursive(codec->debugfs_codec_root);
270}
271
Mark Brownc3c5a192010-09-15 18:15:14 +0100272static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
273 size_t count, loff_t *ppos)
274{
275 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100276 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100277 struct snd_soc_codec *codec;
278
279 if (!buf)
280 return -ENOMEM;
281
Mark Brown2b194f9d2010-10-13 10:52:16 +0100282 list_for_each_entry(codec, &codec_list, list) {
283 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
284 codec->name);
285 if (len >= 0)
286 ret += len;
287 if (ret > PAGE_SIZE) {
288 ret = PAGE_SIZE;
289 break;
290 }
291 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100292
293 if (ret >= 0)
294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
295
296 kfree(buf);
297
298 return ret;
299}
300
301static const struct file_operations codec_list_fops = {
302 .read = codec_list_read_file,
303 .llseek = default_llseek,/* read accesses f_pos */
304};
305
Mark Brownf3208782010-09-15 18:19:07 +0100306static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
307 size_t count, loff_t *ppos)
308{
309 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100310 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100311 struct snd_soc_dai *dai;
312
313 if (!buf)
314 return -ENOMEM;
315
Mark Brown2b194f9d2010-10-13 10:52:16 +0100316 list_for_each_entry(dai, &dai_list, list) {
317 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
318 if (len >= 0)
319 ret += len;
320 if (ret > PAGE_SIZE) {
321 ret = PAGE_SIZE;
322 break;
323 }
324 }
Mark Brownf3208782010-09-15 18:19:07 +0100325
Mark Brown2b194f9d2010-10-13 10:52:16 +0100326 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100327
328 kfree(buf);
329
330 return ret;
331}
332
333static const struct file_operations dai_list_fops = {
334 .read = dai_list_read_file,
335 .llseek = default_llseek,/* read accesses f_pos */
336};
337
Mark Brown19c7ac22010-09-15 18:22:40 +0100338static ssize_t platform_list_read_file(struct file *file,
339 char __user *user_buf,
340 size_t count, loff_t *ppos)
341{
342 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100344 struct snd_soc_platform *platform;
345
346 if (!buf)
347 return -ENOMEM;
348
Mark Brown2b194f9d2010-10-13 10:52:16 +0100349 list_for_each_entry(platform, &platform_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
351 platform->name);
352 if (len >= 0)
353 ret += len;
354 if (ret > PAGE_SIZE) {
355 ret = PAGE_SIZE;
356 break;
357 }
358 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100359
Mark Brown2b194f9d2010-10-13 10:52:16 +0100360 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100361
362 kfree(buf);
363
364 return ret;
365}
366
367static const struct file_operations platform_list_fops = {
368 .read = platform_list_read_file,
369 .llseek = default_llseek,/* read accesses f_pos */
370};
371
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200372static void soc_init_card_debugfs(struct snd_soc_card *card)
373{
374 card->debugfs_card_root = debugfs_create_dir(card->name,
375 debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200376 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200377 dev_warn(card->dev,
378 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200379 return;
380 }
381
382 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
383 card->debugfs_card_root,
384 &card->pop_time);
385 if (!card->debugfs_pop_time)
386 dev_warn(card->dev,
387 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200388}
389
390static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
391{
392 debugfs_remove_recursive(card->debugfs_card_root);
393}
394
Mark Brown2624d5f2009-11-03 21:56:13 +0000395#else
396
397static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
398{
399}
400
401static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
402{
403}
404#endif
405
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406#ifdef CONFIG_SND_SOC_AC97_BUS
407/* unregister ac97 codec */
408static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
409{
410 if (codec->ac97->dev.bus)
411 device_unregister(&codec->ac97->dev);
412 return 0;
413}
414
415/* stop no dev release warning */
416static void soc_ac97_device_release(struct device *dev){}
417
418/* register ac97 codec to bus */
419static int soc_ac97_dev_register(struct snd_soc_codec *codec)
420{
421 int err;
422
423 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100424 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 codec->ac97->dev.release = soc_ac97_device_release;
426
Kay Sieversbb072bf2008-11-02 03:50:35 +0100427 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 err = device_register(&codec->ac97->dev);
430 if (err < 0) {
431 snd_printk(KERN_ERR "Can't register ac97 bus\n");
432 codec->ac97->dev.bus = NULL;
433 return err;
434 }
435 return 0;
436}
437#endif
438
Mark Brown06f409d2009-04-07 18:10:13 +0100439static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
440{
441 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100444 int ret;
445
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
447 rtd->dai_link->symmetric_rates) {
448 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
449 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100450
451 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
452 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453 rtd->rate,
454 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100455 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100457 "Unable to apply rate symmetry constraint: %d\n", ret);
458 return ret;
459 }
460 }
461
462 return 0;
463}
464
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465/*
466 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
467 * then initialized and any private data can be allocated. This also calls
468 * startup for the cpu DAI, platform, machine and codec DAI.
469 */
470static int soc_pcm_open(struct snd_pcm_substream *substream)
471{
472 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 struct snd_soc_platform *platform = rtd->platform;
475 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
476 struct snd_soc_dai *codec_dai = rtd->codec_dai;
477 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
478 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200479 int ret = 0;
480
481 mutex_lock(&pcm_mutex);
482
483 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 if (cpu_dai->driver->ops->startup) {
485 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486 if (ret < 0) {
487 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200489 goto out;
490 }
491 }
492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 if (platform->driver->ops->open) {
494 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 if (ret < 0) {
496 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
497 goto platform_err;
498 }
499 }
500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 if (codec_dai->driver->ops->startup) {
502 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100503 if (ret < 0) {
504 printk(KERN_ERR "asoc: can't open codec %s\n",
505 codec_dai->name);
506 goto codec_dai_err;
507 }
508 }
509
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 goto machine_err;
515 }
516 }
517
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 /* Check that the codec and cpu DAI's are compatible */
519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 max(codec_dai_drv->playback.rate_min,
522 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 min(codec_dai_drv->playback.rate_max,
525 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 max(codec_dai_drv->playback.channels_min,
528 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 min(codec_dai_drv->playback.channels_max,
531 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100532 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
536 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900537 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 runtime->hw.rates |= cpu_dai_drv->playback.rates;
539 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900540 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 } else {
543 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 max(codec_dai_drv->capture.rate_min,
545 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 min(codec_dai_drv->capture.rate_max,
548 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200549 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 max(codec_dai_drv->capture.channels_min,
551 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 min(codec_dai_drv->capture.channels_max,
554 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100555 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100557 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
559 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900560 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 runtime->hw.rates |= cpu_dai_drv->capture.rates;
562 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900563 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565 }
566
567 snd_pcm_limit_hw_rates(runtime);
568 if (!runtime->hw.rates) {
569 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100570 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900571 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 }
573 if (!runtime->hw.formats) {
574 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100575 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900576 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577 }
578 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
579 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900581 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582 }
583
Mark Brown06f409d2009-04-07 18:10:13 +0100584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai->active || codec_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream);
587 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900588 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100589 }
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 pr_debug("asoc: %s <-> %s info:\n",
592 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100593 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
594 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
595 runtime->hw.channels_max);
596 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
597 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598
Jassi Brar14dc5732010-02-26 09:12:32 +0900599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 cpu_dai->playback_active++;
601 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900602 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 cpu_dai->capture_active++;
604 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900605 }
606 cpu_dai->active++;
607 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609 mutex_unlock(&pcm_mutex);
610 return 0;
611
Jassi Brarbb1c0472010-02-25 11:24:53 +0900612config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
614 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615
Jassi Brarbb1c0472010-02-25 11:24:53 +0900616machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 if (codec_dai->driver->ops->shutdown)
618 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900619
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100620codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 if (platform->driver->ops->close)
622 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623
624platform_err:
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 +0200627out:
628 mutex_unlock(&pcm_mutex);
629 return ret;
630}
631
632/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200633 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634 * This is to ensure there are no pops or clicks in between any music tracks
635 * due to DAPM power cycling.
636 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100637static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 struct snd_soc_pcm_runtime *rtd =
640 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200642
643 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
646 codec_dai->driver->playback.stream_name,
647 codec_dai->playback_active ? "active" : "inactive",
648 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 /* are we waiting on this codec DAI stream */
651 if (codec_dai->pop_wait == 1) {
652 codec_dai->pop_wait = 0;
653 snd_soc_dapm_stream_event(rtd,
654 codec_dai->driver->playback.stream_name,
655 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658 mutex_unlock(&pcm_mutex);
659}
660
661/*
662 * Called by ALSA when a PCM substream is closed. Private data can be
663 * freed here. The cpu DAI, codec DAI, machine and platform are also
664 * shutdown.
665 */
666static int soc_codec_close(struct snd_pcm_substream *substream)
667{
668 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 struct snd_soc_platform *platform = rtd->platform;
670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
671 struct snd_soc_dai *codec_dai = rtd->codec_dai;
672 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
674 mutex_lock(&pcm_mutex);
675
Jassi Brar14dc5732010-02-26 09:12:32 +0900676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 cpu_dai->playback_active--;
678 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900679 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 cpu_dai->capture_active--;
681 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900683
684 cpu_dai->active--;
685 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 codec->active--;
687
Mark Brown6010b2d2008-09-06 18:33:24 +0100688 /* Muting the DAC suppresses artifacts caused during digital
689 * shutdown, for example from stopping clocks.
690 */
691 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
692 snd_soc_dai_digital_mute(codec_dai, 1);
693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 if (cpu_dai->driver->ops->shutdown)
695 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 if (codec_dai->driver->ops->shutdown)
698 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
701 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 if (platform->driver->ops->close)
704 platform->driver->ops->close(substream);
705 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706
707 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
708 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100709 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 schedule_delayed_work(&rtd->delayed_work,
711 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 } else {
713 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 snd_soc_dapm_stream_event(rtd,
715 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100716 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 }
718
719 mutex_unlock(&pcm_mutex);
720 return 0;
721}
722
723/*
724 * Called by ALSA when the PCM substream is prepared, can set format, sample
725 * rate, etc. This function is non atomic and can be called multiple times,
726 * it can refer to the runtime info.
727 */
728static int soc_pcm_prepare(struct snd_pcm_substream *substream)
729{
730 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 struct snd_soc_platform *platform = rtd->platform;
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
733 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 int ret = 0;
735
736 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100737
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
739 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100740 if (ret < 0) {
741 printk(KERN_ERR "asoc: machine prepare error\n");
742 goto out;
743 }
744 }
745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (platform->driver->ops->prepare) {
747 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200748 if (ret < 0) {
749 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200751 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752 }
753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (codec_dai->driver->ops->prepare) {
755 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200756 if (ret < 0) {
757 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200759 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 if (cpu_dai->driver->ops->prepare) {
763 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100764 if (ret < 0) {
765 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
766 goto out;
767 }
768 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
Mark Brownd45f6212008-10-14 13:58:36 +0100770 /* cancel any delayed stream shutdown that is pending */
771 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
772 codec_dai->pop_wait) {
773 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100775 }
776
Mark Brown452c5ea2009-05-17 21:41:23 +0100777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 snd_soc_dapm_stream_event(rtd,
779 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100780 SND_SOC_DAPM_STREAM_START);
781 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 snd_soc_dapm_stream_event(rtd,
783 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100784 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100785
Mark Brown452c5ea2009-05-17 21:41:23 +0100786 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787
788out:
789 mutex_unlock(&pcm_mutex);
790 return ret;
791}
792
793/*
794 * Called by ALSA when the hardware params are set by application. This
795 * function can also be called multiple times and can allocate buffers
796 * (using snd_pcm_lib_* ). It's non-atomic.
797 */
798static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
799 struct snd_pcm_hw_params *params)
800{
801 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802 struct snd_soc_platform *platform = rtd->platform;
803 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
804 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 int ret = 0;
806
807 mutex_lock(&pcm_mutex);
808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
810 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100812 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 goto out;
814 }
815 }
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 if (codec_dai->driver->ops->hw_params) {
818 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100819 if (ret < 0) {
820 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
821 codec_dai->name);
822 goto codec_err;
823 }
824 }
825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 if (cpu_dai->driver->ops->hw_params) {
827 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200829 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100830 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200831 goto interface_err;
832 }
833 }
834
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 if (platform->driver->ops->hw_params) {
836 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200837 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200838 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 platform->name);
840 goto platform_err;
841 }
842 }
843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000844 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846out:
847 mutex_unlock(&pcm_mutex);
848 return ret;
849
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200850platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (cpu_dai->driver->ops->hw_free)
852 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853
854interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 if (codec_dai->driver->ops->hw_free)
856 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100857
858codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
860 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861
862 mutex_unlock(&pcm_mutex);
863 return ret;
864}
865
866/*
867 * Free's resources allocated by hw_params, can be called multiple times
868 */
869static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
870{
871 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 struct snd_soc_platform *platform = rtd->platform;
873 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
874 struct snd_soc_dai *codec_dai = rtd->codec_dai;
875 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876
877 mutex_lock(&pcm_mutex);
878
879 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100880 if (!codec->active)
881 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
883 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
885 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886
887 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 if (platform->driver->ops->hw_free)
889 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
891 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 if (codec_dai->driver->ops->hw_free)
893 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895 if (cpu_dai->driver->ops->hw_free)
896 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897
898 mutex_unlock(&pcm_mutex);
899 return 0;
900}
901
902static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
903{
904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 struct snd_soc_platform *platform = rtd->platform;
906 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
907 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 int ret;
909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (codec_dai->driver->ops->trigger) {
911 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912 if (ret < 0)
913 return ret;
914 }
915
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 if (platform->driver->ops->trigger) {
917 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200918 if (ret < 0)
919 return ret;
920 }
921
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 if (cpu_dai->driver->ops->trigger) {
923 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200924 if (ret < 0)
925 return ret;
926 }
927 return 0;
928}
929
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200930/*
931 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200932 * If cpu_dai, codec_dai, platform driver has the delay callback, than
933 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200934 */
935static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
936{
937 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 struct snd_soc_platform *platform = rtd->platform;
939 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
940 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200941 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200942 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200943 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200944
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 if (platform->driver->ops->pointer)
946 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200947
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 if (cpu_dai->driver->ops->delay)
949 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 if (codec_dai->driver->ops->delay)
952 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200953
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000954 if (platform->driver->delay)
955 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200956
957 runtime->delay = delay;
958
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200959 return offset;
960}
961
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200962/* ASoC PCM operations */
963static struct snd_pcm_ops soc_pcm_ops = {
964 .open = soc_pcm_open,
965 .close = soc_codec_close,
966 .hw_params = soc_pcm_hw_params,
967 .hw_free = soc_pcm_hw_free,
968 .prepare = soc_pcm_prepare,
969 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200970 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200971};
972
973#ifdef CONFIG_PM
974/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100975static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976{
Mark Brown416356f2009-06-30 19:05:15 +0100977 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000978 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200979 int i;
980
Daniel Macke3509ff2009-06-03 17:44:49 +0200981 /* If the initialization of this soc device failed, there is no codec
982 * associated with it. Just bail out in this case.
983 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200985 return 0;
986
Andy Green6ed25972008-06-13 16:24:05 +0100987 /* Due to the resume being scheduled into a workqueue we could
988 * suspend before that's finished - wait for it to complete.
989 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000990 snd_power_lock(card->snd_card);
991 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
992 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100993
994 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100996
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200997 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 for (i = 0; i < card->num_rtd; i++) {
999 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1000 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001003 continue;
1004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 if (drv->ops->digital_mute && dai->playback_active)
1006 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001007 }
1008
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001009 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 for (i = 0; i < card->num_rtd; i++) {
1011 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001012 continue;
1013
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001015 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001016
Mark Brown87506542008-11-18 20:50:34 +00001017 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001018 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020 for (i = 0; i < card->num_rtd; i++) {
1021 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1022 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001024 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001025 continue;
1026
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001027 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1028 cpu_dai->driver->suspend(cpu_dai);
1029 if (platform->driver->suspend && !platform->suspended) {
1030 platform->driver->suspend(cpu_dai);
1031 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001032 }
1033 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 /* close any waiting streams and save state */
1036 for (i = 0; i < card->num_rtd; i++) {
1037 run_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001038 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001040
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1043
1044 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001045 continue;
1046
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047 if (driver->playback.stream_name != NULL)
1048 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1049 SND_SOC_DAPM_STREAM_SUSPEND);
1050
1051 if (driver->capture.stream_name != NULL)
1052 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1053 SND_SOC_DAPM_STREAM_SUSPEND);
1054 }
1055
1056 /* suspend all CODECs */
1057 for (i = 0; i < card->num_rtd; i++) {
1058 struct snd_soc_codec *codec = card->rtd[i].codec;
1059 /* If there are paths active then the CODEC will be held with
1060 * bias _ON and should not be suspended. */
1061 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001062 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001063 case SND_SOC_BIAS_STANDBY:
1064 case SND_SOC_BIAS_OFF:
1065 codec->driver->suspend(codec, PMSG_SUSPEND);
1066 codec->suspended = 1;
1067 break;
1068 default:
1069 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1070 break;
1071 }
1072 }
1073 }
1074
1075 for (i = 0; i < card->num_rtd; i++) {
1076 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1077
1078 if (card->rtd[i].dai_link->ignore_suspend)
1079 continue;
1080
1081 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1082 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001083 }
1084
Mark Brown87506542008-11-18 20:50:34 +00001085 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001086 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001087
1088 return 0;
1089}
1090
Andy Green6ed25972008-06-13 16:24:05 +01001091/* deferred resume work, so resume can complete before we finished
1092 * setting our codec back up, which can be very slow on I2C
1093 */
1094static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001096 struct snd_soc_card *card =
1097 container_of(work, struct snd_soc_card, deferred_resume_work);
1098 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001099 int i;
1100
Andy Green6ed25972008-06-13 16:24:05 +01001101 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1102 * so userspace apps are blocked from touching us
1103 */
1104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001106
Mark Brown99497882010-05-07 20:24:05 +01001107 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001108 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001109
Mark Brown87506542008-11-18 20:50:34 +00001110 if (card->resume_pre)
1111 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 /* resume AC97 DAIs */
1114 for (i = 0; i < card->num_rtd; i++) {
1115 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001117 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001118 continue;
1119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1121 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001122 }
1123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124 for (i = 0; i < card->num_rtd; i++) {
1125 struct snd_soc_codec *codec = card->rtd[i].codec;
1126 /* If the CODEC was idle over suspend then it will have been
1127 * left with bias OFF or STANDBY and suspended so we must now
1128 * resume. Otherwise the suspend was suppressed.
1129 */
1130 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001131 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001132 case SND_SOC_BIAS_STANDBY:
1133 case SND_SOC_BIAS_OFF:
1134 codec->driver->resume(codec);
1135 codec->suspended = 0;
1136 break;
1137 default:
1138 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1139 break;
1140 }
Mark Brown1547aba2010-05-07 21:11:40 +01001141 }
1142 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001144 for (i = 0; i < card->num_rtd; i++) {
1145 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001147 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001148 continue;
1149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001150 if (driver->playback.stream_name != NULL)
1151 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001152 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153
1154 if (driver->capture.stream_name != NULL)
1155 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001156 SND_SOC_DAPM_STREAM_RESUME);
1157 }
1158
Mark Brown3ff3f642008-05-19 12:32:25 +02001159 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 for (i = 0; i < card->num_rtd; i++) {
1161 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1162 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001163
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001164 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001165 continue;
1166
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 if (drv->ops->digital_mute && dai->playback_active)
1168 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001169 }
1170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 for (i = 0; i < card->num_rtd; i++) {
1172 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1173 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001176 continue;
1177
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001178 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1179 cpu_dai->driver->resume(cpu_dai);
1180 if (platform->driver->resume && platform->suspended) {
1181 platform->driver->resume(cpu_dai);
1182 platform->suspended = 0;
1183 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001184 }
1185
Mark Brown87506542008-11-18 20:50:34 +00001186 if (card->resume_post)
1187 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001189 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001190
1191 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001193}
1194
1195/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001196static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001197{
Mark Brown416356f2009-06-30 19:05:15 +01001198 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001199 struct snd_soc_card *card = platform_get_drvdata(pdev);
1200 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001201
Mark Brown64ab9ba2009-03-31 11:27:03 +01001202 /* AC97 devices might have other drivers hanging off them so
1203 * need to resume immediately. Other drivers don't have that
1204 * problem and may take a substantial amount of time to resume
1205 * due to I/O costs and anti-pop so handle them out of line.
1206 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001207 for (i = 0; i < card->num_rtd; i++) {
1208 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1209 if (cpu_dai->driver->ac97_control) {
1210 dev_dbg(dev, "Resuming AC97 immediately\n");
1211 soc_resume_deferred(&card->deferred_resume_work);
1212 } else {
1213 dev_dbg(dev, "Scheduling resume work\n");
1214 if (!schedule_work(&card->deferred_resume_work))
1215 dev_err(dev, "resume work item may be lost\n");
1216 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001217 }
Andy Green6ed25972008-06-13 16:24:05 +01001218
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001219 return 0;
1220}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001221#else
1222#define soc_suspend NULL
1223#define soc_resume NULL
1224#endif
1225
Barry Song02a06d32009-10-16 18:13:38 +08001226static struct snd_soc_dai_ops null_dai_ops = {
1227};
1228
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001229static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001230{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001231 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1232 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001233 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001234 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 if (rtd->complete)
1238 return 1;
1239 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001240
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001241 /* do we already have the CPU DAI for this link ? */
1242 if (rtd->cpu_dai) {
1243 goto find_codec;
1244 }
1245 /* no, then find CPU DAI from registered DAIs*/
1246 list_for_each_entry(cpu_dai, &dai_list, list) {
1247 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1248
1249 if (!try_module_get(cpu_dai->dev->driver->owner))
1250 return -ENODEV;
1251
1252 rtd->cpu_dai = cpu_dai;
1253 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001254 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255 }
1256 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1257 dai_link->cpu_dai_name);
1258
1259find_codec:
1260 /* do we already have the CODEC for this link ? */
1261 if (rtd->codec) {
1262 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001263 }
1264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 /* no, then find CODEC from registered CODECs*/
1266 list_for_each_entry(codec, &codec_list, list) {
1267 if (!strcmp(codec->name, dai_link->codec_name)) {
1268 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001269
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001270 if (!try_module_get(codec->dev->driver->owner))
1271 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001272
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001273 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1274 list_for_each_entry(codec_dai, &dai_list, list) {
1275 if (codec->dev == codec_dai->dev &&
1276 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1277 rtd->codec_dai = codec_dai;
1278 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001279 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 }
1281 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1282 dai_link->codec_dai_name);
1283
1284 goto find_platform;
1285 }
1286 }
1287 dev_dbg(card->dev, "CODEC %s not registered\n",
1288 dai_link->codec_name);
1289
1290find_platform:
1291 /* do we already have the CODEC DAI for this link ? */
1292 if (rtd->platform) {
1293 goto out;
1294 }
1295 /* no, then find CPU DAI from registered DAIs*/
1296 list_for_each_entry(platform, &platform_list, list) {
1297 if (!strcmp(platform->name, dai_link->platform_name)) {
1298
1299 if (!try_module_get(platform->dev->driver->owner))
1300 return -ENODEV;
1301
1302 rtd->platform = platform;
1303 goto out;
1304 }
1305 }
1306
1307 dev_dbg(card->dev, "platform %s not registered\n",
1308 dai_link->platform_name);
1309 return 0;
1310
1311out:
1312 /* mark rtd as complete if we found all 4 of our client devices */
1313 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1314 rtd->complete = 1;
1315 card->num_rtd++;
1316 }
1317 return 1;
1318}
1319
1320static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1321{
1322 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1323 struct snd_soc_codec *codec = rtd->codec;
1324 struct snd_soc_platform *platform = rtd->platform;
1325 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1326 int err;
1327
1328 /* unregister the rtd device */
1329 if (rtd->dev_registered) {
1330 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1331 device_unregister(&rtd->dev);
1332 rtd->dev_registered = 0;
1333 }
1334
1335 /* remove the CODEC DAI */
1336 if (codec_dai && codec_dai->probed) {
1337 if (codec_dai->driver->remove) {
1338 err = codec_dai->driver->remove(codec_dai);
1339 if (err < 0)
1340 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1341 }
1342 codec_dai->probed = 0;
1343 list_del(&codec_dai->card_list);
1344 }
1345
1346 /* remove the platform */
1347 if (platform && platform->probed) {
1348 if (platform->driver->remove) {
1349 err = platform->driver->remove(platform);
1350 if (err < 0)
1351 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1352 }
1353 platform->probed = 0;
1354 list_del(&platform->card_list);
1355 module_put(platform->dev->driver->owner);
1356 }
1357
1358 /* remove the CODEC */
1359 if (codec && codec->probed) {
1360 if (codec->driver->remove) {
1361 err = codec->driver->remove(codec);
1362 if (err < 0)
1363 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1364 }
1365
1366 /* Make sure all DAPM widgets are freed */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001367 snd_soc_dapm_free(&codec->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001368
1369 soc_cleanup_codec_debugfs(codec);
1370 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1371 codec->probed = 0;
1372 list_del(&codec->card_list);
1373 module_put(codec->dev->driver->owner);
1374 }
1375
1376 /* remove the cpu_dai */
1377 if (cpu_dai && cpu_dai->probed) {
1378 if (cpu_dai->driver->remove) {
1379 err = cpu_dai->driver->remove(cpu_dai);
1380 if (err < 0)
1381 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1382 }
1383 cpu_dai->probed = 0;
1384 list_del(&cpu_dai->card_list);
1385 module_put(cpu_dai->dev->driver->owner);
1386 }
1387}
1388
1389static void rtd_release(struct device *dev) {}
1390
1391static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1392{
1393 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1394 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1395 struct snd_soc_codec *codec = rtd->codec;
1396 struct snd_soc_platform *platform = rtd->platform;
1397 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1398 int ret;
1399
1400 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1401
1402 /* config components */
1403 codec_dai->codec = codec;
1404 codec->card = card;
1405 cpu_dai->platform = platform;
1406 rtd->card = card;
1407 rtd->dev.parent = card->dev;
1408 codec_dai->card = card;
1409 cpu_dai->card = card;
1410
1411 /* set default power off timeout */
1412 rtd->pmdown_time = pmdown_time;
1413
1414 /* probe the cpu_dai */
1415 if (!cpu_dai->probed) {
1416 if (cpu_dai->driver->probe) {
1417 ret = cpu_dai->driver->probe(cpu_dai);
1418 if (ret < 0) {
1419 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1420 cpu_dai->name);
1421 return ret;
1422 }
1423 }
1424 cpu_dai->probed = 1;
1425 /* mark cpu_dai as probed and add to card cpu_dai list */
1426 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1427 }
1428
1429 /* probe the CODEC */
1430 if (!codec->probed) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001431 codec->dapm.card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001432 if (codec->driver->probe) {
1433 ret = codec->driver->probe(codec);
1434 if (ret < 0) {
1435 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1436 codec->name);
1437 return ret;
1438 }
1439 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001440
1441 soc_init_codec_debugfs(codec);
1442
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443 /* mark codec as probed and add to card codec list */
1444 codec->probed = 1;
1445 list_add(&codec->card_list, &card->codec_dev_list);
1446 }
1447
1448 /* probe the platform */
1449 if (!platform->probed) {
1450 if (platform->driver->probe) {
1451 ret = platform->driver->probe(platform);
1452 if (ret < 0) {
1453 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1454 platform->name);
1455 return ret;
1456 }
1457 }
1458 /* mark platform as probed and add to card platform list */
1459 platform->probed = 1;
1460 list_add(&platform->card_list, &card->platform_dev_list);
1461 }
1462
1463 /* probe the CODEC DAI */
1464 if (!codec_dai->probed) {
1465 if (codec_dai->driver->probe) {
1466 ret = codec_dai->driver->probe(codec_dai);
1467 if (ret < 0) {
1468 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1469 codec_dai->name);
1470 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001471 }
1472 }
1473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474 /* mark cpu_dai as probed and add to card cpu_dai list */
1475 codec_dai->probed = 1;
1476 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001477 }
1478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001479 /* DAPM dai link stream work */
1480 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1481
1482 /* now that all clients have probed, initialise the DAI link */
1483 if (dai_link->init) {
1484 ret = dai_link->init(rtd);
1485 if (ret < 0) {
1486 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1487 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001488 }
1489 }
1490
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491 /* Make sure all DAPM widgets are instantiated */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001492 snd_soc_dapm_new_widgets(&codec->dapm);
1493 snd_soc_dapm_sync(&codec->dapm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 rtd->dev.release = rtd_release;
1497 rtd->dev.init_name = dai_link->name;
1498 ret = device_register(&rtd->dev);
1499 if (ret < 0) {
1500 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1501 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001502 }
1503
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504 rtd->dev_registered = 1;
1505 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1506 if (ret < 0)
1507 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1508
1509 /* add DAPM sysfs entries for this codec */
1510 ret = snd_soc_dapm_sys_add(&rtd->dev);
1511 if (ret < 0)
1512 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1513
1514 /* add codec sysfs entries */
1515 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1516 if (ret < 0)
1517 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 /* create the pcm */
1520 ret = soc_new_pcm(rtd, num);
1521 if (ret < 0) {
1522 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1523 return ret;
1524 }
1525
1526 /* add platform data for AC97 devices */
1527 if (rtd->codec_dai->driver->ac97_control)
1528 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1529
1530 return 0;
1531}
1532
1533#ifdef CONFIG_SND_SOC_AC97_BUS
1534static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1535{
1536 int ret;
1537
1538 /* Only instantiate AC97 if not already done by the adaptor
1539 * for the generic AC97 subsystem.
1540 */
1541 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001542 /*
1543 * It is possible that the AC97 device is already registered to
1544 * the device subsystem. This happens when the device is created
1545 * via snd_ac97_mixer(). Currently only SoC codec that does so
1546 * is the generic AC97 glue but others migh emerge.
1547 *
1548 * In those cases we don't try to register the device again.
1549 */
1550 if (!rtd->codec->ac97_created)
1551 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001552
1553 ret = soc_ac97_dev_register(rtd->codec);
1554 if (ret < 0) {
1555 printk(KERN_ERR "asoc: AC97 device register failed\n");
1556 return ret;
1557 }
1558
1559 rtd->codec->ac97_registered = 1;
1560 }
1561 return 0;
1562}
1563
1564static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1565{
1566 if (codec->ac97_registered) {
1567 soc_ac97_dev_unregister(codec);
1568 codec->ac97_registered = 0;
1569 }
1570}
1571#endif
1572
1573static void snd_soc_instantiate_card(struct snd_soc_card *card)
1574{
1575 struct platform_device *pdev = to_platform_device(card->dev);
1576 int ret, i;
1577
1578 mutex_lock(&card->mutex);
1579
1580 if (card->instantiated) {
1581 mutex_unlock(&card->mutex);
1582 return;
1583 }
1584
1585 /* bind DAIs */
1586 for (i = 0; i < card->num_links; i++)
1587 soc_bind_dai_link(card, i);
1588
1589 /* bind completed ? */
1590 if (card->num_rtd != card->num_links) {
1591 mutex_unlock(&card->mutex);
1592 return;
1593 }
1594
1595 /* card bind complete so register a sound card */
1596 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1597 card->owner, 0, &card->snd_card);
1598 if (ret < 0) {
1599 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1600 card->name);
1601 mutex_unlock(&card->mutex);
1602 return;
1603 }
1604 card->snd_card->dev = card->dev;
1605
Randy Dunlap1301a962008-06-17 19:19:34 +01001606#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001607 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001608 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001609#endif
Andy Green6ed25972008-06-13 16:24:05 +01001610
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611 /* initialise the sound card only once */
1612 if (card->probe) {
1613 ret = card->probe(pdev);
1614 if (ret < 0)
1615 goto card_probe_error;
1616 }
1617
Mark Brownfe3e78e2009-11-03 22:13:13 +00001618 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619 ret = soc_probe_dai_link(card, i);
1620 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001621 pr_err("asoc: failed to instantiate card %s: %d\n",
1622 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623 goto probe_dai_err;
1624 }
1625 }
1626
1627 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1628 "%s", card->name);
1629 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1630 "%s", card->name);
1631
1632 ret = snd_card_register(card->snd_card);
1633 if (ret < 0) {
1634 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1635 goto probe_dai_err;
1636 }
1637
1638#ifdef CONFIG_SND_SOC_AC97_BUS
1639 /* register any AC97 codecs */
1640 for (i = 0; i < card->num_rtd; i++) {
1641 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001642 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1644 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001645 }
1646 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001647#endif
1648
Mark Brown435c5e22008-12-04 15:32:53 +00001649 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001651 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653probe_dai_err:
1654 for (i = 0; i < card->num_links; i++)
1655 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001657card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001658 if (card->remove)
1659 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660
1661 snd_card_free(card->snd_card);
1662
1663 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001664}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001665
Mark Brown435c5e22008-12-04 15:32:53 +00001666/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001667 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001668 * client_mutex.
1669 */
1670static void snd_soc_instantiate_cards(void)
1671{
1672 struct snd_soc_card *card;
1673 list_for_each_entry(card, &card_list, list)
1674 snd_soc_instantiate_card(card);
1675}
1676
1677/* probes a new socdev */
1678static int soc_probe(struct platform_device *pdev)
1679{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001680 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001681 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001682
1683 /* Bodge while we unpick instantiation */
1684 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685 INIT_LIST_HEAD(&card->dai_dev_list);
1686 INIT_LIST_HEAD(&card->codec_dev_list);
1687 INIT_LIST_HEAD(&card->platform_dev_list);
1688
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001689 soc_init_card_debugfs(card);
1690
Mark Brown435c5e22008-12-04 15:32:53 +00001691 ret = snd_soc_register_card(card);
1692 if (ret != 0) {
1693 dev_err(&pdev->dev, "Failed to register card\n");
1694 return ret;
1695 }
1696
1697 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001698}
1699
1700/* removes a socdev */
1701static int soc_remove(struct platform_device *pdev)
1702{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001708 /* make sure any delayed work runs */
1709 for (i = 0; i < card->num_rtd; i++) {
1710 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1711 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001712 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 /* remove and free each DAI */
1715 for (i = 0; i < card->num_rtd; i++)
1716 soc_remove_dai_link(card, i);
1717
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001718 soc_cleanup_card_debugfs(card);
1719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001721 if (card->remove)
1722 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001723
1724 kfree(card->rtd);
1725 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001726 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001727 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001728 return 0;
1729}
1730
Mark Brown416356f2009-06-30 19:05:15 +01001731static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001732{
Mark Brown416356f2009-06-30 19:05:15 +01001733 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001734 struct snd_soc_card *card = platform_get_drvdata(pdev);
1735 int i;
Mark Brown51737472009-06-22 13:16:51 +01001736
1737 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001738 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001739
1740 /* Flush out pmdown_time work - we actually do want to run it
1741 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 for (i = 0; i < card->num_rtd; i++) {
1743 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1744 run_delayed_work(&rtd->delayed_work);
1745 }
Mark Brown51737472009-06-22 13:16:51 +01001746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001748
1749 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001750}
1751
Alexey Dobriyan47145212009-12-14 18:00:08 -08001752static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001753 .suspend = soc_suspend,
1754 .resume = soc_resume,
1755 .poweroff = soc_poweroff,
1756};
1757
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001758/* ASoC platform driver */
1759static struct platform_driver soc_driver = {
1760 .driver = {
1761 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001762 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001763 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764 },
1765 .probe = soc_probe,
1766 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001767};
1768
1769/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 struct snd_soc_codec *codec = rtd->codec;
1773 struct snd_soc_platform *platform = rtd->platform;
1774 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1775 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001776 struct snd_pcm *pcm;
1777 char new_name[64];
1778 int ret = 0, playback = 0, capture = 0;
1779
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001780 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001781 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001782 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001785 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001787 capture = 1;
1788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1790 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1791 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001792 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001793 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001794 return ret;
1795 }
1796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001798 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1800 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1801 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1802 soc_pcm_ops.copy = platform->driver->ops->copy;
1803 soc_pcm_ops.silence = platform->driver->ops->silence;
1804 soc_pcm_ops.ack = platform->driver->ops->ack;
1805 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001806
1807 if (playback)
1808 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1809
1810 if (capture)
1811 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001814 if (ret < 0) {
1815 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816 return ret;
1817 }
1818
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001819 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001820 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1821 cpu_dai->name);
1822 return ret;
1823}
1824
Mark Brown096e49d2009-07-05 15:12:22 +01001825/**
1826 * snd_soc_codec_volatile_register: Report if a register is volatile.
1827 *
1828 * @codec: CODEC to query.
1829 * @reg: Register to query.
1830 *
1831 * Boolean function indiciating if a CODEC register is volatile.
1832 */
1833int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1834{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001835 if (codec->driver->volatile_register)
1836 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001837 else
1838 return 0;
1839}
1840EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1841
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001842/**
1843 * snd_soc_new_ac97_codec - initailise AC97 device
1844 * @codec: audio codec
1845 * @ops: AC97 bus operations
1846 * @num: AC97 codec number
1847 *
1848 * Initialises AC97 codec resources for use by ad-hoc devices only.
1849 */
1850int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1851 struct snd_ac97_bus_ops *ops, int num)
1852{
1853 mutex_lock(&codec->mutex);
1854
1855 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1856 if (codec->ac97 == NULL) {
1857 mutex_unlock(&codec->mutex);
1858 return -ENOMEM;
1859 }
1860
1861 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1862 if (codec->ac97->bus == NULL) {
1863 kfree(codec->ac97);
1864 codec->ac97 = NULL;
1865 mutex_unlock(&codec->mutex);
1866 return -ENOMEM;
1867 }
1868
1869 codec->ac97->bus->ops = ops;
1870 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001871
1872 /*
1873 * Mark the AC97 device to be created by us. This way we ensure that the
1874 * device will be registered with the device subsystem later on.
1875 */
1876 codec->ac97_created = 1;
1877
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 mutex_unlock(&codec->mutex);
1879 return 0;
1880}
1881EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1882
1883/**
1884 * snd_soc_free_ac97_codec - free AC97 codec device
1885 * @codec: audio codec
1886 *
1887 * Frees AC97 codec device resources.
1888 */
1889void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1890{
1891 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001892#ifdef CONFIG_SND_SOC_AC97_BUS
1893 soc_unregister_ac97_dai_link(codec);
1894#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895 kfree(codec->ac97->bus);
1896 kfree(codec->ac97);
1897 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001898 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899 mutex_unlock(&codec->mutex);
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1902
Mark Brownc3753702010-11-01 15:41:57 -04001903unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1904{
1905 unsigned int ret;
1906
1907 ret = codec->driver->read(codec, reg);
1908 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1909
1910 return ret;
1911}
1912EXPORT_SYMBOL_GPL(snd_soc_read);
1913
1914unsigned int snd_soc_write(struct snd_soc_codec *codec,
1915 unsigned int reg, unsigned int val)
1916{
1917 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1918 return codec->driver->write(codec, reg, val);
1919}
1920EXPORT_SYMBOL_GPL(snd_soc_write);
1921
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922/**
1923 * snd_soc_update_bits - update codec register bits
1924 * @codec: audio codec
1925 * @reg: codec register
1926 * @mask: register mask
1927 * @value: new value
1928 *
1929 * Writes new register value.
1930 *
1931 * Returns 1 for change else 0.
1932 */
1933int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001934 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935{
1936 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001937 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001938
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001939 old = snd_soc_read(codec, reg);
1940 new = (old & ~mask) | value;
1941 change = old != new;
1942 if (change)
1943 snd_soc_write(codec, reg, new);
1944
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945 return change;
1946}
1947EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1948
1949/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001950 * snd_soc_update_bits_locked - update codec register bits
1951 * @codec: audio codec
1952 * @reg: codec register
1953 * @mask: register mask
1954 * @value: new value
1955 *
1956 * Writes new register value, and takes the codec mutex.
1957 *
1958 * Returns 1 for change else 0.
1959 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001960int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1961 unsigned short reg, unsigned int mask,
1962 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001963{
1964 int change;
1965
1966 mutex_lock(&codec->mutex);
1967 change = snd_soc_update_bits(codec, reg, mask, value);
1968 mutex_unlock(&codec->mutex);
1969
1970 return change;
1971}
Mark Browndd1b3d52009-12-04 14:22:03 +00001972EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001973
1974/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975 * snd_soc_test_bits - test register for change
1976 * @codec: audio codec
1977 * @reg: codec register
1978 * @mask: register mask
1979 * @value: new value
1980 *
1981 * Tests a register with a new value and checks if the new value is
1982 * different from the old value.
1983 *
1984 * Returns 1 for change else 0.
1985 */
1986int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001987 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988{
1989 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001990 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001992 old = snd_soc_read(codec, reg);
1993 new = (old & ~mask) | value;
1994 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995
1996 return change;
1997}
1998EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1999
2000/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2002 * @substream: the pcm substream
2003 * @hw: the hardware parameters
2004 *
2005 * Sets the substream runtime hardware parameters.
2006 */
2007int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2008 const struct snd_pcm_hardware *hw)
2009{
2010 struct snd_pcm_runtime *runtime = substream->runtime;
2011 runtime->hw.info = hw->info;
2012 runtime->hw.formats = hw->formats;
2013 runtime->hw.period_bytes_min = hw->period_bytes_min;
2014 runtime->hw.period_bytes_max = hw->period_bytes_max;
2015 runtime->hw.periods_min = hw->periods_min;
2016 runtime->hw.periods_max = hw->periods_max;
2017 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2018 runtime->hw.fifo_size = hw->fifo_size;
2019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2022
2023/**
2024 * snd_soc_cnew - create new control
2025 * @_template: control template
2026 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002027 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028 *
2029 * Create a new mixer control from a template control.
2030 *
2031 * Returns 0 for success, else error.
2032 */
2033struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2034 void *data, char *long_name)
2035{
2036 struct snd_kcontrol_new template;
2037
2038 memcpy(&template, _template, sizeof(template));
2039 if (long_name)
2040 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 template.index = 0;
2042
2043 return snd_ctl_new1(&template, data);
2044}
2045EXPORT_SYMBOL_GPL(snd_soc_cnew);
2046
2047/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002048 * snd_soc_add_controls - add an array of controls to a codec.
2049 * Convienience function to add a list of controls. Many codecs were
2050 * duplicating this code.
2051 *
2052 * @codec: codec to add controls to
2053 * @controls: array of controls to add
2054 * @num_controls: number of elements in the array
2055 *
2056 * Return 0 for success, else error.
2057 */
2058int snd_soc_add_controls(struct snd_soc_codec *codec,
2059 const struct snd_kcontrol_new *controls, int num_controls)
2060{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002061 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002062 int err, i;
2063
2064 for (i = 0; i < num_controls; i++) {
2065 const struct snd_kcontrol_new *control = &controls[i];
2066 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2067 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002068 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2069 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002070 return err;
2071 }
2072 }
2073
2074 return 0;
2075}
2076EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2077
2078/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079 * snd_soc_info_enum_double - enumerated double mixer info callback
2080 * @kcontrol: mixer control
2081 * @uinfo: control element information
2082 *
2083 * Callback to provide information about a double enumerated
2084 * mixer control.
2085 *
2086 * Returns 0 for success.
2087 */
2088int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *uinfo)
2090{
2091 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2092
2093 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2094 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002095 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002097 if (uinfo->value.enumerated.item > e->max - 1)
2098 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 strcpy(uinfo->value.enumerated.name,
2100 e->texts[uinfo->value.enumerated.item]);
2101 return 0;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2104
2105/**
2106 * snd_soc_get_enum_double - enumerated double mixer get callback
2107 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002108 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 *
2110 * Callback to get the value of a double enumerated mixer.
2111 *
2112 * Returns 0 for success.
2113 */
2114int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2116{
2117 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2118 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002119 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002120
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002121 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002122 ;
2123 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002124 ucontrol->value.enumerated.item[0]
2125 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126 if (e->shift_l != e->shift_r)
2127 ucontrol->value.enumerated.item[1] =
2128 (val >> e->shift_r) & (bitmask - 1);
2129
2130 return 0;
2131}
2132EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2133
2134/**
2135 * snd_soc_put_enum_double - enumerated double mixer put callback
2136 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002137 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138 *
2139 * Callback to set the value of a double enumerated mixer.
2140 *
2141 * Returns 0 for success.
2142 */
2143int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
2145{
2146 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2147 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002148 unsigned int val;
2149 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002150
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002151 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002152 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002153 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002154 return -EINVAL;
2155 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2156 mask = (bitmask - 1) << e->shift_l;
2157 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002158 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159 return -EINVAL;
2160 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2161 mask |= (bitmask - 1) << e->shift_r;
2162 }
2163
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002164 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165}
2166EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2167
2168/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002169 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2170 * @kcontrol: mixer control
2171 * @ucontrol: control element information
2172 *
2173 * Callback to get the value of a double semi enumerated mixer.
2174 *
2175 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2176 * used for handling bitfield coded enumeration for example.
2177 *
2178 * Returns 0 for success.
2179 */
2180int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2182{
2183 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002184 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002185 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002186
2187 reg_val = snd_soc_read(codec, e->reg);
2188 val = (reg_val >> e->shift_l) & e->mask;
2189 for (mux = 0; mux < e->max; mux++) {
2190 if (val == e->values[mux])
2191 break;
2192 }
2193 ucontrol->value.enumerated.item[0] = mux;
2194 if (e->shift_l != e->shift_r) {
2195 val = (reg_val >> e->shift_r) & e->mask;
2196 for (mux = 0; mux < e->max; mux++) {
2197 if (val == e->values[mux])
2198 break;
2199 }
2200 ucontrol->value.enumerated.item[1] = mux;
2201 }
2202
2203 return 0;
2204}
2205EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2206
2207/**
2208 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2209 * @kcontrol: mixer control
2210 * @ucontrol: control element information
2211 *
2212 * Callback to set the value of a double semi enumerated mixer.
2213 *
2214 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2215 * used for handling bitfield coded enumeration for example.
2216 *
2217 * Returns 0 for success.
2218 */
2219int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2221{
2222 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002223 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002224 unsigned int val;
2225 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002226
2227 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2228 return -EINVAL;
2229 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2230 mask = e->mask << e->shift_l;
2231 if (e->shift_l != e->shift_r) {
2232 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2233 return -EINVAL;
2234 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2235 mask |= e->mask << e->shift_r;
2236 }
2237
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002238 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002239}
2240EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2241
2242/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002243 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2244 * @kcontrol: mixer control
2245 * @uinfo: control element information
2246 *
2247 * Callback to provide information about an external enumerated
2248 * single mixer.
2249 *
2250 * Returns 0 for success.
2251 */
2252int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_info *uinfo)
2254{
2255 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2256
2257 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2258 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002259 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002261 if (uinfo->value.enumerated.item > e->max - 1)
2262 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263 strcpy(uinfo->value.enumerated.name,
2264 e->texts[uinfo->value.enumerated.item]);
2265 return 0;
2266}
2267EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2268
2269/**
2270 * snd_soc_info_volsw_ext - external single mixer info callback
2271 * @kcontrol: mixer control
2272 * @uinfo: control element information
2273 *
2274 * Callback to provide information about a single external mixer control.
2275 *
2276 * Returns 0 for success.
2277 */
2278int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2279 struct snd_ctl_elem_info *uinfo)
2280{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002281 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282
Mark Brownfd5dfad2009-04-15 21:37:46 +01002283 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002284 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2285 else
2286 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2287
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002288 uinfo->count = 1;
2289 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002290 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002291 return 0;
2292}
2293EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2294
2295/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002296 * snd_soc_info_volsw - single mixer info callback
2297 * @kcontrol: mixer control
2298 * @uinfo: control element information
2299 *
2300 * Callback to provide information about a single mixer control.
2301 *
2302 * Returns 0 for success.
2303 */
2304int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_info *uinfo)
2306{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002307 struct soc_mixer_control *mc =
2308 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002309 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002310 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04002311 unsigned int rshift = mc->rshift;
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 = shift == rshift ? 1 : 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);
2328
2329/**
2330 * snd_soc_get_volsw - single 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 single mixer control.
2335 *
2336 * Returns 0 for success.
2337 */
2338int snd_soc_get_volsw(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 shift = mc->shift;
2346 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002347 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002348 unsigned int mask = (1 << fls(max)) - 1;
2349 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 if (shift != rshift)
2354 ucontrol->value.integer.value[1] =
2355 (snd_soc_read(codec, reg) >> rshift) & mask;
2356 if (invert) {
2357 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002358 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359 if (shift != rshift)
2360 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002361 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362 }
2363
2364 return 0;
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2367
2368/**
2369 * snd_soc_put_volsw - single mixer put callback
2370 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002371 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372 *
2373 * Callback to set the value of a single mixer control.
2374 *
2375 * Returns 0 for success.
2376 */
2377int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002380 struct soc_mixer_control *mc =
2381 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002382 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002383 unsigned int reg = mc->reg;
2384 unsigned int shift = mc->shift;
2385 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002386 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002387 unsigned int mask = (1 << fls(max)) - 1;
2388 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002389 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390
2391 val = (ucontrol->value.integer.value[0] & mask);
2392 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002393 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394 val_mask = mask << shift;
2395 val = val << shift;
2396 if (shift != rshift) {
2397 val2 = (ucontrol->value.integer.value[1] & mask);
2398 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002399 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 val_mask |= mask << rshift;
2401 val |= val2 << rshift;
2402 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002403 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404}
2405EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2406
2407/**
2408 * snd_soc_info_volsw_2r - double mixer info callback
2409 * @kcontrol: mixer control
2410 * @uinfo: control element information
2411 *
2412 * Callback to provide information about a double mixer control that
2413 * spans 2 codec registers.
2414 *
2415 * Returns 0 for success.
2416 */
2417int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2418 struct snd_ctl_elem_info *uinfo)
2419{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002420 struct soc_mixer_control *mc =
2421 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002422 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002424 if (!mc->platform_max)
2425 mc->platform_max = mc->max;
2426 platform_max = mc->platform_max;
2427
2428 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002429 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2430 else
2431 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2432
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433 uinfo->count = 2;
2434 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002435 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436 return 0;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2439
2440/**
2441 * snd_soc_get_volsw_2r - double mixer get callback
2442 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002443 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444 *
2445 * Callback to get the value of a double mixer control that spans 2 registers.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_get_volsw_2r(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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002455 unsigned int reg = mc->reg;
2456 unsigned int reg2 = mc->rreg;
2457 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002458 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002459 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002460 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461
2462 ucontrol->value.integer.value[0] =
2463 (snd_soc_read(codec, reg) >> shift) & mask;
2464 ucontrol->value.integer.value[1] =
2465 (snd_soc_read(codec, reg2) >> shift) & mask;
2466 if (invert) {
2467 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002468 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002470 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 }
2472
2473 return 0;
2474}
2475EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2476
2477/**
2478 * snd_soc_put_volsw_2r - double mixer set callback
2479 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002480 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002481 *
2482 * Callback to set the value of a double mixer control that spans 2 registers.
2483 *
2484 * Returns 0 for success.
2485 */
2486int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_value *ucontrol)
2488{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002489 struct soc_mixer_control *mc =
2490 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002491 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002492 unsigned int reg = mc->reg;
2493 unsigned int reg2 = mc->rreg;
2494 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002495 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002496 unsigned int mask = (1 << fls(max)) - 1;
2497 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002499 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002500
2501 val_mask = mask << shift;
2502 val = (ucontrol->value.integer.value[0] & mask);
2503 val2 = (ucontrol->value.integer.value[1] & mask);
2504
2505 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002506 val = max - val;
2507 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508 }
2509
2510 val = val << shift;
2511 val2 = val2 << shift;
2512
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002513 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002514 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515 return err;
2516
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002517 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 return err;
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2521
Mark Browne13ac2e2008-05-28 17:58:05 +01002522/**
2523 * snd_soc_info_volsw_s8 - signed mixer info callback
2524 * @kcontrol: mixer control
2525 * @uinfo: control element information
2526 *
2527 * Callback to provide information about a signed mixer control.
2528 *
2529 * Returns 0 for success.
2530 */
2531int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_info *uinfo)
2533{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002534 struct soc_mixer_control *mc =
2535 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002536 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002537 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002538
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002539 if (!mc->platform_max)
2540 mc->platform_max = mc->max;
2541 platform_max = mc->platform_max;
2542
Mark Browne13ac2e2008-05-28 17:58:05 +01002543 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2544 uinfo->count = 2;
2545 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002546 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002547 return 0;
2548}
2549EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2550
2551/**
2552 * snd_soc_get_volsw_s8 - signed mixer get callback
2553 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002554 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002555 *
2556 * Callback to get the value of a signed mixer control.
2557 *
2558 * Returns 0 for success.
2559 */
2560int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2562{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002563 struct soc_mixer_control *mc =
2564 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002565 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002566 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002567 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002568 int val = snd_soc_read(codec, reg);
2569
2570 ucontrol->value.integer.value[0] =
2571 ((signed char)(val & 0xff))-min;
2572 ucontrol->value.integer.value[1] =
2573 ((signed char)((val >> 8) & 0xff))-min;
2574 return 0;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2577
2578/**
2579 * snd_soc_put_volsw_sgn - signed mixer put callback
2580 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002581 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002582 *
2583 * Callback to set the value of a signed mixer control.
2584 *
2585 * Returns 0 for success.
2586 */
2587int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_value *ucontrol)
2589{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002590 struct soc_mixer_control *mc =
2591 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002592 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002593 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002594 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002595 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002596
2597 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2598 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2599
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002600 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002601}
2602EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2603
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002604/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002605 * snd_soc_limit_volume - Set new limit to an existing volume control.
2606 *
2607 * @codec: where to look for the control
2608 * @name: Name of the control
2609 * @max: new maximum limit
2610 *
2611 * Return 0 for success, else error.
2612 */
2613int snd_soc_limit_volume(struct snd_soc_codec *codec,
2614 const char *name, int max)
2615{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002616 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002617 struct snd_kcontrol *kctl;
2618 struct soc_mixer_control *mc;
2619 int found = 0;
2620 int ret = -EINVAL;
2621
2622 /* Sanity check for name and max */
2623 if (unlikely(!name || max <= 0))
2624 return -EINVAL;
2625
2626 list_for_each_entry(kctl, &card->controls, list) {
2627 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2628 found = 1;
2629 break;
2630 }
2631 }
2632 if (found) {
2633 mc = (struct soc_mixer_control *)kctl->private_value;
2634 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002635 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002636 ret = 0;
2637 }
2638 }
2639 return ret;
2640}
2641EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2642
2643/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002644 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2645 * mixer info callback
2646 * @kcontrol: mixer control
2647 * @uinfo: control element information
2648 *
2649 * Returns 0 for success.
2650 */
2651int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_info *uinfo)
2653{
2654 struct soc_mixer_control *mc =
2655 (struct soc_mixer_control *)kcontrol->private_value;
2656 int max = mc->max;
2657 int min = mc->min;
2658
2659 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2660 uinfo->count = 2;
2661 uinfo->value.integer.min = 0;
2662 uinfo->value.integer.max = max-min;
2663
2664 return 0;
2665}
2666EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2667
2668/**
2669 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2670 * mixer get callback
2671 * @kcontrol: mixer control
2672 * @uinfo: control element information
2673 *
2674 * Returns 0 for success.
2675 */
2676int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2678{
2679 struct soc_mixer_control *mc =
2680 (struct soc_mixer_control *)kcontrol->private_value;
2681 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2682 unsigned int mask = (1<<mc->shift)-1;
2683 int min = mc->min;
2684 int val = snd_soc_read(codec, mc->reg) & mask;
2685 int valr = snd_soc_read(codec, mc->rreg) & mask;
2686
Stuart Longland20630c72010-06-18 12:56:10 +10002687 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2688 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2692
2693/**
2694 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2695 * mixer put callback
2696 * @kcontrol: mixer control
2697 * @uinfo: control element information
2698 *
2699 * Returns 0 for success.
2700 */
2701int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2703{
2704 struct soc_mixer_control *mc =
2705 (struct soc_mixer_control *)kcontrol->private_value;
2706 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2707 unsigned int mask = (1<<mc->shift)-1;
2708 int min = mc->min;
2709 int ret;
2710 unsigned int val, valr, oval, ovalr;
2711
2712 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2713 val &= mask;
2714 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2715 valr &= mask;
2716
2717 oval = snd_soc_read(codec, mc->reg) & mask;
2718 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2719
2720 ret = 0;
2721 if (oval != val) {
2722 ret = snd_soc_write(codec, mc->reg, val);
2723 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002724 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002725 }
2726 if (ovalr != valr) {
2727 ret = snd_soc_write(codec, mc->rreg, valr);
2728 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002729 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002730 }
2731
2732 return 0;
2733}
2734EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2735
2736/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002737 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2738 * @dai: DAI
2739 * @clk_id: DAI specific clock ID
2740 * @freq: new clock frequency in Hz
2741 * @dir: new clock direction - input/output.
2742 *
2743 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2744 */
2745int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2746 unsigned int freq, int dir)
2747{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002748 if (dai->driver && dai->driver->ops->set_sysclk)
2749 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002750 else
2751 return -EINVAL;
2752}
2753EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2754
2755/**
2756 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2757 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002758 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002759 * @div: new clock divisor.
2760 *
2761 * Configures the clock dividers. This is used to derive the best DAI bit and
2762 * frame clocks from the system or master clock. It's best to set the DAI bit
2763 * and frame clocks as low as possible to save system power.
2764 */
2765int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2766 int div_id, int div)
2767{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002768 if (dai->driver && dai->driver->ops->set_clkdiv)
2769 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002770 else
2771 return -EINVAL;
2772}
2773EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2774
2775/**
2776 * snd_soc_dai_set_pll - configure DAI PLL.
2777 * @dai: DAI
2778 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002779 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002780 * @freq_in: PLL input clock frequency in Hz
2781 * @freq_out: requested PLL output clock frequency in Hz
2782 *
2783 * Configures and enables PLL to generate output clock based on input clock.
2784 */
Mark Brown85488032009-09-05 18:52:16 +01002785int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2786 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002787{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002788 if (dai->driver && dai->driver->ops->set_pll)
2789 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002790 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002791 else
2792 return -EINVAL;
2793}
2794EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2795
2796/**
2797 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2798 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002799 * @fmt: SND_SOC_DAIFMT_ format value.
2800 *
2801 * Configures the DAI hardware format and clocking.
2802 */
2803int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2804{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002805 if (dai->driver && dai->driver->ops->set_fmt)
2806 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002807 else
2808 return -EINVAL;
2809}
2810EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2811
2812/**
2813 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2814 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002815 * @tx_mask: bitmask representing active TX slots.
2816 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002817 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002818 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002819 *
2820 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2821 * specific.
2822 */
2823int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002824 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002825{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002826 if (dai->driver && dai->driver->ops->set_tdm_slot)
2827 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002828 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002829 else
2830 return -EINVAL;
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2833
2834/**
Barry Song472df3c2009-09-12 01:16:29 +08002835 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2836 * @dai: DAI
2837 * @tx_num: how many TX channels
2838 * @tx_slot: pointer to an array which imply the TX slot number channel
2839 * 0~num-1 uses
2840 * @rx_num: how many RX channels
2841 * @rx_slot: pointer to an array which imply the RX slot number channel
2842 * 0~num-1 uses
2843 *
2844 * configure the relationship between channel number and TDM slot number.
2845 */
2846int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2847 unsigned int tx_num, unsigned int *tx_slot,
2848 unsigned int rx_num, unsigned int *rx_slot)
2849{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002850 if (dai->driver && dai->driver->ops->set_channel_map)
2851 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002852 rx_num, rx_slot);
2853 else
2854 return -EINVAL;
2855}
2856EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2857
2858/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002859 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2860 * @dai: DAI
2861 * @tristate: tristate enable
2862 *
2863 * Tristates the DAI so that others can use it.
2864 */
2865int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2866{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002867 if (dai->driver && dai->driver->ops->set_tristate)
2868 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002869 else
2870 return -EINVAL;
2871}
2872EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2873
2874/**
2875 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2876 * @dai: DAI
2877 * @mute: mute enable
2878 *
2879 * Mutes the DAI DAC.
2880 */
2881int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2882{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002883 if (dai->driver && dai->driver->ops->digital_mute)
2884 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002885 else
2886 return -EINVAL;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2889
Mark Brownc5af3a22008-11-28 13:29:45 +00002890/**
2891 * snd_soc_register_card - Register a card with the ASoC core
2892 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002893 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002894 *
2895 * Note that currently this is an internal only function: it will be
2896 * exposed to machine drivers after further backporting of ASoC v2
2897 * registration APIs.
2898 */
2899static int snd_soc_register_card(struct snd_soc_card *card)
2900{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002901 int i;
2902
Mark Brownc5af3a22008-11-28 13:29:45 +00002903 if (!card->name || !card->dev)
2904 return -EINVAL;
2905
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002906 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2907 GFP_KERNEL);
2908 if (card->rtd == NULL)
2909 return -ENOMEM;
2910
2911 for (i = 0; i < card->num_links; i++)
2912 card->rtd[i].dai_link = &card->dai_link[i];
2913
Mark Brownc5af3a22008-11-28 13:29:45 +00002914 INIT_LIST_HEAD(&card->list);
2915 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002916 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002917
2918 mutex_lock(&client_mutex);
2919 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002920 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002921 mutex_unlock(&client_mutex);
2922
2923 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2924
2925 return 0;
2926}
2927
2928/**
2929 * snd_soc_unregister_card - Unregister a card with the ASoC core
2930 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002931 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002932 *
2933 * Note that currently this is an internal only function: it will be
2934 * exposed to machine drivers after further backporting of ASoC v2
2935 * registration APIs.
2936 */
2937static int snd_soc_unregister_card(struct snd_soc_card *card)
2938{
2939 mutex_lock(&client_mutex);
2940 list_del(&card->list);
2941 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002942 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2943
2944 return 0;
2945}
2946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002947/*
2948 * Simplify DAI link configuration by removing ".-1" from device names
2949 * and sanitizing names.
2950 */
2951static inline char *fmt_single_name(struct device *dev, int *id)
2952{
2953 char *found, name[NAME_SIZE];
2954 int id1, id2;
2955
2956 if (dev_name(dev) == NULL)
2957 return NULL;
2958
2959 strncpy(name, dev_name(dev), NAME_SIZE);
2960
2961 /* are we a "%s.%d" name (platform and SPI components) */
2962 found = strstr(name, dev->driver->name);
2963 if (found) {
2964 /* get ID */
2965 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2966
2967 /* discard ID from name if ID == -1 */
2968 if (*id == -1)
2969 found[strlen(dev->driver->name)] = '\0';
2970 }
2971
2972 } else {
2973 /* I2C component devices are named "bus-addr" */
2974 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2975 char tmp[NAME_SIZE];
2976
2977 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002978 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002979
2980 /* sanitize component name for DAI link creation */
2981 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2982 strncpy(name, tmp, NAME_SIZE);
2983 } else
2984 *id = 0;
2985 }
2986
2987 return kstrdup(name, GFP_KERNEL);
2988}
2989
2990/*
2991 * Simplify DAI link naming for single devices with multiple DAIs by removing
2992 * any ".-1" and using the DAI name (instead of device name).
2993 */
2994static inline char *fmt_multiple_name(struct device *dev,
2995 struct snd_soc_dai_driver *dai_drv)
2996{
2997 if (dai_drv->name == NULL) {
2998 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2999 dev_name(dev));
3000 return NULL;
3001 }
3002
3003 return kstrdup(dai_drv->name, GFP_KERNEL);
3004}
3005
Mark Brown91151712008-11-30 23:31:24 +00003006/**
3007 * snd_soc_register_dai - Register a DAI with the ASoC core
3008 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003009 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003010 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011int snd_soc_register_dai(struct device *dev,
3012 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003013{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003014 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003016 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003017
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3019 if (dai == NULL)
3020 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003022 /* create DAI component name */
3023 dai->name = fmt_single_name(dev, &dai->id);
3024 if (dai->name == NULL) {
3025 kfree(dai);
3026 return -ENOMEM;
3027 }
3028
3029 dai->dev = dev;
3030 dai->driver = dai_drv;
3031 if (!dai->driver->ops)
3032 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003033
3034 mutex_lock(&client_mutex);
3035 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003036 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003037 mutex_unlock(&client_mutex);
3038
3039 pr_debug("Registered DAI '%s'\n", dai->name);
3040
3041 return 0;
3042}
3043EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3044
3045/**
3046 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3047 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003048 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003049 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003050void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003051{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003052 struct snd_soc_dai *dai;
3053
3054 list_for_each_entry(dai, &dai_list, list) {
3055 if (dev == dai->dev)
3056 goto found;
3057 }
3058 return;
3059
3060found:
Mark Brown91151712008-11-30 23:31:24 +00003061 mutex_lock(&client_mutex);
3062 list_del(&dai->list);
3063 mutex_unlock(&client_mutex);
3064
3065 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003066 kfree(dai->name);
3067 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003068}
3069EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3070
3071/**
3072 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3073 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003074 * @dai: Array of DAIs to register
3075 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003076 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003077int snd_soc_register_dais(struct device *dev,
3078 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003079{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003080 struct snd_soc_dai *dai;
3081 int i, ret = 0;
3082
Liam Girdwood720ffa42010-08-18 00:30:30 +01003083 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003084
3085 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003086
3087 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003088 if (dai == NULL) {
3089 ret = -ENOMEM;
3090 goto err;
3091 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003092
3093 /* create DAI component name */
3094 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3095 if (dai->name == NULL) {
3096 kfree(dai);
3097 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003098 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003099 }
3100
3101 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003102 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003103 if (dai->driver->id)
3104 dai->id = dai->driver->id;
3105 else
3106 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003107 if (!dai->driver->ops)
3108 dai->driver->ops = &null_dai_ops;
3109
3110 mutex_lock(&client_mutex);
3111 list_add(&dai->list, &dai_list);
3112 mutex_unlock(&client_mutex);
3113
3114 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003115 }
3116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003117 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003118 return 0;
3119
3120err:
3121 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003123
3124 return ret;
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3127
3128/**
3129 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3130 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003131 * @dai: Array of DAIs to unregister
3132 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003133 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003134void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003135{
3136 int i;
3137
3138 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003139 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003140}
3141EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3142
Mark Brown12a48a8c2008-12-03 19:40:30 +00003143/**
3144 * snd_soc_register_platform - Register a platform with the ASoC core
3145 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003146 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003147 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148int snd_soc_register_platform(struct device *dev,
3149 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003150{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003151 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003153 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3154
3155 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3156 if (platform == NULL)
3157 return -ENOMEM;
3158
3159 /* create platform component name */
3160 platform->name = fmt_single_name(dev, &platform->id);
3161 if (platform->name == NULL) {
3162 kfree(platform);
3163 return -ENOMEM;
3164 }
3165
3166 platform->dev = dev;
3167 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003168
3169 mutex_lock(&client_mutex);
3170 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003171 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003172 mutex_unlock(&client_mutex);
3173
3174 pr_debug("Registered platform '%s'\n", platform->name);
3175
3176 return 0;
3177}
3178EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3179
3180/**
3181 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3182 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003183 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003184 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003185void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003186{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187 struct snd_soc_platform *platform;
3188
3189 list_for_each_entry(platform, &platform_list, list) {
3190 if (dev == platform->dev)
3191 goto found;
3192 }
3193 return;
3194
3195found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003196 mutex_lock(&client_mutex);
3197 list_del(&platform->list);
3198 mutex_unlock(&client_mutex);
3199
3200 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003201 kfree(platform->name);
3202 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003203}
3204EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3205
Mark Brown151ab222009-05-09 16:22:58 +01003206static u64 codec_format_map[] = {
3207 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3208 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3209 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3210 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3211 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3212 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3213 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3214 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3215 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3216 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3217 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3218 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3219 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3220 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3221 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3222 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3223};
3224
3225/* Fix up the DAI formats for endianness: codecs don't actually see
3226 * the endianness of the data but we're using the CPU format
3227 * definitions which do need to include endianness so we ensure that
3228 * codec DAIs always have both big and little endian variants set.
3229 */
3230static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3231{
3232 int i;
3233
3234 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3235 if (stream->formats & codec_format_map[i])
3236 stream->formats |= codec_format_map[i];
3237}
3238
Mark Brown0d0cf002008-12-10 14:32:45 +00003239/**
3240 * snd_soc_register_codec - Register a codec with the ASoC core
3241 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003242 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003243 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003244int snd_soc_register_codec(struct device *dev,
3245 struct snd_soc_codec_driver *codec_drv,
3246 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003247{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 struct snd_soc_codec *codec;
3249 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003251 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003252
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003253 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3254 if (codec == NULL)
3255 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003257 /* create CODEC component name */
3258 codec->name = fmt_single_name(dev, &codec->id);
3259 if (codec->name == NULL) {
3260 kfree(codec);
3261 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003262 }
3263
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003264 INIT_LIST_HEAD(&codec->dapm.widgets);
3265 INIT_LIST_HEAD(&codec->dapm.paths);
3266 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3267 codec->dapm.dev = dev;
3268 codec->dapm.codec = codec;
3269
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003270 /* allocate CODEC register cache */
3271 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3272
3273 if (codec_drv->reg_cache_default)
3274 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3275 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3276 else
3277 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3278 codec_drv->reg_word_size, GFP_KERNEL);
3279
3280 if (codec->reg_cache == NULL) {
3281 kfree(codec->name);
3282 kfree(codec);
3283 return -ENOMEM;
3284 }
3285 }
3286
3287 codec->dev = dev;
3288 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003289 codec->num_dai = num_dai;
3290 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003291
3292 for (i = 0; i < num_dai; i++) {
3293 fixup_codec_formats(&dai_drv[i].playback);
3294 fixup_codec_formats(&dai_drv[i].capture);
3295 }
3296
Mark Brown26b01cc2010-08-18 20:20:55 +01003297 /* register any DAIs */
3298 if (num_dai) {
3299 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3300 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003301 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003302 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003303
Mark Brown0d0cf002008-12-10 14:32:45 +00003304 mutex_lock(&client_mutex);
3305 list_add(&codec->list, &codec_list);
3306 snd_soc_instantiate_cards();
3307 mutex_unlock(&client_mutex);
3308
3309 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003310 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003311
3312error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003313 if (codec->reg_cache)
3314 kfree(codec->reg_cache);
3315 kfree(codec->name);
3316 kfree(codec);
3317 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003318}
3319EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3320
3321/**
3322 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3323 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003324 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003325 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003327{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003328 struct snd_soc_codec *codec;
3329 int i;
3330
3331 list_for_each_entry(codec, &codec_list, list) {
3332 if (dev == codec->dev)
3333 goto found;
3334 }
3335 return;
3336
3337found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003338 if (codec->num_dai)
3339 for (i = 0; i < codec->num_dai; i++)
3340 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003341
Mark Brown0d0cf002008-12-10 14:32:45 +00003342 mutex_lock(&client_mutex);
3343 list_del(&codec->list);
3344 mutex_unlock(&client_mutex);
3345
3346 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003347
3348 if (codec->reg_cache)
3349 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003350 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003352}
3353EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3354
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003355static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003356{
Mark Brown384c89e2008-12-03 17:34:03 +00003357#ifdef CONFIG_DEBUG_FS
3358 debugfs_root = debugfs_create_dir("asoc", NULL);
3359 if (IS_ERR(debugfs_root) || !debugfs_root) {
3360 printk(KERN_WARNING
3361 "ASoC: Failed to create debugfs directory\n");
3362 debugfs_root = NULL;
3363 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003364
3365 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3366 &codec_list_fops))
3367 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3368
Mark Brownf3208782010-09-15 18:19:07 +01003369 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3370 &dai_list_fops))
3371 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003372
3373 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3374 &platform_list_fops))
3375 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003376#endif
3377
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003378 return platform_driver_register(&soc_driver);
3379}
Mark Brown4abe8e12010-10-12 17:41:03 +01003380module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003381
Mark Brown7d8c16a2008-11-30 22:11:24 +00003382static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003383{
Mark Brown384c89e2008-12-03 17:34:03 +00003384#ifdef CONFIG_DEBUG_FS
3385 debugfs_remove_recursive(debugfs_root);
3386#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003387 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003388}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003389module_exit(snd_soc_exit);
3390
3391/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003392MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003393MODULE_DESCRIPTION("ALSA SoC Core");
3394MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003395MODULE_ALIAS("platform:soc-audio");