blob: 96bf381683f7e68566ff60578ddb97600f6778b5 [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>
Mark Brown3028eb82010-12-05 12:22:46 +000036#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/initval.h>
41
Mark Browna8b1d342010-11-03 18:05:58 -040042#define CREATE_TRACE_POINTS
43#include <trace/events/asoc.h>
44
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045#define NAME_SIZE 32
46
Frank Mandarinodb2a4162006-10-06 18:31:09 +020047static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020048static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
51static struct dentry *debugfs_root;
52#endif
53
Mark Brownc5af3a22008-11-28 13:29:45 +000054static DEFINE_MUTEX(client_mutex);
55static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000056static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000057static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000058static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000059
60static int snd_soc_register_card(struct snd_soc_card *card);
61static int snd_soc_unregister_card(struct snd_soc_card *card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000062static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000063
Frank Mandarinodb2a4162006-10-06 18:31:09 +020064/*
65 * This is a timeout to do a DAPM powerdown after a stream is closed().
66 * It can be used to eliminate pops between different playback streams, e.g.
67 * between two audio tracks.
68 */
69static int pmdown_time = 5000;
70module_param(pmdown_time, int, 0);
71MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72
Mark Brown2624d5f2009-11-03 21:56:13 +000073/* codec register dump */
74static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
75{
Mark Brown5164d742010-07-14 16:14:33 +010076 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000077
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000078 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000079 return 0;
80
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000081 if (codec->driver->reg_cache_step)
82 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000083
84 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000085 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
86 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +000087 continue;
88
89 count += sprintf(buf + count, "%2x: ", i);
90 if (count >= PAGE_SIZE - 1)
91 break;
92
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000093 if (codec->driver->display_register) {
94 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +000095 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +010096 } else {
97 /* If the read fails it's almost certainly due to
98 * the register being volatile and the device being
99 * powered off.
100 */
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000101 ret = snd_soc_read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100102 if (ret >= 0)
103 count += snprintf(buf + count,
104 PAGE_SIZE - count,
105 "%4x", ret);
106 else
107 count += snprintf(buf + count,
108 PAGE_SIZE - count,
109 "<no data: %d>", ret);
110 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000111
112 if (count >= PAGE_SIZE - 1)
113 break;
114
115 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
116 if (count >= PAGE_SIZE - 1)
117 break;
118 }
119
120 /* Truncate count; min() would cause a warning */
121 if (count >= PAGE_SIZE)
122 count = PAGE_SIZE - 1;
123
124 return count;
125}
126static ssize_t codec_reg_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
128{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000129 struct snd_soc_pcm_runtime *rtd =
130 container_of(dev, struct snd_soc_pcm_runtime, dev);
131
132 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000133}
134
135static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
136
Mark Browndbe21402010-02-12 11:37:24 +0000137static ssize_t pmdown_time_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 struct snd_soc_pcm_runtime *rtd =
141 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000144}
145
146static ssize_t pmdown_time_set(struct device *dev,
147 struct device_attribute *attr,
148 const char *buf, size_t count)
149{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 struct snd_soc_pcm_runtime *rtd =
151 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700152 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000153
Mark Brownc593b522010-10-27 20:11:17 -0700154 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
155 if (ret)
156 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000157
158 return count;
159}
160
161static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
162
Mark Brown2624d5f2009-11-03 21:56:13 +0000163#ifdef CONFIG_DEBUG_FS
164static int codec_reg_open_file(struct inode *inode, struct file *file)
165{
166 file->private_data = inode->i_private;
167 return 0;
168}
169
170static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
171 size_t count, loff_t *ppos)
172{
173 ssize_t ret;
174 struct snd_soc_codec *codec = file->private_data;
175 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
176 if (!buf)
177 return -ENOMEM;
178 ret = soc_codec_reg_show(codec, buf);
179 if (ret >= 0)
180 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
181 kfree(buf);
182 return ret;
183}
184
185static ssize_t codec_reg_write_file(struct file *file,
186 const char __user *user_buf, size_t count, loff_t *ppos)
187{
188 char buf[32];
189 int buf_size;
190 char *start = buf;
191 unsigned long reg, value;
192 int step = 1;
193 struct snd_soc_codec *codec = file->private_data;
194
195 buf_size = min(count, (sizeof(buf)-1));
196 if (copy_from_user(buf, user_buf, buf_size))
197 return -EFAULT;
198 buf[buf_size] = 0;
199
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000200 if (codec->driver->reg_cache_step)
201 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000202
203 while (*start == ' ')
204 start++;
205 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000206 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000207 return -EINVAL;
208 while (*start == ' ')
209 start++;
210 if (strict_strtoul(start, 16, &value))
211 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000212
213 /* Userspace has been fiddling around behind the kernel's back */
214 add_taint(TAINT_USER);
215
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000216 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000217 return buf_size;
218}
219
220static const struct file_operations codec_reg_fops = {
221 .open = codec_reg_open_file,
222 .read = codec_reg_read_file,
223 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200224 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000225};
226
227static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
228{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200229 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
230
231 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
232 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000233 if (!codec->debugfs_codec_root) {
234 printk(KERN_WARNING
235 "ASoC: Failed to create codec debugfs directory\n");
236 return;
237 }
238
239 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
240 codec->debugfs_codec_root,
241 codec, &codec_reg_fops);
242 if (!codec->debugfs_reg)
243 printk(KERN_WARNING
244 "ASoC: Failed to create codec register debugfs file\n");
245
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200246 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200248 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 printk(KERN_WARNING
250 "Failed to create DAPM debugfs directory\n");
251
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200252 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253}
254
255static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
256{
257 debugfs_remove_recursive(codec->debugfs_codec_root);
258}
259
Mark Brownc3c5a192010-09-15 18:15:14 +0100260static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
261 size_t count, loff_t *ppos)
262{
263 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100264 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100265 struct snd_soc_codec *codec;
266
267 if (!buf)
268 return -ENOMEM;
269
Mark Brown2b194f9d2010-10-13 10:52:16 +0100270 list_for_each_entry(codec, &codec_list, list) {
271 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
272 codec->name);
273 if (len >= 0)
274 ret += len;
275 if (ret > PAGE_SIZE) {
276 ret = PAGE_SIZE;
277 break;
278 }
279 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100280
281 if (ret >= 0)
282 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
283
284 kfree(buf);
285
286 return ret;
287}
288
289static const struct file_operations codec_list_fops = {
290 .read = codec_list_read_file,
291 .llseek = default_llseek,/* read accesses f_pos */
292};
293
Mark Brownf3208782010-09-15 18:19:07 +0100294static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
295 size_t count, loff_t *ppos)
296{
297 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100298 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100299 struct snd_soc_dai *dai;
300
301 if (!buf)
302 return -ENOMEM;
303
Mark Brown2b194f9d2010-10-13 10:52:16 +0100304 list_for_each_entry(dai, &dai_list, list) {
305 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
306 if (len >= 0)
307 ret += len;
308 if (ret > PAGE_SIZE) {
309 ret = PAGE_SIZE;
310 break;
311 }
312 }
Mark Brownf3208782010-09-15 18:19:07 +0100313
Mark Brown2b194f9d2010-10-13 10:52:16 +0100314 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100315
316 kfree(buf);
317
318 return ret;
319}
320
321static const struct file_operations dai_list_fops = {
322 .read = dai_list_read_file,
323 .llseek = default_llseek,/* read accesses f_pos */
324};
325
Mark Brown19c7ac22010-09-15 18:22:40 +0100326static ssize_t platform_list_read_file(struct file *file,
327 char __user *user_buf,
328 size_t count, loff_t *ppos)
329{
330 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100331 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100332 struct snd_soc_platform *platform;
333
334 if (!buf)
335 return -ENOMEM;
336
Mark Brown2b194f9d2010-10-13 10:52:16 +0100337 list_for_each_entry(platform, &platform_list, list) {
338 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
339 platform->name);
340 if (len >= 0)
341 ret += len;
342 if (ret > PAGE_SIZE) {
343 ret = PAGE_SIZE;
344 break;
345 }
346 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100347
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100349
350 kfree(buf);
351
352 return ret;
353}
354
355static const struct file_operations platform_list_fops = {
356 .read = platform_list_read_file,
357 .llseek = default_llseek,/* read accesses f_pos */
358};
359
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200360static void soc_init_card_debugfs(struct snd_soc_card *card)
361{
362 card->debugfs_card_root = debugfs_create_dir(card->name,
363 debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200364 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200365 dev_warn(card->dev,
366 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200367 return;
368 }
369
370 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
371 card->debugfs_card_root,
372 &card->pop_time);
373 if (!card->debugfs_pop_time)
374 dev_warn(card->dev,
375 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200376}
377
378static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
379{
380 debugfs_remove_recursive(card->debugfs_card_root);
381}
382
Mark Brown2624d5f2009-11-03 21:56:13 +0000383#else
384
385static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
386{
387}
388
389static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
390{
391}
Axel Linb95fccb2010-11-09 17:06:44 +0800392
393static inline void soc_init_card_debugfs(struct snd_soc_card *card)
394{
395}
396
397static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
398{
399}
Mark Brown2624d5f2009-11-03 21:56:13 +0000400#endif
401
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402#ifdef CONFIG_SND_SOC_AC97_BUS
403/* unregister ac97 codec */
404static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
405{
406 if (codec->ac97->dev.bus)
407 device_unregister(&codec->ac97->dev);
408 return 0;
409}
410
411/* stop no dev release warning */
412static void soc_ac97_device_release(struct device *dev){}
413
414/* register ac97 codec to bus */
415static int soc_ac97_dev_register(struct snd_soc_codec *codec)
416{
417 int err;
418
419 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100420 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200421 codec->ac97->dev.release = soc_ac97_device_release;
422
Kay Sieversbb072bf2008-11-02 03:50:35 +0100423 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 err = device_register(&codec->ac97->dev);
426 if (err < 0) {
427 snd_printk(KERN_ERR "Can't register ac97 bus\n");
428 codec->ac97->dev.bus = NULL;
429 return err;
430 }
431 return 0;
432}
433#endif
434
Mark Brown06f409d2009-04-07 18:10:13 +0100435static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
436{
437 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100440 int ret;
441
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
443 rtd->dai_link->symmetric_rates) {
444 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
445 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100446
447 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
448 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 rtd->rate,
450 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100451 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100453 "Unable to apply rate symmetry constraint: %d\n", ret);
454 return ret;
455 }
456 }
457
458 return 0;
459}
460
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461/*
462 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
463 * then initialized and any private data can be allocated. This also calls
464 * startup for the cpu DAI, platform, machine and codec DAI.
465 */
466static int soc_pcm_open(struct snd_pcm_substream *substream)
467{
468 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470 struct snd_soc_platform *platform = rtd->platform;
471 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
472 struct snd_soc_dai *codec_dai = rtd->codec_dai;
473 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
474 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 int ret = 0;
476
477 mutex_lock(&pcm_mutex);
478
479 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 if (cpu_dai->driver->ops->startup) {
481 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200482 if (ret < 0) {
483 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100484 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485 goto out;
486 }
487 }
488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 if (platform->driver->ops->open) {
490 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 if (ret < 0) {
492 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
493 goto platform_err;
494 }
495 }
496
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 if (codec_dai->driver->ops->startup) {
498 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100499 if (ret < 0) {
500 printk(KERN_ERR "asoc: can't open codec %s\n",
501 codec_dai->name);
502 goto codec_dai_err;
503 }
504 }
505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
507 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 goto machine_err;
511 }
512 }
513
Mark Browna00f90f2010-12-02 16:24:24 +0000514 /* Check that the codec and cpu DAIs are compatible */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
516 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 max(codec_dai_drv->playback.rate_min,
518 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 min(codec_dai_drv->playback.rate_max,
521 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 max(codec_dai_drv->playback.channels_min,
524 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200525 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 min(codec_dai_drv->playback.channels_max,
527 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100528 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100530 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
532 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900533 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 runtime->hw.rates |= cpu_dai_drv->playback.rates;
535 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900536 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538 } else {
539 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 max(codec_dai_drv->capture.rate_min,
541 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 min(codec_dai_drv->capture.rate_max,
544 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 max(codec_dai_drv->capture.channels_min,
547 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 min(codec_dai_drv->capture.channels_max,
550 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100553 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
555 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900556 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 runtime->hw.rates |= cpu_dai_drv->capture.rates;
558 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900559 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561 }
562
563 snd_pcm_limit_hw_rates(runtime);
564 if (!runtime->hw.rates) {
565 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100566 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900567 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568 }
569 if (!runtime->hw.formats) {
570 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100571 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900572 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573 }
574 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
575 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900577 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578 }
579
Mark Brown06f409d2009-04-07 18:10:13 +0100580 /* Symmetry only applies if we've already got an active stream. */
581 if (cpu_dai->active || codec_dai->active) {
582 ret = soc_pcm_apply_symmetry(substream);
583 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900584 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100585 }
586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 pr_debug("asoc: %s <-> %s info:\n",
588 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100589 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
590 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
591 runtime->hw.channels_max);
592 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
593 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594
Jassi Brar14dc5732010-02-26 09:12:32 +0900595 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 cpu_dai->playback_active++;
597 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900598 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 cpu_dai->capture_active++;
600 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900601 }
602 cpu_dai->active++;
603 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605 mutex_unlock(&pcm_mutex);
606 return 0;
607
Jassi Brarbb1c0472010-02-25 11:24:53 +0900608config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
610 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Jassi Brarbb1c0472010-02-25 11:24:53 +0900612machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 if (codec_dai->driver->ops->shutdown)
614 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900615
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100616codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 if (platform->driver->ops->close)
618 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
620platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 if (cpu_dai->driver->ops->shutdown)
622 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623out:
624 mutex_unlock(&pcm_mutex);
625 return ret;
626}
627
628/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200629 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 * This is to ensure there are no pops or clicks in between any music tracks
631 * due to DAPM power cycling.
632 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100633static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 struct snd_soc_pcm_runtime *rtd =
636 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
637 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638
639 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
642 codec_dai->driver->playback.stream_name,
643 codec_dai->playback_active ? "active" : "inactive",
644 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 /* are we waiting on this codec DAI stream */
647 if (codec_dai->pop_wait == 1) {
648 codec_dai->pop_wait = 0;
649 snd_soc_dapm_stream_event(rtd,
650 codec_dai->driver->playback.stream_name,
651 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 mutex_unlock(&pcm_mutex);
655}
656
657/*
658 * Called by ALSA when a PCM substream is closed. Private data can be
659 * freed here. The cpu DAI, codec DAI, machine and platform are also
660 * shutdown.
661 */
662static int soc_codec_close(struct snd_pcm_substream *substream)
663{
664 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 struct snd_soc_platform *platform = rtd->platform;
666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 struct snd_soc_dai *codec_dai = rtd->codec_dai;
668 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
670 mutex_lock(&pcm_mutex);
671
Jassi Brar14dc5732010-02-26 09:12:32 +0900672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 cpu_dai->playback_active--;
674 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900675 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 cpu_dai->capture_active--;
677 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900679
680 cpu_dai->active--;
681 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 codec->active--;
683
Mark Brown6010b2d2008-09-06 18:33:24 +0100684 /* Muting the DAC suppresses artifacts caused during digital
685 * shutdown, for example from stopping clocks.
686 */
687 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
688 snd_soc_dai_digital_mute(codec_dai, 1);
689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 if (cpu_dai->driver->ops->shutdown)
691 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 if (codec_dai->driver->ops->shutdown)
694 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
697 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 if (platform->driver->ops->close)
700 platform->driver->ops->close(substream);
701 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702
703 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
704 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100705 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 schedule_delayed_work(&rtd->delayed_work,
707 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708 } else {
709 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 snd_soc_dapm_stream_event(rtd,
711 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100712 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713 }
714
715 mutex_unlock(&pcm_mutex);
716 return 0;
717}
718
719/*
720 * Called by ALSA when the PCM substream is prepared, can set format, sample
721 * rate, etc. This function is non atomic and can be called multiple times,
722 * it can refer to the runtime info.
723 */
724static int soc_pcm_prepare(struct snd_pcm_substream *substream)
725{
726 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 struct snd_soc_platform *platform = rtd->platform;
728 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
729 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 int ret = 0;
731
732 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
735 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100736 if (ret < 0) {
737 printk(KERN_ERR "asoc: machine prepare error\n");
738 goto out;
739 }
740 }
741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 if (platform->driver->ops->prepare) {
743 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200744 if (ret < 0) {
745 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200747 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 }
749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (codec_dai->driver->ops->prepare) {
751 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200752 if (ret < 0) {
753 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200755 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 if (cpu_dai->driver->ops->prepare) {
759 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100760 if (ret < 0) {
761 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
762 goto out;
763 }
764 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765
Mark Brownd45f6212008-10-14 13:58:36 +0100766 /* cancel any delayed stream shutdown that is pending */
767 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
768 codec_dai->pop_wait) {
769 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100771 }
772
Mark Brown452c5ea2009-05-17 21:41:23 +0100773 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 snd_soc_dapm_stream_event(rtd,
775 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100776 SND_SOC_DAPM_STREAM_START);
777 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 snd_soc_dapm_stream_event(rtd,
779 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100780 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100781
Mark Brown452c5ea2009-05-17 21:41:23 +0100782 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783
784out:
785 mutex_unlock(&pcm_mutex);
786 return ret;
787}
788
789/*
790 * Called by ALSA when the hardware params are set by application. This
791 * function can also be called multiple times and can allocate buffers
792 * (using snd_pcm_lib_* ). It's non-atomic.
793 */
794static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
795 struct snd_pcm_hw_params *params)
796{
797 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 struct snd_soc_platform *platform = rtd->platform;
799 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
800 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801 int ret = 0;
802
803 mutex_lock(&pcm_mutex);
804
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000805 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
806 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100808 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 goto out;
810 }
811 }
812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813 if (codec_dai->driver->ops->hw_params) {
814 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100815 if (ret < 0) {
816 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
817 codec_dai->name);
818 goto codec_err;
819 }
820 }
821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 if (cpu_dai->driver->ops->hw_params) {
823 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200825 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100826 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827 goto interface_err;
828 }
829 }
830
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 if (platform->driver->ops->hw_params) {
832 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200833 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200834 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835 platform->name);
836 goto platform_err;
837 }
838 }
839
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000840 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100841
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842out:
843 mutex_unlock(&pcm_mutex);
844 return ret;
845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (cpu_dai->driver->ops->hw_free)
848 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
850interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (codec_dai->driver->ops->hw_free)
852 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100853
854codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
856 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857
858 mutex_unlock(&pcm_mutex);
859 return ret;
860}
861
862/*
Mark Browna00f90f2010-12-02 16:24:24 +0000863 * Frees resources allocated by hw_params, can be called multiple times
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864 */
865static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
866{
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 struct snd_soc_platform *platform = rtd->platform;
869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 struct snd_soc_dai *codec_dai = rtd->codec_dai;
871 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200872
873 mutex_lock(&pcm_mutex);
874
875 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100876 if (!codec->active)
877 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878
879 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
881 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
883 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 if (platform->driver->ops->hw_free)
885 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886
Mark Browna00f90f2010-12-02 16:24:24 +0000887 /* now free hw params for the DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 if (codec_dai->driver->ops->hw_free)
889 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 if (cpu_dai->driver->ops->hw_free)
892 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200893
894 mutex_unlock(&pcm_mutex);
895 return 0;
896}
897
898static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
899{
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
903 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 int ret;
905
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 if (codec_dai->driver->ops->trigger) {
907 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 if (ret < 0)
909 return ret;
910 }
911
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 if (platform->driver->ops->trigger) {
913 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914 if (ret < 0)
915 return ret;
916 }
917
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 if (cpu_dai->driver->ops->trigger) {
919 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200920 if (ret < 0)
921 return ret;
922 }
923 return 0;
924}
925
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200926/*
927 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200928 * If cpu_dai, codec_dai, platform driver has the delay callback, than
929 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200930 */
931static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
932{
933 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 struct snd_soc_platform *platform = rtd->platform;
935 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
936 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200937 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200938 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200939 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 if (platform->driver->ops->pointer)
942 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200943
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 if (cpu_dai->driver->ops->delay)
945 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 if (codec_dai->driver->ops->delay)
948 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200949
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000950 if (platform->driver->delay)
951 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200952
953 runtime->delay = delay;
954
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200955 return offset;
956}
957
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958/* ASoC PCM operations */
959static struct snd_pcm_ops soc_pcm_ops = {
960 .open = soc_pcm_open,
961 .close = soc_codec_close,
962 .hw_params = soc_pcm_hw_params,
963 .hw_free = soc_pcm_hw_free,
964 .prepare = soc_pcm_prepare,
965 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200966 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200967};
968
969#ifdef CONFIG_PM
970/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100971static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972{
Mark Brown416356f2009-06-30 19:05:15 +0100973 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 struct snd_soc_card *card = platform_get_drvdata(pdev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200975 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976 int i;
977
Daniel Macke3509ff2009-06-03 17:44:49 +0200978 /* If the initialization of this soc device failed, there is no codec
979 * associated with it. Just bail out in this case.
980 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200982 return 0;
983
Andy Green6ed25972008-06-13 16:24:05 +0100984 /* Due to the resume being scheduled into a workqueue we could
985 * suspend before that's finished - wait for it to complete.
986 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 snd_power_lock(card->snd_card);
988 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
989 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100990
991 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100993
Mark Browna00f90f2010-12-02 16:24:24 +0000994 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 for (i = 0; i < card->num_rtd; i++) {
996 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
997 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100998
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000999 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001000 continue;
1001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 if (drv->ops->digital_mute && dai->playback_active)
1003 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001004 }
1005
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001006 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001007 for (i = 0; i < card->num_rtd; i++) {
1008 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001009 continue;
1010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001012 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001013
Mark Brown87506542008-11-18 20:50:34 +00001014 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001015 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001016
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017 for (i = 0; i < card->num_rtd; i++) {
1018 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1019 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001020
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001021 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001022 continue;
1023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001024 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1025 cpu_dai->driver->suspend(cpu_dai);
1026 if (platform->driver->suspend && !platform->suspended) {
1027 platform->driver->suspend(cpu_dai);
1028 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001029 }
1030 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032 /* close any waiting streams and save state */
1033 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +01001034 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001035 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001036 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001037
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1040
1041 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001042 continue;
1043
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001044 if (driver->playback.stream_name != NULL)
1045 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1046 SND_SOC_DAPM_STREAM_SUSPEND);
1047
1048 if (driver->capture.stream_name != NULL)
1049 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1050 SND_SOC_DAPM_STREAM_SUSPEND);
1051 }
1052
1053 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001054 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 /* If there are paths active then the CODEC will be held with
1056 * bias _ON and should not be suspended. */
1057 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001058 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059 case SND_SOC_BIAS_STANDBY:
1060 case SND_SOC_BIAS_OFF:
1061 codec->driver->suspend(codec, PMSG_SUSPEND);
1062 codec->suspended = 1;
1063 break;
1064 default:
1065 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1066 break;
1067 }
1068 }
1069 }
1070
1071 for (i = 0; i < card->num_rtd; i++) {
1072 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1073
1074 if (card->rtd[i].dai_link->ignore_suspend)
1075 continue;
1076
1077 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1078 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001079 }
1080
Mark Brown87506542008-11-18 20:50:34 +00001081 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001082 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001083
1084 return 0;
1085}
1086
Andy Green6ed25972008-06-13 16:24:05 +01001087/* deferred resume work, so resume can complete before we finished
1088 * setting our codec back up, which can be very slow on I2C
1089 */
1090static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001091{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 struct snd_soc_card *card =
1093 container_of(work, struct snd_soc_card, deferred_resume_work);
1094 struct platform_device *pdev = to_platform_device(card->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001095 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001096 int i;
1097
Andy Green6ed25972008-06-13 16:24:05 +01001098 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1099 * so userspace apps are blocked from touching us
1100 */
1101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001103
Mark Brown99497882010-05-07 20:24:05 +01001104 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001106
Mark Brown87506542008-11-18 20:50:34 +00001107 if (card->resume_pre)
1108 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001109
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001110 /* resume AC97 DAIs */
1111 for (i = 0; i < card->num_rtd; i++) {
1112 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001113
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001114 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001115 continue;
1116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001117 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1118 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001119 }
1120
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001121 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122 /* If the CODEC was idle over suspend then it will have been
1123 * left with bias OFF or STANDBY and suspended so we must now
1124 * resume. Otherwise the suspend was suppressed.
1125 */
1126 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001127 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 case SND_SOC_BIAS_STANDBY:
1129 case SND_SOC_BIAS_OFF:
1130 codec->driver->resume(codec);
1131 codec->suspended = 0;
1132 break;
1133 default:
1134 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1135 break;
1136 }
Mark Brown1547aba2010-05-07 21:11:40 +01001137 }
1138 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001140 for (i = 0; i < card->num_rtd; i++) {
1141 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001143 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001144 continue;
1145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 if (driver->playback.stream_name != NULL)
1147 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001148 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001149
1150 if (driver->capture.stream_name != NULL)
1151 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001152 SND_SOC_DAPM_STREAM_RESUME);
1153 }
1154
Mark Brown3ff3f642008-05-19 12:32:25 +02001155 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 for (i = 0; i < card->num_rtd; i++) {
1157 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1158 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001161 continue;
1162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001163 if (drv->ops->digital_mute && dai->playback_active)
1164 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001165 }
1166
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 for (i = 0; i < card->num_rtd; i++) {
1168 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1169 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001172 continue;
1173
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001174 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1175 cpu_dai->driver->resume(cpu_dai);
1176 if (platform->driver->resume && platform->suspended) {
1177 platform->driver->resume(cpu_dai);
1178 platform->suspended = 0;
1179 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001180 }
1181
Mark Brown87506542008-11-18 20:50:34 +00001182 if (card->resume_post)
1183 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001184
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001186
1187 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001189}
1190
1191/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001192static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001193{
Mark Brown416356f2009-06-30 19:05:15 +01001194 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001195 struct snd_soc_card *card = platform_get_drvdata(pdev);
1196 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001197
Mark Brown64ab9ba2009-03-31 11:27:03 +01001198 /* AC97 devices might have other drivers hanging off them so
1199 * need to resume immediately. Other drivers don't have that
1200 * problem and may take a substantial amount of time to resume
1201 * due to I/O costs and anti-pop so handle them out of line.
1202 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001203 for (i = 0; i < card->num_rtd; i++) {
1204 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1205 if (cpu_dai->driver->ac97_control) {
1206 dev_dbg(dev, "Resuming AC97 immediately\n");
1207 soc_resume_deferred(&card->deferred_resume_work);
1208 } else {
1209 dev_dbg(dev, "Scheduling resume work\n");
1210 if (!schedule_work(&card->deferred_resume_work))
1211 dev_err(dev, "resume work item may be lost\n");
1212 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001213 }
Andy Green6ed25972008-06-13 16:24:05 +01001214
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001215 return 0;
1216}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217#else
1218#define soc_suspend NULL
1219#define soc_resume NULL
1220#endif
1221
Barry Song02a06d32009-10-16 18:13:38 +08001222static struct snd_soc_dai_ops null_dai_ops = {
1223};
1224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001226{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001227 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1228 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001229 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001230 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001231 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 if (rtd->complete)
1234 return 1;
1235 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 /* do we already have the CPU DAI for this link ? */
1238 if (rtd->cpu_dai) {
1239 goto find_codec;
1240 }
1241 /* no, then find CPU DAI from registered DAIs*/
1242 list_for_each_entry(cpu_dai, &dai_list, list) {
1243 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1244
1245 if (!try_module_get(cpu_dai->dev->driver->owner))
1246 return -ENODEV;
1247
1248 rtd->cpu_dai = cpu_dai;
1249 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001250 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 }
1252 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1253 dai_link->cpu_dai_name);
1254
1255find_codec:
1256 /* do we already have the CODEC for this link ? */
1257 if (rtd->codec) {
1258 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001259 }
1260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 /* no, then find CODEC from registered CODECs*/
1262 list_for_each_entry(codec, &codec_list, list) {
1263 if (!strcmp(codec->name, dai_link->codec_name)) {
1264 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1267 list_for_each_entry(codec_dai, &dai_list, list) {
1268 if (codec->dev == codec_dai->dev &&
1269 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1270 rtd->codec_dai = codec_dai;
1271 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001272 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001273 }
1274 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1275 dai_link->codec_dai_name);
1276
1277 goto find_platform;
1278 }
1279 }
1280 dev_dbg(card->dev, "CODEC %s not registered\n",
1281 dai_link->codec_name);
1282
1283find_platform:
1284 /* do we already have the CODEC DAI for this link ? */
1285 if (rtd->platform) {
1286 goto out;
1287 }
1288 /* no, then find CPU DAI from registered DAIs*/
1289 list_for_each_entry(platform, &platform_list, list) {
1290 if (!strcmp(platform->name, dai_link->platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001291 rtd->platform = platform;
1292 goto out;
1293 }
1294 }
1295
1296 dev_dbg(card->dev, "platform %s not registered\n",
1297 dai_link->platform_name);
1298 return 0;
1299
1300out:
1301 /* mark rtd as complete if we found all 4 of our client devices */
1302 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1303 rtd->complete = 1;
1304 card->num_rtd++;
1305 }
1306 return 1;
1307}
1308
Jarkko Nikula589c3562010-12-06 16:27:07 +02001309static void soc_remove_codec(struct snd_soc_codec *codec)
1310{
1311 int err;
1312
1313 if (codec->driver->remove) {
1314 err = codec->driver->remove(codec);
1315 if (err < 0)
1316 dev_err(codec->dev,
1317 "asoc: failed to remove %s: %d\n",
1318 codec->name, err);
1319 }
1320
1321 /* Make sure all DAPM widgets are freed */
1322 snd_soc_dapm_free(&codec->dapm);
1323
1324 soc_cleanup_codec_debugfs(codec);
1325 codec->probed = 0;
1326 list_del(&codec->card_list);
1327 module_put(codec->dev->driver->owner);
1328}
1329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1331{
1332 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1333 struct snd_soc_codec *codec = rtd->codec;
1334 struct snd_soc_platform *platform = rtd->platform;
1335 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1336 int err;
1337
1338 /* unregister the rtd device */
1339 if (rtd->dev_registered) {
1340 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001341 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 device_unregister(&rtd->dev);
1343 rtd->dev_registered = 0;
1344 }
1345
1346 /* remove the CODEC DAI */
1347 if (codec_dai && codec_dai->probed) {
1348 if (codec_dai->driver->remove) {
1349 err = codec_dai->driver->remove(codec_dai);
1350 if (err < 0)
1351 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1352 }
1353 codec_dai->probed = 0;
1354 list_del(&codec_dai->card_list);
1355 }
1356
1357 /* remove the platform */
1358 if (platform && platform->probed) {
1359 if (platform->driver->remove) {
1360 err = platform->driver->remove(platform);
1361 if (err < 0)
1362 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1363 }
1364 platform->probed = 0;
1365 list_del(&platform->card_list);
1366 module_put(platform->dev->driver->owner);
1367 }
1368
1369 /* remove the CODEC */
Jarkko Nikula589c3562010-12-06 16:27:07 +02001370 if (codec && codec->probed)
1371 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372
1373 /* remove the cpu_dai */
1374 if (cpu_dai && cpu_dai->probed) {
1375 if (cpu_dai->driver->remove) {
1376 err = cpu_dai->driver->remove(cpu_dai);
1377 if (err < 0)
1378 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1379 }
1380 cpu_dai->probed = 0;
1381 list_del(&cpu_dai->card_list);
1382 module_put(cpu_dai->dev->driver->owner);
1383 }
1384}
1385
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001386static void soc_set_name_prefix(struct snd_soc_card *card,
1387 struct snd_soc_codec *codec)
1388{
1389 int i;
1390
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001391 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001392 return;
1393
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001394 for (i = 0; i < card->num_configs; i++) {
1395 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001396 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1397 codec->name_prefix = map->name_prefix;
1398 break;
1399 }
1400 }
1401}
1402
Jarkko Nikula589c3562010-12-06 16:27:07 +02001403static int soc_probe_codec(struct snd_soc_card *card,
1404 struct snd_soc_codec *codec)
1405{
1406 int ret = 0;
1407
1408 codec->card = card;
1409 codec->dapm.card = card;
1410 soc_set_name_prefix(card, codec);
1411
1412 if (codec->driver->probe) {
1413 ret = codec->driver->probe(codec);
1414 if (ret < 0) {
1415 dev_err(codec->dev,
1416 "asoc: failed to probe CODEC %s: %d\n",
1417 codec->name, ret);
1418 return ret;
1419 }
1420 }
1421
1422 soc_init_codec_debugfs(codec);
1423
1424 /* mark codec as probed and add to card codec list */
Harsha Priyaf6c2ed52011-01-05 11:34:51 +05301425 if (!try_module_get(codec->dev->driver->owner))
1426 return -ENODEV;
1427
Jarkko Nikula589c3562010-12-06 16:27:07 +02001428 codec->probed = 1;
1429 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001430 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001431
1432 return ret;
1433}
1434
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435static void rtd_release(struct device *dev) {}
1436
Jarkko Nikula589c3562010-12-06 16:27:07 +02001437static int soc_post_component_init(struct snd_soc_card *card,
1438 struct snd_soc_codec *codec,
1439 int num, int dailess)
1440{
1441 struct snd_soc_dai_link *dai_link = NULL;
1442 struct snd_soc_aux_dev *aux_dev = NULL;
1443 struct snd_soc_pcm_runtime *rtd;
1444 const char *temp, *name;
1445 int ret = 0;
1446
1447 if (!dailess) {
1448 dai_link = &card->dai_link[num];
1449 rtd = &card->rtd[num];
1450 name = dai_link->name;
1451 } else {
1452 aux_dev = &card->aux_dev[num];
1453 rtd = &card->rtd_aux[num];
1454 name = aux_dev->name;
1455 }
1456
1457 /* machine controls, routes and widgets are not prefixed */
1458 temp = codec->name_prefix;
1459 codec->name_prefix = NULL;
1460
1461 /* do machine specific initialization */
1462 if (!dailess && dai_link->init)
1463 ret = dai_link->init(rtd);
1464 else if (dailess && aux_dev->init)
1465 ret = aux_dev->init(&codec->dapm);
1466 if (ret < 0) {
1467 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1468 return ret;
1469 }
1470 codec->name_prefix = temp;
1471
1472 /* Make sure all DAPM widgets are instantiated */
1473 snd_soc_dapm_new_widgets(&codec->dapm);
1474 snd_soc_dapm_sync(&codec->dapm);
1475
1476 /* register the rtd device */
1477 rtd->codec = codec;
1478 rtd->card = card;
1479 rtd->dev.parent = card->dev;
1480 rtd->dev.release = rtd_release;
1481 rtd->dev.init_name = name;
1482 ret = device_register(&rtd->dev);
1483 if (ret < 0) {
1484 dev_err(card->dev,
1485 "asoc: failed to register runtime device: %d\n", ret);
1486 return ret;
1487 }
1488 rtd->dev_registered = 1;
1489
1490 /* add DAPM sysfs entries for this codec */
1491 ret = snd_soc_dapm_sys_add(&rtd->dev);
1492 if (ret < 0)
1493 dev_err(codec->dev,
1494 "asoc: failed to add codec dapm sysfs entries: %d\n",
1495 ret);
1496
1497 /* add codec sysfs entries */
1498 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1499 if (ret < 0)
1500 dev_err(codec->dev,
1501 "asoc: failed to add codec sysfs files: %d\n", ret);
1502
1503 return 0;
1504}
1505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001506static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1507{
1508 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1509 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1510 struct snd_soc_codec *codec = rtd->codec;
1511 struct snd_soc_platform *platform = rtd->platform;
1512 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1513 int ret;
1514
1515 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1516
1517 /* config components */
1518 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 codec_dai->card = card;
1521 cpu_dai->card = card;
1522
1523 /* set default power off timeout */
1524 rtd->pmdown_time = pmdown_time;
1525
1526 /* probe the cpu_dai */
1527 if (!cpu_dai->probed) {
1528 if (cpu_dai->driver->probe) {
1529 ret = cpu_dai->driver->probe(cpu_dai);
1530 if (ret < 0) {
1531 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1532 cpu_dai->name);
1533 return ret;
1534 }
1535 }
1536 cpu_dai->probed = 1;
1537 /* mark cpu_dai as probed and add to card cpu_dai list */
1538 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1539 }
1540
1541 /* probe the CODEC */
1542 if (!codec->probed) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001543 ret = soc_probe_codec(card, codec);
1544 if (ret < 0)
1545 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001546 }
1547
1548 /* probe the platform */
1549 if (!platform->probed) {
1550 if (platform->driver->probe) {
1551 ret = platform->driver->probe(platform);
1552 if (ret < 0) {
1553 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1554 platform->name);
1555 return ret;
1556 }
1557 }
1558 /* mark platform as probed and add to card platform list */
Harsha Priyaf6c2ed52011-01-05 11:34:51 +05301559
1560 if (!try_module_get(platform->dev->driver->owner))
1561 return -ENODEV;
1562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 platform->probed = 1;
1564 list_add(&platform->card_list, &card->platform_dev_list);
1565 }
1566
1567 /* probe the CODEC DAI */
1568 if (!codec_dai->probed) {
1569 if (codec_dai->driver->probe) {
1570 ret = codec_dai->driver->probe(codec_dai);
1571 if (ret < 0) {
1572 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1573 codec_dai->name);
1574 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001575 }
1576 }
1577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 /* mark cpu_dai as probed and add to card cpu_dai list */
1579 codec_dai->probed = 1;
1580 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001581 }
1582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583 /* DAPM dai link stream work */
1584 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1585
Jarkko Nikula589c3562010-12-06 16:27:07 +02001586 ret = soc_post_component_init(card, codec, num, 0);
1587 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001589
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001590 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1591 if (ret < 0)
1592 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594 /* create the pcm */
1595 ret = soc_new_pcm(rtd, num);
1596 if (ret < 0) {
1597 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1598 return ret;
1599 }
1600
1601 /* add platform data for AC97 devices */
1602 if (rtd->codec_dai->driver->ac97_control)
1603 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1604
1605 return 0;
1606}
1607
1608#ifdef CONFIG_SND_SOC_AC97_BUS
1609static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1610{
1611 int ret;
1612
1613 /* Only instantiate AC97 if not already done by the adaptor
1614 * for the generic AC97 subsystem.
1615 */
1616 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001617 /*
1618 * It is possible that the AC97 device is already registered to
1619 * the device subsystem. This happens when the device is created
1620 * via snd_ac97_mixer(). Currently only SoC codec that does so
1621 * is the generic AC97 glue but others migh emerge.
1622 *
1623 * In those cases we don't try to register the device again.
1624 */
1625 if (!rtd->codec->ac97_created)
1626 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627
1628 ret = soc_ac97_dev_register(rtd->codec);
1629 if (ret < 0) {
1630 printk(KERN_ERR "asoc: AC97 device register failed\n");
1631 return ret;
1632 }
1633
1634 rtd->codec->ac97_registered = 1;
1635 }
1636 return 0;
1637}
1638
1639static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1640{
1641 if (codec->ac97_registered) {
1642 soc_ac97_dev_unregister(codec);
1643 codec->ac97_registered = 0;
1644 }
1645}
1646#endif
1647
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001648static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1649{
1650 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001651 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001652 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001653
1654 /* find CODEC from registered CODECs*/
1655 list_for_each_entry(codec, &codec_list, list) {
1656 if (!strcmp(codec->name, aux_dev->codec_name)) {
1657 if (codec->probed) {
1658 dev_err(codec->dev,
1659 "asoc: codec already probed");
1660 ret = -EBUSY;
1661 goto out;
1662 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001663 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001664 }
1665 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001666 /* codec not found */
1667 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1668 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001669
Jarkko Nikula676ad982010-12-03 09:18:22 +02001670found:
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001671 if (!try_module_get(codec->dev->driver->owner))
1672 return -ENODEV;
1673
Jarkko Nikula589c3562010-12-06 16:27:07 +02001674 ret = soc_probe_codec(card, codec);
1675 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001677
Jarkko Nikula589c3562010-12-06 16:27:07 +02001678 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001679
1680out:
1681 return ret;
1682}
1683
1684static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1685{
1686 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1687 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001688
1689 /* unregister the rtd device */
1690 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001691 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001692 device_unregister(&rtd->dev);
1693 rtd->dev_registered = 0;
1694 }
1695
Jarkko Nikula589c3562010-12-06 16:27:07 +02001696 if (codec && codec->probed)
1697 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001698}
1699
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001700static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1701 enum snd_soc_compress_type compress_type)
1702{
1703 int ret;
1704
1705 if (codec->cache_init)
1706 return 0;
1707
1708 /* override the compress_type if necessary */
1709 if (compress_type && codec->compress_type != compress_type)
1710 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001711 ret = snd_soc_cache_init(codec);
1712 if (ret < 0) {
1713 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1714 ret);
1715 return ret;
1716 }
1717 codec->cache_init = 1;
1718 return 0;
1719}
1720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721static void snd_soc_instantiate_card(struct snd_soc_card *card)
1722{
1723 struct platform_device *pdev = to_platform_device(card->dev);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001724 struct snd_soc_codec *codec;
1725 struct snd_soc_codec_conf *codec_conf;
1726 enum snd_soc_compress_type compress_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001727 int ret, i;
1728
1729 mutex_lock(&card->mutex);
1730
1731 if (card->instantiated) {
1732 mutex_unlock(&card->mutex);
1733 return;
1734 }
1735
1736 /* bind DAIs */
1737 for (i = 0; i < card->num_links; i++)
1738 soc_bind_dai_link(card, i);
1739
1740 /* bind completed ? */
1741 if (card->num_rtd != card->num_links) {
1742 mutex_unlock(&card->mutex);
1743 return;
1744 }
1745
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001746 /* initialize the register cache for each available codec */
1747 list_for_each_entry(codec, &codec_list, list) {
1748 if (codec->cache_init)
1749 continue;
1750 /* check to see if we need to override the compress_type */
1751 for (i = 0; i < card->num_configs; ++i) {
1752 codec_conf = &card->codec_conf[i];
1753 if (!strcmp(codec->name, codec_conf->dev_name)) {
1754 compress_type = codec_conf->compress_type;
1755 if (compress_type && compress_type
1756 != codec->compress_type)
1757 break;
1758 }
1759 }
1760 if (i == card->num_configs) {
1761 /* no need to override the compress_type so
1762 * go ahead and do the standard thing */
1763 ret = snd_soc_init_codec_cache(codec, 0);
1764 if (ret < 0) {
1765 mutex_unlock(&card->mutex);
1766 return;
1767 }
1768 continue;
1769 }
1770 /* override the compress_type with the one supplied in
1771 * the machine driver */
1772 ret = snd_soc_init_codec_cache(codec, compress_type);
1773 if (ret < 0) {
1774 mutex_unlock(&card->mutex);
1775 return;
1776 }
1777 }
1778
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 /* card bind complete so register a sound card */
1780 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1781 card->owner, 0, &card->snd_card);
1782 if (ret < 0) {
1783 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1784 card->name);
1785 mutex_unlock(&card->mutex);
1786 return;
1787 }
1788 card->snd_card->dev = card->dev;
1789
Randy Dunlap1301a962008-06-17 19:19:34 +01001790#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001791 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001792 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001793#endif
Andy Green6ed25972008-06-13 16:24:05 +01001794
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 /* initialise the sound card only once */
1796 if (card->probe) {
1797 ret = card->probe(pdev);
1798 if (ret < 0)
1799 goto card_probe_error;
1800 }
1801
Mark Brownfe3e78e2009-11-03 22:13:13 +00001802 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001803 ret = soc_probe_dai_link(card, i);
1804 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001805 pr_err("asoc: failed to instantiate card %s: %d\n",
1806 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 goto probe_dai_err;
1808 }
1809 }
1810
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001811 for (i = 0; i < card->num_aux_devs; i++) {
1812 ret = soc_probe_aux_dev(card, i);
1813 if (ret < 0) {
1814 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1815 card->name, ret);
1816 goto probe_aux_dev_err;
1817 }
1818 }
1819
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001820 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1821 "%s", card->name);
1822 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1823 "%s", card->name);
1824
1825 ret = snd_card_register(card->snd_card);
1826 if (ret < 0) {
1827 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001828 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829 }
1830
1831#ifdef CONFIG_SND_SOC_AC97_BUS
1832 /* register any AC97 codecs */
1833 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001834 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1835 if (ret < 0) {
1836 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1837 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001838 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001839 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001840 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001841 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001842#endif
1843
Mark Brown435c5e22008-12-04 15:32:53 +00001844 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001845 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001846 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001847
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001848probe_aux_dev_err:
1849 for (i = 0; i < card->num_aux_devs; i++)
1850 soc_remove_aux_dev(card, i);
1851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852probe_dai_err:
1853 for (i = 0; i < card->num_links; i++)
1854 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001855
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001857 if (card->remove)
1858 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859
1860 snd_card_free(card->snd_card);
1861
1862 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001863}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864
Mark Brown435c5e22008-12-04 15:32:53 +00001865/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001866 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001867 * client_mutex.
1868 */
1869static void snd_soc_instantiate_cards(void)
1870{
1871 struct snd_soc_card *card;
1872 list_for_each_entry(card, &card_list, list)
1873 snd_soc_instantiate_card(card);
1874}
1875
1876/* probes a new socdev */
1877static int soc_probe(struct platform_device *pdev)
1878{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001879 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001880 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001881
1882 /* Bodge while we unpick instantiation */
1883 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001884 INIT_LIST_HEAD(&card->dai_dev_list);
1885 INIT_LIST_HEAD(&card->codec_dev_list);
1886 INIT_LIST_HEAD(&card->platform_dev_list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001887 INIT_LIST_HEAD(&card->widgets);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001888 INIT_LIST_HEAD(&card->paths);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001889 INIT_LIST_HEAD(&card->dapm_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001890
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001891 soc_init_card_debugfs(card);
1892
Mark Brown435c5e22008-12-04 15:32:53 +00001893 ret = snd_soc_register_card(card);
1894 if (ret != 0) {
1895 dev_err(&pdev->dev, "Failed to register card\n");
1896 return ret;
1897 }
1898
1899 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900}
1901
1902/* removes a socdev */
1903static int soc_remove(struct platform_device *pdev)
1904{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001905 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001906 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001908 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910 /* make sure any delayed work runs */
1911 for (i = 0; i < card->num_rtd; i++) {
1912 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001913 flush_delayed_work_sync(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001914 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001915
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001916 /* remove auxiliary devices */
1917 for (i = 0; i < card->num_aux_devs; i++)
1918 soc_remove_aux_dev(card, i);
1919
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001920 /* remove and free each DAI */
1921 for (i = 0; i < card->num_rtd; i++)
1922 soc_remove_dai_link(card, i);
1923
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001924 soc_cleanup_card_debugfs(card);
1925
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001926 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001927 if (card->remove)
1928 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929
1930 kfree(card->rtd);
1931 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001933 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934 return 0;
1935}
1936
Mark Brown416356f2009-06-30 19:05:15 +01001937static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001938{
Mark Brown416356f2009-06-30 19:05:15 +01001939 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001940 struct snd_soc_card *card = platform_get_drvdata(pdev);
1941 int i;
Mark Brown51737472009-06-22 13:16:51 +01001942
1943 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001944 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001945
1946 /* Flush out pmdown_time work - we actually do want to run it
1947 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948 for (i = 0; i < card->num_rtd; i++) {
1949 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001950 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001951 }
Mark Brown51737472009-06-22 13:16:51 +01001952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001954
1955 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001956}
1957
Alexey Dobriyan47145212009-12-14 18:00:08 -08001958static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001959 .suspend = soc_suspend,
1960 .resume = soc_resume,
1961 .poweroff = soc_poweroff,
1962};
1963
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964/* ASoC platform driver */
1965static struct platform_driver soc_driver = {
1966 .driver = {
1967 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001968 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001969 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970 },
1971 .probe = soc_probe,
1972 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973};
1974
1975/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001977{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978 struct snd_soc_codec *codec = rtd->codec;
1979 struct snd_soc_platform *platform = rtd->platform;
1980 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1981 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 struct snd_pcm *pcm;
1983 char new_name[64];
1984 int ret = 0, playback = 0, capture = 0;
1985
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001987 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001988 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001990 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001993 capture = 1;
1994
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1996 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1997 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000 return ret;
2001 }
2002
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002003 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002005 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2006 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2007 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2008 soc_pcm_ops.copy = platform->driver->ops->copy;
2009 soc_pcm_ops.silence = platform->driver->ops->silence;
2010 soc_pcm_ops.ack = platform->driver->ops->ack;
2011 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002012
2013 if (playback)
2014 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2015
2016 if (capture)
2017 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2018
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002019 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002020 if (ret < 0) {
2021 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 return ret;
2023 }
2024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002025 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2027 cpu_dai->name);
2028 return ret;
2029}
2030
Mark Brown096e49d2009-07-05 15:12:22 +01002031/**
2032 * snd_soc_codec_volatile_register: Report if a register is volatile.
2033 *
2034 * @codec: CODEC to query.
2035 * @reg: Register to query.
2036 *
2037 * Boolean function indiciating if a CODEC register is volatile.
2038 */
2039int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
2040{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002041 if (codec->driver->volatile_register)
2042 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002043 else
2044 return 0;
2045}
2046EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2047
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002048/**
2049 * snd_soc_new_ac97_codec - initailise AC97 device
2050 * @codec: audio codec
2051 * @ops: AC97 bus operations
2052 * @num: AC97 codec number
2053 *
2054 * Initialises AC97 codec resources for use by ad-hoc devices only.
2055 */
2056int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2057 struct snd_ac97_bus_ops *ops, int num)
2058{
2059 mutex_lock(&codec->mutex);
2060
2061 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2062 if (codec->ac97 == NULL) {
2063 mutex_unlock(&codec->mutex);
2064 return -ENOMEM;
2065 }
2066
2067 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2068 if (codec->ac97->bus == NULL) {
2069 kfree(codec->ac97);
2070 codec->ac97 = NULL;
2071 mutex_unlock(&codec->mutex);
2072 return -ENOMEM;
2073 }
2074
2075 codec->ac97->bus->ops = ops;
2076 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002077
2078 /*
2079 * Mark the AC97 device to be created by us. This way we ensure that the
2080 * device will be registered with the device subsystem later on.
2081 */
2082 codec->ac97_created = 1;
2083
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084 mutex_unlock(&codec->mutex);
2085 return 0;
2086}
2087EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2088
2089/**
2090 * snd_soc_free_ac97_codec - free AC97 codec device
2091 * @codec: audio codec
2092 *
2093 * Frees AC97 codec device resources.
2094 */
2095void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2096{
2097 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002098#ifdef CONFIG_SND_SOC_AC97_BUS
2099 soc_unregister_ac97_dai_link(codec);
2100#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002101 kfree(codec->ac97->bus);
2102 kfree(codec->ac97);
2103 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002104 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002105 mutex_unlock(&codec->mutex);
2106}
2107EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2108
Mark Brownc3753702010-11-01 15:41:57 -04002109unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2110{
2111 unsigned int ret;
2112
Mark Brownc3acec22010-12-02 16:15:29 +00002113 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002114 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002115 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002116
2117 return ret;
2118}
2119EXPORT_SYMBOL_GPL(snd_soc_read);
2120
2121unsigned int snd_soc_write(struct snd_soc_codec *codec,
2122 unsigned int reg, unsigned int val)
2123{
2124 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002125 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002126 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002127}
2128EXPORT_SYMBOL_GPL(snd_soc_write);
2129
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002130/**
2131 * snd_soc_update_bits - update codec register bits
2132 * @codec: audio codec
2133 * @reg: codec register
2134 * @mask: register mask
2135 * @value: new value
2136 *
2137 * Writes new register value.
2138 *
2139 * Returns 1 for change else 0.
2140 */
2141int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002142 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002143{
2144 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002145 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147 old = snd_soc_read(codec, reg);
2148 new = (old & ~mask) | value;
2149 change = old != new;
2150 if (change)
2151 snd_soc_write(codec, reg, new);
2152
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153 return change;
2154}
2155EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2156
2157/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002158 * snd_soc_update_bits_locked - update codec register bits
2159 * @codec: audio codec
2160 * @reg: codec register
2161 * @mask: register mask
2162 * @value: new value
2163 *
2164 * Writes new register value, and takes the codec mutex.
2165 *
2166 * Returns 1 for change else 0.
2167 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002168int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2169 unsigned short reg, unsigned int mask,
2170 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002171{
2172 int change;
2173
2174 mutex_lock(&codec->mutex);
2175 change = snd_soc_update_bits(codec, reg, mask, value);
2176 mutex_unlock(&codec->mutex);
2177
2178 return change;
2179}
Mark Browndd1b3d52009-12-04 14:22:03 +00002180EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002181
2182/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183 * snd_soc_test_bits - test register for change
2184 * @codec: audio codec
2185 * @reg: codec register
2186 * @mask: register mask
2187 * @value: new value
2188 *
2189 * Tests a register with a new value and checks if the new value is
2190 * different from the old value.
2191 *
2192 * Returns 1 for change else 0.
2193 */
2194int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002195 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196{
2197 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002198 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 old = snd_soc_read(codec, reg);
2201 new = (old & ~mask) | value;
2202 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203
2204 return change;
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2207
2208/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2210 * @substream: the pcm substream
2211 * @hw: the hardware parameters
2212 *
2213 * Sets the substream runtime hardware parameters.
2214 */
2215int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2216 const struct snd_pcm_hardware *hw)
2217{
2218 struct snd_pcm_runtime *runtime = substream->runtime;
2219 runtime->hw.info = hw->info;
2220 runtime->hw.formats = hw->formats;
2221 runtime->hw.period_bytes_min = hw->period_bytes_min;
2222 runtime->hw.period_bytes_max = hw->period_bytes_max;
2223 runtime->hw.periods_min = hw->periods_min;
2224 runtime->hw.periods_max = hw->periods_max;
2225 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2226 runtime->hw.fifo_size = hw->fifo_size;
2227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2230
2231/**
2232 * snd_soc_cnew - create new control
2233 * @_template: control template
2234 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002235 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236 *
2237 * Create a new mixer control from a template control.
2238 *
2239 * Returns 0 for success, else error.
2240 */
2241struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2242 void *data, char *long_name)
2243{
2244 struct snd_kcontrol_new template;
2245
2246 memcpy(&template, _template, sizeof(template));
2247 if (long_name)
2248 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249 template.index = 0;
2250
2251 return snd_ctl_new1(&template, data);
2252}
2253EXPORT_SYMBOL_GPL(snd_soc_cnew);
2254
2255/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002256 * snd_soc_add_controls - add an array of controls to a codec.
2257 * Convienience function to add a list of controls. Many codecs were
2258 * duplicating this code.
2259 *
2260 * @codec: codec to add controls to
2261 * @controls: array of controls to add
2262 * @num_controls: number of elements in the array
2263 *
2264 * Return 0 for success, else error.
2265 */
2266int snd_soc_add_controls(struct snd_soc_codec *codec,
2267 const struct snd_kcontrol_new *controls, int num_controls)
2268{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002269 struct snd_card *card = codec->card->snd_card;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002270 char prefixed_name[44], *name;
Ian Molton3e8e1952009-01-09 00:23:21 +00002271 int err, i;
2272
2273 for (i = 0; i < num_controls; i++) {
2274 const struct snd_kcontrol_new *control = &controls[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002275 if (codec->name_prefix) {
2276 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2277 codec->name_prefix, control->name);
2278 name = prefixed_name;
2279 } else {
2280 name = control->name;
2281 }
2282 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
Ian Molton3e8e1952009-01-09 00:23:21 +00002283 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002284 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002285 codec->name, name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002286 return err;
2287 }
2288 }
2289
2290 return 0;
2291}
2292EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2293
2294/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295 * snd_soc_info_enum_double - enumerated double mixer info callback
2296 * @kcontrol: mixer control
2297 * @uinfo: control element information
2298 *
2299 * Callback to provide information about a double enumerated
2300 * mixer control.
2301 *
2302 * Returns 0 for success.
2303 */
2304int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_info *uinfo)
2306{
2307 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2308
2309 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2310 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002311 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002313 if (uinfo->value.enumerated.item > e->max - 1)
2314 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315 strcpy(uinfo->value.enumerated.name,
2316 e->texts[uinfo->value.enumerated.item]);
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2320
2321/**
2322 * snd_soc_get_enum_double - enumerated double mixer get callback
2323 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002324 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325 *
2326 * Callback to get the value of a double enumerated mixer.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
2333 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2334 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002335 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002337 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338 ;
2339 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002340 ucontrol->value.enumerated.item[0]
2341 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 if (e->shift_l != e->shift_r)
2343 ucontrol->value.enumerated.item[1] =
2344 (val >> e->shift_r) & (bitmask - 1);
2345
2346 return 0;
2347}
2348EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2349
2350/**
2351 * snd_soc_put_enum_double - enumerated double mixer put callback
2352 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002353 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 *
2355 * Callback to set the value of a double enumerated mixer.
2356 *
2357 * Returns 0 for success.
2358 */
2359int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2363 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002364 unsigned int val;
2365 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002366
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002367 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002369 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 return -EINVAL;
2371 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2372 mask = (bitmask - 1) << e->shift_l;
2373 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002374 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 return -EINVAL;
2376 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2377 mask |= (bitmask - 1) << e->shift_r;
2378 }
2379
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002380 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381}
2382EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2383
2384/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002385 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2386 * @kcontrol: mixer control
2387 * @ucontrol: control element information
2388 *
2389 * Callback to get the value of a double semi enumerated mixer.
2390 *
2391 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2392 * used for handling bitfield coded enumeration for example.
2393 *
2394 * Returns 0 for success.
2395 */
2396int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2398{
2399 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002400 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002401 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002402
2403 reg_val = snd_soc_read(codec, e->reg);
2404 val = (reg_val >> e->shift_l) & e->mask;
2405 for (mux = 0; mux < e->max; mux++) {
2406 if (val == e->values[mux])
2407 break;
2408 }
2409 ucontrol->value.enumerated.item[0] = mux;
2410 if (e->shift_l != e->shift_r) {
2411 val = (reg_val >> e->shift_r) & e->mask;
2412 for (mux = 0; mux < e->max; mux++) {
2413 if (val == e->values[mux])
2414 break;
2415 }
2416 ucontrol->value.enumerated.item[1] = mux;
2417 }
2418
2419 return 0;
2420}
2421EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2422
2423/**
2424 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2425 * @kcontrol: mixer control
2426 * @ucontrol: control element information
2427 *
2428 * Callback to set the value of a double semi enumerated mixer.
2429 *
2430 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2431 * used for handling bitfield coded enumeration for example.
2432 *
2433 * Returns 0 for success.
2434 */
2435int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2436 struct snd_ctl_elem_value *ucontrol)
2437{
2438 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002439 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002440 unsigned int val;
2441 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002442
2443 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2444 return -EINVAL;
2445 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2446 mask = e->mask << e->shift_l;
2447 if (e->shift_l != e->shift_r) {
2448 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2449 return -EINVAL;
2450 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2451 mask |= e->mask << e->shift_r;
2452 }
2453
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002454 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002455}
2456EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2457
2458/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2460 * @kcontrol: mixer control
2461 * @uinfo: control element information
2462 *
2463 * Callback to provide information about an external enumerated
2464 * single mixer.
2465 *
2466 * Returns 0 for success.
2467 */
2468int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_info *uinfo)
2470{
2471 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2472
2473 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2474 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002475 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002476
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002477 if (uinfo->value.enumerated.item > e->max - 1)
2478 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479 strcpy(uinfo->value.enumerated.name,
2480 e->texts[uinfo->value.enumerated.item]);
2481 return 0;
2482}
2483EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2484
2485/**
2486 * snd_soc_info_volsw_ext - external single mixer info callback
2487 * @kcontrol: mixer control
2488 * @uinfo: control element information
2489 *
2490 * Callback to provide information about a single external mixer control.
2491 *
2492 * Returns 0 for success.
2493 */
2494int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_info *uinfo)
2496{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002497 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498
Mark Brownfd5dfad2009-04-15 21:37:46 +01002499 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002500 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2501 else
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2503
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504 uinfo->count = 1;
2505 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002506 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2510
2511/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002512 * snd_soc_info_volsw - single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2515 *
2516 * Callback to provide information about a single mixer control.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_info *uinfo)
2522{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002523 struct soc_mixer_control *mc =
2524 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002525 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002526 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002527 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002529 if (!mc->platform_max)
2530 mc->platform_max = mc->max;
2531 platform_max = mc->platform_max;
2532
2533 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002534 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2535 else
2536 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2537
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002538 uinfo->count = shift == rshift ? 1 : 2;
2539 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002540 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002541 return 0;
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2544
2545/**
2546 * snd_soc_get_volsw - single mixer get callback
2547 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002548 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549 *
2550 * Callback to get the value of a single mixer control.
2551 *
2552 * Returns 0 for success.
2553 */
2554int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002557 struct soc_mixer_control *mc =
2558 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002560 unsigned int reg = mc->reg;
2561 unsigned int shift = mc->shift;
2562 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002563 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002564 unsigned int mask = (1 << fls(max)) - 1;
2565 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566
2567 ucontrol->value.integer.value[0] =
2568 (snd_soc_read(codec, reg) >> shift) & mask;
2569 if (shift != rshift)
2570 ucontrol->value.integer.value[1] =
2571 (snd_soc_read(codec, reg) >> rshift) & mask;
2572 if (invert) {
2573 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002574 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002575 if (shift != rshift)
2576 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002577 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002578 }
2579
2580 return 0;
2581}
2582EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2583
2584/**
2585 * snd_soc_put_volsw - single mixer put callback
2586 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002587 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002588 *
2589 * Callback to set the value of a single mixer control.
2590 *
2591 * Returns 0 for success.
2592 */
2593int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002596 struct soc_mixer_control *mc =
2597 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002598 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002599 unsigned int reg = mc->reg;
2600 unsigned int shift = mc->shift;
2601 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002602 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002603 unsigned int mask = (1 << fls(max)) - 1;
2604 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002605 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002606
2607 val = (ucontrol->value.integer.value[0] & mask);
2608 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002609 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 val_mask = mask << shift;
2611 val = val << shift;
2612 if (shift != rshift) {
2613 val2 = (ucontrol->value.integer.value[1] & mask);
2614 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002615 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002616 val_mask |= mask << rshift;
2617 val |= val2 << rshift;
2618 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002619 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620}
2621EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2622
2623/**
2624 * snd_soc_info_volsw_2r - double mixer info callback
2625 * @kcontrol: mixer control
2626 * @uinfo: control element information
2627 *
2628 * Callback to provide information about a double mixer control that
2629 * spans 2 codec registers.
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_info *uinfo)
2635{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002636 struct soc_mixer_control *mc =
2637 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002638 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002639
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002640 if (!mc->platform_max)
2641 mc->platform_max = mc->max;
2642 platform_max = mc->platform_max;
2643
2644 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002645 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2646 else
2647 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2648
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002649 uinfo->count = 2;
2650 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002651 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2655
2656/**
2657 * snd_soc_get_volsw_2r - double mixer get callback
2658 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002659 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002660 *
2661 * Callback to get the value of a double mixer control that spans 2 registers.
2662 *
2663 * Returns 0 for success.
2664 */
2665int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_value *ucontrol)
2667{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002668 struct soc_mixer_control *mc =
2669 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002670 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002671 unsigned int reg = mc->reg;
2672 unsigned int reg2 = mc->rreg;
2673 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002674 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002675 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002676 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002677
2678 ucontrol->value.integer.value[0] =
2679 (snd_soc_read(codec, reg) >> shift) & mask;
2680 ucontrol->value.integer.value[1] =
2681 (snd_soc_read(codec, reg2) >> shift) & mask;
2682 if (invert) {
2683 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002684 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002686 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002687 }
2688
2689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2692
2693/**
2694 * snd_soc_put_volsw_2r - double mixer set callback
2695 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002696 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002697 *
2698 * Callback to set the value of a double mixer control that spans 2 registers.
2699 *
2700 * Returns 0 for success.
2701 */
2702int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2703 struct snd_ctl_elem_value *ucontrol)
2704{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002705 struct soc_mixer_control *mc =
2706 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002707 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002708 unsigned int reg = mc->reg;
2709 unsigned int reg2 = mc->rreg;
2710 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002711 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002712 unsigned int mask = (1 << fls(max)) - 1;
2713 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002715 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002716
2717 val_mask = mask << shift;
2718 val = (ucontrol->value.integer.value[0] & mask);
2719 val2 = (ucontrol->value.integer.value[1] & mask);
2720
2721 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002722 val = max - val;
2723 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002724 }
2725
2726 val = val << shift;
2727 val2 = val2 << shift;
2728
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002729 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002730 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002731 return err;
2732
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002733 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734 return err;
2735}
2736EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2737
Mark Browne13ac2e2008-05-28 17:58:05 +01002738/**
2739 * snd_soc_info_volsw_s8 - signed mixer info callback
2740 * @kcontrol: mixer control
2741 * @uinfo: control element information
2742 *
2743 * Callback to provide information about a signed mixer control.
2744 *
2745 * Returns 0 for success.
2746 */
2747int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_info *uinfo)
2749{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002750 struct soc_mixer_control *mc =
2751 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002752 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002753 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002754
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002755 if (!mc->platform_max)
2756 mc->platform_max = mc->max;
2757 platform_max = mc->platform_max;
2758
Mark Browne13ac2e2008-05-28 17:58:05 +01002759 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2760 uinfo->count = 2;
2761 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002762 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002763 return 0;
2764}
2765EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2766
2767/**
2768 * snd_soc_get_volsw_s8 - signed mixer get callback
2769 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002770 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002771 *
2772 * Callback to get the value of a signed mixer control.
2773 *
2774 * Returns 0 for success.
2775 */
2776int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
2778{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002779 struct soc_mixer_control *mc =
2780 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002781 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002782 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002783 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002784 int val = snd_soc_read(codec, reg);
2785
2786 ucontrol->value.integer.value[0] =
2787 ((signed char)(val & 0xff))-min;
2788 ucontrol->value.integer.value[1] =
2789 ((signed char)((val >> 8) & 0xff))-min;
2790 return 0;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2793
2794/**
2795 * snd_soc_put_volsw_sgn - signed mixer put callback
2796 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002797 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002798 *
2799 * Callback to set the value of a signed mixer control.
2800 *
2801 * Returns 0 for success.
2802 */
2803int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2805{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002806 struct soc_mixer_control *mc =
2807 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002808 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002809 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002810 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002811 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002812
2813 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2814 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2815
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002816 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002817}
2818EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2819
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002820/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002821 * snd_soc_limit_volume - Set new limit to an existing volume control.
2822 *
2823 * @codec: where to look for the control
2824 * @name: Name of the control
2825 * @max: new maximum limit
2826 *
2827 * Return 0 for success, else error.
2828 */
2829int snd_soc_limit_volume(struct snd_soc_codec *codec,
2830 const char *name, int max)
2831{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002832 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002833 struct snd_kcontrol *kctl;
2834 struct soc_mixer_control *mc;
2835 int found = 0;
2836 int ret = -EINVAL;
2837
2838 /* Sanity check for name and max */
2839 if (unlikely(!name || max <= 0))
2840 return -EINVAL;
2841
2842 list_for_each_entry(kctl, &card->controls, list) {
2843 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2844 found = 1;
2845 break;
2846 }
2847 }
2848 if (found) {
2849 mc = (struct soc_mixer_control *)kctl->private_value;
2850 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002851 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002852 ret = 0;
2853 }
2854 }
2855 return ret;
2856}
2857EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2858
2859/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002860 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2861 * mixer info callback
2862 * @kcontrol: mixer control
2863 * @uinfo: control element information
2864 *
2865 * Returns 0 for success.
2866 */
2867int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_info *uinfo)
2869{
2870 struct soc_mixer_control *mc =
2871 (struct soc_mixer_control *)kcontrol->private_value;
2872 int max = mc->max;
2873 int min = mc->min;
2874
2875 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2876 uinfo->count = 2;
2877 uinfo->value.integer.min = 0;
2878 uinfo->value.integer.max = max-min;
2879
2880 return 0;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2883
2884/**
2885 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2886 * mixer get callback
2887 * @kcontrol: mixer control
2888 * @uinfo: control element information
2889 *
2890 * Returns 0 for success.
2891 */
2892int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2894{
2895 struct soc_mixer_control *mc =
2896 (struct soc_mixer_control *)kcontrol->private_value;
2897 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2898 unsigned int mask = (1<<mc->shift)-1;
2899 int min = mc->min;
2900 int val = snd_soc_read(codec, mc->reg) & mask;
2901 int valr = snd_soc_read(codec, mc->rreg) & mask;
2902
Stuart Longland20630c72010-06-18 12:56:10 +10002903 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2904 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002905 return 0;
2906}
2907EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2908
2909/**
2910 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2911 * mixer put callback
2912 * @kcontrol: mixer control
2913 * @uinfo: control element information
2914 *
2915 * Returns 0 for success.
2916 */
2917int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2918 struct snd_ctl_elem_value *ucontrol)
2919{
2920 struct soc_mixer_control *mc =
2921 (struct soc_mixer_control *)kcontrol->private_value;
2922 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2923 unsigned int mask = (1<<mc->shift)-1;
2924 int min = mc->min;
2925 int ret;
2926 unsigned int val, valr, oval, ovalr;
2927
2928 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2929 val &= mask;
2930 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2931 valr &= mask;
2932
2933 oval = snd_soc_read(codec, mc->reg) & mask;
2934 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2935
2936 ret = 0;
2937 if (oval != val) {
2938 ret = snd_soc_write(codec, mc->reg, val);
2939 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002940 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002941 }
2942 if (ovalr != valr) {
2943 ret = snd_soc_write(codec, mc->rreg, valr);
2944 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002945 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002946 }
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2951
2952/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002953 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2954 * @dai: DAI
2955 * @clk_id: DAI specific clock ID
2956 * @freq: new clock frequency in Hz
2957 * @dir: new clock direction - input/output.
2958 *
2959 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2960 */
2961int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2962 unsigned int freq, int dir)
2963{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002964 if (dai->driver && dai->driver->ops->set_sysclk)
2965 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002966 else
2967 return -EINVAL;
2968}
2969EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2970
2971/**
2972 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2973 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002974 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002975 * @div: new clock divisor.
2976 *
2977 * Configures the clock dividers. This is used to derive the best DAI bit and
2978 * frame clocks from the system or master clock. It's best to set the DAI bit
2979 * and frame clocks as low as possible to save system power.
2980 */
2981int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2982 int div_id, int div)
2983{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002984 if (dai->driver && dai->driver->ops->set_clkdiv)
2985 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002986 else
2987 return -EINVAL;
2988}
2989EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2990
2991/**
2992 * snd_soc_dai_set_pll - configure DAI PLL.
2993 * @dai: DAI
2994 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002995 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002996 * @freq_in: PLL input clock frequency in Hz
2997 * @freq_out: requested PLL output clock frequency in Hz
2998 *
2999 * Configures and enables PLL to generate output clock based on input clock.
3000 */
Mark Brown85488032009-09-05 18:52:16 +01003001int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3002 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003003{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003004 if (dai->driver && dai->driver->ops->set_pll)
3005 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003006 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003007 else
3008 return -EINVAL;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3011
3012/**
3013 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3014 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003015 * @fmt: SND_SOC_DAIFMT_ format value.
3016 *
3017 * Configures the DAI hardware format and clocking.
3018 */
3019int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3020{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003021 if (dai->driver && dai->driver->ops->set_fmt)
3022 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003023 else
3024 return -EINVAL;
3025}
3026EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3027
3028/**
3029 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3030 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003031 * @tx_mask: bitmask representing active TX slots.
3032 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003033 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003034 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003035 *
3036 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3037 * specific.
3038 */
3039int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003040 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003041{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003042 if (dai->driver && dai->driver->ops->set_tdm_slot)
3043 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003044 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003045 else
3046 return -EINVAL;
3047}
3048EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3049
3050/**
Barry Song472df3c2009-09-12 01:16:29 +08003051 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3052 * @dai: DAI
3053 * @tx_num: how many TX channels
3054 * @tx_slot: pointer to an array which imply the TX slot number channel
3055 * 0~num-1 uses
3056 * @rx_num: how many RX channels
3057 * @rx_slot: pointer to an array which imply the RX slot number channel
3058 * 0~num-1 uses
3059 *
3060 * configure the relationship between channel number and TDM slot number.
3061 */
3062int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3063 unsigned int tx_num, unsigned int *tx_slot,
3064 unsigned int rx_num, unsigned int *rx_slot)
3065{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003066 if (dai->driver && dai->driver->ops->set_channel_map)
3067 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003068 rx_num, rx_slot);
3069 else
3070 return -EINVAL;
3071}
3072EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3073
3074/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003075 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3076 * @dai: DAI
3077 * @tristate: tristate enable
3078 *
3079 * Tristates the DAI so that others can use it.
3080 */
3081int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3082{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003083 if (dai->driver && dai->driver->ops->set_tristate)
3084 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003085 else
3086 return -EINVAL;
3087}
3088EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3089
3090/**
3091 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3092 * @dai: DAI
3093 * @mute: mute enable
3094 *
3095 * Mutes the DAI DAC.
3096 */
3097int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3098{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003099 if (dai->driver && dai->driver->ops->digital_mute)
3100 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003101 else
3102 return -EINVAL;
3103}
3104EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3105
Mark Brownc5af3a22008-11-28 13:29:45 +00003106/**
3107 * snd_soc_register_card - Register a card with the ASoC core
3108 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003109 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003110 *
3111 * Note that currently this is an internal only function: it will be
3112 * exposed to machine drivers after further backporting of ASoC v2
3113 * registration APIs.
3114 */
3115static int snd_soc_register_card(struct snd_soc_card *card)
3116{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003117 int i;
3118
Mark Brownc5af3a22008-11-28 13:29:45 +00003119 if (!card->name || !card->dev)
3120 return -EINVAL;
3121
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003122 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3123 (card->num_links + card->num_aux_devs),
3124 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 if (card->rtd == NULL)
3126 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003127 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003128
3129 for (i = 0; i < card->num_links; i++)
3130 card->rtd[i].dai_link = &card->dai_link[i];
3131
Mark Brownc5af3a22008-11-28 13:29:45 +00003132 INIT_LIST_HEAD(&card->list);
3133 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003134 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003135
3136 mutex_lock(&client_mutex);
3137 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003138 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003139 mutex_unlock(&client_mutex);
3140
3141 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3142
3143 return 0;
3144}
3145
3146/**
3147 * snd_soc_unregister_card - Unregister a card with the ASoC core
3148 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003149 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003150 *
3151 * Note that currently this is an internal only function: it will be
3152 * exposed to machine drivers after further backporting of ASoC v2
3153 * registration APIs.
3154 */
3155static int snd_soc_unregister_card(struct snd_soc_card *card)
3156{
3157 mutex_lock(&client_mutex);
3158 list_del(&card->list);
3159 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003160 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3161
3162 return 0;
3163}
3164
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003165/*
3166 * Simplify DAI link configuration by removing ".-1" from device names
3167 * and sanitizing names.
3168 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003169static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003170{
3171 char *found, name[NAME_SIZE];
3172 int id1, id2;
3173
3174 if (dev_name(dev) == NULL)
3175 return NULL;
3176
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003177 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003178
3179 /* are we a "%s.%d" name (platform and SPI components) */
3180 found = strstr(name, dev->driver->name);
3181 if (found) {
3182 /* get ID */
3183 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3184
3185 /* discard ID from name if ID == -1 */
3186 if (*id == -1)
3187 found[strlen(dev->driver->name)] = '\0';
3188 }
3189
3190 } else {
3191 /* I2C component devices are named "bus-addr" */
3192 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3193 char tmp[NAME_SIZE];
3194
3195 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003196 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003197
3198 /* sanitize component name for DAI link creation */
3199 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003200 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003201 } else
3202 *id = 0;
3203 }
3204
3205 return kstrdup(name, GFP_KERNEL);
3206}
3207
3208/*
3209 * Simplify DAI link naming for single devices with multiple DAIs by removing
3210 * any ".-1" and using the DAI name (instead of device name).
3211 */
3212static inline char *fmt_multiple_name(struct device *dev,
3213 struct snd_soc_dai_driver *dai_drv)
3214{
3215 if (dai_drv->name == NULL) {
3216 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3217 dev_name(dev));
3218 return NULL;
3219 }
3220
3221 return kstrdup(dai_drv->name, GFP_KERNEL);
3222}
3223
Mark Brown91151712008-11-30 23:31:24 +00003224/**
3225 * snd_soc_register_dai - Register a DAI with the ASoC core
3226 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003227 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003228 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003229int snd_soc_register_dai(struct device *dev,
3230 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003231{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003232 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003233
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003234 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003236 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3237 if (dai == NULL)
3238 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003239
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003240 /* create DAI component name */
3241 dai->name = fmt_single_name(dev, &dai->id);
3242 if (dai->name == NULL) {
3243 kfree(dai);
3244 return -ENOMEM;
3245 }
3246
3247 dai->dev = dev;
3248 dai->driver = dai_drv;
3249 if (!dai->driver->ops)
3250 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003251
3252 mutex_lock(&client_mutex);
3253 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003254 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003255 mutex_unlock(&client_mutex);
3256
3257 pr_debug("Registered DAI '%s'\n", dai->name);
3258
3259 return 0;
3260}
3261EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3262
3263/**
3264 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3265 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003266 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003267 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003268void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003269{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003270 struct snd_soc_dai *dai;
3271
3272 list_for_each_entry(dai, &dai_list, list) {
3273 if (dev == dai->dev)
3274 goto found;
3275 }
3276 return;
3277
3278found:
Mark Brown91151712008-11-30 23:31:24 +00003279 mutex_lock(&client_mutex);
3280 list_del(&dai->list);
3281 mutex_unlock(&client_mutex);
3282
3283 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003284 kfree(dai->name);
3285 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003286}
3287EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3288
3289/**
3290 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3291 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003292 * @dai: Array of DAIs to register
3293 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003294 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003295int snd_soc_register_dais(struct device *dev,
3296 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003297{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298 struct snd_soc_dai *dai;
3299 int i, ret = 0;
3300
Liam Girdwood720ffa42010-08-18 00:30:30 +01003301 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003302
3303 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003304
3305 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003306 if (dai == NULL) {
3307 ret = -ENOMEM;
3308 goto err;
3309 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003310
3311 /* create DAI component name */
3312 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3313 if (dai->name == NULL) {
3314 kfree(dai);
3315 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003316 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003317 }
3318
3319 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003320 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003321 if (dai->driver->id)
3322 dai->id = dai->driver->id;
3323 else
3324 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003325 if (!dai->driver->ops)
3326 dai->driver->ops = &null_dai_ops;
3327
3328 mutex_lock(&client_mutex);
3329 list_add(&dai->list, &dai_list);
3330 mutex_unlock(&client_mutex);
3331
3332 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003333 }
3334
Axel Lin1dcb4f32010-12-06 16:48:03 +08003335 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003336 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003337 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003338 return 0;
3339
3340err:
3341 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003342 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003343
3344 return ret;
3345}
3346EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3347
3348/**
3349 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3350 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003351 * @dai: Array of DAIs to unregister
3352 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003353 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003354void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003355{
3356 int i;
3357
3358 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003359 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003360}
3361EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3362
Mark Brown12a48a8c2008-12-03 19:40:30 +00003363/**
3364 * snd_soc_register_platform - Register a platform with the ASoC core
3365 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003366 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003367 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003368int snd_soc_register_platform(struct device *dev,
3369 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003370{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003371 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003372
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003373 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3374
3375 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3376 if (platform == NULL)
3377 return -ENOMEM;
3378
3379 /* create platform component name */
3380 platform->name = fmt_single_name(dev, &platform->id);
3381 if (platform->name == NULL) {
3382 kfree(platform);
3383 return -ENOMEM;
3384 }
3385
3386 platform->dev = dev;
3387 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003388
3389 mutex_lock(&client_mutex);
3390 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003391 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003392 mutex_unlock(&client_mutex);
3393
3394 pr_debug("Registered platform '%s'\n", platform->name);
3395
3396 return 0;
3397}
3398EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3399
3400/**
3401 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3402 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003403 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003404 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003405void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003406{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003407 struct snd_soc_platform *platform;
3408
3409 list_for_each_entry(platform, &platform_list, list) {
3410 if (dev == platform->dev)
3411 goto found;
3412 }
3413 return;
3414
3415found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003416 mutex_lock(&client_mutex);
3417 list_del(&platform->list);
3418 mutex_unlock(&client_mutex);
3419
3420 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003421 kfree(platform->name);
3422 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003423}
3424EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3425
Mark Brown151ab222009-05-09 16:22:58 +01003426static u64 codec_format_map[] = {
3427 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3428 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3429 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3430 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3431 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3432 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3433 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3434 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3435 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3436 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3437 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3438 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3439 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3440 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3441 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3442 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3443};
3444
3445/* Fix up the DAI formats for endianness: codecs don't actually see
3446 * the endianness of the data but we're using the CPU format
3447 * definitions which do need to include endianness so we ensure that
3448 * codec DAIs always have both big and little endian variants set.
3449 */
3450static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3451{
3452 int i;
3453
3454 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3455 if (stream->formats & codec_format_map[i])
3456 stream->formats |= codec_format_map[i];
3457}
3458
Mark Brown0d0cf002008-12-10 14:32:45 +00003459/**
3460 * snd_soc_register_codec - Register a codec with the ASoC core
3461 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003462 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003463 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003464int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003465 const struct snd_soc_codec_driver *codec_drv,
3466 struct snd_soc_dai_driver *dai_drv,
3467 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003468{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003469 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003470 struct snd_soc_codec *codec;
3471 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003472
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003473 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003475 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3476 if (codec == NULL)
3477 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003479 /* create CODEC component name */
3480 codec->name = fmt_single_name(dev, &codec->id);
3481 if (codec->name == NULL) {
3482 kfree(codec);
3483 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003484 }
3485
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003486 if (codec_drv->compress_type)
3487 codec->compress_type = codec_drv->compress_type;
3488 else
3489 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3490
Mark Brownc3acec22010-12-02 16:15:29 +00003491 codec->write = codec_drv->write;
3492 codec->read = codec_drv->read;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003493 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3494 codec->dapm.dev = dev;
3495 codec->dapm.codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003496 codec->dev = dev;
3497 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003498 codec->num_dai = num_dai;
3499 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003501 /* allocate CODEC register cache */
3502 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003503 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3504 /* it is necessary to make a copy of the default register cache
3505 * because in the case of using a compression type that requires
3506 * the default register cache to be marked as __devinitconst the
3507 * kernel might have freed the array by the time we initialize
3508 * the cache.
3509 */
3510 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3511 reg_size, GFP_KERNEL);
3512 if (!codec->reg_def_copy) {
3513 ret = -ENOMEM;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003514 goto fail;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003515 }
3516 }
3517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003518 for (i = 0; i < num_dai; i++) {
3519 fixup_codec_formats(&dai_drv[i].playback);
3520 fixup_codec_formats(&dai_drv[i].capture);
3521 }
3522
Mark Brown26b01cc2010-08-18 20:20:55 +01003523 /* register any DAIs */
3524 if (num_dai) {
3525 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3526 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003527 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003528 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003529
Mark Brown0d0cf002008-12-10 14:32:45 +00003530 mutex_lock(&client_mutex);
3531 list_add(&codec->list, &codec_list);
3532 snd_soc_instantiate_cards();
3533 mutex_unlock(&client_mutex);
3534
3535 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003536 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003537
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003538fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003539 kfree(codec->reg_def_copy);
3540 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541 kfree(codec->name);
3542 kfree(codec);
3543 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003544}
3545EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3546
3547/**
3548 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3549 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003550 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003551 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003553{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003554 struct snd_soc_codec *codec;
3555 int i;
3556
3557 list_for_each_entry(codec, &codec_list, list) {
3558 if (dev == codec->dev)
3559 goto found;
3560 }
3561 return;
3562
3563found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003564 if (codec->num_dai)
3565 for (i = 0; i < codec->num_dai; i++)
3566 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003567
Mark Brown0d0cf002008-12-10 14:32:45 +00003568 mutex_lock(&client_mutex);
3569 list_del(&codec->list);
3570 mutex_unlock(&client_mutex);
3571
3572 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003573
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003574 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003575 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003576 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003577 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003578}
3579EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3580
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003581static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003582{
Mark Brown384c89e2008-12-03 17:34:03 +00003583#ifdef CONFIG_DEBUG_FS
3584 debugfs_root = debugfs_create_dir("asoc", NULL);
3585 if (IS_ERR(debugfs_root) || !debugfs_root) {
3586 printk(KERN_WARNING
3587 "ASoC: Failed to create debugfs directory\n");
3588 debugfs_root = NULL;
3589 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003590
3591 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3592 &codec_list_fops))
3593 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3594
Mark Brownf3208782010-09-15 18:19:07 +01003595 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3596 &dai_list_fops))
3597 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003598
3599 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3600 &platform_list_fops))
3601 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003602#endif
3603
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003604 return platform_driver_register(&soc_driver);
3605}
Mark Brown4abe8e12010-10-12 17:41:03 +01003606module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003607
Mark Brown7d8c16a2008-11-30 22:11:24 +00003608static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003609{
Mark Brown384c89e2008-12-03 17:34:03 +00003610#ifdef CONFIG_DEBUG_FS
3611 debugfs_remove_recursive(debugfs_root);
3612#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003613 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003614}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003615module_exit(snd_soc_exit);
3616
3617/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003618MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003619MODULE_DESCRIPTION("ALSA SoC Core");
3620MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003621MODULE_ALIAS("platform:soc-audio");