blob: 64e047dc7cdfdea5518320ed4ff9a4a1088468eb [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>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.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
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000056static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000057static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070058static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000059
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000069/* returns the minimum number of bytes needed to represent
70 * a particular given value */
71static int min_bytes_needed(unsigned long val)
72{
73 int c = 0;
74 int i;
75
76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
77 if (val & (1UL << i))
78 break;
79 c = (sizeof val * 8) - c;
80 if (!c || (c % 8))
81 c = (c + 8) / 8;
82 else
83 c /= 8;
84 return c;
85}
86
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000087/* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89static int format_register_str(struct snd_soc_codec *codec,
90 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000091{
Stephen Warren00b317a2011-04-01 14:50:44 -060092 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
93 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000094 int ret;
95 char tmpbuf[len + 1];
96 char regbuf[regsize + 1];
97
98 /* since tmpbuf is allocated on the stack, warn the callers if they
99 * try to abuse this function */
100 WARN_ON(len > 63);
101
102 /* +2 for ': ' and + 1 for '\n' */
103 if (wordsize + regsize + 2 + 1 != len)
104 return -EINVAL;
105
Mark Brown25032c12011-08-01 13:52:48 +0900106 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 if (ret < 0) {
108 memset(regbuf, 'X', regsize);
109 regbuf[regsize] = '\0';
110 } else {
111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
112 }
113
114 /* prepare the buffer */
115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
116 /* copy it back to the caller without the '\0' */
117 memcpy(buf, tmpbuf, len);
118
119 return 0;
120}
121
122/* codec register dump */
123static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
124 size_t count, loff_t pos)
125{
126 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000127 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000128 int len;
129 size_t total = 0;
130 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000131
Stephen Warren00b317a2011-04-01 14:50:44 -0600132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
133 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000134
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000135 len = wordsize + regsize + 2 + 1;
136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000137 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000138 return 0;
139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 if (codec->driver->reg_cache_step)
141 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100144 /* only support larger than PAGE_SIZE bytes debugfs
145 * entries for the default case */
146 if (p >= pos) {
147 if (total + len >= count - 1)
148 break;
149 format_register_str(codec, i, buf + total, len);
150 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100151 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100152 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000153 }
154
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000155 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000156
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000157 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000158}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000159
Mark Brown2624d5f2009-11-03 21:56:13 +0000160static ssize_t codec_reg_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
Mark Brown36ae1a92012-01-06 17:12:45 -0800163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000166}
167
168static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
169
Mark Browndbe21402010-02-12 11:37:24 +0000170static ssize_t pmdown_time_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
Mark Brown36ae1a92012-01-06 17:12:45 -0800173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000176}
177
178static ssize_t pmdown_time_set(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700183 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000184
Jingoo Hanb785a492013-07-19 16:24:59 +0900185 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700186 if (ret)
187 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000188
189 return count;
190}
191
192static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
193
Mark Brown2624d5f2009-11-03 21:56:13 +0000194#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000195static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000196 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000197{
198 ssize_t ret;
199 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000200 char *buf;
201
202 if (*ppos < 0 || !count)
203 return -EINVAL;
204
205 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000206 if (!buf)
207 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000208
209 ret = soc_codec_reg_show(codec, buf, count, *ppos);
210 if (ret >= 0) {
211 if (copy_to_user(user_buf, buf, ret)) {
212 kfree(buf);
213 return -EFAULT;
214 }
215 *ppos += ret;
216 }
217
Mark Brown2624d5f2009-11-03 21:56:13 +0000218 kfree(buf);
219 return ret;
220}
221
222static ssize_t codec_reg_write_file(struct file *file,
223 const char __user *user_buf, size_t count, loff_t *ppos)
224{
225 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700226 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 char *start = buf;
228 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000229 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900230 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000231
232 buf_size = min(count, (sizeof(buf)-1));
233 if (copy_from_user(buf, user_buf, buf_size))
234 return -EFAULT;
235 buf[buf_size] = 0;
236
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 while (*start == ' ')
238 start++;
239 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 while (*start == ' ')
241 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900242 ret = kstrtoul(start, 16, &value);
243 if (ret)
244 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000245
246 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030247 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000248
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000249 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 return buf_size;
251}
252
253static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700254 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000255 .read = codec_reg_read_file,
256 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200257 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000258};
259
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200260static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100261{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200262 if (component->debugfs_prefix) {
263 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100264
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200265 name = kasprintf(GFP_KERNEL, "%s:%s",
266 component->debugfs_prefix, component->name);
267 if (name) {
268 component->debugfs_root = debugfs_create_dir(name,
269 component->card->debugfs_card_root);
270 kfree(name);
271 }
272 } else {
273 component->debugfs_root = debugfs_create_dir(component->name,
274 component->card->debugfs_card_root);
275 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100276
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200277 if (!component->debugfs_root) {
278 dev_warn(component->dev,
279 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000280 return;
281 }
282
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200283 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
284 component->debugfs_root);
285
286 if (component->init_debugfs)
287 component->init_debugfs(component);
288}
289
290static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
291{
292 debugfs_remove_recursive(component->debugfs_root);
293}
294
295static void soc_init_codec_debugfs(struct snd_soc_component *component)
296{
297 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
298
Mark Brown2624d5f2009-11-03 21:56:13 +0000299 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200300 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000301 codec, &codec_reg_fops);
302 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200303 dev_warn(codec->dev,
304 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000305}
306
Mark Brownc3c5a192010-09-15 18:15:14 +0100307static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
308 size_t count, loff_t *ppos)
309{
310 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100311 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100312 struct snd_soc_codec *codec;
313
314 if (!buf)
315 return -ENOMEM;
316
Mark Brown2b194f9d2010-10-13 10:52:16 +0100317 list_for_each_entry(codec, &codec_list, list) {
318 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200319 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100320 if (len >= 0)
321 ret += len;
322 if (ret > PAGE_SIZE) {
323 ret = PAGE_SIZE;
324 break;
325 }
326 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100327
328 if (ret >= 0)
329 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
330
331 kfree(buf);
332
333 return ret;
334}
335
336static const struct file_operations codec_list_fops = {
337 .read = codec_list_read_file,
338 .llseek = default_llseek,/* read accesses f_pos */
339};
340
Mark Brownf3208782010-09-15 18:19:07 +0100341static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
342 size_t count, loff_t *ppos)
343{
344 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100345 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100346 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100347 struct snd_soc_dai *dai;
348
349 if (!buf)
350 return -ENOMEM;
351
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100352 list_for_each_entry(component, &component_list, list) {
353 list_for_each_entry(dai, &component->dai_list, list) {
354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
355 dai->name);
356 if (len >= 0)
357 ret += len;
358 if (ret > PAGE_SIZE) {
359 ret = PAGE_SIZE;
360 break;
361 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 }
363 }
Mark Brownf3208782010-09-15 18:19:07 +0100364
Mark Brown2b194f9d2010-10-13 10:52:16 +0100365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100366
367 kfree(buf);
368
369 return ret;
370}
371
372static const struct file_operations dai_list_fops = {
373 .read = dai_list_read_file,
374 .llseek = default_llseek,/* read accesses f_pos */
375};
376
Mark Brown19c7ac22010-09-15 18:22:40 +0100377static ssize_t platform_list_read_file(struct file *file,
378 char __user *user_buf,
379 size_t count, loff_t *ppos)
380{
381 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100382 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100383 struct snd_soc_platform *platform;
384
385 if (!buf)
386 return -ENOMEM;
387
Mark Brown2b194f9d2010-10-13 10:52:16 +0100388 list_for_each_entry(platform, &platform_list, list) {
389 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200390 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100391 if (len >= 0)
392 ret += len;
393 if (ret > PAGE_SIZE) {
394 ret = PAGE_SIZE;
395 break;
396 }
397 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100398
Mark Brown2b194f9d2010-10-13 10:52:16 +0100399 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100400
401 kfree(buf);
402
403 return ret;
404}
405
406static const struct file_operations platform_list_fops = {
407 .read = platform_list_read_file,
408 .llseek = default_llseek,/* read accesses f_pos */
409};
410
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200411static void soc_init_card_debugfs(struct snd_soc_card *card)
412{
413 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000414 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200415 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200416 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100417 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200418 return;
419 }
420
421 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
422 card->debugfs_card_root,
423 &card->pop_time);
424 if (!card->debugfs_pop_time)
425 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000426 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200427}
428
429static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
430{
431 debugfs_remove_recursive(card->debugfs_card_root);
432}
433
Mark Brown2624d5f2009-11-03 21:56:13 +0000434#else
435
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200436#define soc_init_codec_debugfs NULL
437
438static inline void soc_init_component_debugfs(
439 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000440{
441}
442
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200443static inline void soc_cleanup_component_debugfs(
444 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000445{
446}
447
Axel Linb95fccb2010-11-09 17:06:44 +0800448static inline void soc_init_card_debugfs(struct snd_soc_card *card)
449{
450}
451
452static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
453{
454}
Mark Brown2624d5f2009-11-03 21:56:13 +0000455#endif
456
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100457struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
458 const char *dai_link, int stream)
459{
460 int i;
461
462 for (i = 0; i < card->num_links; i++) {
463 if (card->rtd[i].dai_link->no_pcm &&
464 !strcmp(card->rtd[i].dai_link->name, dai_link))
465 return card->rtd[i].pcm->streams[stream].substream;
466 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000467 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100468 return NULL;
469}
470EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
471
472struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
473 const char *dai_link)
474{
475 int i;
476
477 for (i = 0; i < card->num_links; i++) {
478 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
479 return &card->rtd[i];
480 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000481 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100482 return NULL;
483}
484EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
485
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100486static void codec2codec_close_delayed_work(struct work_struct *work)
487{
488 /* Currently nothing to do for c2c links
489 * Since c2c links are internal nodes in the DAPM graph and
490 * don't interface with the outside world or application layer
491 * we don't have to do any special handling on close.
492 */
493}
494
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000495#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000497int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000499 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200500 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200501 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200503 /* If the card is not initialized yet there is nothing to do */
504 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200505 return 0;
506
Andy Green6ed25972008-06-13 16:24:05 +0100507 /* Due to the resume being scheduled into a workqueue we could
508 * suspend before that's finished - wait for it to complete.
509 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 snd_power_lock(card->snd_card);
511 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
512 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100513
514 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100516
Mark Browna00f90f2010-12-02 16:24:24 +0000517 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100519
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100521 continue;
522
Benoit Cousson88bd8702014-07-08 23:19:34 +0200523 for (j = 0; j < card->rtd[i].num_codecs; j++) {
524 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
525 struct snd_soc_dai_driver *drv = dai->driver;
526
527 if (drv->ops->digital_mute && dai->playback_active)
528 drv->ops->digital_mute(dai, 1);
529 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530 }
531
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100532 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 for (i = 0; i < card->num_rtd; i++) {
534 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100535 continue;
536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100538 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100539
Mark Brown87506542008-11-18 20:50:34 +0000540 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000541 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 for (i = 0; i < card->num_rtd; i++) {
544 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100547 continue;
548
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100549 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100551 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 /* close any waiting streams and save state */
554 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200555 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700556 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200557 for (j = 0; j < card->rtd[i].num_codecs; j++) {
558 codec_dais[j]->codec->dapm.suspend_bias_level =
559 codec_dais[j]->codec->dapm.bias_level;
560 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564
565 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100566 continue;
567
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800568 snd_soc_dapm_stream_event(&card->rtd[i],
569 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800570 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800572 snd_soc_dapm_stream_event(&card->rtd[i],
573 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800574 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 }
576
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200577 /* Recheck all endpoints too, their state is affected by suspend */
578 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700579 snd_soc_dapm_sync(&card->dapm);
580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200582 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 /* If there are paths active then the CODEC will be held with
584 * bias _ON and should not be suspended. */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200585 if (!codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200586 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000588 /*
589 * If the CODEC is capable of idle
590 * bias off then being in STANDBY
591 * means it's doing something,
592 * otherwise fall through.
593 */
594 if (codec->dapm.idle_bias_off) {
595 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200596 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000597 break;
598 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200601 if (codec->driver->suspend)
602 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 codec->suspended = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200604 if (codec->component.regmap)
605 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800606 /* deactivate pins to sleep state */
607 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 break;
609 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200610 dev_dbg(codec->dev,
611 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 break;
613 }
614 }
615 }
616
617 for (i = 0; i < card->num_rtd; i++) {
618 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
619
620 if (card->rtd[i].dai_link->ignore_suspend)
621 continue;
622
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100623 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800625
626 /* deactivate pins to sleep state */
627 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 }
629
Mark Brown87506542008-11-18 20:50:34 +0000630 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000631 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632
633 return 0;
634}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000635EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636
Andy Green6ed25972008-06-13 16:24:05 +0100637/* deferred resume work, so resume can complete before we finished
638 * setting our codec back up, which can be very slow on I2C
639 */
640static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 struct snd_soc_card *card =
643 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200644 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200645 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200646
Andy Green6ed25972008-06-13 16:24:05 +0100647 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
648 * so userspace apps are blocked from touching us
649 */
650
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000651 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100652
Mark Brown9949788b2010-05-07 20:24:05 +0100653 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100655
Mark Brown87506542008-11-18 20:50:34 +0000656 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000657 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100659 /* resume control bus DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 for (i = 0; i < card->num_rtd; i++) {
661 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100664 continue;
665
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100666 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 }
669
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200670 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 /* If the CODEC was idle over suspend then it will have been
672 * left with bias OFF or STANDBY and suspended so we must now
673 * resume. Otherwise the suspend was suppressed.
674 */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200675 if (codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200676 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 case SND_SOC_BIAS_STANDBY:
678 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200679 if (codec->driver->resume)
680 codec->driver->resume(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 codec->suspended = 0;
682 break;
683 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200684 dev_dbg(codec->dev,
685 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 break;
687 }
Mark Brown1547aba2010-05-07 21:11:40 +0100688 }
689 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100694 continue;
695
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800696 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000697 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800698 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800700 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000701 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800702 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703 }
704
Mark Brown3ff3f642008-05-19 12:32:25 +0200705 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100709 continue;
710
Benoit Cousson88bd8702014-07-08 23:19:34 +0200711 for (j = 0; j < card->rtd[i].num_codecs; j++) {
712 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
713 struct snd_soc_dai_driver *drv = dai->driver;
714
715 if (drv->ops->digital_mute && dai->playback_active)
716 drv->ops->digital_mute(dai, 0);
717 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 }
719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 for (i = 0; i < card->num_rtd; i++) {
721 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100722
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000723 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100724 continue;
725
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100726 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728 }
729
Mark Brown87506542008-11-18 20:50:34 +0000730 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000731 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000733 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100734
735 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700737
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200738 /* Recheck all endpoints too, their state is affected by suspend */
739 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700740 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100741}
742
743/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000744int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100745{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000746 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100747 bool bus_control = false;
748 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200749
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200750 /* If the card is not initialized yet there is nothing to do */
751 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800752 return 0;
753
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800754 /* activate pins from sleep state */
755 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200756 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
757 struct snd_soc_dai **codec_dais = rtd->codec_dais;
758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
759 int j;
760
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800761 if (cpu_dai->active)
762 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200763
764 for (j = 0; j < rtd->num_codecs; j++) {
765 struct snd_soc_dai *codec_dai = codec_dais[j];
766 if (codec_dai->active)
767 pinctrl_pm_select_default_state(codec_dai->dev);
768 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800769 }
770
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100771 /*
772 * DAIs that also act as the control bus master might have other drivers
773 * hanging off them so need to resume immediately. Other drivers don't
774 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100775 * due to I/O costs and anti-pop so handle them out of line.
776 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100779 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100781 if (bus_control) {
782 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600783 soc_resume_deferred(&card->deferred_resume_work);
784 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000785 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600786 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000787 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100788 }
Andy Green6ed25972008-06-13 16:24:05 +0100789
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 return 0;
791}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000792EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200793#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000794#define snd_soc_suspend NULL
795#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796#endif
797
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100798static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800799};
800
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200801static struct snd_soc_component *soc_find_component(
802 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100803{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200804 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100805
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200806 list_for_each_entry(component, &component_list, list) {
807 if (of_node) {
808 if (component->dev->of_node == of_node)
809 return component;
810 } else if (strcmp(component->name, name) == 0) {
811 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100812 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100813 }
814
815 return NULL;
816}
817
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200818static struct snd_soc_dai *snd_soc_find_dai(
819 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100820{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200821 struct snd_soc_component *component;
822 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100823
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200824 /* Find CPU DAI from registered DAIs*/
825 list_for_each_entry(component, &component_list, list) {
826 if (dlc->of_node && component->dev->of_node != dlc->of_node)
827 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100828 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200829 continue;
830 list_for_each_entry(dai, &component->dai_list, list) {
831 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100832 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100833
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200834 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100835 }
836 }
837
838 return NULL;
839}
840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
844 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200845 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200846 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200847 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000848 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100849 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200850 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000852 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000853
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200854 cpu_dai_component.name = dai_link->cpu_name;
855 cpu_dai_component.of_node = dai_link->cpu_of_node;
856 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
857 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000858 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000859 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000861 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000862 }
863
Benoit Cousson88bd8702014-07-08 23:19:34 +0200864 rtd->num_codecs = dai_link->num_codecs;
865
866 /* Find CODEC from registered CODECs */
867 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200868 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200869 if (!codec_dais[i]) {
870 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
871 codecs[i].dai_name);
872 return -EPROBE_DEFER;
873 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000874 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100875
Benoit Cousson88bd8702014-07-08 23:19:34 +0200876 /* Single codec links expect codec and codec_dai in runtime data */
877 rtd->codec_dai = codec_dais[0];
878 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100879
Mark Brown848dd8b2011-04-27 18:16:32 +0100880 /* if there's no platform we match on the empty platform */
881 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700882 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100883 platform_name = "snd-soc-dummy";
884
Mark Brownb19e6e72012-03-14 21:18:39 +0000885 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700887 if (dai_link->platform_of_node) {
888 if (platform->dev->of_node !=
889 dai_link->platform_of_node)
890 continue;
891 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200892 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700893 continue;
894 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700895
896 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000898 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000899 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000901 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000902 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000903
904 card->num_rtd++;
905
906 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907}
908
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200909static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600910{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200911 if (!component->probed)
912 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600913
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200914 /* This is a HACK and will be removed soon */
915 if (component->codec)
916 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600917
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200918 if (component->remove)
919 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600920
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200921 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600922
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200923 soc_cleanup_component_debugfs(component);
924 component->probed = 0;
925 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600926}
927
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200928static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200929{
930 int err;
931
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200932 if (dai && dai->probed &&
933 dai->driver->remove_order == order) {
934 if (dai->driver->remove) {
935 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100936 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200937 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100938 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200939 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100940 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200941 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100942 }
943}
944
Stephen Warren62ae68f2012-06-08 12:34:23 -0600945static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946{
947 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200948 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000949
950 /* unregister the rtd device */
951 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800952 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
953 device_remove_file(rtd->dev, &dev_attr_codec_reg);
954 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 rtd->dev_registered = 0;
956 }
957
958 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200959 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200960 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200962 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963}
964
Stephen Warren62ae68f2012-06-08 12:34:23 -0600965static void soc_remove_link_components(struct snd_soc_card *card, int num,
966 int order)
967{
968 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
969 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600970 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200971 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200972 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600973
974 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200975 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200976 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600977
978 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200979 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200980 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200981 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200982 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600983 }
984
985 /* remove any CPU-side CODEC */
986 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200987 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200988 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989 }
990}
991
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900992static void soc_remove_dai_links(struct snd_soc_card *card)
993{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100994 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900995
Liam Girdwood0168bf02011-06-07 16:08:05 +0100996 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
997 order++) {
998 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001000 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001
1002 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1003 order++) {
1004 for (dai = 0; dai < card->num_rtd; dai++)
1005 soc_remove_link_components(card, dai, order);
1006 }
1007
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001008 card->num_rtd = 0;
1009}
1010
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001011static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001012 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001013{
1014 int i;
1015
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001016 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001017 return;
1018
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001019 for (i = 0; i < card->num_configs; i++) {
1020 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001021 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001022 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001023 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001024 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001025 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001026 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001027 }
1028}
1029
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001030static int soc_probe_component(struct snd_soc_card *card,
1031 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001032{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001033 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001034 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001035 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001036
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001037 if (component->probed)
1038 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001039
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001040 component->card = card;
1041 dapm->card = card;
1042 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001043
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001044 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d293312011-01-27 16:24:22 +02001045 return -ENODEV;
1046
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001047 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001048
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001049 if (component->dapm_widgets) {
1050 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1051 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001052
1053 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001054 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001055 "Failed to create new controls %d\n", ret);
1056 goto err_probe;
1057 }
1058 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001059
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001060 list_for_each_entry(dai, &component->dai_list, list) {
1061 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001062 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001063 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001064 "Failed to create DAI widgets %d\n", ret);
1065 goto err_probe;
1066 }
1067 }
Mark Brown888df392012-02-16 19:37:51 -08001068
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001069 if (component->probe) {
1070 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001071 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001072 dev_err(component->dev,
1073 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001074 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001075 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001076
1077 WARN(dapm->idle_bias_off &&
1078 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001079 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001080 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001081 }
1082
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001083 if (component->controls)
1084 snd_soc_add_component_controls(component, component->controls,
1085 component->num_controls);
1086 if (component->dapm_routes)
1087 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1088 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001089
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001090 component->probed = 1;
1091 list_add(&dapm->list, &card->dapm_list);
1092
1093 /* This is a HACK and will be removed soon */
1094 if (component->codec)
1095 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001096
Jarkko Nikula70d293312011-01-27 16:24:22 +02001097 return 0;
1098
1099err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001100 soc_cleanup_component_debugfs(component);
1101 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001102
1103 return ret;
1104}
1105
Mark Brown36ae1a92012-01-06 17:12:45 -08001106static void rtd_release(struct device *dev)
1107{
1108 kfree(dev);
1109}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001110
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001111static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1112 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001113{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114 int ret = 0;
1115
Jarkko Nikula589c3562010-12-06 16:27:07 +02001116 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001117 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1118 if (!rtd->dev)
1119 return -ENOMEM;
1120 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001121 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001122 rtd->dev->release = rtd_release;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001123 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001124 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001125 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001126 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1127 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1128 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1129 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001130 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001132 /* calling put_device() here to free the rtd->dev */
1133 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001134 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001135 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001136 return ret;
1137 }
1138 rtd->dev_registered = 1;
1139
Lars-Peter Clausen93c3ce762014-08-19 15:51:20 +02001140 if (rtd->codec) {
1141 /* add DAPM sysfs entries for this codec */
1142 ret = snd_soc_dapm_sys_add(rtd->dev);
1143 if (ret < 0)
1144 dev_err(rtd->dev,
1145 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1146 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001147
Lars-Peter Clausen93c3ce762014-08-19 15:51:20 +02001148 /* add codec sysfs entries */
1149 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1150 if (ret < 0)
1151 dev_err(rtd->dev,
1152 "ASoC: failed to add codec sysfs files: %d\n",
1153 ret);
1154 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001155
1156 return 0;
1157}
1158
Stephen Warren62ae68f2012-06-08 12:34:23 -06001159static int soc_probe_link_components(struct snd_soc_card *card, int num,
1160 int order)
1161{
1162 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001163 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001164 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001165 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001166
1167 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001168 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001169 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001170 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001171 if (ret < 0)
1172 return ret;
1173 }
1174
Benoit Cousson88bd8702014-07-08 23:19:34 +02001175 /* probe the CODEC-side components */
1176 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001177 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001178 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001179 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001180 if (ret < 0)
1181 return ret;
1182 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001183 }
1184
1185 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001186 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001187 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001188 if (ret < 0)
1189 return ret;
1190 }
1191
1192 return 0;
1193}
1194
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001195static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001196{
1197 int ret;
1198
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001199 if (!dai->probed && dai->driver->probe_order == order) {
1200 if (dai->driver->probe) {
1201 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001202 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001203 dev_err(dai->dev,
1204 "ASoC: failed to probe DAI %s: %d\n",
1205 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001206 return ret;
1207 }
1208 }
1209
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001210 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001211 }
1212
1213 return 0;
1214}
1215
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001216static int soc_link_dai_widgets(struct snd_soc_card *card,
1217 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001218 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001219{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001220 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1221 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001222 struct snd_soc_dapm_widget *play_w, *capture_w;
1223 int ret;
1224
Benoit Cousson88bd8702014-07-08 23:19:34 +02001225 if (rtd->num_codecs > 1)
1226 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1227
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001228 /* link the DAI widgets */
1229 play_w = codec_dai->playback_widget;
1230 capture_w = cpu_dai->capture_widget;
1231 if (play_w && capture_w) {
1232 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1233 capture_w, play_w);
1234 if (ret != 0) {
1235 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1236 play_w->name, capture_w->name, ret);
1237 return ret;
1238 }
1239 }
1240
1241 play_w = cpu_dai->playback_widget;
1242 capture_w = codec_dai->capture_widget;
1243 if (play_w && capture_w) {
1244 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1245 capture_w, play_w);
1246 if (ret != 0) {
1247 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1248 play_w->name, capture_w->name, ret);
1249 return ret;
1250 }
1251 }
1252
1253 return 0;
1254}
1255
Stephen Warren62ae68f2012-06-08 12:34:23 -06001256static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001257{
1258 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1259 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownc74184e2012-04-04 22:12:09 +01001260 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001261 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001262
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001263 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001264 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 /* set default power off timeout */
1267 rtd->pmdown_time = pmdown_time;
1268
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001269 ret = soc_probe_dai(cpu_dai, order);
1270 if (ret)
1271 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001272
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001273 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001274 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001275 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001276 if (ret)
1277 return ret;
1278 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001279
Liam Girdwood0168bf02011-06-07 16:08:05 +01001280 /* complete DAI probe during last probe */
1281 if (order != SND_SOC_COMP_ORDER_LAST)
1282 return 0;
1283
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001284 /* do machine specific initialization */
1285 if (dai_link->init) {
1286 ret = dai_link->init(rtd);
1287 if (ret < 0) {
1288 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1289 dai_link->name, ret);
1290 return ret;
1291 }
1292 }
1293
1294 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001296 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001297
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001298#ifdef CONFIG_DEBUG_FS
1299 /* add DPCM sysfs entries */
1300 if (dai_link->dynamic) {
1301 ret = soc_dpcm_debugfs_add(rtd);
1302 if (ret < 0) {
1303 dev_err(rtd->dev,
1304 "ASoC: failed to add dpcm sysfs entries: %d\n",
1305 ret);
1306 return ret;
1307 }
1308 }
1309#endif
1310
Mark Brown36ae1a92012-01-06 17:12:45 -08001311 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001312 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001313 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1314 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315
Namarta Kohli1245b702012-08-16 17:10:41 +05301316 if (cpu_dai->driver->compress_dai) {
1317 /*create compress_device"*/
1318 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001319 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001320 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301321 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001322 return ret;
1323 }
1324 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301325
1326 if (!dai_link->params) {
1327 /* create the pcm */
1328 ret = soc_new_pcm(rtd, num);
1329 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001330 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301331 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001332 return ret;
1333 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301334 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001335 INIT_DELAYED_WORK(&rtd->delayed_work,
1336 codec2codec_close_delayed_work);
1337
Namarta Kohli1245b702012-08-16 17:10:41 +05301338 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001339 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001340 if (ret)
1341 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001342 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001343 }
1344
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001345 return 0;
1346}
1347
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001348static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001349{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001350 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001351 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001352 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001353
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001354 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1355 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001356 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001357 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001358
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001359 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001360 return -EPROBE_DEFER;
1361 }
1362
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001363 /*
1364 * Some places still reference rtd->codec, so we have to keep that
1365 * initialized if the component is a CODEC. Once all those references
1366 * have been removed, this code can be removed as well.
1367 */
1368 rtd->codec = rtd->component->codec;
1369
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001370 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001371}
1372
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001373static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1374{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001375 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001376 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001377 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001378
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001379 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001380 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001381 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001382
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001383 /* do machine specific initialization */
1384 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001385 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001386 if (ret < 0) {
1387 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1388 aux_dev->name, ret);
1389 return ret;
1390 }
1391 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001392
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001393 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001394}
1395
1396static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1397{
1398 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001399 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001400
1401 /* unregister the rtd device */
1402 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001403 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001404 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001405 rtd->dev_registered = 0;
1406 }
1407
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001408 if (component && component->probed)
1409 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001410}
1411
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001412static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001413{
1414 int ret;
1415
1416 if (codec->cache_init)
1417 return 0;
1418
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001419 ret = snd_soc_cache_init(codec);
1420 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001421 dev_err(codec->dev,
1422 "ASoC: Failed to set cache compression type: %d\n",
1423 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001424 return ret;
1425 }
1426 codec->cache_init = 1;
1427 return 0;
1428}
1429
Mark Brownb19e6e72012-03-14 21:18:39 +00001430static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001431{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001432 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001433 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001434 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435
Liam Girdwood01b9d992012-03-07 10:38:25 +00001436 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437
Mark Brownb19e6e72012-03-14 21:18:39 +00001438 /* bind DAIs */
1439 for (i = 0; i < card->num_links; i++) {
1440 ret = soc_bind_dai_link(card, i);
1441 if (ret != 0)
1442 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443 }
1444
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001445 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001446 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001447 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001448 if (ret != 0)
1449 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450 }
1451
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001452 /* initialize the register cache for each available codec */
1453 list_for_each_entry(codec, &codec_list, list) {
1454 if (codec->cache_init)
1455 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001456 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001457 if (ret < 0)
1458 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001459 }
1460
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001461 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001462 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 card->owner, 0, &card->snd_card);
1464 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001465 dev_err(card->dev,
1466 "ASoC: can't create sound card for card %s: %d\n",
1467 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001468 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001469 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470
Mark Browne37a4972011-03-02 18:21:57 +00001471 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1472 card->dapm.dev = card->dev;
1473 card->dapm.card = card;
1474 list_add(&card->dapm.list, &card->dapm_list);
1475
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001476#ifdef CONFIG_DEBUG_FS
1477 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1478#endif
1479
Mark Brown88ee1c62011-02-02 10:43:26 +00001480#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001481 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001482 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001483#endif
Andy Green6ed25972008-06-13 16:24:05 +01001484
Mark Brown9a841eb2011-04-12 17:51:37 -07001485 if (card->dapm_widgets)
1486 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1487 card->num_dapm_widgets);
1488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 /* initialise the sound card only once */
1490 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001491 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 if (ret < 0)
1493 goto card_probe_error;
1494 }
1495
Stephen Warren62ae68f2012-06-08 12:34:23 -06001496 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001497 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1498 order++) {
1499 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001500 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001501 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001502 dev_err(card->dev,
1503 "ASoC: failed to instantiate card %d\n",
1504 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001505 goto probe_dai_err;
1506 }
1507 }
1508 }
1509
1510 /* probe all DAI links on this card */
1511 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1512 order++) {
1513 for (i = 0; i < card->num_links; i++) {
1514 ret = soc_probe_link_dais(card, i, order);
1515 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001516 dev_err(card->dev,
1517 "ASoC: failed to instantiate card %d\n",
1518 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001519 goto probe_dai_err;
1520 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001521 }
1522 }
1523
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001524 for (i = 0; i < card->num_aux_devs; i++) {
1525 ret = soc_probe_aux_dev(card, i);
1526 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001527 dev_err(card->dev,
1528 "ASoC: failed to add auxiliary devices %d\n",
1529 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001530 goto probe_aux_dev_err;
1531 }
1532 }
1533
Mark Brown888df392012-02-16 19:37:51 -08001534 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001535 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001536
Mark Brownb7af1da2011-04-07 19:18:44 +09001537 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001538 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001539
Mark Brownb8ad29d2011-03-02 18:35:51 +00001540 if (card->dapm_routes)
1541 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1542 card->num_dapm_routes);
1543
Mark Brown75d9ac42011-09-27 16:41:01 +01001544 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001545 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001546 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001547 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001548
Mark Brownf04209a2012-04-09 16:29:21 +01001549 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001550 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1551 int j;
1552
1553 for (j = 0; j < rtd->num_codecs; j++) {
1554 struct snd_soc_dai *codec_dai = codec_dais[j];
1555
1556 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1557 if (ret != 0 && ret != -ENOTSUPP)
1558 dev_warn(codec_dai->dev,
1559 "ASoC: Failed to set DAI format: %d\n",
1560 ret);
1561 }
Mark Brownf04209a2012-04-09 16:29:21 +01001562 }
1563
1564 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001565 if (dai_fmt &&
1566 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001567 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1568 dai_fmt);
1569 if (ret != 0 && ret != -ENOTSUPP)
1570 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001571 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001572 ret);
1573 } else if (dai_fmt) {
1574 /* Flip the polarity for the "CPU" end */
1575 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1576 switch (dai_link->dai_fmt &
1577 SND_SOC_DAIFMT_MASTER_MASK) {
1578 case SND_SOC_DAIFMT_CBM_CFM:
1579 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1580 break;
1581 case SND_SOC_DAIFMT_CBM_CFS:
1582 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1583 break;
1584 case SND_SOC_DAIFMT_CBS_CFM:
1585 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1586 break;
1587 case SND_SOC_DAIFMT_CBS_CFS:
1588 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1589 break;
1590 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001591
1592 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001593 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001594 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001595 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001596 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001597 ret);
1598 }
1599 }
1600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001603 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1604 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001605 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1606 "%s", card->driver_name ? card->driver_name : card->name);
1607 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1608 switch (card->snd_card->driver[i]) {
1609 case '_':
1610 case '-':
1611 case '\0':
1612 break;
1613 default:
1614 if (!isalnum(card->snd_card->driver[i]))
1615 card->snd_card->driver[i] = '_';
1616 break;
1617 }
1618 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619
Mark Brown28e9ad92011-03-02 18:36:34 +00001620 if (card->late_probe) {
1621 ret = card->late_probe(card);
1622 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001623 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001624 card->name, ret);
1625 goto probe_aux_dev_err;
1626 }
1627 }
1628
Stephen Warren16332812011-11-23 12:42:04 -07001629 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001630 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001631
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001632 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 ret = snd_card_register(card->snd_card);
1635 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001636 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1637 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001638 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639 }
1640
Mark Brown435c5e22008-12-04 15:32:53 +00001641 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001642 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001644
1645 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001646
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001647probe_aux_dev_err:
1648 for (i = 0; i < card->num_aux_devs; i++)
1649 soc_remove_aux_dev(card, i);
1650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001652 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001655 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001656 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001657
1658 snd_card_free(card->snd_card);
1659
Mark Brownb19e6e72012-03-14 21:18:39 +00001660base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001662
Mark Brownb19e6e72012-03-14 21:18:39 +00001663 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001664}
1665
1666/* probes a new socdev */
1667static int soc_probe(struct platform_device *pdev)
1668{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001670
Vinod Koul70a7ca32011-01-14 19:22:48 +05301671 /*
1672 * no card, so machine driver should be registering card
1673 * we should not be here in that case so ret error
1674 */
1675 if (!card)
1676 return -EINVAL;
1677
Mark Brownfe4085e2012-03-02 13:07:41 +00001678 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001679 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001680 card->name);
1681
Mark Brown435c5e22008-12-04 15:32:53 +00001682 /* Bodge while we unpick instantiation */
1683 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001684
Mark Brown28d528c2012-08-09 18:45:23 +01001685 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686}
1687
Vinod Koulb0e26482011-01-13 22:48:02 +05301688static int soc_cleanup_card_resources(struct snd_soc_card *card)
1689{
Vinod Koulb0e26482011-01-13 22:48:02 +05301690 int i;
1691
1692 /* make sure any delayed work runs */
1693 for (i = 0; i < card->num_rtd; i++) {
1694 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001695 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301696 }
1697
1698 /* remove auxiliary devices */
1699 for (i = 0; i < card->num_aux_devs; i++)
1700 soc_remove_aux_dev(card, i);
1701
1702 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001703 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301704
1705 soc_cleanup_card_debugfs(card);
1706
1707 /* remove the card */
1708 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001709 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301710
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001711 snd_soc_dapm_free(&card->dapm);
1712
Vinod Koulb0e26482011-01-13 22:48:02 +05301713 snd_card_free(card->snd_card);
1714 return 0;
1715
1716}
1717
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001718/* removes a socdev */
1719static int soc_remove(struct platform_device *pdev)
1720{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001722
Mark Brownc5af3a22008-11-28 13:29:45 +00001723 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001724 return 0;
1725}
1726
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001727int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001728{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001729 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730 int i;
Mark Brown51737472009-06-22 13:16:51 +01001731
1732 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001733 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001734
1735 /* Flush out pmdown_time work - we actually do want to run it
1736 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737 for (i = 0; i < card->num_rtd; i++) {
1738 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001739 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740 }
Mark Brown51737472009-06-22 13:16:51 +01001741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001743
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001744 /* deactivate pins to sleep state */
1745 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001746 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1747 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1748 int j;
1749
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001750 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001751 for (j = 0; j < rtd->num_codecs; j++) {
1752 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1753 pinctrl_pm_select_sleep_state(codec_dai->dev);
1754 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001755 }
1756
Mark Brown416356f2009-06-30 19:05:15 +01001757 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001758}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001759EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001760
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001761const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301762 .suspend = snd_soc_suspend,
1763 .resume = snd_soc_resume,
1764 .freeze = snd_soc_suspend,
1765 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001766 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301767 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001768};
Stephen Warrendeb26072011-04-05 19:35:30 -06001769EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001770
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771/* ASoC platform driver */
1772static struct platform_driver soc_driver = {
1773 .driver = {
1774 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001775 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001776 },
1777 .probe = soc_probe,
1778 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001779};
1780
Mark Brown096e49d2009-07-05 15:12:22 +01001781/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782 * snd_soc_cnew - create new control
1783 * @_template: control template
1784 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001785 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001786 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001787 *
1788 * Create a new mixer control from a template control.
1789 *
1790 * Returns 0 for success, else error.
1791 */
1792struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08001793 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00001794 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795{
1796 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001797 struct snd_kcontrol *kcontrol;
1798 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001799
1800 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001801 template.index = 0;
1802
Mark Brownefb7ac32011-03-08 17:23:24 +00001803 if (!long_name)
1804 long_name = template.name;
1805
1806 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02001807 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00001808 if (!name)
1809 return NULL;
1810
Mark Brownefb7ac32011-03-08 17:23:24 +00001811 template.name = name;
1812 } else {
1813 template.name = long_name;
1814 }
1815
1816 kcontrol = snd_ctl_new1(&template, data);
1817
1818 kfree(name);
1819
1820 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001821}
1822EXPORT_SYMBOL_GPL(snd_soc_cnew);
1823
Liam Girdwood022658b2012-02-03 17:43:09 +00001824static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1825 const struct snd_kcontrol_new *controls, int num_controls,
1826 const char *prefix, void *data)
1827{
1828 int err, i;
1829
1830 for (i = 0; i < num_controls; i++) {
1831 const struct snd_kcontrol_new *control = &controls[i];
1832 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1833 control->name, prefix));
1834 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001835 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1836 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00001837 return err;
1838 }
1839 }
1840
1841 return 0;
1842}
1843
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01001844struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1845 const char *name)
1846{
1847 struct snd_card *card = soc_card->snd_card;
1848 struct snd_kcontrol *kctl;
1849
1850 if (unlikely(!name))
1851 return NULL;
1852
1853 list_for_each_entry(kctl, &card->controls, list)
1854 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1855 return kctl;
1856 return NULL;
1857}
1858EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1859
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001860/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001861 * snd_soc_add_component_controls - Add an array of controls to a component.
1862 *
1863 * @component: Component to add controls to
1864 * @controls: Array of controls to add
1865 * @num_controls: Number of elements in the array
1866 *
1867 * Return: 0 for success, else error.
1868 */
1869int snd_soc_add_component_controls(struct snd_soc_component *component,
1870 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1871{
1872 struct snd_card *card = component->card->snd_card;
1873
1874 return snd_soc_add_controls(card, component->dev, controls,
1875 num_controls, component->name_prefix, component);
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1878
1879/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001880 * snd_soc_add_codec_controls - add an array of controls to a codec.
1881 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00001882 * duplicating this code.
1883 *
1884 * @codec: codec to add controls to
1885 * @controls: array of controls to add
1886 * @num_controls: number of elements in the array
1887 *
1888 * Return 0 for success, else error.
1889 */
Liam Girdwood022658b2012-02-03 17:43:09 +00001890int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001891 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00001892{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001893 return snd_soc_add_component_controls(&codec->component, controls,
1894 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001895}
Liam Girdwood022658b2012-02-03 17:43:09 +00001896EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001897
1898/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001899 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00001900 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001901 *
1902 * @platform: platform to add controls to
1903 * @controls: array of controls to add
1904 * @num_controls: number of elements in the array
1905 *
1906 * Return 0 for success, else error.
1907 */
1908int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001909 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001910{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001911 return snd_soc_add_component_controls(&platform->component, controls,
1912 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001913}
1914EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1915
1916/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001917 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1918 * Convenience function to add a list of controls.
1919 *
1920 * @soc_card: SoC card to add controls to
1921 * @controls: array of controls to add
1922 * @num_controls: number of elements in the array
1923 *
1924 * Return 0 for success, else error.
1925 */
1926int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1927 const struct snd_kcontrol_new *controls, int num_controls)
1928{
1929 struct snd_card *card = soc_card->snd_card;
1930
1931 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1932 NULL, soc_card);
1933}
1934EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1935
1936/**
1937 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1938 * Convienience function to add a list of controls.
1939 *
1940 * @dai: DAI to add controls to
1941 * @controls: array of controls to add
1942 * @num_controls: number of elements in the array
1943 *
1944 * Return 0 for success, else error.
1945 */
1946int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1947 const struct snd_kcontrol_new *controls, int num_controls)
1948{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01001949 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00001950
1951 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1952 NULL, dai);
1953}
1954EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1955
1956/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001957 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1958 * @dai: DAI
1959 * @clk_id: DAI specific clock ID
1960 * @freq: new clock frequency in Hz
1961 * @dir: new clock direction - input/output.
1962 *
1963 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1964 */
1965int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1966 unsigned int freq, int dir)
1967{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968 if (dai->driver && dai->driver->ops->set_sysclk)
1969 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00001970 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01001971 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00001972 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001973 else
Mark Brown1104a9c2014-01-15 19:04:19 +00001974 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001975}
1976EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1977
1978/**
Mark Brownec4ee522011-03-07 20:58:11 +00001979 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
1980 * @codec: CODEC
1981 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01001982 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00001983 * @freq: new clock frequency in Hz
1984 * @dir: new clock direction - input/output.
1985 *
1986 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
1987 */
1988int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01001989 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00001990{
1991 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01001992 return codec->driver->set_sysclk(codec, clk_id, source,
1993 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00001994 else
Mark Brown1104a9c2014-01-15 19:04:19 +00001995 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00001996}
1997EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
1998
1999/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002000 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2001 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002002 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002003 * @div: new clock divisor.
2004 *
2005 * Configures the clock dividers. This is used to derive the best DAI bit and
2006 * frame clocks from the system or master clock. It's best to set the DAI bit
2007 * and frame clocks as low as possible to save system power.
2008 */
2009int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2010 int div_id, int div)
2011{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 if (dai->driver && dai->driver->ops->set_clkdiv)
2013 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002014 else
2015 return -EINVAL;
2016}
2017EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2018
2019/**
2020 * snd_soc_dai_set_pll - configure DAI PLL.
2021 * @dai: DAI
2022 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002023 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002024 * @freq_in: PLL input clock frequency in Hz
2025 * @freq_out: requested PLL output clock frequency in Hz
2026 *
2027 * Configures and enables PLL to generate output clock based on input clock.
2028 */
Mark Brown85488032009-09-05 18:52:16 +01002029int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2030 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002031{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002032 if (dai->driver && dai->driver->ops->set_pll)
2033 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002034 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002035 else if (dai->codec && dai->codec->driver->set_pll)
2036 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2037 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002038 else
2039 return -EINVAL;
2040}
2041EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2042
Mark Brownec4ee522011-03-07 20:58:11 +00002043/*
2044 * snd_soc_codec_set_pll - configure codec PLL.
2045 * @codec: CODEC
2046 * @pll_id: DAI specific PLL ID
2047 * @source: DAI specific source for the PLL
2048 * @freq_in: PLL input clock frequency in Hz
2049 * @freq_out: requested PLL output clock frequency in Hz
2050 *
2051 * Configures and enables PLL to generate output clock based on input clock.
2052 */
2053int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2054 unsigned int freq_in, unsigned int freq_out)
2055{
2056 if (codec->driver->set_pll)
2057 return codec->driver->set_pll(codec, pll_id, source,
2058 freq_in, freq_out);
2059 else
2060 return -EINVAL;
2061}
2062EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2063
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002064/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002065 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2066 * @dai: DAI
2067 * @ratio Ratio of BCLK to Sample rate.
2068 *
2069 * Configures the DAI for a preset BCLK to sample rate ratio.
2070 */
2071int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2072{
2073 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2074 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2075 else
2076 return -EINVAL;
2077}
2078EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2079
2080/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002081 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2082 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002083 * @fmt: SND_SOC_DAIFMT_ format value.
2084 *
2085 * Configures the DAI hardware format and clocking.
2086 */
2087int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2088{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002089 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002090 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002091 if (dai->driver->ops->set_fmt == NULL)
2092 return -ENOTSUPP;
2093 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002094}
2095EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2096
2097/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002098 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002099 * @slots: Number of slots in use.
2100 * @tx_mask: bitmask representing active TX slots.
2101 * @rx_mask: bitmask representing active RX slots.
2102 *
2103 * Generates the TDM tx and rx slot default masks for DAI.
2104 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002105static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002106 unsigned int *tx_mask,
2107 unsigned int *rx_mask)
2108{
2109 if (*tx_mask || *rx_mask)
2110 return 0;
2111
2112 if (!slots)
2113 return -EINVAL;
2114
2115 *tx_mask = (1 << slots) - 1;
2116 *rx_mask = (1 << slots) - 1;
2117
2118 return 0;
2119}
2120
2121/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002122 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2123 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002124 * @tx_mask: bitmask representing active TX slots.
2125 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002126 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002127 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002128 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002129 * This function configures the specified DAI for TDM operation. @slot contains
2130 * the total number of slots of the TDM stream and @slot_with the width of each
2131 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2132 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2133 * DAI should write to or read from. If a bit is set the corresponding slot is
2134 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2135 * the first slot, bit 1 to the second slot and so on. The first active slot
2136 * maps to the first channel of the DAI, the second active slot to the second
2137 * channel and so on.
2138 *
2139 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2140 * @rx_mask and @slot_width will be ignored.
2141 *
2142 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002143 */
2144int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002145 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002146{
Xiubo Lie5c21512014-03-21 14:17:12 +08002147 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2148 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002149 &tx_mask, &rx_mask);
2150 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002151 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002152
Benoit Cousson88bd8702014-07-08 23:19:34 +02002153 dai->tx_mask = tx_mask;
2154 dai->rx_mask = rx_mask;
2155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002156 if (dai->driver && dai->driver->ops->set_tdm_slot)
2157 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002158 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002159 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002160 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002161}
2162EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2163
2164/**
Barry Song472df3c2009-09-12 01:16:29 +08002165 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2166 * @dai: DAI
2167 * @tx_num: how many TX channels
2168 * @tx_slot: pointer to an array which imply the TX slot number channel
2169 * 0~num-1 uses
2170 * @rx_num: how many RX channels
2171 * @rx_slot: pointer to an array which imply the RX slot number channel
2172 * 0~num-1 uses
2173 *
2174 * configure the relationship between channel number and TDM slot number.
2175 */
2176int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2177 unsigned int tx_num, unsigned int *tx_slot,
2178 unsigned int rx_num, unsigned int *rx_slot)
2179{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002180 if (dai->driver && dai->driver->ops->set_channel_map)
2181 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002182 rx_num, rx_slot);
2183 else
2184 return -EINVAL;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2187
2188/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002189 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2190 * @dai: DAI
2191 * @tristate: tristate enable
2192 *
2193 * Tristates the DAI so that others can use it.
2194 */
2195int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2196{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002197 if (dai->driver && dai->driver->ops->set_tristate)
2198 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002199 else
2200 return -EINVAL;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2203
2204/**
2205 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2206 * @dai: DAI
2207 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002208 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002209 *
2210 * Mutes the DAI DAC.
2211 */
Mark Brownda183962013-02-06 15:44:07 +00002212int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2213 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002214{
Mark Brownda183962013-02-06 15:44:07 +00002215 if (!dai->driver)
2216 return -ENOTSUPP;
2217
2218 if (dai->driver->ops->mute_stream)
2219 return dai->driver->ops->mute_stream(dai, mute, direction);
2220 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2221 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002222 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002223 else
Mark Brown04570c62012-04-13 19:16:03 +01002224 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002225}
2226EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2227
Benoit Cousson88bd8702014-07-08 23:19:34 +02002228static int snd_soc_init_multicodec(struct snd_soc_card *card,
2229 struct snd_soc_dai_link *dai_link)
2230{
2231 /* Legacy codec/codec_dai link is a single entry in multicodec */
2232 if (dai_link->codec_name || dai_link->codec_of_node ||
2233 dai_link->codec_dai_name) {
2234 dai_link->num_codecs = 1;
2235
2236 dai_link->codecs = devm_kzalloc(card->dev,
2237 sizeof(struct snd_soc_dai_link_component),
2238 GFP_KERNEL);
2239 if (!dai_link->codecs)
2240 return -ENOMEM;
2241
2242 dai_link->codecs[0].name = dai_link->codec_name;
2243 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2244 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2245 }
2246
2247 if (!dai_link->codecs) {
2248 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2249 return -EINVAL;
2250 }
2251
2252 return 0;
2253}
2254
Mark Brownc5af3a22008-11-28 13:29:45 +00002255/**
2256 * snd_soc_register_card - Register a card with the ASoC core
2257 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002258 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002259 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002260 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302261int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002262{
Benoit Cousson88bd8702014-07-08 23:19:34 +02002263 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002264
Mark Brownc5af3a22008-11-28 13:29:45 +00002265 if (!card->name || !card->dev)
2266 return -EINVAL;
2267
Stephen Warren5a504962011-12-21 10:40:59 -07002268 for (i = 0; i < card->num_links; i++) {
2269 struct snd_soc_dai_link *link = &card->dai_link[i];
2270
Benoit Cousson88bd8702014-07-08 23:19:34 +02002271 ret = snd_soc_init_multicodec(card, link);
2272 if (ret) {
2273 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2274 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002275 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02002276
2277 for (j = 0; j < link->num_codecs; j++) {
2278 /*
2279 * Codec must be specified by 1 of name or OF node,
2280 * not both or neither.
2281 */
2282 if (!!link->codecs[j].name ==
2283 !!link->codecs[j].of_node) {
2284 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2285 link->name);
2286 return -EINVAL;
2287 }
2288 /* Codec DAI name must be specified */
2289 if (!link->codecs[j].dai_name) {
2290 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2291 link->name);
2292 return -EINVAL;
2293 }
Stephen Warrenbc926572012-05-25 18:22:11 -06002294 }
Stephen Warren5a504962011-12-21 10:40:59 -07002295
2296 /*
2297 * Platform may be specified by either name or OF node, but
2298 * can be left unspecified, and a dummy platform will be used.
2299 */
2300 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002301 dev_err(card->dev,
2302 "ASoC: Both platform name/of_node are set for %s\n",
2303 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002304 return -EINVAL;
2305 }
2306
2307 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06002308 * CPU device may be specified by either name or OF node, but
2309 * can be left unspecified, and will be matched based on DAI
2310 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07002311 */
Stephen Warrenbc926572012-05-25 18:22:11 -06002312 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002313 dev_err(card->dev,
2314 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2315 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06002316 return -EINVAL;
2317 }
2318 /*
2319 * At least one of CPU DAI name or CPU device name/node must be
2320 * specified
2321 */
2322 if (!link->cpu_dai_name &&
2323 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002324 dev_err(card->dev,
2325 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2326 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002327 return -EINVAL;
2328 }
2329 }
2330
Mark Browned77cc12011-05-03 18:25:34 +01002331 dev_set_drvdata(card->dev, card);
2332
Stephen Warren111c6412011-01-28 14:26:35 -07002333 snd_soc_initialize_card_lists(card);
2334
Vinod Koul150dd2f2011-01-13 22:48:32 +05302335 soc_init_card_debugfs(card);
2336
Mark Brown181a6892012-03-14 20:18:49 +00002337 card->rtd = devm_kzalloc(card->dev,
2338 sizeof(struct snd_soc_pcm_runtime) *
2339 (card->num_links + card->num_aux_devs),
2340 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002341 if (card->rtd == NULL)
2342 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01002343 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002344 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002345
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002346 for (i = 0; i < card->num_links; i++) {
2347 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002348 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002349 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2350 sizeof(struct snd_soc_dai *) *
2351 (card->rtd[i].dai_link->num_codecs),
2352 GFP_KERNEL);
2353 if (card->rtd[i].codec_dais == NULL)
2354 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002355 }
2356
2357 for (i = 0; i < card->num_aux_devs; i++)
2358 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002359
Mark Browndb432b42011-10-03 21:06:40 +01002360 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002361 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002362 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002363 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002364
Mark Brownb19e6e72012-03-14 21:18:39 +00002365 ret = snd_soc_instantiate_card(card);
2366 if (ret != 0)
2367 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002368
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002369 /* deactivate pins to sleep state */
2370 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002371 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2372 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2373 int j;
2374
2375 for (j = 0; j < rtd->num_codecs; j++) {
2376 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2377 if (!codec_dai->active)
2378 pinctrl_pm_select_sleep_state(codec_dai->dev);
2379 }
2380
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002381 if (!cpu_dai->active)
2382 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2383 }
2384
Mark Brownb19e6e72012-03-14 21:18:39 +00002385 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00002386}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302387EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002388
2389/**
2390 * snd_soc_unregister_card - Unregister a card with the ASoC core
2391 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002392 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002393 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002394 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302395int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002396{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002397 if (card->instantiated) {
2398 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002399 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302400 soc_cleanup_card_resources(card);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002401 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002402 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002403
2404 return 0;
2405}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302406EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002407
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002408/*
2409 * Simplify DAI link configuration by removing ".-1" from device names
2410 * and sanitizing names.
2411 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002412static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002413{
2414 char *found, name[NAME_SIZE];
2415 int id1, id2;
2416
2417 if (dev_name(dev) == NULL)
2418 return NULL;
2419
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002420 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002421
2422 /* are we a "%s.%d" name (platform and SPI components) */
2423 found = strstr(name, dev->driver->name);
2424 if (found) {
2425 /* get ID */
2426 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2427
2428 /* discard ID from name if ID == -1 */
2429 if (*id == -1)
2430 found[strlen(dev->driver->name)] = '\0';
2431 }
2432
2433 } else {
2434 /* I2C component devices are named "bus-addr" */
2435 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2436 char tmp[NAME_SIZE];
2437
2438 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002439 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002440
2441 /* sanitize component name for DAI link creation */
2442 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002443 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002444 } else
2445 *id = 0;
2446 }
2447
2448 return kstrdup(name, GFP_KERNEL);
2449}
2450
2451/*
2452 * Simplify DAI link naming for single devices with multiple DAIs by removing
2453 * any ".-1" and using the DAI name (instead of device name).
2454 */
2455static inline char *fmt_multiple_name(struct device *dev,
2456 struct snd_soc_dai_driver *dai_drv)
2457{
2458 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002459 dev_err(dev,
2460 "ASoC: error - multiple DAI %s registered with no name\n",
2461 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002462 return NULL;
2463 }
2464
2465 return kstrdup(dai_drv->name, GFP_KERNEL);
2466}
2467
Mark Brown91151712008-11-30 23:31:24 +00002468/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002469 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002470 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002471 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002472 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002473static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002474{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002475 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002476
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002477 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002478 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2479 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002480 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002481 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002482 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002483 }
Mark Brown91151712008-11-30 23:31:24 +00002484}
Mark Brown91151712008-11-30 23:31:24 +00002485
2486/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002487 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002488 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002489 * @component: The component the DAIs are registered for
2490 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002491 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002492 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2493 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002494 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002495static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002496 struct snd_soc_dai_driver *dai_drv, size_t count,
2497 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00002498{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002499 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002500 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002501 unsigned int i;
2502 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002503
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002504 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002505
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002506 component->dai_drv = dai_drv;
2507 component->num_dai = count;
2508
Mark Brown91151712008-11-30 23:31:24 +00002509 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002510
2511 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08002512 if (dai == NULL) {
2513 ret = -ENOMEM;
2514 goto err;
2515 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002516
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002517 /*
2518 * Back in the old days when we still had component-less DAIs,
2519 * instead of having a static name, component-less DAIs would
2520 * inherit the name of the parent device so it is possible to
2521 * register multiple instances of the DAI. We still need to keep
2522 * the same naming style even though those DAIs are not
2523 * component-less anymore.
2524 */
2525 if (count == 1 && legacy_dai_naming) {
2526 dai->name = fmt_single_name(dev, &dai->id);
2527 } else {
2528 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2529 if (dai_drv[i].id)
2530 dai->id = dai_drv[i].id;
2531 else
2532 dai->id = i;
2533 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002534 if (dai->name == NULL) {
2535 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002536 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00002537 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002538 }
2539
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002540 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002541 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002542 dai->driver = &dai_drv[i];
2543 if (!dai->driver->ops)
2544 dai->driver->ops = &null_dai_ops;
2545
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01002546 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01002547
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002548 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002549 }
2550
2551 return 0;
2552
2553err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002554 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002555
2556 return ret;
2557}
Mark Brown91151712008-11-30 23:31:24 +00002558
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002559static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2560 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002561{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002562 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002563
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002564 component->driver->seq_notifier(component, type, subseq);
2565}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002566
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002567static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2568 int event)
2569{
2570 struct snd_soc_component *component = dapm->component;
2571
2572 return component->driver->stream_event(component, event);
2573}
2574
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002575static int snd_soc_component_initialize(struct snd_soc_component *component,
2576 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002577{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002578 struct snd_soc_dapm_context *dapm;
2579
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002580 component->name = fmt_single_name(dev, &component->id);
2581 if (!component->name) {
2582 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002583 return -ENOMEM;
2584 }
2585
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002586 component->dev = dev;
2587 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002588 component->probe = component->driver->probe;
2589 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002590
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002591 if (!component->dapm_ptr)
2592 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002593
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002594 dapm = component->dapm_ptr;
2595 dapm->dev = dev;
2596 dapm->component = component;
2597 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02002598 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002599 if (driver->seq_notifier)
2600 dapm->seq_notifier = snd_soc_component_seq_notifier;
2601 if (driver->stream_event)
2602 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002603
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002604 component->controls = driver->controls;
2605 component->num_controls = driver->num_controls;
2606 component->dapm_widgets = driver->dapm_widgets;
2607 component->num_dapm_widgets = driver->num_dapm_widgets;
2608 component->dapm_routes = driver->dapm_routes;
2609 component->num_dapm_routes = driver->num_dapm_routes;
2610
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002611 INIT_LIST_HEAD(&component->dai_list);
2612 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002613
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002614 return 0;
2615}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002616
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002617static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002618{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002619 int val_bytes = regmap_get_val_bytes(component->regmap);
2620
2621 /* Errors are legitimate for non-integer byte multiples */
2622 if (val_bytes > 0)
2623 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002624}
2625
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002626#ifdef CONFIG_REGMAP
2627
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002628/**
2629 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2630 * @component: The component for which to initialize the regmap instance
2631 * @regmap: The regmap instance that should be used by the component
2632 *
2633 * This function allows deferred assignment of the regmap instance that is
2634 * associated with the component. Only use this if the regmap instance is not
2635 * yet ready when the component is registered. The function must also be called
2636 * before the first IO attempt of the component.
2637 */
2638void snd_soc_component_init_regmap(struct snd_soc_component *component,
2639 struct regmap *regmap)
2640{
2641 component->regmap = regmap;
2642 snd_soc_component_setup_regmap(component);
2643}
2644EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2645
2646/**
2647 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2648 * @component: The component for which to de-initialize the regmap instance
2649 *
2650 * Calls regmap_exit() on the regmap instance associated to the component and
2651 * removes the regmap instance from the component.
2652 *
2653 * This function should only be used if snd_soc_component_init_regmap() was used
2654 * to initialize the regmap instance.
2655 */
2656void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2657{
2658 regmap_exit(component->regmap);
2659 component->regmap = NULL;
2660}
2661EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2662
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002663#endif
2664
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002665static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2666{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002667 if (!component->write && !component->read) {
2668 if (!component->regmap)
2669 component->regmap = dev_get_regmap(component->dev, NULL);
2670 if (component->regmap)
2671 snd_soc_component_setup_regmap(component);
2672 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002673
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002674 list_add(&component->list, &component_list);
2675}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002676
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002677static void snd_soc_component_add(struct snd_soc_component *component)
2678{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002679 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002680 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002681 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002682}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002683
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002684static void snd_soc_component_cleanup(struct snd_soc_component *component)
2685{
2686 snd_soc_unregister_dais(component);
2687 kfree(component->name);
2688}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002689
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002690static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2691{
2692 list_del(&component->list);
2693}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002694
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002695static void snd_soc_component_del(struct snd_soc_component *component)
2696{
2697 mutex_lock(&client_mutex);
2698 snd_soc_component_del_unlocked(component);
2699 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002700}
2701
2702int snd_soc_register_component(struct device *dev,
2703 const struct snd_soc_component_driver *cmpnt_drv,
2704 struct snd_soc_dai_driver *dai_drv,
2705 int num_dai)
2706{
2707 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002708 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002709
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002710 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002711 if (!cmpnt) {
2712 dev_err(dev, "ASoC: Failed to allocate memory\n");
2713 return -ENOMEM;
2714 }
2715
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002716 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2717 if (ret)
2718 goto err_free;
2719
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002720 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002721 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002722
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002723 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2724 if (ret < 0) {
2725 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
2726 goto err_cleanup;
2727 }
2728
2729 snd_soc_component_add(cmpnt);
2730
2731 return 0;
2732
2733err_cleanup:
2734 snd_soc_component_cleanup(cmpnt);
2735err_free:
2736 kfree(cmpnt);
2737 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002738}
2739EXPORT_SYMBOL_GPL(snd_soc_register_component);
2740
2741/**
2742 * snd_soc_unregister_component - Unregister a component from the ASoC core
2743 *
2744 */
2745void snd_soc_unregister_component(struct device *dev)
2746{
2747 struct snd_soc_component *cmpnt;
2748
2749 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002750 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002751 goto found;
2752 }
2753 return;
2754
2755found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002756 snd_soc_component_del(cmpnt);
2757 snd_soc_component_cleanup(cmpnt);
2758 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002759}
2760EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2761
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002762static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002763{
2764 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2765
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002766 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002767}
2768
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002769static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002770{
2771 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2772
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002773 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002774}
2775
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002776/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002777 * snd_soc_add_platform - Add a platform to the ASoC core
2778 * @dev: The parent device for the platform
2779 * @platform: The platform to add
2780 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00002781 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002782int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01002783 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002784{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002785 int ret;
2786
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002787 ret = snd_soc_component_initialize(&platform->component,
2788 &platform_drv->component_driver, dev);
2789 if (ret)
2790 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002791
2792 platform->dev = dev;
2793 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002794
2795 if (platform_drv->probe)
2796 platform->component.probe = snd_soc_platform_drv_probe;
2797 if (platform_drv->remove)
2798 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002799
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02002800#ifdef CONFIG_DEBUG_FS
2801 platform->component.debugfs_prefix = "platform";
2802#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00002803
2804 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002805 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002806 list_add(&platform->list, &platform_list);
2807 mutex_unlock(&client_mutex);
2808
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002809 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2810 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002811
2812 return 0;
2813}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002814EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2815
2816/**
2817 * snd_soc_register_platform - Register a platform with the ASoC core
2818 *
2819 * @platform: platform to register
2820 */
2821int snd_soc_register_platform(struct device *dev,
2822 const struct snd_soc_platform_driver *platform_drv)
2823{
2824 struct snd_soc_platform *platform;
2825 int ret;
2826
2827 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2828
2829 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2830 if (platform == NULL)
2831 return -ENOMEM;
2832
2833 ret = snd_soc_add_platform(dev, platform, platform_drv);
2834 if (ret)
2835 kfree(platform);
2836
2837 return ret;
2838}
Mark Brown12a48a8c2008-12-03 19:40:30 +00002839EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2840
2841/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002842 * snd_soc_remove_platform - Remove a platform from the ASoC core
2843 * @platform: the platform to remove
2844 */
2845void snd_soc_remove_platform(struct snd_soc_platform *platform)
2846{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002847
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002848 mutex_lock(&client_mutex);
2849 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002850 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002851 mutex_unlock(&client_mutex);
2852
2853 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002854 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02002855
2856 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002857}
2858EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2859
2860struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2861{
2862 struct snd_soc_platform *platform;
2863
2864 list_for_each_entry(platform, &platform_list, list) {
2865 if (dev == platform->dev)
2866 return platform;
2867 }
2868
2869 return NULL;
2870}
2871EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2872
2873/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00002874 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2875 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002876 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002877 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002878void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002879{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002880 struct snd_soc_platform *platform;
2881
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002882 platform = snd_soc_lookup_platform(dev);
2883 if (!platform)
2884 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002885
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002886 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002887 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002888}
2889EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2890
Mark Brown151ab222009-05-09 16:22:58 +01002891static u64 codec_format_map[] = {
2892 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2893 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2894 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2895 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2896 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2897 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2898 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2899 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2900 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2901 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2902 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2903 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2904 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2905 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2906 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2907 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2908};
2909
2910/* Fix up the DAI formats for endianness: codecs don't actually see
2911 * the endianness of the data but we're using the CPU format
2912 * definitions which do need to include endianness so we ensure that
2913 * codec DAIs always have both big and little endian variants set.
2914 */
2915static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2916{
2917 int i;
2918
2919 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2920 if (stream->formats & codec_format_map[i])
2921 stream->formats |= codec_format_map[i];
2922}
2923
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002924static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2925{
2926 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2927
2928 return codec->driver->probe(codec);
2929}
2930
2931static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
2932{
2933 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2934
2935 codec->driver->remove(codec);
2936}
2937
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002938static int snd_soc_codec_drv_write(struct snd_soc_component *component,
2939 unsigned int reg, unsigned int val)
2940{
2941 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2942
2943 return codec->driver->write(codec, reg, val);
2944}
2945
2946static int snd_soc_codec_drv_read(struct snd_soc_component *component,
2947 unsigned int reg, unsigned int *val)
2948{
2949 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2950
2951 *val = codec->driver->read(codec, reg);
2952
2953 return 0;
2954}
2955
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02002956static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
2957 enum snd_soc_bias_level level)
2958{
2959 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
2960
2961 return codec->driver->set_bias_level(codec, level);
2962}
2963
Mark Brown0d0cf002008-12-10 14:32:45 +00002964/**
2965 * snd_soc_register_codec - Register a codec with the ASoC core
2966 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002967 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002968 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002969int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00002970 const struct snd_soc_codec_driver *codec_drv,
2971 struct snd_soc_dai_driver *dai_drv,
2972 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00002973{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002974 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002975 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002976 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01002977
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002978 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00002979
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
2981 if (codec == NULL)
2982 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00002983
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002984 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002985 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002986
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002987 ret = snd_soc_component_initialize(&codec->component,
2988 &codec_drv->component_driver, dev);
2989 if (ret)
2990 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01002991
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002992 if (codec_drv->controls) {
2993 codec->component.controls = codec_drv->controls;
2994 codec->component.num_controls = codec_drv->num_controls;
2995 }
2996 if (codec_drv->dapm_widgets) {
2997 codec->component.dapm_widgets = codec_drv->dapm_widgets;
2998 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
2999 }
3000 if (codec_drv->dapm_routes) {
3001 codec->component.dapm_routes = codec_drv->dapm_routes;
3002 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3003 }
3004
3005 if (codec_drv->probe)
3006 codec->component.probe = snd_soc_codec_drv_probe;
3007 if (codec_drv->remove)
3008 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003009 if (codec_drv->write)
3010 codec->component.write = snd_soc_codec_drv_write;
3011 if (codec_drv->read)
3012 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003013 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003014 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02003015 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003016 if (codec_drv->seq_notifier)
3017 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003018 if (codec_drv->set_bias_level)
3019 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003020 codec->dev = dev;
3021 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003022 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003023
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003024#ifdef CONFIG_DEBUG_FS
3025 codec->component.init_debugfs = soc_init_codec_debugfs;
3026 codec->component.debugfs_prefix = "codec";
3027#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003028
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003029 if (codec_drv->get_regmap)
3030 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003032 for (i = 0; i < num_dai; i++) {
3033 fixup_codec_formats(&dai_drv[i].playback);
3034 fixup_codec_formats(&dai_drv[i].capture);
3035 }
3036
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003037 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3038 if (ret < 0) {
3039 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3040 goto err_cleanup;
3041 }
3042
3043 list_for_each_entry(dai, &codec->component.dai_list, list)
3044 dai->codec = codec;
3045
Mark Brown054880f2012-04-09 17:29:19 +01003046 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003047 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003048 list_add(&codec->list, &codec_list);
3049 mutex_unlock(&client_mutex);
3050
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003051 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3052 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003053 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003055err_cleanup:
3056 snd_soc_component_cleanup(&codec->component);
3057err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003058 kfree(codec);
3059 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003060}
3061EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3062
3063/**
3064 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3065 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003066 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003067 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003068void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003069{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003070 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003071
3072 list_for_each_entry(codec, &codec_list, list) {
3073 if (dev == codec->dev)
3074 goto found;
3075 }
3076 return;
3077
3078found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079
Mark Brown0d0cf002008-12-10 14:32:45 +00003080 mutex_lock(&client_mutex);
3081 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003082 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003083 mutex_unlock(&client_mutex);
3084
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003085 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3086 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003088 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003089 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003090 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003091}
3092EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3093
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003094/* Retrieve a card's name from device tree */
3095int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3096 const char *propname)
3097{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303098 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003099 int ret;
3100
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303101 if (!card->dev) {
3102 pr_err("card->dev is not set before calling %s\n", __func__);
3103 return -EINVAL;
3104 }
3105
3106 np = card->dev->of_node;
3107
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003108 ret = of_property_read_string_index(np, propname, 0, &card->name);
3109 /*
3110 * EINVAL means the property does not exist. This is fine providing
3111 * card->name was previously set, which is checked later in
3112 * snd_soc_register_card.
3113 */
3114 if (ret < 0 && ret != -EINVAL) {
3115 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003116 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003117 propname, ret);
3118 return ret;
3119 }
3120
3121 return 0;
3122}
3123EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3124
Xiubo Li9a6d4862014-02-08 15:59:52 +08003125static const struct snd_soc_dapm_widget simple_widgets[] = {
3126 SND_SOC_DAPM_MIC("Microphone", NULL),
3127 SND_SOC_DAPM_LINE("Line", NULL),
3128 SND_SOC_DAPM_HP("Headphone", NULL),
3129 SND_SOC_DAPM_SPK("Speaker", NULL),
3130};
3131
3132int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3133 const char *propname)
3134{
3135 struct device_node *np = card->dev->of_node;
3136 struct snd_soc_dapm_widget *widgets;
3137 const char *template, *wname;
3138 int i, j, num_widgets, ret;
3139
3140 num_widgets = of_property_count_strings(np, propname);
3141 if (num_widgets < 0) {
3142 dev_err(card->dev,
3143 "ASoC: Property '%s' does not exist\n", propname);
3144 return -EINVAL;
3145 }
3146 if (num_widgets & 1) {
3147 dev_err(card->dev,
3148 "ASoC: Property '%s' length is not even\n", propname);
3149 return -EINVAL;
3150 }
3151
3152 num_widgets /= 2;
3153 if (!num_widgets) {
3154 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3155 propname);
3156 return -EINVAL;
3157 }
3158
3159 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3160 GFP_KERNEL);
3161 if (!widgets) {
3162 dev_err(card->dev,
3163 "ASoC: Could not allocate memory for widgets\n");
3164 return -ENOMEM;
3165 }
3166
3167 for (i = 0; i < num_widgets; i++) {
3168 ret = of_property_read_string_index(np, propname,
3169 2 * i, &template);
3170 if (ret) {
3171 dev_err(card->dev,
3172 "ASoC: Property '%s' index %d read error:%d\n",
3173 propname, 2 * i, ret);
3174 return -EINVAL;
3175 }
3176
3177 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3178 if (!strncmp(template, simple_widgets[j].name,
3179 strlen(simple_widgets[j].name))) {
3180 widgets[i] = simple_widgets[j];
3181 break;
3182 }
3183 }
3184
3185 if (j >= ARRAY_SIZE(simple_widgets)) {
3186 dev_err(card->dev,
3187 "ASoC: DAPM widget '%s' is not supported\n",
3188 template);
3189 return -EINVAL;
3190 }
3191
3192 ret = of_property_read_string_index(np, propname,
3193 (2 * i) + 1,
3194 &wname);
3195 if (ret) {
3196 dev_err(card->dev,
3197 "ASoC: Property '%s' index %d read error:%d\n",
3198 propname, (2 * i) + 1, ret);
3199 return -EINVAL;
3200 }
3201
3202 widgets[i].name = wname;
3203 }
3204
3205 card->dapm_widgets = widgets;
3206 card->num_dapm_widgets = num_widgets;
3207
3208 return 0;
3209}
3210EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3211
Xiubo Li89c67852014-02-14 09:34:35 +08003212int snd_soc_of_parse_tdm_slot(struct device_node *np,
3213 unsigned int *slots,
3214 unsigned int *slot_width)
3215{
3216 u32 val;
3217 int ret;
3218
3219 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3220 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3221 if (ret)
3222 return ret;
3223
3224 if (slots)
3225 *slots = val;
3226 }
3227
3228 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3229 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3230 if (ret)
3231 return ret;
3232
3233 if (slot_width)
3234 *slot_width = val;
3235 }
3236
3237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3240
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003241int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3242 const char *propname)
3243{
3244 struct device_node *np = card->dev->of_node;
Peter Rosinf8781db2014-11-27 22:02:42 +01003245 int num_routes, old_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003246 struct snd_soc_dapm_route *routes;
3247 int i, ret;
3248
3249 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003250 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003251 dev_err(card->dev,
3252 "ASoC: Property '%s' does not exist or its length is not even\n",
3253 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003254 return -EINVAL;
3255 }
3256 num_routes /= 2;
3257 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003258 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003259 propname);
3260 return -EINVAL;
3261 }
3262
Peter Rosinf8781db2014-11-27 22:02:42 +01003263 old_routes = card->num_dapm_routes;
3264 routes = devm_kzalloc(card->dev,
3265 (old_routes + num_routes) * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003266 GFP_KERNEL);
3267 if (!routes) {
3268 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003269 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003270 return -EINVAL;
3271 }
3272
Peter Rosinf8781db2014-11-27 22:02:42 +01003273 memcpy(routes, card->dapm_routes, old_routes * sizeof(*routes));
3274
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003275 for (i = 0; i < num_routes; i++) {
3276 ret = of_property_read_string_index(np, propname,
Peter Rosinf8781db2014-11-27 22:02:42 +01003277 2 * i, &routes[old_routes + i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003278 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003279 dev_err(card->dev,
3280 "ASoC: Property '%s' index %d could not be read: %d\n",
3281 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003282 return -EINVAL;
3283 }
3284 ret = of_property_read_string_index(np, propname,
Peter Rosinf8781db2014-11-27 22:02:42 +01003285 (2 * i) + 1, &routes[old_routes + i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003286 if (ret) {
3287 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003288 "ASoC: Property '%s' index %d could not be read: %d\n",
3289 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003290 return -EINVAL;
3291 }
3292 }
3293
Peter Rosinf8781db2014-11-27 22:02:42 +01003294 card->num_dapm_routes += num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003295 card->dapm_routes = routes;
3296
3297 return 0;
3298}
3299EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3300
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003301unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003302 const char *prefix,
3303 struct device_node **bitclkmaster,
3304 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003305{
3306 int ret, i;
3307 char prop[128];
3308 unsigned int format = 0;
3309 int bit, frame;
3310 const char *str;
3311 struct {
3312 char *name;
3313 unsigned int val;
3314 } of_fmt_table[] = {
3315 { "i2s", SND_SOC_DAIFMT_I2S },
3316 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3317 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3318 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3319 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3320 { "ac97", SND_SOC_DAIFMT_AC97 },
3321 { "pdm", SND_SOC_DAIFMT_PDM},
3322 { "msb", SND_SOC_DAIFMT_MSB },
3323 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003324 };
3325
3326 if (!prefix)
3327 prefix = "";
3328
3329 /*
3330 * check "[prefix]format = xxx"
3331 * SND_SOC_DAIFMT_FORMAT_MASK area
3332 */
3333 snprintf(prop, sizeof(prop), "%sformat", prefix);
3334 ret = of_property_read_string(np, prop, &str);
3335 if (ret == 0) {
3336 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3337 if (strcmp(str, of_fmt_table[i].name) == 0) {
3338 format |= of_fmt_table[i].val;
3339 break;
3340 }
3341 }
3342 }
3343
3344 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003345 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003346 * SND_SOC_DAIFMT_CLOCK_MASK area
3347 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003348 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3349 if (of_get_property(np, prop, NULL))
3350 format |= SND_SOC_DAIFMT_CONT;
3351 else
3352 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003353
3354 /*
3355 * check "[prefix]bitclock-inversion"
3356 * check "[prefix]frame-inversion"
3357 * SND_SOC_DAIFMT_INV_MASK area
3358 */
3359 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3360 bit = !!of_get_property(np, prop, NULL);
3361
3362 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3363 frame = !!of_get_property(np, prop, NULL);
3364
3365 switch ((bit << 4) + frame) {
3366 case 0x11:
3367 format |= SND_SOC_DAIFMT_IB_IF;
3368 break;
3369 case 0x10:
3370 format |= SND_SOC_DAIFMT_IB_NF;
3371 break;
3372 case 0x01:
3373 format |= SND_SOC_DAIFMT_NB_IF;
3374 break;
3375 default:
3376 /* SND_SOC_DAIFMT_NB_NF is default */
3377 break;
3378 }
3379
3380 /*
3381 * check "[prefix]bitclock-master"
3382 * check "[prefix]frame-master"
3383 * SND_SOC_DAIFMT_MASTER_MASK area
3384 */
3385 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3386 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003387 if (bit && bitclkmaster)
3388 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003389
3390 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3391 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003392 if (frame && framemaster)
3393 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003394
3395 switch ((bit << 4) + frame) {
3396 case 0x11:
3397 format |= SND_SOC_DAIFMT_CBM_CFM;
3398 break;
3399 case 0x10:
3400 format |= SND_SOC_DAIFMT_CBM_CFS;
3401 break;
3402 case 0x01:
3403 format |= SND_SOC_DAIFMT_CBS_CFM;
3404 break;
3405 default:
3406 format |= SND_SOC_DAIFMT_CBS_CFS;
3407 break;
3408 }
3409
3410 return format;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3413
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003414static int snd_soc_get_dai_name(struct of_phandle_args *args,
3415 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003416{
3417 struct snd_soc_component *pos;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003418 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003419
3420 mutex_lock(&client_mutex);
3421 list_for_each_entry(pos, &component_list, list) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003422 if (pos->dev->of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003423 continue;
3424
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003425 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003426 ret = pos->driver->of_xlate_dai_name(pos,
3427 args,
3428 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003429 } else {
3430 int id = -1;
3431
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003432 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003433 case 0:
3434 id = 0; /* same as dai_drv[0] */
3435 break;
3436 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003437 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003438 break;
3439 default:
3440 /* not supported */
3441 break;
3442 }
3443
3444 if (id < 0 || id >= pos->num_dai) {
3445 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003446 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003447 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003448
3449 ret = 0;
3450
3451 *dai_name = pos->dai_drv[id].name;
3452 if (!*dai_name)
3453 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003454 }
3455
Kuninori Morimotocb470082013-09-10 17:39:56 -07003456 break;
3457 }
3458 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003459 return ret;
3460}
3461
3462int snd_soc_of_get_dai_name(struct device_node *of_node,
3463 const char **dai_name)
3464{
3465 struct of_phandle_args args;
3466 int ret;
3467
3468 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3469 "#sound-dai-cells", 0, &args);
3470 if (ret)
3471 return ret;
3472
3473 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003474
3475 of_node_put(args.np);
3476
3477 return ret;
3478}
3479EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3480
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003481/*
3482 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3483 * @dev: Card device
3484 * @of_node: Device node
3485 * @dai_link: DAI link
3486 *
3487 * Builds an array of CODEC DAI components from the DAI link property
3488 * 'sound-dai'.
3489 * The array is set in the DAI link and the number of DAIs is set accordingly.
3490 * The device nodes in the array (of_node) must be dereferenced by the caller.
3491 *
3492 * Returns 0 for success
3493 */
3494int snd_soc_of_get_dai_link_codecs(struct device *dev,
3495 struct device_node *of_node,
3496 struct snd_soc_dai_link *dai_link)
3497{
3498 struct of_phandle_args args;
3499 struct snd_soc_dai_link_component *component;
3500 char *name;
3501 int index, num_codecs, ret;
3502
3503 /* Count the number of CODECs */
3504 name = "sound-dai";
3505 num_codecs = of_count_phandle_with_args(of_node, name,
3506 "#sound-dai-cells");
3507 if (num_codecs <= 0) {
3508 if (num_codecs == -ENOENT)
3509 dev_err(dev, "No 'sound-dai' property\n");
3510 else
3511 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3512 return num_codecs;
3513 }
3514 component = devm_kzalloc(dev,
3515 sizeof *component * num_codecs,
3516 GFP_KERNEL);
3517 if (!component)
3518 return -ENOMEM;
3519 dai_link->codecs = component;
3520 dai_link->num_codecs = num_codecs;
3521
3522 /* Parse the list */
3523 for (index = 0, component = dai_link->codecs;
3524 index < dai_link->num_codecs;
3525 index++, component++) {
3526 ret = of_parse_phandle_with_args(of_node, name,
3527 "#sound-dai-cells",
3528 index, &args);
3529 if (ret)
3530 goto err;
3531 component->of_node = args.np;
3532 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3533 if (ret < 0)
3534 goto err;
3535 }
3536 return 0;
3537err:
3538 for (index = 0, component = dai_link->codecs;
3539 index < dai_link->num_codecs;
3540 index++, component++) {
3541 if (!component->of_node)
3542 break;
3543 of_node_put(component->of_node);
3544 component->of_node = NULL;
3545 }
3546 dai_link->codecs = NULL;
3547 dai_link->num_codecs = 0;
3548 return ret;
3549}
3550EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3551
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003552static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003553{
Mark Brown384c89e2008-12-03 17:34:03 +00003554#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003555 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3556 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003557 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003558 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003559 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003560
Mark Brown8a9dab12011-01-10 22:25:21 +00003561 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003562 &codec_list_fops))
3563 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3564
Mark Brown8a9dab12011-01-10 22:25:21 +00003565 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003566 &dai_list_fops))
3567 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003568
Mark Brown8a9dab12011-01-10 22:25:21 +00003569 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003570 &platform_list_fops))
3571 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003572#endif
3573
Mark Brownfb257892011-04-28 10:57:54 +01003574 snd_soc_util_init();
3575
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003576 return platform_driver_register(&soc_driver);
3577}
Mark Brown4abe8e12010-10-12 17:41:03 +01003578module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003579
Mark Brown7d8c16a2008-11-30 22:11:24 +00003580static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003581{
Mark Brownfb257892011-04-28 10:57:54 +01003582 snd_soc_util_exit();
3583
Mark Brown384c89e2008-12-03 17:34:03 +00003584#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003585 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003586#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003587 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003588}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003589module_exit(snd_soc_exit);
3590
3591/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003592MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003593MODULE_DESCRIPTION("ALSA SoC Core");
3594MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003595MODULE_ALIAS("platform:soc-audio");