blob: 32d7d2f8147cf19c8ffa1c57fc2810e927bcbf5d [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 DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
48
Mark Brown384c89e2008-12-03 17:34:03 +000049#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000050struct dentry *snd_soc_debugfs_root;
51EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000052#endif
53
Mark Brownc5af3a22008-11-28 13:29:45 +000054static DEFINE_MUTEX(client_mutex);
55static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000056static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000057static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000058static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000059
Liam Girdwoodddee6272011-06-09 14:45:53 +010060int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
108 ret = snd_soc_read(codec , reg);
109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +0000146 if (codec->readable_register && !codec->readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172 struct snd_soc_pcm_runtime *rtd =
173 container_of(dev, struct snd_soc_pcm_runtime, dev);
174
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000175 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176}
177
178static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
179
Mark Browndbe21402010-02-12 11:37:24 +0000180static ssize_t pmdown_time_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 struct snd_soc_pcm_runtime *rtd =
184 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000186 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000187}
188
189static ssize_t pmdown_time_set(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
192{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000193 struct snd_soc_pcm_runtime *rtd =
194 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700195 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000196
Mark Brownc593b522010-10-27 20:11:17 -0700197 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
198 if (ret)
199 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000200
201 return count;
202}
203
204static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
205
Mark Brown2624d5f2009-11-03 21:56:13 +0000206#ifdef CONFIG_DEBUG_FS
207static int codec_reg_open_file(struct inode *inode, struct file *file)
208{
209 file->private_data = inode->i_private;
210 return 0;
211}
212
213static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000214 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000215{
216 ssize_t ret;
217 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000218 char *buf;
219
220 if (*ppos < 0 || !count)
221 return -EINVAL;
222
223 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000224 if (!buf)
225 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000226
227 ret = soc_codec_reg_show(codec, buf, count, *ppos);
228 if (ret >= 0) {
229 if (copy_to_user(user_buf, buf, ret)) {
230 kfree(buf);
231 return -EFAULT;
232 }
233 *ppos += ret;
234 }
235
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 kfree(buf);
237 return ret;
238}
239
240static ssize_t codec_reg_write_file(struct file *file,
241 const char __user *user_buf, size_t count, loff_t *ppos)
242{
243 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700244 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 char *start = buf;
246 unsigned long reg, value;
247 int step = 1;
248 struct snd_soc_codec *codec = file->private_data;
249
250 buf_size = min(count, (sizeof(buf)-1));
251 if (copy_from_user(buf, user_buf, buf_size))
252 return -EFAULT;
253 buf[buf_size] = 0;
254
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000255 if (codec->driver->reg_cache_step)
256 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000257
258 while (*start == ' ')
259 start++;
260 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 while (*start == ' ')
262 start++;
263 if (strict_strtoul(start, 16, &value))
264 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000265
266 /* Userspace has been fiddling around behind the kernel's back */
267 add_taint(TAINT_USER);
268
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000269 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000270 return buf_size;
271}
272
273static const struct file_operations codec_reg_fops = {
274 .open = codec_reg_open_file,
275 .read = codec_reg_read_file,
276 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200277 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000278};
279
280static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
281{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200282 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283
284 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000286 if (!codec->debugfs_codec_root) {
287 printk(KERN_WARNING
288 "ASoC: Failed to create codec debugfs directory\n");
289 return;
290 }
291
Mark Brownaaee8ef2011-01-26 20:53:50 +0000292 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293 &codec->cache_sync);
294 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
295 &codec->cache_only);
296
Mark Brown2624d5f2009-11-03 21:56:13 +0000297 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
298 codec->debugfs_codec_root,
299 codec, &codec_reg_fops);
300 if (!codec->debugfs_reg)
301 printk(KERN_WARNING
302 "ASoC: Failed to create codec register debugfs file\n");
303
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200304 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000305}
306
307static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
308{
309 debugfs_remove_recursive(codec->debugfs_codec_root);
310}
311
Mark Brownc3c5a192010-09-15 18:15:14 +0100312static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
313 size_t count, loff_t *ppos)
314{
315 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100316 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100317 struct snd_soc_codec *codec;
318
319 if (!buf)
320 return -ENOMEM;
321
Mark Brown2b194f9d2010-10-13 10:52:16 +0100322 list_for_each_entry(codec, &codec_list, list) {
323 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
324 codec->name);
325 if (len >= 0)
326 ret += len;
327 if (ret > PAGE_SIZE) {
328 ret = PAGE_SIZE;
329 break;
330 }
331 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100332
333 if (ret >= 0)
334 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
335
336 kfree(buf);
337
338 return ret;
339}
340
341static const struct file_operations codec_list_fops = {
342 .read = codec_list_read_file,
343 .llseek = default_llseek,/* read accesses f_pos */
344};
345
Mark Brownf3208782010-09-15 18:19:07 +0100346static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
347 size_t count, loff_t *ppos)
348{
349 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100350 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100351 struct snd_soc_dai *dai;
352
353 if (!buf)
354 return -ENOMEM;
355
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 list_for_each_entry(dai, &dai_list, list) {
357 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
358 if (len >= 0)
359 ret += len;
360 if (ret > PAGE_SIZE) {
361 ret = PAGE_SIZE;
362 break;
363 }
364 }
Mark Brownf3208782010-09-15 18:19:07 +0100365
Mark Brown2b194f9d2010-10-13 10:52:16 +0100366 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100367
368 kfree(buf);
369
370 return ret;
371}
372
373static const struct file_operations dai_list_fops = {
374 .read = dai_list_read_file,
375 .llseek = default_llseek,/* read accesses f_pos */
376};
377
Mark Brown19c7ac22010-09-15 18:22:40 +0100378static ssize_t platform_list_read_file(struct file *file,
379 char __user *user_buf,
380 size_t count, loff_t *ppos)
381{
382 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100384 struct snd_soc_platform *platform;
385
386 if (!buf)
387 return -ENOMEM;
388
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 list_for_each_entry(platform, &platform_list, list) {
390 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
391 platform->name);
392 if (len >= 0)
393 ret += len;
394 if (ret > PAGE_SIZE) {
395 ret = PAGE_SIZE;
396 break;
397 }
398 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100399
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100401
402 kfree(buf);
403
404 return ret;
405}
406
407static const struct file_operations platform_list_fops = {
408 .read = platform_list_read_file,
409 .llseek = default_llseek,/* read accesses f_pos */
410};
411
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200412static void soc_init_card_debugfs(struct snd_soc_card *card)
413{
414 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000415 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200416 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200417 dev_warn(card->dev,
418 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200419 return;
420 }
421
422 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
423 card->debugfs_card_root,
424 &card->pop_time);
425 if (!card->debugfs_pop_time)
426 dev_warn(card->dev,
427 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200428}
429
430static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
431{
432 debugfs_remove_recursive(card->debugfs_card_root);
433}
434
Mark Brown2624d5f2009-11-03 21:56:13 +0000435#else
436
437static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
438{
439}
440
441static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
442{
443}
Axel Linb95fccb2010-11-09 17:06:44 +0800444
445static inline void soc_init_card_debugfs(struct snd_soc_card *card)
446{
447}
448
449static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
450{
451}
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#endif
453
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454#ifdef CONFIG_SND_SOC_AC97_BUS
455/* unregister ac97 codec */
456static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
457{
458 if (codec->ac97->dev.bus)
459 device_unregister(&codec->ac97->dev);
460 return 0;
461}
462
463/* stop no dev release warning */
464static void soc_ac97_device_release(struct device *dev){}
465
466/* register ac97 codec to bus */
467static int soc_ac97_dev_register(struct snd_soc_codec *codec)
468{
469 int err;
470
471 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100472 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 codec->ac97->dev.release = soc_ac97_device_release;
474
Kay Sieversbb072bf2008-11-02 03:50:35 +0100475 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 err = device_register(&codec->ac97->dev);
478 if (err < 0) {
479 snd_printk(KERN_ERR "Can't register ac97 bus\n");
480 codec->ac97->dev.bus = NULL;
481 return err;
482 }
483 return 0;
484}
485#endif
486
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000487#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000489int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000491 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200492 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 int i;
494
Daniel Macke3509ff2009-06-03 17:44:49 +0200495 /* If the initialization of this soc device failed, there is no codec
496 * associated with it. Just bail out in this case.
497 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200499 return 0;
500
Andy Green6ed25972008-06-13 16:24:05 +0100501 /* Due to the resume being scheduled into a workqueue we could
502 * suspend before that's finished - wait for it to complete.
503 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 snd_power_lock(card->snd_card);
505 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
506 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100507
508 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100510
Mark Browna00f90f2010-12-02 16:24:24 +0000511 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 for (i = 0; i < card->num_rtd; i++) {
513 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
514 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100515
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100517 continue;
518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 if (drv->ops->digital_mute && dai->playback_active)
520 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 }
522
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100523 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 for (i = 0; i < card->num_rtd; i++) {
525 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100526 continue;
527
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100529 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100530
Mark Brown87506542008-11-18 20:50:34 +0000531 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000532 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 for (i = 0; i < card->num_rtd; i++) {
535 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
536 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100539 continue;
540
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
542 cpu_dai->driver->suspend(cpu_dai);
543 if (platform->driver->suspend && !platform->suspended) {
544 platform->driver->suspend(cpu_dai);
545 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100546 }
547 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 /* close any waiting streams and save state */
550 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100551 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200552 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100554
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 for (i = 0; i < card->num_rtd; i++) {
556 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
557
558 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100559 continue;
560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 if (driver->playback.stream_name != NULL)
562 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
563 SND_SOC_DAPM_STREAM_SUSPEND);
564
565 if (driver->capture.stream_name != NULL)
566 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
567 SND_SOC_DAPM_STREAM_SUSPEND);
568 }
569
570 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200571 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 /* If there are paths active then the CODEC will be held with
573 * bias _ON and should not be suspended. */
574 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200575 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 case SND_SOC_BIAS_STANDBY:
577 case SND_SOC_BIAS_OFF:
578 codec->driver->suspend(codec, PMSG_SUSPEND);
579 codec->suspended = 1;
580 break;
581 default:
582 dev_dbg(codec->dev, "CODEC is on over suspend\n");
583 break;
584 }
585 }
586 }
587
588 for (i = 0; i < card->num_rtd; i++) {
589 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
590
591 if (card->rtd[i].dai_link->ignore_suspend)
592 continue;
593
594 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
595 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596 }
597
Mark Brown87506542008-11-18 20:50:34 +0000598 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000599 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200600
601 return 0;
602}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000603EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
Andy Green6ed25972008-06-13 16:24:05 +0100605/* deferred resume work, so resume can complete before we finished
606 * setting our codec back up, which can be very slow on I2C
607 */
608static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 struct snd_soc_card *card =
611 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200612 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200613 int i;
614
Andy Green6ed25972008-06-13 16:24:05 +0100615 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
616 * so userspace apps are blocked from touching us
617 */
618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100620
Mark Brown99497882010-05-07 20:24:05 +0100621 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100623
Mark Brown87506542008-11-18 20:50:34 +0000624 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000625 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 /* resume AC97 DAIs */
628 for (i = 0; i < card->num_rtd; i++) {
629 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100632 continue;
633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
635 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 }
637
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200638 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 /* If the CODEC was idle over suspend then it will have been
640 * left with bias OFF or STANDBY and suspended so we must now
641 * resume. Otherwise the suspend was suppressed.
642 */
643 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200644 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 case SND_SOC_BIAS_STANDBY:
646 case SND_SOC_BIAS_OFF:
647 codec->driver->resume(codec);
648 codec->suspended = 0;
649 break;
650 default:
651 dev_dbg(codec->dev, "CODEC was on over suspend\n");
652 break;
653 }
Mark Brown1547aba2010-05-07 21:11:40 +0100654 }
655 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 for (i = 0; i < card->num_rtd; i++) {
658 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100661 continue;
662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 if (driver->playback.stream_name != NULL)
664 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666
667 if (driver->capture.stream_name != NULL)
668 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 SND_SOC_DAPM_STREAM_RESUME);
670 }
671
Mark Brown3ff3f642008-05-19 12:32:25 +0200672 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 for (i = 0; i < card->num_rtd; i++) {
674 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
675 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100676
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100678 continue;
679
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 if (drv->ops->digital_mute && dai->playback_active)
681 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 }
683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 for (i = 0; i < card->num_rtd; i++) {
685 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
686 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100687
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100689 continue;
690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
692 cpu_dai->driver->resume(cpu_dai);
693 if (platform->driver->resume && platform->suspended) {
694 platform->driver->resume(cpu_dai);
695 platform->suspended = 0;
696 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697 }
698
Mark Brown87506542008-11-18 20:50:34 +0000699 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000700 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100703
704 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100706}
707
708/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000709int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100710{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000711 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600712 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200713
Mark Brown64ab9ba2009-03-31 11:27:03 +0100714 /* AC97 devices might have other drivers hanging off them so
715 * need to resume immediately. Other drivers don't have that
716 * problem and may take a substantial amount of time to resume
717 * due to I/O costs and anti-pop so handle them out of line.
718 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 for (i = 0; i < card->num_rtd; i++) {
720 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600721 ac97_control |= cpu_dai->driver->ac97_control;
722 }
723 if (ac97_control) {
724 dev_dbg(dev, "Resuming AC97 immediately\n");
725 soc_resume_deferred(&card->deferred_resume_work);
726 } else {
727 dev_dbg(dev, "Scheduling resume work\n");
728 if (!schedule_work(&card->deferred_resume_work))
729 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100730 }
Andy Green6ed25972008-06-13 16:24:05 +0100731
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 return 0;
733}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000736#define snd_soc_suspend NULL
737#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738#endif
739
Barry Song02a06d32009-10-16 18:13:38 +0800740static struct snd_soc_dai_ops null_dai_ops = {
741};
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
746 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000747 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000748 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100750 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 if (rtd->complete)
753 return 1;
754 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 /* do we already have the CPU DAI for this link ? */
757 if (rtd->cpu_dai) {
758 goto find_codec;
759 }
760 /* no, then find CPU DAI from registered DAIs*/
761 list_for_each_entry(cpu_dai, &dai_list, list) {
762 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 rtd->cpu_dai = cpu_dai;
764 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000765 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766 }
767 dev_dbg(card->dev, "CPU DAI %s not registered\n",
768 dai_link->cpu_dai_name);
769
770find_codec:
771 /* do we already have the CODEC for this link ? */
772 if (rtd->codec) {
773 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000774 }
775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 /* no, then find CODEC from registered CODECs*/
777 list_for_each_entry(codec, &codec_list, list) {
778 if (!strcmp(codec->name, dai_link->codec_name)) {
779 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +0000780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
782 list_for_each_entry(codec_dai, &dai_list, list) {
783 if (codec->dev == codec_dai->dev &&
784 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
785 rtd->codec_dai = codec_dai;
786 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +0000787 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788 }
789 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
790 dai_link->codec_dai_name);
791
792 goto find_platform;
793 }
794 }
795 dev_dbg(card->dev, "CODEC %s not registered\n",
796 dai_link->codec_name);
797
798find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100799 /* do we need a platform? */
800 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000801 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100802
803 /* if there's no platform we match on the empty platform */
804 platform_name = dai_link->platform_name;
805 if (!platform_name)
806 platform_name = "snd-soc-dummy";
807
808 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 list_for_each_entry(platform, &platform_list, list) {
Mark Brown848dd8b2011-04-27 18:16:32 +0100810 if (!strcmp(platform->name, platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 rtd->platform = platform;
812 goto out;
813 }
814 }
815
816 dev_dbg(card->dev, "platform %s not registered\n",
817 dai_link->platform_name);
818 return 0;
819
820out:
821 /* mark rtd as complete if we found all 4 of our client devices */
822 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
823 rtd->complete = 1;
824 card->num_rtd++;
825 }
826 return 1;
827}
828
Jarkko Nikula589c3562010-12-06 16:27:07 +0200829static void soc_remove_codec(struct snd_soc_codec *codec)
830{
831 int err;
832
833 if (codec->driver->remove) {
834 err = codec->driver->remove(codec);
835 if (err < 0)
836 dev_err(codec->dev,
837 "asoc: failed to remove %s: %d\n",
838 codec->name, err);
839 }
840
841 /* Make sure all DAPM widgets are freed */
842 snd_soc_dapm_free(&codec->dapm);
843
844 soc_cleanup_codec_debugfs(codec);
845 codec->probed = 0;
846 list_del(&codec->card_list);
847 module_put(codec->dev->driver->owner);
848}
849
Liam Girdwood0168bf02011-06-07 16:08:05 +0100850static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851{
852 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
853 struct snd_soc_codec *codec = rtd->codec;
854 struct snd_soc_platform *platform = rtd->platform;
855 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
856 int err;
857
858 /* unregister the rtd device */
859 if (rtd->dev_registered) {
860 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200861 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000862 device_unregister(&rtd->dev);
863 rtd->dev_registered = 0;
864 }
865
866 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100867 if (codec_dai && codec_dai->probed &&
868 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 if (codec_dai->driver->remove) {
870 err = codec_dai->driver->remove(codec_dai);
871 if (err < 0)
872 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
873 }
874 codec_dai->probed = 0;
875 list_del(&codec_dai->card_list);
876 }
877
878 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100879 if (platform && platform->probed &&
880 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881 if (platform->driver->remove) {
882 err = platform->driver->remove(platform);
883 if (err < 0)
884 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
885 }
886 platform->probed = 0;
887 list_del(&platform->card_list);
888 module_put(platform->dev->driver->owner);
889 }
890
891 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100892 if (codec && codec->probed &&
893 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200894 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895
896 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100897 if (cpu_dai && cpu_dai->probed &&
898 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899 if (cpu_dai->driver->remove) {
900 err = cpu_dai->driver->remove(cpu_dai);
901 if (err < 0)
902 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
903 }
904 cpu_dai->probed = 0;
905 list_del(&cpu_dai->card_list);
906 module_put(cpu_dai->dev->driver->owner);
907 }
908}
909
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900910static void soc_remove_dai_links(struct snd_soc_card *card)
911{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100912 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900913
Liam Girdwood0168bf02011-06-07 16:08:05 +0100914 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
915 order++) {
916 for (dai = 0; dai < card->num_rtd; dai++)
917 soc_remove_dai_link(card, dai, order);
918 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900919 card->num_rtd = 0;
920}
921
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200922static void soc_set_name_prefix(struct snd_soc_card *card,
923 struct snd_soc_codec *codec)
924{
925 int i;
926
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000927 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200928 return;
929
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000930 for (i = 0; i < card->num_configs; i++) {
931 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200932 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
933 codec->name_prefix = map->name_prefix;
934 break;
935 }
936 }
937}
938
Jarkko Nikula589c3562010-12-06 16:27:07 +0200939static int soc_probe_codec(struct snd_soc_card *card,
940 struct snd_soc_codec *codec)
941{
942 int ret = 0;
Mark Brown89b95ac2011-03-07 16:38:44 +0000943 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200944
945 codec->card = card;
946 codec->dapm.card = card;
947 soc_set_name_prefix(card, codec);
948
Jarkko Nikula70d293312011-01-27 16:24:22 +0200949 if (!try_module_get(codec->dev->driver->owner))
950 return -ENODEV;
951
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200952 soc_init_codec_debugfs(codec);
953
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200954 if (driver->dapm_widgets)
955 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
956 driver->num_dapm_widgets);
957
Mark Brown89b95ac2011-03-07 16:38:44 +0000958 if (driver->probe) {
959 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200960 if (ret < 0) {
961 dev_err(codec->dev,
962 "asoc: failed to probe CODEC %s: %d\n",
963 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +0200964 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200965 }
966 }
967
Mark Brownb7af1da2011-04-07 19:18:44 +0900968 if (driver->controls)
969 snd_soc_add_controls(codec, driver->controls,
970 driver->num_controls);
Mark Brown89b95ac2011-03-07 16:38:44 +0000971 if (driver->dapm_routes)
972 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
973 driver->num_dapm_routes);
974
Jarkko Nikula589c3562010-12-06 16:27:07 +0200975 /* mark codec as probed and add to card codec list */
976 codec->probed = 1;
977 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200978 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200979
Jarkko Nikula70d293312011-01-27 16:24:22 +0200980 return 0;
981
982err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200983 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +0200984 module_put(codec->dev->driver->owner);
985
Jarkko Nikula589c3562010-12-06 16:27:07 +0200986 return ret;
987}
988
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000989static void rtd_release(struct device *dev) {}
990
Jarkko Nikula589c3562010-12-06 16:27:07 +0200991static int soc_post_component_init(struct snd_soc_card *card,
992 struct snd_soc_codec *codec,
993 int num, int dailess)
994{
995 struct snd_soc_dai_link *dai_link = NULL;
996 struct snd_soc_aux_dev *aux_dev = NULL;
997 struct snd_soc_pcm_runtime *rtd;
998 const char *temp, *name;
999 int ret = 0;
1000
1001 if (!dailess) {
1002 dai_link = &card->dai_link[num];
1003 rtd = &card->rtd[num];
1004 name = dai_link->name;
1005 } else {
1006 aux_dev = &card->aux_dev[num];
1007 rtd = &card->rtd_aux[num];
1008 name = aux_dev->name;
1009 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001010 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001011
1012 /* machine controls, routes and widgets are not prefixed */
1013 temp = codec->name_prefix;
1014 codec->name_prefix = NULL;
1015
1016 /* do machine specific initialization */
1017 if (!dailess && dai_link->init)
1018 ret = dai_link->init(rtd);
1019 else if (dailess && aux_dev->init)
1020 ret = aux_dev->init(&codec->dapm);
1021 if (ret < 0) {
1022 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1023 return ret;
1024 }
1025 codec->name_prefix = temp;
1026
1027 /* Make sure all DAPM widgets are instantiated */
1028 snd_soc_dapm_new_widgets(&codec->dapm);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001029
1030 /* register the rtd device */
1031 rtd->codec = codec;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001032 rtd->dev.parent = card->dev;
1033 rtd->dev.release = rtd_release;
1034 rtd->dev.init_name = name;
1035 ret = device_register(&rtd->dev);
1036 if (ret < 0) {
1037 dev_err(card->dev,
1038 "asoc: failed to register runtime device: %d\n", ret);
1039 return ret;
1040 }
1041 rtd->dev_registered = 1;
1042
1043 /* add DAPM sysfs entries for this codec */
1044 ret = snd_soc_dapm_sys_add(&rtd->dev);
1045 if (ret < 0)
1046 dev_err(codec->dev,
1047 "asoc: failed to add codec dapm sysfs entries: %d\n",
1048 ret);
1049
1050 /* add codec sysfs entries */
1051 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1052 if (ret < 0)
1053 dev_err(codec->dev,
1054 "asoc: failed to add codec sysfs files: %d\n", ret);
1055
1056 return 0;
1057}
1058
Liam Girdwood0168bf02011-06-07 16:08:05 +01001059static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001060{
1061 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1062 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1063 struct snd_soc_codec *codec = rtd->codec;
1064 struct snd_soc_platform *platform = rtd->platform;
1065 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1066 int ret;
1067
Liam Girdwood0168bf02011-06-07 16:08:05 +01001068 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1069 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001070
1071 /* config components */
1072 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001073 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001074 codec_dai->card = card;
1075 cpu_dai->card = card;
1076
1077 /* set default power off timeout */
1078 rtd->pmdown_time = pmdown_time;
1079
1080 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001081 if (!cpu_dai->probed &&
1082 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001083 if (!try_module_get(cpu_dai->dev->driver->owner))
1084 return -ENODEV;
1085
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001086 if (cpu_dai->driver->probe) {
1087 ret = cpu_dai->driver->probe(cpu_dai);
1088 if (ret < 0) {
1089 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1090 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001091 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 return ret;
1093 }
1094 }
1095 cpu_dai->probed = 1;
1096 /* mark cpu_dai as probed and add to card cpu_dai list */
1097 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1098 }
1099
1100 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001101 if (!codec->probed &&
1102 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001103 ret = soc_probe_codec(card, codec);
1104 if (ret < 0)
1105 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 }
1107
1108 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001109 if (!platform->probed &&
1110 platform->driver->probe_order == order) {
Jarkko Nikula70d293312011-01-27 16:24:22 +02001111 if (!try_module_get(platform->dev->driver->owner))
1112 return -ENODEV;
1113
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001114 if (platform->driver->probe) {
1115 ret = platform->driver->probe(platform);
1116 if (ret < 0) {
1117 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1118 platform->name);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001119 module_put(platform->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 return ret;
1121 }
1122 }
1123 /* mark platform as probed and add to card platform list */
1124 platform->probed = 1;
1125 list_add(&platform->card_list, &card->platform_dev_list);
1126 }
1127
1128 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001129 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001130 if (codec_dai->driver->probe) {
1131 ret = codec_dai->driver->probe(codec_dai);
1132 if (ret < 0) {
1133 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1134 codec_dai->name);
1135 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001136 }
1137 }
1138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001139 /* mark cpu_dai as probed and add to card cpu_dai list */
1140 codec_dai->probed = 1;
1141 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001142 }
1143
Liam Girdwood0168bf02011-06-07 16:08:05 +01001144 /* complete DAI probe during last probe */
1145 if (order != SND_SOC_COMP_ORDER_LAST)
1146 return 0;
1147
Jarkko Nikula589c3562010-12-06 16:27:07 +02001148 ret = soc_post_component_init(card, codec, num, 0);
1149 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001150 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1153 if (ret < 0)
1154 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 /* create the pcm */
1157 ret = soc_new_pcm(rtd, num);
1158 if (ret < 0) {
1159 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1160 return ret;
1161 }
1162
1163 /* add platform data for AC97 devices */
1164 if (rtd->codec_dai->driver->ac97_control)
1165 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1166
1167 return 0;
1168}
1169
1170#ifdef CONFIG_SND_SOC_AC97_BUS
1171static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1172{
1173 int ret;
1174
1175 /* Only instantiate AC97 if not already done by the adaptor
1176 * for the generic AC97 subsystem.
1177 */
1178 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001179 /*
1180 * It is possible that the AC97 device is already registered to
1181 * the device subsystem. This happens when the device is created
1182 * via snd_ac97_mixer(). Currently only SoC codec that does so
1183 * is the generic AC97 glue but others migh emerge.
1184 *
1185 * In those cases we don't try to register the device again.
1186 */
1187 if (!rtd->codec->ac97_created)
1188 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001189
1190 ret = soc_ac97_dev_register(rtd->codec);
1191 if (ret < 0) {
1192 printk(KERN_ERR "asoc: AC97 device register failed\n");
1193 return ret;
1194 }
1195
1196 rtd->codec->ac97_registered = 1;
1197 }
1198 return 0;
1199}
1200
1201static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1202{
1203 if (codec->ac97_registered) {
1204 soc_ac97_dev_unregister(codec);
1205 codec->ac97_registered = 0;
1206 }
1207}
1208#endif
1209
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001210static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1211{
1212 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001213 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001214 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001215
1216 /* find CODEC from registered CODECs*/
1217 list_for_each_entry(codec, &codec_list, list) {
1218 if (!strcmp(codec->name, aux_dev->codec_name)) {
1219 if (codec->probed) {
1220 dev_err(codec->dev,
1221 "asoc: codec already probed");
1222 ret = -EBUSY;
1223 goto out;
1224 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001225 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001226 }
1227 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001228 /* codec not found */
1229 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1230 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001231
Jarkko Nikula676ad982010-12-03 09:18:22 +02001232found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001233 ret = soc_probe_codec(card, codec);
1234 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001235 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001236
Jarkko Nikula589c3562010-12-06 16:27:07 +02001237 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001238
1239out:
1240 return ret;
1241}
1242
1243static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1244{
1245 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1246 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001247
1248 /* unregister the rtd device */
1249 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001250 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001251 device_unregister(&rtd->dev);
1252 rtd->dev_registered = 0;
1253 }
1254
Jarkko Nikula589c3562010-12-06 16:27:07 +02001255 if (codec && codec->probed)
1256 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001257}
1258
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001259static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1260 enum snd_soc_compress_type compress_type)
1261{
1262 int ret;
1263
1264 if (codec->cache_init)
1265 return 0;
1266
1267 /* override the compress_type if necessary */
1268 if (compress_type && codec->compress_type != compress_type)
1269 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001270 ret = snd_soc_cache_init(codec);
1271 if (ret < 0) {
1272 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1273 ret);
1274 return ret;
1275 }
1276 codec->cache_init = 1;
1277 return 0;
1278}
1279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280static void snd_soc_instantiate_card(struct snd_soc_card *card)
1281{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001282 struct snd_soc_codec *codec;
1283 struct snd_soc_codec_conf *codec_conf;
1284 enum snd_soc_compress_type compress_type;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001285 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286
1287 mutex_lock(&card->mutex);
1288
1289 if (card->instantiated) {
1290 mutex_unlock(&card->mutex);
1291 return;
1292 }
1293
1294 /* bind DAIs */
1295 for (i = 0; i < card->num_links; i++)
1296 soc_bind_dai_link(card, i);
1297
1298 /* bind completed ? */
1299 if (card->num_rtd != card->num_links) {
1300 mutex_unlock(&card->mutex);
1301 return;
1302 }
1303
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001304 /* initialize the register cache for each available codec */
1305 list_for_each_entry(codec, &codec_list, list) {
1306 if (codec->cache_init)
1307 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001308 /* by default we don't override the compress_type */
1309 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001310 /* check to see if we need to override the compress_type */
1311 for (i = 0; i < card->num_configs; ++i) {
1312 codec_conf = &card->codec_conf[i];
1313 if (!strcmp(codec->name, codec_conf->dev_name)) {
1314 compress_type = codec_conf->compress_type;
1315 if (compress_type && compress_type
1316 != codec->compress_type)
1317 break;
1318 }
1319 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001320 ret = snd_soc_init_codec_cache(codec, compress_type);
1321 if (ret < 0) {
1322 mutex_unlock(&card->mutex);
1323 return;
1324 }
1325 }
1326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001327 /* card bind complete so register a sound card */
1328 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1329 card->owner, 0, &card->snd_card);
1330 if (ret < 0) {
1331 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1332 card->name);
1333 mutex_unlock(&card->mutex);
1334 return;
1335 }
1336 card->snd_card->dev = card->dev;
1337
Mark Browne37a4972011-03-02 18:21:57 +00001338 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1339 card->dapm.dev = card->dev;
1340 card->dapm.card = card;
1341 list_add(&card->dapm.list, &card->dapm_list);
1342
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001343#ifdef CONFIG_DEBUG_FS
1344 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1345#endif
1346
Mark Brown88ee1c62011-02-02 10:43:26 +00001347#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001348 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001349 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001350#endif
Andy Green6ed25972008-06-13 16:24:05 +01001351
Mark Brown9a841eb2011-04-12 17:51:37 -07001352 if (card->dapm_widgets)
1353 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1354 card->num_dapm_widgets);
1355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001356 /* initialise the sound card only once */
1357 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001358 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001359 if (ret < 0)
1360 goto card_probe_error;
1361 }
1362
Liam Girdwood0168bf02011-06-07 16:08:05 +01001363 /* early DAI link probe */
1364 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1365 order++) {
1366 for (i = 0; i < card->num_links; i++) {
1367 ret = soc_probe_dai_link(card, i, order);
1368 if (ret < 0) {
1369 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001370 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001371 goto probe_dai_err;
1372 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 }
1374 }
1375
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001376 for (i = 0; i < card->num_aux_devs; i++) {
1377 ret = soc_probe_aux_dev(card, i);
1378 if (ret < 0) {
1379 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1380 card->name, ret);
1381 goto probe_aux_dev_err;
1382 }
1383 }
1384
Mark Brownb7af1da2011-04-07 19:18:44 +09001385 /* We should have a non-codec control add function but we don't */
1386 if (card->controls)
1387 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1388 struct snd_soc_codec,
1389 card_list),
1390 card->controls,
1391 card->num_controls);
1392
Mark Brownb8ad29d2011-03-02 18:35:51 +00001393 if (card->dapm_routes)
1394 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1395 card->num_dapm_routes);
1396
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001398 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001399 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1400 "%s", card->long_name ? card->long_name : card->name);
1401 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
Jarkko Nikula2b395352011-05-20 15:47:40 +03001402 "%s", card->driver_name ? card->driver_name : card->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001403
Mark Brown28e9ad92011-03-02 18:36:34 +00001404 if (card->late_probe) {
1405 ret = card->late_probe(card);
1406 if (ret < 0) {
1407 dev_err(card->dev, "%s late_probe() failed: %d\n",
1408 card->name, ret);
1409 goto probe_aux_dev_err;
1410 }
1411 }
1412
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001413 ret = snd_card_register(card->snd_card);
1414 if (ret < 0) {
1415 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001416 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001417 }
1418
1419#ifdef CONFIG_SND_SOC_AC97_BUS
1420 /* register any AC97 codecs */
1421 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001422 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1423 if (ret < 0) {
1424 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1425 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001426 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001427 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001428 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001429 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001430#endif
1431
Mark Brown435c5e22008-12-04 15:32:53 +00001432 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001434 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001435
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001436probe_aux_dev_err:
1437 for (i = 0; i < card->num_aux_devs; i++)
1438 soc_remove_aux_dev(card, i);
1439
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001441 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001442
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001444 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001445 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001446
1447 snd_card_free(card->snd_card);
1448
1449 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001450}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001451
Mark Brown435c5e22008-12-04 15:32:53 +00001452/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001453 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001454 * client_mutex.
1455 */
1456static void snd_soc_instantiate_cards(void)
1457{
1458 struct snd_soc_card *card;
1459 list_for_each_entry(card, &card_list, list)
1460 snd_soc_instantiate_card(card);
1461}
1462
1463/* probes a new socdev */
1464static int soc_probe(struct platform_device *pdev)
1465{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001467 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001468
Vinod Koul70a7ca32011-01-14 19:22:48 +05301469 /*
1470 * no card, so machine driver should be registering card
1471 * we should not be here in that case so ret error
1472 */
1473 if (!card)
1474 return -EINVAL;
1475
Mark Brown435c5e22008-12-04 15:32:53 +00001476 /* Bodge while we unpick instantiation */
1477 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478
Mark Brown435c5e22008-12-04 15:32:53 +00001479 ret = snd_soc_register_card(card);
1480 if (ret != 0) {
1481 dev_err(&pdev->dev, "Failed to register card\n");
1482 return ret;
1483 }
1484
1485 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001486}
1487
Vinod Koulb0e26482011-01-13 22:48:02 +05301488static int soc_cleanup_card_resources(struct snd_soc_card *card)
1489{
Vinod Koulb0e26482011-01-13 22:48:02 +05301490 int i;
1491
1492 /* make sure any delayed work runs */
1493 for (i = 0; i < card->num_rtd; i++) {
1494 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1495 flush_delayed_work_sync(&rtd->delayed_work);
1496 }
1497
1498 /* remove auxiliary devices */
1499 for (i = 0; i < card->num_aux_devs; i++)
1500 soc_remove_aux_dev(card, i);
1501
1502 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001503 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301504
1505 soc_cleanup_card_debugfs(card);
1506
1507 /* remove the card */
1508 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001509 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301510
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001511 snd_soc_dapm_free(&card->dapm);
1512
Vinod Koulb0e26482011-01-13 22:48:02 +05301513 kfree(card->rtd);
1514 snd_card_free(card->snd_card);
1515 return 0;
1516
1517}
1518
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001519/* removes a socdev */
1520static int soc_remove(struct platform_device *pdev)
1521{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001523
Mark Brownc5af3a22008-11-28 13:29:45 +00001524 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001525 return 0;
1526}
1527
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001528int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001529{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001530 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001531 int i;
Mark Brown51737472009-06-22 13:16:51 +01001532
1533 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001534 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001535
1536 /* Flush out pmdown_time work - we actually do want to run it
1537 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538 for (i = 0; i < card->num_rtd; i++) {
1539 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001540 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001541 }
Mark Brown51737472009-06-22 13:16:51 +01001542
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001544
1545 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001546}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001547EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001548
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001549const struct dev_pm_ops snd_soc_pm_ops = {
1550 .suspend = snd_soc_suspend,
1551 .resume = snd_soc_resume,
1552 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001553};
Stephen Warrendeb26072011-04-05 19:35:30 -06001554EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001555
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001556/* ASoC platform driver */
1557static struct platform_driver soc_driver = {
1558 .driver = {
1559 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001560 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001561 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562 },
1563 .probe = soc_probe,
1564 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001565};
1566
Mark Brown096e49d2009-07-05 15:12:22 +01001567/**
1568 * snd_soc_codec_volatile_register: Report if a register is volatile.
1569 *
1570 * @codec: CODEC to query.
1571 * @reg: Register to query.
1572 *
1573 * Boolean function indiciating if a CODEC register is volatile.
1574 */
Mark Brown181e0552011-01-24 14:05:25 +00001575int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1576 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001577{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001578 if (codec->volatile_register)
1579 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001580 else
1581 return 0;
1582}
1583EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1584
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001585/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001586 * snd_soc_codec_readable_register: Report if a register is readable.
1587 *
1588 * @codec: CODEC to query.
1589 * @reg: Register to query.
1590 *
1591 * Boolean function indicating if a CODEC register is readable.
1592 */
1593int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1594 unsigned int reg)
1595{
1596 if (codec->readable_register)
1597 return codec->readable_register(codec, reg);
1598 else
1599 return 0;
1600}
1601EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1602
1603/**
1604 * snd_soc_codec_writable_register: Report if a register is writable.
1605 *
1606 * @codec: CODEC to query.
1607 * @reg: Register to query.
1608 *
1609 * Boolean function indicating if a CODEC register is writable.
1610 */
1611int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1612 unsigned int reg)
1613{
1614 if (codec->writable_register)
1615 return codec->writable_register(codec, reg);
1616 else
1617 return 0;
1618}
1619EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1620
1621/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001622 * snd_soc_new_ac97_codec - initailise AC97 device
1623 * @codec: audio codec
1624 * @ops: AC97 bus operations
1625 * @num: AC97 codec number
1626 *
1627 * Initialises AC97 codec resources for use by ad-hoc devices only.
1628 */
1629int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1630 struct snd_ac97_bus_ops *ops, int num)
1631{
1632 mutex_lock(&codec->mutex);
1633
1634 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1635 if (codec->ac97 == NULL) {
1636 mutex_unlock(&codec->mutex);
1637 return -ENOMEM;
1638 }
1639
1640 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1641 if (codec->ac97->bus == NULL) {
1642 kfree(codec->ac97);
1643 codec->ac97 = NULL;
1644 mutex_unlock(&codec->mutex);
1645 return -ENOMEM;
1646 }
1647
1648 codec->ac97->bus->ops = ops;
1649 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001650
1651 /*
1652 * Mark the AC97 device to be created by us. This way we ensure that the
1653 * device will be registered with the device subsystem later on.
1654 */
1655 codec->ac97_created = 1;
1656
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001657 mutex_unlock(&codec->mutex);
1658 return 0;
1659}
1660EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1661
1662/**
1663 * snd_soc_free_ac97_codec - free AC97 codec device
1664 * @codec: audio codec
1665 *
1666 * Frees AC97 codec device resources.
1667 */
1668void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1669{
1670 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671#ifdef CONFIG_SND_SOC_AC97_BUS
1672 soc_unregister_ac97_dai_link(codec);
1673#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001674 kfree(codec->ac97->bus);
1675 kfree(codec->ac97);
1676 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001677 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001678 mutex_unlock(&codec->mutex);
1679}
1680EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1681
Mark Brownc3753702010-11-01 15:41:57 -04001682unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1683{
1684 unsigned int ret;
1685
Mark Brownc3acec22010-12-02 16:15:29 +00001686 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001687 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001688 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001689
1690 return ret;
1691}
1692EXPORT_SYMBOL_GPL(snd_soc_read);
1693
1694unsigned int snd_soc_write(struct snd_soc_codec *codec,
1695 unsigned int reg, unsigned int val)
1696{
1697 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001698 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001699 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001700}
1701EXPORT_SYMBOL_GPL(snd_soc_write);
1702
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001703unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1704 unsigned int reg, const void *data, size_t len)
1705{
1706 return codec->bulk_write_raw(codec, reg, data, len);
1707}
1708EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1709
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710/**
1711 * snd_soc_update_bits - update codec register bits
1712 * @codec: audio codec
1713 * @reg: codec register
1714 * @mask: register mask
1715 * @value: new value
1716 *
1717 * Writes new register value.
1718 *
Timur Tabi180c3292011-01-10 15:58:13 -06001719 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001720 */
1721int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001722 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001723{
1724 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001725 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001726 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001727
Timur Tabi180c3292011-01-10 15:58:13 -06001728 ret = snd_soc_read(codec, reg);
1729 if (ret < 0)
1730 return ret;
1731
1732 old = ret;
Mark Brown78bf3c92011-06-03 17:09:14 +01001733 new = (old & ~mask) | (value & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06001735 if (change) {
1736 ret = snd_soc_write(codec, reg, new);
1737 if (ret < 0)
1738 return ret;
1739 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001741 return change;
1742}
1743EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1744
1745/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001746 * snd_soc_update_bits_locked - update codec register bits
1747 * @codec: audio codec
1748 * @reg: codec register
1749 * @mask: register mask
1750 * @value: new value
1751 *
1752 * Writes new register value, and takes the codec mutex.
1753 *
1754 * Returns 1 for change else 0.
1755 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001756int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1757 unsigned short reg, unsigned int mask,
1758 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001759{
1760 int change;
1761
1762 mutex_lock(&codec->mutex);
1763 change = snd_soc_update_bits(codec, reg, mask, value);
1764 mutex_unlock(&codec->mutex);
1765
1766 return change;
1767}
Mark Browndd1b3d52009-12-04 14:22:03 +00001768EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001769
1770/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771 * snd_soc_test_bits - test register for change
1772 * @codec: audio codec
1773 * @reg: codec register
1774 * @mask: register mask
1775 * @value: new value
1776 *
1777 * Tests a register with a new value and checks if the new value is
1778 * different from the old value.
1779 *
1780 * Returns 1 for change else 0.
1781 */
1782int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001783 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001784{
1785 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001786 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001787
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788 old = snd_soc_read(codec, reg);
1789 new = (old & ~mask) | value;
1790 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791
1792 return change;
1793}
1794EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1795
1796/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001797 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1798 * @substream: the pcm substream
1799 * @hw: the hardware parameters
1800 *
1801 * Sets the substream runtime hardware parameters.
1802 */
1803int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1804 const struct snd_pcm_hardware *hw)
1805{
1806 struct snd_pcm_runtime *runtime = substream->runtime;
1807 runtime->hw.info = hw->info;
1808 runtime->hw.formats = hw->formats;
1809 runtime->hw.period_bytes_min = hw->period_bytes_min;
1810 runtime->hw.period_bytes_max = hw->period_bytes_max;
1811 runtime->hw.periods_min = hw->periods_min;
1812 runtime->hw.periods_max = hw->periods_max;
1813 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1814 runtime->hw.fifo_size = hw->fifo_size;
1815 return 0;
1816}
1817EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1818
1819/**
1820 * snd_soc_cnew - create new control
1821 * @_template: control template
1822 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001823 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001824 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001825 *
1826 * Create a new mixer control from a template control.
1827 *
1828 * Returns 0 for success, else error.
1829 */
1830struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001831 void *data, char *long_name,
1832 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833{
1834 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001835 struct snd_kcontrol *kcontrol;
1836 char *name = NULL;
1837 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838
1839 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001840 template.index = 0;
1841
Mark Brownefb7ac32011-03-08 17:23:24 +00001842 if (!long_name)
1843 long_name = template.name;
1844
1845 if (prefix) {
1846 name_len = strlen(long_name) + strlen(prefix) + 2;
1847 name = kmalloc(name_len, GFP_ATOMIC);
1848 if (!name)
1849 return NULL;
1850
1851 snprintf(name, name_len, "%s %s", prefix, long_name);
1852
1853 template.name = name;
1854 } else {
1855 template.name = long_name;
1856 }
1857
1858 kcontrol = snd_ctl_new1(&template, data);
1859
1860 kfree(name);
1861
1862 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001863}
1864EXPORT_SYMBOL_GPL(snd_soc_cnew);
1865
1866/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001867 * snd_soc_add_controls - add an array of controls to a codec.
1868 * Convienience function to add a list of controls. Many codecs were
1869 * duplicating this code.
1870 *
1871 * @codec: codec to add controls to
1872 * @controls: array of controls to add
1873 * @num_controls: number of elements in the array
1874 *
1875 * Return 0 for success, else error.
1876 */
1877int snd_soc_add_controls(struct snd_soc_codec *codec,
1878 const struct snd_kcontrol_new *controls, int num_controls)
1879{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001880 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001881 int err, i;
1882
1883 for (i = 0; i < num_controls; i++) {
1884 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00001885 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
1886 control->name,
1887 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00001888 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01001889 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00001890 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00001891 return err;
1892 }
1893 }
1894
1895 return 0;
1896}
1897EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1898
1899/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900 * snd_soc_info_enum_double - enumerated double mixer info callback
1901 * @kcontrol: mixer control
1902 * @uinfo: control element information
1903 *
1904 * Callback to provide information about a double enumerated
1905 * mixer control.
1906 *
1907 * Returns 0 for success.
1908 */
1909int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_info *uinfo)
1911{
1912 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1913
1914 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1915 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001916 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001918 if (uinfo->value.enumerated.item > e->max - 1)
1919 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001920 strcpy(uinfo->value.enumerated.name,
1921 e->texts[uinfo->value.enumerated.item]);
1922 return 0;
1923}
1924EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1925
1926/**
1927 * snd_soc_get_enum_double - enumerated double mixer get callback
1928 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001929 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930 *
1931 * Callback to get the value of a double enumerated mixer.
1932 *
1933 * Returns 0 for success.
1934 */
1935int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_value *ucontrol)
1937{
1938 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1939 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001940 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001942 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943 ;
1944 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001945 ucontrol->value.enumerated.item[0]
1946 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 if (e->shift_l != e->shift_r)
1948 ucontrol->value.enumerated.item[1] =
1949 (val >> e->shift_r) & (bitmask - 1);
1950
1951 return 0;
1952}
1953EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1954
1955/**
1956 * snd_soc_put_enum_double - enumerated double mixer put callback
1957 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001958 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001959 *
1960 * Callback to set the value of a double enumerated mixer.
1961 *
1962 * Returns 0 for success.
1963 */
1964int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1966{
1967 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1968 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001969 unsigned int val;
1970 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001972 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001974 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975 return -EINVAL;
1976 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1977 mask = (bitmask - 1) << e->shift_l;
1978 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001979 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 return -EINVAL;
1981 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1982 mask |= (bitmask - 1) << e->shift_r;
1983 }
1984
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001985 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986}
1987EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1988
1989/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001990 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1991 * @kcontrol: mixer control
1992 * @ucontrol: control element information
1993 *
1994 * Callback to get the value of a double semi enumerated mixer.
1995 *
1996 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1997 * used for handling bitfield coded enumeration for example.
1998 *
1999 * Returns 0 for success.
2000 */
2001int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
2003{
2004 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002005 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002006 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002007
2008 reg_val = snd_soc_read(codec, e->reg);
2009 val = (reg_val >> e->shift_l) & e->mask;
2010 for (mux = 0; mux < e->max; mux++) {
2011 if (val == e->values[mux])
2012 break;
2013 }
2014 ucontrol->value.enumerated.item[0] = mux;
2015 if (e->shift_l != e->shift_r) {
2016 val = (reg_val >> e->shift_r) & e->mask;
2017 for (mux = 0; mux < e->max; mux++) {
2018 if (val == e->values[mux])
2019 break;
2020 }
2021 ucontrol->value.enumerated.item[1] = mux;
2022 }
2023
2024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2027
2028/**
2029 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2030 * @kcontrol: mixer control
2031 * @ucontrol: control element information
2032 *
2033 * Callback to set the value of a double semi enumerated mixer.
2034 *
2035 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2036 * used for handling bitfield coded enumeration for example.
2037 *
2038 * Returns 0 for success.
2039 */
2040int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
2042{
2043 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002044 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002045 unsigned int val;
2046 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002047
2048 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2049 return -EINVAL;
2050 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2051 mask = e->mask << e->shift_l;
2052 if (e->shift_l != e->shift_r) {
2053 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2054 return -EINVAL;
2055 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2056 mask |= e->mask << e->shift_r;
2057 }
2058
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002059 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002060}
2061EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2062
2063/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002064 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2065 * @kcontrol: mixer control
2066 * @uinfo: control element information
2067 *
2068 * Callback to provide information about an external enumerated
2069 * single mixer.
2070 *
2071 * Returns 0 for success.
2072 */
2073int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_info *uinfo)
2075{
2076 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2077
2078 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2079 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002080 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002081
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002082 if (uinfo->value.enumerated.item > e->max - 1)
2083 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084 strcpy(uinfo->value.enumerated.name,
2085 e->texts[uinfo->value.enumerated.item]);
2086 return 0;
2087}
2088EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2089
2090/**
2091 * snd_soc_info_volsw_ext - external single mixer info callback
2092 * @kcontrol: mixer control
2093 * @uinfo: control element information
2094 *
2095 * Callback to provide information about a single external mixer control.
2096 *
2097 * Returns 0 for success.
2098 */
2099int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2100 struct snd_ctl_elem_info *uinfo)
2101{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002102 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103
Mark Brownfd5dfad2009-04-15 21:37:46 +01002104 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002105 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2106 else
2107 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2108
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 uinfo->count = 1;
2110 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002111 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 return 0;
2113}
2114EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2115
2116/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 * snd_soc_info_volsw - single mixer info callback
2118 * @kcontrol: mixer control
2119 * @uinfo: control element information
2120 *
2121 * Callback to provide information about a single mixer control.
2122 *
2123 * Returns 0 for success.
2124 */
2125int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_info *uinfo)
2127{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002128 struct soc_mixer_control *mc =
2129 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002130 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002131 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04002132 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002134 if (!mc->platform_max)
2135 mc->platform_max = mc->max;
2136 platform_max = mc->platform_max;
2137
2138 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002139 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2140 else
2141 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2142
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002143 uinfo->count = shift == rshift ? 1 : 2;
2144 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002145 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2149
2150/**
2151 * snd_soc_get_volsw - single mixer get callback
2152 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002153 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002154 *
2155 * Callback to get the value of a single mixer control.
2156 *
2157 * Returns 0 for success.
2158 */
2159int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2161{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002162 struct soc_mixer_control *mc =
2163 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002164 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002165 unsigned int reg = mc->reg;
2166 unsigned int shift = mc->shift;
2167 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002168 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002169 unsigned int mask = (1 << fls(max)) - 1;
2170 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002171
2172 ucontrol->value.integer.value[0] =
2173 (snd_soc_read(codec, reg) >> shift) & mask;
2174 if (shift != rshift)
2175 ucontrol->value.integer.value[1] =
2176 (snd_soc_read(codec, reg) >> rshift) & mask;
2177 if (invert) {
2178 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002179 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180 if (shift != rshift)
2181 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002182 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183 }
2184
2185 return 0;
2186}
2187EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2188
2189/**
2190 * snd_soc_put_volsw - single mixer put callback
2191 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002192 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193 *
2194 * Callback to set the value of a single mixer control.
2195 *
2196 * Returns 0 for success.
2197 */
2198int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2200{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002201 struct soc_mixer_control *mc =
2202 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002204 unsigned int reg = mc->reg;
2205 unsigned int shift = mc->shift;
2206 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002207 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002208 unsigned int mask = (1 << fls(max)) - 1;
2209 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002210 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211
2212 val = (ucontrol->value.integer.value[0] & mask);
2213 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002214 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215 val_mask = mask << shift;
2216 val = val << shift;
2217 if (shift != rshift) {
2218 val2 = (ucontrol->value.integer.value[1] & mask);
2219 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002220 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221 val_mask |= mask << rshift;
2222 val |= val2 << rshift;
2223 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002224 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002225}
2226EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2227
2228/**
2229 * snd_soc_info_volsw_2r - double mixer info callback
2230 * @kcontrol: mixer control
2231 * @uinfo: control element information
2232 *
2233 * Callback to provide information about a double mixer control that
2234 * spans 2 codec registers.
2235 *
2236 * Returns 0 for success.
2237 */
2238int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_info *uinfo)
2240{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002241 struct soc_mixer_control *mc =
2242 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002243 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002245 if (!mc->platform_max)
2246 mc->platform_max = mc->max;
2247 platform_max = mc->platform_max;
2248
2249 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002250 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2251 else
2252 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2253
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 uinfo->count = 2;
2255 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002256 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257 return 0;
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2260
2261/**
2262 * snd_soc_get_volsw_2r - double mixer get callback
2263 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002264 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265 *
2266 * Callback to get the value of a double mixer control that spans 2 registers.
2267 *
2268 * Returns 0 for success.
2269 */
2270int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2272{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002273 struct soc_mixer_control *mc =
2274 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002276 unsigned int reg = mc->reg;
2277 unsigned int reg2 = mc->rreg;
2278 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002279 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002280 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002281 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282
2283 ucontrol->value.integer.value[0] =
2284 (snd_soc_read(codec, reg) >> shift) & mask;
2285 ucontrol->value.integer.value[1] =
2286 (snd_soc_read(codec, reg2) >> shift) & mask;
2287 if (invert) {
2288 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002289 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002291 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002292 }
2293
2294 return 0;
2295}
2296EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2297
2298/**
2299 * snd_soc_put_volsw_2r - double mixer set callback
2300 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002301 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002302 *
2303 * Callback to set the value of a double mixer control that spans 2 registers.
2304 *
2305 * Returns 0 for success.
2306 */
2307int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_value *ucontrol)
2309{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002310 struct soc_mixer_control *mc =
2311 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002313 unsigned int reg = mc->reg;
2314 unsigned int reg2 = mc->rreg;
2315 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002316 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002317 unsigned int mask = (1 << fls(max)) - 1;
2318 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002320 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321
2322 val_mask = mask << shift;
2323 val = (ucontrol->value.integer.value[0] & mask);
2324 val2 = (ucontrol->value.integer.value[1] & mask);
2325
2326 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002327 val = max - val;
2328 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329 }
2330
2331 val = val << shift;
2332 val2 = val2 << shift;
2333
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002334 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002335 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336 return err;
2337
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002338 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339 return err;
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2342
Mark Browne13ac2e2008-05-28 17:58:05 +01002343/**
2344 * snd_soc_info_volsw_s8 - signed mixer info callback
2345 * @kcontrol: mixer control
2346 * @uinfo: control element information
2347 *
2348 * Callback to provide information about a signed mixer control.
2349 *
2350 * Returns 0 for success.
2351 */
2352int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2354{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002355 struct soc_mixer_control *mc =
2356 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002357 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002358 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002359
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002360 if (!mc->platform_max)
2361 mc->platform_max = mc->max;
2362 platform_max = mc->platform_max;
2363
Mark Browne13ac2e2008-05-28 17:58:05 +01002364 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2365 uinfo->count = 2;
2366 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002367 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002368 return 0;
2369}
2370EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2371
2372/**
2373 * snd_soc_get_volsw_s8 - signed mixer get callback
2374 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002375 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002376 *
2377 * Callback to get the value of a signed mixer control.
2378 *
2379 * Returns 0 for success.
2380 */
2381int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
2383{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002384 struct soc_mixer_control *mc =
2385 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002386 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002387 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002388 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002389 int val = snd_soc_read(codec, reg);
2390
2391 ucontrol->value.integer.value[0] =
2392 ((signed char)(val & 0xff))-min;
2393 ucontrol->value.integer.value[1] =
2394 ((signed char)((val >> 8) & 0xff))-min;
2395 return 0;
2396}
2397EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2398
2399/**
2400 * snd_soc_put_volsw_sgn - signed mixer put callback
2401 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002402 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002403 *
2404 * Callback to set the value of a signed mixer control.
2405 *
2406 * Returns 0 for success.
2407 */
2408int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_value *ucontrol)
2410{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002411 struct soc_mixer_control *mc =
2412 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002413 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002414 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002415 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002416 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002417
2418 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2419 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2420
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002421 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002422}
2423EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2424
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002425/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002426 * snd_soc_limit_volume - Set new limit to an existing volume control.
2427 *
2428 * @codec: where to look for the control
2429 * @name: Name of the control
2430 * @max: new maximum limit
2431 *
2432 * Return 0 for success, else error.
2433 */
2434int snd_soc_limit_volume(struct snd_soc_codec *codec,
2435 const char *name, int max)
2436{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002437 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002438 struct snd_kcontrol *kctl;
2439 struct soc_mixer_control *mc;
2440 int found = 0;
2441 int ret = -EINVAL;
2442
2443 /* Sanity check for name and max */
2444 if (unlikely(!name || max <= 0))
2445 return -EINVAL;
2446
2447 list_for_each_entry(kctl, &card->controls, list) {
2448 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2449 found = 1;
2450 break;
2451 }
2452 }
2453 if (found) {
2454 mc = (struct soc_mixer_control *)kctl->private_value;
2455 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002456 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002457 ret = 0;
2458 }
2459 }
2460 return ret;
2461}
2462EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2463
2464/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002465 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2466 * mixer info callback
2467 * @kcontrol: mixer control
2468 * @uinfo: control element information
2469 *
2470 * Returns 0 for success.
2471 */
2472int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_info *uinfo)
2474{
2475 struct soc_mixer_control *mc =
2476 (struct soc_mixer_control *)kcontrol->private_value;
2477 int max = mc->max;
2478 int min = mc->min;
2479
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2481 uinfo->count = 2;
2482 uinfo->value.integer.min = 0;
2483 uinfo->value.integer.max = max-min;
2484
2485 return 0;
2486}
2487EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2488
2489/**
2490 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2491 * mixer get callback
2492 * @kcontrol: mixer control
2493 * @uinfo: control element information
2494 *
2495 * Returns 0 for success.
2496 */
2497int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
2499{
2500 struct soc_mixer_control *mc =
2501 (struct soc_mixer_control *)kcontrol->private_value;
2502 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2503 unsigned int mask = (1<<mc->shift)-1;
2504 int min = mc->min;
2505 int val = snd_soc_read(codec, mc->reg) & mask;
2506 int valr = snd_soc_read(codec, mc->rreg) & mask;
2507
Stuart Longland20630c72010-06-18 12:56:10 +10002508 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2509 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002510 return 0;
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2513
2514/**
2515 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2516 * mixer put callback
2517 * @kcontrol: mixer control
2518 * @uinfo: control element information
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
2525 struct soc_mixer_control *mc =
2526 (struct soc_mixer_control *)kcontrol->private_value;
2527 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2528 unsigned int mask = (1<<mc->shift)-1;
2529 int min = mc->min;
2530 int ret;
2531 unsigned int val, valr, oval, ovalr;
2532
2533 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2534 val &= mask;
2535 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2536 valr &= mask;
2537
2538 oval = snd_soc_read(codec, mc->reg) & mask;
2539 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2540
2541 ret = 0;
2542 if (oval != val) {
2543 ret = snd_soc_write(codec, mc->reg, val);
2544 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002545 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002546 }
2547 if (ovalr != valr) {
2548 ret = snd_soc_write(codec, mc->rreg, valr);
2549 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002550 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002551 }
2552
2553 return 0;
2554}
2555EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2556
2557/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002558 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2559 * @dai: DAI
2560 * @clk_id: DAI specific clock ID
2561 * @freq: new clock frequency in Hz
2562 * @dir: new clock direction - input/output.
2563 *
2564 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2565 */
2566int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2567 unsigned int freq, int dir)
2568{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002569 if (dai->driver && dai->driver->ops->set_sysclk)
2570 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002571 else if (dai->codec && dai->codec->driver->set_sysclk)
2572 return dai->codec->driver->set_sysclk(dai->codec, clk_id,
2573 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002574 else
2575 return -EINVAL;
2576}
2577EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2578
2579/**
Mark Brownec4ee522011-03-07 20:58:11 +00002580 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2581 * @codec: CODEC
2582 * @clk_id: DAI specific clock ID
2583 * @freq: new clock frequency in Hz
2584 * @dir: new clock direction - input/output.
2585 *
2586 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2587 */
2588int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2589 unsigned int freq, int dir)
2590{
2591 if (codec->driver->set_sysclk)
2592 return codec->driver->set_sysclk(codec, clk_id, freq, dir);
2593 else
2594 return -EINVAL;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2597
2598/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002599 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2600 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002601 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002602 * @div: new clock divisor.
2603 *
2604 * Configures the clock dividers. This is used to derive the best DAI bit and
2605 * frame clocks from the system or master clock. It's best to set the DAI bit
2606 * and frame clocks as low as possible to save system power.
2607 */
2608int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2609 int div_id, int div)
2610{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002611 if (dai->driver && dai->driver->ops->set_clkdiv)
2612 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002613 else
2614 return -EINVAL;
2615}
2616EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2617
2618/**
2619 * snd_soc_dai_set_pll - configure DAI PLL.
2620 * @dai: DAI
2621 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002622 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002623 * @freq_in: PLL input clock frequency in Hz
2624 * @freq_out: requested PLL output clock frequency in Hz
2625 *
2626 * Configures and enables PLL to generate output clock based on input clock.
2627 */
Mark Brown85488032009-09-05 18:52:16 +01002628int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2629 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002630{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002631 if (dai->driver && dai->driver->ops->set_pll)
2632 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002633 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002634 else if (dai->codec && dai->codec->driver->set_pll)
2635 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2636 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002637 else
2638 return -EINVAL;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2641
Mark Brownec4ee522011-03-07 20:58:11 +00002642/*
2643 * snd_soc_codec_set_pll - configure codec PLL.
2644 * @codec: CODEC
2645 * @pll_id: DAI specific PLL ID
2646 * @source: DAI specific source for the PLL
2647 * @freq_in: PLL input clock frequency in Hz
2648 * @freq_out: requested PLL output clock frequency in Hz
2649 *
2650 * Configures and enables PLL to generate output clock based on input clock.
2651 */
2652int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2653 unsigned int freq_in, unsigned int freq_out)
2654{
2655 if (codec->driver->set_pll)
2656 return codec->driver->set_pll(codec, pll_id, source,
2657 freq_in, freq_out);
2658 else
2659 return -EINVAL;
2660}
2661EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2662
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002663/**
2664 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2665 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002666 * @fmt: SND_SOC_DAIFMT_ format value.
2667 *
2668 * Configures the DAI hardware format and clocking.
2669 */
2670int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2671{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002672 if (dai->driver && dai->driver->ops->set_fmt)
2673 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002674 else
2675 return -EINVAL;
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2678
2679/**
2680 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2681 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002682 * @tx_mask: bitmask representing active TX slots.
2683 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002684 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002685 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002686 *
2687 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2688 * specific.
2689 */
2690int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002691 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002692{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002693 if (dai->driver && dai->driver->ops->set_tdm_slot)
2694 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002695 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002696 else
2697 return -EINVAL;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2700
2701/**
Barry Song472df3c2009-09-12 01:16:29 +08002702 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2703 * @dai: DAI
2704 * @tx_num: how many TX channels
2705 * @tx_slot: pointer to an array which imply the TX slot number channel
2706 * 0~num-1 uses
2707 * @rx_num: how many RX channels
2708 * @rx_slot: pointer to an array which imply the RX slot number channel
2709 * 0~num-1 uses
2710 *
2711 * configure the relationship between channel number and TDM slot number.
2712 */
2713int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2714 unsigned int tx_num, unsigned int *tx_slot,
2715 unsigned int rx_num, unsigned int *rx_slot)
2716{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002717 if (dai->driver && dai->driver->ops->set_channel_map)
2718 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002719 rx_num, rx_slot);
2720 else
2721 return -EINVAL;
2722}
2723EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2724
2725/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2727 * @dai: DAI
2728 * @tristate: tristate enable
2729 *
2730 * Tristates the DAI so that others can use it.
2731 */
2732int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2733{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002734 if (dai->driver && dai->driver->ops->set_tristate)
2735 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002736 else
2737 return -EINVAL;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2740
2741/**
2742 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2743 * @dai: DAI
2744 * @mute: mute enable
2745 *
2746 * Mutes the DAI DAC.
2747 */
2748int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2749{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002750 if (dai->driver && dai->driver->ops->digital_mute)
2751 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002752 else
2753 return -EINVAL;
2754}
2755EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2756
Mark Brownc5af3a22008-11-28 13:29:45 +00002757/**
2758 * snd_soc_register_card - Register a card with the ASoC core
2759 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002760 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002761 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002762 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302763int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002764{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002765 int i;
2766
Mark Brownc5af3a22008-11-28 13:29:45 +00002767 if (!card->name || !card->dev)
2768 return -EINVAL;
2769
Mark Browned77cc12011-05-03 18:25:34 +01002770 dev_set_drvdata(card->dev, card);
2771
Stephen Warren111c6412011-01-28 14:26:35 -07002772 snd_soc_initialize_card_lists(card);
2773
Vinod Koul150dd2f2011-01-13 22:48:32 +05302774 soc_init_card_debugfs(card);
2775
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002776 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2777 (card->num_links + card->num_aux_devs),
2778 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002779 if (card->rtd == NULL)
2780 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002781 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002782
2783 for (i = 0; i < card->num_links; i++)
2784 card->rtd[i].dai_link = &card->dai_link[i];
2785
Mark Brownc5af3a22008-11-28 13:29:45 +00002786 INIT_LIST_HEAD(&card->list);
2787 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002788 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002789
2790 mutex_lock(&client_mutex);
2791 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002792 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002793 mutex_unlock(&client_mutex);
2794
2795 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2796
2797 return 0;
2798}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302799EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002800
2801/**
2802 * snd_soc_unregister_card - Unregister a card with the ASoC core
2803 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002804 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002805 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002806 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302807int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002808{
Vinod Koulb0e26482011-01-13 22:48:02 +05302809 if (card->instantiated)
2810 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002811 mutex_lock(&client_mutex);
2812 list_del(&card->list);
2813 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002814 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2815
2816 return 0;
2817}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302818EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002819
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002820/*
2821 * Simplify DAI link configuration by removing ".-1" from device names
2822 * and sanitizing names.
2823 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002824static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002825{
2826 char *found, name[NAME_SIZE];
2827 int id1, id2;
2828
2829 if (dev_name(dev) == NULL)
2830 return NULL;
2831
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002832 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002833
2834 /* are we a "%s.%d" name (platform and SPI components) */
2835 found = strstr(name, dev->driver->name);
2836 if (found) {
2837 /* get ID */
2838 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2839
2840 /* discard ID from name if ID == -1 */
2841 if (*id == -1)
2842 found[strlen(dev->driver->name)] = '\0';
2843 }
2844
2845 } else {
2846 /* I2C component devices are named "bus-addr" */
2847 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2848 char tmp[NAME_SIZE];
2849
2850 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002851 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002852
2853 /* sanitize component name for DAI link creation */
2854 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002855 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002856 } else
2857 *id = 0;
2858 }
2859
2860 return kstrdup(name, GFP_KERNEL);
2861}
2862
2863/*
2864 * Simplify DAI link naming for single devices with multiple DAIs by removing
2865 * any ".-1" and using the DAI name (instead of device name).
2866 */
2867static inline char *fmt_multiple_name(struct device *dev,
2868 struct snd_soc_dai_driver *dai_drv)
2869{
2870 if (dai_drv->name == NULL) {
2871 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2872 dev_name(dev));
2873 return NULL;
2874 }
2875
2876 return kstrdup(dai_drv->name, GFP_KERNEL);
2877}
2878
Mark Brown91151712008-11-30 23:31:24 +00002879/**
2880 * snd_soc_register_dai - Register a DAI with the ASoC core
2881 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002882 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002883 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002884int snd_soc_register_dai(struct device *dev,
2885 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002886{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002887 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002888
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002889 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002891 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2892 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08002893 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002894
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002895 /* create DAI component name */
2896 dai->name = fmt_single_name(dev, &dai->id);
2897 if (dai->name == NULL) {
2898 kfree(dai);
2899 return -ENOMEM;
2900 }
2901
2902 dai->dev = dev;
2903 dai->driver = dai_drv;
2904 if (!dai->driver->ops)
2905 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002906
2907 mutex_lock(&client_mutex);
2908 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002909 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002910 mutex_unlock(&client_mutex);
2911
2912 pr_debug("Registered DAI '%s'\n", dai->name);
2913
2914 return 0;
2915}
2916EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2917
2918/**
2919 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2920 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002921 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002922 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002923void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002924{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002925 struct snd_soc_dai *dai;
2926
2927 list_for_each_entry(dai, &dai_list, list) {
2928 if (dev == dai->dev)
2929 goto found;
2930 }
2931 return;
2932
2933found:
Mark Brown91151712008-11-30 23:31:24 +00002934 mutex_lock(&client_mutex);
2935 list_del(&dai->list);
2936 mutex_unlock(&client_mutex);
2937
2938 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939 kfree(dai->name);
2940 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002941}
2942EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2943
2944/**
2945 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2946 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002947 * @dai: Array of DAIs to register
2948 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002949 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002950int snd_soc_register_dais(struct device *dev,
2951 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002952{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953 struct snd_soc_dai *dai;
2954 int i, ret = 0;
2955
Liam Girdwood720ffa42010-08-18 00:30:30 +01002956 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002957
2958 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002959
2960 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08002961 if (dai == NULL) {
2962 ret = -ENOMEM;
2963 goto err;
2964 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002965
2966 /* create DAI component name */
2967 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2968 if (dai->name == NULL) {
2969 kfree(dai);
2970 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00002971 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002972 }
2973
2974 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002975 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01002976 if (dai->driver->id)
2977 dai->id = dai->driver->id;
2978 else
2979 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 if (!dai->driver->ops)
2981 dai->driver->ops = &null_dai_ops;
2982
2983 mutex_lock(&client_mutex);
2984 list_add(&dai->list, &dai_list);
2985 mutex_unlock(&client_mutex);
2986
2987 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002988 }
2989
Axel Lin1dcb4f32010-12-06 16:48:03 +08002990 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002991 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08002992 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00002993 return 0;
2994
2995err:
2996 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002997 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00002998
2999 return ret;
3000}
3001EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3002
3003/**
3004 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3005 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003006 * @dai: Array of DAIs to unregister
3007 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003008 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003010{
3011 int i;
3012
3013 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003014 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003015}
3016EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3017
Mark Brown12a48a8c2008-12-03 19:40:30 +00003018/**
3019 * snd_soc_register_platform - Register a platform with the ASoC core
3020 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003021 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003022 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003023int snd_soc_register_platform(struct device *dev,
3024 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003025{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003026 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003027
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003028 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3029
3030 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3031 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003032 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003033
3034 /* create platform component name */
3035 platform->name = fmt_single_name(dev, &platform->id);
3036 if (platform->name == NULL) {
3037 kfree(platform);
3038 return -ENOMEM;
3039 }
3040
3041 platform->dev = dev;
3042 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003043
3044 mutex_lock(&client_mutex);
3045 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003046 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003047 mutex_unlock(&client_mutex);
3048
3049 pr_debug("Registered platform '%s'\n", platform->name);
3050
3051 return 0;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3054
3055/**
3056 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3057 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003058 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003059 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003060void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003061{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003062 struct snd_soc_platform *platform;
3063
3064 list_for_each_entry(platform, &platform_list, list) {
3065 if (dev == platform->dev)
3066 goto found;
3067 }
3068 return;
3069
3070found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003071 mutex_lock(&client_mutex);
3072 list_del(&platform->list);
3073 mutex_unlock(&client_mutex);
3074
3075 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003076 kfree(platform->name);
3077 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003078}
3079EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3080
Mark Brown151ab222009-05-09 16:22:58 +01003081static u64 codec_format_map[] = {
3082 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3083 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3084 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3085 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3086 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3087 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3088 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3089 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3090 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3091 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3092 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3093 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3094 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3095 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3096 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3097 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3098};
3099
3100/* Fix up the DAI formats for endianness: codecs don't actually see
3101 * the endianness of the data but we're using the CPU format
3102 * definitions which do need to include endianness so we ensure that
3103 * codec DAIs always have both big and little endian variants set.
3104 */
3105static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3106{
3107 int i;
3108
3109 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3110 if (stream->formats & codec_format_map[i])
3111 stream->formats |= codec_format_map[i];
3112}
3113
Mark Brown0d0cf002008-12-10 14:32:45 +00003114/**
3115 * snd_soc_register_codec - Register a codec with the ASoC core
3116 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003117 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003118 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003119int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003120 const struct snd_soc_codec_driver *codec_drv,
3121 struct snd_soc_dai_driver *dai_drv,
3122 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003123{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003124 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 struct snd_soc_codec *codec;
3126 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003128 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003129
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3131 if (codec == NULL)
3132 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003134 /* create CODEC component name */
3135 codec->name = fmt_single_name(dev, &codec->id);
3136 if (codec->name == NULL) {
3137 kfree(codec);
3138 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003139 }
3140
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003141 if (codec_drv->compress_type)
3142 codec->compress_type = codec_drv->compress_type;
3143 else
3144 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3145
Mark Brownc3acec22010-12-02 16:15:29 +00003146 codec->write = codec_drv->write;
3147 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003148 codec->volatile_register = codec_drv->volatile_register;
3149 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003150 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003151 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3152 codec->dapm.dev = dev;
3153 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003154 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155 codec->dev = dev;
3156 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003157 codec->num_dai = num_dai;
3158 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003159
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003160 /* allocate CODEC register cache */
3161 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003162 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003163 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003164 /* it is necessary to make a copy of the default register cache
3165 * because in the case of using a compression type that requires
3166 * the default register cache to be marked as __devinitconst the
3167 * kernel might have freed the array by the time we initialize
3168 * the cache.
3169 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003170 if (codec_drv->reg_cache_default) {
3171 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3172 reg_size, GFP_KERNEL);
3173 if (!codec->reg_def_copy) {
3174 ret = -ENOMEM;
3175 goto fail;
3176 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003177 }
3178 }
3179
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003180 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3181 if (!codec->volatile_register)
3182 codec->volatile_register = snd_soc_default_volatile_register;
3183 if (!codec->readable_register)
3184 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003185 if (!codec->writable_register)
3186 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003187 }
3188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003189 for (i = 0; i < num_dai; i++) {
3190 fixup_codec_formats(&dai_drv[i].playback);
3191 fixup_codec_formats(&dai_drv[i].capture);
3192 }
3193
Mark Brown26b01cc2010-08-18 20:20:55 +01003194 /* register any DAIs */
3195 if (num_dai) {
3196 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3197 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003198 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003199 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003200
Mark Brown0d0cf002008-12-10 14:32:45 +00003201 mutex_lock(&client_mutex);
3202 list_add(&codec->list, &codec_list);
3203 snd_soc_instantiate_cards();
3204 mutex_unlock(&client_mutex);
3205
3206 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003207 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003208
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003209fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003210 kfree(codec->reg_def_copy);
3211 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003212 kfree(codec->name);
3213 kfree(codec);
3214 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003215}
3216EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3217
3218/**
3219 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3220 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003221 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003222 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003223void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003224{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225 struct snd_soc_codec *codec;
3226 int i;
3227
3228 list_for_each_entry(codec, &codec_list, list) {
3229 if (dev == codec->dev)
3230 goto found;
3231 }
3232 return;
3233
3234found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003235 if (codec->num_dai)
3236 for (i = 0; i < codec->num_dai; i++)
3237 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003238
Mark Brown0d0cf002008-12-10 14:32:45 +00003239 mutex_lock(&client_mutex);
3240 list_del(&codec->list);
3241 mutex_unlock(&client_mutex);
3242
3243 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003244
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003245 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003246 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003247 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003249}
3250EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3251
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003252static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003253{
Mark Brown384c89e2008-12-03 17:34:03 +00003254#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003255 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3256 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003257 printk(KERN_WARNING
3258 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003259 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003260 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003261
Mark Brown8a9dab12011-01-10 22:25:21 +00003262 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003263 &codec_list_fops))
3264 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3265
Mark Brown8a9dab12011-01-10 22:25:21 +00003266 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003267 &dai_list_fops))
3268 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003269
Mark Brown8a9dab12011-01-10 22:25:21 +00003270 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003271 &platform_list_fops))
3272 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003273#endif
3274
Mark Brownfb257892011-04-28 10:57:54 +01003275 snd_soc_util_init();
3276
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003277 return platform_driver_register(&soc_driver);
3278}
Mark Brown4abe8e12010-10-12 17:41:03 +01003279module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003280
Mark Brown7d8c16a2008-11-30 22:11:24 +00003281static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003282{
Mark Brownfb257892011-04-28 10:57:54 +01003283 snd_soc_util_exit();
3284
Mark Brown384c89e2008-12-03 17:34:03 +00003285#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003286 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003287#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003288 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003289}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003290module_exit(snd_soc_exit);
3291
3292/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003293MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003294MODULE_DESCRIPTION("ALSA SoC Core");
3295MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003296MODULE_ALIAS("platform:soc-audio");