blob: e19c24ade414a3eef34073f6f98da139c8ff8b5a [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>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
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
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 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) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_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{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000205 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000206{
207 ssize_t ret;
208 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 char *buf;
210
211 if (*ppos < 0 || !count)
212 return -EINVAL;
213
214 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000215 if (!buf)
216 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000217
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 if (ret >= 0) {
220 if (copy_to_user(user_buf, buf, ret)) {
221 kfree(buf);
222 return -EFAULT;
223 }
224 *ppos += ret;
225 }
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 kfree(buf);
228 return ret;
229}
230
231static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
233{
234 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700235 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 char *start = buf;
237 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 struct snd_soc_codec *codec = file->private_data;
239
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
242 return -EFAULT;
243 buf[buf_size] = 0;
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 while (*start == ' ')
246 start++;
247 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 while (*start == ' ')
249 start++;
250 if (strict_strtoul(start, 16, &value))
251 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000252
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
255
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000256 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 return buf_size;
258}
259
260static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200264 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265};
266
267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000274 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 return;
276 }
277
Mark Brownaaee8ef2011-01-26 20:53:50 +0000278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279 &codec->cache_sync);
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281 &codec->cache_only);
282
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000287 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000288
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200289 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290}
291
292static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
293{
294 debugfs_remove_recursive(codec->debugfs_codec_root);
295}
296
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000297static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298{
299 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300
301 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302 debugfs_card_root);
303 if (!platform->debugfs_platform_root) {
304 dev_warn(platform->dev,
305 "Failed to create platform debugfs directory\n");
306 return;
307 }
308
309 snd_soc_dapm_debugfs_init(&platform->dapm,
310 platform->debugfs_platform_root);
311}
312
313static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314{
315 debugfs_remove_recursive(platform->debugfs_platform_root);
316}
317
Mark Brownc3c5a192010-09-15 18:15:14 +0100318static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319 size_t count, loff_t *ppos)
320{
321 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100322 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100323 struct snd_soc_codec *codec;
324
325 if (!buf)
326 return -ENOMEM;
327
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 list_for_each_entry(codec, &codec_list, list) {
329 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330 codec->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100338
339 if (ret >= 0)
340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341
342 kfree(buf);
343
344 return ret;
345}
346
347static const struct file_operations codec_list_fops = {
348 .read = codec_list_read_file,
349 .llseek = default_llseek,/* read accesses f_pos */
350};
351
Mark Brownf3208782010-09-15 18:19:07 +0100352static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100357 struct snd_soc_dai *dai;
358
359 if (!buf)
360 return -ENOMEM;
361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 list_for_each_entry(dai, &dai_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
364 if (len >= 0)
365 ret += len;
366 if (ret > PAGE_SIZE) {
367 ret = PAGE_SIZE;
368 break;
369 }
370 }
Mark Brownf3208782010-09-15 18:19:07 +0100371
Mark Brown2b194f9d2010-10-13 10:52:16 +0100372 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100373
374 kfree(buf);
375
376 return ret;
377}
378
379static const struct file_operations dai_list_fops = {
380 .read = dai_list_read_file,
381 .llseek = default_llseek,/* read accesses f_pos */
382};
383
Mark Brown19c7ac22010-09-15 18:22:40 +0100384static ssize_t platform_list_read_file(struct file *file,
385 char __user *user_buf,
386 size_t count, loff_t *ppos)
387{
388 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100390 struct snd_soc_platform *platform;
391
392 if (!buf)
393 return -ENOMEM;
394
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 list_for_each_entry(platform, &platform_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 platform->name);
398 if (len >= 0)
399 ret += len;
400 if (ret > PAGE_SIZE) {
401 ret = PAGE_SIZE;
402 break;
403 }
404 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations platform_list_fops = {
414 .read = platform_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200418static void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000421 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200422 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100424 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200425 return;
426 }
427
428 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
429 card->debugfs_card_root,
430 &card->pop_time);
431 if (!card->debugfs_pop_time)
432 dev_warn(card->dev,
433 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434}
435
436static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437{
438 debugfs_remove_recursive(card->debugfs_card_root);
439}
440
Mark Brown2624d5f2009-11-03 21:56:13 +0000441#else
442
443static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
444{
445}
446
447static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
448{
449}
Axel Linb95fccb2010-11-09 17:06:44 +0800450
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000451static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452{
453}
454
455static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456{
457}
458
Axel Linb95fccb2010-11-09 17:06:44 +0800459static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460{
461}
462
463static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464{
465}
Mark Brown2624d5f2009-11-03 21:56:13 +0000466#endif
467
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468#ifdef CONFIG_SND_SOC_AC97_BUS
469/* unregister ac97 codec */
470static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
471{
472 if (codec->ac97->dev.bus)
473 device_unregister(&codec->ac97->dev);
474 return 0;
475}
476
477/* stop no dev release warning */
478static void soc_ac97_device_release(struct device *dev){}
479
480/* register ac97 codec to bus */
481static int soc_ac97_dev_register(struct snd_soc_codec *codec)
482{
483 int err;
484
485 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100486 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 codec->ac97->dev.release = soc_ac97_device_release;
488
Kay Sieversbb072bf2008-11-02 03:50:35 +0100489 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 err = device_register(&codec->ac97->dev);
492 if (err < 0) {
493 snd_printk(KERN_ERR "Can't register ac97 bus\n");
494 codec->ac97->dev.bus = NULL;
495 return err;
496 }
497 return 0;
498}
499#endif
500
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000501#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000503int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000505 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200506 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 int i;
508
Daniel Macke3509ff2009-06-03 17:44:49 +0200509 /* If the initialization of this soc device failed, there is no codec
510 * associated with it. Just bail out in this case.
511 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200513 return 0;
514
Andy Green6ed25972008-06-13 16:24:05 +0100515 /* Due to the resume being scheduled into a workqueue we could
516 * suspend before that's finished - wait for it to complete.
517 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 snd_power_lock(card->snd_card);
519 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
520 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100521
522 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100524
Mark Browna00f90f2010-12-02 16:24:24 +0000525 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 for (i = 0; i < card->num_rtd; i++) {
527 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
528 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100529
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100531 continue;
532
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 if (drv->ops->digital_mute && dai->playback_active)
534 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 }
536
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100537 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 for (i = 0; i < card->num_rtd; i++) {
539 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100540 continue;
541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100543 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100544
Mark Brown87506542008-11-18 20:50:34 +0000545 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000546 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 for (i = 0; i < card->num_rtd; i++) {
549 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
550 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100553 continue;
554
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
556 cpu_dai->driver->suspend(cpu_dai);
557 if (platform->driver->suspend && !platform->suspended) {
558 platform->driver->suspend(cpu_dai);
559 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100560 }
561 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 /* close any waiting streams and save state */
564 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100565 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200566 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800570 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571
572 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100573 continue;
574
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800575 snd_soc_dapm_stream_event(&card->rtd[i],
576 SNDRV_PCM_STREAM_PLAYBACK,
577 codec_dai,
578 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800580 snd_soc_dapm_stream_event(&card->rtd[i],
581 SNDRV_PCM_STREAM_CAPTURE,
582 codec_dai,
583 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 }
585
586 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200587 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 /* If there are paths active then the CODEC will be held with
589 * bias _ON and should not be suspended. */
590 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200591 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000593 /*
594 * If the CODEC is capable of idle
595 * bias off then being in STANDBY
596 * means it's doing something,
597 * otherwise fall through.
598 */
599 if (codec->dapm.idle_bias_off) {
600 dev_dbg(codec->dev,
601 "idle_bias_off CODEC on over suspend\n");
602 break;
603 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100605 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900607 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 break;
609 default:
610 dev_dbg(codec->dev, "CODEC is on over suspend\n");
611 break;
612 }
613 }
614 }
615
616 for (i = 0; i < card->num_rtd; i++) {
617 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
618
619 if (card->rtd[i].dai_link->ignore_suspend)
620 continue;
621
622 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
623 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624 }
625
Mark Brown87506542008-11-18 20:50:34 +0000626 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000627 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628
629 return 0;
630}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000631EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632
Andy Green6ed25972008-06-13 16:24:05 +0100633/* deferred resume work, so resume can complete before we finished
634 * setting our codec back up, which can be very slow on I2C
635 */
636static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 struct snd_soc_card *card =
639 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200640 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 int i;
642
Andy Green6ed25972008-06-13 16:24:05 +0100643 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
644 * so userspace apps are blocked from touching us
645 */
646
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100648
Mark Brown99497882010-05-07 20:24:05 +0100649 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000653 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 /* resume AC97 DAIs */
656 for (i = 0; i < card->num_rtd; i++) {
657 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100660 continue;
661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
663 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 }
665
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200666 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 /* If the CODEC was idle over suspend then it will have been
668 * left with bias OFF or STANDBY and suspended so we must now
669 * resume. Otherwise the suspend was suppressed.
670 */
671 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200672 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 case SND_SOC_BIAS_STANDBY:
674 case SND_SOC_BIAS_OFF:
675 codec->driver->resume(codec);
676 codec->suspended = 0;
677 break;
678 default:
679 dev_dbg(codec->dev, "CODEC was on over suspend\n");
680 break;
681 }
Mark Brown1547aba2010-05-07 21:11:40 +0100682 }
683 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800686 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
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
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800691 snd_soc_dapm_stream_event(&card->rtd[i],
692 SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
693 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800695 snd_soc_dapm_stream_event(&card->rtd[i],
696 SNDRV_PCM_STREAM_CAPTURE, codec_dai,
697 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698 }
699
Mark Brown3ff3f642008-05-19 12:32:25 +0200700 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 for (i = 0; i < card->num_rtd; i++) {
702 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
703 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100706 continue;
707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 if (drv->ops->digital_mute && dai->playback_active)
709 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 }
711
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 for (i = 0; i < card->num_rtd; i++) {
713 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
714 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100717 continue;
718
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
720 cpu_dai->driver->resume(cpu_dai);
721 if (platform->driver->resume && platform->suspended) {
722 platform->driver->resume(cpu_dai);
723 platform->suspended = 0;
724 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725 }
726
Mark Brown87506542008-11-18 20:50:34 +0000727 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000728 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100731
732 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100734}
735
736/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000737int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100738{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000739 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600740 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200741
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800742 /* If the initialization of this soc device failed, there is no codec
743 * associated with it. Just bail out in this case.
744 */
745 if (list_empty(&card->codec_dev_list))
746 return 0;
747
Mark Brown64ab9ba2009-03-31 11:27:03 +0100748 /* AC97 devices might have other drivers hanging off them so
749 * need to resume immediately. Other drivers don't have that
750 * problem and may take a substantial amount of time to resume
751 * due to I/O costs and anti-pop so handle them out of line.
752 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000753 for (i = 0; i < card->num_rtd; i++) {
754 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600755 ac97_control |= cpu_dai->driver->ac97_control;
756 }
757 if (ac97_control) {
758 dev_dbg(dev, "Resuming AC97 immediately\n");
759 soc_resume_deferred(&card->deferred_resume_work);
760 } else {
761 dev_dbg(dev, "Scheduling resume work\n");
762 if (!schedule_work(&card->deferred_resume_work))
763 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100764 }
Andy Green6ed25972008-06-13 16:24:05 +0100765
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766 return 0;
767}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000768EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000770#define snd_soc_suspend NULL
771#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772#endif
773
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100774static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800775};
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200778{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
780 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000781 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000782 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100784 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 if (rtd->complete)
787 return 1;
788 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000789
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 /* do we already have the CPU DAI for this link ? */
791 if (rtd->cpu_dai) {
792 goto find_codec;
793 }
794 /* no, then find CPU DAI from registered DAIs*/
795 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700796 if (dai_link->cpu_dai_of_node) {
797 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
798 continue;
799 } else {
800 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
801 continue;
802 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700803
804 rtd->cpu_dai = cpu_dai;
805 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 }
807 dev_dbg(card->dev, "CPU DAI %s not registered\n",
808 dai_link->cpu_dai_name);
809
810find_codec:
811 /* do we already have the CODEC for this link ? */
812 if (rtd->codec) {
813 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000814 }
815
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000816 /* no, then find CODEC from registered CODECs*/
817 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700818 if (dai_link->codec_of_node) {
819 if (codec->dev->of_node != dai_link->codec_of_node)
820 continue;
821 } else {
822 if (strcmp(codec->name, dai_link->codec_name))
823 continue;
824 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000825
Stephen Warren2610ab72011-12-07 13:58:27 -0700826 rtd->codec = codec;
827
828 /*
829 * CODEC found, so find CODEC DAI from registered DAIs from
830 * this CODEC
831 */
832 list_for_each_entry(codec_dai, &dai_list, list) {
833 if (codec->dev == codec_dai->dev &&
834 !strcmp(codec_dai->name,
835 dai_link->codec_dai_name)) {
836
837 rtd->codec_dai = codec_dai;
838 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000840 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700841 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
842 dai_link->codec_dai_name);
843
844 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 }
846 dev_dbg(card->dev, "CODEC %s not registered\n",
847 dai_link->codec_name);
848
849find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100850 /* do we need a platform? */
851 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100853
854 /* if there's no platform we match on the empty platform */
855 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700856 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100857 platform_name = "snd-soc-dummy";
858
859 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700861 if (dai_link->platform_of_node) {
862 if (platform->dev->of_node !=
863 dai_link->platform_of_node)
864 continue;
865 } else {
866 if (strcmp(platform->name, platform_name))
867 continue;
868 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700869
870 rtd->platform = platform;
871 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 }
873
874 dev_dbg(card->dev, "platform %s not registered\n",
875 dai_link->platform_name);
876 return 0;
877
878out:
879 /* mark rtd as complete if we found all 4 of our client devices */
880 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
881 rtd->complete = 1;
882 card->num_rtd++;
883 }
884 return 1;
885}
886
Jarkko Nikula589c3562010-12-06 16:27:07 +0200887static void soc_remove_codec(struct snd_soc_codec *codec)
888{
889 int err;
890
891 if (codec->driver->remove) {
892 err = codec->driver->remove(codec);
893 if (err < 0)
894 dev_err(codec->dev,
895 "asoc: failed to remove %s: %d\n",
896 codec->name, err);
897 }
898
899 /* Make sure all DAPM widgets are freed */
900 snd_soc_dapm_free(&codec->dapm);
901
902 soc_cleanup_codec_debugfs(codec);
903 codec->probed = 0;
904 list_del(&codec->card_list);
905 module_put(codec->dev->driver->owner);
906}
907
Liam Girdwood0168bf02011-06-07 16:08:05 +0100908static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909{
910 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
911 struct snd_soc_codec *codec = rtd->codec;
912 struct snd_soc_platform *platform = rtd->platform;
913 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
914 int err;
915
916 /* unregister the rtd device */
917 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800918 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
919 device_remove_file(rtd->dev, &dev_attr_codec_reg);
920 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 rtd->dev_registered = 0;
922 }
923
924 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100925 if (codec_dai && codec_dai->probed &&
926 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 if (codec_dai->driver->remove) {
928 err = codec_dai->driver->remove(codec_dai);
929 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200930 pr_err("asoc: failed to remove %s: %d\n",
931 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 }
933 codec_dai->probed = 0;
934 list_del(&codec_dai->card_list);
935 }
936
937 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100938 if (platform && platform->probed &&
939 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000940 if (platform->driver->remove) {
941 err = platform->driver->remove(platform);
942 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200943 pr_err("asoc: failed to remove %s: %d\n",
944 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000946
947 /* Make sure all DAPM widgets are freed */
948 snd_soc_dapm_free(&platform->dapm);
949
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000950 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 platform->probed = 0;
952 list_del(&platform->card_list);
953 module_put(platform->dev->driver->owner);
954 }
955
956 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100957 if (codec && codec->probed &&
958 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200959 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960
961 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100962 if (cpu_dai && cpu_dai->probed &&
963 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 if (cpu_dai->driver->remove) {
965 err = cpu_dai->driver->remove(cpu_dai);
966 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200967 pr_err("asoc: failed to remove %s: %d\n",
968 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 }
970 cpu_dai->probed = 0;
971 list_del(&cpu_dai->card_list);
972 module_put(cpu_dai->dev->driver->owner);
973 }
974}
975
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900976static void soc_remove_dai_links(struct snd_soc_card *card)
977{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100978 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900979
Liam Girdwood0168bf02011-06-07 16:08:05 +0100980 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
981 order++) {
982 for (dai = 0; dai < card->num_rtd; dai++)
983 soc_remove_dai_link(card, dai, order);
984 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900985 card->num_rtd = 0;
986}
987
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200988static void soc_set_name_prefix(struct snd_soc_card *card,
989 struct snd_soc_codec *codec)
990{
991 int i;
992
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000993 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200994 return;
995
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000996 for (i = 0; i < card->num_configs; i++) {
997 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200998 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
999 codec->name_prefix = map->name_prefix;
1000 break;
1001 }
1002 }
1003}
1004
Jarkko Nikula589c3562010-12-06 16:27:07 +02001005static int soc_probe_codec(struct snd_soc_card *card,
1006 struct snd_soc_codec *codec)
1007{
1008 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001009 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001010 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001011
1012 codec->card = card;
1013 codec->dapm.card = card;
1014 soc_set_name_prefix(card, codec);
1015
Jarkko Nikula70d29332011-01-27 16:24:22 +02001016 if (!try_module_get(codec->dev->driver->owner))
1017 return -ENODEV;
1018
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001019 soc_init_codec_debugfs(codec);
1020
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001021 if (driver->dapm_widgets)
1022 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1023 driver->num_dapm_widgets);
1024
Mark Brown888df392012-02-16 19:37:51 -08001025 /* Create DAPM widgets for each DAI stream */
1026 list_for_each_entry(dai, &dai_list, list) {
1027 if (dai->dev != codec->dev)
1028 continue;
1029
1030 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1031 }
1032
Mark Brown33c5f962011-08-22 18:40:30 +01001033 codec->dapm.idle_bias_off = driver->idle_bias_off;
1034
Mark Brown89b95ac02011-03-07 16:38:44 +00001035 if (driver->probe) {
1036 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001037 if (ret < 0) {
1038 dev_err(codec->dev,
1039 "asoc: failed to probe CODEC %s: %d\n",
1040 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001041 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001042 }
1043 }
1044
Mark Brownb7af1da2011-04-07 19:18:44 +09001045 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001046 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001047 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001048 if (driver->dapm_routes)
1049 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1050 driver->num_dapm_routes);
1051
Jarkko Nikula589c3562010-12-06 16:27:07 +02001052 /* mark codec as probed and add to card codec list */
1053 codec->probed = 1;
1054 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001055 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001056
Jarkko Nikula70d29332011-01-27 16:24:22 +02001057 return 0;
1058
1059err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001060 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001061 module_put(codec->dev->driver->owner);
1062
Jarkko Nikula589c3562010-12-06 16:27:07 +02001063 return ret;
1064}
1065
Liam Girdwood956245e2011-07-01 16:54:08 +01001066static int soc_probe_platform(struct snd_soc_card *card,
1067 struct snd_soc_platform *platform)
1068{
1069 int ret = 0;
1070 const struct snd_soc_platform_driver *driver = platform->driver;
1071
1072 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001073 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001074
1075 if (!try_module_get(platform->dev->driver->owner))
1076 return -ENODEV;
1077
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001078 soc_init_platform_debugfs(platform);
1079
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001080 if (driver->dapm_widgets)
1081 snd_soc_dapm_new_controls(&platform->dapm,
1082 driver->dapm_widgets, driver->num_dapm_widgets);
1083
Liam Girdwood956245e2011-07-01 16:54:08 +01001084 if (driver->probe) {
1085 ret = driver->probe(platform);
1086 if (ret < 0) {
1087 dev_err(platform->dev,
1088 "asoc: failed to probe platform %s: %d\n",
1089 platform->name, ret);
1090 goto err_probe;
1091 }
1092 }
1093
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001094 if (driver->controls)
1095 snd_soc_add_platform_controls(platform, driver->controls,
1096 driver->num_controls);
1097 if (driver->dapm_routes)
1098 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1099 driver->num_dapm_routes);
1100
Liam Girdwood956245e2011-07-01 16:54:08 +01001101 /* mark platform as probed and add to card platform list */
1102 platform->probed = 1;
1103 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001104 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001105
1106 return 0;
1107
1108err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001109 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001110 module_put(platform->dev->driver->owner);
1111
1112 return ret;
1113}
1114
Mark Brown36ae1a92012-01-06 17:12:45 -08001115static void rtd_release(struct device *dev)
1116{
1117 kfree(dev);
1118}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001119
Jarkko Nikula589c3562010-12-06 16:27:07 +02001120static int soc_post_component_init(struct snd_soc_card *card,
1121 struct snd_soc_codec *codec,
1122 int num, int dailess)
1123{
1124 struct snd_soc_dai_link *dai_link = NULL;
1125 struct snd_soc_aux_dev *aux_dev = NULL;
1126 struct snd_soc_pcm_runtime *rtd;
1127 const char *temp, *name;
1128 int ret = 0;
1129
1130 if (!dailess) {
1131 dai_link = &card->dai_link[num];
1132 rtd = &card->rtd[num];
1133 name = dai_link->name;
1134 } else {
1135 aux_dev = &card->aux_dev[num];
1136 rtd = &card->rtd_aux[num];
1137 name = aux_dev->name;
1138 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001139 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001140
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001141 /* Make sure all DAPM widgets are instantiated */
1142 snd_soc_dapm_new_widgets(&codec->dapm);
1143
Jarkko Nikula589c3562010-12-06 16:27:07 +02001144 /* machine controls, routes and widgets are not prefixed */
1145 temp = codec->name_prefix;
1146 codec->name_prefix = NULL;
1147
1148 /* do machine specific initialization */
1149 if (!dailess && dai_link->init)
1150 ret = dai_link->init(rtd);
1151 else if (dailess && aux_dev->init)
1152 ret = aux_dev->init(&codec->dapm);
1153 if (ret < 0) {
1154 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1155 return ret;
1156 }
1157 codec->name_prefix = temp;
1158
Jarkko Nikula589c3562010-12-06 16:27:07 +02001159 /* register the rtd device */
1160 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001161
1162 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1163 if (!rtd->dev)
1164 return -ENOMEM;
1165 device_initialize(rtd->dev);
1166 rtd->dev->parent = card->dev;
1167 rtd->dev->release = rtd_release;
1168 rtd->dev->init_name = name;
1169 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001170 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001171 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001172 if (ret < 0) {
1173 dev_err(card->dev,
1174 "asoc: failed to register runtime device: %d\n", ret);
1175 return ret;
1176 }
1177 rtd->dev_registered = 1;
1178
1179 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001180 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181 if (ret < 0)
1182 dev_err(codec->dev,
1183 "asoc: failed to add codec dapm sysfs entries: %d\n",
1184 ret);
1185
1186 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001187 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001188 if (ret < 0)
1189 dev_err(codec->dev,
1190 "asoc: failed to add codec sysfs files: %d\n", ret);
1191
1192 return 0;
1193}
1194
Liam Girdwood0168bf02011-06-07 16:08:05 +01001195static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001196{
1197 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1198 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1199 struct snd_soc_codec *codec = rtd->codec;
1200 struct snd_soc_platform *platform = rtd->platform;
1201 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1202 int ret;
1203
Liam Girdwood0168bf02011-06-07 16:08:05 +01001204 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1205 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206
1207 /* config components */
1208 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001210 codec_dai->card = card;
1211 cpu_dai->card = card;
1212
1213 /* set default power off timeout */
1214 rtd->pmdown_time = pmdown_time;
1215
1216 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001217 if (!cpu_dai->probed &&
1218 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001219 if (!try_module_get(cpu_dai->dev->driver->owner))
1220 return -ENODEV;
1221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222 if (cpu_dai->driver->probe) {
1223 ret = cpu_dai->driver->probe(cpu_dai);
1224 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001225 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1226 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001227 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 return ret;
1229 }
1230 }
1231 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001232 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1234 }
1235
1236 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001237 if (!codec->probed &&
1238 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001239 ret = soc_probe_codec(card, codec);
1240 if (ret < 0)
1241 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001242 }
1243
1244 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001245 if (!platform->probed &&
1246 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001247 ret = soc_probe_platform(card, platform);
1248 if (ret < 0)
1249 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001250 }
1251
1252 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001253 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 if (codec_dai->driver->probe) {
1255 ret = codec_dai->driver->probe(codec_dai);
1256 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001257 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1258 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001260 }
1261 }
1262
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001263 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001264 codec_dai->probed = 1;
1265 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001266 }
1267
Liam Girdwood0168bf02011-06-07 16:08:05 +01001268 /* complete DAI probe during last probe */
1269 if (order != SND_SOC_COMP_ORDER_LAST)
1270 return 0;
1271
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 ret = soc_post_component_init(card, codec, num, 0);
1273 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001275
Mark Brown36ae1a92012-01-06 17:12:45 -08001276 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001278 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 /* create the pcm */
1281 ret = soc_new_pcm(rtd, num);
1282 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001283 pr_err("asoc: can't create pcm %s :%d\n",
1284 dai_link->stream_name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001285 return ret;
1286 }
1287
1288 /* add platform data for AC97 devices */
1289 if (rtd->codec_dai->driver->ac97_control)
1290 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1291
1292 return 0;
1293}
1294
1295#ifdef CONFIG_SND_SOC_AC97_BUS
1296static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1297{
1298 int ret;
1299
1300 /* Only instantiate AC97 if not already done by the adaptor
1301 * for the generic AC97 subsystem.
1302 */
1303 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001304 /*
1305 * It is possible that the AC97 device is already registered to
1306 * the device subsystem. This happens when the device is created
1307 * via snd_ac97_mixer(). Currently only SoC codec that does so
1308 * is the generic AC97 glue but others migh emerge.
1309 *
1310 * In those cases we don't try to register the device again.
1311 */
1312 if (!rtd->codec->ac97_created)
1313 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001314
1315 ret = soc_ac97_dev_register(rtd->codec);
1316 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001317 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001318 return ret;
1319 }
1320
1321 rtd->codec->ac97_registered = 1;
1322 }
1323 return 0;
1324}
1325
1326static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1327{
1328 if (codec->ac97_registered) {
1329 soc_ac97_dev_unregister(codec);
1330 codec->ac97_registered = 0;
1331 }
1332}
1333#endif
1334
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001335static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1336{
1337 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001338 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001339 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001340
1341 /* find CODEC from registered CODECs*/
1342 list_for_each_entry(codec, &codec_list, list) {
1343 if (!strcmp(codec->name, aux_dev->codec_name)) {
1344 if (codec->probed) {
1345 dev_err(codec->dev,
1346 "asoc: codec already probed");
1347 ret = -EBUSY;
1348 goto out;
1349 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001350 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001351 }
1352 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001353 /* codec not found */
1354 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1355 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001356
Jarkko Nikula676ad982010-12-03 09:18:22 +02001357found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001358 ret = soc_probe_codec(card, codec);
1359 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001360 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001361
Jarkko Nikula589c3562010-12-06 16:27:07 +02001362 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001363
1364out:
1365 return ret;
1366}
1367
1368static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1369{
1370 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1371 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001372
1373 /* unregister the rtd device */
1374 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001375 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1376 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001377 rtd->dev_registered = 0;
1378 }
1379
Jarkko Nikula589c3562010-12-06 16:27:07 +02001380 if (codec && codec->probed)
1381 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001382}
1383
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001384static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1385 enum snd_soc_compress_type compress_type)
1386{
1387 int ret;
1388
1389 if (codec->cache_init)
1390 return 0;
1391
1392 /* override the compress_type if necessary */
1393 if (compress_type && codec->compress_type != compress_type)
1394 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001395 ret = snd_soc_cache_init(codec);
1396 if (ret < 0) {
1397 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1398 ret);
1399 return ret;
1400 }
1401 codec->cache_init = 1;
1402 return 0;
1403}
1404
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001405static void snd_soc_instantiate_card(struct snd_soc_card *card)
1406{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001407 struct snd_soc_codec *codec;
1408 struct snd_soc_codec_conf *codec_conf;
1409 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001410 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001411 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412
1413 mutex_lock(&card->mutex);
1414
1415 if (card->instantiated) {
1416 mutex_unlock(&card->mutex);
1417 return;
1418 }
1419
1420 /* bind DAIs */
1421 for (i = 0; i < card->num_links; i++)
1422 soc_bind_dai_link(card, i);
1423
1424 /* bind completed ? */
1425 if (card->num_rtd != card->num_links) {
1426 mutex_unlock(&card->mutex);
1427 return;
1428 }
1429
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001430 /* initialize the register cache for each available codec */
1431 list_for_each_entry(codec, &codec_list, list) {
1432 if (codec->cache_init)
1433 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001434 /* by default we don't override the compress_type */
1435 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001436 /* check to see if we need to override the compress_type */
1437 for (i = 0; i < card->num_configs; ++i) {
1438 codec_conf = &card->codec_conf[i];
1439 if (!strcmp(codec->name, codec_conf->dev_name)) {
1440 compress_type = codec_conf->compress_type;
1441 if (compress_type && compress_type
1442 != codec->compress_type)
1443 break;
1444 }
1445 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001446 ret = snd_soc_init_codec_cache(codec, compress_type);
1447 if (ret < 0) {
1448 mutex_unlock(&card->mutex);
1449 return;
1450 }
1451 }
1452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 /* card bind complete so register a sound card */
1454 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1455 card->owner, 0, &card->snd_card);
1456 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001457 pr_err("asoc: can't create sound card for card %s: %d\n",
1458 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001459 mutex_unlock(&card->mutex);
1460 return;
1461 }
1462 card->snd_card->dev = card->dev;
1463
Mark Browne37a4972011-03-02 18:21:57 +00001464 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1465 card->dapm.dev = card->dev;
1466 card->dapm.card = card;
1467 list_add(&card->dapm.list, &card->dapm_list);
1468
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001469#ifdef CONFIG_DEBUG_FS
1470 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1471#endif
1472
Mark Brown88ee1c62011-02-02 10:43:26 +00001473#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001474 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001475 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001476#endif
Andy Green6ed25972008-06-13 16:24:05 +01001477
Mark Brown9a841eb2011-04-12 17:51:37 -07001478 if (card->dapm_widgets)
1479 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1480 card->num_dapm_widgets);
1481
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482 /* initialise the sound card only once */
1483 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001484 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485 if (ret < 0)
1486 goto card_probe_error;
1487 }
1488
Liam Girdwood0168bf02011-06-07 16:08:05 +01001489 /* early DAI link probe */
1490 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1491 order++) {
1492 for (i = 0; i < card->num_links; i++) {
1493 ret = soc_probe_dai_link(card, i, order);
1494 if (ret < 0) {
1495 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001496 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001497 goto probe_dai_err;
1498 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 }
1500 }
1501
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001502 for (i = 0; i < card->num_aux_devs; i++) {
1503 ret = soc_probe_aux_dev(card, i);
1504 if (ret < 0) {
1505 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1506 card->name, ret);
1507 goto probe_aux_dev_err;
1508 }
1509 }
1510
Mark Brown888df392012-02-16 19:37:51 -08001511 snd_soc_dapm_link_dai_widgets(card);
1512
Mark Brownb7af1da2011-04-07 19:18:44 +09001513 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001514 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001515
Mark Brownb8ad29d2011-03-02 18:35:51 +00001516 if (card->dapm_routes)
1517 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1518 card->num_dapm_routes);
1519
Mark Brownb90d2f92011-10-10 13:38:06 +01001520 snd_soc_dapm_new_widgets(&card->dapm);
1521
Mark Brown75d9ac42011-09-27 16:41:01 +01001522 for (i = 0; i < card->num_links; i++) {
1523 dai_link = &card->dai_link[i];
1524
1525 if (dai_link->dai_fmt) {
1526 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1527 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001528 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001529 dev_warn(card->rtd[i].codec_dai->dev,
1530 "Failed to set DAI format: %d\n",
1531 ret);
1532
1533 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1534 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001535 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001536 dev_warn(card->rtd[i].cpu_dai->dev,
1537 "Failed to set DAI format: %d\n",
1538 ret);
1539 }
1540 }
1541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001544 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1545 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001546 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1547 "%s", card->driver_name ? card->driver_name : card->name);
1548 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1549 switch (card->snd_card->driver[i]) {
1550 case '_':
1551 case '-':
1552 case '\0':
1553 break;
1554 default:
1555 if (!isalnum(card->snd_card->driver[i]))
1556 card->snd_card->driver[i] = '_';
1557 break;
1558 }
1559 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560
Mark Brown28e9ad92011-03-02 18:36:34 +00001561 if (card->late_probe) {
1562 ret = card->late_probe(card);
1563 if (ret < 0) {
1564 dev_err(card->dev, "%s late_probe() failed: %d\n",
1565 card->name, ret);
1566 goto probe_aux_dev_err;
1567 }
1568 }
1569
Mark Brown2dc00212011-10-08 13:59:44 +01001570 snd_soc_dapm_new_widgets(&card->dapm);
1571
Stephen Warren16332812011-11-23 12:42:04 -07001572 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001573 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001574 snd_soc_dapm_auto_nc_codec_pins(codec);
1575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001576 ret = snd_card_register(card->snd_card);
1577 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001578 pr_err("asoc: failed to register soundcard for %s: %d\n",
1579 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001580 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 }
1582
1583#ifdef CONFIG_SND_SOC_AC97_BUS
1584 /* register any AC97 codecs */
1585 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001586 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1587 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001588 pr_err("asoc: failed to register AC97 %s: %d\n",
1589 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001590 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001591 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001592 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001593 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001594 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001595#endif
1596
Mark Brown435c5e22008-12-04 15:32:53 +00001597 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001598 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001600 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001601
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001602probe_aux_dev_err:
1603 for (i = 0; i < card->num_aux_devs; i++)
1604 soc_remove_aux_dev(card, i);
1605
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001607 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001610 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001611 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612
1613 snd_card_free(card->snd_card);
1614
1615 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001616}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001617
Mark Brown435c5e22008-12-04 15:32:53 +00001618/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001619 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001620 * client_mutex.
1621 */
1622static void snd_soc_instantiate_cards(void)
1623{
1624 struct snd_soc_card *card;
1625 list_for_each_entry(card, &card_list, list)
1626 snd_soc_instantiate_card(card);
1627}
1628
1629/* probes a new socdev */
1630static int soc_probe(struct platform_device *pdev)
1631{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001633 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001634
Vinod Koul70a7ca32011-01-14 19:22:48 +05301635 /*
1636 * no card, so machine driver should be registering card
1637 * we should not be here in that case so ret error
1638 */
1639 if (!card)
1640 return -EINVAL;
1641
Mark Brownfe4085e2012-03-02 13:07:41 +00001642 dev_warn(&pdev->dev,
1643 "ASoC machine %s should use snd_soc_register_card()\n",
1644 card->name);
1645
Mark Brown435c5e22008-12-04 15:32:53 +00001646 /* Bodge while we unpick instantiation */
1647 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648
Mark Brown435c5e22008-12-04 15:32:53 +00001649 ret = snd_soc_register_card(card);
1650 if (ret != 0) {
1651 dev_err(&pdev->dev, "Failed to register card\n");
1652 return ret;
1653 }
1654
1655 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001656}
1657
Vinod Koulb0e26482011-01-13 22:48:02 +05301658static int soc_cleanup_card_resources(struct snd_soc_card *card)
1659{
Vinod Koulb0e26482011-01-13 22:48:02 +05301660 int i;
1661
1662 /* make sure any delayed work runs */
1663 for (i = 0; i < card->num_rtd; i++) {
1664 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1665 flush_delayed_work_sync(&rtd->delayed_work);
1666 }
1667
1668 /* remove auxiliary devices */
1669 for (i = 0; i < card->num_aux_devs; i++)
1670 soc_remove_aux_dev(card, i);
1671
1672 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001673 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301674
1675 soc_cleanup_card_debugfs(card);
1676
1677 /* remove the card */
1678 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001679 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301680
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001681 snd_soc_dapm_free(&card->dapm);
1682
Vinod Koulb0e26482011-01-13 22:48:02 +05301683 snd_card_free(card->snd_card);
1684 return 0;
1685
1686}
1687
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001688/* removes a socdev */
1689static int soc_remove(struct platform_device *pdev)
1690{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001691 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001692
Mark Brownc5af3a22008-11-28 13:29:45 +00001693 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001694 return 0;
1695}
1696
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001697int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001698{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001699 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 int i;
Mark Brown51737472009-06-22 13:16:51 +01001701
1702 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001703 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001704
1705 /* Flush out pmdown_time work - we actually do want to run it
1706 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 for (i = 0; i < card->num_rtd; i++) {
1708 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001709 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710 }
Mark Brown51737472009-06-22 13:16:51 +01001711
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001712 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001713
1714 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001715}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001716EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001717
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001718const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301719 .suspend = snd_soc_suspend,
1720 .resume = snd_soc_resume,
1721 .freeze = snd_soc_suspend,
1722 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001723 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301724 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001725};
Stephen Warrendeb26072011-04-05 19:35:30 -06001726EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001727
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001728/* ASoC platform driver */
1729static struct platform_driver soc_driver = {
1730 .driver = {
1731 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001732 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001733 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734 },
1735 .probe = soc_probe,
1736 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737};
1738
Mark Brown096e49d2009-07-05 15:12:22 +01001739/**
1740 * snd_soc_codec_volatile_register: Report if a register is volatile.
1741 *
1742 * @codec: CODEC to query.
1743 * @reg: Register to query.
1744 *
1745 * Boolean function indiciating if a CODEC register is volatile.
1746 */
Mark Brown181e0552011-01-24 14:05:25 +00001747int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1748 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001749{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001750 if (codec->volatile_register)
1751 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001752 else
1753 return 0;
1754}
1755EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1756
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001757/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001758 * snd_soc_codec_readable_register: Report if a register is readable.
1759 *
1760 * @codec: CODEC to query.
1761 * @reg: Register to query.
1762 *
1763 * Boolean function indicating if a CODEC register is readable.
1764 */
1765int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1766 unsigned int reg)
1767{
1768 if (codec->readable_register)
1769 return codec->readable_register(codec, reg);
1770 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001771 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001772}
1773EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1774
1775/**
1776 * snd_soc_codec_writable_register: Report if a register is writable.
1777 *
1778 * @codec: CODEC to query.
1779 * @reg: Register to query.
1780 *
1781 * Boolean function indicating if a CODEC register is writable.
1782 */
1783int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1784 unsigned int reg)
1785{
1786 if (codec->writable_register)
1787 return codec->writable_register(codec, reg);
1788 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001789 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001790}
1791EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1792
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001793int snd_soc_platform_read(struct snd_soc_platform *platform,
1794 unsigned int reg)
1795{
1796 unsigned int ret;
1797
1798 if (!platform->driver->read) {
1799 dev_err(platform->dev, "platform has no read back\n");
1800 return -1;
1801 }
1802
1803 ret = platform->driver->read(platform, reg);
1804 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001805 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001806
1807 return ret;
1808}
1809EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1810
1811int snd_soc_platform_write(struct snd_soc_platform *platform,
1812 unsigned int reg, unsigned int val)
1813{
1814 if (!platform->driver->write) {
1815 dev_err(platform->dev, "platform has no write back\n");
1816 return -1;
1817 }
1818
1819 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001820 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001821 return platform->driver->write(platform, reg, val);
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1824
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001825/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001826 * snd_soc_new_ac97_codec - initailise AC97 device
1827 * @codec: audio codec
1828 * @ops: AC97 bus operations
1829 * @num: AC97 codec number
1830 *
1831 * Initialises AC97 codec resources for use by ad-hoc devices only.
1832 */
1833int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1834 struct snd_ac97_bus_ops *ops, int num)
1835{
1836 mutex_lock(&codec->mutex);
1837
1838 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1839 if (codec->ac97 == NULL) {
1840 mutex_unlock(&codec->mutex);
1841 return -ENOMEM;
1842 }
1843
1844 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1845 if (codec->ac97->bus == NULL) {
1846 kfree(codec->ac97);
1847 codec->ac97 = NULL;
1848 mutex_unlock(&codec->mutex);
1849 return -ENOMEM;
1850 }
1851
1852 codec->ac97->bus->ops = ops;
1853 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001854
1855 /*
1856 * Mark the AC97 device to be created by us. This way we ensure that the
1857 * device will be registered with the device subsystem later on.
1858 */
1859 codec->ac97_created = 1;
1860
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861 mutex_unlock(&codec->mutex);
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1865
1866/**
1867 * snd_soc_free_ac97_codec - free AC97 codec device
1868 * @codec: audio codec
1869 *
1870 * Frees AC97 codec device resources.
1871 */
1872void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1873{
1874 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001875#ifdef CONFIG_SND_SOC_AC97_BUS
1876 soc_unregister_ac97_dai_link(codec);
1877#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 kfree(codec->ac97->bus);
1879 kfree(codec->ac97);
1880 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001881 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882 mutex_unlock(&codec->mutex);
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1885
Mark Brownc3753702010-11-01 15:41:57 -04001886unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1887{
1888 unsigned int ret;
1889
Mark Brownc3acec22010-12-02 16:15:29 +00001890 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001891 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001892 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001893
1894 return ret;
1895}
1896EXPORT_SYMBOL_GPL(snd_soc_read);
1897
1898unsigned int snd_soc_write(struct snd_soc_codec *codec,
1899 unsigned int reg, unsigned int val)
1900{
1901 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001902 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001903 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001904}
1905EXPORT_SYMBOL_GPL(snd_soc_write);
1906
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001907unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1908 unsigned int reg, const void *data, size_t len)
1909{
1910 return codec->bulk_write_raw(codec, reg, data, len);
1911}
1912EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1913
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914/**
1915 * snd_soc_update_bits - update codec register bits
1916 * @codec: audio codec
1917 * @reg: codec register
1918 * @mask: register mask
1919 * @value: new value
1920 *
1921 * Writes new register value.
1922 *
Timur Tabi180c3292011-01-10 15:58:13 -06001923 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001924 */
1925int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001926 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927{
Mark Brown8a713da2011-12-03 12:33:55 +00001928 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001929 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001930 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931
Mark Brown8a713da2011-12-03 12:33:55 +00001932 if (codec->using_regmap) {
1933 ret = regmap_update_bits_check(codec->control_data, reg,
1934 mask, value, &change);
1935 } else {
1936 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001937 if (ret < 0)
1938 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001939
1940 old = ret;
1941 new = (old & ~mask) | (value & mask);
1942 change = old != new;
1943 if (change)
1944 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001945 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946
Mark Brown8a713da2011-12-03 12:33:55 +00001947 if (ret < 0)
1948 return ret;
1949
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950 return change;
1951}
1952EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1953
1954/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001955 * snd_soc_update_bits_locked - update codec register bits
1956 * @codec: audio codec
1957 * @reg: codec register
1958 * @mask: register mask
1959 * @value: new value
1960 *
1961 * Writes new register value, and takes the codec mutex.
1962 *
1963 * Returns 1 for change else 0.
1964 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001965int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1966 unsigned short reg, unsigned int mask,
1967 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001968{
1969 int change;
1970
1971 mutex_lock(&codec->mutex);
1972 change = snd_soc_update_bits(codec, reg, mask, value);
1973 mutex_unlock(&codec->mutex);
1974
1975 return change;
1976}
Mark Browndd1b3d52009-12-04 14:22:03 +00001977EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001978
1979/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 * snd_soc_test_bits - test register for change
1981 * @codec: audio codec
1982 * @reg: codec register
1983 * @mask: register mask
1984 * @value: new value
1985 *
1986 * Tests a register with a new value and checks if the new value is
1987 * different from the old value.
1988 *
1989 * Returns 1 for change else 0.
1990 */
1991int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001992 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001993{
1994 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001995 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001997 old = snd_soc_read(codec, reg);
1998 new = (old & ~mask) | value;
1999 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000
2001 return change;
2002}
2003EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2004
2005/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002006 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2007 * @substream: the pcm substream
2008 * @hw: the hardware parameters
2009 *
2010 * Sets the substream runtime hardware parameters.
2011 */
2012int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2013 const struct snd_pcm_hardware *hw)
2014{
2015 struct snd_pcm_runtime *runtime = substream->runtime;
2016 runtime->hw.info = hw->info;
2017 runtime->hw.formats = hw->formats;
2018 runtime->hw.period_bytes_min = hw->period_bytes_min;
2019 runtime->hw.period_bytes_max = hw->period_bytes_max;
2020 runtime->hw.periods_min = hw->periods_min;
2021 runtime->hw.periods_max = hw->periods_max;
2022 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2023 runtime->hw.fifo_size = hw->fifo_size;
2024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2027
2028/**
2029 * snd_soc_cnew - create new control
2030 * @_template: control template
2031 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002032 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002033 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034 *
2035 * Create a new mixer control from a template control.
2036 *
2037 * Returns 0 for success, else error.
2038 */
2039struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002040 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002041 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002042{
2043 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002044 struct snd_kcontrol *kcontrol;
2045 char *name = NULL;
2046 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002047
2048 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049 template.index = 0;
2050
Mark Brownefb7ac32011-03-08 17:23:24 +00002051 if (!long_name)
2052 long_name = template.name;
2053
2054 if (prefix) {
2055 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002056 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002057 if (!name)
2058 return NULL;
2059
2060 snprintf(name, name_len, "%s %s", prefix, long_name);
2061
2062 template.name = name;
2063 } else {
2064 template.name = long_name;
2065 }
2066
2067 kcontrol = snd_ctl_new1(&template, data);
2068
2069 kfree(name);
2070
2071 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002072}
2073EXPORT_SYMBOL_GPL(snd_soc_cnew);
2074
Liam Girdwood022658b2012-02-03 17:43:09 +00002075static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2076 const struct snd_kcontrol_new *controls, int num_controls,
2077 const char *prefix, void *data)
2078{
2079 int err, i;
2080
2081 for (i = 0; i < num_controls; i++) {
2082 const struct snd_kcontrol_new *control = &controls[i];
2083 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2084 control->name, prefix));
2085 if (err < 0) {
2086 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2087 return err;
2088 }
2089 }
2090
2091 return 0;
2092}
2093
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002095 * snd_soc_add_codec_controls - add an array of controls to a codec.
2096 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002097 * duplicating this code.
2098 *
2099 * @codec: codec to add controls to
2100 * @controls: array of controls to add
2101 * @num_controls: number of elements in the array
2102 *
2103 * Return 0 for success, else error.
2104 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002105int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002106 const struct snd_kcontrol_new *controls, int num_controls)
2107{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002108 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002109
Liam Girdwood022658b2012-02-03 17:43:09 +00002110 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2111 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002112}
Liam Girdwood022658b2012-02-03 17:43:09 +00002113EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002114
2115/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002116 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002117 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002118 *
2119 * @platform: platform to add controls to
2120 * @controls: array of controls to add
2121 * @num_controls: number of elements in the array
2122 *
2123 * Return 0 for success, else error.
2124 */
2125int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2126 const struct snd_kcontrol_new *controls, int num_controls)
2127{
2128 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002129
Liam Girdwood022658b2012-02-03 17:43:09 +00002130 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2131 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002132}
2133EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2134
2135/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002136 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2137 * Convenience function to add a list of controls.
2138 *
2139 * @soc_card: SoC card to add controls to
2140 * @controls: array of controls to add
2141 * @num_controls: number of elements in the array
2142 *
2143 * Return 0 for success, else error.
2144 */
2145int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2146 const struct snd_kcontrol_new *controls, int num_controls)
2147{
2148 struct snd_card *card = soc_card->snd_card;
2149
2150 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2151 NULL, soc_card);
2152}
2153EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2154
2155/**
2156 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2157 * Convienience function to add a list of controls.
2158 *
2159 * @dai: DAI to add controls to
2160 * @controls: array of controls to add
2161 * @num_controls: number of elements in the array
2162 *
2163 * Return 0 for success, else error.
2164 */
2165int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2166 const struct snd_kcontrol_new *controls, int num_controls)
2167{
2168 struct snd_card *card = dai->card->snd_card;
2169
2170 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2171 NULL, dai);
2172}
2173EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2174
2175/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176 * snd_soc_info_enum_double - enumerated double mixer info callback
2177 * @kcontrol: mixer control
2178 * @uinfo: control element information
2179 *
2180 * Callback to provide information about a double enumerated
2181 * mixer control.
2182 *
2183 * Returns 0 for success.
2184 */
2185int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2186 struct snd_ctl_elem_info *uinfo)
2187{
2188 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2189
2190 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2191 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002192 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002194 if (uinfo->value.enumerated.item > e->max - 1)
2195 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196 strcpy(uinfo->value.enumerated.name,
2197 e->texts[uinfo->value.enumerated.item]);
2198 return 0;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2201
2202/**
2203 * snd_soc_get_enum_double - enumerated double mixer get callback
2204 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002205 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 *
2207 * Callback to get the value of a double enumerated mixer.
2208 *
2209 * Returns 0 for success.
2210 */
2211int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2213{
2214 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2215 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002216 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002218 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219 ;
2220 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002221 ucontrol->value.enumerated.item[0]
2222 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002223 if (e->shift_l != e->shift_r)
2224 ucontrol->value.enumerated.item[1] =
2225 (val >> e->shift_r) & (bitmask - 1);
2226
2227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2230
2231/**
2232 * snd_soc_put_enum_double - enumerated double mixer put callback
2233 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002234 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 *
2236 * Callback to set the value of a double enumerated mixer.
2237 *
2238 * Returns 0 for success.
2239 */
2240int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002245 unsigned int val;
2246 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002248 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002250 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251 return -EINVAL;
2252 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2253 mask = (bitmask - 1) << e->shift_l;
2254 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002255 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 return -EINVAL;
2257 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2258 mask |= (bitmask - 1) << e->shift_r;
2259 }
2260
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002261 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262}
2263EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2264
2265/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002266 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2267 * @kcontrol: mixer control
2268 * @ucontrol: control element information
2269 *
2270 * Callback to get the value of a double semi enumerated mixer.
2271 *
2272 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2273 * used for handling bitfield coded enumeration for example.
2274 *
2275 * Returns 0 for success.
2276 */
2277int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002281 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002282 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002283
2284 reg_val = snd_soc_read(codec, e->reg);
2285 val = (reg_val >> e->shift_l) & e->mask;
2286 for (mux = 0; mux < e->max; mux++) {
2287 if (val == e->values[mux])
2288 break;
2289 }
2290 ucontrol->value.enumerated.item[0] = mux;
2291 if (e->shift_l != e->shift_r) {
2292 val = (reg_val >> e->shift_r) & e->mask;
2293 for (mux = 0; mux < e->max; mux++) {
2294 if (val == e->values[mux])
2295 break;
2296 }
2297 ucontrol->value.enumerated.item[1] = mux;
2298 }
2299
2300 return 0;
2301}
2302EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2303
2304/**
2305 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2306 * @kcontrol: mixer control
2307 * @ucontrol: control element information
2308 *
2309 * Callback to set the value of a double semi enumerated mixer.
2310 *
2311 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2312 * used for handling bitfield coded enumeration for example.
2313 *
2314 * Returns 0 for success.
2315 */
2316int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2318{
2319 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002320 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002321 unsigned int val;
2322 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002323
2324 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2325 return -EINVAL;
2326 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2327 mask = e->mask << e->shift_l;
2328 if (e->shift_l != e->shift_r) {
2329 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2330 return -EINVAL;
2331 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2332 mask |= e->mask << e->shift_r;
2333 }
2334
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002335 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002336}
2337EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2338
2339/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002340 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2341 * @kcontrol: mixer control
2342 * @uinfo: control element information
2343 *
2344 * Callback to provide information about an external enumerated
2345 * single mixer.
2346 *
2347 * Returns 0 for success.
2348 */
2349int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_info *uinfo)
2351{
2352 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2353
2354 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2355 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002356 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002358 if (uinfo->value.enumerated.item > e->max - 1)
2359 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 strcpy(uinfo->value.enumerated.name,
2361 e->texts[uinfo->value.enumerated.item]);
2362 return 0;
2363}
2364EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2365
2366/**
2367 * snd_soc_info_volsw_ext - external single mixer info callback
2368 * @kcontrol: mixer control
2369 * @uinfo: control element information
2370 *
2371 * Callback to provide information about a single external mixer control.
2372 *
2373 * Returns 0 for success.
2374 */
2375int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_info *uinfo)
2377{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002378 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002379
Mark Brownfd5dfad2009-04-15 21:37:46 +01002380 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002381 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2382 else
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2384
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002385 uinfo->count = 1;
2386 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002387 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002388 return 0;
2389}
2390EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2391
2392/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 * snd_soc_info_volsw - single mixer info callback
2394 * @kcontrol: mixer control
2395 * @uinfo: control element information
2396 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002397 * Callback to provide information about a single mixer control, or a double
2398 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 *
2400 * Returns 0 for success.
2401 */
2402int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_info *uinfo)
2404{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002405 struct soc_mixer_control *mc =
2406 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002407 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002409 if (!mc->platform_max)
2410 mc->platform_max = mc->max;
2411 platform_max = mc->platform_max;
2412
2413 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002414 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2415 else
2416 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2417
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002418 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002420 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002421 return 0;
2422}
2423EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2424
2425/**
2426 * snd_soc_get_volsw - single mixer get callback
2427 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002428 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002429 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002430 * Callback to get the value of a single mixer control, or a double mixer
2431 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432 *
2433 * Returns 0 for success.
2434 */
2435int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2436 struct snd_ctl_elem_value *ucontrol)
2437{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002438 struct soc_mixer_control *mc =
2439 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002441 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002442 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002443 unsigned int shift = mc->shift;
2444 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002445 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002446 unsigned int mask = (1 << fls(max)) - 1;
2447 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002448
2449 ucontrol->value.integer.value[0] =
2450 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002451 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002453 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002454
2455 if (snd_soc_volsw_is_stereo(mc)) {
2456 if (reg == reg2)
2457 ucontrol->value.integer.value[1] =
2458 (snd_soc_read(codec, reg) >> rshift) & mask;
2459 else
2460 ucontrol->value.integer.value[1] =
2461 (snd_soc_read(codec, reg2) >> shift) & mask;
2462 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002464 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 }
2466
2467 return 0;
2468}
2469EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2470
2471/**
2472 * snd_soc_put_volsw - single mixer put callback
2473 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002474 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002476 * Callback to set the value of a single mixer control, or a double mixer
2477 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002478 *
2479 * Returns 0 for success.
2480 */
2481int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_value *ucontrol)
2483{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002484 struct soc_mixer_control *mc =
2485 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002487 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002488 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002489 unsigned int shift = mc->shift;
2490 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002491 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002492 unsigned int mask = (1 << fls(max)) - 1;
2493 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002494 int err;
2495 bool type_2r = 0;
2496 unsigned int val2 = 0;
2497 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498
2499 val = (ucontrol->value.integer.value[0] & mask);
2500 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002501 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502 val_mask = mask << shift;
2503 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002504 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 val2 = (ucontrol->value.integer.value[1] & mask);
2506 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002507 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002508 if (reg == reg2) {
2509 val_mask |= mask << rshift;
2510 val |= val2 << rshift;
2511 } else {
2512 val2 = val2 << shift;
2513 type_2r = 1;
2514 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002516 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002517 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 return err;
2519
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002520 if (type_2r)
2521 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2522
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 return err;
2524}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002525EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526
Mark Browne13ac2e2008-05-28 17:58:05 +01002527/**
2528 * snd_soc_info_volsw_s8 - signed mixer info callback
2529 * @kcontrol: mixer control
2530 * @uinfo: control element information
2531 *
2532 * Callback to provide information about a signed mixer control.
2533 *
2534 * Returns 0 for success.
2535 */
2536int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_info *uinfo)
2538{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002539 struct soc_mixer_control *mc =
2540 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002541 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002542 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002543
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002544 if (!mc->platform_max)
2545 mc->platform_max = mc->max;
2546 platform_max = mc->platform_max;
2547
Mark Browne13ac2e2008-05-28 17:58:05 +01002548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2549 uinfo->count = 2;
2550 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002551 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002552 return 0;
2553}
2554EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2555
2556/**
2557 * snd_soc_get_volsw_s8 - signed mixer get callback
2558 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002559 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002560 *
2561 * Callback to get the value of a signed mixer control.
2562 *
2563 * Returns 0 for success.
2564 */
2565int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2567{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002570 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002571 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002572 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002573 int val = snd_soc_read(codec, reg);
2574
2575 ucontrol->value.integer.value[0] =
2576 ((signed char)(val & 0xff))-min;
2577 ucontrol->value.integer.value[1] =
2578 ((signed char)((val >> 8) & 0xff))-min;
2579 return 0;
2580}
2581EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2582
2583/**
2584 * snd_soc_put_volsw_sgn - signed mixer put callback
2585 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002586 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002587 *
2588 * Callback to set the value of a signed mixer control.
2589 *
2590 * Returns 0 for success.
2591 */
2592int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
2594{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002595 struct soc_mixer_control *mc =
2596 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002597 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002598 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002599 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002600 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002601
2602 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2603 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2604
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002605 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002606}
2607EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2608
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002609/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002610 * snd_soc_limit_volume - Set new limit to an existing volume control.
2611 *
2612 * @codec: where to look for the control
2613 * @name: Name of the control
2614 * @max: new maximum limit
2615 *
2616 * Return 0 for success, else error.
2617 */
2618int snd_soc_limit_volume(struct snd_soc_codec *codec,
2619 const char *name, int max)
2620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002621 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002622 struct snd_kcontrol *kctl;
2623 struct soc_mixer_control *mc;
2624 int found = 0;
2625 int ret = -EINVAL;
2626
2627 /* Sanity check for name and max */
2628 if (unlikely(!name || max <= 0))
2629 return -EINVAL;
2630
2631 list_for_each_entry(kctl, &card->controls, list) {
2632 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2633 found = 1;
2634 break;
2635 }
2636 }
2637 if (found) {
2638 mc = (struct soc_mixer_control *)kctl->private_value;
2639 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002640 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002641 ret = 0;
2642 }
2643 }
2644 return ret;
2645}
2646EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2647
2648/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002649 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2650 * mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2653 *
2654 * Returns 0 for success.
2655 */
2656int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_info *uinfo)
2658{
2659 struct soc_mixer_control *mc =
2660 (struct soc_mixer_control *)kcontrol->private_value;
2661 int max = mc->max;
2662 int min = mc->min;
2663
2664 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2665 uinfo->count = 2;
2666 uinfo->value.integer.min = 0;
2667 uinfo->value.integer.max = max-min;
2668
2669 return 0;
2670}
2671EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2672
2673/**
2674 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2675 * mixer get callback
2676 * @kcontrol: mixer control
2677 * @uinfo: control element information
2678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
2684 struct soc_mixer_control *mc =
2685 (struct soc_mixer_control *)kcontrol->private_value;
2686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2687 unsigned int mask = (1<<mc->shift)-1;
2688 int min = mc->min;
2689 int val = snd_soc_read(codec, mc->reg) & mask;
2690 int valr = snd_soc_read(codec, mc->rreg) & mask;
2691
Stuart Longland20630c72010-06-18 12:56:10 +10002692 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2693 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002694 return 0;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2697
2698/**
2699 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2700 * mixer put callback
2701 * @kcontrol: mixer control
2702 * @uinfo: control element information
2703 *
2704 * Returns 0 for success.
2705 */
2706int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2708{
2709 struct soc_mixer_control *mc =
2710 (struct soc_mixer_control *)kcontrol->private_value;
2711 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2712 unsigned int mask = (1<<mc->shift)-1;
2713 int min = mc->min;
2714 int ret;
2715 unsigned int val, valr, oval, ovalr;
2716
2717 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2718 val &= mask;
2719 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2720 valr &= mask;
2721
2722 oval = snd_soc_read(codec, mc->reg) & mask;
2723 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2724
2725 ret = 0;
2726 if (oval != val) {
2727 ret = snd_soc_write(codec, mc->reg, val);
2728 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002729 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002730 }
2731 if (ovalr != valr) {
2732 ret = snd_soc_write(codec, mc->rreg, valr);
2733 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002734 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002735 }
2736
2737 return 0;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2740
Mark Brown71d08512011-10-10 18:31:26 +01002741int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_info *uinfo)
2743{
2744 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2745 struct soc_bytes *params = (void *)kcontrol->private_value;
2746
2747 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2748 uinfo->count = params->num_regs * codec->val_bytes;
2749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2753
2754int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
2757 struct soc_bytes *params = (void *)kcontrol->private_value;
2758 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2759 int ret;
2760
2761 if (codec->using_regmap)
2762 ret = regmap_raw_read(codec->control_data, params->base,
2763 ucontrol->value.bytes.data,
2764 params->num_regs * codec->val_bytes);
2765 else
2766 ret = -EINVAL;
2767
Mark Brownf831b052012-02-17 16:20:33 -08002768 /* Hide any masked bytes to ensure consistent data reporting */
2769 if (ret == 0 && params->mask) {
2770 switch (codec->val_bytes) {
2771 case 1:
2772 ucontrol->value.bytes.data[0] &= ~params->mask;
2773 break;
2774 case 2:
2775 ((u16 *)(&ucontrol->value.bytes.data))[0]
2776 &= ~params->mask;
2777 break;
2778 case 4:
2779 ((u32 *)(&ucontrol->value.bytes.data))[0]
2780 &= ~params->mask;
2781 break;
2782 default:
2783 return -EINVAL;
2784 }
2785 }
2786
Mark Brown71d08512011-10-10 18:31:26 +01002787 return ret;
2788}
2789EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2790
2791int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2792 struct snd_ctl_elem_value *ucontrol)
2793{
2794 struct soc_bytes *params = (void *)kcontrol->private_value;
2795 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002796 int ret, len;
2797 unsigned int val;
2798 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002799
Mark Brownf831b052012-02-17 16:20:33 -08002800 if (!codec->using_regmap)
2801 return -EINVAL;
2802
2803 data = ucontrol->value.bytes.data;
2804 len = params->num_regs * codec->val_bytes;
2805
2806 /*
2807 * If we've got a mask then we need to preserve the register
2808 * bits. We shouldn't modify the incoming data so take a
2809 * copy.
2810 */
2811 if (params->mask) {
2812 ret = regmap_read(codec->control_data, params->base, &val);
2813 if (ret != 0)
2814 return ret;
2815
2816 val &= params->mask;
2817
2818 data = kmemdup(data, len, GFP_KERNEL);
2819 if (!data)
2820 return -ENOMEM;
2821
2822 switch (codec->val_bytes) {
2823 case 1:
2824 ((u8 *)data)[0] &= ~params->mask;
2825 ((u8 *)data)[0] |= val;
2826 break;
2827 case 2:
2828 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2829 ((u16 *)data)[0] |= cpu_to_be16(val);
2830 break;
2831 case 4:
2832 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2833 ((u32 *)data)[0] |= cpu_to_be32(val);
2834 break;
2835 default:
2836 return -EINVAL;
2837 }
2838 }
2839
2840 ret = regmap_raw_write(codec->control_data, params->base,
2841 data, len);
2842
2843 if (params->mask)
2844 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002845
2846 return ret;
2847}
2848EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2849
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002850/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002851 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2852 * @dai: DAI
2853 * @clk_id: DAI specific clock ID
2854 * @freq: new clock frequency in Hz
2855 * @dir: new clock direction - input/output.
2856 *
2857 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2858 */
2859int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2860 unsigned int freq, int dir)
2861{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002862 if (dai->driver && dai->driver->ops->set_sysclk)
2863 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002864 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002865 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002866 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002867 else
2868 return -EINVAL;
2869}
2870EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2871
2872/**
Mark Brownec4ee522011-03-07 20:58:11 +00002873 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2874 * @codec: CODEC
2875 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002876 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002877 * @freq: new clock frequency in Hz
2878 * @dir: new clock direction - input/output.
2879 *
2880 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2881 */
2882int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002883 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002884{
2885 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002886 return codec->driver->set_sysclk(codec, clk_id, source,
2887 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002888 else
2889 return -EINVAL;
2890}
2891EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2892
2893/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002894 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2895 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002896 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002897 * @div: new clock divisor.
2898 *
2899 * Configures the clock dividers. This is used to derive the best DAI bit and
2900 * frame clocks from the system or master clock. It's best to set the DAI bit
2901 * and frame clocks as low as possible to save system power.
2902 */
2903int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2904 int div_id, int div)
2905{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002906 if (dai->driver && dai->driver->ops->set_clkdiv)
2907 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002908 else
2909 return -EINVAL;
2910}
2911EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2912
2913/**
2914 * snd_soc_dai_set_pll - configure DAI PLL.
2915 * @dai: DAI
2916 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002917 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002918 * @freq_in: PLL input clock frequency in Hz
2919 * @freq_out: requested PLL output clock frequency in Hz
2920 *
2921 * Configures and enables PLL to generate output clock based on input clock.
2922 */
Mark Brown85488032009-09-05 18:52:16 +01002923int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2924 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002925{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002926 if (dai->driver && dai->driver->ops->set_pll)
2927 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002928 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002929 else if (dai->codec && dai->codec->driver->set_pll)
2930 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2931 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002932 else
2933 return -EINVAL;
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2936
Mark Brownec4ee522011-03-07 20:58:11 +00002937/*
2938 * snd_soc_codec_set_pll - configure codec PLL.
2939 * @codec: CODEC
2940 * @pll_id: DAI specific PLL ID
2941 * @source: DAI specific source for the PLL
2942 * @freq_in: PLL input clock frequency in Hz
2943 * @freq_out: requested PLL output clock frequency in Hz
2944 *
2945 * Configures and enables PLL to generate output clock based on input clock.
2946 */
2947int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2948 unsigned int freq_in, unsigned int freq_out)
2949{
2950 if (codec->driver->set_pll)
2951 return codec->driver->set_pll(codec, pll_id, source,
2952 freq_in, freq_out);
2953 else
2954 return -EINVAL;
2955}
2956EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2957
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002958/**
2959 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2960 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002961 * @fmt: SND_SOC_DAIFMT_ format value.
2962 *
2963 * Configures the DAI hardware format and clocking.
2964 */
2965int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2966{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002967 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002968 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002969 if (dai->driver->ops->set_fmt == NULL)
2970 return -ENOTSUPP;
2971 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002972}
2973EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2974
2975/**
2976 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2977 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002978 * @tx_mask: bitmask representing active TX slots.
2979 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002980 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002981 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002982 *
2983 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2984 * specific.
2985 */
2986int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002987 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002988{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002989 if (dai->driver && dai->driver->ops->set_tdm_slot)
2990 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002991 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002992 else
2993 return -EINVAL;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2996
2997/**
Barry Song472df3c2009-09-12 01:16:29 +08002998 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2999 * @dai: DAI
3000 * @tx_num: how many TX channels
3001 * @tx_slot: pointer to an array which imply the TX slot number channel
3002 * 0~num-1 uses
3003 * @rx_num: how many RX channels
3004 * @rx_slot: pointer to an array which imply the RX slot number channel
3005 * 0~num-1 uses
3006 *
3007 * configure the relationship between channel number and TDM slot number.
3008 */
3009int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3010 unsigned int tx_num, unsigned int *tx_slot,
3011 unsigned int rx_num, unsigned int *rx_slot)
3012{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 if (dai->driver && dai->driver->ops->set_channel_map)
3014 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003015 rx_num, rx_slot);
3016 else
3017 return -EINVAL;
3018}
3019EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3020
3021/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003022 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3023 * @dai: DAI
3024 * @tristate: tristate enable
3025 *
3026 * Tristates the DAI so that others can use it.
3027 */
3028int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3029{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003030 if (dai->driver && dai->driver->ops->set_tristate)
3031 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003032 else
3033 return -EINVAL;
3034}
3035EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3036
3037/**
3038 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3039 * @dai: DAI
3040 * @mute: mute enable
3041 *
3042 * Mutes the DAI DAC.
3043 */
3044int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3045{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003046 if (dai->driver && dai->driver->ops->digital_mute)
3047 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003048 else
3049 return -EINVAL;
3050}
3051EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3052
Mark Brownc5af3a22008-11-28 13:29:45 +00003053/**
3054 * snd_soc_register_card - Register a card with the ASoC core
3055 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003056 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003057 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003058 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303059int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003060{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003061 int i;
3062
Mark Brownc5af3a22008-11-28 13:29:45 +00003063 if (!card->name || !card->dev)
3064 return -EINVAL;
3065
Stephen Warren5a504962011-12-21 10:40:59 -07003066 for (i = 0; i < card->num_links; i++) {
3067 struct snd_soc_dai_link *link = &card->dai_link[i];
3068
3069 /*
3070 * Codec must be specified by 1 of name or OF node,
3071 * not both or neither.
3072 */
3073 if (!!link->codec_name == !!link->codec_of_node) {
3074 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003075 "Neither/both codec name/of_node are set for %s\n",
3076 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003077 return -EINVAL;
3078 }
3079
3080 /*
3081 * Platform may be specified by either name or OF node, but
3082 * can be left unspecified, and a dummy platform will be used.
3083 */
3084 if (link->platform_name && link->platform_of_node) {
3085 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003086 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003087 return -EINVAL;
3088 }
3089
3090 /*
3091 * CPU DAI must be specified by 1 of name or OF node,
3092 * not both or neither.
3093 */
3094 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3095 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003096 "Neither/both cpu_dai name/of_node are set for %s\n",
3097 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003098 return -EINVAL;
3099 }
3100 }
3101
Mark Browned77cc12011-05-03 18:25:34 +01003102 dev_set_drvdata(card->dev, card);
3103
Stephen Warren111c6412011-01-28 14:26:35 -07003104 snd_soc_initialize_card_lists(card);
3105
Vinod Koul150dd2f2011-01-13 22:48:32 +05303106 soc_init_card_debugfs(card);
3107
Mark Brown181a6892012-03-14 20:18:49 +00003108 card->rtd = devm_kzalloc(card->dev,
3109 sizeof(struct snd_soc_pcm_runtime) *
3110 (card->num_links + card->num_aux_devs),
3111 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003112 if (card->rtd == NULL)
3113 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003114 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115
3116 for (i = 0; i < card->num_links; i++)
3117 card->rtd[i].dai_link = &card->dai_link[i];
3118
Mark Brownc5af3a22008-11-28 13:29:45 +00003119 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003120 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003121 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003123
3124 mutex_lock(&client_mutex);
3125 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003126 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003127 mutex_unlock(&client_mutex);
3128
3129 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3130
3131 return 0;
3132}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303133EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003134
3135/**
3136 * snd_soc_unregister_card - Unregister a card with the ASoC core
3137 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003138 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003139 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003140 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303141int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003142{
Vinod Koulb0e26482011-01-13 22:48:02 +05303143 if (card->instantiated)
3144 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003145 mutex_lock(&client_mutex);
3146 list_del(&card->list);
3147 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003148 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3149
3150 return 0;
3151}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303152EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003153
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003154/*
3155 * Simplify DAI link configuration by removing ".-1" from device names
3156 * and sanitizing names.
3157 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003158static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003159{
3160 char *found, name[NAME_SIZE];
3161 int id1, id2;
3162
3163 if (dev_name(dev) == NULL)
3164 return NULL;
3165
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003166 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003167
3168 /* are we a "%s.%d" name (platform and SPI components) */
3169 found = strstr(name, dev->driver->name);
3170 if (found) {
3171 /* get ID */
3172 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3173
3174 /* discard ID from name if ID == -1 */
3175 if (*id == -1)
3176 found[strlen(dev->driver->name)] = '\0';
3177 }
3178
3179 } else {
3180 /* I2C component devices are named "bus-addr" */
3181 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3182 char tmp[NAME_SIZE];
3183
3184 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003185 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003186
3187 /* sanitize component name for DAI link creation */
3188 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003189 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003190 } else
3191 *id = 0;
3192 }
3193
3194 return kstrdup(name, GFP_KERNEL);
3195}
3196
3197/*
3198 * Simplify DAI link naming for single devices with multiple DAIs by removing
3199 * any ".-1" and using the DAI name (instead of device name).
3200 */
3201static inline char *fmt_multiple_name(struct device *dev,
3202 struct snd_soc_dai_driver *dai_drv)
3203{
3204 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003205 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003206 dev_name(dev));
3207 return NULL;
3208 }
3209
3210 return kstrdup(dai_drv->name, GFP_KERNEL);
3211}
3212
Mark Brown91151712008-11-30 23:31:24 +00003213/**
3214 * snd_soc_register_dai - Register a DAI with the ASoC core
3215 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003216 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003217 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003218int snd_soc_register_dai(struct device *dev,
3219 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003220{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003221 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003222
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003223 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3226 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003227 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003228
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003229 /* create DAI component name */
3230 dai->name = fmt_single_name(dev, &dai->id);
3231 if (dai->name == NULL) {
3232 kfree(dai);
3233 return -ENOMEM;
3234 }
3235
3236 dai->dev = dev;
3237 dai->driver = dai_drv;
3238 if (!dai->driver->ops)
3239 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003240
3241 mutex_lock(&client_mutex);
3242 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003243 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003244 mutex_unlock(&client_mutex);
3245
3246 pr_debug("Registered DAI '%s'\n", dai->name);
3247
3248 return 0;
3249}
3250EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3251
3252/**
3253 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3254 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003255 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003256 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003257void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003258{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 struct snd_soc_dai *dai;
3260
3261 list_for_each_entry(dai, &dai_list, list) {
3262 if (dev == dai->dev)
3263 goto found;
3264 }
3265 return;
3266
3267found:
Mark Brown91151712008-11-30 23:31:24 +00003268 mutex_lock(&client_mutex);
3269 list_del(&dai->list);
3270 mutex_unlock(&client_mutex);
3271
3272 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003273 kfree(dai->name);
3274 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003275}
3276EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3277
3278/**
3279 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3280 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003281 * @dai: Array of DAIs to register
3282 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003283 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003284int snd_soc_register_dais(struct device *dev,
3285 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003286{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003287 struct snd_soc_dai *dai;
3288 int i, ret = 0;
3289
Liam Girdwood720ffa42010-08-18 00:30:30 +01003290 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003291
3292 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003293
3294 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003295 if (dai == NULL) {
3296 ret = -ENOMEM;
3297 goto err;
3298 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003299
3300 /* create DAI component name */
3301 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3302 if (dai->name == NULL) {
3303 kfree(dai);
3304 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003305 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003306 }
3307
3308 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003309 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003310 if (dai->driver->id)
3311 dai->id = dai->driver->id;
3312 else
3313 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003314 if (!dai->driver->ops)
3315 dai->driver->ops = &null_dai_ops;
3316
3317 mutex_lock(&client_mutex);
3318 list_add(&dai->list, &dai_list);
3319 mutex_unlock(&client_mutex);
3320
3321 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003322 }
3323
Axel Lin1dcb4f32010-12-06 16:48:03 +08003324 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003325 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003326 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003327 return 0;
3328
3329err:
3330 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003331 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003332
3333 return ret;
3334}
3335EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3336
3337/**
3338 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3339 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003340 * @dai: Array of DAIs to unregister
3341 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003342 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003343void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003344{
3345 int i;
3346
3347 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003348 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003349}
3350EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3351
Mark Brown12a48a8c2008-12-03 19:40:30 +00003352/**
3353 * snd_soc_register_platform - Register a platform with the ASoC core
3354 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003355 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003356 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003357int snd_soc_register_platform(struct device *dev,
3358 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003359{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003361
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003362 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3363
3364 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3365 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003366 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003367
3368 /* create platform component name */
3369 platform->name = fmt_single_name(dev, &platform->id);
3370 if (platform->name == NULL) {
3371 kfree(platform);
3372 return -ENOMEM;
3373 }
3374
3375 platform->dev = dev;
3376 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003377 platform->dapm.dev = dev;
3378 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003379 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003380 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003381
3382 mutex_lock(&client_mutex);
3383 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003384 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003385 mutex_unlock(&client_mutex);
3386
3387 pr_debug("Registered platform '%s'\n", platform->name);
3388
3389 return 0;
3390}
3391EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3392
3393/**
3394 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3395 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003396 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003397 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003399{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400 struct snd_soc_platform *platform;
3401
3402 list_for_each_entry(platform, &platform_list, list) {
3403 if (dev == platform->dev)
3404 goto found;
3405 }
3406 return;
3407
3408found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003409 mutex_lock(&client_mutex);
3410 list_del(&platform->list);
3411 mutex_unlock(&client_mutex);
3412
3413 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003414 kfree(platform->name);
3415 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003416}
3417EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3418
Mark Brown151ab222009-05-09 16:22:58 +01003419static u64 codec_format_map[] = {
3420 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3421 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3422 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3423 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3424 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3425 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3426 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3427 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3428 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3429 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3430 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3431 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3432 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3433 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3434 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3435 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3436};
3437
3438/* Fix up the DAI formats for endianness: codecs don't actually see
3439 * the endianness of the data but we're using the CPU format
3440 * definitions which do need to include endianness so we ensure that
3441 * codec DAIs always have both big and little endian variants set.
3442 */
3443static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3444{
3445 int i;
3446
3447 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3448 if (stream->formats & codec_format_map[i])
3449 stream->formats |= codec_format_map[i];
3450}
3451
Mark Brown0d0cf002008-12-10 14:32:45 +00003452/**
3453 * snd_soc_register_codec - Register a codec with the ASoC core
3454 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003455 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003456 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003457int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003458 const struct snd_soc_codec_driver *codec_drv,
3459 struct snd_soc_dai_driver *dai_drv,
3460 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003461{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003462 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463 struct snd_soc_codec *codec;
3464 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003468 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3469 if (codec == NULL)
3470 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003472 /* create CODEC component name */
3473 codec->name = fmt_single_name(dev, &codec->id);
3474 if (codec->name == NULL) {
3475 kfree(codec);
3476 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003477 }
3478
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003479 if (codec_drv->compress_type)
3480 codec->compress_type = codec_drv->compress_type;
3481 else
3482 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3483
Mark Brownc3acec22010-12-02 16:15:29 +00003484 codec->write = codec_drv->write;
3485 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003486 codec->volatile_register = codec_drv->volatile_register;
3487 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003488 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003489 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003490 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3491 codec->dapm.dev = dev;
3492 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003493 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003494 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003495 codec->dev = dev;
3496 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003497 codec->num_dai = num_dai;
3498 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003499
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003500 /* allocate CODEC register cache */
3501 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003502 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003503 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003504 /* it is necessary to make a copy of the default register cache
3505 * because in the case of using a compression type that requires
3506 * the default register cache to be marked as __devinitconst the
3507 * kernel might have freed the array by the time we initialize
3508 * the cache.
3509 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003510 if (codec_drv->reg_cache_default) {
3511 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3512 reg_size, GFP_KERNEL);
3513 if (!codec->reg_def_copy) {
3514 ret = -ENOMEM;
3515 goto fail;
3516 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003517 }
3518 }
3519
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003520 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3521 if (!codec->volatile_register)
3522 codec->volatile_register = snd_soc_default_volatile_register;
3523 if (!codec->readable_register)
3524 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003525 if (!codec->writable_register)
3526 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003527 }
3528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003529 for (i = 0; i < num_dai; i++) {
3530 fixup_codec_formats(&dai_drv[i].playback);
3531 fixup_codec_formats(&dai_drv[i].capture);
3532 }
3533
Mark Brown26b01cc2010-08-18 20:20:55 +01003534 /* register any DAIs */
3535 if (num_dai) {
3536 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3537 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003538 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003539 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003540
Mark Brown0d0cf002008-12-10 14:32:45 +00003541 mutex_lock(&client_mutex);
3542 list_add(&codec->list, &codec_list);
3543 snd_soc_instantiate_cards();
3544 mutex_unlock(&client_mutex);
3545
3546 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003547 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003549fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003550 kfree(codec->reg_def_copy);
3551 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552 kfree(codec->name);
3553 kfree(codec);
3554 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003555}
3556EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3557
3558/**
3559 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3560 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003561 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003562 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003563void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003564{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003565 struct snd_soc_codec *codec;
3566 int i;
3567
3568 list_for_each_entry(codec, &codec_list, list) {
3569 if (dev == codec->dev)
3570 goto found;
3571 }
3572 return;
3573
3574found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003575 if (codec->num_dai)
3576 for (i = 0; i < codec->num_dai; i++)
3577 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578
Mark Brown0d0cf002008-12-10 14:32:45 +00003579 mutex_lock(&client_mutex);
3580 list_del(&codec->list);
3581 mutex_unlock(&client_mutex);
3582
3583 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003585 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003586 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003587 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003588 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003589}
3590EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3591
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003592/* Retrieve a card's name from device tree */
3593int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3594 const char *propname)
3595{
3596 struct device_node *np = card->dev->of_node;
3597 int ret;
3598
3599 ret = of_property_read_string_index(np, propname, 0, &card->name);
3600 /*
3601 * EINVAL means the property does not exist. This is fine providing
3602 * card->name was previously set, which is checked later in
3603 * snd_soc_register_card.
3604 */
3605 if (ret < 0 && ret != -EINVAL) {
3606 dev_err(card->dev,
3607 "Property '%s' could not be read: %d\n",
3608 propname, ret);
3609 return ret;
3610 }
3611
3612 return 0;
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3615
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003616int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3617 const char *propname)
3618{
3619 struct device_node *np = card->dev->of_node;
3620 int num_routes;
3621 struct snd_soc_dapm_route *routes;
3622 int i, ret;
3623
3624 num_routes = of_property_count_strings(np, propname);
3625 if (num_routes & 1) {
3626 dev_err(card->dev,
3627 "Property '%s's length is not even\n",
3628 propname);
3629 return -EINVAL;
3630 }
3631 num_routes /= 2;
3632 if (!num_routes) {
3633 dev_err(card->dev,
3634 "Property '%s's length is zero\n",
3635 propname);
3636 return -EINVAL;
3637 }
3638
3639 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3640 GFP_KERNEL);
3641 if (!routes) {
3642 dev_err(card->dev,
3643 "Could not allocate DAPM route table\n");
3644 return -EINVAL;
3645 }
3646
3647 for (i = 0; i < num_routes; i++) {
3648 ret = of_property_read_string_index(np, propname,
3649 2 * i, &routes[i].sink);
3650 if (ret) {
3651 dev_err(card->dev,
3652 "Property '%s' index %d could not be read: %d\n",
3653 propname, 2 * i, ret);
3654 return -EINVAL;
3655 }
3656 ret = of_property_read_string_index(np, propname,
3657 (2 * i) + 1, &routes[i].source);
3658 if (ret) {
3659 dev_err(card->dev,
3660 "Property '%s' index %d could not be read: %d\n",
3661 propname, (2 * i) + 1, ret);
3662 return -EINVAL;
3663 }
3664 }
3665
3666 card->num_dapm_routes = num_routes;
3667 card->dapm_routes = routes;
3668
3669 return 0;
3670}
3671EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3672
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003673static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003674{
Mark Brown384c89e2008-12-03 17:34:03 +00003675#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003676 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3677 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003678 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003679 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003680 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003681
Mark Brown8a9dab12011-01-10 22:25:21 +00003682 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003683 &codec_list_fops))
3684 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3685
Mark Brown8a9dab12011-01-10 22:25:21 +00003686 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003687 &dai_list_fops))
3688 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003689
Mark Brown8a9dab12011-01-10 22:25:21 +00003690 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003691 &platform_list_fops))
3692 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003693#endif
3694
Mark Brownfb257892011-04-28 10:57:54 +01003695 snd_soc_util_init();
3696
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003697 return platform_driver_register(&soc_driver);
3698}
Mark Brown4abe8e12010-10-12 17:41:03 +01003699module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003700
Mark Brown7d8c16a2008-11-30 22:11:24 +00003701static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003702{
Mark Brownfb257892011-04-28 10:57:54 +01003703 snd_soc_util_exit();
3704
Mark Brown384c89e2008-12-03 17:34:03 +00003705#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003706 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003707#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003708 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003709}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003710module_exit(snd_soc_exit);
3711
3712/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003713MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003714MODULE_DESCRIPTION("ALSA SoC Core");
3715MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003716MODULE_ALIAS("platform:soc-audio");