blob: 05c4d9564b0b27405fd876973ff1b46a935d4815 [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>
Liam Girdwood345233d2017-01-14 16:13:02 +080037#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000039#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010043#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010044#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020045#include <sound/initval.h>
46
Mark Browna8b1d342010-11-03 18:05:58 -040047#define CREATE_TRACE_POINTS
48#include <trace/events/asoc.h>
49
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000050#define NAME_SIZE 32
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000058static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070060static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000097
98 /* +2 for ': ' and + 1 for '\n' */
99 if (wordsize + regsize + 2 + 1 != len)
100 return -EINVAL;
101
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200102 sprintf(buf, "%.*x: ", wordsize, reg);
103 buf += wordsize + 2;
104
Mark Brown25032c12011-08-01 13:52:48 +0900105 ret = snd_soc_read(codec, reg);
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200106 if (ret < 0)
107 memset(buf, 'X', regsize);
108 else
109 sprintf(buf, "%.*x", regsize, ret);
110 buf[regsize] = '\n';
111 /* no NUL-termination needed */
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000112 return 0;
113}
114
115/* codec register dump */
116static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
117 size_t count, loff_t pos)
118{
119 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000120 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000121 int len;
122 size_t total = 0;
123 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000124
Stephen Warren00b317a2011-04-01 14:50:44 -0600125 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
126 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000127
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000128 len = wordsize + regsize + 2 + 1;
129
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000130 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000131 return 0;
132
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000133 if (codec->driver->reg_cache_step)
134 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000135
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000136 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100137 /* only support larger than PAGE_SIZE bytes debugfs
138 * entries for the default case */
139 if (p >= pos) {
140 if (total + len >= count - 1)
141 break;
142 format_register_str(codec, i, buf + total, len);
143 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100144 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100145 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000146 }
147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000149
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000150 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000151}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152
Mark Brown2624d5f2009-11-03 21:56:13 +0000153static ssize_t codec_reg_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
Mark Brown36ae1a92012-01-06 17:12:45 -0800156 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000157
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000158 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000159}
160
161static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
162
Mark Browndbe21402010-02-12 11:37:24 +0000163static ssize_t pmdown_time_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
165{
Mark Brown36ae1a92012-01-06 17:12:45 -0800166 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000167
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000168 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000169}
170
171static ssize_t pmdown_time_set(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf, size_t count)
174{
Mark Brown36ae1a92012-01-06 17:12:45 -0800175 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700176 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000177
Jingoo Hanb785a492013-07-19 16:24:59 +0900178 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700179 if (ret)
180 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000181
182 return count;
183}
184
185static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
186
Takashi Iwaid29697d2015-01-30 20:16:37 +0100187static struct attribute *soc_dev_attrs[] = {
188 &dev_attr_codec_reg.attr,
189 &dev_attr_pmdown_time.attr,
190 NULL
191};
192
193static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
194 struct attribute *attr, int idx)
195{
196 struct device *dev = kobj_to_dev(kobj);
197 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198
199 if (attr == &dev_attr_pmdown_time.attr)
200 return attr->mode; /* always visible */
201 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
202}
203
204static const struct attribute_group soc_dapm_dev_group = {
205 .attrs = soc_dapm_dev_attrs,
206 .is_visible = soc_dev_attr_is_visible,
207};
208
209static const struct attribute_group soc_dev_roup = {
210 .attrs = soc_dev_attrs,
211 .is_visible = soc_dev_attr_is_visible,
212};
213
214static const struct attribute_group *soc_dev_attr_groups[] = {
215 &soc_dapm_dev_group,
216 &soc_dev_roup,
217 NULL
218};
219
Mark Brown2624d5f2009-11-03 21:56:13 +0000220#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000221static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000222 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000223{
224 ssize_t ret;
225 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000226 char *buf;
227
228 if (*ppos < 0 || !count)
229 return -EINVAL;
230
231 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000232 if (!buf)
233 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000234
235 ret = soc_codec_reg_show(codec, buf, count, *ppos);
236 if (ret >= 0) {
237 if (copy_to_user(user_buf, buf, ret)) {
238 kfree(buf);
239 return -EFAULT;
240 }
241 *ppos += ret;
242 }
243
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 kfree(buf);
245 return ret;
246}
247
248static ssize_t codec_reg_write_file(struct file *file,
249 const char __user *user_buf, size_t count, loff_t *ppos)
250{
251 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700252 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 char *start = buf;
254 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000255 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900256 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000257
258 buf_size = min(count, (sizeof(buf)-1));
259 if (copy_from_user(buf, user_buf, buf_size))
260 return -EFAULT;
261 buf[buf_size] = 0;
262
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 while (*start == ' ')
264 start++;
265 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000266 while (*start == ' ')
267 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900268 ret = kstrtoul(start, 16, &value);
269 if (ret)
270 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000271
272 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030273 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000274
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000275 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000276 return buf_size;
277}
278
279static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700280 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000281 .read = codec_reg_read_file,
282 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200283 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000284};
285
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200286static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100287{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200288 if (!component->card->debugfs_card_root)
289 return;
290
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200291 if (component->debugfs_prefix) {
292 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100293
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200294 name = kasprintf(GFP_KERNEL, "%s:%s",
295 component->debugfs_prefix, component->name);
296 if (name) {
297 component->debugfs_root = debugfs_create_dir(name,
298 component->card->debugfs_card_root);
299 kfree(name);
300 }
301 } else {
302 component->debugfs_root = debugfs_create_dir(component->name,
303 component->card->debugfs_card_root);
304 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100305
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200306 if (!component->debugfs_root) {
307 dev_warn(component->dev,
308 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000309 return;
310 }
311
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200312 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
313 component->debugfs_root);
314
315 if (component->init_debugfs)
316 component->init_debugfs(component);
317}
318
319static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
320{
321 debugfs_remove_recursive(component->debugfs_root);
322}
323
324static void soc_init_codec_debugfs(struct snd_soc_component *component)
325{
326 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
327
Mark Brown2624d5f2009-11-03 21:56:13 +0000328 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200329 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000330 codec, &codec_reg_fops);
331 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200332 dev_warn(codec->dev,
333 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000334}
335
Mark Brownc3c5a192010-09-15 18:15:14 +0100336static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
337 size_t count, loff_t *ppos)
338{
339 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100340 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100341 struct snd_soc_codec *codec;
342
343 if (!buf)
344 return -ENOMEM;
345
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100346 mutex_lock(&client_mutex);
347
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 list_for_each_entry(codec, &codec_list, list) {
349 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200350 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100351 if (len >= 0)
352 ret += len;
353 if (ret > PAGE_SIZE) {
354 ret = PAGE_SIZE;
355 break;
356 }
357 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100358
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100359 mutex_unlock(&client_mutex);
360
Mark Brownc3c5a192010-09-15 18:15:14 +0100361 if (ret >= 0)
362 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
363
364 kfree(buf);
365
366 return ret;
367}
368
369static const struct file_operations codec_list_fops = {
370 .read = codec_list_read_file,
371 .llseek = default_llseek,/* read accesses f_pos */
372};
373
Mark Brownf3208782010-09-15 18:19:07 +0100374static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
375 size_t count, loff_t *ppos)
376{
377 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100378 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100379 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100380 struct snd_soc_dai *dai;
381
382 if (!buf)
383 return -ENOMEM;
384
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100385 mutex_lock(&client_mutex);
386
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100387 list_for_each_entry(component, &component_list, list) {
388 list_for_each_entry(dai, &component->dai_list, list) {
389 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
390 dai->name);
391 if (len >= 0)
392 ret += len;
393 if (ret > PAGE_SIZE) {
394 ret = PAGE_SIZE;
395 break;
396 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100397 }
398 }
Mark Brownf3208782010-09-15 18:19:07 +0100399
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100400 mutex_unlock(&client_mutex);
401
Mark Brown2b194f9d2010-10-13 10:52:16 +0100402 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100403
404 kfree(buf);
405
406 return ret;
407}
408
409static const struct file_operations dai_list_fops = {
410 .read = dai_list_read_file,
411 .llseek = default_llseek,/* read accesses f_pos */
412};
413
Mark Brown19c7ac22010-09-15 18:22:40 +0100414static ssize_t platform_list_read_file(struct file *file,
415 char __user *user_buf,
416 size_t count, loff_t *ppos)
417{
418 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100419 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100420 struct snd_soc_platform *platform;
421
422 if (!buf)
423 return -ENOMEM;
424
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100425 mutex_lock(&client_mutex);
426
Mark Brown2b194f9d2010-10-13 10:52:16 +0100427 list_for_each_entry(platform, &platform_list, list) {
428 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200429 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100430 if (len >= 0)
431 ret += len;
432 if (ret > PAGE_SIZE) {
433 ret = PAGE_SIZE;
434 break;
435 }
436 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100437
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100438 mutex_unlock(&client_mutex);
439
Mark Brown2b194f9d2010-10-13 10:52:16 +0100440 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100441
442 kfree(buf);
443
444 return ret;
445}
446
447static const struct file_operations platform_list_fops = {
448 .read = platform_list_read_file,
449 .llseek = default_llseek,/* read accesses f_pos */
450};
451
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200452static void soc_init_card_debugfs(struct snd_soc_card *card)
453{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200454 if (!snd_soc_debugfs_root)
455 return;
456
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200457 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000458 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200459 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200460 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100461 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200462 return;
463 }
464
465 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
466 card->debugfs_card_root,
467 &card->pop_time);
468 if (!card->debugfs_pop_time)
469 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000470 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200471}
472
473static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
474{
475 debugfs_remove_recursive(card->debugfs_card_root);
476}
477
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200478
479static void snd_soc_debugfs_init(void)
480{
481 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
482 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
483 pr_warn("ASoC: Failed to create debugfs directory\n");
484 snd_soc_debugfs_root = NULL;
485 return;
486 }
487
488 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
489 &codec_list_fops))
490 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
491
492 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
493 &dai_list_fops))
494 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
495
496 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
497 &platform_list_fops))
498 pr_warn("ASoC: Failed to create platform list debugfs file\n");
499}
500
501static void snd_soc_debugfs_exit(void)
502{
503 debugfs_remove_recursive(snd_soc_debugfs_root);
504}
505
Mark Brown2624d5f2009-11-03 21:56:13 +0000506#else
507
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200508#define soc_init_codec_debugfs NULL
509
510static inline void soc_init_component_debugfs(
511 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000512{
513}
514
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200515static inline void soc_cleanup_component_debugfs(
516 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000517{
518}
519
Axel Linb95fccb2010-11-09 17:06:44 +0800520static inline void soc_init_card_debugfs(struct snd_soc_card *card)
521{
522}
523
524static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
525{
526}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200527
528static inline void snd_soc_debugfs_init(void)
529{
530}
531
532static inline void snd_soc_debugfs_exit(void)
533{
534}
535
Mark Brown2624d5f2009-11-03 21:56:13 +0000536#endif
537
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100538struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
539 const char *dai_link, int stream)
540{
Mengdong Lin1a497982015-11-18 02:34:11 -0500541 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100542
Mengdong Lin1a497982015-11-18 02:34:11 -0500543 list_for_each_entry(rtd, &card->rtd_list, list) {
544 if (rtd->dai_link->no_pcm &&
545 !strcmp(rtd->dai_link->name, dai_link))
546 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100547 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000548 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100549 return NULL;
550}
551EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
552
Mengdong Lin1a497982015-11-18 02:34:11 -0500553static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
554 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
555{
556 struct snd_soc_pcm_runtime *rtd;
557
558 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
559 if (!rtd)
560 return NULL;
561
562 rtd->card = card;
563 rtd->dai_link = dai_link;
564 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
565 dai_link->num_codecs,
566 GFP_KERNEL);
567 if (!rtd->codec_dais) {
568 kfree(rtd);
569 return NULL;
570 }
571
572 return rtd;
573}
574
575static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
576{
577 if (rtd && rtd->codec_dais)
578 kfree(rtd->codec_dais);
579 kfree(rtd);
580}
581
582static void soc_add_pcm_runtime(struct snd_soc_card *card,
583 struct snd_soc_pcm_runtime *rtd)
584{
585 list_add_tail(&rtd->list, &card->rtd_list);
586 rtd->num = card->num_rtd;
587 card->num_rtd++;
588}
589
590static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
591{
592 struct snd_soc_pcm_runtime *rtd, *_rtd;
593
594 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
595 list_del(&rtd->list);
596 soc_free_pcm_runtime(rtd);
597 }
598
599 card->num_rtd = 0;
600}
601
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100602struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
603 const char *dai_link)
604{
Mengdong Lin1a497982015-11-18 02:34:11 -0500605 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100606
Mengdong Lin1a497982015-11-18 02:34:11 -0500607 list_for_each_entry(rtd, &card->rtd_list, list) {
608 if (!strcmp(rtd->dai_link->name, dai_link))
609 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100610 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000611 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100612 return NULL;
613}
614EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
615
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100616static void codec2codec_close_delayed_work(struct work_struct *work)
617{
618 /* Currently nothing to do for c2c links
619 * Since c2c links are internal nodes in the DAPM graph and
620 * don't interface with the outside world or application layer
621 * we don't have to do any special handling on close.
622 */
623}
624
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000625#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000627int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000629 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000630 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500631 struct snd_soc_pcm_runtime *rtd;
632 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200634 /* If the card is not initialized yet there is nothing to do */
635 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200636 return 0;
637
Andy Green6ed25972008-06-13 16:24:05 +0100638 /* Due to the resume being scheduled into a workqueue we could
639 * suspend before that's finished - wait for it to complete.
640 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 snd_power_lock(card->snd_card);
642 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
643 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100644
645 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100647
Mark Browna00f90f2010-12-02 16:24:24 +0000648 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500649 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100650
Mengdong Lin1a497982015-11-18 02:34:11 -0500651 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100652 continue;
653
Mengdong Lin1a497982015-11-18 02:34:11 -0500654 for (i = 0; i < rtd->num_codecs; i++) {
655 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200656 struct snd_soc_dai_driver *drv = dai->driver;
657
658 if (drv->ops->digital_mute && dai->playback_active)
659 drv->ops->digital_mute(dai, 1);
660 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661 }
662
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100663 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 list_for_each_entry(rtd, &card->rtd_list, list) {
665 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100666 continue;
667
Mengdong Lin1a497982015-11-18 02:34:11 -0500668 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100669 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100670
Mark Brown87506542008-11-18 20:50:34 +0000671 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000672 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
Mengdong Lin1a497982015-11-18 02:34:11 -0500674 list_for_each_entry(rtd, &card->rtd_list, list) {
675 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100676
Mengdong Lin1a497982015-11-18 02:34:11 -0500677 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100678 continue;
679
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100680 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100682 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200684 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500685 list_for_each_entry(rtd, &card->rtd_list, list)
686 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100687
Mengdong Lin1a497982015-11-18 02:34:11 -0500688 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689
Mengdong Lin1a497982015-11-18 02:34:11 -0500690 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100691 continue;
692
Mengdong Lin1a497982015-11-18 02:34:11 -0500693 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800694 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800695 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696
Mengdong Lin1a497982015-11-18 02:34:11 -0500697 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800698 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800699 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 }
701
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200702 /* Recheck all endpoints too, their state is affected by suspend */
703 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700704 snd_soc_dapm_sync(&card->dapm);
705
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000706 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000707 list_for_each_entry(component, &card->component_dev_list, card_list) {
708 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000709
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000710 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000712 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200713 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000715 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000716 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000717 * bias off then being in STANDBY
718 * means it's doing something,
719 * otherwise fall through.
720 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200721 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000722 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200723 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000724 break;
725 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200726
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 case SND_SOC_BIAS_OFF:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000728 if (component->suspend)
729 component->suspend(component);
730 component->suspended = 1;
731 if (component->regmap)
732 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800733 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000734 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 break;
736 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000737 dev_dbg(component->dev,
738 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 break;
740 }
741 }
742 }
743
Mengdong Lin1a497982015-11-18 02:34:11 -0500744 list_for_each_entry(rtd, &card->rtd_list, list) {
745 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746
Mengdong Lin1a497982015-11-18 02:34:11 -0500747 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 continue;
749
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100750 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800752
753 /* deactivate pins to sleep state */
754 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755 }
756
Mark Brown87506542008-11-18 20:50:34 +0000757 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000758 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200759
760 return 0;
761}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200763
Andy Green6ed25972008-06-13 16:24:05 +0100764/* deferred resume work, so resume can complete before we finished
765 * setting our codec back up, which can be very slow on I2C
766 */
767static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 struct snd_soc_card *card =
770 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500771 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000772 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500773 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774
Andy Green6ed25972008-06-13 16:24:05 +0100775 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
776 * so userspace apps are blocked from touching us
777 */
778
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000779 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100780
Mark Brown9949788b2010-05-07 20:24:05 +0100781 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100783
Mark Brown87506542008-11-18 20:50:34 +0000784 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000785 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100787 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500788 list_for_each_entry(rtd, &card->rtd_list, list) {
789 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100790
Mengdong Lin1a497982015-11-18 02:34:11 -0500791 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100792 continue;
793
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100794 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000795 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796 }
797
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000798 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000799 if (component->suspended) {
800 if (component->resume)
801 component->resume(component);
802 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100803 }
804 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805
Mengdong Lin1a497982015-11-18 02:34:11 -0500806 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100807
Mengdong Lin1a497982015-11-18 02:34:11 -0500808 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100809 continue;
810
Mengdong Lin1a497982015-11-18 02:34:11 -0500811 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000812 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800813 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814
Mengdong Lin1a497982015-11-18 02:34:11 -0500815 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000816 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800817 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818 }
819
Mark Brown3ff3f642008-05-19 12:32:25 +0200820 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500821 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100822
Mengdong Lin1a497982015-11-18 02:34:11 -0500823 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100824 continue;
825
Mengdong Lin1a497982015-11-18 02:34:11 -0500826 for (i = 0; i < rtd->num_codecs; i++) {
827 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200828 struct snd_soc_dai_driver *drv = dai->driver;
829
830 if (drv->ops->digital_mute && dai->playback_active)
831 drv->ops->digital_mute(dai, 0);
832 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200833 }
834
Mengdong Lin1a497982015-11-18 02:34:11 -0500835 list_for_each_entry(rtd, &card->rtd_list, list) {
836 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100837
Mengdong Lin1a497982015-11-18 02:34:11 -0500838 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100839 continue;
840
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100841 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000842 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 }
844
Mark Brown87506542008-11-18 20:50:34 +0000845 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000846 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000848 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100849
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200850 /* Recheck all endpoints too, their state is affected by suspend */
851 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700852 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530853
854 /* userspace can access us now we are back as we were before */
855 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100856}
857
858/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000859int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100860{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000861 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100862 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500863 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200864
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200865 /* If the card is not initialized yet there is nothing to do */
866 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800867 return 0;
868
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800869 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500870 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200871 struct snd_soc_dai **codec_dais = rtd->codec_dais;
872 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
873 int j;
874
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800875 if (cpu_dai->active)
876 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200877
878 for (j = 0; j < rtd->num_codecs; j++) {
879 struct snd_soc_dai *codec_dai = codec_dais[j];
880 if (codec_dai->active)
881 pinctrl_pm_select_default_state(codec_dai->dev);
882 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800883 }
884
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100885 /*
886 * DAIs that also act as the control bus master might have other drivers
887 * hanging off them so need to resume immediately. Other drivers don't
888 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100889 * due to I/O costs and anti-pop so handle them out of line.
890 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500891 list_for_each_entry(rtd, &card->rtd_list, list) {
892 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100893 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600894 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100895 if (bus_control) {
896 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600897 soc_resume_deferred(&card->deferred_resume_work);
898 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000899 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600900 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000901 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100902 }
Andy Green6ed25972008-06-13 16:24:05 +0100903
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 return 0;
905}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000906EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000908#define snd_soc_suspend NULL
909#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200910#endif
911
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100912static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800913};
914
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200915static struct snd_soc_component *soc_find_component(
916 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100917{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200918 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100919
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100920 lockdep_assert_held(&client_mutex);
921
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200922 list_for_each_entry(component, &component_list, list) {
923 if (of_node) {
924 if (component->dev->of_node == of_node)
925 return component;
926 } else if (strcmp(component->name, name) == 0) {
927 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100928 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100929 }
930
931 return NULL;
932}
933
Mengdong Linfbb88b52016-04-22 12:25:33 +0800934/**
935 * snd_soc_find_dai - Find a registered DAI
936 *
937 * @dlc: name of the DAI and optional component info to match
938 *
939 * This function will search all regsitered components and their DAIs to
940 * find the DAI of the same name. The component's of_node and name
941 * should also match if being specified.
942 *
943 * Return: pointer of DAI, or NULL if not found.
944 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800945struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200946 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100947{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200948 struct snd_soc_component *component;
949 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300950 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100951
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100952 lockdep_assert_held(&client_mutex);
953
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200954 /* Find CPU DAI from registered DAIs*/
955 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300956 component_of_node = component->dev->of_node;
957 if (!component_of_node && component->dev->parent)
958 component_of_node = component->dev->parent->of_node;
959
960 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200961 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100962 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200963 continue;
964 list_for_each_entry(dai, &component->dai_list, list) {
965 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100966 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100967
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200968 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100969 }
970 }
971
972 return NULL;
973}
Mengdong Lin305e9022016-04-19 13:12:25 +0800974EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100975
Mengdong Lin17fb17552016-11-03 01:04:12 +0800976
977/**
978 * snd_soc_find_dai_link - Find a DAI link
979 *
980 * @card: soc card
981 * @id: DAI link ID to match
982 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000983 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800984 *
985 * This function will search all existing DAI links of the soc card to
986 * find the link of the same ID. Since DAI links may not have their
987 * unique ID, so name and stream name should also match if being
988 * specified.
989 *
990 * Return: pointer of DAI link, or NULL if not found.
991 */
992struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
993 int id, const char *name,
994 const char *stream_name)
995{
996 struct snd_soc_dai_link *link, *_link;
997
998 lockdep_assert_held(&client_mutex);
999
1000 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1001 if (link->id != id)
1002 continue;
1003
1004 if (name && (!link->name || strcmp(name, link->name)))
1005 continue;
1006
1007 if (stream_name && (!link->stream_name
1008 || strcmp(stream_name, link->stream_name)))
1009 continue;
1010
1011 return link;
1012 }
1013
1014 return NULL;
1015}
1016EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1017
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001018static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1019 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001020{
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001021 struct snd_soc_pcm_runtime *rtd;
1022
1023 list_for_each_entry(rtd, &card->rtd_list, list) {
1024 if (rtd->dai_link == dai_link)
1025 return true;
1026 }
1027
1028 return false;
1029}
1030
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001031static int soc_bind_dai_link(struct snd_soc_card *card,
1032 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001033{
Mengdong Lin1a497982015-11-18 02:34:11 -05001034 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001035 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001036 struct snd_soc_dai_link_component cpu_dai_component;
Mengdong Lin1a497982015-11-18 02:34:11 -05001037 struct snd_soc_dai **codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +00001038 struct snd_soc_platform *platform;
Charles Keepax06859fc2016-11-07 13:03:17 +00001039 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +01001040 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001041 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001042
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001043 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001044
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001045 if (soc_is_dai_link_bound(card, dai_link)) {
1046 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1047 dai_link->name);
1048 return 0;
1049 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001050
Sudip Mukherjee513cb3112016-02-22 14:14:31 +05301051 rtd = soc_new_pcm_runtime(card, dai_link);
1052 if (!rtd)
1053 return -ENOMEM;
1054
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001055 cpu_dai_component.name = dai_link->cpu_name;
1056 cpu_dai_component.of_node = dai_link->cpu_of_node;
1057 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1058 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001059 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001060 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001061 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001062 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001063 }
1064
Benoit Cousson88bd8702014-07-08 23:19:34 +02001065 rtd->num_codecs = dai_link->num_codecs;
1066
1067 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -05001068 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001069 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001070 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001071 if (!codec_dais[i]) {
1072 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1073 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001074 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001075 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001076 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001077
Benoit Cousson88bd8702014-07-08 23:19:34 +02001078 /* Single codec links expect codec and codec_dai in runtime data */
1079 rtd->codec_dai = codec_dais[0];
1080 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001081
Mark Brown848dd8b2011-04-27 18:16:32 +01001082 /* if there's no platform we match on the empty platform */
1083 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -07001084 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +01001085 platform_name = "snd-soc-dummy";
1086
Mark Brownb19e6e72012-03-14 21:18:39 +00001087 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001088 list_for_each_entry(platform, &platform_list, list) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001089 platform_of_node = platform->dev->of_node;
1090 if (!platform_of_node && platform->dev->parent->of_node)
1091 platform_of_node = platform->dev->parent->of_node;
1092
Stephen Warren5a504962011-12-21 10:40:59 -07001093 if (dai_link->platform_of_node) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001094 if (platform_of_node != dai_link->platform_of_node)
Stephen Warren5a504962011-12-21 10:40:59 -07001095 continue;
1096 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001097 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -07001098 continue;
1099 }
Stephen Warren2610ab72011-12-07 13:58:27 -07001100
1101 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001103 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001104 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 dai_link->platform_name);
Charles Keepax70fcad42016-08-11 14:39:00 +01001106 goto _err_defer;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001108
Mengdong Lin1a497982015-11-18 02:34:11 -05001109 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +00001110 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001111
1112_err_defer:
1113 soc_free_pcm_runtime(rtd);
1114 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001115}
1116
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001117static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001118{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001119 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001120 return;
Stephen Warrend12cd192012-06-08 12:34:22 -06001121
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001122 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001123
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001124 if (component->remove)
1125 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001126
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001127 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001128
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001129 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001130 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001131 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -06001132}
1133
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001134static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001135{
1136 int err;
1137
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001138 if (dai && dai->probed &&
1139 dai->driver->remove_order == order) {
1140 if (dai->driver->remove) {
1141 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001142 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001143 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001144 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001145 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001146 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001147 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001148 }
1149}
1150
Mengdong Lin1a497982015-11-18 02:34:11 -05001151static void soc_remove_link_dais(struct snd_soc_card *card,
1152 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001154 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001155
1156 /* unregister the rtd device */
1157 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001158 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159 rtd->dev_registered = 0;
1160 }
1161
1162 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001163 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001164 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001165
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001166 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167}
1168
Mengdong Lin1a497982015-11-18 02:34:11 -05001169static void soc_remove_link_components(struct snd_soc_card *card,
1170 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001171{
Stephen Warren62ae68f2012-06-08 12:34:23 -06001172 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001173 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001174 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001175 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001176
1177 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001178 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001179 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001180
1181 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001182 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001183 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001184 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001185 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001186 }
1187
1188 /* remove any CPU-side CODEC */
1189 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001190 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001191 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001192 }
1193}
1194
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001195static void soc_remove_dai_links(struct snd_soc_card *card)
1196{
Mengdong Lin1a497982015-11-18 02:34:11 -05001197 int order;
1198 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001199 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001200
Liam Girdwood0168bf02011-06-07 16:08:05 +01001201 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1202 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001203 list_for_each_entry(rtd, &card->rtd_list, list)
1204 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001205 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001206
1207 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1208 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001209 list_for_each_entry(rtd, &card->rtd_list, list)
1210 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001211 }
1212
Mengdong Linf8f80362015-12-02 14:11:22 +08001213 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1214 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1215 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1216 link->name);
1217
1218 list_del(&link->list);
1219 card->num_dai_links--;
1220 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001221}
1222
Mengdong Lin923c5e612015-11-23 11:01:49 -05001223static int snd_soc_init_multicodec(struct snd_soc_card *card,
1224 struct snd_soc_dai_link *dai_link)
1225{
1226 /* Legacy codec/codec_dai link is a single entry in multicodec */
1227 if (dai_link->codec_name || dai_link->codec_of_node ||
1228 dai_link->codec_dai_name) {
1229 dai_link->num_codecs = 1;
1230
1231 dai_link->codecs = devm_kzalloc(card->dev,
1232 sizeof(struct snd_soc_dai_link_component),
1233 GFP_KERNEL);
1234 if (!dai_link->codecs)
1235 return -ENOMEM;
1236
1237 dai_link->codecs[0].name = dai_link->codec_name;
1238 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1239 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1240 }
1241
1242 if (!dai_link->codecs) {
1243 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1244 return -EINVAL;
1245 }
1246
1247 return 0;
1248}
1249
1250static int soc_init_dai_link(struct snd_soc_card *card,
1251 struct snd_soc_dai_link *link)
1252{
1253 int i, ret;
1254
1255 ret = snd_soc_init_multicodec(card, link);
1256 if (ret) {
1257 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1258 return ret;
1259 }
1260
1261 for (i = 0; i < link->num_codecs; i++) {
1262 /*
1263 * Codec must be specified by 1 of name or OF node,
1264 * not both or neither.
1265 */
1266 if (!!link->codecs[i].name ==
1267 !!link->codecs[i].of_node) {
1268 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1269 link->name);
1270 return -EINVAL;
1271 }
1272 /* Codec DAI name must be specified */
1273 if (!link->codecs[i].dai_name) {
1274 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1275 link->name);
1276 return -EINVAL;
1277 }
1278 }
1279
1280 /*
1281 * Platform may be specified by either name or OF node, but
1282 * can be left unspecified, and a dummy platform will be used.
1283 */
1284 if (link->platform_name && link->platform_of_node) {
1285 dev_err(card->dev,
1286 "ASoC: Both platform name/of_node are set for %s\n",
1287 link->name);
1288 return -EINVAL;
1289 }
1290
1291 /*
1292 * CPU device may be specified by either name or OF node, but
1293 * can be left unspecified, and will be matched based on DAI
1294 * name alone..
1295 */
1296 if (link->cpu_name && link->cpu_of_node) {
1297 dev_err(card->dev,
1298 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1299 link->name);
1300 return -EINVAL;
1301 }
1302 /*
1303 * At least one of CPU DAI name or CPU device name/node must be
1304 * specified
1305 */
1306 if (!link->cpu_dai_name &&
1307 !(link->cpu_name || link->cpu_of_node)) {
1308 dev_err(card->dev,
1309 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1310 link->name);
1311 return -EINVAL;
1312 }
1313
1314 return 0;
1315}
1316
Mengdong Linf8f80362015-12-02 14:11:22 +08001317/**
1318 * snd_soc_add_dai_link - Add a DAI link dynamically
1319 * @card: The ASoC card to which the DAI link is added
1320 * @dai_link: The new DAI link to add
1321 *
1322 * This function adds a DAI link to the ASoC card's link list.
1323 *
1324 * Note: Topology can use this API to add DAI links when probing the
1325 * topology component. And machine drivers can still define static
1326 * DAI links in dai_link array.
1327 */
1328int snd_soc_add_dai_link(struct snd_soc_card *card,
1329 struct snd_soc_dai_link *dai_link)
1330{
1331 if (dai_link->dobj.type
1332 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1333 dev_err(card->dev, "Invalid dai link type %d\n",
1334 dai_link->dobj.type);
1335 return -EINVAL;
1336 }
1337
1338 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001339 /* Notify the machine driver for extra initialization
1340 * on the link created by topology.
1341 */
1342 if (dai_link->dobj.type && card->add_dai_link)
1343 card->add_dai_link(card, dai_link);
1344
Mengdong Linf8f80362015-12-02 14:11:22 +08001345 list_add_tail(&dai_link->list, &card->dai_link_list);
1346 card->num_dai_links++;
1347
1348 return 0;
1349}
1350EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1351
1352/**
1353 * snd_soc_remove_dai_link - Remove a DAI link from the list
1354 * @card: The ASoC card that owns the link
1355 * @dai_link: The DAI link to remove
1356 *
1357 * This function removes a DAI link from the ASoC card's link list.
1358 *
1359 * For DAI links previously added by topology, topology should
1360 * remove them by using the dobj embedded in the link.
1361 */
1362void snd_soc_remove_dai_link(struct snd_soc_card *card,
1363 struct snd_soc_dai_link *dai_link)
1364{
1365 struct snd_soc_dai_link *link, *_link;
1366
1367 if (dai_link->dobj.type
1368 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1369 dev_err(card->dev, "Invalid dai link type %d\n",
1370 dai_link->dobj.type);
1371 return;
1372 }
1373
1374 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001375 /* Notify the machine driver for extra destruction
1376 * on the link created by topology.
1377 */
1378 if (dai_link->dobj.type && card->remove_dai_link)
1379 card->remove_dai_link(card, dai_link);
1380
Mengdong Linf8f80362015-12-02 14:11:22 +08001381 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1382 if (link == dai_link) {
1383 list_del(&link->list);
1384 card->num_dai_links--;
1385 return;
1386 }
1387 }
1388}
1389EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1390
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001391static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001392 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001393{
1394 int i;
1395
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001396 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001397 return;
1398
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001399 for (i = 0; i < card->num_configs; i++) {
1400 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001401 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001402 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001403 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001404 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001405 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001406 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001407 }
1408}
1409
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001410static int soc_probe_component(struct snd_soc_card *card,
1411 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001412{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001413 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001414 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001415 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001416
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001417 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001418 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001419
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001420 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001421 if (component->card != card) {
1422 dev_err(component->dev,
1423 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1424 card->name, component->card->name);
1425 return -ENODEV;
1426 }
1427 return 0;
1428 }
1429
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001430 if (!try_module_get(component->dev->driver->owner))
1431 return -ENODEV;
1432
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001433 component->card = card;
1434 dapm->card = card;
1435 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001436
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001437 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001438
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001439 if (component->dapm_widgets) {
1440 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1441 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001442
1443 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001444 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001445 "Failed to create new controls %d\n", ret);
1446 goto err_probe;
1447 }
1448 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001449
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001450 list_for_each_entry(dai, &component->dai_list, list) {
1451 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001452 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001453 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001454 "Failed to create DAI widgets %d\n", ret);
1455 goto err_probe;
1456 }
1457 }
Mark Brown888df392012-02-16 19:37:51 -08001458
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001459 if (component->probe) {
1460 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001461 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001462 dev_err(component->dev,
1463 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001464 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001465 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001466
1467 WARN(dapm->idle_bias_off &&
1468 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001469 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001470 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001471 }
1472
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001473 /* machine specific init */
1474 if (component->init) {
1475 ret = component->init(component);
1476 if (ret < 0) {
1477 dev_err(component->dev,
1478 "Failed to do machine specific init %d\n", ret);
1479 goto err_probe;
1480 }
1481 }
1482
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001483 if (component->controls)
1484 snd_soc_add_component_controls(component, component->controls,
1485 component->num_controls);
1486 if (component->dapm_routes)
1487 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1488 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001489
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001490 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001491 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001492
Jarkko Nikula70d293312011-01-27 16:24:22 +02001493 return 0;
1494
1495err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001496 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001497 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001498 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001499
1500 return ret;
1501}
1502
Mark Brown36ae1a92012-01-06 17:12:45 -08001503static void rtd_release(struct device *dev)
1504{
1505 kfree(dev);
1506}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001507
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001508static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1509 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001510{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001511 int ret = 0;
1512
Jarkko Nikula589c3562010-12-06 16:27:07 +02001513 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001514 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1515 if (!rtd->dev)
1516 return -ENOMEM;
1517 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001518 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001519 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001520 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001521 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001522 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001523 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001524 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1525 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1526 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1527 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001528 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001529 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001530 /* calling put_device() here to free the rtd->dev */
1531 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001532 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001534 return ret;
1535 }
1536 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001537 return 0;
1538}
1539
Mengdong Lin1a497982015-11-18 02:34:11 -05001540static int soc_probe_link_components(struct snd_soc_card *card,
1541 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001542 int order)
1543{
Stephen Warren62ae68f2012-06-08 12:34:23 -06001544 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001545 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001546 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001547
1548 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001549 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001550 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001551 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001552 if (ret < 0)
1553 return ret;
1554 }
1555
Benoit Cousson88bd8702014-07-08 23:19:34 +02001556 /* probe the CODEC-side components */
1557 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001558 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001559 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001560 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001561 if (ret < 0)
1562 return ret;
1563 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001564 }
1565
1566 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001567 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001568 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001569 if (ret < 0)
1570 return ret;
1571 }
1572
1573 return 0;
1574}
1575
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001576static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001577{
1578 int ret;
1579
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001580 if (!dai->probed && dai->driver->probe_order == order) {
1581 if (dai->driver->probe) {
1582 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001583 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001584 dev_err(dai->dev,
1585 "ASoC: failed to probe DAI %s: %d\n",
1586 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001587 return ret;
1588 }
1589 }
1590
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001591 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001592 }
1593
1594 return 0;
1595}
1596
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001597static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1598 struct snd_soc_pcm_runtime *rtd)
1599{
1600 int i, ret = 0;
1601
1602 for (i = 0; i < num_dais; ++i) {
1603 struct snd_soc_dai_driver *drv = dais[i]->driver;
1604
1605 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1606 ret = drv->pcm_new(rtd, dais[i]);
1607 if (ret < 0) {
1608 dev_err(dais[i]->dev,
1609 "ASoC: Failed to bind %s with pcm device\n",
1610 dais[i]->name);
1611 return ret;
1612 }
1613 }
1614
1615 return 0;
1616}
1617
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001618static int soc_link_dai_widgets(struct snd_soc_card *card,
1619 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001620 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001621{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001622 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1623 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301624 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001625 int ret;
1626
Benoit Cousson88bd8702014-07-08 23:19:34 +02001627 if (rtd->num_codecs > 1)
1628 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1629
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001630 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301631 sink = codec_dai->playback_widget;
1632 source = cpu_dai->capture_widget;
1633 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001634 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301635 dai_link->num_params,
1636 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001637 if (ret != 0) {
1638 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301639 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001640 return ret;
1641 }
1642 }
1643
Vinod Koul3de7c422015-11-09 23:19:58 +05301644 sink = cpu_dai->playback_widget;
1645 source = codec_dai->capture_widget;
1646 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001647 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301648 dai_link->num_params,
1649 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001650 if (ret != 0) {
1651 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301652 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001653 return ret;
1654 }
1655 }
1656
1657 return 0;
1658}
1659
Mengdong Lin1a497982015-11-18 02:34:11 -05001660static int soc_probe_link_dais(struct snd_soc_card *card,
1661 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662{
Mengdong Lin1a497982015-11-18 02:34:11 -05001663 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001664 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001665 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001667 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001668 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001670 /* set default power off timeout */
1671 rtd->pmdown_time = pmdown_time;
1672
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001673 ret = soc_probe_dai(cpu_dai, order);
1674 if (ret)
1675 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001678 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001679 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001680 if (ret)
1681 return ret;
1682 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683
Liam Girdwood0168bf02011-06-07 16:08:05 +01001684 /* complete DAI probe during last probe */
1685 if (order != SND_SOC_COMP_ORDER_LAST)
1686 return 0;
1687
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001688 /* do machine specific initialization */
1689 if (dai_link->init) {
1690 ret = dai_link->init(rtd);
1691 if (ret < 0) {
1692 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1693 dai_link->name, ret);
1694 return ret;
1695 }
1696 }
1697
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001698 if (dai_link->dai_fmt)
1699 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1700
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001701 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001702 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001705#ifdef CONFIG_DEBUG_FS
1706 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001707 if (dai_link->dynamic)
1708 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001709#endif
1710
Jie Yang6f0c4222015-10-13 23:41:00 +08001711 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301712 /*create compress_device"*/
Mengdong Lin1a497982015-11-18 02:34:11 -05001713 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
Mark Brownc74184e2012-04-04 22:12:09 +01001714 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001715 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301716 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001717 return ret;
1718 }
1719 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301720
1721 if (!dai_link->params) {
1722 /* create the pcm */
Mengdong Lin1a497982015-11-18 02:34:11 -05001723 ret = soc_new_pcm(rtd, rtd->num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301724 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001725 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301726 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001727 return ret;
1728 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001729 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1730 if (ret < 0)
1731 return ret;
1732 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1733 rtd->num_codecs, rtd);
1734 if (ret < 0)
1735 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301736 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001737 INIT_DELAYED_WORK(&rtd->delayed_work,
1738 codec2codec_close_delayed_work);
1739
Namarta Kohli1245b702012-08-16 17:10:41 +05301740 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001741 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001742 if (ret)
1743 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001744 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 }
1746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747 return 0;
1748}
1749
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001750static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001751{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001752 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001753 struct snd_soc_component *component;
1754 const char *name;
1755 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001756
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001757 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1758 /* codecs, usually analog devices */
1759 name = aux_dev->codec_name;
1760 codec_of_node = aux_dev->codec_of_node;
1761 component = soc_find_component(codec_of_node, name);
1762 if (!component) {
1763 if (codec_of_node)
1764 name = of_node_full_name(codec_of_node);
1765 goto err_defer;
1766 }
1767 } else if (aux_dev->name) {
1768 /* generic components */
1769 name = aux_dev->name;
1770 component = soc_find_component(NULL, name);
1771 if (!component)
1772 goto err_defer;
1773 } else {
1774 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1775 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001776 }
1777
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001778 component->init = aux_dev->init;
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001779 component->auxiliary = 1;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001780 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001781
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001782 return 0;
1783
1784err_defer:
1785 dev_err(card->dev, "ASoC: %s not registered\n", name);
1786 return -EPROBE_DEFER;
1787}
1788
1789static int soc_probe_aux_devices(struct snd_soc_card *card)
1790{
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001791 struct snd_soc_component *comp, *tmp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001792 int order;
1793 int ret;
1794
1795 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1796 order++) {
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001797 list_for_each_entry_safe(comp, tmp, &card->aux_comp_list,
1798 card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001799 if (comp->driver->probe_order == order) {
1800 ret = soc_probe_component(card, comp);
1801 if (ret < 0) {
1802 dev_err(card->dev,
1803 "ASoC: failed to probe aux component %s %d\n",
1804 comp->name, ret);
1805 return ret;
1806 }
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001807 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001808 }
1809 }
1810 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001811
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001812 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001813}
1814
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001815static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001816{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001817 struct snd_soc_component *comp, *_comp;
1818 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001819
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001820 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1821 order++) {
1822 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001823 &card->component_dev_list, card_list) {
1824
1825 if (!comp->auxiliary)
1826 continue;
1827
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001828 if (comp->driver->remove_order == order) {
1829 soc_remove_component(comp);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001830 comp->auxiliary = 0;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001831 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001832 }
1833 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001834}
1835
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001836static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001837{
1838 int ret;
1839
1840 if (codec->cache_init)
1841 return 0;
1842
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001843 ret = snd_soc_cache_init(codec);
1844 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001845 dev_err(codec->dev,
1846 "ASoC: Failed to set cache compression type: %d\n",
1847 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001848 return ret;
1849 }
1850 codec->cache_init = 1;
1851 return 0;
1852}
1853
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001854/**
1855 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1856 * @rtd: The runtime for which the DAI link format should be changed
1857 * @dai_fmt: The new DAI link format
1858 *
1859 * This function updates the DAI link format for all DAIs connected to the DAI
1860 * link for the specified runtime.
1861 *
1862 * Note: For setups with a static format set the dai_fmt field in the
1863 * corresponding snd_dai_link struct instead of using this function.
1864 *
1865 * Returns 0 on success, otherwise a negative error code.
1866 */
1867int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1868 unsigned int dai_fmt)
1869{
1870 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1871 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1872 unsigned int i;
1873 int ret;
1874
1875 for (i = 0; i < rtd->num_codecs; i++) {
1876 struct snd_soc_dai *codec_dai = codec_dais[i];
1877
1878 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1879 if (ret != 0 && ret != -ENOTSUPP) {
1880 dev_warn(codec_dai->dev,
1881 "ASoC: Failed to set DAI format: %d\n", ret);
1882 return ret;
1883 }
1884 }
1885
1886 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1887 if (cpu_dai->codec) {
1888 unsigned int inv_dai_fmt;
1889
1890 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1891 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1892 case SND_SOC_DAIFMT_CBM_CFM:
1893 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1894 break;
1895 case SND_SOC_DAIFMT_CBM_CFS:
1896 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1897 break;
1898 case SND_SOC_DAIFMT_CBS_CFM:
1899 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1900 break;
1901 case SND_SOC_DAIFMT_CBS_CFS:
1902 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1903 break;
1904 }
1905
1906 dai_fmt = inv_dai_fmt;
1907 }
1908
1909 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1910 if (ret != 0 && ret != -ENOTSUPP) {
1911 dev_warn(cpu_dai->dev,
1912 "ASoC: Failed to set DAI format: %d\n", ret);
1913 return ret;
1914 }
1915
1916 return 0;
1917}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001918EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001919
Liam Girdwood345233d2017-01-14 16:13:02 +08001920
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001921#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001922/* Trim special characters, and replace '-' with '_' since '-' is used to
1923 * separate different DMI fields in the card long name. Only number and
1924 * alphabet characters and a few separator characters are kept.
1925 */
1926static void cleanup_dmi_name(char *name)
1927{
1928 int i, j = 0;
1929
1930 for (i = 0; name[i]; i++) {
1931 if (isalnum(name[i]) || (name[i] == '.')
1932 || (name[i] == '_'))
1933 name[j++] = name[i];
1934 else if (name[i] == '-')
1935 name[j++] = '_';
1936 }
1937
1938 name[j] = '\0';
1939}
1940
1941/**
1942 * snd_soc_set_dmi_name() - Register DMI names to card
1943 * @card: The card to register DMI names
1944 * @flavour: The flavour "differentiator" for the card amongst its peers.
1945 *
1946 * An Intel machine driver may be used by many different devices but are
1947 * difficult for userspace to differentiate, since machine drivers ususally
1948 * use their own name as the card short name and leave the card long name
1949 * blank. To differentiate such devices and fix bugs due to lack of
1950 * device-specific configurations, this function allows DMI info to be used
1951 * as the sound card long name, in the format of
1952 * "vendor-product-version-board"
1953 * (Character '-' is used to separate different DMI fields here).
1954 * This will help the user space to load the device-specific Use Case Manager
1955 * (UCM) configurations for the card.
1956 *
1957 * Possible card long names may be:
1958 * DellInc.-XPS139343-01-0310JH
1959 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1960 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1961 *
1962 * This function also supports flavoring the card longname to provide
1963 * the extra differentiation, like "vendor-product-version-board-flavor".
1964 *
1965 * We only keep number and alphabet characters and a few separator characters
1966 * in the card long name since UCM in the user space uses the card long names
1967 * as card configuration directory names and AudoConf cannot support special
1968 * charactors like SPACE.
1969 *
1970 * Returns 0 on success, otherwise a negative error code.
1971 */
1972int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1973{
1974 const char *vendor, *product, *product_version, *board;
1975 size_t longname_buf_size = sizeof(card->snd_card->longname);
1976 size_t len;
1977
1978 if (card->long_name)
1979 return 0; /* long name already set by driver or from DMI */
1980
1981 /* make up dmi long name as: vendor.product.version.board */
1982 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1983 if (!vendor) {
1984 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1985 return 0;
1986 }
1987
1988 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1989 "%s", vendor);
1990 cleanup_dmi_name(card->dmi_longname);
1991
1992 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1993 if (product) {
1994 len = strlen(card->dmi_longname);
1995 snprintf(card->dmi_longname + len,
1996 longname_buf_size - len,
1997 "-%s", product);
1998
1999 len++; /* skip the separator "-" */
2000 if (len < longname_buf_size)
2001 cleanup_dmi_name(card->dmi_longname + len);
2002
2003 /* some vendors like Lenovo may only put a self-explanatory
2004 * name in the product version field
2005 */
2006 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2007 if (product_version) {
2008 len = strlen(card->dmi_longname);
2009 snprintf(card->dmi_longname + len,
2010 longname_buf_size - len,
2011 "-%s", product_version);
2012
2013 len++;
2014 if (len < longname_buf_size)
2015 cleanup_dmi_name(card->dmi_longname + len);
2016 }
2017 }
2018
2019 board = dmi_get_system_info(DMI_BOARD_NAME);
2020 if (board) {
2021 len = strlen(card->dmi_longname);
2022 snprintf(card->dmi_longname + len,
2023 longname_buf_size - len,
2024 "-%s", board);
2025
2026 len++;
2027 if (len < longname_buf_size)
2028 cleanup_dmi_name(card->dmi_longname + len);
2029 } else if (!product) {
2030 /* fall back to using legacy name */
2031 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2032 return 0;
2033 }
2034
2035 /* Add flavour to dmi long name */
2036 if (flavour) {
2037 len = strlen(card->dmi_longname);
2038 snprintf(card->dmi_longname + len,
2039 longname_buf_size - len,
2040 "-%s", flavour);
2041
2042 len++;
2043 if (len < longname_buf_size)
2044 cleanup_dmi_name(card->dmi_longname + len);
2045 }
2046
2047 /* set the card long name */
2048 card->long_name = card->dmi_longname;
2049
2050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02002053#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08002054
Mark Brownb19e6e72012-03-14 21:18:39 +00002055static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002057 struct snd_soc_codec *codec;
Mengdong Lin1a497982015-11-18 02:34:11 -05002058 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002059 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002060 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002061
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002062 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002063 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002064
Mark Brownb19e6e72012-03-14 21:18:39 +00002065 /* bind DAIs */
2066 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002067 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002068 if (ret != 0)
2069 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 }
2071
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002072 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002073 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002074 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002075 if (ret != 0)
2076 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002077 }
2078
Mengdong Linf8f80362015-12-02 14:11:22 +08002079 /* add predefined DAI links to the list */
2080 for (i = 0; i < card->num_links; i++)
2081 snd_soc_add_dai_link(card, card->dai_link+i);
2082
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002083 /* initialize the register cache for each available codec */
2084 list_for_each_entry(codec, &codec_list, list) {
2085 if (codec->cache_init)
2086 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02002087 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00002088 if (ret < 0)
2089 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002090 }
2091
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002092 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002093 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002094 card->owner, 0, &card->snd_card);
2095 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002096 dev_err(card->dev,
2097 "ASoC: can't create sound card for card %s: %d\n",
2098 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002099 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002100 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002101
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002102 soc_init_card_debugfs(card);
2103
Mark Browne37a4972011-03-02 18:21:57 +00002104 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2105 card->dapm.dev = card->dev;
2106 card->dapm.card = card;
2107 list_add(&card->dapm.list, &card->dapm_list);
2108
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002109#ifdef CONFIG_DEBUG_FS
2110 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2111#endif
2112
Mark Brown88ee1c62011-02-02 10:43:26 +00002113#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002114 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002115 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002116#endif
Andy Green6ed25972008-06-13 16:24:05 +01002117
Mark Brown9a841eb2011-04-12 17:51:37 -07002118 if (card->dapm_widgets)
2119 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2120 card->num_dapm_widgets);
2121
Nicolin Chenf23e8602015-02-14 17:22:49 -08002122 if (card->of_dapm_widgets)
2123 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2124 card->num_of_dapm_widgets);
2125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 /* initialise the sound card only once */
2127 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002128 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002129 if (ret < 0)
2130 goto card_probe_error;
2131 }
2132
Stephen Warren62ae68f2012-06-08 12:34:23 -06002133 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002134 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2135 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002136 list_for_each_entry(rtd, &card->rtd_list, list) {
2137 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002138 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002139 dev_err(card->dev,
2140 "ASoC: failed to instantiate card %d\n",
2141 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002142 goto probe_dai_err;
2143 }
2144 }
2145 }
2146
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002147 /* probe auxiliary components */
2148 ret = soc_probe_aux_devices(card);
2149 if (ret < 0)
2150 goto probe_dai_err;
2151
Mengdong Lin61b00882015-12-02 14:11:48 +08002152 /* Find new DAI links added during probing components and bind them.
2153 * Components with topology may bring new DAIs and DAI links.
2154 */
2155 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2156 if (soc_is_dai_link_bound(card, dai_link))
2157 continue;
2158
2159 ret = soc_init_dai_link(card, dai_link);
2160 if (ret)
2161 goto probe_dai_err;
2162 ret = soc_bind_dai_link(card, dai_link);
2163 if (ret)
2164 goto probe_dai_err;
2165 }
2166
Stephen Warren62ae68f2012-06-08 12:34:23 -06002167 /* probe all DAI links on this card */
2168 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2169 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002170 list_for_each_entry(rtd, &card->rtd_list, list) {
2171 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002172 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002173 dev_err(card->dev,
2174 "ASoC: failed to instantiate card %d\n",
2175 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002176 goto probe_dai_err;
2177 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002178 }
2179 }
2180
Mark Brown888df392012-02-16 19:37:51 -08002181 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002182 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002183
Mark Brownb7af1da2011-04-07 19:18:44 +09002184 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002185 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002186
Mark Brownb8ad29d2011-03-02 18:35:51 +00002187 if (card->dapm_routes)
2188 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2189 card->num_dapm_routes);
2190
Nicolin Chenf23e8602015-02-14 17:22:49 -08002191 if (card->of_dapm_routes)
2192 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2193 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002194
Takashi Iwai861886d2017-04-24 08:54:42 +02002195 /* try to set some sane longname if DMI is available */
2196 snd_soc_set_dmi_name(card, NULL);
2197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002198 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002200 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2201 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002202 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2203 "%s", card->driver_name ? card->driver_name : card->name);
2204 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2205 switch (card->snd_card->driver[i]) {
2206 case '_':
2207 case '-':
2208 case '\0':
2209 break;
2210 default:
2211 if (!isalnum(card->snd_card->driver[i]))
2212 card->snd_card->driver[i] = '_';
2213 break;
2214 }
2215 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002216
Mark Brown28e9ad92011-03-02 18:36:34 +00002217 if (card->late_probe) {
2218 ret = card->late_probe(card);
2219 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002220 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002221 card->name, ret);
2222 goto probe_aux_dev_err;
2223 }
2224 }
2225
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002226 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228 ret = snd_card_register(card->snd_card);
2229 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002230 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2231 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002232 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002233 }
2234
Mark Brown435c5e22008-12-04 15:32:53 +00002235 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002236 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002237 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002238 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002239
2240 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002242probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002243 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002245probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002246 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002248card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002249 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002250 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002251
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002252 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002253 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254 snd_card_free(card->snd_card);
2255
Mark Brownb19e6e72012-03-14 21:18:39 +00002256base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002257 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002259 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260
Mark Brownb19e6e72012-03-14 21:18:39 +00002261 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002262}
2263
2264/* probes a new socdev */
2265static int soc_probe(struct platform_device *pdev)
2266{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002267 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002268
Vinod Koul70a7ca32011-01-14 19:22:48 +05302269 /*
2270 * no card, so machine driver should be registering card
2271 * we should not be here in that case so ret error
2272 */
2273 if (!card)
2274 return -EINVAL;
2275
Mark Brownfe4085e2012-03-02 13:07:41 +00002276 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002277 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002278 card->name);
2279
Mark Brown435c5e22008-12-04 15:32:53 +00002280 /* Bodge while we unpick instantiation */
2281 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002282
Mark Brown28d528c2012-08-09 18:45:23 +01002283 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284}
2285
Vinod Koulb0e26482011-01-13 22:48:02 +05302286static int soc_cleanup_card_resources(struct snd_soc_card *card)
2287{
Mengdong Lin1a497982015-11-18 02:34:11 -05002288 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302289
2290 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002291 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002292 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302293
Vinod Koulb0e26482011-01-13 22:48:02 +05302294 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002295 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002296 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302297
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002298 /* remove auxiliary devices */
2299 soc_remove_aux_devices(card);
2300
Mark Brownd1e81422016-08-18 19:32:59 +01002301 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302302 soc_cleanup_card_debugfs(card);
2303
2304 /* remove the card */
2305 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002306 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302307
Vinod Koulb0e26482011-01-13 22:48:02 +05302308 snd_card_free(card->snd_card);
2309 return 0;
2310
2311}
2312
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002313/* removes a socdev */
2314static int soc_remove(struct platform_device *pdev)
2315{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002316 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317
Mark Brownc5af3a22008-11-28 13:29:45 +00002318 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 return 0;
2320}
2321
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002322int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002323{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002324 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002325 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002326
2327 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002328 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002329
2330 /* Flush out pmdown_time work - we actually do want to run it
2331 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002332 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002333 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002334
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002335 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002336
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002337 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002338 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002340 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002341
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002342 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002343 for (i = 0; i < rtd->num_codecs; i++) {
2344 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002345 pinctrl_pm_select_sleep_state(codec_dai->dev);
2346 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002347 }
2348
Mark Brown416356f2009-06-30 19:05:15 +01002349 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002350}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002351EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002352
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002353const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302354 .suspend = snd_soc_suspend,
2355 .resume = snd_soc_resume,
2356 .freeze = snd_soc_suspend,
2357 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002358 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302359 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002360};
Stephen Warrendeb26072011-04-05 19:35:30 -06002361EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002362
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363/* ASoC platform driver */
2364static struct platform_driver soc_driver = {
2365 .driver = {
2366 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002367 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 },
2369 .probe = soc_probe,
2370 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371};
2372
Mark Brown096e49d2009-07-05 15:12:22 +01002373/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002374 * snd_soc_cnew - create new control
2375 * @_template: control template
2376 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002377 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002378 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002379 *
2380 * Create a new mixer control from a template control.
2381 *
2382 * Returns 0 for success, else error.
2383 */
2384struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002385 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002386 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387{
2388 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002389 struct snd_kcontrol *kcontrol;
2390 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391
2392 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 template.index = 0;
2394
Mark Brownefb7ac32011-03-08 17:23:24 +00002395 if (!long_name)
2396 long_name = template.name;
2397
2398 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002399 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002400 if (!name)
2401 return NULL;
2402
Mark Brownefb7ac32011-03-08 17:23:24 +00002403 template.name = name;
2404 } else {
2405 template.name = long_name;
2406 }
2407
2408 kcontrol = snd_ctl_new1(&template, data);
2409
2410 kfree(name);
2411
2412 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002413}
2414EXPORT_SYMBOL_GPL(snd_soc_cnew);
2415
Liam Girdwood022658b2012-02-03 17:43:09 +00002416static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2417 const struct snd_kcontrol_new *controls, int num_controls,
2418 const char *prefix, void *data)
2419{
2420 int err, i;
2421
2422 for (i = 0; i < num_controls; i++) {
2423 const struct snd_kcontrol_new *control = &controls[i];
2424 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2425 control->name, prefix));
2426 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002427 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2428 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002429 return err;
2430 }
2431 }
2432
2433 return 0;
2434}
2435
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002436struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2437 const char *name)
2438{
2439 struct snd_card *card = soc_card->snd_card;
2440 struct snd_kcontrol *kctl;
2441
2442 if (unlikely(!name))
2443 return NULL;
2444
2445 list_for_each_entry(kctl, &card->controls, list)
2446 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2447 return kctl;
2448 return NULL;
2449}
2450EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2451
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002453 * snd_soc_add_component_controls - Add an array of controls to a component.
2454 *
2455 * @component: Component to add controls to
2456 * @controls: Array of controls to add
2457 * @num_controls: Number of elements in the array
2458 *
2459 * Return: 0 for success, else error.
2460 */
2461int snd_soc_add_component_controls(struct snd_soc_component *component,
2462 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2463{
2464 struct snd_card *card = component->card->snd_card;
2465
2466 return snd_soc_add_controls(card, component->dev, controls,
2467 num_controls, component->name_prefix, component);
2468}
2469EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2470
2471/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002472 * snd_soc_add_codec_controls - add an array of controls to a codec.
2473 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002474 * duplicating this code.
2475 *
2476 * @codec: codec to add controls to
2477 * @controls: array of controls to add
2478 * @num_controls: number of elements in the array
2479 *
2480 * Return 0 for success, else error.
2481 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002482int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002483 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002484{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002485 return snd_soc_add_component_controls(&codec->component, controls,
2486 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002487}
Liam Girdwood022658b2012-02-03 17:43:09 +00002488EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002489
2490/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002491 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002492 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002493 *
2494 * @platform: platform to add controls to
2495 * @controls: array of controls to add
2496 * @num_controls: number of elements in the array
2497 *
2498 * Return 0 for success, else error.
2499 */
2500int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002501 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002502{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002503 return snd_soc_add_component_controls(&platform->component, controls,
2504 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002505}
2506EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2507
2508/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002509 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2510 * Convenience function to add a list of controls.
2511 *
2512 * @soc_card: SoC card to add controls to
2513 * @controls: array of controls to add
2514 * @num_controls: number of elements in the array
2515 *
2516 * Return 0 for success, else error.
2517 */
2518int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2519 const struct snd_kcontrol_new *controls, int num_controls)
2520{
2521 struct snd_card *card = soc_card->snd_card;
2522
2523 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2524 NULL, soc_card);
2525}
2526EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2527
2528/**
2529 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2530 * Convienience function to add a list of controls.
2531 *
2532 * @dai: DAI to add controls to
2533 * @controls: array of controls to add
2534 * @num_controls: number of elements in the array
2535 *
2536 * Return 0 for success, else error.
2537 */
2538int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2539 const struct snd_kcontrol_new *controls, int num_controls)
2540{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002541 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002542
2543 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2544 NULL, dai);
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2547
2548/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002549 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2550 * @dai: DAI
2551 * @clk_id: DAI specific clock ID
2552 * @freq: new clock frequency in Hz
2553 * @dir: new clock direction - input/output.
2554 *
2555 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2556 */
2557int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2558 unsigned int freq, int dir)
2559{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002560 if (dai->driver && dai->driver->ops->set_sysclk)
2561 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002562 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002563 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002564 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002565 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002566 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002567}
2568EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2569
2570/**
Mark Brownec4ee522011-03-07 20:58:11 +00002571 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2572 * @codec: CODEC
2573 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002574 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002575 * @freq: new clock frequency in Hz
2576 * @dir: new clock direction - input/output.
2577 *
2578 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2579 */
2580int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002581 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002582{
2583 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002584 return codec->driver->set_sysclk(codec, clk_id, source,
2585 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002586 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002587 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002588}
2589EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2590
2591/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002592 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2593 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002594 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002595 * @div: new clock divisor.
2596 *
2597 * Configures the clock dividers. This is used to derive the best DAI bit and
2598 * frame clocks from the system or master clock. It's best to set the DAI bit
2599 * and frame clocks as low as possible to save system power.
2600 */
2601int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2602 int div_id, int div)
2603{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002604 if (dai->driver && dai->driver->ops->set_clkdiv)
2605 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002606 else
2607 return -EINVAL;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2610
2611/**
2612 * snd_soc_dai_set_pll - configure DAI PLL.
2613 * @dai: DAI
2614 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002615 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002616 * @freq_in: PLL input clock frequency in Hz
2617 * @freq_out: requested PLL output clock frequency in Hz
2618 *
2619 * Configures and enables PLL to generate output clock based on input clock.
2620 */
Mark Brown85488032009-09-05 18:52:16 +01002621int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2622 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002623{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002624 if (dai->driver && dai->driver->ops->set_pll)
2625 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002626 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002627 else if (dai->codec && dai->codec->driver->set_pll)
2628 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2629 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002630 else
2631 return -EINVAL;
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2634
Mark Brownec4ee522011-03-07 20:58:11 +00002635/*
2636 * snd_soc_codec_set_pll - configure codec PLL.
2637 * @codec: CODEC
2638 * @pll_id: DAI specific PLL ID
2639 * @source: DAI specific source for the PLL
2640 * @freq_in: PLL input clock frequency in Hz
2641 * @freq_out: requested PLL output clock frequency in Hz
2642 *
2643 * Configures and enables PLL to generate output clock based on input clock.
2644 */
2645int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2646 unsigned int freq_in, unsigned int freq_out)
2647{
2648 if (codec->driver->set_pll)
2649 return codec->driver->set_pll(codec, pll_id, source,
2650 freq_in, freq_out);
2651 else
2652 return -EINVAL;
2653}
2654EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2655
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002656/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002657 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2658 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002659 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002660 *
2661 * Configures the DAI for a preset BCLK to sample rate ratio.
2662 */
2663int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2664{
2665 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2666 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2667 else
2668 return -EINVAL;
2669}
2670EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2671
2672/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002673 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2674 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002675 * @fmt: SND_SOC_DAIFMT_ format value.
2676 *
2677 * Configures the DAI hardware format and clocking.
2678 */
2679int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2680{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002681 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002682 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002683 if (dai->driver->ops->set_fmt == NULL)
2684 return -ENOTSUPP;
2685 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002686}
2687EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2688
2689/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002690 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002691 * @slots: Number of slots in use.
2692 * @tx_mask: bitmask representing active TX slots.
2693 * @rx_mask: bitmask representing active RX slots.
2694 *
2695 * Generates the TDM tx and rx slot default masks for DAI.
2696 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002697static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002698 unsigned int *tx_mask,
2699 unsigned int *rx_mask)
2700{
2701 if (*tx_mask || *rx_mask)
2702 return 0;
2703
2704 if (!slots)
2705 return -EINVAL;
2706
2707 *tx_mask = (1 << slots) - 1;
2708 *rx_mask = (1 << slots) - 1;
2709
2710 return 0;
2711}
2712
2713/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002714 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2715 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002716 * @tx_mask: bitmask representing active TX slots.
2717 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002718 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002719 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002720 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002721 * This function configures the specified DAI for TDM operation. @slot contains
2722 * the total number of slots of the TDM stream and @slot_with the width of each
2723 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2724 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2725 * DAI should write to or read from. If a bit is set the corresponding slot is
2726 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2727 * the first slot, bit 1 to the second slot and so on. The first active slot
2728 * maps to the first channel of the DAI, the second active slot to the second
2729 * channel and so on.
2730 *
2731 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2732 * @rx_mask and @slot_width will be ignored.
2733 *
2734 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002735 */
2736int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002737 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002738{
Xiubo Lie5c21512014-03-21 14:17:12 +08002739 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2740 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002741 &tx_mask, &rx_mask);
2742 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002743 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002744
Benoit Cousson88bd8702014-07-08 23:19:34 +02002745 dai->tx_mask = tx_mask;
2746 dai->rx_mask = rx_mask;
2747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002748 if (dai->driver && dai->driver->ops->set_tdm_slot)
2749 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002750 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002751 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002752 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002753}
2754EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2755
2756/**
Barry Song472df3c2009-09-12 01:16:29 +08002757 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2758 * @dai: DAI
2759 * @tx_num: how many TX channels
2760 * @tx_slot: pointer to an array which imply the TX slot number channel
2761 * 0~num-1 uses
2762 * @rx_num: how many RX channels
2763 * @rx_slot: pointer to an array which imply the RX slot number channel
2764 * 0~num-1 uses
2765 *
2766 * configure the relationship between channel number and TDM slot number.
2767 */
2768int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2769 unsigned int tx_num, unsigned int *tx_slot,
2770 unsigned int rx_num, unsigned int *rx_slot)
2771{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002772 if (dai->driver && dai->driver->ops->set_channel_map)
2773 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002774 rx_num, rx_slot);
2775 else
2776 return -EINVAL;
2777}
2778EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2779
2780/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002781 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2782 * @dai: DAI
2783 * @tristate: tristate enable
2784 *
2785 * Tristates the DAI so that others can use it.
2786 */
2787int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2788{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002789 if (dai->driver && dai->driver->ops->set_tristate)
2790 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002791 else
2792 return -EINVAL;
2793}
2794EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2795
2796/**
2797 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2798 * @dai: DAI
2799 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002800 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002801 *
2802 * Mutes the DAI DAC.
2803 */
Mark Brownda183962013-02-06 15:44:07 +00002804int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2805 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002806{
Mark Brownda183962013-02-06 15:44:07 +00002807 if (!dai->driver)
2808 return -ENOTSUPP;
2809
2810 if (dai->driver->ops->mute_stream)
2811 return dai->driver->ops->mute_stream(dai, mute, direction);
2812 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2813 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002814 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002815 else
Mark Brown04570c62012-04-13 19:16:03 +01002816 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002817}
2818EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2819
Mark Brownc5af3a22008-11-28 13:29:45 +00002820/**
2821 * snd_soc_register_card - Register a card with the ASoC core
2822 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002823 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002824 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002825 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302826int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002827{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002828 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002829 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002830
Mark Brownc5af3a22008-11-28 13:29:45 +00002831 if (!card->name || !card->dev)
2832 return -EINVAL;
2833
Stephen Warren5a504962011-12-21 10:40:59 -07002834 for (i = 0; i < card->num_links; i++) {
2835 struct snd_soc_dai_link *link = &card->dai_link[i];
2836
Mengdong Lin923c5e612015-11-23 11:01:49 -05002837 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002838 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002839 dev_err(card->dev, "ASoC: failed to init link %s\n",
2840 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002841 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002842 }
Stephen Warren5a504962011-12-21 10:40:59 -07002843 }
2844
Mark Browned77cc12011-05-03 18:25:34 +01002845 dev_set_drvdata(card->dev, card);
2846
Stephen Warren111c6412011-01-28 14:26:35 -07002847 snd_soc_initialize_card_lists(card);
2848
Mengdong Linf8f80362015-12-02 14:11:22 +08002849 INIT_LIST_HEAD(&card->dai_link_list);
2850 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002851
Mengdong Lin1a497982015-11-18 02:34:11 -05002852 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002853 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002854
Mark Browndb432b42011-10-03 21:06:40 +01002855 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002856 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002857 card->instantiated = 0;
2858 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002859 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002860
Mark Brownb19e6e72012-03-14 21:18:39 +00002861 ret = snd_soc_instantiate_card(card);
2862 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002863 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002864
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002865 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002866 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002867 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2868 int j;
2869
2870 for (j = 0; j < rtd->num_codecs; j++) {
2871 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2872 if (!codec_dai->active)
2873 pinctrl_pm_select_sleep_state(codec_dai->dev);
2874 }
2875
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002876 if (!cpu_dai->active)
2877 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2878 }
2879
Mark Brownb19e6e72012-03-14 21:18:39 +00002880 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002881}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302882EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002883
2884/**
2885 * snd_soc_unregister_card - Unregister a card with the ASoC core
2886 *
2887 * @card: Card to unregister
2888 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002889 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302890int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002891{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002892 if (card->instantiated) {
2893 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002894 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302895 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002896 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002897 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002898
2899 return 0;
2900}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302901EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002902
2903/*
2904 * Simplify DAI link configuration by removing ".-1" from device names
2905 * and sanitizing names.
2906 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002907static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002908{
2909 char *found, name[NAME_SIZE];
2910 int id1, id2;
2911
2912 if (dev_name(dev) == NULL)
2913 return NULL;
2914
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002915 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002916
2917 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002918 found = strstr(name, dev->driver->name);
2919 if (found) {
2920 /* get ID */
2921 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2922
2923 /* discard ID from name if ID == -1 */
2924 if (*id == -1)
2925 found[strlen(dev->driver->name)] = '\0';
2926 }
2927
2928 } else {
2929 /* I2C component devices are named "bus-addr" */
2930 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2931 char tmp[NAME_SIZE];
2932
2933 /* create unique ID number from I2C addr and bus */
2934 *id = ((id1 & 0xffff) << 16) + id2;
2935
2936 /* sanitize component name for DAI link creation */
2937 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002938 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002939 } else
2940 *id = 0;
2941 }
2942
2943 return kstrdup(name, GFP_KERNEL);
2944}
2945
2946/*
2947 * Simplify DAI link naming for single devices with multiple DAIs by removing
2948 * any ".-1" and using the DAI name (instead of device name).
2949 */
2950static inline char *fmt_multiple_name(struct device *dev,
2951 struct snd_soc_dai_driver *dai_drv)
2952{
2953 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002954 dev_err(dev,
2955 "ASoC: error - multiple DAI %s registered with no name\n",
2956 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002957 return NULL;
2958 }
2959
2960 return kstrdup(dai_drv->name, GFP_KERNEL);
2961}
2962
2963/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002964 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002965 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002966 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002967 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002968static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002969{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002970 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002971
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002972 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002973 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2974 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002975 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002976 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002977 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002978 }
Mark Brown91151712008-11-30 23:31:24 +00002979}
Mark Brown91151712008-11-30 23:31:24 +00002980
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002981/* Create a DAI and add it to the component's DAI list */
2982static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2983 struct snd_soc_dai_driver *dai_drv,
2984 bool legacy_dai_naming)
2985{
2986 struct device *dev = component->dev;
2987 struct snd_soc_dai *dai;
2988
2989 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2990
2991 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2992 if (dai == NULL)
2993 return NULL;
2994
2995 /*
2996 * Back in the old days when we still had component-less DAIs,
2997 * instead of having a static name, component-less DAIs would
2998 * inherit the name of the parent device so it is possible to
2999 * register multiple instances of the DAI. We still need to keep
3000 * the same naming style even though those DAIs are not
3001 * component-less anymore.
3002 */
3003 if (legacy_dai_naming &&
3004 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3005 dai->name = fmt_single_name(dev, &dai->id);
3006 } else {
3007 dai->name = fmt_multiple_name(dev, dai_drv);
3008 if (dai_drv->id)
3009 dai->id = dai_drv->id;
3010 else
3011 dai->id = component->num_dai;
3012 }
3013 if (dai->name == NULL) {
3014 kfree(dai);
3015 return NULL;
3016 }
3017
3018 dai->component = component;
3019 dai->dev = dev;
3020 dai->driver = dai_drv;
3021 if (!dai->driver->ops)
3022 dai->driver->ops = &null_dai_ops;
3023
3024 list_add(&dai->list, &component->dai_list);
3025 component->num_dai++;
3026
3027 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3028 return dai;
3029}
3030
Mark Brown91151712008-11-30 23:31:24 +00003031/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003032 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003033 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003034 * @component: The component the DAIs are registered for
3035 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003036 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003037 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3038 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003039 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003040static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003041 struct snd_soc_dai_driver *dai_drv, size_t count,
3042 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003043{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003044 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003045 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003046 unsigned int i;
3047 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003049 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003050
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003051 component->dai_drv = dai_drv;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003052
Mark Brown91151712008-11-30 23:31:24 +00003053 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003055 dai = soc_add_dai(component, dai_drv + i,
3056 count == 1 && legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003057 if (dai == NULL) {
3058 ret = -ENOMEM;
3059 goto err;
3060 }
Mark Brown91151712008-11-30 23:31:24 +00003061 }
3062
3063 return 0;
3064
3065err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003066 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003067
3068 return ret;
3069}
Mark Brown91151712008-11-30 23:31:24 +00003070
Mengdong Lin68003e62015-12-31 16:40:43 +08003071/**
3072 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3073 *
3074 * @component: The component the DAIs are registered for
3075 * @dai_drv: DAI driver to use for the DAI
3076 *
3077 * Topology can use this API to register DAIs when probing a component.
3078 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3079 * will be freed in the component cleanup.
3080 */
3081int snd_soc_register_dai(struct snd_soc_component *component,
3082 struct snd_soc_dai_driver *dai_drv)
3083{
3084 struct snd_soc_dapm_context *dapm =
3085 snd_soc_component_get_dapm(component);
3086 struct snd_soc_dai *dai;
3087 int ret;
3088
3089 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3090 dev_err(component->dev, "Invalid dai type %d\n",
3091 dai_drv->dobj.type);
3092 return -EINVAL;
3093 }
3094
3095 lockdep_assert_held(&client_mutex);
3096 dai = soc_add_dai(component, dai_drv, false);
3097 if (!dai)
3098 return -ENOMEM;
3099
3100 /* Create the DAI widgets here. After adding DAIs, topology may
3101 * also add routes that need these widgets as source or sink.
3102 */
3103 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3104 if (ret != 0) {
3105 dev_err(component->dev,
3106 "Failed to create DAI widgets %d\n", ret);
3107 }
3108
3109 return ret;
3110}
3111EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3112
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003113static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3114 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003115{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003116 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003117
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003118 component->driver->seq_notifier(component, type, subseq);
3119}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003120
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003121static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3122 int event)
3123{
3124 struct snd_soc_component *component = dapm->component;
3125
3126 return component->driver->stream_event(component, event);
3127}
3128
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003129static int snd_soc_component_initialize(struct snd_soc_component *component,
3130 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003131{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003132 struct snd_soc_dapm_context *dapm;
3133
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003134 component->name = fmt_single_name(dev, &component->id);
3135 if (!component->name) {
3136 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003137 return -ENOMEM;
3138 }
3139
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003140 component->dev = dev;
3141 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003142 component->probe = component->driver->probe;
3143 component->remove = component->driver->remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003144 component->suspend = component->driver->suspend;
3145 component->resume = component->driver->resume;
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003146 component->pcm_new = component->driver->pcm_new;
3147 component->pcm_free= component->driver->pcm_free;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003148
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003149 dapm = &component->dapm;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003150 dapm->dev = dev;
3151 dapm->component = component;
3152 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003153 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003154 if (driver->seq_notifier)
3155 dapm->seq_notifier = snd_soc_component_seq_notifier;
3156 if (driver->stream_event)
3157 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003158
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003159 component->controls = driver->controls;
3160 component->num_controls = driver->num_controls;
3161 component->dapm_widgets = driver->dapm_widgets;
3162 component->num_dapm_widgets = driver->num_dapm_widgets;
3163 component->dapm_routes = driver->dapm_routes;
3164 component->num_dapm_routes = driver->num_dapm_routes;
3165
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003166 INIT_LIST_HEAD(&component->dai_list);
3167 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003168
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003169 return 0;
3170}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003171
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003172static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003173{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003174 int val_bytes = regmap_get_val_bytes(component->regmap);
3175
3176 /* Errors are legitimate for non-integer byte multiples */
3177 if (val_bytes > 0)
3178 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003179}
3180
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003181#ifdef CONFIG_REGMAP
3182
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003183/**
3184 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3185 * @component: The component for which to initialize the regmap instance
3186 * @regmap: The regmap instance that should be used by the component
3187 *
3188 * This function allows deferred assignment of the regmap instance that is
3189 * associated with the component. Only use this if the regmap instance is not
3190 * yet ready when the component is registered. The function must also be called
3191 * before the first IO attempt of the component.
3192 */
3193void snd_soc_component_init_regmap(struct snd_soc_component *component,
3194 struct regmap *regmap)
3195{
3196 component->regmap = regmap;
3197 snd_soc_component_setup_regmap(component);
3198}
3199EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3200
3201/**
3202 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3203 * @component: The component for which to de-initialize the regmap instance
3204 *
3205 * Calls regmap_exit() on the regmap instance associated to the component and
3206 * removes the regmap instance from the component.
3207 *
3208 * This function should only be used if snd_soc_component_init_regmap() was used
3209 * to initialize the regmap instance.
3210 */
3211void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3212{
3213 regmap_exit(component->regmap);
3214 component->regmap = NULL;
3215}
3216EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3217
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003218#endif
3219
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003220static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3221{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003222 if (!component->write && !component->read) {
3223 if (!component->regmap)
3224 component->regmap = dev_get_regmap(component->dev, NULL);
3225 if (component->regmap)
3226 snd_soc_component_setup_regmap(component);
3227 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003228
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003229 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003230 INIT_LIST_HEAD(&component->dobj_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003231}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003232
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003233static void snd_soc_component_add(struct snd_soc_component *component)
3234{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003235 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003236 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003237 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003238}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003239
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003240static void snd_soc_component_cleanup(struct snd_soc_component *component)
3241{
3242 snd_soc_unregister_dais(component);
3243 kfree(component->name);
3244}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003245
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003246static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3247{
3248 list_del(&component->list);
3249}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003250
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003251int snd_soc_register_component(struct device *dev,
3252 const struct snd_soc_component_driver *cmpnt_drv,
3253 struct snd_soc_dai_driver *dai_drv,
3254 int num_dai)
3255{
3256 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003257 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003258
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003259 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003260 if (!cmpnt) {
3261 dev_err(dev, "ASoC: Failed to allocate memory\n");
3262 return -ENOMEM;
3263 }
3264
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003265 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3266 if (ret)
3267 goto err_free;
3268
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003269 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003270 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003271
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003272 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3273 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003274 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003275 goto err_cleanup;
3276 }
3277
3278 snd_soc_component_add(cmpnt);
3279
3280 return 0;
3281
3282err_cleanup:
3283 snd_soc_component_cleanup(cmpnt);
3284err_free:
3285 kfree(cmpnt);
3286 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003287}
3288EXPORT_SYMBOL_GPL(snd_soc_register_component);
3289
3290/**
3291 * snd_soc_unregister_component - Unregister a component from the ASoC core
3292 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003293 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003294 */
3295void snd_soc_unregister_component(struct device *dev)
3296{
3297 struct snd_soc_component *cmpnt;
3298
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003299 mutex_lock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003300 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003301 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003302 goto found;
3303 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003304 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003305 return;
3306
3307found:
Liam Girdwood8a978232015-05-29 19:06:14 +01003308 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003309 snd_soc_component_del_unlocked(cmpnt);
3310 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003311 snd_soc_component_cleanup(cmpnt);
3312 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003313}
3314EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3315
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003316static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003317{
3318 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3319
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003320 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003321}
3322
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003323static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003324{
3325 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3326
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003327 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003328}
3329
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003330static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
3331{
3332 struct snd_soc_platform *platform = rtd->platform;
3333
3334 return platform->driver->pcm_new(rtd);
3335}
3336
3337static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
3338{
3339 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
3340 struct snd_soc_platform *platform = rtd->platform;
3341
3342 platform->driver->pcm_free(pcm);
3343}
3344
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003345/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003346 * snd_soc_add_platform - Add a platform to the ASoC core
3347 * @dev: The parent device for the platform
3348 * @platform: The platform to add
Masanari Iida7d1442b2015-07-15 23:02:39 +09003349 * @platform_drv: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003350 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003351int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003352 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003353{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003354 int ret;
3355
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003356 ret = snd_soc_component_initialize(&platform->component,
3357 &platform_drv->component_driver, dev);
3358 if (ret)
3359 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360
3361 platform->dev = dev;
3362 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003363
3364 if (platform_drv->probe)
3365 platform->component.probe = snd_soc_platform_drv_probe;
3366 if (platform_drv->remove)
3367 platform->component.remove = snd_soc_platform_drv_remove;
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003368 if (platform_drv->pcm_new)
3369 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3370 if (platform_drv->pcm_free)
3371 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003372
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003373#ifdef CONFIG_DEBUG_FS
3374 platform->component.debugfs_prefix = "platform";
3375#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00003376
3377 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003378 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003379 list_add(&platform->list, &platform_list);
3380 mutex_unlock(&client_mutex);
3381
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003382 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3383 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003384
3385 return 0;
3386}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003387EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3388
3389/**
3390 * snd_soc_register_platform - Register a platform with the ASoC core
3391 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003392 * @dev: The device for the platform
3393 * @platform_drv: The driver for the platform
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003394 */
3395int snd_soc_register_platform(struct device *dev,
3396 const struct snd_soc_platform_driver *platform_drv)
3397{
3398 struct snd_soc_platform *platform;
3399 int ret;
3400
3401 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3402
3403 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3404 if (platform == NULL)
3405 return -ENOMEM;
3406
3407 ret = snd_soc_add_platform(dev, platform, platform_drv);
3408 if (ret)
3409 kfree(platform);
3410
3411 return ret;
3412}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003413EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3414
3415/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003416 * snd_soc_remove_platform - Remove a platform from the ASoC core
3417 * @platform: the platform to remove
3418 */
3419void snd_soc_remove_platform(struct snd_soc_platform *platform)
3420{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003421
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003422 mutex_lock(&client_mutex);
3423 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003424 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003425 mutex_unlock(&client_mutex);
3426
3427 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003428 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02003429
3430 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003431}
3432EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3433
3434struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3435{
3436 struct snd_soc_platform *platform;
3437
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003438 mutex_lock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003439 list_for_each_entry(platform, &platform_list, list) {
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003440 if (dev == platform->dev) {
3441 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003442 return platform;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003443 }
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003444 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003445 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003446
3447 return NULL;
3448}
3449EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3450
3451/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003452 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3453 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003454 * @dev: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003455 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003456void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003457{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003458 struct snd_soc_platform *platform;
3459
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003460 platform = snd_soc_lookup_platform(dev);
3461 if (!platform)
3462 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003464 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003465 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003466}
3467EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3468
Mark Brown151ab222009-05-09 16:22:58 +01003469static u64 codec_format_map[] = {
3470 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3471 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3472 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3473 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3474 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3475 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3476 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3477 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3478 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3479 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3480 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3481 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3482 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3483 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3484 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3485 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3486};
3487
3488/* Fix up the DAI formats for endianness: codecs don't actually see
3489 * the endianness of the data but we're using the CPU format
3490 * definitions which do need to include endianness so we ensure that
3491 * codec DAIs always have both big and little endian variants set.
3492 */
3493static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3494{
3495 int i;
3496
3497 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3498 if (stream->formats & codec_format_map[i])
3499 stream->formats |= codec_format_map[i];
3500}
3501
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003502static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3503{
3504 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3505
3506 return codec->driver->probe(codec);
3507}
3508
3509static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3510{
3511 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3512
3513 codec->driver->remove(codec);
3514}
3515
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003516static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3517{
3518 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3519
3520 return codec->driver->suspend(codec);
3521}
3522
3523static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3524{
3525 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3526
3527 return codec->driver->resume(codec);
3528}
3529
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003530static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3531 unsigned int reg, unsigned int val)
3532{
3533 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3534
3535 return codec->driver->write(codec, reg, val);
3536}
3537
3538static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3539 unsigned int reg, unsigned int *val)
3540{
3541 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3542
3543 *val = codec->driver->read(codec, reg);
3544
3545 return 0;
3546}
3547
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003548static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3549 enum snd_soc_bias_level level)
3550{
3551 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3552
3553 return codec->driver->set_bias_level(codec, level);
3554}
3555
Mark Brown0d0cf002008-12-10 14:32:45 +00003556/**
3557 * snd_soc_register_codec - Register a codec with the ASoC core
3558 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003559 * @dev: The parent device for this codec
3560 * @codec_drv: Codec driver
3561 * @dai_drv: The associated DAI driver
3562 * @num_dai: Number of DAIs
Mark Brown0d0cf002008-12-10 14:32:45 +00003563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003564int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003565 const struct snd_soc_codec_driver *codec_drv,
3566 struct snd_soc_dai_driver *dai_drv,
3567 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003568{
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003569 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003571 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003572 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003574 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003576 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3577 if (codec == NULL)
3578 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003579
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003580 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003581
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003582 ret = snd_soc_component_initialize(&codec->component,
3583 &codec_drv->component_driver, dev);
3584 if (ret)
3585 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003586
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003587 if (codec_drv->probe)
3588 codec->component.probe = snd_soc_codec_drv_probe;
3589 if (codec_drv->remove)
3590 codec->component.remove = snd_soc_codec_drv_remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003591 if (codec_drv->suspend)
3592 codec->component.suspend = snd_soc_codec_drv_suspend;
3593 if (codec_drv->resume)
3594 codec->component.resume = snd_soc_codec_drv_resume;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003595 if (codec_drv->write)
3596 codec->component.write = snd_soc_codec_drv_write;
3597 if (codec_drv->read)
3598 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003599 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003600
3601 dapm = snd_soc_codec_get_dapm(codec);
3602 dapm->idle_bias_off = codec_drv->idle_bias_off;
3603 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003604 if (codec_drv->seq_notifier)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003605 dapm->seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003606 if (codec_drv->set_bias_level)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003607 dapm->set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608 codec->dev = dev;
3609 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003610 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003611
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003612#ifdef CONFIG_DEBUG_FS
3613 codec->component.init_debugfs = soc_init_codec_debugfs;
3614 codec->component.debugfs_prefix = "codec";
3615#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003616
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003617 if (codec_drv->get_regmap)
3618 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003620 for (i = 0; i < num_dai; i++) {
3621 fixup_codec_formats(&dai_drv[i].playback);
3622 fixup_codec_formats(&dai_drv[i].capture);
3623 }
3624
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003625 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3626 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003627 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003628 goto err_cleanup;
3629 }
3630
3631 list_for_each_entry(dai, &codec->component.dai_list, list)
3632 dai->codec = codec;
3633
Mark Brown054880f2012-04-09 17:29:19 +01003634 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003635 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003636 list_add(&codec->list, &codec_list);
3637 mutex_unlock(&client_mutex);
3638
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003639 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3640 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003641 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003642
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003643err_cleanup:
3644 snd_soc_component_cleanup(&codec->component);
3645err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003646 kfree(codec);
3647 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003648}
3649EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3650
3651/**
3652 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3653 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003654 * @dev: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003655 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003656void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003657{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003658 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003660 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003661 list_for_each_entry(codec, &codec_list, list) {
3662 if (dev == codec->dev)
3663 goto found;
3664 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003665 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003666 return;
3667
3668found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003669 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003670 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003671 mutex_unlock(&client_mutex);
3672
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003673 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3674 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003675
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003676 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003677 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003679}
3680EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3681
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003682/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003683int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3684 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003685{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003686 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003687 int ret;
3688
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303689 if (!card->dev) {
3690 pr_err("card->dev is not set before calling %s\n", __func__);
3691 return -EINVAL;
3692 }
3693
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003694 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303695
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003696 ret = of_property_read_string_index(np, propname, 0, &card->name);
3697 /*
3698 * EINVAL means the property does not exist. This is fine providing
3699 * card->name was previously set, which is checked later in
3700 * snd_soc_register_card.
3701 */
3702 if (ret < 0 && ret != -EINVAL) {
3703 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003704 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003705 propname, ret);
3706 return ret;
3707 }
3708
3709 return 0;
3710}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003711EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003712
Xiubo Li9a6d4862014-02-08 15:59:52 +08003713static const struct snd_soc_dapm_widget simple_widgets[] = {
3714 SND_SOC_DAPM_MIC("Microphone", NULL),
3715 SND_SOC_DAPM_LINE("Line", NULL),
3716 SND_SOC_DAPM_HP("Headphone", NULL),
3717 SND_SOC_DAPM_SPK("Speaker", NULL),
3718};
3719
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003720int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003721 const char *propname)
3722{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003723 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003724 struct snd_soc_dapm_widget *widgets;
3725 const char *template, *wname;
3726 int i, j, num_widgets, ret;
3727
3728 num_widgets = of_property_count_strings(np, propname);
3729 if (num_widgets < 0) {
3730 dev_err(card->dev,
3731 "ASoC: Property '%s' does not exist\n", propname);
3732 return -EINVAL;
3733 }
3734 if (num_widgets & 1) {
3735 dev_err(card->dev,
3736 "ASoC: Property '%s' length is not even\n", propname);
3737 return -EINVAL;
3738 }
3739
3740 num_widgets /= 2;
3741 if (!num_widgets) {
3742 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3743 propname);
3744 return -EINVAL;
3745 }
3746
3747 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3748 GFP_KERNEL);
3749 if (!widgets) {
3750 dev_err(card->dev,
3751 "ASoC: Could not allocate memory for widgets\n");
3752 return -ENOMEM;
3753 }
3754
3755 for (i = 0; i < num_widgets; i++) {
3756 ret = of_property_read_string_index(np, propname,
3757 2 * i, &template);
3758 if (ret) {
3759 dev_err(card->dev,
3760 "ASoC: Property '%s' index %d read error:%d\n",
3761 propname, 2 * i, ret);
3762 return -EINVAL;
3763 }
3764
3765 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3766 if (!strncmp(template, simple_widgets[j].name,
3767 strlen(simple_widgets[j].name))) {
3768 widgets[i] = simple_widgets[j];
3769 break;
3770 }
3771 }
3772
3773 if (j >= ARRAY_SIZE(simple_widgets)) {
3774 dev_err(card->dev,
3775 "ASoC: DAPM widget '%s' is not supported\n",
3776 template);
3777 return -EINVAL;
3778 }
3779
3780 ret = of_property_read_string_index(np, propname,
3781 (2 * i) + 1,
3782 &wname);
3783 if (ret) {
3784 dev_err(card->dev,
3785 "ASoC: Property '%s' index %d read error:%d\n",
3786 propname, (2 * i) + 1, ret);
3787 return -EINVAL;
3788 }
3789
3790 widgets[i].name = wname;
3791 }
3792
Nicolin Chenf23e8602015-02-14 17:22:49 -08003793 card->of_dapm_widgets = widgets;
3794 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003795
3796 return 0;
3797}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003798EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003799
Jyri Sarha61310842015-09-09 21:27:43 +03003800static int snd_soc_of_get_slot_mask(struct device_node *np,
3801 const char *prop_name,
3802 unsigned int *mask)
3803{
3804 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003805 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003806 int i;
3807
3808 if (!of_slot_mask)
3809 return 0;
3810 val /= sizeof(u32);
3811 for (i = 0; i < val; i++)
3812 if (be32_to_cpup(&of_slot_mask[i]))
3813 *mask |= (1 << i);
3814
3815 return val;
3816}
3817
Xiubo Li89c67852014-02-14 09:34:35 +08003818int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003819 unsigned int *tx_mask,
3820 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003821 unsigned int *slots,
3822 unsigned int *slot_width)
3823{
3824 u32 val;
3825 int ret;
3826
Jyri Sarha61310842015-09-09 21:27:43 +03003827 if (tx_mask)
3828 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3829 if (rx_mask)
3830 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3831
Xiubo Li89c67852014-02-14 09:34:35 +08003832 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3833 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3834 if (ret)
3835 return ret;
3836
3837 if (slots)
3838 *slots = val;
3839 }
3840
3841 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3842 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3843 if (ret)
3844 return ret;
3845
3846 if (slot_width)
3847 *slot_width = val;
3848 }
3849
3850 return 0;
3851}
3852EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3853
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003854void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003855 struct snd_soc_codec_conf *codec_conf,
3856 struct device_node *of_node,
3857 const char *propname)
3858{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003859 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003860 const char *str;
3861 int ret;
3862
3863 ret = of_property_read_string(np, propname, &str);
3864 if (ret < 0) {
3865 /* no prefix is not error */
3866 return;
3867 }
3868
3869 codec_conf->of_node = of_node;
3870 codec_conf->name_prefix = str;
3871}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003872EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003873
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003874int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003875 const char *propname)
3876{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003877 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003878 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003879 struct snd_soc_dapm_route *routes;
3880 int i, ret;
3881
3882 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003883 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003884 dev_err(card->dev,
3885 "ASoC: Property '%s' does not exist or its length is not even\n",
3886 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003887 return -EINVAL;
3888 }
3889 num_routes /= 2;
3890 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003891 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003892 propname);
3893 return -EINVAL;
3894 }
3895
Mark Browne3b1e6a2014-12-18 11:46:38 +00003896 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003897 GFP_KERNEL);
3898 if (!routes) {
3899 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003900 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003901 return -EINVAL;
3902 }
3903
3904 for (i = 0; i < num_routes; i++) {
3905 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003906 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003907 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003908 dev_err(card->dev,
3909 "ASoC: Property '%s' index %d could not be read: %d\n",
3910 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003911 return -EINVAL;
3912 }
3913 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003914 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003915 if (ret) {
3916 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003917 "ASoC: Property '%s' index %d could not be read: %d\n",
3918 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003919 return -EINVAL;
3920 }
3921 }
3922
Nicolin Chenf23e8602015-02-14 17:22:49 -08003923 card->num_of_dapm_routes = num_routes;
3924 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003925
3926 return 0;
3927}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003928EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003929
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003930unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003931 const char *prefix,
3932 struct device_node **bitclkmaster,
3933 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003934{
3935 int ret, i;
3936 char prop[128];
3937 unsigned int format = 0;
3938 int bit, frame;
3939 const char *str;
3940 struct {
3941 char *name;
3942 unsigned int val;
3943 } of_fmt_table[] = {
3944 { "i2s", SND_SOC_DAIFMT_I2S },
3945 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3946 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3947 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3948 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3949 { "ac97", SND_SOC_DAIFMT_AC97 },
3950 { "pdm", SND_SOC_DAIFMT_PDM},
3951 { "msb", SND_SOC_DAIFMT_MSB },
3952 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003953 };
3954
3955 if (!prefix)
3956 prefix = "";
3957
3958 /*
3959 * check "[prefix]format = xxx"
3960 * SND_SOC_DAIFMT_FORMAT_MASK area
3961 */
3962 snprintf(prop, sizeof(prop), "%sformat", prefix);
3963 ret = of_property_read_string(np, prop, &str);
3964 if (ret == 0) {
3965 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3966 if (strcmp(str, of_fmt_table[i].name) == 0) {
3967 format |= of_fmt_table[i].val;
3968 break;
3969 }
3970 }
3971 }
3972
3973 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003974 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003975 * SND_SOC_DAIFMT_CLOCK_MASK area
3976 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003977 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003978 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003979 format |= SND_SOC_DAIFMT_CONT;
3980 else
3981 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003982
3983 /*
3984 * check "[prefix]bitclock-inversion"
3985 * check "[prefix]frame-inversion"
3986 * SND_SOC_DAIFMT_INV_MASK area
3987 */
3988 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3989 bit = !!of_get_property(np, prop, NULL);
3990
3991 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3992 frame = !!of_get_property(np, prop, NULL);
3993
3994 switch ((bit << 4) + frame) {
3995 case 0x11:
3996 format |= SND_SOC_DAIFMT_IB_IF;
3997 break;
3998 case 0x10:
3999 format |= SND_SOC_DAIFMT_IB_NF;
4000 break;
4001 case 0x01:
4002 format |= SND_SOC_DAIFMT_NB_IF;
4003 break;
4004 default:
4005 /* SND_SOC_DAIFMT_NB_NF is default */
4006 break;
4007 }
4008
4009 /*
4010 * check "[prefix]bitclock-master"
4011 * check "[prefix]frame-master"
4012 * SND_SOC_DAIFMT_MASTER_MASK area
4013 */
4014 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4015 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004016 if (bit && bitclkmaster)
4017 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004018
4019 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4020 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004021 if (frame && framemaster)
4022 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004023
4024 switch ((bit << 4) + frame) {
4025 case 0x11:
4026 format |= SND_SOC_DAIFMT_CBM_CFM;
4027 break;
4028 case 0x10:
4029 format |= SND_SOC_DAIFMT_CBM_CFS;
4030 break;
4031 case 0x01:
4032 format |= SND_SOC_DAIFMT_CBS_CFM;
4033 break;
4034 default:
4035 format |= SND_SOC_DAIFMT_CBS_CFS;
4036 break;
4037 }
4038
4039 return format;
4040}
4041EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4042
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004043int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004044 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004045{
4046 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004047 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004048 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004049
4050 mutex_lock(&client_mutex);
4051 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004052 component_of_node = pos->dev->of_node;
4053 if (!component_of_node && pos->dev->parent)
4054 component_of_node = pos->dev->parent->of_node;
4055
4056 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004057 continue;
4058
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004059 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004060 ret = pos->driver->of_xlate_dai_name(pos,
4061 args,
4062 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004063 } else {
4064 int id = -1;
4065
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004066 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004067 case 0:
4068 id = 0; /* same as dai_drv[0] */
4069 break;
4070 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004071 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004072 break;
4073 default:
4074 /* not supported */
4075 break;
4076 }
4077
4078 if (id < 0 || id >= pos->num_dai) {
4079 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004080 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004081 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004082
4083 ret = 0;
4084
4085 *dai_name = pos->dai_drv[id].name;
4086 if (!*dai_name)
4087 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004088 }
4089
Kuninori Morimotocb470082013-09-10 17:39:56 -07004090 break;
4091 }
4092 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004093 return ret;
4094}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004095EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004096
4097int snd_soc_of_get_dai_name(struct device_node *of_node,
4098 const char **dai_name)
4099{
4100 struct of_phandle_args args;
4101 int ret;
4102
4103 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4104 "#sound-dai-cells", 0, &args);
4105 if (ret)
4106 return ret;
4107
4108 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07004109
4110 of_node_put(args.np);
4111
4112 return ret;
4113}
4114EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4115
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004116/*
4117 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4118 * @dev: Card device
4119 * @of_node: Device node
4120 * @dai_link: DAI link
4121 *
4122 * Builds an array of CODEC DAI components from the DAI link property
4123 * 'sound-dai'.
4124 * The array is set in the DAI link and the number of DAIs is set accordingly.
4125 * The device nodes in the array (of_node) must be dereferenced by the caller.
4126 *
4127 * Returns 0 for success
4128 */
4129int snd_soc_of_get_dai_link_codecs(struct device *dev,
4130 struct device_node *of_node,
4131 struct snd_soc_dai_link *dai_link)
4132{
4133 struct of_phandle_args args;
4134 struct snd_soc_dai_link_component *component;
4135 char *name;
4136 int index, num_codecs, ret;
4137
4138 /* Count the number of CODECs */
4139 name = "sound-dai";
4140 num_codecs = of_count_phandle_with_args(of_node, name,
4141 "#sound-dai-cells");
4142 if (num_codecs <= 0) {
4143 if (num_codecs == -ENOENT)
4144 dev_err(dev, "No 'sound-dai' property\n");
4145 else
4146 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4147 return num_codecs;
4148 }
4149 component = devm_kzalloc(dev,
4150 sizeof *component * num_codecs,
4151 GFP_KERNEL);
4152 if (!component)
4153 return -ENOMEM;
4154 dai_link->codecs = component;
4155 dai_link->num_codecs = num_codecs;
4156
4157 /* Parse the list */
4158 for (index = 0, component = dai_link->codecs;
4159 index < dai_link->num_codecs;
4160 index++, component++) {
4161 ret = of_parse_phandle_with_args(of_node, name,
4162 "#sound-dai-cells",
4163 index, &args);
4164 if (ret)
4165 goto err;
4166 component->of_node = args.np;
4167 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4168 if (ret < 0)
4169 goto err;
4170 }
4171 return 0;
4172err:
4173 for (index = 0, component = dai_link->codecs;
4174 index < dai_link->num_codecs;
4175 index++, component++) {
4176 if (!component->of_node)
4177 break;
4178 of_node_put(component->of_node);
4179 component->of_node = NULL;
4180 }
4181 dai_link->codecs = NULL;
4182 dai_link->num_codecs = 0;
4183 return ret;
4184}
4185EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4186
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004187static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004188{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004189 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01004190 snd_soc_util_init();
4191
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004192 return platform_driver_register(&soc_driver);
4193}
Mark Brown4abe8e12010-10-12 17:41:03 +01004194module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004195
Mark Brown7d8c16a2008-11-30 22:11:24 +00004196static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004197{
Mark Brownfb257892011-04-28 10:57:54 +01004198 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004199 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01004200
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004201 platform_driver_unregister(&soc_driver);
4202}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004203module_exit(snd_soc_exit);
4204
4205/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004206MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004207MODULE_DESCRIPTION("ALSA SoC Core");
4208MODULE_LICENSE("GPL");
4209MODULE_ALIAS("platform:soc-audio");