blob: b0fb17082691def49a57f10631b4fb17a51e9d33 [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>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000037#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080038#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000040#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010044#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010045#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000072/* returns the minimum number of bytes needed to represent
73 * a particular given value */
74static int min_bytes_needed(unsigned long val)
75{
76 int c = 0;
77 int i;
78
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
88}
89
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000090/* fill buf which is 'len' bytes with a formatted
91 * string of the form 'reg: value\n' */
92static int format_register_str(struct snd_soc_codec *codec,
93 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000094{
Stephen Warren00b317a2011-04-01 14:50:44 -060095 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
96 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000097 int ret;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000098
99 /* +2 for ': ' and + 1 for '\n' */
100 if (wordsize + regsize + 2 + 1 != len)
101 return -EINVAL;
102
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200103 sprintf(buf, "%.*x: ", wordsize, reg);
104 buf += wordsize + 2;
105
Mark Brown25032c12011-08-01 13:52:48 +0900106 ret = snd_soc_read(codec, reg);
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200107 if (ret < 0)
108 memset(buf, 'X', regsize);
109 else
110 sprintf(buf, "%.*x", regsize, ret);
111 buf[regsize] = '\n';
112 /* no NUL-termination needed */
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000113 return 0;
114}
115
116/* codec register dump */
117static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
118 size_t count, loff_t pos)
119{
120 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000121 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000122 int len;
123 size_t total = 0;
124 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000125
Stephen Warren00b317a2011-04-01 14:50:44 -0600126 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
127 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000128
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000129 len = wordsize + regsize + 2 + 1;
130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000131 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000132 return 0;
133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000134 if (codec->driver->reg_cache_step)
135 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000137 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100138 /* only support larger than PAGE_SIZE bytes debugfs
139 * entries for the default case */
140 if (p >= pos) {
141 if (total + len >= count - 1)
142 break;
143 format_register_str(codec, i, buf + total, len);
144 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100145 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100146 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 }
148
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000149 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000150
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000151 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000152}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000153
Mark Brown2624d5f2009-11-03 21:56:13 +0000154static ssize_t codec_reg_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
Mark Brown36ae1a92012-01-06 17:12:45 -0800157 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000159 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000160}
161
162static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
163
Mark Browndbe21402010-02-12 11:37:24 +0000164static ssize_t pmdown_time_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
Mark Brown36ae1a92012-01-06 17:12:45 -0800167 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000170}
171
172static ssize_t pmdown_time_set(struct device *dev,
173 struct device_attribute *attr,
174 const char *buf, size_t count)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700177 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000178
Jingoo Hanb785a492013-07-19 16:24:59 +0900179 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700180 if (ret)
181 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000182
183 return count;
184}
185
186static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
187
Takashi Iwaid29697d2015-01-30 20:16:37 +0100188static struct attribute *soc_dev_attrs[] = {
189 &dev_attr_codec_reg.attr,
190 &dev_attr_pmdown_time.attr,
191 NULL
192};
193
194static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
195 struct attribute *attr, int idx)
196{
197 struct device *dev = kobj_to_dev(kobj);
198 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
199
200 if (attr == &dev_attr_pmdown_time.attr)
201 return attr->mode; /* always visible */
202 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
203}
204
205static const struct attribute_group soc_dapm_dev_group = {
206 .attrs = soc_dapm_dev_attrs,
207 .is_visible = soc_dev_attr_is_visible,
208};
209
210static const struct attribute_group soc_dev_roup = {
211 .attrs = soc_dev_attrs,
212 .is_visible = soc_dev_attr_is_visible,
213};
214
215static const struct attribute_group *soc_dev_attr_groups[] = {
216 &soc_dapm_dev_group,
217 &soc_dev_roup,
218 NULL
219};
220
Mark Brown2624d5f2009-11-03 21:56:13 +0000221#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000222static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000223 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000224{
225 ssize_t ret;
226 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000227 char *buf;
228
229 if (*ppos < 0 || !count)
230 return -EINVAL;
231
232 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000233 if (!buf)
234 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000235
236 ret = soc_codec_reg_show(codec, buf, count, *ppos);
237 if (ret >= 0) {
238 if (copy_to_user(user_buf, buf, ret)) {
239 kfree(buf);
240 return -EFAULT;
241 }
242 *ppos += ret;
243 }
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 kfree(buf);
246 return ret;
247}
248
249static ssize_t codec_reg_write_file(struct file *file,
250 const char __user *user_buf, size_t count, loff_t *ppos)
251{
252 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700253 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000254 char *start = buf;
255 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900257 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000258
259 buf_size = min(count, (sizeof(buf)-1));
260 if (copy_from_user(buf, user_buf, buf_size))
261 return -EFAULT;
262 buf[buf_size] = 0;
263
Mark Brown2624d5f2009-11-03 21:56:13 +0000264 while (*start == ' ')
265 start++;
266 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000267 while (*start == ' ')
268 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900269 ret = kstrtoul(start, 16, &value);
270 if (ret)
271 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000272
273 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030274 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000275
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000276 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000277 return buf_size;
278}
279
280static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700281 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000282 .read = codec_reg_read_file,
283 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200284 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000285};
286
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200287static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100288{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200289 if (!component->card->debugfs_card_root)
290 return;
291
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200292 if (component->debugfs_prefix) {
293 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100294
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200295 name = kasprintf(GFP_KERNEL, "%s:%s",
296 component->debugfs_prefix, component->name);
297 if (name) {
298 component->debugfs_root = debugfs_create_dir(name,
299 component->card->debugfs_card_root);
300 kfree(name);
301 }
302 } else {
303 component->debugfs_root = debugfs_create_dir(component->name,
304 component->card->debugfs_card_root);
305 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100306
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200307 if (!component->debugfs_root) {
308 dev_warn(component->dev,
309 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000310 return;
311 }
312
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200313 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
314 component->debugfs_root);
315
316 if (component->init_debugfs)
317 component->init_debugfs(component);
318}
319
320static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
321{
322 debugfs_remove_recursive(component->debugfs_root);
323}
324
325static void soc_init_codec_debugfs(struct snd_soc_component *component)
326{
327 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
328
Mark Brown2624d5f2009-11-03 21:56:13 +0000329 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200330 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000331 codec, &codec_reg_fops);
332 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200333 dev_warn(codec->dev,
334 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000335}
336
Mark Brownc3c5a192010-09-15 18:15:14 +0100337static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
338 size_t count, loff_t *ppos)
339{
340 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100341 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100342 struct snd_soc_codec *codec;
343
344 if (!buf)
345 return -ENOMEM;
346
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100347 mutex_lock(&client_mutex);
348
Mark Brown2b194f9d2010-10-13 10:52:16 +0100349 list_for_each_entry(codec, &codec_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200351 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100352 if (len >= 0)
353 ret += len;
354 if (ret > PAGE_SIZE) {
355 ret = PAGE_SIZE;
356 break;
357 }
358 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100359
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100360 mutex_unlock(&client_mutex);
361
Mark Brownc3c5a192010-09-15 18:15:14 +0100362 if (ret >= 0)
363 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
364
365 kfree(buf);
366
367 return ret;
368}
369
370static const struct file_operations codec_list_fops = {
371 .read = codec_list_read_file,
372 .llseek = default_llseek,/* read accesses f_pos */
373};
374
Mark Brownf3208782010-09-15 18:19:07 +0100375static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
376 size_t count, loff_t *ppos)
377{
378 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100379 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100380 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100381 struct snd_soc_dai *dai;
382
383 if (!buf)
384 return -ENOMEM;
385
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100386 mutex_lock(&client_mutex);
387
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100388 list_for_each_entry(component, &component_list, list) {
389 list_for_each_entry(dai, &component->dai_list, list) {
390 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
391 dai->name);
392 if (len >= 0)
393 ret += len;
394 if (ret > PAGE_SIZE) {
395 ret = PAGE_SIZE;
396 break;
397 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 }
399 }
Mark Brownf3208782010-09-15 18:19:07 +0100400
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100401 mutex_unlock(&client_mutex);
402
Mark Brown2b194f9d2010-10-13 10:52:16 +0100403 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100404
405 kfree(buf);
406
407 return ret;
408}
409
410static const struct file_operations dai_list_fops = {
411 .read = dai_list_read_file,
412 .llseek = default_llseek,/* read accesses f_pos */
413};
414
Mark Brown19c7ac22010-09-15 18:22:40 +0100415static ssize_t platform_list_read_file(struct file *file,
416 char __user *user_buf,
417 size_t count, loff_t *ppos)
418{
419 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100420 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100421 struct snd_soc_platform *platform;
422
423 if (!buf)
424 return -ENOMEM;
425
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100426 mutex_lock(&client_mutex);
427
Mark Brown2b194f9d2010-10-13 10:52:16 +0100428 list_for_each_entry(platform, &platform_list, list) {
429 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200430 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100431 if (len >= 0)
432 ret += len;
433 if (ret > PAGE_SIZE) {
434 ret = PAGE_SIZE;
435 break;
436 }
437 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100438
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100439 mutex_unlock(&client_mutex);
440
Mark Brown2b194f9d2010-10-13 10:52:16 +0100441 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100442
443 kfree(buf);
444
445 return ret;
446}
447
448static const struct file_operations platform_list_fops = {
449 .read = platform_list_read_file,
450 .llseek = default_llseek,/* read accesses f_pos */
451};
452
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200453static void soc_init_card_debugfs(struct snd_soc_card *card)
454{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200455 if (!snd_soc_debugfs_root)
456 return;
457
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200458 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000459 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200460 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200461 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100462 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200463 return;
464 }
465
466 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
467 card->debugfs_card_root,
468 &card->pop_time);
469 if (!card->debugfs_pop_time)
470 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000471 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200472}
473
474static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
475{
476 debugfs_remove_recursive(card->debugfs_card_root);
477}
478
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200479
480static void snd_soc_debugfs_init(void)
481{
482 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
483 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
484 pr_warn("ASoC: Failed to create debugfs directory\n");
485 snd_soc_debugfs_root = NULL;
486 return;
487 }
488
489 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
490 &codec_list_fops))
491 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
492
493 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
494 &dai_list_fops))
495 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
496
497 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
498 &platform_list_fops))
499 pr_warn("ASoC: Failed to create platform list debugfs file\n");
500}
501
502static void snd_soc_debugfs_exit(void)
503{
504 debugfs_remove_recursive(snd_soc_debugfs_root);
505}
506
Mark Brown2624d5f2009-11-03 21:56:13 +0000507#else
508
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200509#define soc_init_codec_debugfs NULL
510
511static inline void soc_init_component_debugfs(
512 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000513{
514}
515
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200516static inline void soc_cleanup_component_debugfs(
517 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000518{
519}
520
Axel Linb95fccb2010-11-09 17:06:44 +0800521static inline void soc_init_card_debugfs(struct snd_soc_card *card)
522{
523}
524
525static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
526{
527}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200528
529static inline void snd_soc_debugfs_init(void)
530{
531}
532
533static inline void snd_soc_debugfs_exit(void)
534{
535}
536
Mark Brown2624d5f2009-11-03 21:56:13 +0000537#endif
538
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100539struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
540 const char *dai_link, int stream)
541{
Mengdong Lin1a497982015-11-18 02:34:11 -0500542 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100543
Mengdong Lin1a497982015-11-18 02:34:11 -0500544 list_for_each_entry(rtd, &card->rtd_list, list) {
545 if (rtd->dai_link->no_pcm &&
546 !strcmp(rtd->dai_link->name, dai_link))
547 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100548 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000549 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100550 return NULL;
551}
552EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
553
Mengdong Lin1a497982015-11-18 02:34:11 -0500554static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
555 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
556{
557 struct snd_soc_pcm_runtime *rtd;
558
559 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
560 if (!rtd)
561 return NULL;
562
563 rtd->card = card;
564 rtd->dai_link = dai_link;
565 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
566 dai_link->num_codecs,
567 GFP_KERNEL);
568 if (!rtd->codec_dais) {
569 kfree(rtd);
570 return NULL;
571 }
572
573 return rtd;
574}
575
576static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
577{
578 if (rtd && rtd->codec_dais)
579 kfree(rtd->codec_dais);
580 kfree(rtd);
581}
582
583static void soc_add_pcm_runtime(struct snd_soc_card *card,
584 struct snd_soc_pcm_runtime *rtd)
585{
586 list_add_tail(&rtd->list, &card->rtd_list);
587 rtd->num = card->num_rtd;
588 card->num_rtd++;
589}
590
591static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
592{
593 struct snd_soc_pcm_runtime *rtd, *_rtd;
594
595 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
596 list_del(&rtd->list);
597 soc_free_pcm_runtime(rtd);
598 }
599
600 card->num_rtd = 0;
601}
602
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100603struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
604 const char *dai_link)
605{
Mengdong Lin1a497982015-11-18 02:34:11 -0500606 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100607
Mengdong Lin1a497982015-11-18 02:34:11 -0500608 list_for_each_entry(rtd, &card->rtd_list, list) {
609 if (!strcmp(rtd->dai_link->name, dai_link))
610 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100611 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000612 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100613 return NULL;
614}
615EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
616
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100617static void codec2codec_close_delayed_work(struct work_struct *work)
618{
619 /* Currently nothing to do for c2c links
620 * Since c2c links are internal nodes in the DAPM graph and
621 * don't interface with the outside world or application layer
622 * we don't have to do any special handling on close.
623 */
624}
625
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000626#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000628int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200629{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000630 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000631 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500632 struct snd_soc_pcm_runtime *rtd;
633 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200635 /* If the card is not initialized yet there is nothing to do */
636 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200637 return 0;
638
Andy Green6ed25972008-06-13 16:24:05 +0100639 /* Due to the resume being scheduled into a workqueue we could
640 * suspend before that's finished - wait for it to complete.
641 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 snd_power_lock(card->snd_card);
643 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
644 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100645
646 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100648
Mark Browna00f90f2010-12-02 16:24:24 +0000649 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500650 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100651
Mengdong Lin1a497982015-11-18 02:34:11 -0500652 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100653 continue;
654
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 for (i = 0; i < rtd->num_codecs; i++) {
656 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200657 struct snd_soc_dai_driver *drv = dai->driver;
658
659 if (drv->ops->digital_mute && dai->playback_active)
660 drv->ops->digital_mute(dai, 1);
661 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100664 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500665 list_for_each_entry(rtd, &card->rtd_list, list) {
666 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100667 continue;
668
Mengdong Lin1a497982015-11-18 02:34:11 -0500669 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100670 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100671
Mark Brown87506542008-11-18 20:50:34 +0000672 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000673 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674
Mengdong Lin1a497982015-11-18 02:34:11 -0500675 list_for_each_entry(rtd, &card->rtd_list, list) {
676 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100677
Mengdong Lin1a497982015-11-18 02:34:11 -0500678 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100679 continue;
680
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100681 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100683 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200685 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500686 list_for_each_entry(rtd, &card->rtd_list, list)
687 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100688
Mengdong Lin1a497982015-11-18 02:34:11 -0500689 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690
Mengdong Lin1a497982015-11-18 02:34:11 -0500691 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100692 continue;
693
Mengdong Lin1a497982015-11-18 02:34:11 -0500694 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800695 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800696 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697
Mengdong Lin1a497982015-11-18 02:34:11 -0500698 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800699 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800700 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 }
702
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200703 /* Recheck all endpoints too, their state is affected by suspend */
704 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700705 snd_soc_dapm_sync(&card->dapm);
706
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000707 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000708 list_for_each_entry(component, &card->component_dev_list, card_list) {
709 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000710
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000711 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000713 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200714 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000716 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000717 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000718 * bias off then being in STANDBY
719 * means it's doing something,
720 * otherwise fall through.
721 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200722 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000723 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200724 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000725 break;
726 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 case SND_SOC_BIAS_OFF:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000729 if (component->suspend)
730 component->suspend(component);
731 component->suspended = 1;
732 if (component->regmap)
733 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800734 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000735 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 break;
737 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000738 dev_dbg(component->dev,
739 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 break;
741 }
742 }
743 }
744
Mengdong Lin1a497982015-11-18 02:34:11 -0500745 list_for_each_entry(rtd, &card->rtd_list, list) {
746 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747
Mengdong Lin1a497982015-11-18 02:34:11 -0500748 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 continue;
750
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100751 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800753
754 /* deactivate pins to sleep state */
755 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Mark Brown87506542008-11-18 20:50:34 +0000758 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000759 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760
761 return 0;
762}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000763EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764
Andy Green6ed25972008-06-13 16:24:05 +0100765/* deferred resume work, so resume can complete before we finished
766 * setting our codec back up, which can be very slow on I2C
767 */
768static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 struct snd_soc_card *card =
771 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500772 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000773 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500774 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775
Andy Green6ed25972008-06-13 16:24:05 +0100776 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
777 * so userspace apps are blocked from touching us
778 */
779
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000780 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100781
Mark Brown99497882010-05-07 20:24:05 +0100782 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100784
Mark Brown87506542008-11-18 20:50:34 +0000785 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000786 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100788 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500789 list_for_each_entry(rtd, &card->rtd_list, list) {
790 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100791
Mengdong Lin1a497982015-11-18 02:34:11 -0500792 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100793 continue;
794
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100795 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797 }
798
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000799 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000800 if (component->suspended) {
801 if (component->resume)
802 component->resume(component);
803 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100804 }
805 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806
Mengdong Lin1a497982015-11-18 02:34:11 -0500807 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100808
Mengdong Lin1a497982015-11-18 02:34:11 -0500809 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100810 continue;
811
Mengdong Lin1a497982015-11-18 02:34:11 -0500812 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000813 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800814 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000815
Mengdong Lin1a497982015-11-18 02:34:11 -0500816 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000817 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800818 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200819 }
820
Mark Brown3ff3f642008-05-19 12:32:25 +0200821 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500822 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100823
Mengdong Lin1a497982015-11-18 02:34:11 -0500824 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100825 continue;
826
Mengdong Lin1a497982015-11-18 02:34:11 -0500827 for (i = 0; i < rtd->num_codecs; i++) {
828 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200829 struct snd_soc_dai_driver *drv = dai->driver;
830
831 if (drv->ops->digital_mute && dai->playback_active)
832 drv->ops->digital_mute(dai, 0);
833 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200834 }
835
Mengdong Lin1a497982015-11-18 02:34:11 -0500836 list_for_each_entry(rtd, &card->rtd_list, list) {
837 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100838
Mengdong Lin1a497982015-11-18 02:34:11 -0500839 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100840 continue;
841
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100842 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200844 }
845
Mark Brown87506542008-11-18 20:50:34 +0000846 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000847 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200848
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100850
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200851 /* Recheck all endpoints too, their state is affected by suspend */
852 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700853 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530854
855 /* userspace can access us now we are back as we were before */
856 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100857}
858
859/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000860int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100861{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000862 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100863 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500864 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200865
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200866 /* If the card is not initialized yet there is nothing to do */
867 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800868 return 0;
869
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800870 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500871 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200872 struct snd_soc_dai **codec_dais = rtd->codec_dais;
873 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
874 int j;
875
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800876 if (cpu_dai->active)
877 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200878
879 for (j = 0; j < rtd->num_codecs; j++) {
880 struct snd_soc_dai *codec_dai = codec_dais[j];
881 if (codec_dai->active)
882 pinctrl_pm_select_default_state(codec_dai->dev);
883 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800884 }
885
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100886 /*
887 * DAIs that also act as the control bus master might have other drivers
888 * hanging off them so need to resume immediately. Other drivers don't
889 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100890 * due to I/O costs and anti-pop so handle them out of line.
891 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500892 list_for_each_entry(rtd, &card->rtd_list, list) {
893 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100894 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600895 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100896 if (bus_control) {
897 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600898 soc_resume_deferred(&card->deferred_resume_work);
899 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000900 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600901 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000902 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100903 }
Andy Green6ed25972008-06-13 16:24:05 +0100904
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200905 return 0;
906}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000907EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000909#define snd_soc_suspend NULL
910#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200911#endif
912
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100913static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800914};
915
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200916static struct snd_soc_component *soc_find_component(
917 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100918{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200919 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100920
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100921 lockdep_assert_held(&client_mutex);
922
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200923 list_for_each_entry(component, &component_list, list) {
924 if (of_node) {
925 if (component->dev->of_node == of_node)
926 return component;
927 } else if (strcmp(component->name, name) == 0) {
928 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100929 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100930 }
931
932 return NULL;
933}
934
Mengdong Linfbb88b52016-04-22 12:25:33 +0800935/**
936 * snd_soc_find_dai - Find a registered DAI
937 *
938 * @dlc: name of the DAI and optional component info to match
939 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700940 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800941 * find the DAI of the same name. The component's of_node and name
942 * should also match if being specified.
943 *
944 * Return: pointer of DAI, or NULL if not found.
945 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800946struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200947 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100948{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200949 struct snd_soc_component *component;
950 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300951 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100952
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100953 lockdep_assert_held(&client_mutex);
954
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200955 /* Find CPU DAI from registered DAIs*/
956 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300957 component_of_node = component->dev->of_node;
958 if (!component_of_node && component->dev->parent)
959 component_of_node = component->dev->parent->of_node;
960
961 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200962 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100963 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200964 continue;
965 list_for_each_entry(dai, &component->dai_list, list) {
966 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100967 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100968
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200969 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100970 }
971 }
972
973 return NULL;
974}
Mengdong Lin305e9022016-04-19 13:12:25 +0800975EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100976
Mengdong Lin17fb17552016-11-03 01:04:12 +0800977
978/**
979 * snd_soc_find_dai_link - Find a DAI link
980 *
981 * @card: soc card
982 * @id: DAI link ID to match
983 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000984 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800985 *
986 * This function will search all existing DAI links of the soc card to
987 * find the link of the same ID. Since DAI links may not have their
988 * unique ID, so name and stream name should also match if being
989 * specified.
990 *
991 * Return: pointer of DAI link, or NULL if not found.
992 */
993struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
994 int id, const char *name,
995 const char *stream_name)
996{
997 struct snd_soc_dai_link *link, *_link;
998
999 lockdep_assert_held(&client_mutex);
1000
1001 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1002 if (link->id != id)
1003 continue;
1004
1005 if (name && (!link->name || strcmp(name, link->name)))
1006 continue;
1007
1008 if (stream_name && (!link->stream_name
1009 || strcmp(stream_name, link->stream_name)))
1010 continue;
1011
1012 return link;
1013 }
1014
1015 return NULL;
1016}
1017EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1018
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001019static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1020 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001021{
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001022 struct snd_soc_pcm_runtime *rtd;
1023
1024 list_for_each_entry(rtd, &card->rtd_list, list) {
1025 if (rtd->dai_link == dai_link)
1026 return true;
1027 }
1028
1029 return false;
1030}
1031
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001032static int soc_bind_dai_link(struct snd_soc_card *card,
1033 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001034{
Mengdong Lin1a497982015-11-18 02:34:11 -05001035 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001036 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001037 struct snd_soc_dai_link_component cpu_dai_component;
Mengdong Lin1a497982015-11-18 02:34:11 -05001038 struct snd_soc_dai **codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +00001039 struct snd_soc_platform *platform;
Charles Keepax06859fc2016-11-07 13:03:17 +00001040 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +01001041 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001042 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001043
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001044 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001045
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001046 if (soc_is_dai_link_bound(card, dai_link)) {
1047 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1048 dai_link->name);
1049 return 0;
1050 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001051
Sudip Mukherjee513cb312016-02-22 14:14:31 +05301052 rtd = soc_new_pcm_runtime(card, dai_link);
1053 if (!rtd)
1054 return -ENOMEM;
1055
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001056 cpu_dai_component.name = dai_link->cpu_name;
1057 cpu_dai_component.of_node = dai_link->cpu_of_node;
1058 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1059 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001060 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001061 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001063 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001064 }
1065
Benoit Cousson88bd8702014-07-08 23:19:34 +02001066 rtd->num_codecs = dai_link->num_codecs;
1067
1068 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -05001069 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001070 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001071 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001072 if (!codec_dais[i]) {
1073 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1074 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001075 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001076 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001077 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001078
Benoit Cousson88bd8702014-07-08 23:19:34 +02001079 /* Single codec links expect codec and codec_dai in runtime data */
1080 rtd->codec_dai = codec_dais[0];
1081 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001082
Mark Brown848dd8b2011-04-27 18:16:32 +01001083 /* if there's no platform we match on the empty platform */
1084 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -07001085 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +01001086 platform_name = "snd-soc-dummy";
1087
Mark Brownb19e6e72012-03-14 21:18:39 +00001088 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089 list_for_each_entry(platform, &platform_list, list) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001090 platform_of_node = platform->dev->of_node;
1091 if (!platform_of_node && platform->dev->parent->of_node)
1092 platform_of_node = platform->dev->parent->of_node;
1093
Stephen Warren5a504962011-12-21 10:40:59 -07001094 if (dai_link->platform_of_node) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001095 if (platform_of_node != dai_link->platform_of_node)
Stephen Warren5a504962011-12-21 10:40:59 -07001096 continue;
1097 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001098 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -07001099 continue;
1100 }
Stephen Warren2610ab72011-12-07 13:58:27 -07001101
1102 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001103 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001104 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001105 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 dai_link->platform_name);
Charles Keepax70fcad42016-08-11 14:39:00 +01001107 goto _err_defer;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001108 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001109
Mengdong Lin1a497982015-11-18 02:34:11 -05001110 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +00001111 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001112
1113_err_defer:
1114 soc_free_pcm_runtime(rtd);
1115 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116}
1117
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001118static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001119{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001120 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001121 return;
Stephen Warrend12cd192012-06-08 12:34:22 -06001122
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001123 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001124
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001125 if (component->remove)
1126 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001127
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001128 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001129
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001130 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001131 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001132 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -06001133}
1134
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001135static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001136{
1137 int err;
1138
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001139 if (dai && dai->probed &&
1140 dai->driver->remove_order == order) {
1141 if (dai->driver->remove) {
1142 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001143 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001144 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001145 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001146 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001147 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001148 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001149 }
1150}
1151
Mengdong Lin1a497982015-11-18 02:34:11 -05001152static void soc_remove_link_dais(struct snd_soc_card *card,
1153 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001154{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001155 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156
1157 /* unregister the rtd device */
1158 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001159 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 rtd->dev_registered = 0;
1161 }
1162
1163 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001164 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001165 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001167 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001168}
1169
Mengdong Lin1a497982015-11-18 02:34:11 -05001170static void soc_remove_link_components(struct snd_soc_card *card,
1171 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001172{
Stephen Warren62ae68f2012-06-08 12:34:23 -06001173 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001174 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001175 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001176 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001177
1178 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001179 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001180 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001181
1182 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001183 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001184 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001185 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001186 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001187 }
1188
1189 /* remove any CPU-side CODEC */
1190 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001191 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001192 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001193 }
1194}
1195
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001196static void soc_remove_dai_links(struct snd_soc_card *card)
1197{
Mengdong Lin1a497982015-11-18 02:34:11 -05001198 int order;
1199 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001200 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001201
Liam Girdwood0168bf02011-06-07 16:08:05 +01001202 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1203 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001204 list_for_each_entry(rtd, &card->rtd_list, list)
1205 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001206 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001207
1208 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1209 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001210 list_for_each_entry(rtd, &card->rtd_list, list)
1211 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001212 }
1213
Mengdong Linf8f80362015-12-02 14:11:22 +08001214 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1215 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1216 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1217 link->name);
1218
1219 list_del(&link->list);
1220 card->num_dai_links--;
1221 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001222}
1223
Mengdong Lin923c5e612015-11-23 11:01:49 -05001224static int snd_soc_init_multicodec(struct snd_soc_card *card,
1225 struct snd_soc_dai_link *dai_link)
1226{
1227 /* Legacy codec/codec_dai link is a single entry in multicodec */
1228 if (dai_link->codec_name || dai_link->codec_of_node ||
1229 dai_link->codec_dai_name) {
1230 dai_link->num_codecs = 1;
1231
1232 dai_link->codecs = devm_kzalloc(card->dev,
1233 sizeof(struct snd_soc_dai_link_component),
1234 GFP_KERNEL);
1235 if (!dai_link->codecs)
1236 return -ENOMEM;
1237
1238 dai_link->codecs[0].name = dai_link->codec_name;
1239 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1240 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1241 }
1242
1243 if (!dai_link->codecs) {
1244 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1245 return -EINVAL;
1246 }
1247
1248 return 0;
1249}
1250
1251static int soc_init_dai_link(struct snd_soc_card *card,
1252 struct snd_soc_dai_link *link)
1253{
1254 int i, ret;
1255
1256 ret = snd_soc_init_multicodec(card, link);
1257 if (ret) {
1258 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1259 return ret;
1260 }
1261
1262 for (i = 0; i < link->num_codecs; i++) {
1263 /*
1264 * Codec must be specified by 1 of name or OF node,
1265 * not both or neither.
1266 */
1267 if (!!link->codecs[i].name ==
1268 !!link->codecs[i].of_node) {
1269 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1270 link->name);
1271 return -EINVAL;
1272 }
1273 /* Codec DAI name must be specified */
1274 if (!link->codecs[i].dai_name) {
1275 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1276 link->name);
1277 return -EINVAL;
1278 }
1279 }
1280
1281 /*
1282 * Platform may be specified by either name or OF node, but
1283 * can be left unspecified, and a dummy platform will be used.
1284 */
1285 if (link->platform_name && link->platform_of_node) {
1286 dev_err(card->dev,
1287 "ASoC: Both platform name/of_node are set for %s\n",
1288 link->name);
1289 return -EINVAL;
1290 }
1291
1292 /*
1293 * CPU device may be specified by either name or OF node, but
1294 * can be left unspecified, and will be matched based on DAI
1295 * name alone..
1296 */
1297 if (link->cpu_name && link->cpu_of_node) {
1298 dev_err(card->dev,
1299 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1300 link->name);
1301 return -EINVAL;
1302 }
1303 /*
1304 * At least one of CPU DAI name or CPU device name/node must be
1305 * specified
1306 */
1307 if (!link->cpu_dai_name &&
1308 !(link->cpu_name || link->cpu_of_node)) {
1309 dev_err(card->dev,
1310 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1311 link->name);
1312 return -EINVAL;
1313 }
1314
1315 return 0;
1316}
1317
Mengdong Linf8f80362015-12-02 14:11:22 +08001318/**
1319 * snd_soc_add_dai_link - Add a DAI link dynamically
1320 * @card: The ASoC card to which the DAI link is added
1321 * @dai_link: The new DAI link to add
1322 *
1323 * This function adds a DAI link to the ASoC card's link list.
1324 *
1325 * Note: Topology can use this API to add DAI links when probing the
1326 * topology component. And machine drivers can still define static
1327 * DAI links in dai_link array.
1328 */
1329int snd_soc_add_dai_link(struct snd_soc_card *card,
1330 struct snd_soc_dai_link *dai_link)
1331{
1332 if (dai_link->dobj.type
1333 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1334 dev_err(card->dev, "Invalid dai link type %d\n",
1335 dai_link->dobj.type);
1336 return -EINVAL;
1337 }
1338
1339 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001340 /* Notify the machine driver for extra initialization
1341 * on the link created by topology.
1342 */
1343 if (dai_link->dobj.type && card->add_dai_link)
1344 card->add_dai_link(card, dai_link);
1345
Mengdong Linf8f80362015-12-02 14:11:22 +08001346 list_add_tail(&dai_link->list, &card->dai_link_list);
1347 card->num_dai_links++;
1348
1349 return 0;
1350}
1351EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1352
1353/**
1354 * snd_soc_remove_dai_link - Remove a DAI link from the list
1355 * @card: The ASoC card that owns the link
1356 * @dai_link: The DAI link to remove
1357 *
1358 * This function removes a DAI link from the ASoC card's link list.
1359 *
1360 * For DAI links previously added by topology, topology should
1361 * remove them by using the dobj embedded in the link.
1362 */
1363void snd_soc_remove_dai_link(struct snd_soc_card *card,
1364 struct snd_soc_dai_link *dai_link)
1365{
1366 struct snd_soc_dai_link *link, *_link;
1367
1368 if (dai_link->dobj.type
1369 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1370 dev_err(card->dev, "Invalid dai link type %d\n",
1371 dai_link->dobj.type);
1372 return;
1373 }
1374
1375 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001376 /* Notify the machine driver for extra destruction
1377 * on the link created by topology.
1378 */
1379 if (dai_link->dobj.type && card->remove_dai_link)
1380 card->remove_dai_link(card, dai_link);
1381
Mengdong Linf8f80362015-12-02 14:11:22 +08001382 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1383 if (link == dai_link) {
1384 list_del(&link->list);
1385 card->num_dai_links--;
1386 return;
1387 }
1388 }
1389}
1390EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1391
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001392static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001393 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001394{
1395 int i;
1396
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001397 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001398 return;
1399
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001400 for (i = 0; i < card->num_configs; i++) {
1401 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001402 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001403 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001404 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001405 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001406 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001407 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001408 }
1409}
1410
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001411static int soc_probe_component(struct snd_soc_card *card,
1412 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001413{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001414 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001415 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001416 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001417
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001418 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001419 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001420
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001421 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001422 if (component->card != card) {
1423 dev_err(component->dev,
1424 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1425 card->name, component->card->name);
1426 return -ENODEV;
1427 }
1428 return 0;
1429 }
1430
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001431 if (!try_module_get(component->dev->driver->owner))
1432 return -ENODEV;
1433
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001434 component->card = card;
1435 dapm->card = card;
1436 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001437
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001438 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001439
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001440 if (component->dapm_widgets) {
1441 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1442 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001443
1444 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001445 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001446 "Failed to create new controls %d\n", ret);
1447 goto err_probe;
1448 }
1449 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001450
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001451 list_for_each_entry(dai, &component->dai_list, list) {
1452 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001453 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001454 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001455 "Failed to create DAI widgets %d\n", ret);
1456 goto err_probe;
1457 }
1458 }
Mark Brown888df392012-02-16 19:37:51 -08001459
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001460 if (component->probe) {
1461 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001462 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001463 dev_err(component->dev,
1464 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001465 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001466 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001467
1468 WARN(dapm->idle_bias_off &&
1469 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001470 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001471 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001472 }
1473
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001474 /* machine specific init */
1475 if (component->init) {
1476 ret = component->init(component);
1477 if (ret < 0) {
1478 dev_err(component->dev,
1479 "Failed to do machine specific init %d\n", ret);
1480 goto err_probe;
1481 }
1482 }
1483
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001484 if (component->controls)
1485 snd_soc_add_component_controls(component, component->controls,
1486 component->num_controls);
1487 if (component->dapm_routes)
1488 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1489 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001490
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001491 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001492 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001493
Jarkko Nikula70d29332011-01-27 16:24:22 +02001494 return 0;
1495
1496err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001497 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001498 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001499 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001500
1501 return ret;
1502}
1503
Mark Brown36ae1a92012-01-06 17:12:45 -08001504static void rtd_release(struct device *dev)
1505{
1506 kfree(dev);
1507}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001508
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001509static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1510 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001511{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001512 int ret = 0;
1513
Jarkko Nikula589c3562010-12-06 16:27:07 +02001514 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001515 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1516 if (!rtd->dev)
1517 return -ENOMEM;
1518 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001519 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001520 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001521 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001522 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001523 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001524 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001525 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1526 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1527 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1528 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001529 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001530 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001531 /* calling put_device() here to free the rtd->dev */
1532 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001533 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001534 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001535 return ret;
1536 }
1537 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001538 return 0;
1539}
1540
Mengdong Lin1a497982015-11-18 02:34:11 -05001541static int soc_probe_link_components(struct snd_soc_card *card,
1542 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001543 int order)
1544{
Stephen Warren62ae68f2012-06-08 12:34:23 -06001545 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001546 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001547 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001548
1549 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001550 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001551 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001552 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001553 if (ret < 0)
1554 return ret;
1555 }
1556
Benoit Cousson88bd8702014-07-08 23:19:34 +02001557 /* probe the CODEC-side components */
1558 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001559 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001560 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001561 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001562 if (ret < 0)
1563 return ret;
1564 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001565 }
1566
1567 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001568 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001569 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001570 if (ret < 0)
1571 return ret;
1572 }
1573
1574 return 0;
1575}
1576
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001577static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001578{
1579 int ret;
1580
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001581 if (!dai->probed && dai->driver->probe_order == order) {
1582 if (dai->driver->probe) {
1583 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001584 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001585 dev_err(dai->dev,
1586 "ASoC: failed to probe DAI %s: %d\n",
1587 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001588 return ret;
1589 }
1590 }
1591
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001592 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001593 }
1594
1595 return 0;
1596}
1597
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001598static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1599 struct snd_soc_pcm_runtime *rtd)
1600{
1601 int i, ret = 0;
1602
1603 for (i = 0; i < num_dais; ++i) {
1604 struct snd_soc_dai_driver *drv = dais[i]->driver;
1605
1606 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1607 ret = drv->pcm_new(rtd, dais[i]);
1608 if (ret < 0) {
1609 dev_err(dais[i]->dev,
1610 "ASoC: Failed to bind %s with pcm device\n",
1611 dais[i]->name);
1612 return ret;
1613 }
1614 }
1615
1616 return 0;
1617}
1618
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001619static int soc_link_dai_widgets(struct snd_soc_card *card,
1620 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001621 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001622{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1624 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301625 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001626 int ret;
1627
Benoit Cousson88bd8702014-07-08 23:19:34 +02001628 if (rtd->num_codecs > 1)
1629 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1630
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001631 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301632 sink = codec_dai->playback_widget;
1633 source = cpu_dai->capture_widget;
1634 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001635 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301636 dai_link->num_params,
1637 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001638 if (ret != 0) {
1639 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301640 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001641 return ret;
1642 }
1643 }
1644
Vinod Koul3de7c422015-11-09 23:19:58 +05301645 sink = cpu_dai->playback_widget;
1646 source = codec_dai->capture_widget;
1647 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001648 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301649 dai_link->num_params,
1650 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001651 if (ret != 0) {
1652 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301653 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001654 return ret;
1655 }
1656 }
1657
1658 return 0;
1659}
1660
Mengdong Lin1a497982015-11-18 02:34:11 -05001661static int soc_probe_link_dais(struct snd_soc_card *card,
1662 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663{
Mengdong Lin1a497982015-11-18 02:34:11 -05001664 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001666 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001668 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001669 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671 /* set default power off timeout */
1672 rtd->pmdown_time = pmdown_time;
1673
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001674 ret = soc_probe_dai(cpu_dai, order);
1675 if (ret)
1676 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001679 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001680 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001681 if (ret)
1682 return ret;
1683 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001684
Liam Girdwood0168bf02011-06-07 16:08:05 +01001685 /* complete DAI probe during last probe */
1686 if (order != SND_SOC_COMP_ORDER_LAST)
1687 return 0;
1688
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001689 /* do machine specific initialization */
1690 if (dai_link->init) {
1691 ret = dai_link->init(rtd);
1692 if (ret < 0) {
1693 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1694 dai_link->name, ret);
1695 return ret;
1696 }
1697 }
1698
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001699 if (dai_link->dai_fmt)
1700 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1701
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001702 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001703 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001704 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001706#ifdef CONFIG_DEBUG_FS
1707 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001708 if (dai_link->dynamic)
1709 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001710#endif
1711
Jie Yang6f0c4222015-10-13 23:41:00 +08001712 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301713 /*create compress_device"*/
Mengdong Lin1a497982015-11-18 02:34:11 -05001714 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
Mark Brownc74184e2012-04-04 22:12:09 +01001715 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001716 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301717 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001718 return ret;
1719 }
1720 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301721
1722 if (!dai_link->params) {
1723 /* create the pcm */
Mengdong Lin1a497982015-11-18 02:34:11 -05001724 ret = soc_new_pcm(rtd, rtd->num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301725 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001726 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301727 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001728 return ret;
1729 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001730 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1731 if (ret < 0)
1732 return ret;
1733 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1734 rtd->num_codecs, rtd);
1735 if (ret < 0)
1736 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301737 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001738 INIT_DELAYED_WORK(&rtd->delayed_work,
1739 codec2codec_close_delayed_work);
1740
Namarta Kohli1245b702012-08-16 17:10:41 +05301741 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001742 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001743 if (ret)
1744 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001745 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001748 return 0;
1749}
1750
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001751static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001752{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001753 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001754 struct snd_soc_component *component;
1755 const char *name;
1756 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001757
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001758 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1759 /* codecs, usually analog devices */
1760 name = aux_dev->codec_name;
1761 codec_of_node = aux_dev->codec_of_node;
1762 component = soc_find_component(codec_of_node, name);
1763 if (!component) {
1764 if (codec_of_node)
1765 name = of_node_full_name(codec_of_node);
1766 goto err_defer;
1767 }
1768 } else if (aux_dev->name) {
1769 /* generic components */
1770 name = aux_dev->name;
1771 component = soc_find_component(NULL, name);
1772 if (!component)
1773 goto err_defer;
1774 } else {
1775 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1776 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001777 }
1778
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001779 component->init = aux_dev->init;
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{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001791 struct snd_soc_component *comp;
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++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001797 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001798 if (comp->driver->probe_order == order) {
1799 ret = soc_probe_component(card, comp);
1800 if (ret < 0) {
1801 dev_err(card->dev,
1802 "ASoC: failed to probe aux component %s %d\n",
1803 comp->name, ret);
1804 return ret;
1805 }
1806 }
1807 }
1808 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001809
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001810 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001811}
1812
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001813static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001814{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001815 struct snd_soc_component *comp, *_comp;
1816 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001817
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001818 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1819 order++) {
1820 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001821 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001822
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001823 if (comp->driver->remove_order == order) {
1824 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001825 /* remove it from the card's aux_comp_list */
1826 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001827 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001828 }
1829 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001830}
1831
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001832static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001833{
1834 int ret;
1835
1836 if (codec->cache_init)
1837 return 0;
1838
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001839 ret = snd_soc_cache_init(codec);
1840 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001841 dev_err(codec->dev,
1842 "ASoC: Failed to set cache compression type: %d\n",
1843 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001844 return ret;
1845 }
1846 codec->cache_init = 1;
1847 return 0;
1848}
1849
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001850/**
1851 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1852 * @rtd: The runtime for which the DAI link format should be changed
1853 * @dai_fmt: The new DAI link format
1854 *
1855 * This function updates the DAI link format for all DAIs connected to the DAI
1856 * link for the specified runtime.
1857 *
1858 * Note: For setups with a static format set the dai_fmt field in the
1859 * corresponding snd_dai_link struct instead of using this function.
1860 *
1861 * Returns 0 on success, otherwise a negative error code.
1862 */
1863int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1864 unsigned int dai_fmt)
1865{
1866 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1867 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1868 unsigned int i;
1869 int ret;
1870
1871 for (i = 0; i < rtd->num_codecs; i++) {
1872 struct snd_soc_dai *codec_dai = codec_dais[i];
1873
1874 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1875 if (ret != 0 && ret != -ENOTSUPP) {
1876 dev_warn(codec_dai->dev,
1877 "ASoC: Failed to set DAI format: %d\n", ret);
1878 return ret;
1879 }
1880 }
1881
1882 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1883 if (cpu_dai->codec) {
1884 unsigned int inv_dai_fmt;
1885
1886 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1887 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1888 case SND_SOC_DAIFMT_CBM_CFM:
1889 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1890 break;
1891 case SND_SOC_DAIFMT_CBM_CFS:
1892 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1893 break;
1894 case SND_SOC_DAIFMT_CBS_CFM:
1895 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1896 break;
1897 case SND_SOC_DAIFMT_CBS_CFS:
1898 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1899 break;
1900 }
1901
1902 dai_fmt = inv_dai_fmt;
1903 }
1904
1905 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1906 if (ret != 0 && ret != -ENOTSUPP) {
1907 dev_warn(cpu_dai->dev,
1908 "ASoC: Failed to set DAI format: %d\n", ret);
1909 return ret;
1910 }
1911
1912 return 0;
1913}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001914EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001915
Liam Girdwood345233d2017-01-14 16:13:02 +08001916
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001917#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001918/* Trim special characters, and replace '-' with '_' since '-' is used to
1919 * separate different DMI fields in the card long name. Only number and
1920 * alphabet characters and a few separator characters are kept.
1921 */
1922static void cleanup_dmi_name(char *name)
1923{
1924 int i, j = 0;
1925
1926 for (i = 0; name[i]; i++) {
1927 if (isalnum(name[i]) || (name[i] == '.')
1928 || (name[i] == '_'))
1929 name[j++] = name[i];
1930 else if (name[i] == '-')
1931 name[j++] = '_';
1932 }
1933
1934 name[j] = '\0';
1935}
1936
1937/**
1938 * snd_soc_set_dmi_name() - Register DMI names to card
1939 * @card: The card to register DMI names
1940 * @flavour: The flavour "differentiator" for the card amongst its peers.
1941 *
1942 * An Intel machine driver may be used by many different devices but are
1943 * difficult for userspace to differentiate, since machine drivers ususally
1944 * use their own name as the card short name and leave the card long name
1945 * blank. To differentiate such devices and fix bugs due to lack of
1946 * device-specific configurations, this function allows DMI info to be used
1947 * as the sound card long name, in the format of
1948 * "vendor-product-version-board"
1949 * (Character '-' is used to separate different DMI fields here).
1950 * This will help the user space to load the device-specific Use Case Manager
1951 * (UCM) configurations for the card.
1952 *
1953 * Possible card long names may be:
1954 * DellInc.-XPS139343-01-0310JH
1955 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1956 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1957 *
1958 * This function also supports flavoring the card longname to provide
1959 * the extra differentiation, like "vendor-product-version-board-flavor".
1960 *
1961 * We only keep number and alphabet characters and a few separator characters
1962 * in the card long name since UCM in the user space uses the card long names
1963 * as card configuration directory names and AudoConf cannot support special
1964 * charactors like SPACE.
1965 *
1966 * Returns 0 on success, otherwise a negative error code.
1967 */
1968int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1969{
1970 const char *vendor, *product, *product_version, *board;
1971 size_t longname_buf_size = sizeof(card->snd_card->longname);
1972 size_t len;
1973
1974 if (card->long_name)
1975 return 0; /* long name already set by driver or from DMI */
1976
1977 /* make up dmi long name as: vendor.product.version.board */
1978 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1979 if (!vendor) {
1980 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1981 return 0;
1982 }
1983
1984 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1985 "%s", vendor);
1986 cleanup_dmi_name(card->dmi_longname);
1987
1988 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1989 if (product) {
1990 len = strlen(card->dmi_longname);
1991 snprintf(card->dmi_longname + len,
1992 longname_buf_size - len,
1993 "-%s", product);
1994
1995 len++; /* skip the separator "-" */
1996 if (len < longname_buf_size)
1997 cleanup_dmi_name(card->dmi_longname + len);
1998
1999 /* some vendors like Lenovo may only put a self-explanatory
2000 * name in the product version field
2001 */
2002 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2003 if (product_version) {
2004 len = strlen(card->dmi_longname);
2005 snprintf(card->dmi_longname + len,
2006 longname_buf_size - len,
2007 "-%s", product_version);
2008
2009 len++;
2010 if (len < longname_buf_size)
2011 cleanup_dmi_name(card->dmi_longname + len);
2012 }
2013 }
2014
2015 board = dmi_get_system_info(DMI_BOARD_NAME);
2016 if (board) {
2017 len = strlen(card->dmi_longname);
2018 snprintf(card->dmi_longname + len,
2019 longname_buf_size - len,
2020 "-%s", board);
2021
2022 len++;
2023 if (len < longname_buf_size)
2024 cleanup_dmi_name(card->dmi_longname + len);
2025 } else if (!product) {
2026 /* fall back to using legacy name */
2027 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2028 return 0;
2029 }
2030
2031 /* Add flavour to dmi long name */
2032 if (flavour) {
2033 len = strlen(card->dmi_longname);
2034 snprintf(card->dmi_longname + len,
2035 longname_buf_size - len,
2036 "-%s", flavour);
2037
2038 len++;
2039 if (len < longname_buf_size)
2040 cleanup_dmi_name(card->dmi_longname + len);
2041 }
2042
2043 /* set the card long name */
2044 card->long_name = card->dmi_longname;
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02002049#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08002050
Mark Brownb19e6e72012-03-14 21:18:39 +00002051static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002052{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002053 struct snd_soc_codec *codec;
Mengdong Lin1a497982015-11-18 02:34:11 -05002054 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002055 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002056 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002058 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002059 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002060
Mark Brownb19e6e72012-03-14 21:18:39 +00002061 /* bind DAIs */
2062 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002063 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002064 if (ret != 0)
2065 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002066 }
2067
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002068 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002069 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002070 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002071 if (ret != 0)
2072 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002073 }
2074
Mengdong Linf8f80362015-12-02 14:11:22 +08002075 /* add predefined DAI links to the list */
2076 for (i = 0; i < card->num_links; i++)
2077 snd_soc_add_dai_link(card, card->dai_link+i);
2078
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002079 /* initialize the register cache for each available codec */
2080 list_for_each_entry(codec, &codec_list, list) {
2081 if (codec->cache_init)
2082 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02002083 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00002084 if (ret < 0)
2085 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002086 }
2087
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002089 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002090 card->owner, 0, &card->snd_card);
2091 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002092 dev_err(card->dev,
2093 "ASoC: can't create sound card for card %s: %d\n",
2094 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002095 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002098 soc_init_card_debugfs(card);
2099
Mark Browne37a4972011-03-02 18:21:57 +00002100 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2101 card->dapm.dev = card->dev;
2102 card->dapm.card = card;
2103 list_add(&card->dapm.list, &card->dapm_list);
2104
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002105#ifdef CONFIG_DEBUG_FS
2106 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2107#endif
2108
Mark Brown88ee1c62011-02-02 10:43:26 +00002109#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002110 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002111 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002112#endif
Andy Green6ed25972008-06-13 16:24:05 +01002113
Mark Brown9a841eb2011-04-12 17:51:37 -07002114 if (card->dapm_widgets)
2115 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2116 card->num_dapm_widgets);
2117
Nicolin Chenf23e8602015-02-14 17:22:49 -08002118 if (card->of_dapm_widgets)
2119 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2120 card->num_of_dapm_widgets);
2121
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002122 /* initialise the sound card only once */
2123 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002124 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002125 if (ret < 0)
2126 goto card_probe_error;
2127 }
2128
Stephen Warren62ae68f2012-06-08 12:34:23 -06002129 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002130 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2131 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002132 list_for_each_entry(rtd, &card->rtd_list, list) {
2133 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002134 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002135 dev_err(card->dev,
2136 "ASoC: failed to instantiate card %d\n",
2137 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002138 goto probe_dai_err;
2139 }
2140 }
2141 }
2142
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002143 /* probe auxiliary components */
2144 ret = soc_probe_aux_devices(card);
2145 if (ret < 0)
2146 goto probe_dai_err;
2147
Mengdong Lin61b00882015-12-02 14:11:48 +08002148 /* Find new DAI links added during probing components and bind them.
2149 * Components with topology may bring new DAIs and DAI links.
2150 */
2151 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2152 if (soc_is_dai_link_bound(card, dai_link))
2153 continue;
2154
2155 ret = soc_init_dai_link(card, dai_link);
2156 if (ret)
2157 goto probe_dai_err;
2158 ret = soc_bind_dai_link(card, dai_link);
2159 if (ret)
2160 goto probe_dai_err;
2161 }
2162
Stephen Warren62ae68f2012-06-08 12:34:23 -06002163 /* probe all DAI links on this card */
2164 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2165 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002166 list_for_each_entry(rtd, &card->rtd_list, list) {
2167 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002168 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002169 dev_err(card->dev,
2170 "ASoC: failed to instantiate card %d\n",
2171 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002172 goto probe_dai_err;
2173 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002174 }
2175 }
2176
Mark Brown888df392012-02-16 19:37:51 -08002177 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002178 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002179
Mark Brownb7af1da2011-04-07 19:18:44 +09002180 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002181 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002182
Mark Brownb8ad29d2011-03-02 18:35:51 +00002183 if (card->dapm_routes)
2184 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2185 card->num_dapm_routes);
2186
Nicolin Chenf23e8602015-02-14 17:22:49 -08002187 if (card->of_dapm_routes)
2188 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2189 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002190
Takashi Iwai861886d2017-04-24 08:54:42 +02002191 /* try to set some sane longname if DMI is available */
2192 snd_soc_set_dmi_name(card, NULL);
2193
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002194 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002195 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002196 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2197 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002198 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2199 "%s", card->driver_name ? card->driver_name : card->name);
2200 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2201 switch (card->snd_card->driver[i]) {
2202 case '_':
2203 case '-':
2204 case '\0':
2205 break;
2206 default:
2207 if (!isalnum(card->snd_card->driver[i]))
2208 card->snd_card->driver[i] = '_';
2209 break;
2210 }
2211 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002212
Mark Brown28e9ad92011-03-02 18:36:34 +00002213 if (card->late_probe) {
2214 ret = card->late_probe(card);
2215 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002216 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002217 card->name, ret);
2218 goto probe_aux_dev_err;
2219 }
2220 }
2221
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002222 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002224 ret = snd_card_register(card->snd_card);
2225 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002226 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2227 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002228 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002229 }
2230
Mark Brown435c5e22008-12-04 15:32:53 +00002231 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002232 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002233 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002234 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002235
2236 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002237
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002238probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002239 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002240
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002241probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002242 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002243
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002244card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002245 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002246 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002247
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002248 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002249 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002250 snd_card_free(card->snd_card);
2251
Mark Brownb19e6e72012-03-14 21:18:39 +00002252base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002253 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002255 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256
Mark Brownb19e6e72012-03-14 21:18:39 +00002257 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002258}
2259
2260/* probes a new socdev */
2261static int soc_probe(struct platform_device *pdev)
2262{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002263 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002264
Vinod Koul70a7ca32011-01-14 19:22:48 +05302265 /*
2266 * no card, so machine driver should be registering card
2267 * we should not be here in that case so ret error
2268 */
2269 if (!card)
2270 return -EINVAL;
2271
Mark Brownfe4085e2012-03-02 13:07:41 +00002272 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002273 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002274 card->name);
2275
Mark Brown435c5e22008-12-04 15:32:53 +00002276 /* Bodge while we unpick instantiation */
2277 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002278
Mark Brown28d528c2012-08-09 18:45:23 +01002279 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280}
2281
Vinod Koulb0e26482011-01-13 22:48:02 +05302282static int soc_cleanup_card_resources(struct snd_soc_card *card)
2283{
Mengdong Lin1a497982015-11-18 02:34:11 -05002284 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302285
2286 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002287 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002288 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302289
Vinod Koulb0e26482011-01-13 22:48:02 +05302290 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002291 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002292 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302293
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002294 /* remove auxiliary devices */
2295 soc_remove_aux_devices(card);
2296
Mark Brownd1e81422016-08-18 19:32:59 +01002297 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302298 soc_cleanup_card_debugfs(card);
2299
2300 /* remove the card */
2301 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002302 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302303
Vinod Koulb0e26482011-01-13 22:48:02 +05302304 snd_card_free(card->snd_card);
2305 return 0;
2306
2307}
2308
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309/* removes a socdev */
2310static int soc_remove(struct platform_device *pdev)
2311{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002312 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002313
Mark Brownc5af3a22008-11-28 13:29:45 +00002314 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315 return 0;
2316}
2317
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002318int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002319{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002320 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002321 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002322
2323 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002324 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002325
2326 /* Flush out pmdown_time work - we actually do want to run it
2327 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002328 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002329 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002330
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002331 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002332
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002333 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002334 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002335 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002336 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002337
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002338 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002339 for (i = 0; i < rtd->num_codecs; i++) {
2340 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002341 pinctrl_pm_select_sleep_state(codec_dai->dev);
2342 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002343 }
2344
Mark Brown416356f2009-06-30 19:05:15 +01002345 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002346}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002347EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002348
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002349const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302350 .suspend = snd_soc_suspend,
2351 .resume = snd_soc_resume,
2352 .freeze = snd_soc_suspend,
2353 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002354 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302355 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002356};
Stephen Warrendeb26072011-04-05 19:35:30 -06002357EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002358
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359/* ASoC platform driver */
2360static struct platform_driver soc_driver = {
2361 .driver = {
2362 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002363 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002364 },
2365 .probe = soc_probe,
2366 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367};
2368
Mark Brown096e49d2009-07-05 15:12:22 +01002369/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 * snd_soc_cnew - create new control
2371 * @_template: control template
2372 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002373 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002374 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 *
2376 * Create a new mixer control from a template control.
2377 *
2378 * Returns 0 for success, else error.
2379 */
2380struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002381 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002382 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383{
2384 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002385 struct snd_kcontrol *kcontrol;
2386 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387
2388 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389 template.index = 0;
2390
Mark Brownefb7ac32011-03-08 17:23:24 +00002391 if (!long_name)
2392 long_name = template.name;
2393
2394 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002395 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002396 if (!name)
2397 return NULL;
2398
Mark Brownefb7ac32011-03-08 17:23:24 +00002399 template.name = name;
2400 } else {
2401 template.name = long_name;
2402 }
2403
2404 kcontrol = snd_ctl_new1(&template, data);
2405
2406 kfree(name);
2407
2408 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002409}
2410EXPORT_SYMBOL_GPL(snd_soc_cnew);
2411
Liam Girdwood022658b2012-02-03 17:43:09 +00002412static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2413 const struct snd_kcontrol_new *controls, int num_controls,
2414 const char *prefix, void *data)
2415{
2416 int err, i;
2417
2418 for (i = 0; i < num_controls; i++) {
2419 const struct snd_kcontrol_new *control = &controls[i];
2420 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2421 control->name, prefix));
2422 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002423 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2424 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002425 return err;
2426 }
2427 }
2428
2429 return 0;
2430}
2431
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002432struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2433 const char *name)
2434{
2435 struct snd_card *card = soc_card->snd_card;
2436 struct snd_kcontrol *kctl;
2437
2438 if (unlikely(!name))
2439 return NULL;
2440
2441 list_for_each_entry(kctl, &card->controls, list)
2442 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2443 return kctl;
2444 return NULL;
2445}
2446EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2447
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002448/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002449 * snd_soc_add_component_controls - Add an array of controls to a component.
2450 *
2451 * @component: Component to add controls to
2452 * @controls: Array of controls to add
2453 * @num_controls: Number of elements in the array
2454 *
2455 * Return: 0 for success, else error.
2456 */
2457int snd_soc_add_component_controls(struct snd_soc_component *component,
2458 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2459{
2460 struct snd_card *card = component->card->snd_card;
2461
2462 return snd_soc_add_controls(card, component->dev, controls,
2463 num_controls, component->name_prefix, component);
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2466
2467/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002468 * snd_soc_add_codec_controls - add an array of controls to a codec.
2469 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002470 * duplicating this code.
2471 *
2472 * @codec: codec to add controls to
2473 * @controls: array of controls to add
2474 * @num_controls: number of elements in the array
2475 *
2476 * Return 0 for success, else error.
2477 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002478int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002479 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002480{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002481 return snd_soc_add_component_controls(&codec->component, controls,
2482 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002483}
Liam Girdwood022658b2012-02-03 17:43:09 +00002484EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002485
2486/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002487 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002488 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002489 *
2490 * @platform: platform to add controls to
2491 * @controls: array of controls to add
2492 * @num_controls: number of elements in the array
2493 *
2494 * Return 0 for success, else error.
2495 */
2496int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002497 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002498{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002499 return snd_soc_add_component_controls(&platform->component, controls,
2500 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002501}
2502EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2503
2504/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002505 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2506 * Convenience function to add a list of controls.
2507 *
2508 * @soc_card: SoC card to add controls to
2509 * @controls: array of controls to add
2510 * @num_controls: number of elements in the array
2511 *
2512 * Return 0 for success, else error.
2513 */
2514int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2515 const struct snd_kcontrol_new *controls, int num_controls)
2516{
2517 struct snd_card *card = soc_card->snd_card;
2518
2519 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2520 NULL, soc_card);
2521}
2522EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2523
2524/**
2525 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2526 * Convienience function to add a list of controls.
2527 *
2528 * @dai: DAI to add controls to
2529 * @controls: array of controls to add
2530 * @num_controls: number of elements in the array
2531 *
2532 * Return 0 for success, else error.
2533 */
2534int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2535 const struct snd_kcontrol_new *controls, int num_controls)
2536{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002537 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002538
2539 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2540 NULL, dai);
2541}
2542EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2543
2544/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002545 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2546 * @dai: DAI
2547 * @clk_id: DAI specific clock ID
2548 * @freq: new clock frequency in Hz
2549 * @dir: new clock direction - input/output.
2550 *
2551 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2552 */
2553int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2554 unsigned int freq, int dir)
2555{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002556 if (dai->driver && dai->driver->ops->set_sysclk)
2557 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002558 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002559 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002560 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002561 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002562 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002563}
2564EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2565
2566/**
Mark Brownec4ee522011-03-07 20:58:11 +00002567 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2568 * @codec: CODEC
2569 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002570 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002571 * @freq: new clock frequency in Hz
2572 * @dir: new clock direction - input/output.
2573 *
2574 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2575 */
2576int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002577 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002578{
2579 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002580 return codec->driver->set_sysclk(codec, clk_id, source,
2581 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002582 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002583 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002584}
2585EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2586
2587/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002588 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2589 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002590 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002591 * @div: new clock divisor.
2592 *
2593 * Configures the clock dividers. This is used to derive the best DAI bit and
2594 * frame clocks from the system or master clock. It's best to set the DAI bit
2595 * and frame clocks as low as possible to save system power.
2596 */
2597int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2598 int div_id, int div)
2599{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002600 if (dai->driver && dai->driver->ops->set_clkdiv)
2601 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002602 else
2603 return -EINVAL;
2604}
2605EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2606
2607/**
2608 * snd_soc_dai_set_pll - configure DAI PLL.
2609 * @dai: DAI
2610 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002611 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002612 * @freq_in: PLL input clock frequency in Hz
2613 * @freq_out: requested PLL output clock frequency in Hz
2614 *
2615 * Configures and enables PLL to generate output clock based on input clock.
2616 */
Mark Brown85488032009-09-05 18:52:16 +01002617int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2618 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002619{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002620 if (dai->driver && dai->driver->ops->set_pll)
2621 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002622 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002623 else if (dai->codec && dai->codec->driver->set_pll)
2624 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2625 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002626 else
2627 return -EINVAL;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2630
Mark Brownec4ee522011-03-07 20:58:11 +00002631/*
2632 * snd_soc_codec_set_pll - configure codec PLL.
2633 * @codec: CODEC
2634 * @pll_id: DAI specific PLL ID
2635 * @source: DAI specific source for the PLL
2636 * @freq_in: PLL input clock frequency in Hz
2637 * @freq_out: requested PLL output clock frequency in Hz
2638 *
2639 * Configures and enables PLL to generate output clock based on input clock.
2640 */
2641int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2642 unsigned int freq_in, unsigned int freq_out)
2643{
2644 if (codec->driver->set_pll)
2645 return codec->driver->set_pll(codec, pll_id, source,
2646 freq_in, freq_out);
2647 else
2648 return -EINVAL;
2649}
2650EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2651
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002652/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002653 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2654 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002655 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002656 *
2657 * Configures the DAI for a preset BCLK to sample rate ratio.
2658 */
2659int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2660{
2661 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2662 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2663 else
2664 return -EINVAL;
2665}
2666EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2667
2668/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002669 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2670 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002671 * @fmt: SND_SOC_DAIFMT_ format value.
2672 *
2673 * Configures the DAI hardware format and clocking.
2674 */
2675int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2676{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002677 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002678 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002679 if (dai->driver->ops->set_fmt == NULL)
2680 return -ENOTSUPP;
2681 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002682}
2683EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2684
2685/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002686 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002687 * @slots: Number of slots in use.
2688 * @tx_mask: bitmask representing active TX slots.
2689 * @rx_mask: bitmask representing active RX slots.
2690 *
2691 * Generates the TDM tx and rx slot default masks for DAI.
2692 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002693static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002694 unsigned int *tx_mask,
2695 unsigned int *rx_mask)
2696{
2697 if (*tx_mask || *rx_mask)
2698 return 0;
2699
2700 if (!slots)
2701 return -EINVAL;
2702
2703 *tx_mask = (1 << slots) - 1;
2704 *rx_mask = (1 << slots) - 1;
2705
2706 return 0;
2707}
2708
2709/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002710 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2711 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002712 * @tx_mask: bitmask representing active TX slots.
2713 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002714 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002715 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002716 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002717 * This function configures the specified DAI for TDM operation. @slot contains
2718 * the total number of slots of the TDM stream and @slot_with the width of each
2719 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2720 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2721 * DAI should write to or read from. If a bit is set the corresponding slot is
2722 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2723 * the first slot, bit 1 to the second slot and so on. The first active slot
2724 * maps to the first channel of the DAI, the second active slot to the second
2725 * channel and so on.
2726 *
2727 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2728 * @rx_mask and @slot_width will be ignored.
2729 *
2730 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002731 */
2732int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002733 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002734{
Xiubo Lie5c21512014-03-21 14:17:12 +08002735 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2736 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002737 &tx_mask, &rx_mask);
2738 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002739 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002740
Benoit Cousson88bd8702014-07-08 23:19:34 +02002741 dai->tx_mask = tx_mask;
2742 dai->rx_mask = rx_mask;
2743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002744 if (dai->driver && dai->driver->ops->set_tdm_slot)
2745 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002746 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002747 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002748 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002749}
2750EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2751
2752/**
Barry Song472df3c2009-09-12 01:16:29 +08002753 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2754 * @dai: DAI
2755 * @tx_num: how many TX channels
2756 * @tx_slot: pointer to an array which imply the TX slot number channel
2757 * 0~num-1 uses
2758 * @rx_num: how many RX channels
2759 * @rx_slot: pointer to an array which imply the RX slot number channel
2760 * 0~num-1 uses
2761 *
2762 * configure the relationship between channel number and TDM slot number.
2763 */
2764int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2765 unsigned int tx_num, unsigned int *tx_slot,
2766 unsigned int rx_num, unsigned int *rx_slot)
2767{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002768 if (dai->driver && dai->driver->ops->set_channel_map)
2769 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002770 rx_num, rx_slot);
2771 else
2772 return -EINVAL;
2773}
2774EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2775
2776/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002777 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2778 * @dai: DAI
2779 * @tristate: tristate enable
2780 *
2781 * Tristates the DAI so that others can use it.
2782 */
2783int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2784{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002785 if (dai->driver && dai->driver->ops->set_tristate)
2786 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002787 else
2788 return -EINVAL;
2789}
2790EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2791
2792/**
2793 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2794 * @dai: DAI
2795 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002796 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002797 *
2798 * Mutes the DAI DAC.
2799 */
Mark Brownda183962013-02-06 15:44:07 +00002800int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2801 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002802{
Mark Brownda183962013-02-06 15:44:07 +00002803 if (!dai->driver)
2804 return -ENOTSUPP;
2805
2806 if (dai->driver->ops->mute_stream)
2807 return dai->driver->ops->mute_stream(dai, mute, direction);
2808 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2809 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002810 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002811 else
Mark Brown04570c62012-04-13 19:16:03 +01002812 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002813}
2814EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2815
Mark Brownc5af3a22008-11-28 13:29:45 +00002816/**
2817 * snd_soc_register_card - Register a card with the ASoC core
2818 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002819 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002820 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002821 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302822int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002823{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002824 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002825 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002826
Mark Brownc5af3a22008-11-28 13:29:45 +00002827 if (!card->name || !card->dev)
2828 return -EINVAL;
2829
Stephen Warren5a504962011-12-21 10:40:59 -07002830 for (i = 0; i < card->num_links; i++) {
2831 struct snd_soc_dai_link *link = &card->dai_link[i];
2832
Mengdong Lin923c5e612015-11-23 11:01:49 -05002833 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002834 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002835 dev_err(card->dev, "ASoC: failed to init link %s\n",
2836 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002837 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002838 }
Stephen Warren5a504962011-12-21 10:40:59 -07002839 }
2840
Mark Browned77cc12011-05-03 18:25:34 +01002841 dev_set_drvdata(card->dev, card);
2842
Stephen Warren111c6412011-01-28 14:26:35 -07002843 snd_soc_initialize_card_lists(card);
2844
Mengdong Linf8f80362015-12-02 14:11:22 +08002845 INIT_LIST_HEAD(&card->dai_link_list);
2846 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002847
Mengdong Lin1a497982015-11-18 02:34:11 -05002848 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002849 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002850
Mark Browndb432b42011-10-03 21:06:40 +01002851 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002852 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002853 card->instantiated = 0;
2854 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002855 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002856
Mark Brownb19e6e72012-03-14 21:18:39 +00002857 ret = snd_soc_instantiate_card(card);
2858 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002859 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002860
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002861 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002862 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002863 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2864 int j;
2865
2866 for (j = 0; j < rtd->num_codecs; j++) {
2867 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2868 if (!codec_dai->active)
2869 pinctrl_pm_select_sleep_state(codec_dai->dev);
2870 }
2871
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002872 if (!cpu_dai->active)
2873 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2874 }
2875
Mark Brownb19e6e72012-03-14 21:18:39 +00002876 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002877}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302878EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002879
2880/**
2881 * snd_soc_unregister_card - Unregister a card with the ASoC core
2882 *
2883 * @card: Card to unregister
2884 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002885 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302886int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002887{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002888 if (card->instantiated) {
2889 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002890 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302891 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002892 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002893 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002894
2895 return 0;
2896}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302897EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002898
2899/*
2900 * Simplify DAI link configuration by removing ".-1" from device names
2901 * and sanitizing names.
2902 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002903static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002904{
2905 char *found, name[NAME_SIZE];
2906 int id1, id2;
2907
2908 if (dev_name(dev) == NULL)
2909 return NULL;
2910
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002911 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002912
2913 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002914 found = strstr(name, dev->driver->name);
2915 if (found) {
2916 /* get ID */
2917 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2918
2919 /* discard ID from name if ID == -1 */
2920 if (*id == -1)
2921 found[strlen(dev->driver->name)] = '\0';
2922 }
2923
2924 } else {
2925 /* I2C component devices are named "bus-addr" */
2926 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2927 char tmp[NAME_SIZE];
2928
2929 /* create unique ID number from I2C addr and bus */
2930 *id = ((id1 & 0xffff) << 16) + id2;
2931
2932 /* sanitize component name for DAI link creation */
2933 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002934 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002935 } else
2936 *id = 0;
2937 }
2938
2939 return kstrdup(name, GFP_KERNEL);
2940}
2941
2942/*
2943 * Simplify DAI link naming for single devices with multiple DAIs by removing
2944 * any ".-1" and using the DAI name (instead of device name).
2945 */
2946static inline char *fmt_multiple_name(struct device *dev,
2947 struct snd_soc_dai_driver *dai_drv)
2948{
2949 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002950 dev_err(dev,
2951 "ASoC: error - multiple DAI %s registered with no name\n",
2952 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002953 return NULL;
2954 }
2955
2956 return kstrdup(dai_drv->name, GFP_KERNEL);
2957}
2958
2959/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002960 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002961 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002962 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002963 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002964static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002965{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002966 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002967
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002968 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002969 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2970 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002971 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002972 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002973 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002974 }
Mark Brown91151712008-11-30 23:31:24 +00002975}
Mark Brown91151712008-11-30 23:31:24 +00002976
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002977/* Create a DAI and add it to the component's DAI list */
2978static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2979 struct snd_soc_dai_driver *dai_drv,
2980 bool legacy_dai_naming)
2981{
2982 struct device *dev = component->dev;
2983 struct snd_soc_dai *dai;
2984
2985 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2986
2987 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2988 if (dai == NULL)
2989 return NULL;
2990
2991 /*
2992 * Back in the old days when we still had component-less DAIs,
2993 * instead of having a static name, component-less DAIs would
2994 * inherit the name of the parent device so it is possible to
2995 * register multiple instances of the DAI. We still need to keep
2996 * the same naming style even though those DAIs are not
2997 * component-less anymore.
2998 */
2999 if (legacy_dai_naming &&
3000 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3001 dai->name = fmt_single_name(dev, &dai->id);
3002 } else {
3003 dai->name = fmt_multiple_name(dev, dai_drv);
3004 if (dai_drv->id)
3005 dai->id = dai_drv->id;
3006 else
3007 dai->id = component->num_dai;
3008 }
3009 if (dai->name == NULL) {
3010 kfree(dai);
3011 return NULL;
3012 }
3013
3014 dai->component = component;
3015 dai->dev = dev;
3016 dai->driver = dai_drv;
3017 if (!dai->driver->ops)
3018 dai->driver->ops = &null_dai_ops;
3019
3020 list_add(&dai->list, &component->dai_list);
3021 component->num_dai++;
3022
3023 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3024 return dai;
3025}
3026
Mark Brown91151712008-11-30 23:31:24 +00003027/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003028 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003029 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003030 * @component: The component the DAIs are registered for
3031 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003032 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003033 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3034 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003035 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003036static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003037 struct snd_soc_dai_driver *dai_drv, size_t count,
3038 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003039{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003040 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003041 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003042 unsigned int i;
3043 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003044
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003045 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003046
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003047 component->dai_drv = dai_drv;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003048
Mark Brown91151712008-11-30 23:31:24 +00003049 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003050
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003051 dai = soc_add_dai(component, dai_drv + i,
3052 count == 1 && legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003053 if (dai == NULL) {
3054 ret = -ENOMEM;
3055 goto err;
3056 }
Mark Brown91151712008-11-30 23:31:24 +00003057 }
3058
3059 return 0;
3060
3061err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003062 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003063
3064 return ret;
3065}
Mark Brown91151712008-11-30 23:31:24 +00003066
Mengdong Lin68003e62015-12-31 16:40:43 +08003067/**
3068 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3069 *
3070 * @component: The component the DAIs are registered for
3071 * @dai_drv: DAI driver to use for the DAI
3072 *
3073 * Topology can use this API to register DAIs when probing a component.
3074 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3075 * will be freed in the component cleanup.
3076 */
3077int snd_soc_register_dai(struct snd_soc_component *component,
3078 struct snd_soc_dai_driver *dai_drv)
3079{
3080 struct snd_soc_dapm_context *dapm =
3081 snd_soc_component_get_dapm(component);
3082 struct snd_soc_dai *dai;
3083 int ret;
3084
3085 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3086 dev_err(component->dev, "Invalid dai type %d\n",
3087 dai_drv->dobj.type);
3088 return -EINVAL;
3089 }
3090
3091 lockdep_assert_held(&client_mutex);
3092 dai = soc_add_dai(component, dai_drv, false);
3093 if (!dai)
3094 return -ENOMEM;
3095
3096 /* Create the DAI widgets here. After adding DAIs, topology may
3097 * also add routes that need these widgets as source or sink.
3098 */
3099 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3100 if (ret != 0) {
3101 dev_err(component->dev,
3102 "Failed to create DAI widgets %d\n", ret);
3103 }
3104
3105 return ret;
3106}
3107EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3108
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003109static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3110 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003111{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003112 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003113
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003114 component->driver->seq_notifier(component, type, subseq);
3115}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003116
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003117static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3118 int event)
3119{
3120 struct snd_soc_component *component = dapm->component;
3121
3122 return component->driver->stream_event(component, event);
3123}
3124
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003125static int snd_soc_component_initialize(struct snd_soc_component *component,
3126 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003127{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003128 struct snd_soc_dapm_context *dapm;
3129
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003130 component->name = fmt_single_name(dev, &component->id);
3131 if (!component->name) {
3132 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003133 return -ENOMEM;
3134 }
3135
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003136 component->dev = dev;
3137 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003138 component->probe = component->driver->probe;
3139 component->remove = component->driver->remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003140 component->suspend = component->driver->suspend;
3141 component->resume = component->driver->resume;
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003142 component->pcm_new = component->driver->pcm_new;
Adrian Dinuec5a82d2017-02-25 13:23:00 +02003143 component->pcm_free = component->driver->pcm_free;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003144
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003145 dapm = &component->dapm;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003146 dapm->dev = dev;
3147 dapm->component = component;
3148 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003149 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003150 if (driver->seq_notifier)
3151 dapm->seq_notifier = snd_soc_component_seq_notifier;
3152 if (driver->stream_event)
3153 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003154
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003155 component->controls = driver->controls;
3156 component->num_controls = driver->num_controls;
3157 component->dapm_widgets = driver->dapm_widgets;
3158 component->num_dapm_widgets = driver->num_dapm_widgets;
3159 component->dapm_routes = driver->dapm_routes;
3160 component->num_dapm_routes = driver->num_dapm_routes;
3161
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003162 INIT_LIST_HEAD(&component->dai_list);
3163 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003164
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003165 return 0;
3166}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003167
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003168static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003169{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003170 int val_bytes = regmap_get_val_bytes(component->regmap);
3171
3172 /* Errors are legitimate for non-integer byte multiples */
3173 if (val_bytes > 0)
3174 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003175}
3176
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003177#ifdef CONFIG_REGMAP
3178
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003179/**
3180 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3181 * @component: The component for which to initialize the regmap instance
3182 * @regmap: The regmap instance that should be used by the component
3183 *
3184 * This function allows deferred assignment of the regmap instance that is
3185 * associated with the component. Only use this if the regmap instance is not
3186 * yet ready when the component is registered. The function must also be called
3187 * before the first IO attempt of the component.
3188 */
3189void snd_soc_component_init_regmap(struct snd_soc_component *component,
3190 struct regmap *regmap)
3191{
3192 component->regmap = regmap;
3193 snd_soc_component_setup_regmap(component);
3194}
3195EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3196
3197/**
3198 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3199 * @component: The component for which to de-initialize the regmap instance
3200 *
3201 * Calls regmap_exit() on the regmap instance associated to the component and
3202 * removes the regmap instance from the component.
3203 *
3204 * This function should only be used if snd_soc_component_init_regmap() was used
3205 * to initialize the regmap instance.
3206 */
3207void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3208{
3209 regmap_exit(component->regmap);
3210 component->regmap = NULL;
3211}
3212EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3213
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003214#endif
3215
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003216static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3217{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003218 if (!component->write && !component->read) {
3219 if (!component->regmap)
3220 component->regmap = dev_get_regmap(component->dev, NULL);
3221 if (component->regmap)
3222 snd_soc_component_setup_regmap(component);
3223 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003224
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003225 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003226 INIT_LIST_HEAD(&component->dobj_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003227}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003228
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003229static void snd_soc_component_add(struct snd_soc_component *component)
3230{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003231 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003232 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003233 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003234}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003235
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003236static void snd_soc_component_cleanup(struct snd_soc_component *component)
3237{
3238 snd_soc_unregister_dais(component);
3239 kfree(component->name);
3240}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003241
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003242static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3243{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003244 struct snd_soc_card *card = component->card;
3245
3246 if (card)
3247 snd_soc_unregister_card(card);
3248
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003249 list_del(&component->list);
3250}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003251
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003252int snd_soc_register_component(struct device *dev,
3253 const struct snd_soc_component_driver *cmpnt_drv,
3254 struct snd_soc_dai_driver *dai_drv,
3255 int num_dai)
3256{
3257 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003258 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003259
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003260 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003261 if (!cmpnt) {
3262 dev_err(dev, "ASoC: Failed to allocate memory\n");
3263 return -ENOMEM;
3264 }
3265
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003266 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3267 if (ret)
3268 goto err_free;
3269
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003270 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003271 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003272
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003273 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3274 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003275 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003276 goto err_cleanup;
3277 }
3278
3279 snd_soc_component_add(cmpnt);
3280
3281 return 0;
3282
3283err_cleanup:
3284 snd_soc_component_cleanup(cmpnt);
3285err_free:
3286 kfree(cmpnt);
3287 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003288}
3289EXPORT_SYMBOL_GPL(snd_soc_register_component);
3290
3291/**
3292 * snd_soc_unregister_component - Unregister a component from the ASoC core
3293 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003294 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003295 */
3296void snd_soc_unregister_component(struct device *dev)
3297{
3298 struct snd_soc_component *cmpnt;
3299
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003300 mutex_lock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003301 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003302 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003303 goto found;
3304 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003305 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003306 return;
3307
3308found:
Liam Girdwood8a978232015-05-29 19:06:14 +01003309 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003310 snd_soc_component_del_unlocked(cmpnt);
3311 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003312 snd_soc_component_cleanup(cmpnt);
3313 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003314}
3315EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3316
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003317static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003318{
3319 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3320
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003321 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003322}
3323
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003324static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003325{
3326 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3327
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003328 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003329}
3330
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003331static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
3332{
3333 struct snd_soc_platform *platform = rtd->platform;
3334
Brian Norrisd6c098a2017-03-08 15:18:54 -08003335 if (platform->driver->pcm_new)
3336 return platform->driver->pcm_new(rtd);
3337 else
3338 return 0;
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003339}
3340
3341static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
3342{
3343 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
3344 struct snd_soc_platform *platform = rtd->platform;
3345
Brian Norrisd6c098a2017-03-08 15:18:54 -08003346 if (platform->driver->pcm_free)
3347 platform->driver->pcm_free(pcm);
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003348}
3349
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003350/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003351 * snd_soc_add_platform - Add a platform to the ASoC core
3352 * @dev: The parent device for the platform
3353 * @platform: The platform to add
Masanari Iida7d1442b2015-07-15 23:02:39 +09003354 * @platform_drv: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003355 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003356int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01003357 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003358{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003359 int ret;
3360
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003361 ret = snd_soc_component_initialize(&platform->component,
3362 &platform_drv->component_driver, dev);
3363 if (ret)
3364 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003365
3366 platform->dev = dev;
3367 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003368
3369 if (platform_drv->probe)
3370 platform->component.probe = snd_soc_platform_drv_probe;
3371 if (platform_drv->remove)
3372 platform->component.remove = snd_soc_platform_drv_remove;
Kuninori Morimoto99b04f4c2016-12-15 08:41:38 +00003373 if (platform_drv->pcm_new)
3374 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3375 if (platform_drv->pcm_free)
3376 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003377
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003378#ifdef CONFIG_DEBUG_FS
3379 platform->component.debugfs_prefix = "platform";
3380#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00003381
3382 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003383 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003384 list_add(&platform->list, &platform_list);
3385 mutex_unlock(&client_mutex);
3386
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003387 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3388 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003389
3390 return 0;
3391}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003392EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3393
3394/**
3395 * snd_soc_register_platform - Register a platform with the ASoC core
3396 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003397 * @dev: The device for the platform
3398 * @platform_drv: The driver for the platform
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003399 */
3400int snd_soc_register_platform(struct device *dev,
3401 const struct snd_soc_platform_driver *platform_drv)
3402{
3403 struct snd_soc_platform *platform;
3404 int ret;
3405
3406 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3407
3408 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3409 if (platform == NULL)
3410 return -ENOMEM;
3411
3412 ret = snd_soc_add_platform(dev, platform, platform_drv);
3413 if (ret)
3414 kfree(platform);
3415
3416 return ret;
3417}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003418EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3419
3420/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003421 * snd_soc_remove_platform - Remove a platform from the ASoC core
3422 * @platform: the platform to remove
3423 */
3424void snd_soc_remove_platform(struct snd_soc_platform *platform)
3425{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003426
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003427 mutex_lock(&client_mutex);
3428 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003429 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003430 mutex_unlock(&client_mutex);
3431
3432 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003433 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02003434
3435 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003436}
3437EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3438
3439struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3440{
3441 struct snd_soc_platform *platform;
3442
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003443 mutex_lock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003444 list_for_each_entry(platform, &platform_list, list) {
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003445 if (dev == platform->dev) {
3446 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003447 return platform;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003448 }
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003449 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003450 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003451
3452 return NULL;
3453}
3454EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3455
3456/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003457 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3458 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003459 * @dev: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003460 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003461void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003462{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463 struct snd_soc_platform *platform;
3464
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003465 platform = snd_soc_lookup_platform(dev);
3466 if (!platform)
3467 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003468
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003469 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003470 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003471}
3472EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3473
Mark Brown151ab222009-05-09 16:22:58 +01003474static u64 codec_format_map[] = {
3475 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3476 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3477 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3478 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3479 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3480 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3481 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3482 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3483 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3484 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3485 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3486 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3487 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3488 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3489 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3490 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3491};
3492
3493/* Fix up the DAI formats for endianness: codecs don't actually see
3494 * the endianness of the data but we're using the CPU format
3495 * definitions which do need to include endianness so we ensure that
3496 * codec DAIs always have both big and little endian variants set.
3497 */
3498static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3499{
3500 int i;
3501
3502 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3503 if (stream->formats & codec_format_map[i])
3504 stream->formats |= codec_format_map[i];
3505}
3506
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003507static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3508{
3509 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3510
3511 return codec->driver->probe(codec);
3512}
3513
3514static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3515{
3516 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3517
3518 codec->driver->remove(codec);
3519}
3520
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003521static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3522{
3523 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3524
3525 return codec->driver->suspend(codec);
3526}
3527
3528static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3529{
3530 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3531
3532 return codec->driver->resume(codec);
3533}
3534
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003535static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3536 unsigned int reg, unsigned int val)
3537{
3538 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3539
3540 return codec->driver->write(codec, reg, val);
3541}
3542
3543static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3544 unsigned int reg, unsigned int *val)
3545{
3546 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3547
3548 *val = codec->driver->read(codec, reg);
3549
3550 return 0;
3551}
3552
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003553static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3554 enum snd_soc_bias_level level)
3555{
3556 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3557
3558 return codec->driver->set_bias_level(codec, level);
3559}
3560
Mark Brown0d0cf002008-12-10 14:32:45 +00003561/**
3562 * snd_soc_register_codec - Register a codec with the ASoC core
3563 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003564 * @dev: The parent device for this codec
3565 * @codec_drv: Codec driver
3566 * @dai_drv: The associated DAI driver
3567 * @num_dai: Number of DAIs
Mark Brown0d0cf002008-12-10 14:32:45 +00003568 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003569int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003570 const struct snd_soc_codec_driver *codec_drv,
3571 struct snd_soc_dai_driver *dai_drv,
3572 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003573{
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003574 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003575 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003576 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003577 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003578
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003579 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003581 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3582 if (codec == NULL)
3583 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003584
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003585 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003586
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003587 ret = snd_soc_component_initialize(&codec->component,
3588 &codec_drv->component_driver, dev);
3589 if (ret)
3590 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003591
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003592 if (codec_drv->probe)
3593 codec->component.probe = snd_soc_codec_drv_probe;
3594 if (codec_drv->remove)
3595 codec->component.remove = snd_soc_codec_drv_remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003596 if (codec_drv->suspend)
3597 codec->component.suspend = snd_soc_codec_drv_suspend;
3598 if (codec_drv->resume)
3599 codec->component.resume = snd_soc_codec_drv_resume;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003600 if (codec_drv->write)
3601 codec->component.write = snd_soc_codec_drv_write;
3602 if (codec_drv->read)
3603 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003604 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003605
3606 dapm = snd_soc_codec_get_dapm(codec);
3607 dapm->idle_bias_off = codec_drv->idle_bias_off;
3608 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003609 if (codec_drv->seq_notifier)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003610 dapm->seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003611 if (codec_drv->set_bias_level)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003612 dapm->set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003613 codec->dev = dev;
3614 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003615 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003616
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003617#ifdef CONFIG_DEBUG_FS
3618 codec->component.init_debugfs = soc_init_codec_debugfs;
3619 codec->component.debugfs_prefix = "codec";
3620#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003621
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003622 if (codec_drv->get_regmap)
3623 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003625 for (i = 0; i < num_dai; i++) {
3626 fixup_codec_formats(&dai_drv[i].playback);
3627 fixup_codec_formats(&dai_drv[i].capture);
3628 }
3629
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003630 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3631 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003632 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003633 goto err_cleanup;
3634 }
3635
3636 list_for_each_entry(dai, &codec->component.dai_list, list)
3637 dai->codec = codec;
3638
Mark Brown054880f2012-04-09 17:29:19 +01003639 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003640 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003641 list_add(&codec->list, &codec_list);
3642 mutex_unlock(&client_mutex);
3643
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003644 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3645 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003646 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003647
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003648err_cleanup:
3649 snd_soc_component_cleanup(&codec->component);
3650err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003651 kfree(codec);
3652 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003653}
3654EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3655
3656/**
3657 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3658 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003659 * @dev: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003660 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003661void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003662{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003663 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003664
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003665 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003666 list_for_each_entry(codec, &codec_list, list) {
3667 if (dev == codec->dev)
3668 goto found;
3669 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003670 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003671 return;
3672
3673found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003674 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003675 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003676 mutex_unlock(&client_mutex);
3677
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003678 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3679 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003680
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003681 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003682 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003683 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003684}
3685EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3686
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003687/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003688int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3689 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003690{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003691 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003692 int ret;
3693
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303694 if (!card->dev) {
3695 pr_err("card->dev is not set before calling %s\n", __func__);
3696 return -EINVAL;
3697 }
3698
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003699 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303700
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003701 ret = of_property_read_string_index(np, propname, 0, &card->name);
3702 /*
3703 * EINVAL means the property does not exist. This is fine providing
3704 * card->name was previously set, which is checked later in
3705 * snd_soc_register_card.
3706 */
3707 if (ret < 0 && ret != -EINVAL) {
3708 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003709 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003710 propname, ret);
3711 return ret;
3712 }
3713
3714 return 0;
3715}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003716EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003717
Xiubo Li9a6d4862014-02-08 15:59:52 +08003718static const struct snd_soc_dapm_widget simple_widgets[] = {
3719 SND_SOC_DAPM_MIC("Microphone", NULL),
3720 SND_SOC_DAPM_LINE("Line", NULL),
3721 SND_SOC_DAPM_HP("Headphone", NULL),
3722 SND_SOC_DAPM_SPK("Speaker", NULL),
3723};
3724
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003725int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003726 const char *propname)
3727{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003728 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003729 struct snd_soc_dapm_widget *widgets;
3730 const char *template, *wname;
3731 int i, j, num_widgets, ret;
3732
3733 num_widgets = of_property_count_strings(np, propname);
3734 if (num_widgets < 0) {
3735 dev_err(card->dev,
3736 "ASoC: Property '%s' does not exist\n", propname);
3737 return -EINVAL;
3738 }
3739 if (num_widgets & 1) {
3740 dev_err(card->dev,
3741 "ASoC: Property '%s' length is not even\n", propname);
3742 return -EINVAL;
3743 }
3744
3745 num_widgets /= 2;
3746 if (!num_widgets) {
3747 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3748 propname);
3749 return -EINVAL;
3750 }
3751
3752 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3753 GFP_KERNEL);
3754 if (!widgets) {
3755 dev_err(card->dev,
3756 "ASoC: Could not allocate memory for widgets\n");
3757 return -ENOMEM;
3758 }
3759
3760 for (i = 0; i < num_widgets; i++) {
3761 ret = of_property_read_string_index(np, propname,
3762 2 * i, &template);
3763 if (ret) {
3764 dev_err(card->dev,
3765 "ASoC: Property '%s' index %d read error:%d\n",
3766 propname, 2 * i, ret);
3767 return -EINVAL;
3768 }
3769
3770 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3771 if (!strncmp(template, simple_widgets[j].name,
3772 strlen(simple_widgets[j].name))) {
3773 widgets[i] = simple_widgets[j];
3774 break;
3775 }
3776 }
3777
3778 if (j >= ARRAY_SIZE(simple_widgets)) {
3779 dev_err(card->dev,
3780 "ASoC: DAPM widget '%s' is not supported\n",
3781 template);
3782 return -EINVAL;
3783 }
3784
3785 ret = of_property_read_string_index(np, propname,
3786 (2 * i) + 1,
3787 &wname);
3788 if (ret) {
3789 dev_err(card->dev,
3790 "ASoC: Property '%s' index %d read error:%d\n",
3791 propname, (2 * i) + 1, ret);
3792 return -EINVAL;
3793 }
3794
3795 widgets[i].name = wname;
3796 }
3797
Nicolin Chenf23e8602015-02-14 17:22:49 -08003798 card->of_dapm_widgets = widgets;
3799 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003800
3801 return 0;
3802}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003803EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003804
Jyri Sarha61310842015-09-09 21:27:43 +03003805static int snd_soc_of_get_slot_mask(struct device_node *np,
3806 const char *prop_name,
3807 unsigned int *mask)
3808{
3809 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003810 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003811 int i;
3812
3813 if (!of_slot_mask)
3814 return 0;
3815 val /= sizeof(u32);
3816 for (i = 0; i < val; i++)
3817 if (be32_to_cpup(&of_slot_mask[i]))
3818 *mask |= (1 << i);
3819
3820 return val;
3821}
3822
Xiubo Li89c67852014-02-14 09:34:35 +08003823int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003824 unsigned int *tx_mask,
3825 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003826 unsigned int *slots,
3827 unsigned int *slot_width)
3828{
3829 u32 val;
3830 int ret;
3831
Jyri Sarha61310842015-09-09 21:27:43 +03003832 if (tx_mask)
3833 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3834 if (rx_mask)
3835 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3836
Xiubo Li89c67852014-02-14 09:34:35 +08003837 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3838 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3839 if (ret)
3840 return ret;
3841
3842 if (slots)
3843 *slots = val;
3844 }
3845
3846 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3847 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3848 if (ret)
3849 return ret;
3850
3851 if (slot_width)
3852 *slot_width = val;
3853 }
3854
3855 return 0;
3856}
3857EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3858
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003859void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003860 struct snd_soc_codec_conf *codec_conf,
3861 struct device_node *of_node,
3862 const char *propname)
3863{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003864 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003865 const char *str;
3866 int ret;
3867
3868 ret = of_property_read_string(np, propname, &str);
3869 if (ret < 0) {
3870 /* no prefix is not error */
3871 return;
3872 }
3873
3874 codec_conf->of_node = of_node;
3875 codec_conf->name_prefix = str;
3876}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003877EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003878
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003879int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003880 const char *propname)
3881{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003882 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003883 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003884 struct snd_soc_dapm_route *routes;
3885 int i, ret;
3886
3887 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003888 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003889 dev_err(card->dev,
3890 "ASoC: Property '%s' does not exist or its length is not even\n",
3891 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003892 return -EINVAL;
3893 }
3894 num_routes /= 2;
3895 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003896 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003897 propname);
3898 return -EINVAL;
3899 }
3900
Mark Browne3b1e6a2014-12-18 11:46:38 +00003901 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003902 GFP_KERNEL);
3903 if (!routes) {
3904 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003905 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003906 return -EINVAL;
3907 }
3908
3909 for (i = 0; i < num_routes; i++) {
3910 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003911 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003912 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003913 dev_err(card->dev,
3914 "ASoC: Property '%s' index %d could not be read: %d\n",
3915 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003916 return -EINVAL;
3917 }
3918 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003919 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003920 if (ret) {
3921 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003922 "ASoC: Property '%s' index %d could not be read: %d\n",
3923 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003924 return -EINVAL;
3925 }
3926 }
3927
Nicolin Chenf23e8602015-02-14 17:22:49 -08003928 card->num_of_dapm_routes = num_routes;
3929 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003930
3931 return 0;
3932}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003933EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003934
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003935unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003936 const char *prefix,
3937 struct device_node **bitclkmaster,
3938 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003939{
3940 int ret, i;
3941 char prop[128];
3942 unsigned int format = 0;
3943 int bit, frame;
3944 const char *str;
3945 struct {
3946 char *name;
3947 unsigned int val;
3948 } of_fmt_table[] = {
3949 { "i2s", SND_SOC_DAIFMT_I2S },
3950 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3951 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3952 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3953 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3954 { "ac97", SND_SOC_DAIFMT_AC97 },
3955 { "pdm", SND_SOC_DAIFMT_PDM},
3956 { "msb", SND_SOC_DAIFMT_MSB },
3957 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003958 };
3959
3960 if (!prefix)
3961 prefix = "";
3962
3963 /*
3964 * check "[prefix]format = xxx"
3965 * SND_SOC_DAIFMT_FORMAT_MASK area
3966 */
3967 snprintf(prop, sizeof(prop), "%sformat", prefix);
3968 ret = of_property_read_string(np, prop, &str);
3969 if (ret == 0) {
3970 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3971 if (strcmp(str, of_fmt_table[i].name) == 0) {
3972 format |= of_fmt_table[i].val;
3973 break;
3974 }
3975 }
3976 }
3977
3978 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003979 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003980 * SND_SOC_DAIFMT_CLOCK_MASK area
3981 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003982 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003983 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003984 format |= SND_SOC_DAIFMT_CONT;
3985 else
3986 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003987
3988 /*
3989 * check "[prefix]bitclock-inversion"
3990 * check "[prefix]frame-inversion"
3991 * SND_SOC_DAIFMT_INV_MASK area
3992 */
3993 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3994 bit = !!of_get_property(np, prop, NULL);
3995
3996 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3997 frame = !!of_get_property(np, prop, NULL);
3998
3999 switch ((bit << 4) + frame) {
4000 case 0x11:
4001 format |= SND_SOC_DAIFMT_IB_IF;
4002 break;
4003 case 0x10:
4004 format |= SND_SOC_DAIFMT_IB_NF;
4005 break;
4006 case 0x01:
4007 format |= SND_SOC_DAIFMT_NB_IF;
4008 break;
4009 default:
4010 /* SND_SOC_DAIFMT_NB_NF is default */
4011 break;
4012 }
4013
4014 /*
4015 * check "[prefix]bitclock-master"
4016 * check "[prefix]frame-master"
4017 * SND_SOC_DAIFMT_MASTER_MASK area
4018 */
4019 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4020 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004021 if (bit && bitclkmaster)
4022 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004023
4024 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4025 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004026 if (frame && framemaster)
4027 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004028
4029 switch ((bit << 4) + frame) {
4030 case 0x11:
4031 format |= SND_SOC_DAIFMT_CBM_CFM;
4032 break;
4033 case 0x10:
4034 format |= SND_SOC_DAIFMT_CBM_CFS;
4035 break;
4036 case 0x01:
4037 format |= SND_SOC_DAIFMT_CBS_CFM;
4038 break;
4039 default:
4040 format |= SND_SOC_DAIFMT_CBS_CFS;
4041 break;
4042 }
4043
4044 return format;
4045}
4046EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4047
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00004048int snd_soc_get_dai_id(struct device_node *ep)
4049{
4050 struct snd_soc_component *pos;
4051 struct device_node *node;
4052 int ret;
4053
4054 node = of_graph_get_port_parent(ep);
4055
4056 /*
4057 * For example HDMI case, HDMI has video/sound port,
4058 * but ALSA SoC needs sound port number only.
4059 * Thus counting HDMI DT port/endpoint doesn't work.
4060 * Then, it should have .of_xlate_dai_id
4061 */
4062 ret = -ENOTSUPP;
4063 mutex_lock(&client_mutex);
4064 list_for_each_entry(pos, &component_list, list) {
4065 struct device_node *component_of_node = pos->dev->of_node;
4066
4067 if (!component_of_node && pos->dev->parent)
4068 component_of_node = pos->dev->parent->of_node;
4069
4070 if (component_of_node != node)
4071 continue;
4072
4073 if (pos->driver->of_xlate_dai_id)
4074 ret = pos->driver->of_xlate_dai_id(pos, ep);
4075
4076 break;
4077 }
4078 mutex_unlock(&client_mutex);
4079
4080 return ret;
4081}
4082EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4083
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004084int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004085 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004086{
4087 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004088 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004089 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004090
4091 mutex_lock(&client_mutex);
4092 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004093 component_of_node = pos->dev->of_node;
4094 if (!component_of_node && pos->dev->parent)
4095 component_of_node = pos->dev->parent->of_node;
4096
4097 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004098 continue;
4099
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004100 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004101 ret = pos->driver->of_xlate_dai_name(pos,
4102 args,
4103 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004104 } else {
4105 int id = -1;
4106
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004107 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004108 case 0:
4109 id = 0; /* same as dai_drv[0] */
4110 break;
4111 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004112 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004113 break;
4114 default:
4115 /* not supported */
4116 break;
4117 }
4118
4119 if (id < 0 || id >= pos->num_dai) {
4120 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004121 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004122 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004123
4124 ret = 0;
4125
4126 *dai_name = pos->dai_drv[id].name;
4127 if (!*dai_name)
4128 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004129 }
4130
Kuninori Morimotocb470082013-09-10 17:39:56 -07004131 break;
4132 }
4133 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004134 return ret;
4135}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004136EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004137
4138int snd_soc_of_get_dai_name(struct device_node *of_node,
4139 const char **dai_name)
4140{
4141 struct of_phandle_args args;
4142 int ret;
4143
4144 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4145 "#sound-dai-cells", 0, &args);
4146 if (ret)
4147 return ret;
4148
4149 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07004150
4151 of_node_put(args.np);
4152
4153 return ret;
4154}
4155EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4156
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004157/*
4158 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4159 * @dev: Card device
4160 * @of_node: Device node
4161 * @dai_link: DAI link
4162 *
4163 * Builds an array of CODEC DAI components from the DAI link property
4164 * 'sound-dai'.
4165 * The array is set in the DAI link and the number of DAIs is set accordingly.
4166 * The device nodes in the array (of_node) must be dereferenced by the caller.
4167 *
4168 * Returns 0 for success
4169 */
4170int snd_soc_of_get_dai_link_codecs(struct device *dev,
4171 struct device_node *of_node,
4172 struct snd_soc_dai_link *dai_link)
4173{
4174 struct of_phandle_args args;
4175 struct snd_soc_dai_link_component *component;
4176 char *name;
4177 int index, num_codecs, ret;
4178
4179 /* Count the number of CODECs */
4180 name = "sound-dai";
4181 num_codecs = of_count_phandle_with_args(of_node, name,
4182 "#sound-dai-cells");
4183 if (num_codecs <= 0) {
4184 if (num_codecs == -ENOENT)
4185 dev_err(dev, "No 'sound-dai' property\n");
4186 else
4187 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4188 return num_codecs;
4189 }
4190 component = devm_kzalloc(dev,
4191 sizeof *component * num_codecs,
4192 GFP_KERNEL);
4193 if (!component)
4194 return -ENOMEM;
4195 dai_link->codecs = component;
4196 dai_link->num_codecs = num_codecs;
4197
4198 /* Parse the list */
4199 for (index = 0, component = dai_link->codecs;
4200 index < dai_link->num_codecs;
4201 index++, component++) {
4202 ret = of_parse_phandle_with_args(of_node, name,
4203 "#sound-dai-cells",
4204 index, &args);
4205 if (ret)
4206 goto err;
4207 component->of_node = args.np;
4208 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4209 if (ret < 0)
4210 goto err;
4211 }
4212 return 0;
4213err:
4214 for (index = 0, component = dai_link->codecs;
4215 index < dai_link->num_codecs;
4216 index++, component++) {
4217 if (!component->of_node)
4218 break;
4219 of_node_put(component->of_node);
4220 component->of_node = NULL;
4221 }
4222 dai_link->codecs = NULL;
4223 dai_link->num_codecs = 0;
4224 return ret;
4225}
4226EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4227
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004228static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004229{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004230 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01004231 snd_soc_util_init();
4232
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004233 return platform_driver_register(&soc_driver);
4234}
Mark Brown4abe8e12010-10-12 17:41:03 +01004235module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004236
Mark Brown7d8c16a2008-11-30 22:11:24 +00004237static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004238{
Mark Brownfb257892011-04-28 10:57:54 +01004239 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004240 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01004241
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004242 platform_driver_unregister(&soc_driver);
4243}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004244module_exit(snd_soc_exit);
4245
4246/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004247MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004248MODULE_DESCRIPTION("ALSA SoC Core");
4249MODULE_LICENSE("GPL");
4250MODULE_ALIAS("platform:soc-audio");