blob: a539ade477af3429e885941639337fb1e304ee09 [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>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
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
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100468struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
469 const char *dai_link, int stream)
470{
471 int i;
472
473 for (i = 0; i < card->num_links; i++) {
474 if (card->rtd[i].dai_link->no_pcm &&
475 !strcmp(card->rtd[i].dai_link->name, dai_link))
476 return card->rtd[i].pcm->streams[stream].substream;
477 }
478 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
479 return NULL;
480}
481EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
482
483struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
484 const char *dai_link)
485{
486 int i;
487
488 for (i = 0; i < card->num_links; i++) {
489 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
490 return &card->rtd[i];
491 }
492 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
493 return NULL;
494}
495EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
496
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497#ifdef CONFIG_SND_SOC_AC97_BUS
498/* unregister ac97 codec */
499static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
500{
501 if (codec->ac97->dev.bus)
502 device_unregister(&codec->ac97->dev);
503 return 0;
504}
505
506/* stop no dev release warning */
507static void soc_ac97_device_release(struct device *dev){}
508
509/* register ac97 codec to bus */
510static int soc_ac97_dev_register(struct snd_soc_codec *codec)
511{
512 int err;
513
514 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100515 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 codec->ac97->dev.release = soc_ac97_device_release;
517
Kay Sieversbb072bf2008-11-02 03:50:35 +0100518 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 err = device_register(&codec->ac97->dev);
521 if (err < 0) {
522 snd_printk(KERN_ERR "Can't register ac97 bus\n");
523 codec->ac97->dev.bus = NULL;
524 return err;
525 }
526 return 0;
527}
528#endif
529
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000530#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000534 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200535 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 int i;
537
Daniel Macke3509ff2009-06-03 17:44:49 +0200538 /* If the initialization of this soc device failed, there is no codec
539 * associated with it. Just bail out in this case.
540 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200542 return 0;
543
Andy Green6ed25972008-06-13 16:24:05 +0100544 /* Due to the resume being scheduled into a workqueue we could
545 * suspend before that's finished - wait for it to complete.
546 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 snd_power_lock(card->snd_card);
548 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
549 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100550
551 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100553
Mark Browna00f90f2010-12-02 16:24:24 +0000554 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 for (i = 0; i < card->num_rtd; i++) {
556 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
557 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100560 continue;
561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (drv->ops->digital_mute && dai->playback_active)
563 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 }
565
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100566 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 for (i = 0; i < card->num_rtd; i++) {
568 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100569 continue;
570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100573
Mark Brown87506542008-11-18 20:50:34 +0000574 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000575 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 for (i = 0; i < card->num_rtd; i++) {
578 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
579 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100582 continue;
583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
585 cpu_dai->driver->suspend(cpu_dai);
586 if (platform->driver->suspend && !platform->suspended) {
587 platform->driver->suspend(cpu_dai);
588 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100589 }
590 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 /* close any waiting streams and save state */
593 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100594 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599
600 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100601 continue;
602
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800603 snd_soc_dapm_stream_event(&card->rtd[i],
604 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(&card->rtd[i],
608 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800609 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 }
611
612 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200613 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 /* If there are paths active then the CODEC will be held with
615 * bias _ON and should not be suspended. */
616 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000619 /*
620 * If the CODEC is capable of idle
621 * bias off then being in STANDBY
622 * means it's doing something,
623 * otherwise fall through.
624 */
625 if (codec->dapm.idle_bias_off) {
626 dev_dbg(codec->dev,
627 "idle_bias_off CODEC on over suspend\n");
628 break;
629 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100631 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900633 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 break;
635 default:
636 dev_dbg(codec->dev, "CODEC is on over suspend\n");
637 break;
638 }
639 }
640 }
641
642 for (i = 0; i < card->num_rtd; i++) {
643 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
644
645 if (card->rtd[i].dai_link->ignore_suspend)
646 continue;
647
648 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
649 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650 }
651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000653 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
655 return 0;
656}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000657EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Andy Green6ed25972008-06-13 16:24:05 +0100659/* deferred resume work, so resume can complete before we finished
660 * setting our codec back up, which can be very slow on I2C
661 */
662static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 struct snd_soc_card *card =
665 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200666 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 int i;
668
Andy Green6ed25972008-06-13 16:24:05 +0100669 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
670 * so userspace apps are blocked from touching us
671 */
672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100674
Mark Brown9949788b2010-05-07 20:24:05 +0100675 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100677
Mark Brown87506542008-11-18 20:50:34 +0000678 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000679 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 /* resume AC97 DAIs */
682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100686 continue;
687
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
689 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200692 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 /* If the CODEC was idle over suspend then it will have been
694 * left with bias OFF or STANDBY and suspended so we must now
695 * resume. Otherwise the suspend was suppressed.
696 */
697 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 case SND_SOC_BIAS_STANDBY:
700 case SND_SOC_BIAS_OFF:
701 codec->driver->resume(codec);
702 codec->suspended = 0;
703 break;
704 default:
705 dev_dbg(codec->dev, "CODEC was on over suspend\n");
706 break;
707 }
Mark Brown1547aba2010-05-07 21:11:40 +0100708 }
709 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100714 continue;
715
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800716 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000717 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800718 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800720 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000721 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800722 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 for (i = 0; i < card->num_rtd; i++) {
727 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
728 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100731 continue;
732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (drv->ops->digital_mute && dai->playback_active)
734 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 }
736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100742 continue;
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
745 cpu_dai->driver->resume(cpu_dai);
746 if (platform->driver->resume && platform->suspended) {
747 platform->driver->resume(cpu_dai);
748 platform->suspended = 0;
749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Mark Brown87506542008-11-18 20:50:34 +0000752 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000753 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100756
757 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100759}
760
761/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100763{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000764 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600765 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200766
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800767 /* If the initialization of this soc device failed, there is no codec
768 * associated with it. Just bail out in this case.
769 */
770 if (list_empty(&card->codec_dev_list))
771 return 0;
772
Mark Brown64ab9ba2009-03-31 11:27:03 +0100773 /* AC97 devices might have other drivers hanging off them so
774 * need to resume immediately. Other drivers don't have that
775 * problem and may take a substantial amount of time to resume
776 * due to I/O costs and anti-pop so handle them out of line.
777 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 ac97_control |= cpu_dai->driver->ac97_control;
781 }
782 if (ac97_control) {
783 dev_dbg(dev, "Resuming AC97 immediately\n");
784 soc_resume_deferred(&card->deferred_resume_work);
785 } else {
786 dev_dbg(dev, "Scheduling resume work\n");
787 if (!schedule_work(&card->deferred_resume_work))
788 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 }
Andy Green6ed25972008-06-13 16:24:05 +0100790
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return 0;
792}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000793EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000795#define snd_soc_suspend NULL
796#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797#endif
798
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100799static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800800};
801
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
805 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000806 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000807 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100809 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000812
Mark Brownb19e6e72012-03-14 21:18:39 +0000813 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600815 if (dai_link->cpu_of_node &&
816 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
817 continue;
818 if (dai_link->cpu_name &&
819 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
820 continue;
821 if (dai_link->cpu_dai_name &&
822 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
823 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700824
825 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000827
828 if (!rtd->cpu_dai) {
829 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000831 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000832 }
833
Mark Brownb19e6e72012-03-14 21:18:39 +0000834 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700836 if (dai_link->codec_of_node) {
837 if (codec->dev->of_node != dai_link->codec_of_node)
838 continue;
839 } else {
840 if (strcmp(codec->name, dai_link->codec_name))
841 continue;
842 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000843
Stephen Warren2610ab72011-12-07 13:58:27 -0700844 rtd->codec = codec;
845
846 /*
847 * CODEC found, so find CODEC DAI from registered DAIs from
848 * this CODEC
849 */
850 list_for_each_entry(codec_dai, &dai_list, list) {
851 if (codec->dev == codec_dai->dev &&
852 !strcmp(codec_dai->name,
853 dai_link->codec_dai_name)) {
854
855 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000858
859 if (!rtd->codec_dai) {
860 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700861 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 return -EPROBE_DEFER;
863 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865
Mark Brownb19e6e72012-03-14 21:18:39 +0000866 if (!rtd->codec) {
867 dev_dbg(card->dev, "CODEC %s not registered\n",
868 dai_link->codec_name);
869 return -EPROBE_DEFER;
870 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100871
872 /* if there's no platform we match on the empty platform */
873 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700874 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100875 platform_name = "snd-soc-dummy";
876
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700879 if (dai_link->platform_of_node) {
880 if (platform->dev->of_node !=
881 dai_link->platform_of_node)
882 continue;
883 } else {
884 if (strcmp(platform->name, platform_name))
885 continue;
886 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700887
888 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 if (!rtd->platform) {
891 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000893 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000895
896 card->num_rtd++;
897
898 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899}
900
Stephen Warrend12cd192012-06-08 12:34:22 -0600901static int soc_remove_platform(struct snd_soc_platform *platform)
902{
903 int ret;
904
905 if (platform->driver->remove) {
906 ret = platform->driver->remove(platform);
907 if (ret < 0)
908 pr_err("asoc: failed to remove %s: %d\n",
909 platform->name, ret);
910 }
911
912 /* Make sure all DAPM widgets are freed */
913 snd_soc_dapm_free(&platform->dapm);
914
915 soc_cleanup_platform_debugfs(platform);
916 platform->probed = 0;
917 list_del(&platform->card_list);
918 module_put(platform->dev->driver->owner);
919
920 return 0;
921}
922
Jarkko Nikula589c3562010-12-06 16:27:07 +0200923static void soc_remove_codec(struct snd_soc_codec *codec)
924{
925 int err;
926
927 if (codec->driver->remove) {
928 err = codec->driver->remove(codec);
929 if (err < 0)
930 dev_err(codec->dev,
931 "asoc: failed to remove %s: %d\n",
932 codec->name, err);
933 }
934
935 /* Make sure all DAPM widgets are freed */
936 snd_soc_dapm_free(&codec->dapm);
937
938 soc_cleanup_codec_debugfs(codec);
939 codec->probed = 0;
940 list_del(&codec->card_list);
941 module_put(codec->dev->driver->owner);
942}
943
Liam Girdwood0168bf02011-06-07 16:08:05 +0100944static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945{
946 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
947 struct snd_soc_codec *codec = rtd->codec;
948 struct snd_soc_platform *platform = rtd->platform;
949 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
950 int err;
951
952 /* unregister the rtd device */
953 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800954 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
955 device_remove_file(rtd->dev, &dev_attr_codec_reg);
956 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 rtd->dev_registered = 0;
958 }
959
960 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100961 if (codec_dai && codec_dai->probed &&
962 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963 if (codec_dai->driver->remove) {
964 err = codec_dai->driver->remove(codec_dai);
965 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200966 pr_err("asoc: failed to remove %s: %d\n",
967 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 }
969 codec_dai->probed = 0;
970 list_del(&codec_dai->card_list);
971 }
972
973 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100974 if (platform && platform->probed &&
Stephen Warrend12cd192012-06-08 12:34:22 -0600975 platform->driver->remove_order == order)
976 soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977
978 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100979 if (codec && codec->probed &&
980 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200981 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982
983 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100984 if (cpu_dai && cpu_dai->probed &&
985 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 if (cpu_dai->driver->remove) {
987 err = cpu_dai->driver->remove(cpu_dai);
988 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200989 pr_err("asoc: failed to remove %s: %d\n",
990 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 }
992 cpu_dai->probed = 0;
993 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600994
Stephen Warren18d75642012-06-08 12:34:21 -0600995 if (!cpu_dai->codec) {
996 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600997 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -0600998 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000999 }
1000}
1001
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001002static void soc_remove_dai_links(struct snd_soc_card *card)
1003{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001004 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001005
Liam Girdwood0168bf02011-06-07 16:08:05 +01001006 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1007 order++) {
1008 for (dai = 0; dai < card->num_rtd; dai++)
1009 soc_remove_dai_link(card, dai, order);
1010 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001011 card->num_rtd = 0;
1012}
1013
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001014static void soc_set_name_prefix(struct snd_soc_card *card,
1015 struct snd_soc_codec *codec)
1016{
1017 int i;
1018
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001019 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001020 return;
1021
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001022 for (i = 0; i < card->num_configs; i++) {
1023 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001024 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1025 codec->name_prefix = map->name_prefix;
1026 break;
1027 }
1028 }
1029}
1030
Jarkko Nikula589c3562010-12-06 16:27:07 +02001031static int soc_probe_codec(struct snd_soc_card *card,
1032 struct snd_soc_codec *codec)
1033{
1034 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001035 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001036 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001037
1038 codec->card = card;
1039 codec->dapm.card = card;
1040 soc_set_name_prefix(card, codec);
1041
Jarkko Nikula70d293312011-01-27 16:24:22 +02001042 if (!try_module_get(codec->dev->driver->owner))
1043 return -ENODEV;
1044
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001045 soc_init_codec_debugfs(codec);
1046
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001047 if (driver->dapm_widgets)
1048 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1049 driver->num_dapm_widgets);
1050
Mark Brown888df392012-02-16 19:37:51 -08001051 /* Create DAPM widgets for each DAI stream */
1052 list_for_each_entry(dai, &dai_list, list) {
1053 if (dai->dev != codec->dev)
1054 continue;
1055
1056 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1057 }
1058
Mark Brown33c5f962011-08-22 18:40:30 +01001059 codec->dapm.idle_bias_off = driver->idle_bias_off;
1060
Mark Brown89b95ac02011-03-07 16:38:44 +00001061 if (driver->probe) {
1062 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001063 if (ret < 0) {
1064 dev_err(codec->dev,
1065 "asoc: failed to probe CODEC %s: %d\n",
1066 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001067 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001068 }
1069 }
1070
Mark Brownb7af1da2011-04-07 19:18:44 +09001071 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001072 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001073 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001074 if (driver->dapm_routes)
1075 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1076 driver->num_dapm_routes);
1077
Jarkko Nikula589c3562010-12-06 16:27:07 +02001078 /* mark codec as probed and add to card codec list */
1079 codec->probed = 1;
1080 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001081 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001082
Jarkko Nikula70d293312011-01-27 16:24:22 +02001083 return 0;
1084
1085err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001086 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001087 module_put(codec->dev->driver->owner);
1088
Jarkko Nikula589c3562010-12-06 16:27:07 +02001089 return ret;
1090}
1091
Liam Girdwood956245e2011-07-01 16:54:08 +01001092static int soc_probe_platform(struct snd_soc_card *card,
1093 struct snd_soc_platform *platform)
1094{
1095 int ret = 0;
1096 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001097 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001098
1099 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001100 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001101
1102 if (!try_module_get(platform->dev->driver->owner))
1103 return -ENODEV;
1104
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001105 soc_init_platform_debugfs(platform);
1106
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001107 if (driver->dapm_widgets)
1108 snd_soc_dapm_new_controls(&platform->dapm,
1109 driver->dapm_widgets, driver->num_dapm_widgets);
1110
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001111 /* Create DAPM widgets for each DAI stream */
1112 list_for_each_entry(dai, &dai_list, list) {
1113 if (dai->dev != platform->dev)
1114 continue;
1115
1116 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1117 }
1118
Stephen Warren3fec6b62012-04-05 12:28:01 -06001119 platform->dapm.idle_bias_off = 1;
1120
Liam Girdwood956245e2011-07-01 16:54:08 +01001121 if (driver->probe) {
1122 ret = driver->probe(platform);
1123 if (ret < 0) {
1124 dev_err(platform->dev,
1125 "asoc: failed to probe platform %s: %d\n",
1126 platform->name, ret);
1127 goto err_probe;
1128 }
1129 }
1130
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001131 if (driver->controls)
1132 snd_soc_add_platform_controls(platform, driver->controls,
1133 driver->num_controls);
1134 if (driver->dapm_routes)
1135 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1136 driver->num_dapm_routes);
1137
Liam Girdwood956245e2011-07-01 16:54:08 +01001138 /* mark platform as probed and add to card platform list */
1139 platform->probed = 1;
1140 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001141 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001142
1143 return 0;
1144
1145err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001146 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001147 module_put(platform->dev->driver->owner);
1148
1149 return ret;
1150}
1151
Mark Brown36ae1a92012-01-06 17:12:45 -08001152static void rtd_release(struct device *dev)
1153{
1154 kfree(dev);
1155}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156
Jarkko Nikula589c3562010-12-06 16:27:07 +02001157static int soc_post_component_init(struct snd_soc_card *card,
1158 struct snd_soc_codec *codec,
1159 int num, int dailess)
1160{
1161 struct snd_soc_dai_link *dai_link = NULL;
1162 struct snd_soc_aux_dev *aux_dev = NULL;
1163 struct snd_soc_pcm_runtime *rtd;
1164 const char *temp, *name;
1165 int ret = 0;
1166
1167 if (!dailess) {
1168 dai_link = &card->dai_link[num];
1169 rtd = &card->rtd[num];
1170 name = dai_link->name;
1171 } else {
1172 aux_dev = &card->aux_dev[num];
1173 rtd = &card->rtd_aux[num];
1174 name = aux_dev->name;
1175 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001176 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001177
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001178 /* Make sure all DAPM widgets are instantiated */
1179 snd_soc_dapm_new_widgets(&codec->dapm);
1180
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181 /* machine controls, routes and widgets are not prefixed */
1182 temp = codec->name_prefix;
1183 codec->name_prefix = NULL;
1184
1185 /* do machine specific initialization */
1186 if (!dailess && dai_link->init)
1187 ret = dai_link->init(rtd);
1188 else if (dailess && aux_dev->init)
1189 ret = aux_dev->init(&codec->dapm);
1190 if (ret < 0) {
1191 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1192 return ret;
1193 }
1194 codec->name_prefix = temp;
1195
Jarkko Nikula589c3562010-12-06 16:27:07 +02001196 /* register the rtd device */
1197 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001198
1199 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1200 if (!rtd->dev)
1201 return -ENOMEM;
1202 device_initialize(rtd->dev);
1203 rtd->dev->parent = card->dev;
1204 rtd->dev->release = rtd_release;
1205 rtd->dev->init_name = name;
1206 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001207 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001208 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1209 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1210 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1211 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001212 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001213 if (ret < 0) {
1214 dev_err(card->dev,
1215 "asoc: failed to register runtime device: %d\n", ret);
1216 return ret;
1217 }
1218 rtd->dev_registered = 1;
1219
1220 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001221 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001222 if (ret < 0)
1223 dev_err(codec->dev,
1224 "asoc: failed to add codec dapm sysfs entries: %d\n",
1225 ret);
1226
1227 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001228 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001229 if (ret < 0)
1230 dev_err(codec->dev,
1231 "asoc: failed to add codec sysfs files: %d\n", ret);
1232
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001233#ifdef CONFIG_DEBUG_FS
1234 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001235 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001236 goto out;
1237
1238 ret = soc_dpcm_debugfs_add(rtd);
1239 if (ret < 0)
1240 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1241
1242out:
1243#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 return 0;
1245}
1246
Liam Girdwood0168bf02011-06-07 16:08:05 +01001247static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248{
1249 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1250 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1251 struct snd_soc_codec *codec = rtd->codec;
1252 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001253 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1254 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1255 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256 int ret;
1257
Liam Girdwood0168bf02011-06-07 16:08:05 +01001258 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1259 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001260
1261 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001262 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263 codec_dai->card = card;
1264 cpu_dai->card = card;
1265
1266 /* set default power off timeout */
1267 rtd->pmdown_time = pmdown_time;
1268
1269 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001270 if (!cpu_dai->probed &&
1271 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001272 if (!cpu_dai->codec) {
1273 cpu_dai->dapm.card = card;
1274 if (!try_module_get(cpu_dai->dev->driver->owner))
1275 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001276
Stephen Warren18d75642012-06-08 12:34:21 -06001277 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001278 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1279 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001281 if (cpu_dai->driver->probe) {
1282 ret = cpu_dai->driver->probe(cpu_dai);
1283 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001284 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1285 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001286 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001287 return ret;
1288 }
1289 }
1290 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001291 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001292 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1293 }
1294
1295 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001296 if (!codec->probed &&
1297 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001298 ret = soc_probe_codec(card, codec);
1299 if (ret < 0)
1300 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001301 }
1302
1303 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001304 if (!platform->probed &&
1305 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001306 ret = soc_probe_platform(card, platform);
1307 if (ret < 0)
1308 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001309 }
1310
1311 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001312 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001313 if (codec_dai->driver->probe) {
1314 ret = codec_dai->driver->probe(codec_dai);
1315 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001316 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1317 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001318 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001319 }
1320 }
1321
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001322 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001323 codec_dai->probed = 1;
1324 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001325 }
1326
Liam Girdwood0168bf02011-06-07 16:08:05 +01001327 /* complete DAI probe during last probe */
1328 if (order != SND_SOC_COMP_ORDER_LAST)
1329 return 0;
1330
Jarkko Nikula589c3562010-12-06 16:27:07 +02001331 ret = soc_post_component_init(card, codec, num, 0);
1332 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001333 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001334
Mark Brown36ae1a92012-01-06 17:12:45 -08001335 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001337 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001338
Mark Brownc74184e2012-04-04 22:12:09 +01001339 if (!dai_link->params) {
1340 /* create the pcm */
1341 ret = soc_new_pcm(rtd, num);
1342 if (ret < 0) {
1343 pr_err("asoc: can't create pcm %s :%d\n",
1344 dai_link->stream_name, ret);
1345 return ret;
1346 }
1347 } else {
1348 /* link the DAI widgets */
1349 play_w = codec_dai->playback_widget;
1350 capture_w = cpu_dai->capture_widget;
1351 if (play_w && capture_w) {
1352 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1353 capture_w, play_w);
1354 if (ret != 0) {
1355 dev_err(card->dev, "Can't link %s to %s: %d\n",
1356 play_w->name, capture_w->name, ret);
1357 return ret;
1358 }
1359 }
1360
1361 play_w = cpu_dai->playback_widget;
1362 capture_w = codec_dai->capture_widget;
1363 if (play_w && capture_w) {
1364 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1365 capture_w, play_w);
1366 if (ret != 0) {
1367 dev_err(card->dev, "Can't link %s to %s: %d\n",
1368 play_w->name, capture_w->name, ret);
1369 return ret;
1370 }
1371 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372 }
1373
1374 /* add platform data for AC97 devices */
1375 if (rtd->codec_dai->driver->ac97_control)
1376 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1377
1378 return 0;
1379}
1380
1381#ifdef CONFIG_SND_SOC_AC97_BUS
1382static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1383{
1384 int ret;
1385
1386 /* Only instantiate AC97 if not already done by the adaptor
1387 * for the generic AC97 subsystem.
1388 */
1389 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001390 /*
1391 * It is possible that the AC97 device is already registered to
1392 * the device subsystem. This happens when the device is created
1393 * via snd_ac97_mixer(). Currently only SoC codec that does so
1394 * is the generic AC97 glue but others migh emerge.
1395 *
1396 * In those cases we don't try to register the device again.
1397 */
1398 if (!rtd->codec->ac97_created)
1399 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001400
1401 ret = soc_ac97_dev_register(rtd->codec);
1402 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001403 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001404 return ret;
1405 }
1406
1407 rtd->codec->ac97_registered = 1;
1408 }
1409 return 0;
1410}
1411
1412static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1413{
1414 if (codec->ac97_registered) {
1415 soc_ac97_dev_unregister(codec);
1416 codec->ac97_registered = 0;
1417 }
1418}
1419#endif
1420
Mark Brownb19e6e72012-03-14 21:18:39 +00001421static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1422{
1423 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1424 struct snd_soc_codec *codec;
1425
1426 /* find CODEC from registered CODECs*/
1427 list_for_each_entry(codec, &codec_list, list) {
1428 if (!strcmp(codec->name, aux_dev->codec_name))
1429 return 0;
1430 }
1431
1432 return -EPROBE_DEFER;
1433}
1434
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001435static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1436{
1437 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001438 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001439 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001440
1441 /* find CODEC from registered CODECs*/
1442 list_for_each_entry(codec, &codec_list, list) {
1443 if (!strcmp(codec->name, aux_dev->codec_name)) {
1444 if (codec->probed) {
1445 dev_err(codec->dev,
1446 "asoc: codec already probed");
1447 ret = -EBUSY;
1448 goto out;
1449 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001450 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001451 }
1452 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001453 /* codec not found */
1454 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001455 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001456
Jarkko Nikula676ad982010-12-03 09:18:22 +02001457found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001458 ret = soc_probe_codec(card, codec);
1459 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001460 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001461
Jarkko Nikula589c3562010-12-06 16:27:07 +02001462 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001463
1464out:
1465 return ret;
1466}
1467
1468static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1469{
1470 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1471 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001472
1473 /* unregister the rtd device */
1474 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001475 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1476 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001477 rtd->dev_registered = 0;
1478 }
1479
Jarkko Nikula589c3562010-12-06 16:27:07 +02001480 if (codec && codec->probed)
1481 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001482}
1483
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001484static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1485 enum snd_soc_compress_type compress_type)
1486{
1487 int ret;
1488
1489 if (codec->cache_init)
1490 return 0;
1491
1492 /* override the compress_type if necessary */
1493 if (compress_type && codec->compress_type != compress_type)
1494 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001495 ret = snd_soc_cache_init(codec);
1496 if (ret < 0) {
1497 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1498 ret);
1499 return ret;
1500 }
1501 codec->cache_init = 1;
1502 return 0;
1503}
1504
Mark Brownb19e6e72012-03-14 21:18:39 +00001505static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001506{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001507 struct snd_soc_codec *codec;
1508 struct snd_soc_codec_conf *codec_conf;
1509 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001510 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001511 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001512
Liam Girdwood01b9d992012-03-07 10:38:25 +00001513 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514
Mark Brownb19e6e72012-03-14 21:18:39 +00001515 /* bind DAIs */
1516 for (i = 0; i < card->num_links; i++) {
1517 ret = soc_bind_dai_link(card, i);
1518 if (ret != 0)
1519 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 }
1521
Mark Brownb19e6e72012-03-14 21:18:39 +00001522 /* check aux_devs too */
1523 for (i = 0; i < card->num_aux_devs; i++) {
1524 ret = soc_check_aux_dev(card, i);
1525 if (ret != 0)
1526 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 }
1528
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001529 /* initialize the register cache for each available codec */
1530 list_for_each_entry(codec, &codec_list, list) {
1531 if (codec->cache_init)
1532 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001533 /* by default we don't override the compress_type */
1534 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001535 /* check to see if we need to override the compress_type */
1536 for (i = 0; i < card->num_configs; ++i) {
1537 codec_conf = &card->codec_conf[i];
1538 if (!strcmp(codec->name, codec_conf->dev_name)) {
1539 compress_type = codec_conf->compress_type;
1540 if (compress_type && compress_type
1541 != codec->compress_type)
1542 break;
1543 }
1544 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001545 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001546 if (ret < 0)
1547 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001548 }
1549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001550 /* card bind complete so register a sound card */
1551 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1552 card->owner, 0, &card->snd_card);
1553 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001554 pr_err("asoc: can't create sound card for card %s: %d\n",
1555 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001556 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001557 }
1558 card->snd_card->dev = card->dev;
1559
Mark Browne37a4972011-03-02 18:21:57 +00001560 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1561 card->dapm.dev = card->dev;
1562 card->dapm.card = card;
1563 list_add(&card->dapm.list, &card->dapm_list);
1564
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001565#ifdef CONFIG_DEBUG_FS
1566 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1567#endif
1568
Mark Brown88ee1c62011-02-02 10:43:26 +00001569#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001570 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001571 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001572#endif
Andy Green6ed25972008-06-13 16:24:05 +01001573
Mark Brown9a841eb2011-04-12 17:51:37 -07001574 if (card->dapm_widgets)
1575 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1576 card->num_dapm_widgets);
1577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 /* initialise the sound card only once */
1579 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001580 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 if (ret < 0)
1582 goto card_probe_error;
1583 }
1584
Liam Girdwood0168bf02011-06-07 16:08:05 +01001585 /* early DAI link probe */
1586 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1587 order++) {
1588 for (i = 0; i < card->num_links; i++) {
1589 ret = soc_probe_dai_link(card, i, order);
1590 if (ret < 0) {
1591 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001592 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001593 goto probe_dai_err;
1594 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 }
1596 }
1597
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001598 for (i = 0; i < card->num_aux_devs; i++) {
1599 ret = soc_probe_aux_dev(card, i);
1600 if (ret < 0) {
1601 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1602 card->name, ret);
1603 goto probe_aux_dev_err;
1604 }
1605 }
1606
Mark Brown888df392012-02-16 19:37:51 -08001607 snd_soc_dapm_link_dai_widgets(card);
1608
Mark Brownb7af1da2011-04-07 19:18:44 +09001609 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001610 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001611
Mark Brownb8ad29d2011-03-02 18:35:51 +00001612 if (card->dapm_routes)
1613 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1614 card->num_dapm_routes);
1615
Mark Brownb90d2f92011-10-10 13:38:06 +01001616 snd_soc_dapm_new_widgets(&card->dapm);
1617
Mark Brown75d9ac42011-09-27 16:41:01 +01001618 for (i = 0; i < card->num_links; i++) {
1619 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001620 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001621
Mark Brownf04209a2012-04-09 16:29:21 +01001622 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001623 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001624 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001625 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001626 dev_warn(card->rtd[i].codec_dai->dev,
1627 "Failed to set DAI format: %d\n",
1628 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001629 }
1630
1631 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001632 if (dai_fmt &&
1633 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001634 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1635 dai_fmt);
1636 if (ret != 0 && ret != -ENOTSUPP)
1637 dev_warn(card->rtd[i].cpu_dai->dev,
1638 "Failed to set DAI format: %d\n",
1639 ret);
1640 } else if (dai_fmt) {
1641 /* Flip the polarity for the "CPU" end */
1642 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1643 switch (dai_link->dai_fmt &
1644 SND_SOC_DAIFMT_MASTER_MASK) {
1645 case SND_SOC_DAIFMT_CBM_CFM:
1646 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1647 break;
1648 case SND_SOC_DAIFMT_CBM_CFS:
1649 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1650 break;
1651 case SND_SOC_DAIFMT_CBS_CFM:
1652 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1653 break;
1654 case SND_SOC_DAIFMT_CBS_CFS:
1655 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1656 break;
1657 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001658
1659 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001660 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001661 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001662 dev_warn(card->rtd[i].cpu_dai->dev,
1663 "Failed to set DAI format: %d\n",
1664 ret);
1665 }
1666 }
1667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001670 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1671 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001672 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1673 "%s", card->driver_name ? card->driver_name : card->name);
1674 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1675 switch (card->snd_card->driver[i]) {
1676 case '_':
1677 case '-':
1678 case '\0':
1679 break;
1680 default:
1681 if (!isalnum(card->snd_card->driver[i]))
1682 card->snd_card->driver[i] = '_';
1683 break;
1684 }
1685 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686
Mark Brown28e9ad92011-03-02 18:36:34 +00001687 if (card->late_probe) {
1688 ret = card->late_probe(card);
1689 if (ret < 0) {
1690 dev_err(card->dev, "%s late_probe() failed: %d\n",
1691 card->name, ret);
1692 goto probe_aux_dev_err;
1693 }
1694 }
1695
Mark Brown2dc00212011-10-08 13:59:44 +01001696 snd_soc_dapm_new_widgets(&card->dapm);
1697
Stephen Warren16332812011-11-23 12:42:04 -07001698 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001699 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001700 snd_soc_dapm_auto_nc_codec_pins(codec);
1701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702 ret = snd_card_register(card->snd_card);
1703 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001704 pr_err("asoc: failed to register soundcard for %s: %d\n",
1705 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001706 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 }
1708
1709#ifdef CONFIG_SND_SOC_AC97_BUS
1710 /* register any AC97 codecs */
1711 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001712 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1713 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001714 pr_err("asoc: failed to register AC97 %s: %d\n",
1715 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001716 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001717 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001718 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001719 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001720 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001721#endif
1722
Mark Brown435c5e22008-12-04 15:32:53 +00001723 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001724 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001726
1727 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001728
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001729probe_aux_dev_err:
1730 for (i = 0; i < card->num_aux_devs; i++)
1731 soc_remove_aux_dev(card, i);
1732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001733probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001734 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001736card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001737 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001738 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001739
1740 snd_card_free(card->snd_card);
1741
Mark Brownb19e6e72012-03-14 21:18:39 +00001742base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001743 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001744
Mark Brownb19e6e72012-03-14 21:18:39 +00001745 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001746}
1747
1748/* probes a new socdev */
1749static int soc_probe(struct platform_device *pdev)
1750{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001752 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001753
Vinod Koul70a7ca32011-01-14 19:22:48 +05301754 /*
1755 * no card, so machine driver should be registering card
1756 * we should not be here in that case so ret error
1757 */
1758 if (!card)
1759 return -EINVAL;
1760
Mark Brownfe4085e2012-03-02 13:07:41 +00001761 dev_warn(&pdev->dev,
1762 "ASoC machine %s should use snd_soc_register_card()\n",
1763 card->name);
1764
Mark Brown435c5e22008-12-04 15:32:53 +00001765 /* Bodge while we unpick instantiation */
1766 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001767
Mark Brown435c5e22008-12-04 15:32:53 +00001768 ret = snd_soc_register_card(card);
1769 if (ret != 0) {
1770 dev_err(&pdev->dev, "Failed to register card\n");
1771 return ret;
1772 }
1773
1774 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775}
1776
Vinod Koulb0e26482011-01-13 22:48:02 +05301777static int soc_cleanup_card_resources(struct snd_soc_card *card)
1778{
Vinod Koulb0e26482011-01-13 22:48:02 +05301779 int i;
1780
1781 /* make sure any delayed work runs */
1782 for (i = 0; i < card->num_rtd; i++) {
1783 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1784 flush_delayed_work_sync(&rtd->delayed_work);
1785 }
1786
1787 /* remove auxiliary devices */
1788 for (i = 0; i < card->num_aux_devs; i++)
1789 soc_remove_aux_dev(card, i);
1790
1791 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001792 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301793
1794 soc_cleanup_card_debugfs(card);
1795
1796 /* remove the card */
1797 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001798 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301799
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001800 snd_soc_dapm_free(&card->dapm);
1801
Vinod Koulb0e26482011-01-13 22:48:02 +05301802 snd_card_free(card->snd_card);
1803 return 0;
1804
1805}
1806
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001807/* removes a socdev */
1808static int soc_remove(struct platform_device *pdev)
1809{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811
Mark Brownc5af3a22008-11-28 13:29:45 +00001812 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001813 return 0;
1814}
1815
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001816int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001817{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001818 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001819 int i;
Mark Brown51737472009-06-22 13:16:51 +01001820
1821 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001822 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001823
1824 /* Flush out pmdown_time work - we actually do want to run it
1825 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001826 for (i = 0; i < card->num_rtd; i++) {
1827 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001828 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829 }
Mark Brown51737472009-06-22 13:16:51 +01001830
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001832
1833 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001834}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001835EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001836
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001837const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301838 .suspend = snd_soc_suspend,
1839 .resume = snd_soc_resume,
1840 .freeze = snd_soc_suspend,
1841 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001842 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301843 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001844};
Stephen Warrendeb26072011-04-05 19:35:30 -06001845EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001846
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001847/* ASoC platform driver */
1848static struct platform_driver soc_driver = {
1849 .driver = {
1850 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001851 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001852 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001853 },
1854 .probe = soc_probe,
1855 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001856};
1857
Mark Brown096e49d2009-07-05 15:12:22 +01001858/**
1859 * snd_soc_codec_volatile_register: Report if a register is volatile.
1860 *
1861 * @codec: CODEC to query.
1862 * @reg: Register to query.
1863 *
1864 * Boolean function indiciating if a CODEC register is volatile.
1865 */
Mark Brown181e0552011-01-24 14:05:25 +00001866int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1867 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001868{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001869 if (codec->volatile_register)
1870 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001871 else
1872 return 0;
1873}
1874EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1875
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001876/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001877 * snd_soc_codec_readable_register: Report if a register is readable.
1878 *
1879 * @codec: CODEC to query.
1880 * @reg: Register to query.
1881 *
1882 * Boolean function indicating if a CODEC register is readable.
1883 */
1884int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1885 unsigned int reg)
1886{
1887 if (codec->readable_register)
1888 return codec->readable_register(codec, reg);
1889 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001890 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001891}
1892EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1893
1894/**
1895 * snd_soc_codec_writable_register: Report if a register is writable.
1896 *
1897 * @codec: CODEC to query.
1898 * @reg: Register to query.
1899 *
1900 * Boolean function indicating if a CODEC register is writable.
1901 */
1902int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1903 unsigned int reg)
1904{
1905 if (codec->writable_register)
1906 return codec->writable_register(codec, reg);
1907 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001908 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001909}
1910EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1911
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001912int snd_soc_platform_read(struct snd_soc_platform *platform,
1913 unsigned int reg)
1914{
1915 unsigned int ret;
1916
1917 if (!platform->driver->read) {
1918 dev_err(platform->dev, "platform has no read back\n");
1919 return -1;
1920 }
1921
1922 ret = platform->driver->read(platform, reg);
1923 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001924 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001925
1926 return ret;
1927}
1928EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1929
1930int snd_soc_platform_write(struct snd_soc_platform *platform,
1931 unsigned int reg, unsigned int val)
1932{
1933 if (!platform->driver->write) {
1934 dev_err(platform->dev, "platform has no write back\n");
1935 return -1;
1936 }
1937
1938 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001939 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001940 return platform->driver->write(platform, reg, val);
1941}
1942EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1943
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001944/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945 * snd_soc_new_ac97_codec - initailise AC97 device
1946 * @codec: audio codec
1947 * @ops: AC97 bus operations
1948 * @num: AC97 codec number
1949 *
1950 * Initialises AC97 codec resources for use by ad-hoc devices only.
1951 */
1952int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1953 struct snd_ac97_bus_ops *ops, int num)
1954{
1955 mutex_lock(&codec->mutex);
1956
1957 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1958 if (codec->ac97 == NULL) {
1959 mutex_unlock(&codec->mutex);
1960 return -ENOMEM;
1961 }
1962
1963 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1964 if (codec->ac97->bus == NULL) {
1965 kfree(codec->ac97);
1966 codec->ac97 = NULL;
1967 mutex_unlock(&codec->mutex);
1968 return -ENOMEM;
1969 }
1970
1971 codec->ac97->bus->ops = ops;
1972 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001973
1974 /*
1975 * Mark the AC97 device to be created by us. This way we ensure that the
1976 * device will be registered with the device subsystem later on.
1977 */
1978 codec->ac97_created = 1;
1979
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 mutex_unlock(&codec->mutex);
1981 return 0;
1982}
1983EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1984
1985/**
1986 * snd_soc_free_ac97_codec - free AC97 codec device
1987 * @codec: audio codec
1988 *
1989 * Frees AC97 codec device resources.
1990 */
1991void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1992{
1993 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001994#ifdef CONFIG_SND_SOC_AC97_BUS
1995 soc_unregister_ac97_dai_link(codec);
1996#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001997 kfree(codec->ac97->bus);
1998 kfree(codec->ac97);
1999 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002000 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 mutex_unlock(&codec->mutex);
2002}
2003EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2004
Mark Brownc3753702010-11-01 15:41:57 -04002005unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2006{
2007 unsigned int ret;
2008
Mark Brownc3acec22010-12-02 16:15:29 +00002009 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002010 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002011 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002012
2013 return ret;
2014}
2015EXPORT_SYMBOL_GPL(snd_soc_read);
2016
2017unsigned int snd_soc_write(struct snd_soc_codec *codec,
2018 unsigned int reg, unsigned int val)
2019{
2020 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002021 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002022 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002023}
2024EXPORT_SYMBOL_GPL(snd_soc_write);
2025
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002026unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2027 unsigned int reg, const void *data, size_t len)
2028{
2029 return codec->bulk_write_raw(codec, reg, data, len);
2030}
2031EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2032
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002033/**
2034 * snd_soc_update_bits - update codec register bits
2035 * @codec: audio codec
2036 * @reg: codec register
2037 * @mask: register mask
2038 * @value: new value
2039 *
2040 * Writes new register value.
2041 *
Timur Tabi180c3292011-01-10 15:58:13 -06002042 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043 */
2044int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002045 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046{
Mark Brown8a713da2011-12-03 12:33:55 +00002047 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002048 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002049 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Mark Brown8a713da2011-12-03 12:33:55 +00002051 if (codec->using_regmap) {
2052 ret = regmap_update_bits_check(codec->control_data, reg,
2053 mask, value, &change);
2054 } else {
2055 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002056 if (ret < 0)
2057 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002058
2059 old = ret;
2060 new = (old & ~mask) | (value & mask);
2061 change = old != new;
2062 if (change)
2063 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002064 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002065
Mark Brown8a713da2011-12-03 12:33:55 +00002066 if (ret < 0)
2067 return ret;
2068
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002069 return change;
2070}
2071EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2072
2073/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002074 * snd_soc_update_bits_locked - update codec register bits
2075 * @codec: audio codec
2076 * @reg: codec register
2077 * @mask: register mask
2078 * @value: new value
2079 *
2080 * Writes new register value, and takes the codec mutex.
2081 *
2082 * Returns 1 for change else 0.
2083 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002084int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2085 unsigned short reg, unsigned int mask,
2086 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002087{
2088 int change;
2089
2090 mutex_lock(&codec->mutex);
2091 change = snd_soc_update_bits(codec, reg, mask, value);
2092 mutex_unlock(&codec->mutex);
2093
2094 return change;
2095}
Mark Browndd1b3d52009-12-04 14:22:03 +00002096EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002097
2098/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 * snd_soc_test_bits - test register for change
2100 * @codec: audio codec
2101 * @reg: codec register
2102 * @mask: register mask
2103 * @value: new value
2104 *
2105 * Tests a register with a new value and checks if the new value is
2106 * different from the old value.
2107 *
2108 * Returns 1 for change else 0.
2109 */
2110int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002111 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112{
2113 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002114 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116 old = snd_soc_read(codec, reg);
2117 new = (old & ~mask) | value;
2118 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119
2120 return change;
2121}
2122EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2123
2124/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2126 * @substream: the pcm substream
2127 * @hw: the hardware parameters
2128 *
2129 * Sets the substream runtime hardware parameters.
2130 */
2131int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2132 const struct snd_pcm_hardware *hw)
2133{
2134 struct snd_pcm_runtime *runtime = substream->runtime;
2135 runtime->hw.info = hw->info;
2136 runtime->hw.formats = hw->formats;
2137 runtime->hw.period_bytes_min = hw->period_bytes_min;
2138 runtime->hw.period_bytes_max = hw->period_bytes_max;
2139 runtime->hw.periods_min = hw->periods_min;
2140 runtime->hw.periods_max = hw->periods_max;
2141 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2142 runtime->hw.fifo_size = hw->fifo_size;
2143 return 0;
2144}
2145EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2146
2147/**
2148 * snd_soc_cnew - create new control
2149 * @_template: control template
2150 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002151 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002152 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153 *
2154 * Create a new mixer control from a template control.
2155 *
2156 * Returns 0 for success, else error.
2157 */
2158struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002159 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002160 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161{
2162 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002163 struct snd_kcontrol *kcontrol;
2164 char *name = NULL;
2165 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166
2167 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 template.index = 0;
2169
Mark Brownefb7ac32011-03-08 17:23:24 +00002170 if (!long_name)
2171 long_name = template.name;
2172
2173 if (prefix) {
2174 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002175 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002176 if (!name)
2177 return NULL;
2178
2179 snprintf(name, name_len, "%s %s", prefix, long_name);
2180
2181 template.name = name;
2182 } else {
2183 template.name = long_name;
2184 }
2185
2186 kcontrol = snd_ctl_new1(&template, data);
2187
2188 kfree(name);
2189
2190 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002191}
2192EXPORT_SYMBOL_GPL(snd_soc_cnew);
2193
Liam Girdwood022658b2012-02-03 17:43:09 +00002194static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2195 const struct snd_kcontrol_new *controls, int num_controls,
2196 const char *prefix, void *data)
2197{
2198 int err, i;
2199
2200 for (i = 0; i < num_controls; i++) {
2201 const struct snd_kcontrol_new *control = &controls[i];
2202 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2203 control->name, prefix));
2204 if (err < 0) {
2205 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2206 return err;
2207 }
2208 }
2209
2210 return 0;
2211}
2212
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002214 * snd_soc_add_codec_controls - add an array of controls to a codec.
2215 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002216 * duplicating this code.
2217 *
2218 * @codec: codec to add controls to
2219 * @controls: array of controls to add
2220 * @num_controls: number of elements in the array
2221 *
2222 * Return 0 for success, else error.
2223 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002224int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002225 const struct snd_kcontrol_new *controls, int num_controls)
2226{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002227 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002228
Liam Girdwood022658b2012-02-03 17:43:09 +00002229 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2230 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002231}
Liam Girdwood022658b2012-02-03 17:43:09 +00002232EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002233
2234/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002235 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002236 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002237 *
2238 * @platform: platform to add controls to
2239 * @controls: array of controls to add
2240 * @num_controls: number of elements in the array
2241 *
2242 * Return 0 for success, else error.
2243 */
2244int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2245 const struct snd_kcontrol_new *controls, int num_controls)
2246{
2247 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002248
Liam Girdwood022658b2012-02-03 17:43:09 +00002249 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2250 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002251}
2252EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2253
2254/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002255 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2256 * Convenience function to add a list of controls.
2257 *
2258 * @soc_card: SoC card to add controls to
2259 * @controls: array of controls to add
2260 * @num_controls: number of elements in the array
2261 *
2262 * Return 0 for success, else error.
2263 */
2264int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2265 const struct snd_kcontrol_new *controls, int num_controls)
2266{
2267 struct snd_card *card = soc_card->snd_card;
2268
2269 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2270 NULL, soc_card);
2271}
2272EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2273
2274/**
2275 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2276 * Convienience function to add a list of controls.
2277 *
2278 * @dai: DAI to add controls to
2279 * @controls: array of controls to add
2280 * @num_controls: number of elements in the array
2281 *
2282 * Return 0 for success, else error.
2283 */
2284int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2285 const struct snd_kcontrol_new *controls, int num_controls)
2286{
2287 struct snd_card *card = dai->card->snd_card;
2288
2289 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2290 NULL, dai);
2291}
2292EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2293
2294/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295 * snd_soc_info_enum_double - enumerated double mixer info callback
2296 * @kcontrol: mixer control
2297 * @uinfo: control element information
2298 *
2299 * Callback to provide information about a double enumerated
2300 * mixer control.
2301 *
2302 * Returns 0 for success.
2303 */
2304int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_info *uinfo)
2306{
2307 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2308
2309 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2310 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002311 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002313 if (uinfo->value.enumerated.item > e->max - 1)
2314 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315 strcpy(uinfo->value.enumerated.name,
2316 e->texts[uinfo->value.enumerated.item]);
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2320
2321/**
2322 * snd_soc_get_enum_double - enumerated double mixer get callback
2323 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002324 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325 *
2326 * Callback to get the value of a double enumerated mixer.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
2333 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2334 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002335 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002337 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338 ;
2339 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002340 ucontrol->value.enumerated.item[0]
2341 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 if (e->shift_l != e->shift_r)
2343 ucontrol->value.enumerated.item[1] =
2344 (val >> e->shift_r) & (bitmask - 1);
2345
2346 return 0;
2347}
2348EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2349
2350/**
2351 * snd_soc_put_enum_double - enumerated double mixer put callback
2352 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002353 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 *
2355 * Callback to set the value of a double enumerated mixer.
2356 *
2357 * Returns 0 for success.
2358 */
2359int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2363 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002364 unsigned int val;
2365 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002366
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002367 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002369 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 return -EINVAL;
2371 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2372 mask = (bitmask - 1) << e->shift_l;
2373 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002374 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 return -EINVAL;
2376 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2377 mask |= (bitmask - 1) << e->shift_r;
2378 }
2379
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002380 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381}
2382EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2383
2384/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002385 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2386 * @kcontrol: mixer control
2387 * @ucontrol: control element information
2388 *
2389 * Callback to get the value of a double semi enumerated mixer.
2390 *
2391 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2392 * used for handling bitfield coded enumeration for example.
2393 *
2394 * Returns 0 for success.
2395 */
2396int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2398{
2399 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002400 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002401 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002402
2403 reg_val = snd_soc_read(codec, e->reg);
2404 val = (reg_val >> e->shift_l) & e->mask;
2405 for (mux = 0; mux < e->max; mux++) {
2406 if (val == e->values[mux])
2407 break;
2408 }
2409 ucontrol->value.enumerated.item[0] = mux;
2410 if (e->shift_l != e->shift_r) {
2411 val = (reg_val >> e->shift_r) & e->mask;
2412 for (mux = 0; mux < e->max; mux++) {
2413 if (val == e->values[mux])
2414 break;
2415 }
2416 ucontrol->value.enumerated.item[1] = mux;
2417 }
2418
2419 return 0;
2420}
2421EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2422
2423/**
2424 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2425 * @kcontrol: mixer control
2426 * @ucontrol: control element information
2427 *
2428 * Callback to set the value of a double semi enumerated mixer.
2429 *
2430 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2431 * used for handling bitfield coded enumeration for example.
2432 *
2433 * Returns 0 for success.
2434 */
2435int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2436 struct snd_ctl_elem_value *ucontrol)
2437{
2438 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002439 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002440 unsigned int val;
2441 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002442
2443 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2444 return -EINVAL;
2445 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2446 mask = e->mask << e->shift_l;
2447 if (e->shift_l != e->shift_r) {
2448 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2449 return -EINVAL;
2450 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2451 mask |= e->mask << e->shift_r;
2452 }
2453
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002454 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002455}
2456EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2457
2458/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2460 * @kcontrol: mixer control
2461 * @uinfo: control element information
2462 *
2463 * Callback to provide information about an external enumerated
2464 * single mixer.
2465 *
2466 * Returns 0 for success.
2467 */
2468int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_info *uinfo)
2470{
2471 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2472
2473 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2474 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002475 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002476
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002477 if (uinfo->value.enumerated.item > e->max - 1)
2478 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479 strcpy(uinfo->value.enumerated.name,
2480 e->texts[uinfo->value.enumerated.item]);
2481 return 0;
2482}
2483EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2484
2485/**
2486 * snd_soc_info_volsw_ext - external single mixer info callback
2487 * @kcontrol: mixer control
2488 * @uinfo: control element information
2489 *
2490 * Callback to provide information about a single external mixer control.
2491 *
2492 * Returns 0 for success.
2493 */
2494int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_info *uinfo)
2496{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002497 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498
Mark Brownfd5dfad2009-04-15 21:37:46 +01002499 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002500 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2501 else
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2503
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504 uinfo->count = 1;
2505 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002506 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2510
2511/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002512 * snd_soc_info_volsw - single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2515 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002516 * Callback to provide information about a single mixer control, or a double
2517 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 *
2519 * Returns 0 for success.
2520 */
2521int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_info *uinfo)
2523{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002524 struct soc_mixer_control *mc =
2525 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002526 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002528 if (!mc->platform_max)
2529 mc->platform_max = mc->max;
2530 platform_max = mc->platform_max;
2531
2532 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002533 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2534 else
2535 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2536
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002537 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002538 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002539 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 return 0;
2541}
2542EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2543
2544/**
2545 * snd_soc_get_volsw - single mixer get callback
2546 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002547 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002548 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002549 * Callback to get the value of a single mixer control, or a double mixer
2550 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 *
2552 * Returns 0 for success.
2553 */
2554int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002557 struct soc_mixer_control *mc =
2558 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002560 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002561 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002562 unsigned int shift = mc->shift;
2563 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002564 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002565 unsigned int mask = (1 << fls(max)) - 1;
2566 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567
2568 ucontrol->value.integer.value[0] =
2569 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002570 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002571 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002572 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002573
2574 if (snd_soc_volsw_is_stereo(mc)) {
2575 if (reg == reg2)
2576 ucontrol->value.integer.value[1] =
2577 (snd_soc_read(codec, reg) >> rshift) & mask;
2578 else
2579 ucontrol->value.integer.value[1] =
2580 (snd_soc_read(codec, reg2) >> shift) & mask;
2581 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002582 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002583 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002584 }
2585
2586 return 0;
2587}
2588EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2589
2590/**
2591 * snd_soc_put_volsw - single mixer put callback
2592 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002593 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002594 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002595 * Callback to set the value of a single mixer control, or a double mixer
2596 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002597 *
2598 * Returns 0 for success.
2599 */
2600int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2601 struct snd_ctl_elem_value *ucontrol)
2602{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002603 struct soc_mixer_control *mc =
2604 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002606 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002607 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002608 unsigned int shift = mc->shift;
2609 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002610 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002611 unsigned int mask = (1 << fls(max)) - 1;
2612 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002613 int err;
2614 bool type_2r = 0;
2615 unsigned int val2 = 0;
2616 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617
2618 val = (ucontrol->value.integer.value[0] & mask);
2619 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002620 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 val_mask = mask << shift;
2622 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002623 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002624 val2 = (ucontrol->value.integer.value[1] & mask);
2625 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002626 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002627 if (reg == reg2) {
2628 val_mask |= mask << rshift;
2629 val |= val2 << rshift;
2630 } else {
2631 val2 = val2 << shift;
2632 type_2r = 1;
2633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002634 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002635 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002636 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637 return err;
2638
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002639 if (type_2r)
2640 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2641
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002642 return err;
2643}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002644EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645
Mark Browne13ac2e2008-05-28 17:58:05 +01002646/**
Brian Austin1d99f242012-03-30 10:43:55 -05002647 * snd_soc_get_volsw_sx - single mixer get callback
2648 * @kcontrol: mixer control
2649 * @ucontrol: control element information
2650 *
2651 * Callback to get the value of a single mixer control, or a double mixer
2652 * control that spans 2 registers.
2653 *
2654 * Returns 0 for success.
2655 */
2656int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_value *ucontrol)
2658{
2659 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2660 struct soc_mixer_control *mc =
2661 (struct soc_mixer_control *)kcontrol->private_value;
2662
2663 unsigned int reg = mc->reg;
2664 unsigned int reg2 = mc->rreg;
2665 unsigned int shift = mc->shift;
2666 unsigned int rshift = mc->rshift;
2667 int max = mc->max;
2668 int min = mc->min;
2669 int mask = (1 << (fls(min + max) - 1)) - 1;
2670
2671 ucontrol->value.integer.value[0] =
2672 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2673
2674 if (snd_soc_volsw_is_stereo(mc))
2675 ucontrol->value.integer.value[1] =
2676 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2677
2678 return 0;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2681
2682/**
2683 * snd_soc_put_volsw_sx - double mixer set callback
2684 * @kcontrol: mixer control
2685 * @uinfo: control element information
2686 *
2687 * Callback to set the value of a double mixer control that spans 2 registers.
2688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
2694 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2695 struct soc_mixer_control *mc =
2696 (struct soc_mixer_control *)kcontrol->private_value;
2697
2698 unsigned int reg = mc->reg;
2699 unsigned int reg2 = mc->rreg;
2700 unsigned int shift = mc->shift;
2701 unsigned int rshift = mc->rshift;
2702 int max = mc->max;
2703 int min = mc->min;
2704 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002705 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002706 unsigned short val, val_mask, val2 = 0;
2707
2708 val_mask = mask << shift;
2709 val = (ucontrol->value.integer.value[0] + min) & mask;
2710 val = val << shift;
2711
2712 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2713 return err;
2714
2715 if (snd_soc_volsw_is_stereo(mc)) {
2716 val_mask = mask << rshift;
2717 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2718 val2 = val2 << rshift;
2719
2720 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2721 return err;
2722 }
2723 return 0;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2726
2727/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002728 * snd_soc_info_volsw_s8 - signed mixer info callback
2729 * @kcontrol: mixer control
2730 * @uinfo: control element information
2731 *
2732 * Callback to provide information about a signed mixer control.
2733 *
2734 * Returns 0 for success.
2735 */
2736int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2738{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002739 struct soc_mixer_control *mc =
2740 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002741 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002742 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002743
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002744 if (!mc->platform_max)
2745 mc->platform_max = mc->max;
2746 platform_max = mc->platform_max;
2747
Mark Browne13ac2e2008-05-28 17:58:05 +01002748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2749 uinfo->count = 2;
2750 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002751 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002752 return 0;
2753}
2754EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2755
2756/**
2757 * snd_soc_get_volsw_s8 - signed mixer get callback
2758 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002759 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002760 *
2761 * Callback to get the value of a signed mixer control.
2762 *
2763 * Returns 0 for success.
2764 */
2765int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2767{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002768 struct soc_mixer_control *mc =
2769 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002770 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002771 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002772 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002773 int val = snd_soc_read(codec, reg);
2774
2775 ucontrol->value.integer.value[0] =
2776 ((signed char)(val & 0xff))-min;
2777 ucontrol->value.integer.value[1] =
2778 ((signed char)((val >> 8) & 0xff))-min;
2779 return 0;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2782
2783/**
2784 * snd_soc_put_volsw_sgn - signed mixer put callback
2785 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002786 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002787 *
2788 * Callback to set the value of a signed mixer control.
2789 *
2790 * Returns 0 for success.
2791 */
2792int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002795 struct soc_mixer_control *mc =
2796 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002797 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002798 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002799 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002800 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002801
2802 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2803 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2804
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002805 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002806}
2807EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2808
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002809/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002810 * snd_soc_info_volsw_range - single mixer info callback with range.
2811 * @kcontrol: mixer control
2812 * @uinfo: control element information
2813 *
2814 * Callback to provide information, within a range, about a single
2815 * mixer control.
2816 *
2817 * returns 0 for success.
2818 */
2819int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2820 struct snd_ctl_elem_info *uinfo)
2821{
2822 struct soc_mixer_control *mc =
2823 (struct soc_mixer_control *)kcontrol->private_value;
2824 int platform_max;
2825 int min = mc->min;
2826
2827 if (!mc->platform_max)
2828 mc->platform_max = mc->max;
2829 platform_max = mc->platform_max;
2830
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2832 uinfo->count = 1;
2833 uinfo->value.integer.min = 0;
2834 uinfo->value.integer.max = platform_max - min;
2835
2836 return 0;
2837}
2838EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2839
2840/**
2841 * snd_soc_put_volsw_range - single mixer put value callback with range.
2842 * @kcontrol: mixer control
2843 * @ucontrol: control element information
2844 *
2845 * Callback to set the value, within a range, for a single mixer control.
2846 *
2847 * Returns 0 for success.
2848 */
2849int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
2851{
2852 struct soc_mixer_control *mc =
2853 (struct soc_mixer_control *)kcontrol->private_value;
2854 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2855 unsigned int reg = mc->reg;
2856 unsigned int shift = mc->shift;
2857 int min = mc->min;
2858 int max = mc->max;
2859 unsigned int mask = (1 << fls(max)) - 1;
2860 unsigned int invert = mc->invert;
2861 unsigned int val, val_mask;
2862
2863 val = ((ucontrol->value.integer.value[0] + min) & mask);
2864 if (invert)
2865 val = max - val;
2866 val_mask = mask << shift;
2867 val = val << shift;
2868
2869 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2870}
2871EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2872
2873/**
2874 * snd_soc_get_volsw_range - single mixer get callback with range
2875 * @kcontrol: mixer control
2876 * @ucontrol: control element information
2877 *
2878 * Callback to get the value, within a range, of a single mixer control.
2879 *
2880 * Returns 0 for success.
2881 */
2882int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
2884{
2885 struct soc_mixer_control *mc =
2886 (struct soc_mixer_control *)kcontrol->private_value;
2887 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2888 unsigned int reg = mc->reg;
2889 unsigned int shift = mc->shift;
2890 int min = mc->min;
2891 int max = mc->max;
2892 unsigned int mask = (1 << fls(max)) - 1;
2893 unsigned int invert = mc->invert;
2894
2895 ucontrol->value.integer.value[0] =
2896 (snd_soc_read(codec, reg) >> shift) & mask;
2897 if (invert)
2898 ucontrol->value.integer.value[0] =
2899 max - ucontrol->value.integer.value[0];
2900 ucontrol->value.integer.value[0] =
2901 ucontrol->value.integer.value[0] - min;
2902
2903 return 0;
2904}
2905EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2906
2907/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002908 * snd_soc_limit_volume - Set new limit to an existing volume control.
2909 *
2910 * @codec: where to look for the control
2911 * @name: Name of the control
2912 * @max: new maximum limit
2913 *
2914 * Return 0 for success, else error.
2915 */
2916int snd_soc_limit_volume(struct snd_soc_codec *codec,
2917 const char *name, int max)
2918{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002919 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002920 struct snd_kcontrol *kctl;
2921 struct soc_mixer_control *mc;
2922 int found = 0;
2923 int ret = -EINVAL;
2924
2925 /* Sanity check for name and max */
2926 if (unlikely(!name || max <= 0))
2927 return -EINVAL;
2928
2929 list_for_each_entry(kctl, &card->controls, list) {
2930 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2931 found = 1;
2932 break;
2933 }
2934 }
2935 if (found) {
2936 mc = (struct soc_mixer_control *)kctl->private_value;
2937 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002938 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002939 ret = 0;
2940 }
2941 }
2942 return ret;
2943}
2944EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2945
Mark Brown71d08512011-10-10 18:31:26 +01002946int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_info *uinfo)
2948{
2949 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2950 struct soc_bytes *params = (void *)kcontrol->private_value;
2951
2952 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2953 uinfo->count = params->num_regs * codec->val_bytes;
2954
2955 return 0;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2958
2959int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_value *ucontrol)
2961{
2962 struct soc_bytes *params = (void *)kcontrol->private_value;
2963 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2964 int ret;
2965
2966 if (codec->using_regmap)
2967 ret = regmap_raw_read(codec->control_data, params->base,
2968 ucontrol->value.bytes.data,
2969 params->num_regs * codec->val_bytes);
2970 else
2971 ret = -EINVAL;
2972
Mark Brownf831b052012-02-17 16:20:33 -08002973 /* Hide any masked bytes to ensure consistent data reporting */
2974 if (ret == 0 && params->mask) {
2975 switch (codec->val_bytes) {
2976 case 1:
2977 ucontrol->value.bytes.data[0] &= ~params->mask;
2978 break;
2979 case 2:
2980 ((u16 *)(&ucontrol->value.bytes.data))[0]
2981 &= ~params->mask;
2982 break;
2983 case 4:
2984 ((u32 *)(&ucontrol->value.bytes.data))[0]
2985 &= ~params->mask;
2986 break;
2987 default:
2988 return -EINVAL;
2989 }
2990 }
2991
Mark Brown71d08512011-10-10 18:31:26 +01002992 return ret;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2995
2996int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_value *ucontrol)
2998{
2999 struct soc_bytes *params = (void *)kcontrol->private_value;
3000 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003001 int ret, len;
3002 unsigned int val;
3003 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003004
Mark Brownf831b052012-02-17 16:20:33 -08003005 if (!codec->using_regmap)
3006 return -EINVAL;
3007
3008 data = ucontrol->value.bytes.data;
3009 len = params->num_regs * codec->val_bytes;
3010
3011 /*
3012 * If we've got a mask then we need to preserve the register
3013 * bits. We shouldn't modify the incoming data so take a
3014 * copy.
3015 */
3016 if (params->mask) {
3017 ret = regmap_read(codec->control_data, params->base, &val);
3018 if (ret != 0)
3019 return ret;
3020
3021 val &= params->mask;
3022
3023 data = kmemdup(data, len, GFP_KERNEL);
3024 if (!data)
3025 return -ENOMEM;
3026
3027 switch (codec->val_bytes) {
3028 case 1:
3029 ((u8 *)data)[0] &= ~params->mask;
3030 ((u8 *)data)[0] |= val;
3031 break;
3032 case 2:
3033 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3034 ((u16 *)data)[0] |= cpu_to_be16(val);
3035 break;
3036 case 4:
3037 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3038 ((u32 *)data)[0] |= cpu_to_be32(val);
3039 break;
3040 default:
3041 return -EINVAL;
3042 }
3043 }
3044
3045 ret = regmap_raw_write(codec->control_data, params->base,
3046 data, len);
3047
3048 if (params->mask)
3049 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003050
3051 return ret;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3054
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003055/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003056 * snd_soc_info_xr_sx - signed multi register info callback
3057 * @kcontrol: mreg control
3058 * @uinfo: control element information
3059 *
3060 * Callback to provide information of a control that can
3061 * span multiple codec registers which together
3062 * forms a single signed value in a MSB/LSB manner.
3063 *
3064 * Returns 0 for success.
3065 */
3066int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_info *uinfo)
3068{
3069 struct soc_mreg_control *mc =
3070 (struct soc_mreg_control *)kcontrol->private_value;
3071 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3072 uinfo->count = 1;
3073 uinfo->value.integer.min = mc->min;
3074 uinfo->value.integer.max = mc->max;
3075
3076 return 0;
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3079
3080/**
3081 * snd_soc_get_xr_sx - signed multi register get callback
3082 * @kcontrol: mreg control
3083 * @ucontrol: control element information
3084 *
3085 * Callback to get the value of a control that can span
3086 * multiple codec registers which together forms a single
3087 * signed value in a MSB/LSB manner. The control supports
3088 * specifying total no of bits used to allow for bitfields
3089 * across the multiple codec registers.
3090 *
3091 * Returns 0 for success.
3092 */
3093int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3094 struct snd_ctl_elem_value *ucontrol)
3095{
3096 struct soc_mreg_control *mc =
3097 (struct soc_mreg_control *)kcontrol->private_value;
3098 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3099 unsigned int regbase = mc->regbase;
3100 unsigned int regcount = mc->regcount;
3101 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3102 unsigned int regwmask = (1<<regwshift)-1;
3103 unsigned int invert = mc->invert;
3104 unsigned long mask = (1UL<<mc->nbits)-1;
3105 long min = mc->min;
3106 long max = mc->max;
3107 long val = 0;
3108 unsigned long regval;
3109 unsigned int i;
3110
3111 for (i = 0; i < regcount; i++) {
3112 regval = snd_soc_read(codec, regbase+i) & regwmask;
3113 val |= regval << (regwshift*(regcount-i-1));
3114 }
3115 val &= mask;
3116 if (min < 0 && val > max)
3117 val |= ~mask;
3118 if (invert)
3119 val = max - val;
3120 ucontrol->value.integer.value[0] = val;
3121
3122 return 0;
3123}
3124EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3125
3126/**
3127 * snd_soc_put_xr_sx - signed multi register get callback
3128 * @kcontrol: mreg control
3129 * @ucontrol: control element information
3130 *
3131 * Callback to set the value of a control that can span
3132 * multiple codec registers which together forms a single
3133 * signed value in a MSB/LSB manner. The control supports
3134 * specifying total no of bits used to allow for bitfields
3135 * across the multiple codec registers.
3136 *
3137 * Returns 0 for success.
3138 */
3139int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3141{
3142 struct soc_mreg_control *mc =
3143 (struct soc_mreg_control *)kcontrol->private_value;
3144 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3145 unsigned int regbase = mc->regbase;
3146 unsigned int regcount = mc->regcount;
3147 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3148 unsigned int regwmask = (1<<regwshift)-1;
3149 unsigned int invert = mc->invert;
3150 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003151 long max = mc->max;
3152 long val = ucontrol->value.integer.value[0];
3153 unsigned int i, regval, regmask;
3154 int err;
3155
3156 if (invert)
3157 val = max - val;
3158 val &= mask;
3159 for (i = 0; i < regcount; i++) {
3160 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3161 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3162 err = snd_soc_update_bits_locked(codec, regbase+i,
3163 regmask, regval);
3164 if (err < 0)
3165 return err;
3166 }
3167
3168 return 0;
3169}
3170EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3171
3172/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003173 * snd_soc_get_strobe - strobe get callback
3174 * @kcontrol: mixer control
3175 * @ucontrol: control element information
3176 *
3177 * Callback get the value of a strobe mixer control.
3178 *
3179 * Returns 0 for success.
3180 */
3181int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3182 struct snd_ctl_elem_value *ucontrol)
3183{
3184 struct soc_mixer_control *mc =
3185 (struct soc_mixer_control *)kcontrol->private_value;
3186 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3187 unsigned int reg = mc->reg;
3188 unsigned int shift = mc->shift;
3189 unsigned int mask = 1 << shift;
3190 unsigned int invert = mc->invert != 0;
3191 unsigned int val = snd_soc_read(codec, reg) & mask;
3192
3193 if (shift != 0 && val != 0)
3194 val = val >> shift;
3195 ucontrol->value.enumerated.item[0] = val ^ invert;
3196
3197 return 0;
3198}
3199EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3200
3201/**
3202 * snd_soc_put_strobe - strobe put callback
3203 * @kcontrol: mixer control
3204 * @ucontrol: control element information
3205 *
3206 * Callback strobe a register bit to high then low (or the inverse)
3207 * in one pass of a single mixer enum control.
3208 *
3209 * Returns 1 for success.
3210 */
3211int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3212 struct snd_ctl_elem_value *ucontrol)
3213{
3214 struct soc_mixer_control *mc =
3215 (struct soc_mixer_control *)kcontrol->private_value;
3216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3217 unsigned int reg = mc->reg;
3218 unsigned int shift = mc->shift;
3219 unsigned int mask = 1 << shift;
3220 unsigned int invert = mc->invert != 0;
3221 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3222 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3223 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3224 int err;
3225
3226 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3227 if (err < 0)
3228 return err;
3229
3230 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3231 return err;
3232}
3233EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3234
3235/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003236 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3237 * @dai: DAI
3238 * @clk_id: DAI specific clock ID
3239 * @freq: new clock frequency in Hz
3240 * @dir: new clock direction - input/output.
3241 *
3242 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3243 */
3244int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3245 unsigned int freq, int dir)
3246{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003247 if (dai->driver && dai->driver->ops->set_sysclk)
3248 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003249 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003250 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003251 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003252 else
3253 return -EINVAL;
3254}
3255EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3256
3257/**
Mark Brownec4ee522011-03-07 20:58:11 +00003258 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3259 * @codec: CODEC
3260 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003261 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003262 * @freq: new clock frequency in Hz
3263 * @dir: new clock direction - input/output.
3264 *
3265 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3266 */
3267int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003268 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003269{
3270 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003271 return codec->driver->set_sysclk(codec, clk_id, source,
3272 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003273 else
3274 return -EINVAL;
3275}
3276EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3277
3278/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003279 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3280 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003281 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003282 * @div: new clock divisor.
3283 *
3284 * Configures the clock dividers. This is used to derive the best DAI bit and
3285 * frame clocks from the system or master clock. It's best to set the DAI bit
3286 * and frame clocks as low as possible to save system power.
3287 */
3288int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3289 int div_id, int div)
3290{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003291 if (dai->driver && dai->driver->ops->set_clkdiv)
3292 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003293 else
3294 return -EINVAL;
3295}
3296EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3297
3298/**
3299 * snd_soc_dai_set_pll - configure DAI PLL.
3300 * @dai: DAI
3301 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003302 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003303 * @freq_in: PLL input clock frequency in Hz
3304 * @freq_out: requested PLL output clock frequency in Hz
3305 *
3306 * Configures and enables PLL to generate output clock based on input clock.
3307 */
Mark Brown85488032009-09-05 18:52:16 +01003308int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3309 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003310{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003311 if (dai->driver && dai->driver->ops->set_pll)
3312 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003313 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003314 else if (dai->codec && dai->codec->driver->set_pll)
3315 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3316 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003317 else
3318 return -EINVAL;
3319}
3320EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3321
Mark Brownec4ee522011-03-07 20:58:11 +00003322/*
3323 * snd_soc_codec_set_pll - configure codec PLL.
3324 * @codec: CODEC
3325 * @pll_id: DAI specific PLL ID
3326 * @source: DAI specific source for the PLL
3327 * @freq_in: PLL input clock frequency in Hz
3328 * @freq_out: requested PLL output clock frequency in Hz
3329 *
3330 * Configures and enables PLL to generate output clock based on input clock.
3331 */
3332int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3333 unsigned int freq_in, unsigned int freq_out)
3334{
3335 if (codec->driver->set_pll)
3336 return codec->driver->set_pll(codec, pll_id, source,
3337 freq_in, freq_out);
3338 else
3339 return -EINVAL;
3340}
3341EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3342
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003343/**
3344 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3345 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003346 * @fmt: SND_SOC_DAIFMT_ format value.
3347 *
3348 * Configures the DAI hardware format and clocking.
3349 */
3350int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3351{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003352 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003353 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003354 if (dai->driver->ops->set_fmt == NULL)
3355 return -ENOTSUPP;
3356 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003357}
3358EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3359
3360/**
3361 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3362 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003363 * @tx_mask: bitmask representing active TX slots.
3364 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003365 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003366 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003367 *
3368 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3369 * specific.
3370 */
3371int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003372 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003373{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003374 if (dai->driver && dai->driver->ops->set_tdm_slot)
3375 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003376 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003377 else
3378 return -EINVAL;
3379}
3380EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3381
3382/**
Barry Song472df3c2009-09-12 01:16:29 +08003383 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3384 * @dai: DAI
3385 * @tx_num: how many TX channels
3386 * @tx_slot: pointer to an array which imply the TX slot number channel
3387 * 0~num-1 uses
3388 * @rx_num: how many RX channels
3389 * @rx_slot: pointer to an array which imply the RX slot number channel
3390 * 0~num-1 uses
3391 *
3392 * configure the relationship between channel number and TDM slot number.
3393 */
3394int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3395 unsigned int tx_num, unsigned int *tx_slot,
3396 unsigned int rx_num, unsigned int *rx_slot)
3397{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398 if (dai->driver && dai->driver->ops->set_channel_map)
3399 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003400 rx_num, rx_slot);
3401 else
3402 return -EINVAL;
3403}
3404EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3405
3406/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003407 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3408 * @dai: DAI
3409 * @tristate: tristate enable
3410 *
3411 * Tristates the DAI so that others can use it.
3412 */
3413int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3414{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003415 if (dai->driver && dai->driver->ops->set_tristate)
3416 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003417 else
3418 return -EINVAL;
3419}
3420EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3421
3422/**
3423 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3424 * @dai: DAI
3425 * @mute: mute enable
3426 *
3427 * Mutes the DAI DAC.
3428 */
3429int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3430{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003431 if (dai->driver && dai->driver->ops->digital_mute)
3432 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003433 else
Mark Brown04570c62012-04-13 19:16:03 +01003434 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003435}
3436EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3437
Mark Brownc5af3a22008-11-28 13:29:45 +00003438/**
3439 * snd_soc_register_card - Register a card with the ASoC core
3440 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003441 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003442 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003443 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303444int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003445{
Mark Brownb19e6e72012-03-14 21:18:39 +00003446 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003447
Mark Brownc5af3a22008-11-28 13:29:45 +00003448 if (!card->name || !card->dev)
3449 return -EINVAL;
3450
Stephen Warren5a504962011-12-21 10:40:59 -07003451 for (i = 0; i < card->num_links; i++) {
3452 struct snd_soc_dai_link *link = &card->dai_link[i];
3453
3454 /*
3455 * Codec must be specified by 1 of name or OF node,
3456 * not both or neither.
3457 */
3458 if (!!link->codec_name == !!link->codec_of_node) {
3459 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003460 "Neither/both codec name/of_node are set for %s\n",
3461 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003462 return -EINVAL;
3463 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003464 /* Codec DAI name must be specified */
3465 if (!link->codec_dai_name) {
3466 dev_err(card->dev, "codec_dai_name not set for %s\n",
3467 link->name);
3468 return -EINVAL;
3469 }
Stephen Warren5a504962011-12-21 10:40:59 -07003470
3471 /*
3472 * Platform may be specified by either name or OF node, but
3473 * can be left unspecified, and a dummy platform will be used.
3474 */
3475 if (link->platform_name && link->platform_of_node) {
3476 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003477 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003478 return -EINVAL;
3479 }
3480
3481 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003482 * CPU device may be specified by either name or OF node, but
3483 * can be left unspecified, and will be matched based on DAI
3484 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003485 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003486 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003487 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003488 "Neither/both cpu name/of_node are set for %s\n",
3489 link->name);
3490 return -EINVAL;
3491 }
3492 /*
3493 * At least one of CPU DAI name or CPU device name/node must be
3494 * specified
3495 */
3496 if (!link->cpu_dai_name &&
3497 !(link->cpu_name || link->cpu_of_node)) {
3498 dev_err(card->dev,
3499 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003500 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003501 return -EINVAL;
3502 }
3503 }
3504
Mark Browned77cc12011-05-03 18:25:34 +01003505 dev_set_drvdata(card->dev, card);
3506
Stephen Warren111c6412011-01-28 14:26:35 -07003507 snd_soc_initialize_card_lists(card);
3508
Vinod Koul150dd2f2011-01-13 22:48:32 +05303509 soc_init_card_debugfs(card);
3510
Mark Brown181a6892012-03-14 20:18:49 +00003511 card->rtd = devm_kzalloc(card->dev,
3512 sizeof(struct snd_soc_pcm_runtime) *
3513 (card->num_links + card->num_aux_devs),
3514 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003515 if (card->rtd == NULL)
3516 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003517 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003518 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003519
3520 for (i = 0; i < card->num_links; i++)
3521 card->rtd[i].dai_link = &card->dai_link[i];
3522
Mark Brownc5af3a22008-11-28 13:29:45 +00003523 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003524 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003525 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003526 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003527 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003528
Mark Brownb19e6e72012-03-14 21:18:39 +00003529 ret = snd_soc_instantiate_card(card);
3530 if (ret != 0)
3531 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003532
Mark Brownb19e6e72012-03-14 21:18:39 +00003533 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003534}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303535EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003536
3537/**
3538 * snd_soc_unregister_card - Unregister a card with the ASoC core
3539 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003540 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003541 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003542 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303543int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003544{
Vinod Koulb0e26482011-01-13 22:48:02 +05303545 if (card->instantiated)
3546 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003547 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3548
3549 return 0;
3550}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303551EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003553/*
3554 * Simplify DAI link configuration by removing ".-1" from device names
3555 * and sanitizing names.
3556 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003557static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003558{
3559 char *found, name[NAME_SIZE];
3560 int id1, id2;
3561
3562 if (dev_name(dev) == NULL)
3563 return NULL;
3564
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003565 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003566
3567 /* are we a "%s.%d" name (platform and SPI components) */
3568 found = strstr(name, dev->driver->name);
3569 if (found) {
3570 /* get ID */
3571 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3572
3573 /* discard ID from name if ID == -1 */
3574 if (*id == -1)
3575 found[strlen(dev->driver->name)] = '\0';
3576 }
3577
3578 } else {
3579 /* I2C component devices are named "bus-addr" */
3580 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3581 char tmp[NAME_SIZE];
3582
3583 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003584 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003585
3586 /* sanitize component name for DAI link creation */
3587 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003588 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003589 } else
3590 *id = 0;
3591 }
3592
3593 return kstrdup(name, GFP_KERNEL);
3594}
3595
3596/*
3597 * Simplify DAI link naming for single devices with multiple DAIs by removing
3598 * any ".-1" and using the DAI name (instead of device name).
3599 */
3600static inline char *fmt_multiple_name(struct device *dev,
3601 struct snd_soc_dai_driver *dai_drv)
3602{
3603 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003604 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003605 dev_name(dev));
3606 return NULL;
3607 }
3608
3609 return kstrdup(dai_drv->name, GFP_KERNEL);
3610}
3611
Mark Brown91151712008-11-30 23:31:24 +00003612/**
3613 * snd_soc_register_dai - Register a DAI with the ASoC core
3614 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003615 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003616 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003617int snd_soc_register_dai(struct device *dev,
3618 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003619{
Mark Brown054880f2012-04-09 17:29:19 +01003620 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003621 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003622
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003623 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003625 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3626 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003627 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003628
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003629 /* create DAI component name */
3630 dai->name = fmt_single_name(dev, &dai->id);
3631 if (dai->name == NULL) {
3632 kfree(dai);
3633 return -ENOMEM;
3634 }
3635
3636 dai->dev = dev;
3637 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003638 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003639 if (!dai->driver->ops)
3640 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003641
3642 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003643
3644 list_for_each_entry(codec, &codec_list, list) {
3645 if (codec->dev == dev) {
3646 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3647 dai->name, codec->name);
3648 dai->codec = codec;
3649 break;
3650 }
3651 }
3652
Mark Brown91151712008-11-30 23:31:24 +00003653 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003654
Mark Brown91151712008-11-30 23:31:24 +00003655 mutex_unlock(&client_mutex);
3656
3657 pr_debug("Registered DAI '%s'\n", dai->name);
3658
3659 return 0;
3660}
3661EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3662
3663/**
3664 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3665 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003666 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003667 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003668void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003669{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003670 struct snd_soc_dai *dai;
3671
3672 list_for_each_entry(dai, &dai_list, list) {
3673 if (dev == dai->dev)
3674 goto found;
3675 }
3676 return;
3677
3678found:
Mark Brown91151712008-11-30 23:31:24 +00003679 mutex_lock(&client_mutex);
3680 list_del(&dai->list);
3681 mutex_unlock(&client_mutex);
3682
3683 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003684 kfree(dai->name);
3685 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003686}
3687EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3688
3689/**
3690 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3691 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003692 * @dai: Array of DAIs to register
3693 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003694 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003695int snd_soc_register_dais(struct device *dev,
3696 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003697{
Mark Brown054880f2012-04-09 17:29:19 +01003698 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003699 struct snd_soc_dai *dai;
3700 int i, ret = 0;
3701
Liam Girdwood720ffa42010-08-18 00:30:30 +01003702 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003703
3704 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003705
3706 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003707 if (dai == NULL) {
3708 ret = -ENOMEM;
3709 goto err;
3710 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003711
3712 /* create DAI component name */
3713 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3714 if (dai->name == NULL) {
3715 kfree(dai);
3716 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003717 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003718 }
3719
3720 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003721 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003722 if (dai->driver->id)
3723 dai->id = dai->driver->id;
3724 else
3725 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003726 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003727 if (!dai->driver->ops)
3728 dai->driver->ops = &null_dai_ops;
3729
3730 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003731
3732 list_for_each_entry(codec, &codec_list, list) {
3733 if (codec->dev == dev) {
3734 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3735 dai->name, codec->name);
3736 dai->codec = codec;
3737 break;
3738 }
3739 }
3740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003741 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003743 mutex_unlock(&client_mutex);
3744
3745 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003746 }
3747
3748 return 0;
3749
3750err:
3751 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003752 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003753
3754 return ret;
3755}
3756EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3757
3758/**
3759 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3760 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003761 * @dai: Array of DAIs to unregister
3762 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003763 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003764void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003765{
3766 int i;
3767
3768 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003769 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003770}
3771EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3772
Mark Brown12a48a8c2008-12-03 19:40:30 +00003773/**
3774 * snd_soc_register_platform - Register a platform with the ASoC core
3775 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003776 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003777 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003778int snd_soc_register_platform(struct device *dev,
3779 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003780{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003781 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3784
3785 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3786 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003787 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788
3789 /* create platform component name */
3790 platform->name = fmt_single_name(dev, &platform->id);
3791 if (platform->name == NULL) {
3792 kfree(platform);
3793 return -ENOMEM;
3794 }
3795
3796 platform->dev = dev;
3797 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003798 platform->dapm.dev = dev;
3799 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003800 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003801 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003802
3803 mutex_lock(&client_mutex);
3804 list_add(&platform->list, &platform_list);
3805 mutex_unlock(&client_mutex);
3806
3807 pr_debug("Registered platform '%s'\n", platform->name);
3808
3809 return 0;
3810}
3811EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3812
3813/**
3814 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3815 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003816 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003817 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003818void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003819{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003820 struct snd_soc_platform *platform;
3821
3822 list_for_each_entry(platform, &platform_list, list) {
3823 if (dev == platform->dev)
3824 goto found;
3825 }
3826 return;
3827
3828found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003829 mutex_lock(&client_mutex);
3830 list_del(&platform->list);
3831 mutex_unlock(&client_mutex);
3832
3833 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003834 kfree(platform->name);
3835 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003836}
3837EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3838
Mark Brown151ab222009-05-09 16:22:58 +01003839static u64 codec_format_map[] = {
3840 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3841 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3842 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3843 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3844 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3845 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3846 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3847 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3848 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3849 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3850 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3851 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3852 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3853 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3854 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3855 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3856};
3857
3858/* Fix up the DAI formats for endianness: codecs don't actually see
3859 * the endianness of the data but we're using the CPU format
3860 * definitions which do need to include endianness so we ensure that
3861 * codec DAIs always have both big and little endian variants set.
3862 */
3863static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3864{
3865 int i;
3866
3867 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3868 if (stream->formats & codec_format_map[i])
3869 stream->formats |= codec_format_map[i];
3870}
3871
Mark Brown0d0cf002008-12-10 14:32:45 +00003872/**
3873 * snd_soc_register_codec - Register a codec with the ASoC core
3874 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003875 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003876 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003877int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003878 const struct snd_soc_codec_driver *codec_drv,
3879 struct snd_soc_dai_driver *dai_drv,
3880 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003881{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003882 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883 struct snd_soc_codec *codec;
3884 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003885
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003887
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003888 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3889 if (codec == NULL)
3890 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003891
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003892 /* create CODEC component name */
3893 codec->name = fmt_single_name(dev, &codec->id);
3894 if (codec->name == NULL) {
3895 kfree(codec);
3896 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003897 }
3898
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003899 if (codec_drv->compress_type)
3900 codec->compress_type = codec_drv->compress_type;
3901 else
3902 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3903
Mark Brownc3acec22010-12-02 16:15:29 +00003904 codec->write = codec_drv->write;
3905 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003906 codec->volatile_register = codec_drv->volatile_register;
3907 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003908 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003909 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003910 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3911 codec->dapm.dev = dev;
3912 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003913 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003914 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915 codec->dev = dev;
3916 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003917 codec->num_dai = num_dai;
3918 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003919
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003920 /* allocate CODEC register cache */
3921 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003922 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003923 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003924 /* it is necessary to make a copy of the default register cache
3925 * because in the case of using a compression type that requires
3926 * the default register cache to be marked as __devinitconst the
3927 * kernel might have freed the array by the time we initialize
3928 * the cache.
3929 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003930 if (codec_drv->reg_cache_default) {
3931 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3932 reg_size, GFP_KERNEL);
3933 if (!codec->reg_def_copy) {
3934 ret = -ENOMEM;
3935 goto fail;
3936 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003937 }
3938 }
3939
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003940 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3941 if (!codec->volatile_register)
3942 codec->volatile_register = snd_soc_default_volatile_register;
3943 if (!codec->readable_register)
3944 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003945 if (!codec->writable_register)
3946 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003947 }
3948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003949 for (i = 0; i < num_dai; i++) {
3950 fixup_codec_formats(&dai_drv[i].playback);
3951 fixup_codec_formats(&dai_drv[i].capture);
3952 }
3953
Mark Brown054880f2012-04-09 17:29:19 +01003954 mutex_lock(&client_mutex);
3955 list_add(&codec->list, &codec_list);
3956 mutex_unlock(&client_mutex);
3957
Mark Brown26b01cc2010-08-18 20:20:55 +01003958 /* register any DAIs */
3959 if (num_dai) {
3960 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3961 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01003962 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3963 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01003964 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003965
Mark Brown0d0cf002008-12-10 14:32:45 +00003966 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003967 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003969fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003970 kfree(codec->reg_def_copy);
3971 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003972 kfree(codec->name);
3973 kfree(codec);
3974 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003975}
3976EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3977
3978/**
3979 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3980 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003981 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003982 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003983void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003984{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003985 struct snd_soc_codec *codec;
3986 int i;
3987
3988 list_for_each_entry(codec, &codec_list, list) {
3989 if (dev == codec->dev)
3990 goto found;
3991 }
3992 return;
3993
3994found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003995 if (codec->num_dai)
3996 for (i = 0; i < codec->num_dai; i++)
3997 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003998
Mark Brown0d0cf002008-12-10 14:32:45 +00003999 mutex_lock(&client_mutex);
4000 list_del(&codec->list);
4001 mutex_unlock(&client_mutex);
4002
4003 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004005 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004006 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004007 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004008 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004009}
4010EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4011
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004012/* Retrieve a card's name from device tree */
4013int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4014 const char *propname)
4015{
4016 struct device_node *np = card->dev->of_node;
4017 int ret;
4018
4019 ret = of_property_read_string_index(np, propname, 0, &card->name);
4020 /*
4021 * EINVAL means the property does not exist. This is fine providing
4022 * card->name was previously set, which is checked later in
4023 * snd_soc_register_card.
4024 */
4025 if (ret < 0 && ret != -EINVAL) {
4026 dev_err(card->dev,
4027 "Property '%s' could not be read: %d\n",
4028 propname, ret);
4029 return ret;
4030 }
4031
4032 return 0;
4033}
4034EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4035
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004036int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4037 const char *propname)
4038{
4039 struct device_node *np = card->dev->of_node;
4040 int num_routes;
4041 struct snd_soc_dapm_route *routes;
4042 int i, ret;
4043
4044 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004045 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004046 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004047 "Property '%s' does not exist or its length is not even\n",
4048 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004049 return -EINVAL;
4050 }
4051 num_routes /= 2;
4052 if (!num_routes) {
4053 dev_err(card->dev,
4054 "Property '%s's length is zero\n",
4055 propname);
4056 return -EINVAL;
4057 }
4058
4059 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4060 GFP_KERNEL);
4061 if (!routes) {
4062 dev_err(card->dev,
4063 "Could not allocate DAPM route table\n");
4064 return -EINVAL;
4065 }
4066
4067 for (i = 0; i < num_routes; i++) {
4068 ret = of_property_read_string_index(np, propname,
4069 2 * i, &routes[i].sink);
4070 if (ret) {
4071 dev_err(card->dev,
4072 "Property '%s' index %d could not be read: %d\n",
4073 propname, 2 * i, ret);
4074 return -EINVAL;
4075 }
4076 ret = of_property_read_string_index(np, propname,
4077 (2 * i) + 1, &routes[i].source);
4078 if (ret) {
4079 dev_err(card->dev,
4080 "Property '%s' index %d could not be read: %d\n",
4081 propname, (2 * i) + 1, ret);
4082 return -EINVAL;
4083 }
4084 }
4085
4086 card->num_dapm_routes = num_routes;
4087 card->dapm_routes = routes;
4088
4089 return 0;
4090}
4091EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4092
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004093static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004094{
Mark Brown384c89e2008-12-03 17:34:03 +00004095#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004096 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4097 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004098 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004099 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004100 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004101
Mark Brown8a9dab12011-01-10 22:25:21 +00004102 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004103 &codec_list_fops))
4104 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4105
Mark Brown8a9dab12011-01-10 22:25:21 +00004106 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004107 &dai_list_fops))
4108 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004109
Mark Brown8a9dab12011-01-10 22:25:21 +00004110 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004111 &platform_list_fops))
4112 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004113#endif
4114
Mark Brownfb257892011-04-28 10:57:54 +01004115 snd_soc_util_init();
4116
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004117 return platform_driver_register(&soc_driver);
4118}
Mark Brown4abe8e12010-10-12 17:41:03 +01004119module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004120
Mark Brown7d8c16a2008-11-30 22:11:24 +00004121static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004122{
Mark Brownfb257892011-04-28 10:57:54 +01004123 snd_soc_util_exit();
4124
Mark Brown384c89e2008-12-03 17:34:03 +00004125#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004126 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004127#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004128 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004129}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004130module_exit(snd_soc_exit);
4131
4132/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004133MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004134MODULE_DESCRIPTION("ALSA SoC Core");
4135MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004136MODULE_ALIAS("platform:soc-audio");