blob: 10bc3c23b525166c6b6b050afef1cfec468e36d7 [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
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
56static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000057static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000058static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000061static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000072/* returns the minimum number of bytes needed to represent
73 * a particular given value */
74static int min_bytes_needed(unsigned long val)
75{
76 int c = 0;
77 int i;
78
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
88}
89
Mark Brown2624d5f2009-11-03 21:56:13 +000090/* codec register dump */
91static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
92{
Mark Brown5164d742010-07-14 16:14:33 +010093 int ret, i, step = 1, count = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000094 int wordsize, regsize;
95
96 wordsize = codec->driver->reg_word_size * 2;
97 regsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +000098
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000099 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000100 return 0;
101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000102 if (codec->driver->reg_cache_step)
103 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000105 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +0000106 if (codec->readable_register && !codec->readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000107 continue;
108
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000109 count += sprintf(buf + count, "%.*x: ", regsize, i);
Mark Brown2624d5f2009-11-03 21:56:13 +0000110 if (count >= PAGE_SIZE - 1)
111 break;
112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000113 if (codec->driver->display_register) {
114 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000115 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100116 } else {
117 /* If the read fails it's almost certainly due to
118 * the register being volatile and the device being
119 * powered off.
120 */
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000121 ret = snd_soc_read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100122 if (ret >= 0)
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000125 "%.*x", wordsize, ret);
Mark Brown5164d742010-07-14 16:14:33 +0100126 else
127 count += snprintf(buf + count,
128 PAGE_SIZE - count,
129 "<no data: %d>", ret);
130 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000131
132 if (count >= PAGE_SIZE - 1)
133 break;
134
135 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
136 if (count >= PAGE_SIZE - 1)
137 break;
138 }
139
140 /* Truncate count; min() would cause a warning */
141 if (count >= PAGE_SIZE)
142 count = PAGE_SIZE - 1;
143
144 return count;
145}
146static ssize_t codec_reg_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 struct snd_soc_pcm_runtime *rtd =
150 container_of(dev, struct snd_soc_pcm_runtime, dev);
151
152 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000153}
154
155static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
156
Mark Browndbe21402010-02-12 11:37:24 +0000157static ssize_t pmdown_time_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000160 struct snd_soc_pcm_runtime *rtd =
161 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000164}
165
166static ssize_t pmdown_time_set(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
169{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000170 struct snd_soc_pcm_runtime *rtd =
171 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700172 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000173
Mark Brownc593b522010-10-27 20:11:17 -0700174 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
175 if (ret)
176 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000177
178 return count;
179}
180
181static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
182
Mark Brown2624d5f2009-11-03 21:56:13 +0000183#ifdef CONFIG_DEBUG_FS
184static int codec_reg_open_file(struct inode *inode, struct file *file)
185{
186 file->private_data = inode->i_private;
187 return 0;
188}
189
190static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
191 size_t count, loff_t *ppos)
192{
193 ssize_t ret;
194 struct snd_soc_codec *codec = file->private_data;
195 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
196 if (!buf)
197 return -ENOMEM;
198 ret = soc_codec_reg_show(codec, buf);
199 if (ret >= 0)
200 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
201 kfree(buf);
202 return ret;
203}
204
205static ssize_t codec_reg_write_file(struct file *file,
206 const char __user *user_buf, size_t count, loff_t *ppos)
207{
208 char buf[32];
209 int buf_size;
210 char *start = buf;
211 unsigned long reg, value;
212 int step = 1;
213 struct snd_soc_codec *codec = file->private_data;
214
215 buf_size = min(count, (sizeof(buf)-1));
216 if (copy_from_user(buf, user_buf, buf_size))
217 return -EFAULT;
218 buf[buf_size] = 0;
219
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220 if (codec->driver->reg_cache_step)
221 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000222
223 while (*start == ' ')
224 start++;
225 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000226 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 return -EINVAL;
228 while (*start == ' ')
229 start++;
230 if (strict_strtoul(start, 16, &value))
231 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000232
233 /* Userspace has been fiddling around behind the kernel's back */
234 add_taint(TAINT_USER);
235
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000236 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 return buf_size;
238}
239
240static const struct file_operations codec_reg_fops = {
241 .open = codec_reg_open_file,
242 .read = codec_reg_read_file,
243 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200244 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000245};
246
247static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
248{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200249 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
250
251 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
252 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 if (!codec->debugfs_codec_root) {
254 printk(KERN_WARNING
255 "ASoC: Failed to create codec debugfs directory\n");
256 return;
257 }
258
Mark Brownaaee8ef2011-01-26 20:53:50 +0000259 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
260 &codec->cache_sync);
261 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
262 &codec->cache_only);
263
Mark Brown2624d5f2009-11-03 21:56:13 +0000264 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
265 codec->debugfs_codec_root,
266 codec, &codec_reg_fops);
267 if (!codec->debugfs_reg)
268 printk(KERN_WARNING
269 "ASoC: Failed to create codec register debugfs file\n");
270
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000272 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200273 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000274 printk(KERN_WARNING
275 "Failed to create DAPM debugfs directory\n");
276
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200277 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000278}
279
280static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
281{
282 debugfs_remove_recursive(codec->debugfs_codec_root);
283}
284
Mark Brownc3c5a192010-09-15 18:15:14 +0100285static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
286 size_t count, loff_t *ppos)
287{
288 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100289 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100290 struct snd_soc_codec *codec;
291
292 if (!buf)
293 return -ENOMEM;
294
Mark Brown2b194f9d2010-10-13 10:52:16 +0100295 list_for_each_entry(codec, &codec_list, list) {
296 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
297 codec->name);
298 if (len >= 0)
299 ret += len;
300 if (ret > PAGE_SIZE) {
301 ret = PAGE_SIZE;
302 break;
303 }
304 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100305
306 if (ret >= 0)
307 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
308
309 kfree(buf);
310
311 return ret;
312}
313
314static const struct file_operations codec_list_fops = {
315 .read = codec_list_read_file,
316 .llseek = default_llseek,/* read accesses f_pos */
317};
318
Mark Brownf3208782010-09-15 18:19:07 +0100319static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
320 size_t count, loff_t *ppos)
321{
322 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100323 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100324 struct snd_soc_dai *dai;
325
326 if (!buf)
327 return -ENOMEM;
328
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 list_for_each_entry(dai, &dai_list, list) {
330 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
Mark Brownf3208782010-09-15 18:19:07 +0100338
Mark Brown2b194f9d2010-10-13 10:52:16 +0100339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100340
341 kfree(buf);
342
343 return ret;
344}
345
346static const struct file_operations dai_list_fops = {
347 .read = dai_list_read_file,
348 .llseek = default_llseek,/* read accesses f_pos */
349};
350
Mark Brown19c7ac22010-09-15 18:22:40 +0100351static ssize_t platform_list_read_file(struct file *file,
352 char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100357 struct snd_soc_platform *platform;
358
359 if (!buf)
360 return -ENOMEM;
361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 list_for_each_entry(platform, &platform_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
364 platform->name);
365 if (len >= 0)
366 ret += len;
367 if (ret > PAGE_SIZE) {
368 ret = PAGE_SIZE;
369 break;
370 }
371 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100372
Mark Brown2b194f9d2010-10-13 10:52:16 +0100373 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100374
375 kfree(buf);
376
377 return ret;
378}
379
380static const struct file_operations platform_list_fops = {
381 .read = platform_list_read_file,
382 .llseek = default_llseek,/* read accesses f_pos */
383};
384
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200385static void soc_init_card_debugfs(struct snd_soc_card *card)
386{
387 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000388 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200389 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200390 dev_warn(card->dev,
391 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200392 return;
393 }
394
395 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
396 card->debugfs_card_root,
397 &card->pop_time);
398 if (!card->debugfs_pop_time)
399 dev_warn(card->dev,
400 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200401}
402
403static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
404{
405 debugfs_remove_recursive(card->debugfs_card_root);
406}
407
Mark Brown2624d5f2009-11-03 21:56:13 +0000408#else
409
410static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
411{
412}
413
414static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
415{
416}
Axel Linb95fccb2010-11-09 17:06:44 +0800417
418static inline void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420}
421
422static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
423{
424}
Mark Brown2624d5f2009-11-03 21:56:13 +0000425#endif
426
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200427#ifdef CONFIG_SND_SOC_AC97_BUS
428/* unregister ac97 codec */
429static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
430{
431 if (codec->ac97->dev.bus)
432 device_unregister(&codec->ac97->dev);
433 return 0;
434}
435
436/* stop no dev release warning */
437static void soc_ac97_device_release(struct device *dev){}
438
439/* register ac97 codec to bus */
440static int soc_ac97_dev_register(struct snd_soc_codec *codec)
441{
442 int err;
443
444 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100445 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446 codec->ac97->dev.release = soc_ac97_device_release;
447
Kay Sieversbb072bf2008-11-02 03:50:35 +0100448 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 err = device_register(&codec->ac97->dev);
451 if (err < 0) {
452 snd_printk(KERN_ERR "Can't register ac97 bus\n");
453 codec->ac97->dev.bus = NULL;
454 return err;
455 }
456 return 0;
457}
458#endif
459
Mark Brown06f409d2009-04-07 18:10:13 +0100460static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
461{
462 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
464 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100465 int ret;
466
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000467 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
468 rtd->dai_link->symmetric_rates) {
469 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
470 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100471
472 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
473 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 rtd->rate,
475 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100476 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000477 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100478 "Unable to apply rate symmetry constraint: %d\n", ret);
479 return ret;
480 }
481 }
482
483 return 0;
484}
485
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486/*
487 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
488 * then initialized and any private data can be allocated. This also calls
489 * startup for the cpu DAI, platform, machine and codec DAI.
490 */
491static int soc_pcm_open(struct snd_pcm_substream *substream)
492{
493 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 struct snd_soc_platform *platform = rtd->platform;
496 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
497 struct snd_soc_dai *codec_dai = rtd->codec_dai;
498 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
499 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200500 int ret = 0;
501
502 mutex_lock(&pcm_mutex);
503
504 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 if (cpu_dai->driver->ops->startup) {
506 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 if (ret < 0) {
508 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100509 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 goto out;
511 }
512 }
513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 if (platform->driver->ops->open) {
515 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 if (ret < 0) {
517 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
518 goto platform_err;
519 }
520 }
521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 if (codec_dai->driver->ops->startup) {
523 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100524 if (ret < 0) {
525 printk(KERN_ERR "asoc: can't open codec %s\n",
526 codec_dai->name);
527 goto codec_dai_err;
528 }
529 }
530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
532 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 goto machine_err;
536 }
537 }
538
Mark Browna00f90f2010-12-02 16:24:24 +0000539 /* Check that the codec and cpu DAIs are compatible */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
541 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 max(codec_dai_drv->playback.rate_min,
543 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 min(codec_dai_drv->playback.rate_max,
546 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 max(codec_dai_drv->playback.channels_min,
549 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 min(codec_dai_drv->playback.channels_max,
552 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100553 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100555 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
557 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900558 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 runtime->hw.rates |= cpu_dai_drv->playback.rates;
560 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900561 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563 } else {
564 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 max(codec_dai_drv->capture.rate_min,
566 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200567 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 min(codec_dai_drv->capture.rate_max,
569 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200570 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 max(codec_dai_drv->capture.channels_min,
572 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 min(codec_dai_drv->capture.channels_max,
575 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100576 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100578 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
580 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900581 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582 runtime->hw.rates |= cpu_dai_drv->capture.rates;
583 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900584 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586 }
587
588 snd_pcm_limit_hw_rates(runtime);
589 if (!runtime->hw.rates) {
590 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100591 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900592 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593 }
594 if (!runtime->hw.formats) {
595 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100596 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900597 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598 }
599 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
600 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900602 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603 }
604
Mark Brown06f409d2009-04-07 18:10:13 +0100605 /* Symmetry only applies if we've already got an active stream. */
606 if (cpu_dai->active || codec_dai->active) {
607 ret = soc_pcm_apply_symmetry(substream);
608 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900609 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100610 }
611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 pr_debug("asoc: %s <-> %s info:\n",
613 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100614 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
615 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
616 runtime->hw.channels_max);
617 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
618 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
Jassi Brar14dc5732010-02-26 09:12:32 +0900620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 cpu_dai->playback_active++;
622 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900623 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 cpu_dai->capture_active++;
625 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900626 }
627 cpu_dai->active++;
628 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 mutex_unlock(&pcm_mutex);
631 return 0;
632
Jassi Brarbb1c0472010-02-25 11:24:53 +0900633config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
635 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636
Jassi Brarbb1c0472010-02-25 11:24:53 +0900637machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 if (codec_dai->driver->ops->shutdown)
639 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900640
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100641codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 if (platform->driver->ops->close)
643 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644
645platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 if (cpu_dai->driver->ops->shutdown)
647 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200648out:
649 mutex_unlock(&pcm_mutex);
650 return ret;
651}
652
653/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200654 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655 * This is to ensure there are no pops or clicks in between any music tracks
656 * due to DAPM power cycling.
657 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100658static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 struct snd_soc_pcm_runtime *rtd =
661 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
662 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663
664 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
667 codec_dai->driver->playback.stream_name,
668 codec_dai->playback_active ? "active" : "inactive",
669 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 /* are we waiting on this codec DAI stream */
672 if (codec_dai->pop_wait == 1) {
673 codec_dai->pop_wait = 0;
674 snd_soc_dapm_stream_event(rtd,
675 codec_dai->driver->playback.stream_name,
676 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 mutex_unlock(&pcm_mutex);
680}
681
682/*
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and platform are also
685 * shutdown.
686 */
687static int soc_codec_close(struct snd_pcm_substream *substream)
688{
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 struct snd_soc_platform *platform = rtd->platform;
691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 struct snd_soc_dai *codec_dai = rtd->codec_dai;
693 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
695 mutex_lock(&pcm_mutex);
696
Jassi Brar14dc5732010-02-26 09:12:32 +0900697 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 cpu_dai->playback_active--;
699 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900700 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 cpu_dai->capture_active--;
702 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900704
705 cpu_dai->active--;
706 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 codec->active--;
708
Mark Brown6010b2d2008-09-06 18:33:24 +0100709 /* Muting the DAC suppresses artifacts caused during digital
710 * shutdown, for example from stopping clocks.
711 */
712 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
713 snd_soc_dai_digital_mute(codec_dai, 1);
714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 if (cpu_dai->driver->ops->shutdown)
716 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 if (codec_dai->driver->ops->shutdown)
719 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (platform->driver->ops->close)
725 platform->driver->ops->close(substream);
726 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200727
728 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
729 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100730 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 schedule_delayed_work(&rtd->delayed_work,
732 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733 } else {
734 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 snd_soc_dapm_stream_event(rtd,
736 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100737 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738 }
739
740 mutex_unlock(&pcm_mutex);
741 return 0;
742}
743
744/*
745 * Called by ALSA when the PCM substream is prepared, can set format, sample
746 * rate, etc. This function is non atomic and can be called multiple times,
747 * it can refer to the runtime info.
748 */
749static int soc_pcm_prepare(struct snd_pcm_substream *substream)
750{
751 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 struct snd_soc_platform *platform = rtd->platform;
753 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
754 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755 int ret = 0;
756
757 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100758
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
760 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100761 if (ret < 0) {
762 printk(KERN_ERR "asoc: machine prepare error\n");
763 goto out;
764 }
765 }
766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 if (platform->driver->ops->prepare) {
768 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200769 if (ret < 0) {
770 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200772 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773 }
774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 if (codec_dai->driver->ops->prepare) {
776 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200777 if (ret < 0) {
778 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200780 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781 }
782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 if (cpu_dai->driver->ops->prepare) {
784 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100785 if (ret < 0) {
786 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
787 goto out;
788 }
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790
Mark Brownd45f6212008-10-14 13:58:36 +0100791 /* cancel any delayed stream shutdown that is pending */
792 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
793 codec_dai->pop_wait) {
794 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000795 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100796 }
797
Mark Brown452c5ea2009-05-17 21:41:23 +0100798 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 snd_soc_dapm_stream_event(rtd,
800 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100801 SND_SOC_DAPM_STREAM_START);
802 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 snd_soc_dapm_stream_event(rtd,
804 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100805 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100806
Mark Brown452c5ea2009-05-17 21:41:23 +0100807 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200808
809out:
810 mutex_unlock(&pcm_mutex);
811 return ret;
812}
813
814/*
815 * Called by ALSA when the hardware params are set by application. This
816 * function can also be called multiple times and can allocate buffers
817 * (using snd_pcm_lib_* ). It's non-atomic.
818 */
819static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
820 struct snd_pcm_hw_params *params)
821{
822 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 struct snd_soc_platform *platform = rtd->platform;
824 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
825 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200826 int ret = 0;
827
828 mutex_lock(&pcm_mutex);
829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
831 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100833 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200834 goto out;
835 }
836 }
837
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000838 if (codec_dai->driver->ops->hw_params) {
839 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100840 if (ret < 0) {
841 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
842 codec_dai->name);
843 goto codec_err;
844 }
845 }
846
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (cpu_dai->driver->ops->hw_params) {
848 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200850 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100851 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 goto interface_err;
853 }
854 }
855
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 if (platform->driver->ops->hw_params) {
857 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200859 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860 platform->name);
861 goto platform_err;
862 }
863 }
864
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100866
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867out:
868 mutex_unlock(&pcm_mutex);
869 return ret;
870
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 if (cpu_dai->driver->ops->hw_free)
873 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874
875interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000876 if (codec_dai->driver->ops->hw_free)
877 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100878
879codec_err:
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 mutex_unlock(&pcm_mutex);
884 return ret;
885}
886
887/*
Mark Browna00f90f2010-12-02 16:24:24 +0000888 * Frees resources allocated by hw_params, can be called multiple times
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889 */
890static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
891{
892 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 struct snd_soc_platform *platform = rtd->platform;
894 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
895 struct snd_soc_dai *codec_dai = rtd->codec_dai;
896 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897
898 mutex_lock(&pcm_mutex);
899
900 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100901 if (!codec->active)
902 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200903
904 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
906 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907
908 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 if (platform->driver->ops->hw_free)
910 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200911
Mark Browna00f90f2010-12-02 16:24:24 +0000912 /* now free hw params for the DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 if (codec_dai->driver->ops->hw_free)
914 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 if (cpu_dai->driver->ops->hw_free)
917 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200918
919 mutex_unlock(&pcm_mutex);
920 return 0;
921}
922
923static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
924{
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 struct snd_soc_platform *platform = rtd->platform;
927 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
928 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200929 int ret;
930
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 if (codec_dai->driver->ops->trigger) {
932 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200933 if (ret < 0)
934 return ret;
935 }
936
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000937 if (platform->driver->ops->trigger) {
938 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200939 if (ret < 0)
940 return ret;
941 }
942
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 if (cpu_dai->driver->ops->trigger) {
944 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200945 if (ret < 0)
946 return ret;
947 }
948 return 0;
949}
950
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200951/*
952 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200953 * If cpu_dai, codec_dai, platform driver has the delay callback, than
954 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200955 */
956static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
957{
958 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 struct snd_soc_platform *platform = rtd->platform;
960 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
961 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200962 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200963 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200964 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200965
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 if (platform->driver->ops->pointer)
967 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 if (cpu_dai->driver->ops->delay)
970 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200971
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000972 if (codec_dai->driver->ops->delay)
973 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200974
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 if (platform->driver->delay)
976 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200977
978 runtime->delay = delay;
979
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200980 return offset;
981}
982
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200983/* ASoC PCM operations */
984static struct snd_pcm_ops soc_pcm_ops = {
985 .open = soc_pcm_open,
986 .close = soc_codec_close,
987 .hw_params = soc_pcm_hw_params,
988 .hw_free = soc_pcm_hw_free,
989 .prepare = soc_pcm_prepare,
990 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200991 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200992};
993
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000994#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200995/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000996int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200997{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000998 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200999 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000 int i;
1001
Daniel Macke3509ff2009-06-03 17:44:49 +02001002 /* If the initialization of this soc device failed, there is no codec
1003 * associated with it. Just bail out in this case.
1004 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +02001006 return 0;
1007
Andy Green6ed25972008-06-13 16:24:05 +01001008 /* Due to the resume being scheduled into a workqueue we could
1009 * suspend before that's finished - wait for it to complete.
1010 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 snd_power_lock(card->snd_card);
1012 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1013 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +01001014
1015 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +01001017
Mark Browna00f90f2010-12-02 16:24:24 +00001018 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1021 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001024 continue;
1025
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001026 if (drv->ops->digital_mute && dai->playback_active)
1027 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001028 }
1029
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001030 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001031 for (i = 0; i < card->num_rtd; i++) {
1032 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001033 continue;
1034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001036 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001037
Mark Brown87506542008-11-18 20:50:34 +00001038 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001039 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001040
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1043 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001044
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001046 continue;
1047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1049 cpu_dai->driver->suspend(cpu_dai);
1050 if (platform->driver->suspend && !platform->suspended) {
1051 platform->driver->suspend(cpu_dai);
1052 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001053 }
1054 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 /* close any waiting streams and save state */
1057 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +01001058 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001059 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001060 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 for (i = 0; i < card->num_rtd; i++) {
1063 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1064
1065 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001066 continue;
1067
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001068 if (driver->playback.stream_name != NULL)
1069 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1070 SND_SOC_DAPM_STREAM_SUSPEND);
1071
1072 if (driver->capture.stream_name != NULL)
1073 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1074 SND_SOC_DAPM_STREAM_SUSPEND);
1075 }
1076
1077 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001078 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001079 /* If there are paths active then the CODEC will be held with
1080 * bias _ON and should not be suspended. */
1081 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001082 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 case SND_SOC_BIAS_STANDBY:
1084 case SND_SOC_BIAS_OFF:
1085 codec->driver->suspend(codec, PMSG_SUSPEND);
1086 codec->suspended = 1;
1087 break;
1088 default:
1089 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1090 break;
1091 }
1092 }
1093 }
1094
1095 for (i = 0; i < card->num_rtd; i++) {
1096 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1097
1098 if (card->rtd[i].dai_link->ignore_suspend)
1099 continue;
1100
1101 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1102 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001103 }
1104
Mark Brown87506542008-11-18 20:50:34 +00001105 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001106 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001107
1108 return 0;
1109}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001110EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001111
Andy Green6ed25972008-06-13 16:24:05 +01001112/* deferred resume work, so resume can complete before we finished
1113 * setting our codec back up, which can be very slow on I2C
1114 */
1115static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001116{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001117 struct snd_soc_card *card =
1118 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001119 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001120 int i;
1121
Andy Green6ed25972008-06-13 16:24:05 +01001122 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1123 * so userspace apps are blocked from touching us
1124 */
1125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001126 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001127
Mark Brown99497882010-05-07 20:24:05 +01001128 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001129 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001130
Mark Brown87506542008-11-18 20:50:34 +00001131 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001132 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134 /* resume AC97 DAIs */
1135 for (i = 0; i < card->num_rtd; i++) {
1136 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001139 continue;
1140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001141 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1142 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001143 }
1144
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001145 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 /* If the CODEC was idle over suspend then it will have been
1147 * left with bias OFF or STANDBY and suspended so we must now
1148 * resume. Otherwise the suspend was suppressed.
1149 */
1150 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001151 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 case SND_SOC_BIAS_STANDBY:
1153 case SND_SOC_BIAS_OFF:
1154 codec->driver->resume(codec);
1155 codec->suspended = 0;
1156 break;
1157 default:
1158 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1159 break;
1160 }
Mark Brown1547aba2010-05-07 21:11:40 +01001161 }
1162 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001163
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001164 for (i = 0; i < card->num_rtd; i++) {
1165 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001166
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001168 continue;
1169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 if (driver->playback.stream_name != NULL)
1171 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001172 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173
1174 if (driver->capture.stream_name != NULL)
1175 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001176 SND_SOC_DAPM_STREAM_RESUME);
1177 }
1178
Mark Brown3ff3f642008-05-19 12:32:25 +02001179 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001180 for (i = 0; i < card->num_rtd; i++) {
1181 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1182 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001184 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001185 continue;
1186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187 if (drv->ops->digital_mute && dai->playback_active)
1188 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001189 }
1190
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001191 for (i = 0; i < card->num_rtd; i++) {
1192 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1193 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001195 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001196 continue;
1197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1199 cpu_dai->driver->resume(cpu_dai);
1200 if (platform->driver->resume && platform->suspended) {
1201 platform->driver->resume(cpu_dai);
1202 platform->suspended = 0;
1203 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001204 }
1205
Mark Brown87506542008-11-18 20:50:34 +00001206 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001207 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001210
1211 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001213}
1214
1215/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001216int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001217{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001218 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001219 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001220
Mark Brown64ab9ba2009-03-31 11:27:03 +01001221 /* AC97 devices might have other drivers hanging off them so
1222 * need to resume immediately. Other drivers don't have that
1223 * problem and may take a substantial amount of time to resume
1224 * due to I/O costs and anti-pop so handle them out of line.
1225 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001226 for (i = 0; i < card->num_rtd; i++) {
1227 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1228 if (cpu_dai->driver->ac97_control) {
1229 dev_dbg(dev, "Resuming AC97 immediately\n");
1230 soc_resume_deferred(&card->deferred_resume_work);
1231 } else {
1232 dev_dbg(dev, "Scheduling resume work\n");
1233 if (!schedule_work(&card->deferred_resume_work))
1234 dev_err(dev, "resume work item may be lost\n");
1235 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001236 }
Andy Green6ed25972008-06-13 16:24:05 +01001237
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001238 return 0;
1239}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001240EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001241#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001242#define snd_soc_suspend NULL
1243#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001244#endif
1245
Barry Song02a06d32009-10-16 18:13:38 +08001246static struct snd_soc_dai_ops null_dai_ops = {
1247};
1248
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001249static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001250{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1252 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001253 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001254 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001257 if (rtd->complete)
1258 return 1;
1259 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 /* do we already have the CPU DAI for this link ? */
1262 if (rtd->cpu_dai) {
1263 goto find_codec;
1264 }
1265 /* no, then find CPU DAI from registered DAIs*/
1266 list_for_each_entry(cpu_dai, &dai_list, list) {
1267 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1268
1269 if (!try_module_get(cpu_dai->dev->driver->owner))
1270 return -ENODEV;
1271
1272 rtd->cpu_dai = cpu_dai;
1273 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001274 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001275 }
1276 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1277 dai_link->cpu_dai_name);
1278
1279find_codec:
1280 /* do we already have the CODEC for this link ? */
1281 if (rtd->codec) {
1282 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001283 }
1284
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001285 /* no, then find CODEC from registered CODECs*/
1286 list_for_each_entry(codec, &codec_list, list) {
1287 if (!strcmp(codec->name, dai_link->codec_name)) {
1288 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001289
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001290 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1291 list_for_each_entry(codec_dai, &dai_list, list) {
1292 if (codec->dev == codec_dai->dev &&
1293 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1294 rtd->codec_dai = codec_dai;
1295 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001296 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001297 }
1298 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1299 dai_link->codec_dai_name);
1300
1301 goto find_platform;
1302 }
1303 }
1304 dev_dbg(card->dev, "CODEC %s not registered\n",
1305 dai_link->codec_name);
1306
1307find_platform:
1308 /* do we already have the CODEC DAI for this link ? */
1309 if (rtd->platform) {
1310 goto out;
1311 }
1312 /* no, then find CPU DAI from registered DAIs*/
1313 list_for_each_entry(platform, &platform_list, list) {
1314 if (!strcmp(platform->name, dai_link->platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315 rtd->platform = platform;
1316 goto out;
1317 }
1318 }
1319
1320 dev_dbg(card->dev, "platform %s not registered\n",
1321 dai_link->platform_name);
1322 return 0;
1323
1324out:
1325 /* mark rtd as complete if we found all 4 of our client devices */
1326 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1327 rtd->complete = 1;
1328 card->num_rtd++;
1329 }
1330 return 1;
1331}
1332
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333static void soc_remove_codec(struct snd_soc_codec *codec)
1334{
1335 int err;
1336
1337 if (codec->driver->remove) {
1338 err = codec->driver->remove(codec);
1339 if (err < 0)
1340 dev_err(codec->dev,
1341 "asoc: failed to remove %s: %d\n",
1342 codec->name, err);
1343 }
1344
1345 /* Make sure all DAPM widgets are freed */
1346 snd_soc_dapm_free(&codec->dapm);
1347
1348 soc_cleanup_codec_debugfs(codec);
1349 codec->probed = 0;
1350 list_del(&codec->card_list);
1351 module_put(codec->dev->driver->owner);
1352}
1353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001354static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1355{
1356 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1357 struct snd_soc_codec *codec = rtd->codec;
1358 struct snd_soc_platform *platform = rtd->platform;
1359 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1360 int err;
1361
1362 /* unregister the rtd device */
1363 if (rtd->dev_registered) {
1364 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001365 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001366 device_unregister(&rtd->dev);
1367 rtd->dev_registered = 0;
1368 }
1369
1370 /* remove the CODEC DAI */
1371 if (codec_dai && codec_dai->probed) {
1372 if (codec_dai->driver->remove) {
1373 err = codec_dai->driver->remove(codec_dai);
1374 if (err < 0)
1375 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1376 }
1377 codec_dai->probed = 0;
1378 list_del(&codec_dai->card_list);
1379 }
1380
1381 /* remove the platform */
1382 if (platform && platform->probed) {
1383 if (platform->driver->remove) {
1384 err = platform->driver->remove(platform);
1385 if (err < 0)
1386 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1387 }
1388 platform->probed = 0;
1389 list_del(&platform->card_list);
1390 module_put(platform->dev->driver->owner);
1391 }
1392
1393 /* remove the CODEC */
Jarkko Nikula589c3562010-12-06 16:27:07 +02001394 if (codec && codec->probed)
1395 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001396
1397 /* remove the cpu_dai */
1398 if (cpu_dai && cpu_dai->probed) {
1399 if (cpu_dai->driver->remove) {
1400 err = cpu_dai->driver->remove(cpu_dai);
1401 if (err < 0)
1402 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1403 }
1404 cpu_dai->probed = 0;
1405 list_del(&cpu_dai->card_list);
1406 module_put(cpu_dai->dev->driver->owner);
1407 }
1408}
1409
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001410static void soc_set_name_prefix(struct snd_soc_card *card,
1411 struct snd_soc_codec *codec)
1412{
1413 int i;
1414
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001415 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001416 return;
1417
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001418 for (i = 0; i < card->num_configs; i++) {
1419 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001420 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1421 codec->name_prefix = map->name_prefix;
1422 break;
1423 }
1424 }
1425}
1426
Jarkko Nikula589c3562010-12-06 16:27:07 +02001427static int soc_probe_codec(struct snd_soc_card *card,
1428 struct snd_soc_codec *codec)
1429{
1430 int ret = 0;
1431
1432 codec->card = card;
1433 codec->dapm.card = card;
1434 soc_set_name_prefix(card, codec);
1435
Jarkko Nikula70d29332011-01-27 16:24:22 +02001436 if (!try_module_get(codec->dev->driver->owner))
1437 return -ENODEV;
1438
Jarkko Nikula589c3562010-12-06 16:27:07 +02001439 if (codec->driver->probe) {
1440 ret = codec->driver->probe(codec);
1441 if (ret < 0) {
1442 dev_err(codec->dev,
1443 "asoc: failed to probe CODEC %s: %d\n",
1444 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001445 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001446 }
1447 }
1448
1449 soc_init_codec_debugfs(codec);
1450
1451 /* mark codec as probed and add to card codec list */
1452 codec->probed = 1;
1453 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001454 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001455
Jarkko Nikula70d29332011-01-27 16:24:22 +02001456 return 0;
1457
1458err_probe:
1459 module_put(codec->dev->driver->owner);
1460
Jarkko Nikula589c3562010-12-06 16:27:07 +02001461 return ret;
1462}
1463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464static void rtd_release(struct device *dev) {}
1465
Jarkko Nikula589c3562010-12-06 16:27:07 +02001466static int soc_post_component_init(struct snd_soc_card *card,
1467 struct snd_soc_codec *codec,
1468 int num, int dailess)
1469{
1470 struct snd_soc_dai_link *dai_link = NULL;
1471 struct snd_soc_aux_dev *aux_dev = NULL;
1472 struct snd_soc_pcm_runtime *rtd;
1473 const char *temp, *name;
1474 int ret = 0;
1475
1476 if (!dailess) {
1477 dai_link = &card->dai_link[num];
1478 rtd = &card->rtd[num];
1479 name = dai_link->name;
1480 } else {
1481 aux_dev = &card->aux_dev[num];
1482 rtd = &card->rtd_aux[num];
1483 name = aux_dev->name;
1484 }
1485
1486 /* machine controls, routes and widgets are not prefixed */
1487 temp = codec->name_prefix;
1488 codec->name_prefix = NULL;
1489
1490 /* do machine specific initialization */
1491 if (!dailess && dai_link->init)
1492 ret = dai_link->init(rtd);
1493 else if (dailess && aux_dev->init)
1494 ret = aux_dev->init(&codec->dapm);
1495 if (ret < 0) {
1496 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1497 return ret;
1498 }
1499 codec->name_prefix = temp;
1500
1501 /* Make sure all DAPM widgets are instantiated */
1502 snd_soc_dapm_new_widgets(&codec->dapm);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001503
1504 /* register the rtd device */
1505 rtd->codec = codec;
1506 rtd->card = card;
1507 rtd->dev.parent = card->dev;
1508 rtd->dev.release = rtd_release;
1509 rtd->dev.init_name = name;
1510 ret = device_register(&rtd->dev);
1511 if (ret < 0) {
1512 dev_err(card->dev,
1513 "asoc: failed to register runtime device: %d\n", ret);
1514 return ret;
1515 }
1516 rtd->dev_registered = 1;
1517
1518 /* add DAPM sysfs entries for this codec */
1519 ret = snd_soc_dapm_sys_add(&rtd->dev);
1520 if (ret < 0)
1521 dev_err(codec->dev,
1522 "asoc: failed to add codec dapm sysfs entries: %d\n",
1523 ret);
1524
1525 /* add codec sysfs entries */
1526 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1527 if (ret < 0)
1528 dev_err(codec->dev,
1529 "asoc: failed to add codec sysfs files: %d\n", ret);
1530
1531 return 0;
1532}
1533
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001534static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1535{
1536 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1537 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1538 struct snd_soc_codec *codec = rtd->codec;
1539 struct snd_soc_platform *platform = rtd->platform;
1540 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1541 int ret;
1542
1543 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1544
1545 /* config components */
1546 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001548 codec_dai->card = card;
1549 cpu_dai->card = card;
1550
1551 /* set default power off timeout */
1552 rtd->pmdown_time = pmdown_time;
1553
1554 /* probe the cpu_dai */
1555 if (!cpu_dai->probed) {
1556 if (cpu_dai->driver->probe) {
1557 ret = cpu_dai->driver->probe(cpu_dai);
1558 if (ret < 0) {
1559 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1560 cpu_dai->name);
1561 return ret;
1562 }
1563 }
1564 cpu_dai->probed = 1;
1565 /* mark cpu_dai as probed and add to card cpu_dai list */
1566 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1567 }
1568
1569 /* probe the CODEC */
1570 if (!codec->probed) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001571 ret = soc_probe_codec(card, codec);
1572 if (ret < 0)
1573 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001574 }
1575
1576 /* probe the platform */
1577 if (!platform->probed) {
Jarkko Nikula70d29332011-01-27 16:24:22 +02001578 if (!try_module_get(platform->dev->driver->owner))
1579 return -ENODEV;
1580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 if (platform->driver->probe) {
1582 ret = platform->driver->probe(platform);
1583 if (ret < 0) {
1584 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1585 platform->name);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001586 module_put(platform->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 return ret;
1588 }
1589 }
1590 /* mark platform as probed and add to card platform list */
1591 platform->probed = 1;
1592 list_add(&platform->card_list, &card->platform_dev_list);
1593 }
1594
1595 /* probe the CODEC DAI */
1596 if (!codec_dai->probed) {
1597 if (codec_dai->driver->probe) {
1598 ret = codec_dai->driver->probe(codec_dai);
1599 if (ret < 0) {
1600 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1601 codec_dai->name);
1602 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001603 }
1604 }
1605
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 /* mark cpu_dai as probed and add to card cpu_dai list */
1607 codec_dai->probed = 1;
1608 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001609 }
1610
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611 /* DAPM dai link stream work */
1612 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1613
Jarkko Nikula589c3562010-12-06 16:27:07 +02001614 ret = soc_post_component_init(card, codec, num, 0);
1615 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001616 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1619 if (ret < 0)
1620 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622 /* create the pcm */
1623 ret = soc_new_pcm(rtd, num);
1624 if (ret < 0) {
1625 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1626 return ret;
1627 }
1628
1629 /* add platform data for AC97 devices */
1630 if (rtd->codec_dai->driver->ac97_control)
1631 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1632
1633 return 0;
1634}
1635
1636#ifdef CONFIG_SND_SOC_AC97_BUS
1637static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1638{
1639 int ret;
1640
1641 /* Only instantiate AC97 if not already done by the adaptor
1642 * for the generic AC97 subsystem.
1643 */
1644 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001645 /*
1646 * It is possible that the AC97 device is already registered to
1647 * the device subsystem. This happens when the device is created
1648 * via snd_ac97_mixer(). Currently only SoC codec that does so
1649 * is the generic AC97 glue but others migh emerge.
1650 *
1651 * In those cases we don't try to register the device again.
1652 */
1653 if (!rtd->codec->ac97_created)
1654 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655
1656 ret = soc_ac97_dev_register(rtd->codec);
1657 if (ret < 0) {
1658 printk(KERN_ERR "asoc: AC97 device register failed\n");
1659 return ret;
1660 }
1661
1662 rtd->codec->ac97_registered = 1;
1663 }
1664 return 0;
1665}
1666
1667static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1668{
1669 if (codec->ac97_registered) {
1670 soc_ac97_dev_unregister(codec);
1671 codec->ac97_registered = 0;
1672 }
1673}
1674#endif
1675
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1677{
1678 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001679 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001680 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001681
1682 /* find CODEC from registered CODECs*/
1683 list_for_each_entry(codec, &codec_list, list) {
1684 if (!strcmp(codec->name, aux_dev->codec_name)) {
1685 if (codec->probed) {
1686 dev_err(codec->dev,
1687 "asoc: codec already probed");
1688 ret = -EBUSY;
1689 goto out;
1690 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001691 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001692 }
1693 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001694 /* codec not found */
1695 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1696 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001697
Jarkko Nikula676ad982010-12-03 09:18:22 +02001698found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001699 ret = soc_probe_codec(card, codec);
1700 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001701 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702
Jarkko Nikula589c3562010-12-06 16:27:07 +02001703 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001704
1705out:
1706 return ret;
1707}
1708
1709static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1710{
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1712 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001713
1714 /* unregister the rtd device */
1715 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001716 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001717 device_unregister(&rtd->dev);
1718 rtd->dev_registered = 0;
1719 }
1720
Jarkko Nikula589c3562010-12-06 16:27:07 +02001721 if (codec && codec->probed)
1722 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001723}
1724
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001725static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1726 enum snd_soc_compress_type compress_type)
1727{
1728 int ret;
1729
1730 if (codec->cache_init)
1731 return 0;
1732
1733 /* override the compress_type if necessary */
1734 if (compress_type && codec->compress_type != compress_type)
1735 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001736 ret = snd_soc_cache_init(codec);
1737 if (ret < 0) {
1738 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1739 ret);
1740 return ret;
1741 }
1742 codec->cache_init = 1;
1743 return 0;
1744}
1745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746static void snd_soc_instantiate_card(struct snd_soc_card *card)
1747{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001748 struct snd_soc_codec *codec;
1749 struct snd_soc_codec_conf *codec_conf;
1750 enum snd_soc_compress_type compress_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 int ret, i;
1752
1753 mutex_lock(&card->mutex);
1754
1755 if (card->instantiated) {
1756 mutex_unlock(&card->mutex);
1757 return;
1758 }
1759
1760 /* bind DAIs */
1761 for (i = 0; i < card->num_links; i++)
1762 soc_bind_dai_link(card, i);
1763
1764 /* bind completed ? */
1765 if (card->num_rtd != card->num_links) {
1766 mutex_unlock(&card->mutex);
1767 return;
1768 }
1769
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001770 /* initialize the register cache for each available codec */
1771 list_for_each_entry(codec, &codec_list, list) {
1772 if (codec->cache_init)
1773 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001774 /* by default we don't override the compress_type */
1775 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001776 /* check to see if we need to override the compress_type */
1777 for (i = 0; i < card->num_configs; ++i) {
1778 codec_conf = &card->codec_conf[i];
1779 if (!strcmp(codec->name, codec_conf->dev_name)) {
1780 compress_type = codec_conf->compress_type;
1781 if (compress_type && compress_type
1782 != codec->compress_type)
1783 break;
1784 }
1785 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001786 ret = snd_soc_init_codec_cache(codec, compress_type);
1787 if (ret < 0) {
1788 mutex_unlock(&card->mutex);
1789 return;
1790 }
1791 }
1792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001793 /* card bind complete so register a sound card */
1794 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1795 card->owner, 0, &card->snd_card);
1796 if (ret < 0) {
1797 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1798 card->name);
1799 mutex_unlock(&card->mutex);
1800 return;
1801 }
1802 card->snd_card->dev = card->dev;
1803
Mark Brown88ee1c62011-02-02 10:43:26 +00001804#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001805 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001806 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001807#endif
Andy Green6ed25972008-06-13 16:24:05 +01001808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 /* initialise the sound card only once */
1810 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001811 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812 if (ret < 0)
1813 goto card_probe_error;
1814 }
1815
Mark Brownfe3e78e2009-11-03 22:13:13 +00001816 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817 ret = soc_probe_dai_link(card, i);
1818 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001819 pr_err("asoc: failed to instantiate card %s: %d\n",
1820 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001821 goto probe_dai_err;
1822 }
1823 }
1824
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001825 for (i = 0; i < card->num_aux_devs; i++) {
1826 ret = soc_probe_aux_dev(card, i);
1827 if (ret < 0) {
1828 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1829 card->name, ret);
1830 goto probe_aux_dev_err;
1831 }
1832 }
1833
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001834 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1835 "%s", card->name);
1836 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1837 "%s", card->name);
1838
1839 ret = snd_card_register(card->snd_card);
1840 if (ret < 0) {
1841 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001842 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843 }
1844
1845#ifdef CONFIG_SND_SOC_AC97_BUS
1846 /* register any AC97 codecs */
1847 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001848 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1849 if (ret < 0) {
1850 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1851 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001852 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001853 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001854 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001855 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001856#endif
1857
Mark Brown435c5e22008-12-04 15:32:53 +00001858 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001860 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001862probe_aux_dev_err:
1863 for (i = 0; i < card->num_aux_devs; i++)
1864 soc_remove_aux_dev(card, i);
1865
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001866probe_dai_err:
1867 for (i = 0; i < card->num_links; i++)
1868 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001869
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001871 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001872 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001873
1874 snd_card_free(card->snd_card);
1875
1876 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001877}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878
Mark Brown435c5e22008-12-04 15:32:53 +00001879/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001880 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001881 * client_mutex.
1882 */
1883static void snd_soc_instantiate_cards(void)
1884{
1885 struct snd_soc_card *card;
1886 list_for_each_entry(card, &card_list, list)
1887 snd_soc_instantiate_card(card);
1888}
1889
1890/* probes a new socdev */
1891static int soc_probe(struct platform_device *pdev)
1892{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001894 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001895
Vinod Koul70a7ca32011-01-14 19:22:48 +05301896 /*
1897 * no card, so machine driver should be registering card
1898 * we should not be here in that case so ret error
1899 */
1900 if (!card)
1901 return -EINVAL;
1902
Mark Brown435c5e22008-12-04 15:32:53 +00001903 /* Bodge while we unpick instantiation */
1904 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001905
Mark Brown435c5e22008-12-04 15:32:53 +00001906 ret = snd_soc_register_card(card);
1907 if (ret != 0) {
1908 dev_err(&pdev->dev, "Failed to register card\n");
1909 return ret;
1910 }
1911
1912 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001913}
1914
Vinod Koulb0e26482011-01-13 22:48:02 +05301915static int soc_cleanup_card_resources(struct snd_soc_card *card)
1916{
Vinod Koulb0e26482011-01-13 22:48:02 +05301917 int i;
1918
1919 /* make sure any delayed work runs */
1920 for (i = 0; i < card->num_rtd; i++) {
1921 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1922 flush_delayed_work_sync(&rtd->delayed_work);
1923 }
1924
1925 /* remove auxiliary devices */
1926 for (i = 0; i < card->num_aux_devs; i++)
1927 soc_remove_aux_dev(card, i);
1928
1929 /* remove and free each DAI */
1930 for (i = 0; i < card->num_rtd; i++)
1931 soc_remove_dai_link(card, i);
1932
1933 soc_cleanup_card_debugfs(card);
1934
1935 /* remove the card */
1936 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001937 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301938
1939 kfree(card->rtd);
1940 snd_card_free(card->snd_card);
1941 return 0;
1942
1943}
1944
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945/* removes a socdev */
1946static int soc_remove(struct platform_device *pdev)
1947{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
Mark Brownc5af3a22008-11-28 13:29:45 +00001950 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001951 return 0;
1952}
1953
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001954int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001955{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001956 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001957 int i;
Mark Brown51737472009-06-22 13:16:51 +01001958
1959 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001960 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001961
1962 /* Flush out pmdown_time work - we actually do want to run it
1963 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001964 for (i = 0; i < card->num_rtd; i++) {
1965 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001966 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001967 }
Mark Brown51737472009-06-22 13:16:51 +01001968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001970
1971 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001972}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001973EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001974
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001975const struct dev_pm_ops snd_soc_pm_ops = {
1976 .suspend = snd_soc_suspend,
1977 .resume = snd_soc_resume,
1978 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001979};
1980
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981/* ASoC platform driver */
1982static struct platform_driver soc_driver = {
1983 .driver = {
1984 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001985 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001986 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987 },
1988 .probe = soc_probe,
1989 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990};
1991
1992/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001993static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001994{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995 struct snd_soc_codec *codec = rtd->codec;
1996 struct snd_soc_platform *platform = rtd->platform;
1997 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1998 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999 struct snd_pcm *pcm;
2000 char new_name[64];
2001 int ret = 0, playback = 0, capture = 0;
2002
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00002004 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002005 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002006
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002008 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002009 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010 capture = 1;
2011
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2013 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2014 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002016 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002017 return ret;
2018 }
2019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002021 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002022 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2023 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2024 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2025 soc_pcm_ops.copy = platform->driver->ops->copy;
2026 soc_pcm_ops.silence = platform->driver->ops->silence;
2027 soc_pcm_ops.ack = platform->driver->ops->ack;
2028 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002029
2030 if (playback)
2031 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2032
2033 if (capture)
2034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2035
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002036 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037 if (ret < 0) {
2038 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039 return ret;
2040 }
2041
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002042 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2044 cpu_dai->name);
2045 return ret;
2046}
2047
Mark Brown096e49d2009-07-05 15:12:22 +01002048/**
2049 * snd_soc_codec_volatile_register: Report if a register is volatile.
2050 *
2051 * @codec: CODEC to query.
2052 * @reg: Register to query.
2053 *
2054 * Boolean function indiciating if a CODEC register is volatile.
2055 */
Mark Brown181e0552011-01-24 14:05:25 +00002056int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2057 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01002058{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002059 if (codec->volatile_register)
2060 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002061 else
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2065
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002066/**
2067 * snd_soc_new_ac97_codec - initailise AC97 device
2068 * @codec: audio codec
2069 * @ops: AC97 bus operations
2070 * @num: AC97 codec number
2071 *
2072 * Initialises AC97 codec resources for use by ad-hoc devices only.
2073 */
2074int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2075 struct snd_ac97_bus_ops *ops, int num)
2076{
2077 mutex_lock(&codec->mutex);
2078
2079 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2080 if (codec->ac97 == NULL) {
2081 mutex_unlock(&codec->mutex);
2082 return -ENOMEM;
2083 }
2084
2085 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2086 if (codec->ac97->bus == NULL) {
2087 kfree(codec->ac97);
2088 codec->ac97 = NULL;
2089 mutex_unlock(&codec->mutex);
2090 return -ENOMEM;
2091 }
2092
2093 codec->ac97->bus->ops = ops;
2094 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002095
2096 /*
2097 * Mark the AC97 device to be created by us. This way we ensure that the
2098 * device will be registered with the device subsystem later on.
2099 */
2100 codec->ac97_created = 1;
2101
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002102 mutex_unlock(&codec->mutex);
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2106
2107/**
2108 * snd_soc_free_ac97_codec - free AC97 codec device
2109 * @codec: audio codec
2110 *
2111 * Frees AC97 codec device resources.
2112 */
2113void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2114{
2115 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002116#ifdef CONFIG_SND_SOC_AC97_BUS
2117 soc_unregister_ac97_dai_link(codec);
2118#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119 kfree(codec->ac97->bus);
2120 kfree(codec->ac97);
2121 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002122 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002123 mutex_unlock(&codec->mutex);
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2126
Mark Brownc3753702010-11-01 15:41:57 -04002127unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2128{
2129 unsigned int ret;
2130
Mark Brownc3acec22010-12-02 16:15:29 +00002131 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002132 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002133 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002134
2135 return ret;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_read);
2138
2139unsigned int snd_soc_write(struct snd_soc_codec *codec,
2140 unsigned int reg, unsigned int val)
2141{
2142 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002143 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002144 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002145}
2146EXPORT_SYMBOL_GPL(snd_soc_write);
2147
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148/**
2149 * snd_soc_update_bits - update codec register bits
2150 * @codec: audio codec
2151 * @reg: codec register
2152 * @mask: register mask
2153 * @value: new value
2154 *
2155 * Writes new register value.
2156 *
Timur Tabi180c3292011-01-10 15:58:13 -06002157 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158 */
2159int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002160 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161{
2162 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002163 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002164 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165
Timur Tabi180c3292011-01-10 15:58:13 -06002166 ret = snd_soc_read(codec, reg);
2167 if (ret < 0)
2168 return ret;
2169
2170 old = ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002171 new = (old & ~mask) | value;
2172 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06002173 if (change) {
2174 ret = snd_soc_write(codec, reg, new);
2175 if (ret < 0)
2176 return ret;
2177 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179 return change;
2180}
2181EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2182
2183/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002184 * snd_soc_update_bits_locked - update codec register bits
2185 * @codec: audio codec
2186 * @reg: codec register
2187 * @mask: register mask
2188 * @value: new value
2189 *
2190 * Writes new register value, and takes the codec mutex.
2191 *
2192 * Returns 1 for change else 0.
2193 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002194int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2195 unsigned short reg, unsigned int mask,
2196 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002197{
2198 int change;
2199
2200 mutex_lock(&codec->mutex);
2201 change = snd_soc_update_bits(codec, reg, mask, value);
2202 mutex_unlock(&codec->mutex);
2203
2204 return change;
2205}
Mark Browndd1b3d52009-12-04 14:22:03 +00002206EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002207
2208/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 * snd_soc_test_bits - test register for change
2210 * @codec: audio codec
2211 * @reg: codec register
2212 * @mask: register mask
2213 * @value: new value
2214 *
2215 * Tests a register with a new value and checks if the new value is
2216 * different from the old value.
2217 *
2218 * Returns 1 for change else 0.
2219 */
2220int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002221 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222{
2223 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002224 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002225
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 old = snd_soc_read(codec, reg);
2227 new = (old & ~mask) | value;
2228 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002229
2230 return change;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2233
2234/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2236 * @substream: the pcm substream
2237 * @hw: the hardware parameters
2238 *
2239 * Sets the substream runtime hardware parameters.
2240 */
2241int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2242 const struct snd_pcm_hardware *hw)
2243{
2244 struct snd_pcm_runtime *runtime = substream->runtime;
2245 runtime->hw.info = hw->info;
2246 runtime->hw.formats = hw->formats;
2247 runtime->hw.period_bytes_min = hw->period_bytes_min;
2248 runtime->hw.period_bytes_max = hw->period_bytes_max;
2249 runtime->hw.periods_min = hw->periods_min;
2250 runtime->hw.periods_max = hw->periods_max;
2251 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2252 runtime->hw.fifo_size = hw->fifo_size;
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2256
2257/**
2258 * snd_soc_cnew - create new control
2259 * @_template: control template
2260 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002261 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262 *
2263 * Create a new mixer control from a template control.
2264 *
2265 * Returns 0 for success, else error.
2266 */
2267struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2268 void *data, char *long_name)
2269{
2270 struct snd_kcontrol_new template;
2271
2272 memcpy(&template, _template, sizeof(template));
2273 if (long_name)
2274 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275 template.index = 0;
2276
2277 return snd_ctl_new1(&template, data);
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_cnew);
2280
2281/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002282 * snd_soc_add_controls - add an array of controls to a codec.
2283 * Convienience function to add a list of controls. Many codecs were
2284 * duplicating this code.
2285 *
2286 * @codec: codec to add controls to
2287 * @controls: array of controls to add
2288 * @num_controls: number of elements in the array
2289 *
2290 * Return 0 for success, else error.
2291 */
2292int snd_soc_add_controls(struct snd_soc_codec *codec,
2293 const struct snd_kcontrol_new *controls, int num_controls)
2294{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002295 struct snd_card *card = codec->card->snd_card;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002296 char prefixed_name[44], *name;
Ian Molton3e8e1952009-01-09 00:23:21 +00002297 int err, i;
2298
2299 for (i = 0; i < num_controls; i++) {
2300 const struct snd_kcontrol_new *control = &controls[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002301 if (codec->name_prefix) {
2302 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2303 codec->name_prefix, control->name);
2304 name = prefixed_name;
2305 } else {
2306 name = control->name;
2307 }
2308 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
Ian Molton3e8e1952009-01-09 00:23:21 +00002309 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002310 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002311 codec->name, name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002312 return err;
2313 }
2314 }
2315
2316 return 0;
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2319
2320/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321 * snd_soc_info_enum_double - enumerated double mixer info callback
2322 * @kcontrol: mixer control
2323 * @uinfo: control element information
2324 *
2325 * Callback to provide information about a double enumerated
2326 * mixer control.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2332{
2333 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2334
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2336 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002337 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002339 if (uinfo->value.enumerated.item > e->max - 1)
2340 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341 strcpy(uinfo->value.enumerated.name,
2342 e->texts[uinfo->value.enumerated.item]);
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2346
2347/**
2348 * snd_soc_get_enum_double - enumerated double mixer get callback
2349 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002350 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351 *
2352 * Callback to get the value of a double enumerated mixer.
2353 *
2354 * Returns 0 for success.
2355 */
2356int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
2359 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002361 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002363 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002364 ;
2365 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002366 ucontrol->value.enumerated.item[0]
2367 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 if (e->shift_l != e->shift_r)
2369 ucontrol->value.enumerated.item[1] =
2370 (val >> e->shift_r) & (bitmask - 1);
2371
2372 return 0;
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2375
2376/**
2377 * snd_soc_put_enum_double - enumerated double mixer put callback
2378 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002379 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 *
2381 * Callback to set the value of a double enumerated mixer.
2382 *
2383 * Returns 0 for success.
2384 */
2385int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387{
2388 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002390 unsigned int val;
2391 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002393 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002395 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 return -EINVAL;
2397 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2398 mask = (bitmask - 1) << e->shift_l;
2399 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002400 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 return -EINVAL;
2402 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2403 mask |= (bitmask - 1) << e->shift_r;
2404 }
2405
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002406 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407}
2408EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2409
2410/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002411 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2412 * @kcontrol: mixer control
2413 * @ucontrol: control element information
2414 *
2415 * Callback to get the value of a double semi enumerated mixer.
2416 *
2417 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2418 * used for handling bitfield coded enumeration for example.
2419 *
2420 * Returns 0 for success.
2421 */
2422int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2424{
2425 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002426 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002427 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002428
2429 reg_val = snd_soc_read(codec, e->reg);
2430 val = (reg_val >> e->shift_l) & e->mask;
2431 for (mux = 0; mux < e->max; mux++) {
2432 if (val == e->values[mux])
2433 break;
2434 }
2435 ucontrol->value.enumerated.item[0] = mux;
2436 if (e->shift_l != e->shift_r) {
2437 val = (reg_val >> e->shift_r) & e->mask;
2438 for (mux = 0; mux < e->max; mux++) {
2439 if (val == e->values[mux])
2440 break;
2441 }
2442 ucontrol->value.enumerated.item[1] = mux;
2443 }
2444
2445 return 0;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2448
2449/**
2450 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2451 * @kcontrol: mixer control
2452 * @ucontrol: control element information
2453 *
2454 * Callback to set the value of a double semi enumerated mixer.
2455 *
2456 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2457 * used for handling bitfield coded enumeration for example.
2458 *
2459 * Returns 0 for success.
2460 */
2461int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463{
2464 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002466 unsigned int val;
2467 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002468
2469 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2470 return -EINVAL;
2471 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2472 mask = e->mask << e->shift_l;
2473 if (e->shift_l != e->shift_r) {
2474 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2475 return -EINVAL;
2476 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2477 mask |= e->mask << e->shift_r;
2478 }
2479
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002480 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002481}
2482EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2483
2484/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002485 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2486 * @kcontrol: mixer control
2487 * @uinfo: control element information
2488 *
2489 * Callback to provide information about an external enumerated
2490 * single mixer.
2491 *
2492 * Returns 0 for success.
2493 */
2494int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_info *uinfo)
2496{
2497 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2498
2499 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2500 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002501 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002503 if (uinfo->value.enumerated.item > e->max - 1)
2504 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 strcpy(uinfo->value.enumerated.name,
2506 e->texts[uinfo->value.enumerated.item]);
2507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2510
2511/**
2512 * snd_soc_info_volsw_ext - external single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2515 *
2516 * Callback to provide information about a single external mixer control.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_info *uinfo)
2522{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002523 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524
Mark Brownfd5dfad2009-04-15 21:37:46 +01002525 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002526 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2527 else
2528 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2529
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530 uinfo->count = 1;
2531 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002532 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002533 return 0;
2534}
2535EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2536
2537/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002538 * snd_soc_info_volsw - single mixer info callback
2539 * @kcontrol: mixer control
2540 * @uinfo: control element information
2541 *
2542 * Callback to provide information about a single mixer control.
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_info *uinfo)
2548{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002549 struct soc_mixer_control *mc =
2550 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002551 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002552 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002553 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002554
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002555 if (!mc->platform_max)
2556 mc->platform_max = mc->max;
2557 platform_max = mc->platform_max;
2558
2559 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002560 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2561 else
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2563
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564 uinfo->count = shift == rshift ? 1 : 2;
2565 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002566 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 return 0;
2568}
2569EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2570
2571/**
2572 * snd_soc_get_volsw - single mixer get callback
2573 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002574 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002575 *
2576 * Callback to get the value of a single mixer control.
2577 *
2578 * Returns 0 for success.
2579 */
2580int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002583 struct soc_mixer_control *mc =
2584 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002586 unsigned int reg = mc->reg;
2587 unsigned int shift = mc->shift;
2588 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002589 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002590 unsigned int mask = (1 << fls(max)) - 1;
2591 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592
2593 ucontrol->value.integer.value[0] =
2594 (snd_soc_read(codec, reg) >> shift) & mask;
2595 if (shift != rshift)
2596 ucontrol->value.integer.value[1] =
2597 (snd_soc_read(codec, reg) >> rshift) & mask;
2598 if (invert) {
2599 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002600 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002601 if (shift != rshift)
2602 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002603 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604 }
2605
2606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2609
2610/**
2611 * snd_soc_put_volsw - single mixer put callback
2612 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002613 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614 *
2615 * Callback to set the value of a single mixer control.
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002625 unsigned int reg = mc->reg;
2626 unsigned int shift = mc->shift;
2627 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002628 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002629 unsigned int mask = (1 << fls(max)) - 1;
2630 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002631 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632
2633 val = (ucontrol->value.integer.value[0] & mask);
2634 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002635 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636 val_mask = mask << shift;
2637 val = val << shift;
2638 if (shift != rshift) {
2639 val2 = (ucontrol->value.integer.value[1] & mask);
2640 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002641 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002642 val_mask |= mask << rshift;
2643 val |= val2 << rshift;
2644 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002645 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646}
2647EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2648
2649/**
2650 * snd_soc_info_volsw_2r - double mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2653 *
2654 * Callback to provide information about a double mixer control that
2655 * spans 2 codec registers.
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002664 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002666 if (!mc->platform_max)
2667 mc->platform_max = mc->max;
2668 platform_max = mc->platform_max;
2669
2670 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002671 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2672 else
2673 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2674
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 uinfo->count = 2;
2676 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002677 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678 return 0;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2681
2682/**
2683 * snd_soc_get_volsw_2r - double mixer get callback
2684 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002685 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686 *
2687 * Callback to get the value of a double mixer control that spans 2 registers.
2688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002697 unsigned int reg = mc->reg;
2698 unsigned int reg2 = mc->rreg;
2699 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002700 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002701 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002702 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703
2704 ucontrol->value.integer.value[0] =
2705 (snd_soc_read(codec, reg) >> shift) & mask;
2706 ucontrol->value.integer.value[1] =
2707 (snd_soc_read(codec, reg2) >> shift) & mask;
2708 if (invert) {
2709 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002710 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002712 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002713 }
2714
2715 return 0;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2718
2719/**
2720 * snd_soc_put_volsw_2r - double mixer set callback
2721 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002722 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002723 *
2724 * Callback to set the value of a double mixer control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002731 struct soc_mixer_control *mc =
2732 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002733 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002734 unsigned int reg = mc->reg;
2735 unsigned int reg2 = mc->rreg;
2736 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002737 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002738 unsigned int mask = (1 << fls(max)) - 1;
2739 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002740 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002741 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002742
2743 val_mask = mask << shift;
2744 val = (ucontrol->value.integer.value[0] & mask);
2745 val2 = (ucontrol->value.integer.value[1] & mask);
2746
2747 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002748 val = max - val;
2749 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002750 }
2751
2752 val = val << shift;
2753 val2 = val2 << shift;
2754
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002755 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002756 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002757 return err;
2758
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002759 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002760 return err;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2763
Mark Browne13ac2e2008-05-28 17:58:05 +01002764/**
2765 * snd_soc_info_volsw_s8 - signed mixer info callback
2766 * @kcontrol: mixer control
2767 * @uinfo: control element information
2768 *
2769 * Callback to provide information about a signed mixer control.
2770 *
2771 * Returns 0 for success.
2772 */
2773int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_info *uinfo)
2775{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002778 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002779 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002780
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002781 if (!mc->platform_max)
2782 mc->platform_max = mc->max;
2783 platform_max = mc->platform_max;
2784
Mark Browne13ac2e2008-05-28 17:58:05 +01002785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2786 uinfo->count = 2;
2787 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002788 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002789 return 0;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2792
2793/**
2794 * snd_soc_get_volsw_s8 - signed mixer get callback
2795 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002796 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002797 *
2798 * Callback to get the value of a signed mixer control.
2799 *
2800 * Returns 0 for success.
2801 */
2802int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002805 struct soc_mixer_control *mc =
2806 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002807 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002808 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002809 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002810 int val = snd_soc_read(codec, reg);
2811
2812 ucontrol->value.integer.value[0] =
2813 ((signed char)(val & 0xff))-min;
2814 ucontrol->value.integer.value[1] =
2815 ((signed char)((val >> 8) & 0xff))-min;
2816 return 0;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2819
2820/**
2821 * snd_soc_put_volsw_sgn - signed mixer put callback
2822 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002823 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002824 *
2825 * Callback to set the value of a signed mixer control.
2826 *
2827 * Returns 0 for success.
2828 */
2829int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002832 struct soc_mixer_control *mc =
2833 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002835 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002836 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002837 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002838
2839 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2840 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2841
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002842 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002843}
2844EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2845
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002846/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002847 * snd_soc_limit_volume - Set new limit to an existing volume control.
2848 *
2849 * @codec: where to look for the control
2850 * @name: Name of the control
2851 * @max: new maximum limit
2852 *
2853 * Return 0 for success, else error.
2854 */
2855int snd_soc_limit_volume(struct snd_soc_codec *codec,
2856 const char *name, int max)
2857{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002858 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002859 struct snd_kcontrol *kctl;
2860 struct soc_mixer_control *mc;
2861 int found = 0;
2862 int ret = -EINVAL;
2863
2864 /* Sanity check for name and max */
2865 if (unlikely(!name || max <= 0))
2866 return -EINVAL;
2867
2868 list_for_each_entry(kctl, &card->controls, list) {
2869 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2870 found = 1;
2871 break;
2872 }
2873 }
2874 if (found) {
2875 mc = (struct soc_mixer_control *)kctl->private_value;
2876 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002877 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002878 ret = 0;
2879 }
2880 }
2881 return ret;
2882}
2883EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2884
2885/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002886 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2887 * mixer info callback
2888 * @kcontrol: mixer control
2889 * @uinfo: control element information
2890 *
2891 * Returns 0 for success.
2892 */
2893int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_info *uinfo)
2895{
2896 struct soc_mixer_control *mc =
2897 (struct soc_mixer_control *)kcontrol->private_value;
2898 int max = mc->max;
2899 int min = mc->min;
2900
2901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2902 uinfo->count = 2;
2903 uinfo->value.integer.min = 0;
2904 uinfo->value.integer.max = max-min;
2905
2906 return 0;
2907}
2908EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2909
2910/**
2911 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2912 * mixer get callback
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2923 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2924 unsigned int mask = (1<<mc->shift)-1;
2925 int min = mc->min;
2926 int val = snd_soc_read(codec, mc->reg) & mask;
2927 int valr = snd_soc_read(codec, mc->rreg) & mask;
2928
Stuart Longland20630c72010-06-18 12:56:10 +10002929 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2930 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002931 return 0;
2932}
2933EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2934
2935/**
2936 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2937 * mixer put callback
2938 * @kcontrol: mixer control
2939 * @uinfo: control element information
2940 *
2941 * Returns 0 for success.
2942 */
2943int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct soc_mixer_control *mc =
2947 (struct soc_mixer_control *)kcontrol->private_value;
2948 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2949 unsigned int mask = (1<<mc->shift)-1;
2950 int min = mc->min;
2951 int ret;
2952 unsigned int val, valr, oval, ovalr;
2953
2954 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2955 val &= mask;
2956 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2957 valr &= mask;
2958
2959 oval = snd_soc_read(codec, mc->reg) & mask;
2960 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2961
2962 ret = 0;
2963 if (oval != val) {
2964 ret = snd_soc_write(codec, mc->reg, val);
2965 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002966 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002967 }
2968 if (ovalr != valr) {
2969 ret = snd_soc_write(codec, mc->rreg, valr);
2970 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002971 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002972 }
2973
2974 return 0;
2975}
2976EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2977
2978/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002979 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2980 * @dai: DAI
2981 * @clk_id: DAI specific clock ID
2982 * @freq: new clock frequency in Hz
2983 * @dir: new clock direction - input/output.
2984 *
2985 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2986 */
2987int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2988 unsigned int freq, int dir)
2989{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990 if (dai->driver && dai->driver->ops->set_sysclk)
2991 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002992 else
2993 return -EINVAL;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2996
2997/**
2998 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2999 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003000 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003001 * @div: new clock divisor.
3002 *
3003 * Configures the clock dividers. This is used to derive the best DAI bit and
3004 * frame clocks from the system or master clock. It's best to set the DAI bit
3005 * and frame clocks as low as possible to save system power.
3006 */
3007int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3008 int div_id, int div)
3009{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003010 if (dai->driver && dai->driver->ops->set_clkdiv)
3011 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003012 else
3013 return -EINVAL;
3014}
3015EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3016
3017/**
3018 * snd_soc_dai_set_pll - configure DAI PLL.
3019 * @dai: DAI
3020 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003021 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003022 * @freq_in: PLL input clock frequency in Hz
3023 * @freq_out: requested PLL output clock frequency in Hz
3024 *
3025 * Configures and enables PLL to generate output clock based on input clock.
3026 */
Mark Brown85488032009-09-05 18:52:16 +01003027int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3028 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003029{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003030 if (dai->driver && dai->driver->ops->set_pll)
3031 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003032 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003033 else
3034 return -EINVAL;
3035}
3036EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3037
3038/**
3039 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3040 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003041 * @fmt: SND_SOC_DAIFMT_ format value.
3042 *
3043 * Configures the DAI hardware format and clocking.
3044 */
3045int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3046{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003047 if (dai->driver && dai->driver->ops->set_fmt)
3048 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003049 else
3050 return -EINVAL;
3051}
3052EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3053
3054/**
3055 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3056 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003057 * @tx_mask: bitmask representing active TX slots.
3058 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003059 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003060 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003061 *
3062 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3063 * specific.
3064 */
3065int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003066 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003067{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003068 if (dai->driver && dai->driver->ops->set_tdm_slot)
3069 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003070 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003071 else
3072 return -EINVAL;
3073}
3074EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3075
3076/**
Barry Song472df3c2009-09-12 01:16:29 +08003077 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3078 * @dai: DAI
3079 * @tx_num: how many TX channels
3080 * @tx_slot: pointer to an array which imply the TX slot number channel
3081 * 0~num-1 uses
3082 * @rx_num: how many RX channels
3083 * @rx_slot: pointer to an array which imply the RX slot number channel
3084 * 0~num-1 uses
3085 *
3086 * configure the relationship between channel number and TDM slot number.
3087 */
3088int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3089 unsigned int tx_num, unsigned int *tx_slot,
3090 unsigned int rx_num, unsigned int *rx_slot)
3091{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003092 if (dai->driver && dai->driver->ops->set_channel_map)
3093 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003094 rx_num, rx_slot);
3095 else
3096 return -EINVAL;
3097}
3098EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3099
3100/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003101 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3102 * @dai: DAI
3103 * @tristate: tristate enable
3104 *
3105 * Tristates the DAI so that others can use it.
3106 */
3107int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3108{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003109 if (dai->driver && dai->driver->ops->set_tristate)
3110 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003111 else
3112 return -EINVAL;
3113}
3114EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3115
3116/**
3117 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3118 * @dai: DAI
3119 * @mute: mute enable
3120 *
3121 * Mutes the DAI DAC.
3122 */
3123int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3124{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 if (dai->driver && dai->driver->ops->digital_mute)
3126 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003127 else
3128 return -EINVAL;
3129}
3130EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3131
Mark Brownc5af3a22008-11-28 13:29:45 +00003132/**
3133 * snd_soc_register_card - Register a card with the ASoC core
3134 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003135 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003136 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003137 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303138int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003139{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003140 int i;
3141
Mark Brownc5af3a22008-11-28 13:29:45 +00003142 if (!card->name || !card->dev)
3143 return -EINVAL;
3144
Stephen Warren111c6412011-01-28 14:26:35 -07003145 snd_soc_initialize_card_lists(card);
3146
Vinod Koul150dd2f2011-01-13 22:48:32 +05303147 soc_init_card_debugfs(card);
3148
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003149 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3150 (card->num_links + card->num_aux_devs),
3151 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003152 if (card->rtd == NULL)
3153 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003154 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155
3156 for (i = 0; i < card->num_links; i++)
3157 card->rtd[i].dai_link = &card->dai_link[i];
3158
Mark Brownc5af3a22008-11-28 13:29:45 +00003159 INIT_LIST_HEAD(&card->list);
3160 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003161 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003162
3163 mutex_lock(&client_mutex);
3164 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003165 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003166 mutex_unlock(&client_mutex);
3167
3168 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3169
3170 return 0;
3171}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303172EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003173
3174/**
3175 * snd_soc_unregister_card - Unregister a card with the ASoC core
3176 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003177 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003178 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003179 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303180int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003181{
Vinod Koulb0e26482011-01-13 22:48:02 +05303182 if (card->instantiated)
3183 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003184 mutex_lock(&client_mutex);
3185 list_del(&card->list);
3186 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003187 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3188
3189 return 0;
3190}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303191EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003192
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003193/*
3194 * Simplify DAI link configuration by removing ".-1" from device names
3195 * and sanitizing names.
3196 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003197static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003198{
3199 char *found, name[NAME_SIZE];
3200 int id1, id2;
3201
3202 if (dev_name(dev) == NULL)
3203 return NULL;
3204
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003205 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003206
3207 /* are we a "%s.%d" name (platform and SPI components) */
3208 found = strstr(name, dev->driver->name);
3209 if (found) {
3210 /* get ID */
3211 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3212
3213 /* discard ID from name if ID == -1 */
3214 if (*id == -1)
3215 found[strlen(dev->driver->name)] = '\0';
3216 }
3217
3218 } else {
3219 /* I2C component devices are named "bus-addr" */
3220 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3221 char tmp[NAME_SIZE];
3222
3223 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003224 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225
3226 /* sanitize component name for DAI link creation */
3227 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003228 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003229 } else
3230 *id = 0;
3231 }
3232
3233 return kstrdup(name, GFP_KERNEL);
3234}
3235
3236/*
3237 * Simplify DAI link naming for single devices with multiple DAIs by removing
3238 * any ".-1" and using the DAI name (instead of device name).
3239 */
3240static inline char *fmt_multiple_name(struct device *dev,
3241 struct snd_soc_dai_driver *dai_drv)
3242{
3243 if (dai_drv->name == NULL) {
3244 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3245 dev_name(dev));
3246 return NULL;
3247 }
3248
3249 return kstrdup(dai_drv->name, GFP_KERNEL);
3250}
3251
Mark Brown91151712008-11-30 23:31:24 +00003252/**
3253 * snd_soc_register_dai - Register a DAI with the ASoC core
3254 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003255 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003256 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003257int snd_soc_register_dai(struct device *dev,
3258 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003259{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003261
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003262 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003263
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003264 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3265 if (dai == NULL)
3266 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003267
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003268 /* create DAI component name */
3269 dai->name = fmt_single_name(dev, &dai->id);
3270 if (dai->name == NULL) {
3271 kfree(dai);
3272 return -ENOMEM;
3273 }
3274
3275 dai->dev = dev;
3276 dai->driver = dai_drv;
3277 if (!dai->driver->ops)
3278 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003279
3280 mutex_lock(&client_mutex);
3281 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003282 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003283 mutex_unlock(&client_mutex);
3284
3285 pr_debug("Registered DAI '%s'\n", dai->name);
3286
3287 return 0;
3288}
3289EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3290
3291/**
3292 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3293 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003294 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003295 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003296void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003297{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298 struct snd_soc_dai *dai;
3299
3300 list_for_each_entry(dai, &dai_list, list) {
3301 if (dev == dai->dev)
3302 goto found;
3303 }
3304 return;
3305
3306found:
Mark Brown91151712008-11-30 23:31:24 +00003307 mutex_lock(&client_mutex);
3308 list_del(&dai->list);
3309 mutex_unlock(&client_mutex);
3310
3311 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003312 kfree(dai->name);
3313 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003314}
3315EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3316
3317/**
3318 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3319 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003320 * @dai: Array of DAIs to register
3321 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003322 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003323int snd_soc_register_dais(struct device *dev,
3324 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003325{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326 struct snd_soc_dai *dai;
3327 int i, ret = 0;
3328
Liam Girdwood720ffa42010-08-18 00:30:30 +01003329 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003330
3331 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003332
3333 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003334 if (dai == NULL) {
3335 ret = -ENOMEM;
3336 goto err;
3337 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003338
3339 /* create DAI component name */
3340 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3341 if (dai->name == NULL) {
3342 kfree(dai);
3343 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003344 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003345 }
3346
3347 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003348 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003349 if (dai->driver->id)
3350 dai->id = dai->driver->id;
3351 else
3352 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003353 if (!dai->driver->ops)
3354 dai->driver->ops = &null_dai_ops;
3355
3356 mutex_lock(&client_mutex);
3357 list_add(&dai->list, &dai_list);
3358 mutex_unlock(&client_mutex);
3359
3360 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003361 }
3362
Axel Lin1dcb4f32010-12-06 16:48:03 +08003363 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003364 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003365 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003366 return 0;
3367
3368err:
3369 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003370 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003371
3372 return ret;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3375
3376/**
3377 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3378 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003379 * @dai: Array of DAIs to unregister
3380 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003381 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003382void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003383{
3384 int i;
3385
3386 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003387 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003388}
3389EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3390
Mark Brown12a48a8c2008-12-03 19:40:30 +00003391/**
3392 * snd_soc_register_platform - Register a platform with the ASoC core
3393 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003394 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003395 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003396int snd_soc_register_platform(struct device *dev,
3397 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003398{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003399 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003400
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003401 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3402
3403 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3404 if (platform == NULL)
3405 return -ENOMEM;
3406
3407 /* create platform component name */
3408 platform->name = fmt_single_name(dev, &platform->id);
3409 if (platform->name == NULL) {
3410 kfree(platform);
3411 return -ENOMEM;
3412 }
3413
3414 platform->dev = dev;
3415 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003416
3417 mutex_lock(&client_mutex);
3418 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003419 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003420 mutex_unlock(&client_mutex);
3421
3422 pr_debug("Registered platform '%s'\n", platform->name);
3423
3424 return 0;
3425}
3426EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3427
3428/**
3429 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3430 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003431 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003432 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003433void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003434{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003435 struct snd_soc_platform *platform;
3436
3437 list_for_each_entry(platform, &platform_list, list) {
3438 if (dev == platform->dev)
3439 goto found;
3440 }
3441 return;
3442
3443found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003444 mutex_lock(&client_mutex);
3445 list_del(&platform->list);
3446 mutex_unlock(&client_mutex);
3447
3448 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003449 kfree(platform->name);
3450 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003451}
3452EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3453
Mark Brown151ab222009-05-09 16:22:58 +01003454static u64 codec_format_map[] = {
3455 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3456 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3457 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3458 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3459 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3460 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3461 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3462 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3463 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3464 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3465 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3466 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3467 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3468 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3469 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3470 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3471};
3472
3473/* Fix up the DAI formats for endianness: codecs don't actually see
3474 * the endianness of the data but we're using the CPU format
3475 * definitions which do need to include endianness so we ensure that
3476 * codec DAIs always have both big and little endian variants set.
3477 */
3478static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3479{
3480 int i;
3481
3482 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3483 if (stream->formats & codec_format_map[i])
3484 stream->formats |= codec_format_map[i];
3485}
3486
Mark Brown0d0cf002008-12-10 14:32:45 +00003487/**
3488 * snd_soc_register_codec - Register a codec with the ASoC core
3489 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003490 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003491 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003492int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003493 const struct snd_soc_codec_driver *codec_drv,
3494 struct snd_soc_dai_driver *dai_drv,
3495 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003496{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003497 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003498 struct snd_soc_codec *codec;
3499 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003501 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003503 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3504 if (codec == NULL)
3505 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003506
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 /* create CODEC component name */
3508 codec->name = fmt_single_name(dev, &codec->id);
3509 if (codec->name == NULL) {
3510 kfree(codec);
3511 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003512 }
3513
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003514 if (codec_drv->compress_type)
3515 codec->compress_type = codec_drv->compress_type;
3516 else
3517 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3518
Mark Brownc3acec22010-12-02 16:15:29 +00003519 codec->write = codec_drv->write;
3520 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003521 codec->volatile_register = codec_drv->volatile_register;
3522 codec->readable_register = codec_drv->readable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003523 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3524 codec->dapm.dev = dev;
3525 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003526 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003527 codec->dev = dev;
3528 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003529 codec->num_dai = num_dai;
3530 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003531
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003532 /* allocate CODEC register cache */
3533 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003534 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003535 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003536 /* it is necessary to make a copy of the default register cache
3537 * because in the case of using a compression type that requires
3538 * the default register cache to be marked as __devinitconst the
3539 * kernel might have freed the array by the time we initialize
3540 * the cache.
3541 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003542 if (codec_drv->reg_cache_default) {
3543 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3544 reg_size, GFP_KERNEL);
3545 if (!codec->reg_def_copy) {
3546 ret = -ENOMEM;
3547 goto fail;
3548 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003549 }
3550 }
3551
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003552 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3553 if (!codec->volatile_register)
3554 codec->volatile_register = snd_soc_default_volatile_register;
3555 if (!codec->readable_register)
3556 codec->readable_register = snd_soc_default_readable_register;
3557 }
3558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003559 for (i = 0; i < num_dai; i++) {
3560 fixup_codec_formats(&dai_drv[i].playback);
3561 fixup_codec_formats(&dai_drv[i].capture);
3562 }
3563
Mark Brown26b01cc2010-08-18 20:20:55 +01003564 /* register any DAIs */
3565 if (num_dai) {
3566 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3567 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003568 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003569 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570
Mark Brown0d0cf002008-12-10 14:32:45 +00003571 mutex_lock(&client_mutex);
3572 list_add(&codec->list, &codec_list);
3573 snd_soc_instantiate_cards();
3574 mutex_unlock(&client_mutex);
3575
3576 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003577 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003579fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003580 kfree(codec->reg_def_copy);
3581 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003582 kfree(codec->name);
3583 kfree(codec);
3584 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003585}
3586EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3587
3588/**
3589 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3590 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003591 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003592 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003593void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003594{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003595 struct snd_soc_codec *codec;
3596 int i;
3597
3598 list_for_each_entry(codec, &codec_list, list) {
3599 if (dev == codec->dev)
3600 goto found;
3601 }
3602 return;
3603
3604found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003605 if (codec->num_dai)
3606 for (i = 0; i < codec->num_dai; i++)
3607 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608
Mark Brown0d0cf002008-12-10 14:32:45 +00003609 mutex_lock(&client_mutex);
3610 list_del(&codec->list);
3611 mutex_unlock(&client_mutex);
3612
3613 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003614
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003615 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003616 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003617 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003618 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003619}
3620EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3621
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003622static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003623{
Mark Brown384c89e2008-12-03 17:34:03 +00003624#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003625 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3626 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003627 printk(KERN_WARNING
3628 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003629 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003630 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003631
Mark Brown8a9dab12011-01-10 22:25:21 +00003632 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003633 &codec_list_fops))
3634 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3635
Mark Brown8a9dab12011-01-10 22:25:21 +00003636 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003637 &dai_list_fops))
3638 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003639
Mark Brown8a9dab12011-01-10 22:25:21 +00003640 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003641 &platform_list_fops))
3642 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003643#endif
3644
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003645 return platform_driver_register(&soc_driver);
3646}
Mark Brown4abe8e12010-10-12 17:41:03 +01003647module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003648
Mark Brown7d8c16a2008-11-30 22:11:24 +00003649static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003650{
Mark Brown384c89e2008-12-03 17:34:03 +00003651#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003652 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003653#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003654 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003655}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003656module_exit(snd_soc_exit);
3657
3658/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003659MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003660MODULE_DESCRIPTION("ALSA SoC Core");
3661MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003662MODULE_ALIAS("platform:soc-audio");