blob: 1b94119cfb0d0da6d79bc95a97e7e86aa44ab0d5 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090061 * This is used if driver don't need to have CPU/Codec/Platform
62 * dai_link. see soc.h
63 */
64struct snd_soc_dai_link_component null_dailink_component[0];
65EXPORT_SYMBOL_GPL(null_dailink_component);
66
67/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020068 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
71 */
72static int pmdown_time = 5000;
73module_param(pmdown_time, int, 0);
74MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020076/*
77 * If a DMI filed contain strings in this blacklist (e.g.
78 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080079 * as invalid and dropped when setting the card long name from DMI info.
80 */
81static const char * const dmi_blacklist[] = {
82 "To be filled by OEM",
83 "TBD by OEM",
84 "Default String",
85 "Board Manufacturer",
86 "Board Vendor Name",
87 "Board Product Name",
88 NULL, /* terminator */
89};
90
Mark Browndbe21402010-02-12 11:37:24 +000091static ssize_t pmdown_time_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
Mark Brown36ae1a92012-01-06 17:12:45 -080094 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000097}
98
99static ssize_t pmdown_time_set(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
Mark Brown36ae1a92012-01-06 17:12:45 -0800103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700104 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000105
Jingoo Hanb785a492013-07-19 16:24:59 +0900106 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700107 if (ret)
108 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000109
110 return count;
111}
112
113static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114
Takashi Iwaid29697d2015-01-30 20:16:37 +0100115static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100116 &dev_attr_pmdown_time.attr,
117 NULL
118};
119
120static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121 struct attribute *attr, int idx)
122{
123 struct device *dev = kobj_to_dev(kobj);
124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125
126 if (attr == &dev_attr_pmdown_time.attr)
127 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000128 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100129}
130
131static const struct attribute_group soc_dapm_dev_group = {
132 .attrs = soc_dapm_dev_attrs,
133 .is_visible = soc_dev_attr_is_visible,
134};
135
Mark Brownf7e73b262018-03-09 12:46:27 +0000136static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 .attrs = soc_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
141static const struct attribute_group *soc_dev_attr_groups[] = {
142 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000143 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100144 NULL
145};
146
Mark Brown2624d5f2009-11-03 21:56:13 +0000147#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200148static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100149{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200150 if (!component->card->debugfs_card_root)
151 return;
152
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153 if (component->debugfs_prefix) {
154 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 name = kasprintf(GFP_KERNEL, "%s:%s",
157 component->debugfs_prefix, component->name);
158 if (name) {
159 component->debugfs_root = debugfs_create_dir(name,
160 component->card->debugfs_card_root);
161 kfree(name);
162 }
163 } else {
164 component->debugfs_root = debugfs_create_dir(component->name,
165 component->card->debugfs_card_root);
166 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100167
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200168 if (!component->debugfs_root) {
169 dev_warn(component->dev,
170 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000171 return;
172 }
173
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200174 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
175 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200176}
177
178static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
179{
180 debugfs_remove_recursive(component->debugfs_root);
181}
182
Peng Donglinc15b2a12018-02-14 22:48:07 +0800183static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100184{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100185 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100186 struct snd_soc_dai *dai;
187
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100188 mutex_lock(&client_mutex);
189
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000190 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000191 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800192 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100193
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100194 mutex_unlock(&client_mutex);
195
Donglin Peng700c17c2018-01-18 13:31:26 +0800196 return 0;
197}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800198DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100199
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200static int component_list_show(struct seq_file *m, void *v)
201{
202 struct snd_soc_component *component;
203
204 mutex_lock(&client_mutex);
205
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000206 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000207 seq_printf(m, "%s\n", component->name);
208
209 mutex_unlock(&client_mutex);
210
211 return 0;
212}
213DEFINE_SHOW_ATTRIBUTE(component_list);
214
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200215static void soc_init_card_debugfs(struct snd_soc_card *card)
216{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200217 if (!snd_soc_debugfs_root)
218 return;
219
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200220 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000221 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200222 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200223 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100224 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200225 return;
226 }
227
228 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
229 card->debugfs_card_root,
230 &card->pop_time);
231 if (!card->debugfs_pop_time)
232 dev_warn(card->dev,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200233 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200234}
235
236static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
237{
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900238 if (!card->debugfs_card_root)
239 return;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200240 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900241 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200242}
243
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200244static void snd_soc_debugfs_init(void)
245{
246 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300247 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200248 pr_warn("ASoC: Failed to create debugfs directory\n");
249 snd_soc_debugfs_root = NULL;
250 return;
251 }
252
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200253 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
254 &dai_list_fops))
255 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000256
257 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
258 &component_list_fops))
259 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200260}
261
262static void snd_soc_debugfs_exit(void)
263{
264 debugfs_remove_recursive(snd_soc_debugfs_root);
265}
266
Mark Brown2624d5f2009-11-03 21:56:13 +0000267#else
268
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200269static inline void soc_init_component_debugfs(
270 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000271{
272}
273
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200274static inline void soc_cleanup_component_debugfs(
275 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000276{
277}
278
Axel Linb95fccb2010-11-09 17:06:44 +0800279static inline void soc_init_card_debugfs(struct snd_soc_card *card)
280{
281}
282
283static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
284{
285}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200286
287static inline void snd_soc_debugfs_init(void)
288{
289}
290
291static inline void snd_soc_debugfs_exit(void)
292{
293}
294
Mark Brown2624d5f2009-11-03 21:56:13 +0000295#endif
296
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000297static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
298 struct snd_soc_component *component)
299{
300 struct snd_soc_rtdcom_list *rtdcom;
301 struct snd_soc_rtdcom_list *new_rtdcom;
302
303 for_each_rtdcom(rtd, rtdcom) {
304 /* already connected */
305 if (rtdcom->component == component)
306 return 0;
307 }
308
309 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
310 if (!new_rtdcom)
311 return -ENOMEM;
312
313 new_rtdcom->component = component;
314 INIT_LIST_HEAD(&new_rtdcom->list);
315
316 list_add_tail(&new_rtdcom->list, &rtd->component_list);
317
318 return 0;
319}
320
321static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
322{
323 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
324
325 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
326 kfree(rtdcom1);
327
328 INIT_LIST_HEAD(&rtd->component_list);
329}
330
331struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
332 const char *driver_name)
333{
334 struct snd_soc_rtdcom_list *rtdcom;
335
Kuninori Morimoto971da242018-01-23 00:41:24 +0000336 if (!driver_name)
337 return NULL;
338
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000339 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000340 const char *component_name = rtdcom->component->driver->name;
341
342 if (!component_name)
343 continue;
344
345 if ((component_name == driver_name) ||
346 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000347 return rtdcom->component;
348 }
349
350 return NULL;
351}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000352EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000353
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100354struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
355 const char *dai_link, int stream)
356{
Mengdong Lin1a497982015-11-18 02:34:11 -0500357 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100358
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000359 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500360 if (rtd->dai_link->no_pcm &&
361 !strcmp(rtd->dai_link->name, dai_link))
362 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100363 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000364 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100365 return NULL;
366}
367EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
368
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000369static const struct snd_soc_ops null_snd_soc_ops;
370
Mengdong Lin1a497982015-11-18 02:34:11 -0500371static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
372 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
373{
374 struct snd_soc_pcm_runtime *rtd;
375
376 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
377 if (!rtd)
378 return NULL;
379
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000380 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500381 rtd->card = card;
382 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000383 if (!rtd->dai_link->ops)
384 rtd->dai_link->ops = &null_snd_soc_ops;
385
Kees Cook6396bb22018-06-12 14:03:40 -0700386 rtd->codec_dais = kcalloc(dai_link->num_codecs,
387 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500388 GFP_KERNEL);
389 if (!rtd->codec_dais) {
390 kfree(rtd);
391 return NULL;
392 }
393
394 return rtd;
395}
396
397static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
398{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000399 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000400 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500401 kfree(rtd);
402}
403
404static void soc_add_pcm_runtime(struct snd_soc_card *card,
405 struct snd_soc_pcm_runtime *rtd)
406{
407 list_add_tail(&rtd->list, &card->rtd_list);
408 rtd->num = card->num_rtd;
409 card->num_rtd++;
410}
411
412static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
413{
414 struct snd_soc_pcm_runtime *rtd, *_rtd;
415
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000416 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 list_del(&rtd->list);
418 soc_free_pcm_runtime(rtd);
419 }
420
421 card->num_rtd = 0;
422}
423
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
425 const char *dai_link)
426{
Mengdong Lin1a497982015-11-18 02:34:11 -0500427 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100428
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000429 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500430 if (!strcmp(rtd->dai_link->name, dai_link))
431 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100432 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000433 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100434 return NULL;
435}
436EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
437
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900438static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
439{
440 struct snd_soc_pcm_runtime *rtd;
441
442 for_each_card_rtds(card, rtd)
443 flush_delayed_work(&rtd->delayed_work);
444}
445
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100446static void codec2codec_close_delayed_work(struct work_struct *work)
447{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200448 /*
449 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100450 * Since c2c links are internal nodes in the DAPM graph and
451 * don't interface with the outside world or application layer
452 * we don't have to do any special handling on close.
453 */
454}
455
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000456#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000458int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200459{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000460 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000461 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500462 struct snd_soc_pcm_runtime *rtd;
463 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200464
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200465 /* If the card is not initialized yet there is nothing to do */
466 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200467 return 0;
468
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200469 /*
470 * Due to the resume being scheduled into a workqueue we could
471 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100472 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100474
475 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100477
Mark Browna00f90f2010-12-02 16:24:24 +0000478 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000479 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000480 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100481
Mengdong Lin1a497982015-11-18 02:34:11 -0500482 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100483 continue;
484
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000485 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200486 struct snd_soc_dai_driver *drv = dai->driver;
487
488 if (drv->ops->digital_mute && dai->playback_active)
489 drv->ops->digital_mute(dai, 1);
490 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 }
492
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100493 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000494 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100496 continue;
497
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100499 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100500
Mark Brown87506542008-11-18 20:50:34 +0000501 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000502 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000504 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500505 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100506
Mengdong Lin1a497982015-11-18 02:34:11 -0500507 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100508 continue;
509
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100510 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100512 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200514 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900515 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100516
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000517 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518
Mengdong Lin1a497982015-11-18 02:34:11 -0500519 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100520 continue;
521
Mengdong Lin1a497982015-11-18 02:34:11 -0500522 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800523 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800524 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525
Mengdong Lin1a497982015-11-18 02:34:11 -0500526 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800527 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800528 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 }
530
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200531 /* Recheck all endpoints too, their state is affected by suspend */
532 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700533 snd_soc_dapm_sync(&card->dapm);
534
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000535 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000536 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200537 struct snd_soc_dapm_context *dapm =
538 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000539
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200540 /*
541 * If there are paths active then the COMPONENT will be held
542 * with bias _ON and should not be suspended.
543 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000544 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200545 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000547 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000548 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000549 * bias off then being in STANDBY
550 * means it's doing something,
551 * otherwise fall through.
552 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200553 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000554 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200555 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000556 break;
557 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500558 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200559
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000561 if (component->driver->suspend)
562 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000563 component->suspended = 1;
564 if (component->regmap)
565 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800566 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000567 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 break;
569 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000570 dev_dbg(component->dev,
571 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 break;
573 }
574 }
575 }
576
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000577 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500578 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579
Mengdong Lin1a497982015-11-18 02:34:11 -0500580 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 continue;
582
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100583 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800585
586 /* deactivate pins to sleep state */
587 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200588 }
589
Mark Brown87506542008-11-18 20:50:34 +0000590 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000591 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
593 return 0;
594}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000595EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200597/*
598 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100599 * setting our codec back up, which can be very slow on I2C
600 */
601static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200604 container_of(work, struct snd_soc_card,
605 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500606 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000607 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500608 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200610 /*
611 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100612 * so userspace apps are blocked from touching us
613 */
614
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000615 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100616
Mark Brown9949788b2010-05-07 20:24:05 +0100617 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100619
Mark Brown87506542008-11-18 20:50:34 +0000620 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000621 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100623 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000624 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500625 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100626
Mengdong Lin1a497982015-11-18 02:34:11 -0500627 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100628 continue;
629
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100630 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632 }
633
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000634 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000635 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000636 if (component->driver->resume)
637 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000638 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100639 }
640 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000642 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100643
Mengdong Lin1a497982015-11-18 02:34:11 -0500644 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100645 continue;
646
Mengdong Lin1a497982015-11-18 02:34:11 -0500647 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000648 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800649 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650
Mengdong Lin1a497982015-11-18 02:34:11 -0500651 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000652 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800653 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 }
655
Mark Brown3ff3f642008-05-19 12:32:25 +0200656 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000657 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000658 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100659
Mengdong Lin1a497982015-11-18 02:34:11 -0500660 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100661 continue;
662
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000663 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200664 struct snd_soc_dai_driver *drv = dai->driver;
665
666 if (drv->ops->digital_mute && dai->playback_active)
667 drv->ops->digital_mute(dai, 0);
668 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 }
670
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000671 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500672 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100673
Mengdong Lin1a497982015-11-18 02:34:11 -0500674 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100675 continue;
676
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100677 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 }
680
Mark Brown87506542008-11-18 20:50:34 +0000681 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000682 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000684 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100685
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200686 /* Recheck all endpoints too, their state is affected by suspend */
687 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700688 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530689
690 /* userspace can access us now we are back as we were before */
691 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100692}
693
694/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000695int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100696{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000697 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100698 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500699 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900700 struct snd_soc_dai *codec_dai;
701 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200702
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200703 /* If the card is not initialized yet there is nothing to do */
704 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800705 return 0;
706
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800707 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000708 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200709 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200710
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800711 if (cpu_dai->active)
712 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200713
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900714 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200715 if (codec_dai->active)
716 pinctrl_pm_select_default_state(codec_dai->dev);
717 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800718 }
719
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100720 /*
721 * DAIs that also act as the control bus master might have other drivers
722 * hanging off them so need to resume immediately. Other drivers don't
723 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100724 * due to I/O costs and anti-pop so handle them out of line.
725 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000726 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500727 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200728
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100729 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600730 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100731 if (bus_control) {
732 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600733 soc_resume_deferred(&card->deferred_resume_work);
734 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000735 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600736 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000737 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100738 }
Andy Green6ed25972008-06-13 16:24:05 +0100739
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740 return 0;
741}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000742EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000744#define snd_soc_suspend NULL
745#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746#endif
747
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100748static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800749};
750
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900751static struct device_node
752*soc_component_to_node(struct snd_soc_component *component)
753{
754 struct device_node *of_node;
755
756 of_node = component->dev->of_node;
757 if (!of_node && component->dev->parent)
758 of_node = component->dev->parent->of_node;
759
760 return of_node;
761}
762
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900763static int snd_soc_is_matching_component(
764 const struct snd_soc_dai_link_component *dlc,
765 struct snd_soc_component *component)
766{
767 struct device_node *component_of_node;
768
769 if (!dlc)
770 return 0;
771
772 component_of_node = soc_component_to_node(component);
773
774 if (dlc->of_node && component_of_node != dlc->of_node)
775 return 0;
776 if (dlc->name && strcmp(component->name, dlc->name))
777 return 0;
778
779 return 1;
780}
781
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200782static struct snd_soc_component *soc_find_component(
783 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100784{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200785 struct snd_soc_component *component;
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000786 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100787
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100788 lockdep_assert_held(&client_mutex);
789
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000790 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200791 if (of_node) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900792 component_of_node = soc_component_to_node(component);
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000793
794 if (component_of_node == of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200795 return component;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000796 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200797 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100798 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100799 }
800
801 return NULL;
802}
803
Mengdong Linfbb88b52016-04-22 12:25:33 +0800804/**
805 * snd_soc_find_dai - Find a registered DAI
806 *
Jeffy Chen49584712017-08-22 15:57:21 +0800807 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800808 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700809 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800810 * find the DAI of the same name. The component's of_node and name
811 * should also match if being specified.
812 *
813 * Return: pointer of DAI, or NULL if not found.
814 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800815struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200816 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100817{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200818 struct snd_soc_component *component;
819 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100820
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100821 lockdep_assert_held(&client_mutex);
822
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200823 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000824 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000825 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200826 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000827 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800828 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800829 && (!dai->driver->name
830 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100831 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100832
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200833 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100834 }
835 }
836
837 return NULL;
838}
Mengdong Lin305e9022016-04-19 13:12:25 +0800839EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100840
Mengdong Lin17fb17552016-11-03 01:04:12 +0800841/**
842 * snd_soc_find_dai_link - Find a DAI link
843 *
844 * @card: soc card
845 * @id: DAI link ID to match
846 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000847 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800848 *
849 * This function will search all existing DAI links of the soc card to
850 * find the link of the same ID. Since DAI links may not have their
851 * unique ID, so name and stream name should also match if being
852 * specified.
853 *
854 * Return: pointer of DAI link, or NULL if not found.
855 */
856struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
857 int id, const char *name,
858 const char *stream_name)
859{
860 struct snd_soc_dai_link *link, *_link;
861
862 lockdep_assert_held(&client_mutex);
863
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000864 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800865 if (link->id != id)
866 continue;
867
868 if (name && (!link->name || strcmp(name, link->name)))
869 continue;
870
871 if (stream_name && (!link->stream_name
872 || strcmp(stream_name, link->stream_name)))
873 continue;
874
875 return link;
876 }
877
878 return NULL;
879}
880EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
881
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800882static bool soc_is_dai_link_bound(struct snd_soc_card *card,
883 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800885 struct snd_soc_pcm_runtime *rtd;
886
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000887 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800888 if (rtd->dai_link == dai_link)
889 return true;
890 }
891
892 return false;
893}
894
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500895static int soc_bind_dai_link(struct snd_soc_card *card,
896 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897{
Mengdong Lin1a497982015-11-18 02:34:11 -0500898 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900899 struct snd_soc_dai_link_component *codecs;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000900 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200901 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902
Liam Girdwooda655de82018-07-02 16:59:54 +0100903 if (dai_link->ignore)
904 return 0;
905
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500906 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000907
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800908 if (soc_is_dai_link_bound(card, dai_link)) {
909 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
910 dai_link->name);
911 return 0;
912 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200913
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530914 rtd = soc_new_pcm_runtime(card, dai_link);
915 if (!rtd)
916 return -ENOMEM;
917
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900918 /* FIXME: we need multi CPU support in the future */
919 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000920 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100921 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900922 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500923 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000924 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000925 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000926
Benoit Cousson88bd8702014-07-08 23:19:34 +0200927 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900928 rtd->num_codecs = dai_link->num_codecs;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900929 for_each_link_codecs(dai_link, i, codecs) {
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900930 rtd->codec_dais[i] = snd_soc_find_dai(codecs);
931 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100932 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900933 codecs->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500934 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200935 }
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900936 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000937 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100938
Benoit Cousson88bd8702014-07-08 23:19:34 +0200939 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900940 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100941
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900942 /* Find PLATFORM from registered PLATFORMs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000943 for_each_component(component) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900944 if (!snd_soc_is_matching_component(dai_link->platforms,
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000945 component))
946 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000947
948 snd_soc_rtdcom_add(rtd, component);
949 }
950
Mengdong Lin1a497982015-11-18 02:34:11 -0500951 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000952 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500953
954_err_defer:
955 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200956 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957}
958
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900959static void soc_cleanup_component(struct snd_soc_component *component)
960{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200961 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900962 list_del(&component->card_list);
963 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
964 soc_cleanup_component_debugfs(component);
965 component->card = NULL;
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -0700966 if (!component->driver->module_get_upon_open)
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -0600967 module_put(component->dev->driver->owner);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900968}
969
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200970static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600971{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200972 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200973 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600974
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000975 if (component->driver->remove)
976 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600977
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900978 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600979}
980
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200981static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200982{
983 int err;
984
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600985 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000986 dai->driver->remove_order != order)
987 return;
988
989 if (dai->driver->remove) {
990 err = dai->driver->remove(dai);
991 if (err < 0)
992 dev_err(dai->dev,
993 "ASoC: failed to remove %s: %d\n",
994 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100995 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000996 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100997}
998
Mengdong Lin1a497982015-11-18 02:34:11 -0500999static void soc_remove_link_dais(struct snd_soc_card *card,
1000 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001002 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001003 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004
1005 /* unregister the rtd device */
1006 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001007 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 rtd->dev_registered = 0;
1009 }
1010
1011 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001012 for_each_rtd_codec_dai(rtd, i, codec_dai)
1013 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001015 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016}
1017
Mengdong Lin1a497982015-11-18 02:34:11 -05001018static void soc_remove_link_components(struct snd_soc_card *card,
1019 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001020{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001021 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001022 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001023
Ranjani Sridharan34ac3c32019-05-23 10:12:01 -07001024 mutex_lock(&client_mutex);
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001025 for_each_rtdcom(rtd, rtdcom) {
1026 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001027
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001028 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001029 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001030 }
Ranjani Sridharan34ac3c32019-05-23 10:12:01 -07001031 mutex_unlock(&client_mutex);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001032}
1033
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001034static void soc_remove_dai_links(struct snd_soc_card *card)
1035{
Mengdong Lin1a497982015-11-18 02:34:11 -05001036 int order;
1037 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001038 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001039
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001040 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001041 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001042 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001043 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001044
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001045 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001046 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001047 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001048 }
1049
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001050 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001051 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1052 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1053 link->name);
1054
1055 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001056 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001057}
1058
Mengdong Lin923c5e612015-11-23 11:01:49 -05001059static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001060 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001061{
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001062 int i;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001063 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001064
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001065 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001066 /*
1067 * Codec must be specified by 1 of name or OF node,
1068 * not both or neither.
1069 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001070 if (!!codec->name ==
1071 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001072 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1073 link->name);
1074 return -EINVAL;
1075 }
1076 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001077 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001078 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1079 link->name);
1080 return -EINVAL;
1081 }
1082 }
1083
1084 /*
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001085 * Platform may be specified by either name or OF node,
1086 * or no Platform.
1087 *
1088 * FIXME
1089 *
1090 * We need multi-platform support
Mengdong Lin923c5e612015-11-23 11:01:49 -05001091 */
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001092 if (link->num_platforms > 0) {
Ajit Pandey8780cf12019-01-09 14:17:07 +05301093
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001094 if (link->num_platforms > 1) {
1095 dev_err(card->dev,
1096 "ASoC: multi platform is not yet supported %s\n",
1097 link->name);
1098 return -EINVAL;
1099 }
1100
1101 if (link->platforms->name && link->platforms->of_node) {
1102 dev_err(card->dev,
1103 "ASoC: Both platform name/of_node are set for %s\n",
1104 link->name);
1105 return -EINVAL;
1106 }
1107
1108 /*
1109 * Defer card registartion if platform dai component is not
1110 * added to component list.
1111 */
1112 if ((link->platforms->of_node || link->platforms->name) &&
1113 !soc_find_component(link->platforms->of_node,
1114 link->platforms->name))
1115 return -EPROBE_DEFER;
1116 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301117
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001118 /* FIXME */
1119 if (link->num_cpus > 1) {
1120 dev_err(card->dev,
1121 "ASoC: multi cpu is not yet supported %s\n",
1122 link->name);
1123 return -EINVAL;
1124 }
1125
Mengdong Lin923c5e612015-11-23 11:01:49 -05001126 /*
1127 * CPU device may be specified by either name or OF node, but
1128 * can be left unspecified, and will be matched based on DAI
1129 * name alone..
1130 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001131 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001132 dev_err(card->dev,
1133 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1134 link->name);
1135 return -EINVAL;
1136 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301137
1138 /*
1139 * Defer card registartion if cpu dai component is not added to
1140 * component list.
1141 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001142 if ((link->cpus->of_node || link->cpus->name) &&
1143 !soc_find_component(link->cpus->of_node, link->cpus->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301144 return -EPROBE_DEFER;
1145
Mengdong Lin923c5e612015-11-23 11:01:49 -05001146 /*
1147 * At least one of CPU DAI name or CPU device name/node must be
1148 * specified
1149 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001150 if (!link->cpus->dai_name &&
1151 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001152 dev_err(card->dev,
1153 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1154 link->name);
1155 return -EINVAL;
1156 }
1157
1158 return 0;
1159}
1160
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001161void snd_soc_disconnect_sync(struct device *dev)
1162{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001163 struct snd_soc_component *component =
1164 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001165
1166 if (!component || !component->card)
1167 return;
1168
1169 snd_card_disconnect_sync(component->card->snd_card);
1170}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001171EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001172
Mengdong Linf8f80362015-12-02 14:11:22 +08001173/**
1174 * snd_soc_add_dai_link - Add a DAI link dynamically
1175 * @card: The ASoC card to which the DAI link is added
1176 * @dai_link: The new DAI link to add
1177 *
1178 * This function adds a DAI link to the ASoC card's link list.
1179 *
1180 * Note: Topology can use this API to add DAI links when probing the
1181 * topology component. And machine drivers can still define static
1182 * DAI links in dai_link array.
1183 */
1184int snd_soc_add_dai_link(struct snd_soc_card *card,
1185 struct snd_soc_dai_link *dai_link)
1186{
1187 if (dai_link->dobj.type
1188 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1189 dev_err(card->dev, "Invalid dai link type %d\n",
1190 dai_link->dobj.type);
1191 return -EINVAL;
1192 }
1193
1194 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001195 /*
1196 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001197 * on the link created by topology.
1198 */
1199 if (dai_link->dobj.type && card->add_dai_link)
1200 card->add_dai_link(card, dai_link);
1201
Mengdong Linf8f80362015-12-02 14:11:22 +08001202 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001203
1204 return 0;
1205}
1206EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1207
1208/**
1209 * snd_soc_remove_dai_link - Remove a DAI link from the list
1210 * @card: The ASoC card that owns the link
1211 * @dai_link: The DAI link to remove
1212 *
1213 * This function removes a DAI link from the ASoC card's link list.
1214 *
1215 * For DAI links previously added by topology, topology should
1216 * remove them by using the dobj embedded in the link.
1217 */
1218void snd_soc_remove_dai_link(struct snd_soc_card *card,
1219 struct snd_soc_dai_link *dai_link)
1220{
1221 struct snd_soc_dai_link *link, *_link;
1222
1223 if (dai_link->dobj.type
1224 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1225 dev_err(card->dev, "Invalid dai link type %d\n",
1226 dai_link->dobj.type);
1227 return;
1228 }
1229
1230 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001231 /*
1232 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001233 * on the link created by topology.
1234 */
1235 if (dai_link->dobj.type && card->remove_dai_link)
1236 card->remove_dai_link(card, dai_link);
1237
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001238 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001239 if (link == dai_link) {
1240 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001241 return;
1242 }
1243 }
1244}
1245EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1246
Jerome Brunetaefba452018-07-13 14:50:43 +02001247static void soc_set_of_name_prefix(struct snd_soc_component *component)
1248{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001249 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001250 const char *str;
1251 int ret;
1252
Jerome Brunetaefba452018-07-13 14:50:43 +02001253 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1254 &str);
1255 if (!ret)
1256 component->name_prefix = str;
1257}
1258
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001259static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001260 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001261{
1262 int i;
1263
Jerome Brunetaefba452018-07-13 14:50:43 +02001264 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001265 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001266 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001267
1268 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001269 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001270 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001271 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001272 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001273 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001274 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001275
1276 /*
1277 * If there is no configuration table or no match in the table,
1278 * check if a prefix is provided in the node
1279 */
1280 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001281}
1282
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001283static int soc_probe_component(struct snd_soc_card *card,
1284 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001286 struct snd_soc_dapm_context *dapm =
1287 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001288 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001289 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001291 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001292 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001294 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001295 if (component->card != card) {
1296 dev_err(component->dev,
1297 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1298 card->name, component->card->name);
1299 return -ENODEV;
1300 }
1301 return 0;
1302 }
1303
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -07001304 if (!component->driver->module_get_upon_open &&
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -06001305 !try_module_get(component->dev->driver->owner))
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001306 return -ENODEV;
1307
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001308 component->card = card;
1309 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001310 INIT_LIST_HEAD(&component->card_list);
1311 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001312 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001313
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001314 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001315
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001316 if (component->driver->dapm_widgets) {
1317 ret = snd_soc_dapm_new_controls(dapm,
1318 component->driver->dapm_widgets,
1319 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001320
1321 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001323 "Failed to create new controls %d\n", ret);
1324 goto err_probe;
1325 }
1326 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001327
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001328 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001329 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001330 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001331 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001332 "Failed to create DAI widgets %d\n", ret);
1333 goto err_probe;
1334 }
1335 }
Mark Brown888df392012-02-16 19:37:51 -08001336
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001337 if (component->driver->probe) {
1338 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001340 dev_err(component->dev,
1341 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001342 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 }
1344 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001345 WARN(dapm->idle_bias_off &&
1346 dapm->bias_level != SND_SOC_BIAS_OFF,
1347 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1348 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001350 /* machine specific init */
1351 if (component->init) {
1352 ret = component->init(component);
1353 if (ret < 0) {
1354 dev_err(component->dev,
1355 "Failed to do machine specific init %d\n", ret);
1356 goto err_probe;
1357 }
1358 }
1359
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001360 if (component->driver->controls)
1361 snd_soc_add_component_controls(component,
1362 component->driver->controls,
1363 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001364 if (component->driver->dapm_routes)
1365 snd_soc_dapm_add_routes(dapm,
1366 component->driver->dapm_routes,
1367 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001368
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001369 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001370 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001371 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001372
Jarkko Nikula70d293312011-01-27 16:24:22 +02001373err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001374 if (ret < 0)
1375 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001376
1377 return ret;
1378}
1379
Mark Brown36ae1a92012-01-06 17:12:45 -08001380static void rtd_release(struct device *dev)
1381{
1382 kfree(dev);
1383}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001385static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1386 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001387{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001388 int ret = 0;
1389
Jarkko Nikula589c3562010-12-06 16:27:07 +02001390 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001391 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1392 if (!rtd->dev)
1393 return -ENOMEM;
1394 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001395 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001396 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001397 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001398 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001399 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001400 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001401 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1402 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1403 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1404 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001405 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001406 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001407 /* calling put_device() here to free the rtd->dev */
1408 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001409 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001410 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001411 return ret;
1412 }
1413 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001414 return 0;
1415}
1416
Mengdong Lin1a497982015-11-18 02:34:11 -05001417static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001418 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001419{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001420 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001421 struct snd_soc_rtdcom_list *rtdcom;
1422 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001423
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001424 for_each_rtdcom(rtd, rtdcom) {
1425 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001426
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001427 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001428 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001429 if (ret < 0)
1430 return ret;
1431 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001432 }
1433
Stephen Warren62ae68f2012-06-08 12:34:23 -06001434 return 0;
1435}
1436
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001437static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001438{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001439 if (dai->probed ||
1440 dai->driver->probe_order != order)
1441 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001443 if (dai->driver->probe) {
1444 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001445
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001446 if (ret < 0) {
1447 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1448 dai->name, ret);
1449 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001450 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001451 }
1452
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001453 dai->probed = 1;
1454
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001455 return 0;
1456}
1457
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001458static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1459 struct snd_soc_pcm_runtime *rtd)
1460{
1461 int i, ret = 0;
1462
1463 for (i = 0; i < num_dais; ++i) {
1464 struct snd_soc_dai_driver *drv = dais[i]->driver;
1465
Rohit kumarde17f142018-11-01 18:08:49 +05301466 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001467 ret = drv->pcm_new(rtd, dais[i]);
1468 if (ret < 0) {
1469 dev_err(dais[i]->dev,
1470 "ASoC: Failed to bind %s with pcm device\n",
1471 dais[i]->name);
1472 return ret;
1473 }
1474 }
1475
1476 return 0;
1477}
1478
Mengdong Lin1a497982015-11-18 02:34:11 -05001479static int soc_probe_link_dais(struct snd_soc_card *card,
1480 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001481{
Mengdong Lin1a497982015-11-18 02:34:11 -05001482 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001483 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001484 struct snd_soc_rtdcom_list *rtdcom;
1485 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001486 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001487 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001489 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001490 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 /* set default power off timeout */
1493 rtd->pmdown_time = pmdown_time;
1494
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001495 ret = soc_probe_dai(cpu_dai, order);
1496 if (ret)
1497 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001500 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1501 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001502 if (ret)
1503 return ret;
1504 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001505
Liam Girdwood0168bf02011-06-07 16:08:05 +01001506 /* complete DAI probe during last probe */
1507 if (order != SND_SOC_COMP_ORDER_LAST)
1508 return 0;
1509
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001510 /* do machine specific initialization */
1511 if (dai_link->init) {
1512 ret = dai_link->init(rtd);
1513 if (ret < 0) {
1514 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1515 dai_link->name, ret);
1516 return ret;
1517 }
1518 }
1519
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001520 if (dai_link->dai_fmt)
1521 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1522
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001523 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001524 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001527#ifdef CONFIG_DEBUG_FS
1528 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001529 if (dai_link->dynamic)
1530 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001531#endif
1532
Liam Girdwooda655de82018-07-02 16:59:54 +01001533 num = rtd->num;
1534
1535 /*
1536 * most drivers will register their PCMs using DAI link ordering but
1537 * topology based drivers can use the DAI link id field to set PCM
1538 * device number and then use rtd + a base offset of the BEs.
1539 */
1540 for_each_rtdcom(rtd, rtdcom) {
1541 component = rtdcom->component;
1542
1543 if (!component->driver->use_dai_pcm_id)
1544 continue;
1545
1546 if (rtd->dai_link->no_pcm)
1547 num += component->driver->be_pcm_base;
1548 else
1549 num = rtd->dai_link->id;
1550 }
1551
Jie Yang6f0c4222015-10-13 23:41:00 +08001552 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001553 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001554 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001555 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001556 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301557 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001558 return ret;
1559 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001560 } else if (!dai_link->params) {
1561 /* create the pcm */
1562 ret = soc_new_pcm(rtd, num);
1563 if (ret < 0) {
1564 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1565 dai_link->stream_name, ret);
1566 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001567 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001568 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1569 if (ret < 0)
1570 return ret;
1571 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1572 rtd->num_codecs, rtd);
1573 if (ret < 0)
1574 return ret;
1575 } else {
1576 INIT_DELAYED_WORK(&rtd->delayed_work,
1577 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 }
1579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580 return 0;
1581}
1582
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001583static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001584{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001585 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001586 struct snd_soc_component *component;
1587 const char *name;
1588 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001589
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001590 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1591 /* codecs, usually analog devices */
1592 name = aux_dev->codec_name;
1593 codec_of_node = aux_dev->codec_of_node;
1594 component = soc_find_component(codec_of_node, name);
1595 if (!component) {
1596 if (codec_of_node)
1597 name = of_node_full_name(codec_of_node);
1598 goto err_defer;
1599 }
1600 } else if (aux_dev->name) {
1601 /* generic components */
1602 name = aux_dev->name;
1603 component = soc_find_component(NULL, name);
1604 if (!component)
1605 goto err_defer;
1606 } else {
1607 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1608 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001609 }
1610
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001611 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001612 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001613
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001614 return 0;
1615
1616err_defer:
1617 dev_err(card->dev, "ASoC: %s not registered\n", name);
1618 return -EPROBE_DEFER;
1619}
1620
1621static int soc_probe_aux_devices(struct snd_soc_card *card)
1622{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001623 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001624 int order;
1625 int ret;
1626
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001627 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001628 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001629 if (comp->driver->probe_order == order) {
1630 ret = soc_probe_component(card, comp);
1631 if (ret < 0) {
1632 dev_err(card->dev,
1633 "ASoC: failed to probe aux component %s %d\n",
1634 comp->name, ret);
1635 return ret;
1636 }
1637 }
1638 }
1639 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001640
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001641 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001642}
1643
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001644static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001645{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001646 struct snd_soc_component *comp, *_comp;
1647 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001648
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001649 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001650 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001651 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001652
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001653 if (comp->driver->remove_order == order) {
1654 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001655 /* remove it from the card's aux_comp_list */
1656 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001657 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001658 }
1659 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001660}
1661
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001662/**
1663 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1664 * @rtd: The runtime for which the DAI link format should be changed
1665 * @dai_fmt: The new DAI link format
1666 *
1667 * This function updates the DAI link format for all DAIs connected to the DAI
1668 * link for the specified runtime.
1669 *
1670 * Note: For setups with a static format set the dai_fmt field in the
1671 * corresponding snd_dai_link struct instead of using this function.
1672 *
1673 * Returns 0 on success, otherwise a negative error code.
1674 */
1675int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1676 unsigned int dai_fmt)
1677{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001679 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001680 unsigned int i;
1681 int ret;
1682
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001683 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001684 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1685 if (ret != 0 && ret != -ENOTSUPP) {
1686 dev_warn(codec_dai->dev,
1687 "ASoC: Failed to set DAI format: %d\n", ret);
1688 return ret;
1689 }
1690 }
1691
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001692 /*
1693 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1694 * the component which has non_legacy_dai_naming is Codec
1695 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001696 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001697 unsigned int inv_dai_fmt;
1698
1699 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1700 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1701 case SND_SOC_DAIFMT_CBM_CFM:
1702 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1703 break;
1704 case SND_SOC_DAIFMT_CBM_CFS:
1705 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1706 break;
1707 case SND_SOC_DAIFMT_CBS_CFM:
1708 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1709 break;
1710 case SND_SOC_DAIFMT_CBS_CFS:
1711 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1712 break;
1713 }
1714
1715 dai_fmt = inv_dai_fmt;
1716 }
1717
1718 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1719 if (ret != 0 && ret != -ENOTSUPP) {
1720 dev_warn(cpu_dai->dev,
1721 "ASoC: Failed to set DAI format: %d\n", ret);
1722 return ret;
1723 }
1724
1725 return 0;
1726}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001727EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001728
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001729#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001730/*
1731 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001732 * separate different DMI fields in the card long name. Only number and
1733 * alphabet characters and a few separator characters are kept.
1734 */
1735static void cleanup_dmi_name(char *name)
1736{
1737 int i, j = 0;
1738
1739 for (i = 0; name[i]; i++) {
1740 if (isalnum(name[i]) || (name[i] == '.')
1741 || (name[i] == '_'))
1742 name[j++] = name[i];
1743 else if (name[i] == '-')
1744 name[j++] = '_';
1745 }
1746
1747 name[j] = '\0';
1748}
1749
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001750/*
1751 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001752 * in the black list.
1753 */
1754static int is_dmi_valid(const char *field)
1755{
1756 int i = 0;
1757
1758 while (dmi_blacklist[i]) {
1759 if (strstr(field, dmi_blacklist[i]))
1760 return 0;
1761 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001762 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001763
1764 return 1;
1765}
1766
Liam Girdwood345233d2017-01-14 16:13:02 +08001767/**
1768 * snd_soc_set_dmi_name() - Register DMI names to card
1769 * @card: The card to register DMI names
1770 * @flavour: The flavour "differentiator" for the card amongst its peers.
1771 *
1772 * An Intel machine driver may be used by many different devices but are
1773 * difficult for userspace to differentiate, since machine drivers ususally
1774 * use their own name as the card short name and leave the card long name
1775 * blank. To differentiate such devices and fix bugs due to lack of
1776 * device-specific configurations, this function allows DMI info to be used
1777 * as the sound card long name, in the format of
1778 * "vendor-product-version-board"
1779 * (Character '-' is used to separate different DMI fields here).
1780 * This will help the user space to load the device-specific Use Case Manager
1781 * (UCM) configurations for the card.
1782 *
1783 * Possible card long names may be:
1784 * DellInc.-XPS139343-01-0310JH
1785 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1786 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1787 *
1788 * This function also supports flavoring the card longname to provide
1789 * the extra differentiation, like "vendor-product-version-board-flavor".
1790 *
1791 * We only keep number and alphabet characters and a few separator characters
1792 * in the card long name since UCM in the user space uses the card long names
1793 * as card configuration directory names and AudoConf cannot support special
1794 * charactors like SPACE.
1795 *
1796 * Returns 0 on success, otherwise a negative error code.
1797 */
1798int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1799{
1800 const char *vendor, *product, *product_version, *board;
1801 size_t longname_buf_size = sizeof(card->snd_card->longname);
1802 size_t len;
1803
1804 if (card->long_name)
1805 return 0; /* long name already set by driver or from DMI */
1806
1807 /* make up dmi long name as: vendor.product.version.board */
1808 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001809 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001810 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1811 return 0;
1812 }
1813
1814 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1815 "%s", vendor);
1816 cleanup_dmi_name(card->dmi_longname);
1817
1818 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001819 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001820 len = strlen(card->dmi_longname);
1821 snprintf(card->dmi_longname + len,
1822 longname_buf_size - len,
1823 "-%s", product);
1824
1825 len++; /* skip the separator "-" */
1826 if (len < longname_buf_size)
1827 cleanup_dmi_name(card->dmi_longname + len);
1828
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001829 /*
1830 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001831 * name in the product version field
1832 */
1833 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001834 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001835 len = strlen(card->dmi_longname);
1836 snprintf(card->dmi_longname + len,
1837 longname_buf_size - len,
1838 "-%s", product_version);
1839
1840 len++;
1841 if (len < longname_buf_size)
1842 cleanup_dmi_name(card->dmi_longname + len);
1843 }
1844 }
1845
1846 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001847 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001848 len = strlen(card->dmi_longname);
1849 snprintf(card->dmi_longname + len,
1850 longname_buf_size - len,
1851 "-%s", board);
1852
1853 len++;
1854 if (len < longname_buf_size)
1855 cleanup_dmi_name(card->dmi_longname + len);
1856 } else if (!product) {
1857 /* fall back to using legacy name */
1858 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1859 return 0;
1860 }
1861
1862 /* Add flavour to dmi long name */
1863 if (flavour) {
1864 len = strlen(card->dmi_longname);
1865 snprintf(card->dmi_longname + len,
1866 longname_buf_size - len,
1867 "-%s", flavour);
1868
1869 len++;
1870 if (len < longname_buf_size)
1871 cleanup_dmi_name(card->dmi_longname + len);
1872 }
1873
1874 /* set the card long name */
1875 card->long_name = card->dmi_longname;
1876
1877 return 0;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001880#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001881
Liam Girdwooda655de82018-07-02 16:59:54 +01001882static void soc_check_tplg_fes(struct snd_soc_card *card)
1883{
1884 struct snd_soc_component *component;
1885 const struct snd_soc_component_driver *comp_drv;
1886 struct snd_soc_dai_link *dai_link;
1887 int i;
1888
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001889 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001890
1891 /* does this component override FEs ? */
1892 if (!component->driver->ignore_machine)
1893 continue;
1894
1895 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001896 if (!strcmp(component->driver->ignore_machine,
1897 card->dev->driver->name))
1898 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001899 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001900 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001901 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001902match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001903 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001904 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001905
1906 /* ignore this FE */
1907 if (dai_link->dynamic) {
1908 dai_link->ignore = true;
1909 continue;
1910 }
1911
1912 dev_info(card->dev, "info: override FE DAI link %s\n",
1913 card->dai_link[i].name);
1914
1915 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001916 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001917 dev_err(card->dev, "init platform error");
1918 continue;
1919 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001920 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001921
1922 /* convert non BE into BE */
1923 dai_link->no_pcm = 1;
1924
1925 /* override any BE fixups */
1926 dai_link->be_hw_params_fixup =
1927 component->driver->be_hw_params_fixup;
1928
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001929 /*
1930 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001931 * dai link name if it's NULL to help bind widgets.
1932 */
1933 if (!dai_link->stream_name)
1934 dai_link->stream_name = dai_link->name;
1935 }
1936
1937 /* Inform userspace we are using alternate topology */
1938 if (component->driver->topology_name_prefix) {
1939
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001940 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001941 if (!card->topology_shortname_created) {
1942 comp_drv = component->driver;
1943
1944 snprintf(card->topology_shortname, 32, "%s-%s",
1945 comp_drv->topology_name_prefix,
1946 card->name);
1947 card->topology_shortname_created = true;
1948 }
1949
1950 /* use topology shortname */
1951 card->name = card->topology_shortname;
1952 }
1953 }
1954}
1955
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001956static int soc_cleanup_card_resources(struct snd_soc_card *card)
1957{
1958 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001959 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001960 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001961 card->snd_card = NULL;
1962 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001963
1964 /* remove and free each DAI */
1965 soc_remove_dai_links(card);
1966 soc_remove_pcm_runtimes(card);
1967
1968 /* remove auxiliary devices */
1969 soc_remove_aux_devices(card);
1970
1971 snd_soc_dapm_free(&card->dapm);
1972 soc_cleanup_card_debugfs(card);
1973
1974 /* remove the card */
1975 if (card->remove)
1976 card->remove(card);
1977
1978 return 0;
1979}
1980
Mark Brownb19e6e72012-03-14 21:18:39 +00001981static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982{
Mengdong Lin1a497982015-11-18 02:34:11 -05001983 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001984 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001985 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001987 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001988 for_each_card_prelinks(card, i, dai_link) {
1989 ret = soc_init_dai_link(card, dai_link);
1990 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001991 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1992 dai_link->name, ret);
1993 mutex_unlock(&client_mutex);
1994 return ret;
1995 }
1996 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001997 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001999 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2000 card->dapm.dev = card->dev;
2001 card->dapm.card = card;
2002 list_add(&card->dapm.list, &card->dapm_list);
2003
Liam Girdwooda655de82018-07-02 16:59:54 +01002004 /* check whether any platform is ignore machine FE and using topology */
2005 soc_check_tplg_fes(card);
2006
Mark Brownb19e6e72012-03-14 21:18:39 +00002007 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002008 for_each_card_prelinks(card, i, dai_link) {
2009 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002010 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002011 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 }
2013
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002014 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002015 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002016 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002017 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002018 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002019 }
2020
Mengdong Linf8f80362015-12-02 14:11:22 +08002021 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002022 for_each_card_prelinks(card, i, dai_link)
2023 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002025 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002026 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 card->owner, 0, &card->snd_card);
2028 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002029 dev_err(card->dev,
2030 "ASoC: can't create sound card for card %s: %d\n",
2031 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002032 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002033 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002035 soc_init_card_debugfs(card);
2036
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002037#ifdef CONFIG_DEBUG_FS
2038 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2039#endif
2040
Mark Brown88ee1c62011-02-02 10:43:26 +00002041#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002042 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002043 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002044#endif
Andy Green6ed25972008-06-13 16:24:05 +01002045
Mark Brown9a841eb2011-04-12 17:51:37 -07002046 if (card->dapm_widgets)
2047 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2048 card->num_dapm_widgets);
2049
Nicolin Chenf23e8602015-02-14 17:22:49 -08002050 if (card->of_dapm_widgets)
2051 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2052 card->num_of_dapm_widgets);
2053
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002054 /* initialise the sound card only once */
2055 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002056 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002058 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059 }
2060
Stephen Warren62ae68f2012-06-08 12:34:23 -06002061 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002062 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002063 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002064 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002065 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002066 dev_err(card->dev,
2067 "ASoC: failed to instantiate card %d\n",
2068 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002069 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002070 }
2071 }
2072 }
2073
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002074 /* probe auxiliary components */
2075 ret = soc_probe_aux_devices(card);
2076 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002077 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002078
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002079 /*
2080 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002081 * Components with topology may bring new DAIs and DAI links.
2082 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002083 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002084 if (soc_is_dai_link_bound(card, dai_link))
2085 continue;
2086
2087 ret = soc_init_dai_link(card, dai_link);
2088 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002089 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002090 ret = soc_bind_dai_link(card, dai_link);
2091 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002092 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002093 }
2094
Stephen Warren62ae68f2012-06-08 12:34:23 -06002095 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002096 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002097 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002098 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002099 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002100 dev_err(card->dev,
2101 "ASoC: failed to instantiate card %d\n",
2102 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002103 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002104 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002105 }
2106 }
2107
Mark Brown888df392012-02-16 19:37:51 -08002108 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002109 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002110
Mark Brownb7af1da2011-04-07 19:18:44 +09002111 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002112 snd_soc_add_card_controls(card, card->controls,
2113 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002114
Mark Brownb8ad29d2011-03-02 18:35:51 +00002115 if (card->dapm_routes)
2116 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2117 card->num_dapm_routes);
2118
Nicolin Chenf23e8602015-02-14 17:22:49 -08002119 if (card->of_dapm_routes)
2120 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2121 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002122
Takashi Iwai861886d2017-04-24 08:54:42 +02002123 /* try to set some sane longname if DMI is available */
2124 snd_soc_set_dmi_name(card, NULL);
2125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002128 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2129 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002130 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2131 "%s", card->driver_name ? card->driver_name : card->name);
2132 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2133 switch (card->snd_card->driver[i]) {
2134 case '_':
2135 case '-':
2136 case '\0':
2137 break;
2138 default:
2139 if (!isalnum(card->snd_card->driver[i]))
2140 card->snd_card->driver[i] = '_';
2141 break;
2142 }
2143 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002144
Mark Brown28e9ad92011-03-02 18:36:34 +00002145 if (card->late_probe) {
2146 ret = card->late_probe(card);
2147 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002148 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002149 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002150 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002151 }
2152 }
2153
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002154 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002156 ret = snd_card_register(card->snd_card);
2157 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002158 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2159 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002160 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002161 }
2162
Mark Brown435c5e22008-12-04 15:32:53 +00002163 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002164 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002165 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002166
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002167probe_end:
2168 if (ret < 0)
2169 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002171 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002172 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002173
Mark Brownb19e6e72012-03-14 21:18:39 +00002174 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002175}
2176
2177/* probes a new socdev */
2178static int soc_probe(struct platform_device *pdev)
2179{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002180 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002181
Vinod Koul70a7ca32011-01-14 19:22:48 +05302182 /*
2183 * no card, so machine driver should be registering card
2184 * we should not be here in that case so ret error
2185 */
2186 if (!card)
2187 return -EINVAL;
2188
Mark Brownfe4085e2012-03-02 13:07:41 +00002189 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002190 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002191 card->name);
2192
Mark Brown435c5e22008-12-04 15:32:53 +00002193 /* Bodge while we unpick instantiation */
2194 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002195
Mark Brown28d528c2012-08-09 18:45:23 +01002196 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197}
2198
2199/* removes a socdev */
2200static int soc_remove(struct platform_device *pdev)
2201{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002202 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203
Mark Brownc5af3a22008-11-28 13:29:45 +00002204 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205 return 0;
2206}
2207
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002208int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002209{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002210 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002211 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002212
2213 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002214 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002215
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002216 /*
2217 * Flush out pmdown_time work - we actually do want to run it
2218 * now, we're shutting down so no imminent restart.
2219 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002220 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002222 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002223
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002224 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002225 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002226 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002227 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002228 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002229
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002230 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002231 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002232 pinctrl_pm_select_sleep_state(codec_dai->dev);
2233 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002234 }
2235
Mark Brown416356f2009-06-30 19:05:15 +01002236 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002237}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002238EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002239
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002240const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302241 .suspend = snd_soc_suspend,
2242 .resume = snd_soc_resume,
2243 .freeze = snd_soc_suspend,
2244 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002245 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302246 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002247};
Stephen Warrendeb26072011-04-05 19:35:30 -06002248EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002249
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250/* ASoC platform driver */
2251static struct platform_driver soc_driver = {
2252 .driver = {
2253 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002254 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255 },
2256 .probe = soc_probe,
2257 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258};
2259
Mark Brown096e49d2009-07-05 15:12:22 +01002260/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 * snd_soc_cnew - create new control
2262 * @_template: control template
2263 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002264 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002265 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002266 *
2267 * Create a new mixer control from a template control.
2268 *
2269 * Returns 0 for success, else error.
2270 */
2271struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002272 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002273 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274{
2275 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002276 struct snd_kcontrol *kcontrol;
2277 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278
2279 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 template.index = 0;
2281
Mark Brownefb7ac32011-03-08 17:23:24 +00002282 if (!long_name)
2283 long_name = template.name;
2284
2285 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002286 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002287 if (!name)
2288 return NULL;
2289
Mark Brownefb7ac32011-03-08 17:23:24 +00002290 template.name = name;
2291 } else {
2292 template.name = long_name;
2293 }
2294
2295 kcontrol = snd_ctl_new1(&template, data);
2296
2297 kfree(name);
2298
2299 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002300}
2301EXPORT_SYMBOL_GPL(snd_soc_cnew);
2302
Liam Girdwood022658b2012-02-03 17:43:09 +00002303static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2304 const struct snd_kcontrol_new *controls, int num_controls,
2305 const char *prefix, void *data)
2306{
2307 int err, i;
2308
2309 for (i = 0; i < num_controls; i++) {
2310 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002311
Liam Girdwood022658b2012-02-03 17:43:09 +00002312 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2313 control->name, prefix));
2314 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002315 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2316 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002317 return err;
2318 }
2319 }
2320
2321 return 0;
2322}
2323
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002324struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2325 const char *name)
2326{
2327 struct snd_card *card = soc_card->snd_card;
2328 struct snd_kcontrol *kctl;
2329
2330 if (unlikely(!name))
2331 return NULL;
2332
2333 list_for_each_entry(kctl, &card->controls, list)
2334 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2335 return kctl;
2336 return NULL;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2339
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002340/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002341 * snd_soc_add_component_controls - Add an array of controls to a component.
2342 *
2343 * @component: Component to add controls to
2344 * @controls: Array of controls to add
2345 * @num_controls: Number of elements in the array
2346 *
2347 * Return: 0 for success, else error.
2348 */
2349int snd_soc_add_component_controls(struct snd_soc_component *component,
2350 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2351{
2352 struct snd_card *card = component->card->snd_card;
2353
2354 return snd_soc_add_controls(card, component->dev, controls,
2355 num_controls, component->name_prefix, component);
2356}
2357EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2358
2359/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002360 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2361 * Convenience function to add a list of controls.
2362 *
2363 * @soc_card: SoC card to add controls to
2364 * @controls: array of controls to add
2365 * @num_controls: number of elements in the array
2366 *
2367 * Return 0 for success, else error.
2368 */
2369int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2370 const struct snd_kcontrol_new *controls, int num_controls)
2371{
2372 struct snd_card *card = soc_card->snd_card;
2373
2374 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2375 NULL, soc_card);
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2378
2379/**
2380 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2381 * Convienience function to add a list of controls.
2382 *
2383 * @dai: DAI to add controls to
2384 * @controls: array of controls to add
2385 * @num_controls: number of elements in the array
2386 *
2387 * Return 0 for success, else error.
2388 */
2389int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2390 const struct snd_kcontrol_new *controls, int num_controls)
2391{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002392 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002393
2394 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2395 NULL, dai);
2396}
2397EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2398
2399/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002400 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2401 * @dai: DAI
2402 * @clk_id: DAI specific clock ID
2403 * @freq: new clock frequency in Hz
2404 * @dir: new clock direction - input/output.
2405 *
2406 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2407 */
2408int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2409 unsigned int freq, int dir)
2410{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002411 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002412 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002413
2414 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2415 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002416}
2417EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2418
2419/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002420 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2421 * @component: COMPONENT
2422 * @clk_id: DAI specific clock ID
2423 * @source: Source for the clock
2424 * @freq: new clock frequency in Hz
2425 * @dir: new clock direction - input/output.
2426 *
2427 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2428 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002429int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2430 int clk_id, int source, unsigned int freq,
2431 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002432{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002433 if (component->driver->set_sysclk)
2434 return component->driver->set_sysclk(component, clk_id, source,
2435 freq, dir);
2436
2437 return -ENOTSUPP;
2438}
2439EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2440
2441/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002442 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2443 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002444 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002445 * @div: new clock divisor.
2446 *
2447 * Configures the clock dividers. This is used to derive the best DAI bit and
2448 * frame clocks from the system or master clock. It's best to set the DAI bit
2449 * and frame clocks as low as possible to save system power.
2450 */
2451int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2452 int div_id, int div)
2453{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002454 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002455 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002456 else
2457 return -EINVAL;
2458}
2459EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2460
2461/**
2462 * snd_soc_dai_set_pll - configure DAI PLL.
2463 * @dai: DAI
2464 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002465 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002466 * @freq_in: PLL input clock frequency in Hz
2467 * @freq_out: requested PLL output clock frequency in Hz
2468 *
2469 * Configures and enables PLL to generate output clock based on input clock.
2470 */
Mark Brown85488032009-09-05 18:52:16 +01002471int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2472 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002473{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002474 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002475 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002476 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002477
2478 return snd_soc_component_set_pll(dai->component, pll_id, source,
2479 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002480}
2481EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2482
Mark Brownec4ee522011-03-07 20:58:11 +00002483/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002484 * snd_soc_component_set_pll - configure component PLL.
2485 * @component: COMPONENT
2486 * @pll_id: DAI specific PLL ID
2487 * @source: DAI specific source for the PLL
2488 * @freq_in: PLL input clock frequency in Hz
2489 * @freq_out: requested PLL output clock frequency in Hz
2490 *
2491 * Configures and enables PLL to generate output clock based on input clock.
2492 */
2493int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2494 int source, unsigned int freq_in,
2495 unsigned int freq_out)
2496{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002497 if (component->driver->set_pll)
2498 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002499 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002500
2501 return -EINVAL;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2504
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002505/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002506 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2507 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002508 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002509 *
2510 * Configures the DAI for a preset BCLK to sample rate ratio.
2511 */
2512int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2513{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002514 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002515 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2516 else
2517 return -EINVAL;
2518}
2519EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2520
2521/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002522 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2523 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002524 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002525 *
2526 * Configures the DAI hardware format and clocking.
2527 */
2528int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2529{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002530 if (dai->driver->ops->set_fmt == NULL)
2531 return -ENOTSUPP;
2532 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002533}
2534EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2535
2536/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002537 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002538 * @slots: Number of slots in use.
2539 * @tx_mask: bitmask representing active TX slots.
2540 * @rx_mask: bitmask representing active RX slots.
2541 *
2542 * Generates the TDM tx and rx slot default masks for DAI.
2543 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002544static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002545 unsigned int *tx_mask,
2546 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002547{
2548 if (*tx_mask || *rx_mask)
2549 return 0;
2550
2551 if (!slots)
2552 return -EINVAL;
2553
2554 *tx_mask = (1 << slots) - 1;
2555 *rx_mask = (1 << slots) - 1;
2556
2557 return 0;
2558}
2559
2560/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002561 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2562 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002563 * @tx_mask: bitmask representing active TX slots.
2564 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002565 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002566 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002567 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002568 * This function configures the specified DAI for TDM operation. @slot contains
2569 * the total number of slots of the TDM stream and @slot_with the width of each
2570 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2571 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2572 * DAI should write to or read from. If a bit is set the corresponding slot is
2573 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2574 * the first slot, bit 1 to the second slot and so on. The first active slot
2575 * maps to the first channel of the DAI, the second active slot to the second
2576 * channel and so on.
2577 *
2578 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2579 * @rx_mask and @slot_width will be ignored.
2580 *
2581 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002582 */
2583int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002584 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002585{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002586 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002587 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002588 &tx_mask, &rx_mask);
2589 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002590 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002591
Benoit Cousson88bd8702014-07-08 23:19:34 +02002592 dai->tx_mask = tx_mask;
2593 dai->rx_mask = rx_mask;
2594
Kuninori Morimoto46471922017-09-25 01:38:34 +00002595 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002596 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002597 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002598 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002599 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002600}
2601EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2602
2603/**
Barry Song472df3c2009-09-12 01:16:29 +08002604 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2605 * @dai: DAI
2606 * @tx_num: how many TX channels
2607 * @tx_slot: pointer to an array which imply the TX slot number channel
2608 * 0~num-1 uses
2609 * @rx_num: how many RX channels
2610 * @rx_slot: pointer to an array which imply the RX slot number channel
2611 * 0~num-1 uses
2612 *
2613 * configure the relationship between channel number and TDM slot number.
2614 */
2615int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2616 unsigned int tx_num, unsigned int *tx_slot,
2617 unsigned int rx_num, unsigned int *rx_slot)
2618{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002619 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002620 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002621 rx_num, rx_slot);
2622 else
2623 return -EINVAL;
2624}
2625EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2626
2627/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002628 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2629 * @dai: DAI
2630 * @tx_num: how many TX channels
2631 * @tx_slot: pointer to an array which imply the TX slot number channel
2632 * 0~num-1 uses
2633 * @rx_num: how many RX channels
2634 * @rx_slot: pointer to an array which imply the RX slot number channel
2635 * 0~num-1 uses
2636 */
2637int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2638 unsigned int *tx_num, unsigned int *tx_slot,
2639 unsigned int *rx_num, unsigned int *rx_slot)
2640{
2641 if (dai->driver->ops->get_channel_map)
2642 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2643 rx_num, rx_slot);
2644 else
2645 return -ENOTSUPP;
2646}
2647EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2648
2649/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002650 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2651 * @dai: DAI
2652 * @tristate: tristate enable
2653 *
2654 * Tristates the DAI so that others can use it.
2655 */
2656int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2657{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002658 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002659 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002660 else
2661 return -EINVAL;
2662}
2663EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2664
2665/**
2666 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2667 * @dai: DAI
2668 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002669 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002670 *
2671 * Mutes the DAI DAC.
2672 */
Mark Brownda183962013-02-06 15:44:07 +00002673int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2674 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002675{
Mark Brownda183962013-02-06 15:44:07 +00002676 if (dai->driver->ops->mute_stream)
2677 return dai->driver->ops->mute_stream(dai, mute, direction);
2678 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2679 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002680 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002681 else
Mark Brown04570c62012-04-13 19:16:03 +01002682 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002683}
2684EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2685
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002686static int snd_soc_bind_card(struct snd_soc_card *card)
2687{
2688 struct snd_soc_pcm_runtime *rtd;
2689 int ret;
2690
2691 ret = snd_soc_instantiate_card(card);
2692 if (ret != 0)
2693 return ret;
2694
2695 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002696 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002697 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2698 struct snd_soc_dai *codec_dai;
2699 int j;
2700
2701 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2702 if (!codec_dai->active)
2703 pinctrl_pm_select_sleep_state(codec_dai->dev);
2704 }
2705
2706 if (!cpu_dai->active)
2707 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2708 }
2709
2710 return ret;
2711}
2712
Mark Brownc5af3a22008-11-28 13:29:45 +00002713/**
2714 * snd_soc_register_card - Register a card with the ASoC core
2715 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002716 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002717 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002718 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302719int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002720{
2721 if (!card->name || !card->dev)
2722 return -EINVAL;
2723
Mark Browned77cc12011-05-03 18:25:34 +01002724 dev_set_drvdata(card->dev, card);
2725
Stephen Warren111c6412011-01-28 14:26:35 -07002726 snd_soc_initialize_card_lists(card);
2727
Mengdong Linf8f80362015-12-02 14:11:22 +08002728 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002729
Mengdong Lin1a497982015-11-18 02:34:11 -05002730 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002731 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002732
Mark Browndb432b42011-10-03 21:06:40 +01002733 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002734 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002735 card->instantiated = 0;
2736 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002737 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002738 spin_lock_init(&card->dpcm_lock);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002739
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002740 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002741}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302742EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002743
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002744static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2745{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002746 struct snd_soc_pcm_runtime *rtd;
2747 int order;
2748
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002749 if (card->instantiated) {
2750 card->instantiated = false;
2751 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002752 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002753
2754 /* remove all components used by DAI links on this card */
2755 for_each_comp_order(order) {
2756 for_each_card_rtds(card, rtd) {
2757 soc_remove_link_components(card, rtd, order);
2758 }
2759 }
2760
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002761 soc_cleanup_card_resources(card);
2762 if (!unregister)
2763 list_add(&card->list, &unbind_card_list);
2764 } else {
2765 if (unregister)
2766 list_del(&card->list);
2767 }
2768}
2769
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002770/**
2771 * snd_soc_unregister_card - Unregister a card with the ASoC core
2772 *
2773 * @card: Card to unregister
2774 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002775 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302776int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002777{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002778 snd_soc_unbind_card(card, true);
2779 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002780
2781 return 0;
2782}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302783EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002784
2785/*
2786 * Simplify DAI link configuration by removing ".-1" from device names
2787 * and sanitizing names.
2788 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002789static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002790{
2791 char *found, name[NAME_SIZE];
2792 int id1, id2;
2793
2794 if (dev_name(dev) == NULL)
2795 return NULL;
2796
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002797 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002798
2799 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002800 found = strstr(name, dev->driver->name);
2801 if (found) {
2802 /* get ID */
2803 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2804
2805 /* discard ID from name if ID == -1 */
2806 if (*id == -1)
2807 found[strlen(dev->driver->name)] = '\0';
2808 }
2809
2810 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002811 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002812 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2813 char tmp[NAME_SIZE];
2814
2815 /* create unique ID number from I2C addr and bus */
2816 *id = ((id1 & 0xffff) << 16) + id2;
2817
2818 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002819 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2820 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002821 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002822 } else
2823 *id = 0;
2824 }
2825
2826 return kstrdup(name, GFP_KERNEL);
2827}
2828
2829/*
2830 * Simplify DAI link naming for single devices with multiple DAIs by removing
2831 * any ".-1" and using the DAI name (instead of device name).
2832 */
2833static inline char *fmt_multiple_name(struct device *dev,
2834 struct snd_soc_dai_driver *dai_drv)
2835{
2836 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002837 dev_err(dev,
2838 "ASoC: error - multiple DAI %s registered with no name\n",
2839 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002840 return NULL;
2841 }
2842
2843 return kstrdup(dai_drv->name, GFP_KERNEL);
2844}
2845
2846/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002847 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002848 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002849 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002850 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002851static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002852{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002853 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002854
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002855 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002856 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2857 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002858 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002859 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002860 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002861 }
Mark Brown91151712008-11-30 23:31:24 +00002862}
Mark Brown91151712008-11-30 23:31:24 +00002863
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002864/* Create a DAI and add it to the component's DAI list */
2865static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2866 struct snd_soc_dai_driver *dai_drv,
2867 bool legacy_dai_naming)
2868{
2869 struct device *dev = component->dev;
2870 struct snd_soc_dai *dai;
2871
2872 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2873
2874 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2875 if (dai == NULL)
2876 return NULL;
2877
2878 /*
2879 * Back in the old days when we still had component-less DAIs,
2880 * instead of having a static name, component-less DAIs would
2881 * inherit the name of the parent device so it is possible to
2882 * register multiple instances of the DAI. We still need to keep
2883 * the same naming style even though those DAIs are not
2884 * component-less anymore.
2885 */
2886 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002887 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002888 dai->name = fmt_single_name(dev, &dai->id);
2889 } else {
2890 dai->name = fmt_multiple_name(dev, dai_drv);
2891 if (dai_drv->id)
2892 dai->id = dai_drv->id;
2893 else
2894 dai->id = component->num_dai;
2895 }
2896 if (dai->name == NULL) {
2897 kfree(dai);
2898 return NULL;
2899 }
2900
2901 dai->component = component;
2902 dai->dev = dev;
2903 dai->driver = dai_drv;
2904 if (!dai->driver->ops)
2905 dai->driver->ops = &null_dai_ops;
2906
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002907 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002908 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002909 component->num_dai++;
2910
2911 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2912 return dai;
2913}
2914
Mark Brown91151712008-11-30 23:31:24 +00002915/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002916 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002917 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002918 * @component: The component the DAIs are registered for
2919 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002920 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002921 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002922static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002923 struct snd_soc_dai_driver *dai_drv,
2924 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002925{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002926 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002927 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002928 unsigned int i;
2929 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002930
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002931 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002932
2933 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002934
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002935 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2936 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002937 if (dai == NULL) {
2938 ret = -ENOMEM;
2939 goto err;
2940 }
Mark Brown91151712008-11-30 23:31:24 +00002941 }
2942
2943 return 0;
2944
2945err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002946 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002947
2948 return ret;
2949}
Mark Brown91151712008-11-30 23:31:24 +00002950
Mengdong Lin68003e62015-12-31 16:40:43 +08002951/**
2952 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2953 *
2954 * @component: The component the DAIs are registered for
2955 * @dai_drv: DAI driver to use for the DAI
2956 *
2957 * Topology can use this API to register DAIs when probing a component.
2958 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2959 * will be freed in the component cleanup.
2960 */
2961int snd_soc_register_dai(struct snd_soc_component *component,
2962 struct snd_soc_dai_driver *dai_drv)
2963{
2964 struct snd_soc_dapm_context *dapm =
2965 snd_soc_component_get_dapm(component);
2966 struct snd_soc_dai *dai;
2967 int ret;
2968
2969 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2970 dev_err(component->dev, "Invalid dai type %d\n",
2971 dai_drv->dobj.type);
2972 return -EINVAL;
2973 }
2974
2975 lockdep_assert_held(&client_mutex);
2976 dai = soc_add_dai(component, dai_drv, false);
2977 if (!dai)
2978 return -ENOMEM;
2979
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002980 /*
2981 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002982 * also add routes that need these widgets as source or sink.
2983 */
2984 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2985 if (ret != 0) {
2986 dev_err(component->dev,
2987 "Failed to create DAI widgets %d\n", ret);
2988 }
2989
2990 return ret;
2991}
2992EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2993
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002994static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2995 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002996{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002997 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002998
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002999 component->driver->seq_notifier(component, type, subseq);
3000}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003001
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003002static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3003 int event)
3004{
3005 struct snd_soc_component *component = dapm->component;
3006
3007 return component->driver->stream_event(component, event);
3008}
3009
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003010static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3011 enum snd_soc_bias_level level)
3012{
3013 struct snd_soc_component *component = dapm->component;
3014
3015 return component->driver->set_bias_level(component, level);
3016}
3017
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003018static int snd_soc_component_initialize(struct snd_soc_component *component,
3019 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003020{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003021 struct snd_soc_dapm_context *dapm;
3022
Krzysztof Kozlowski9d563eb2019-05-31 12:34:02 +02003023 component->name = fmt_single_name(dev, &component->id);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003024 if (!component->name) {
3025 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003026 return -ENOMEM;
3027 }
3028
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003029 component->dev = dev;
3030 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003031
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003032 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003033 dapm->dev = dev;
3034 dapm->component = component;
3035 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003036 dapm->idle_bias_off = !driver->idle_bias_on;
3037 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003038 if (driver->seq_notifier)
3039 dapm->seq_notifier = snd_soc_component_seq_notifier;
3040 if (driver->stream_event)
3041 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003042 if (driver->set_bias_level)
3043 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003044
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003045 INIT_LIST_HEAD(&component->dai_list);
3046 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003048 return 0;
3049}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003050
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003051static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003052{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003053 int val_bytes = regmap_get_val_bytes(component->regmap);
3054
3055 /* Errors are legitimate for non-integer byte multiples */
3056 if (val_bytes > 0)
3057 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003058}
3059
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003060#ifdef CONFIG_REGMAP
3061
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003062/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003063 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3064 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003065 * @component: The component for which to initialize the regmap instance
3066 * @regmap: The regmap instance that should be used by the component
3067 *
3068 * This function allows deferred assignment of the regmap instance that is
3069 * associated with the component. Only use this if the regmap instance is not
3070 * yet ready when the component is registered. The function must also be called
3071 * before the first IO attempt of the component.
3072 */
3073void snd_soc_component_init_regmap(struct snd_soc_component *component,
3074 struct regmap *regmap)
3075{
3076 component->regmap = regmap;
3077 snd_soc_component_setup_regmap(component);
3078}
3079EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3080
3081/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003082 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3083 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003084 * @component: The component for which to de-initialize the regmap instance
3085 *
3086 * Calls regmap_exit() on the regmap instance associated to the component and
3087 * removes the regmap instance from the component.
3088 *
3089 * This function should only be used if snd_soc_component_init_regmap() was used
3090 * to initialize the regmap instance.
3091 */
3092void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3093{
3094 regmap_exit(component->regmap);
3095 component->regmap = NULL;
3096}
3097EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3098
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003099#endif
3100
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003101static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003102{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003103 mutex_lock(&client_mutex);
3104
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003105 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003106 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003107 component->regmap = dev_get_regmap(component->dev,
3108 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003109 if (component->regmap)
3110 snd_soc_component_setup_regmap(component);
3111 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003112
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003113 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003114 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003115 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003116
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003117 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003118}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003119
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003120static void snd_soc_component_cleanup(struct snd_soc_component *component)
3121{
3122 snd_soc_unregister_dais(component);
3123 kfree(component->name);
3124}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003125
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003126static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3127{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003128 struct snd_soc_card *card = component->card;
3129
3130 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003131 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003132
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003133 list_del(&component->list);
3134}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003135
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003136#define ENDIANNESS_MAP(name) \
3137 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3138static u64 endianness_format_map[] = {
3139 ENDIANNESS_MAP(S16_),
3140 ENDIANNESS_MAP(U16_),
3141 ENDIANNESS_MAP(S24_),
3142 ENDIANNESS_MAP(U24_),
3143 ENDIANNESS_MAP(S32_),
3144 ENDIANNESS_MAP(U32_),
3145 ENDIANNESS_MAP(S24_3),
3146 ENDIANNESS_MAP(U24_3),
3147 ENDIANNESS_MAP(S20_3),
3148 ENDIANNESS_MAP(U20_3),
3149 ENDIANNESS_MAP(S18_3),
3150 ENDIANNESS_MAP(U18_3),
3151 ENDIANNESS_MAP(FLOAT_),
3152 ENDIANNESS_MAP(FLOAT64_),
3153 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3154};
3155
3156/*
3157 * Fix up the DAI formats for endianness: codecs don't actually see
3158 * the endianness of the data but we're using the CPU format
3159 * definitions which do need to include endianness so we ensure that
3160 * codec DAIs always have both big and little endian variants set.
3161 */
3162static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3163{
3164 int i;
3165
3166 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3167 if (stream->formats & endianness_format_map[i])
3168 stream->formats |= endianness_format_map[i];
3169}
3170
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003171static void snd_soc_try_rebind_card(void)
3172{
3173 struct snd_soc_card *card, *c;
3174
3175 if (!list_empty(&unbind_card_list)) {
3176 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3177 if (!snd_soc_bind_card(card))
3178 list_del(&card->list);
3179 }
3180 }
3181}
3182
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003183int snd_soc_add_component(struct device *dev,
3184 struct snd_soc_component *component,
3185 const struct snd_soc_component_driver *component_driver,
3186 struct snd_soc_dai_driver *dai_drv,
3187 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003188{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003189 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003190 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003191
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003192 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003193 if (ret)
3194 goto err_free;
3195
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003196 if (component_driver->endianness) {
3197 for (i = 0; i < num_dai; i++) {
3198 convert_endianness_formats(&dai_drv[i].playback);
3199 convert_endianness_formats(&dai_drv[i].capture);
3200 }
3201 }
3202
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003203 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003204 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003205 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003206 goto err_cleanup;
3207 }
3208
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003209 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003210 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003211
3212 return 0;
3213
3214err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003215 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003216err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003217 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003218}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003219EXPORT_SYMBOL_GPL(snd_soc_add_component);
3220
3221int snd_soc_register_component(struct device *dev,
3222 const struct snd_soc_component_driver *component_driver,
3223 struct snd_soc_dai_driver *dai_drv,
3224 int num_dai)
3225{
3226 struct snd_soc_component *component;
3227
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003228 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003229 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003230 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003231
3232 return snd_soc_add_component(dev, component, component_driver,
3233 dai_drv, num_dai);
3234}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003235EXPORT_SYMBOL_GPL(snd_soc_register_component);
3236
3237/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003238 * snd_soc_unregister_component - Unregister all related component
3239 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003240 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003241 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003242 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003243static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003244{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003245 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003246 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003247
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003248 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003249 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003250 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003251 continue;
3252
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003253 snd_soc_tplg_component_remove(component,
3254 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003255 snd_soc_component_del_unlocked(component);
3256 found = 1;
3257 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003258 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003259 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003260
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003261 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003262 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003263
3264 return found;
3265}
3266
3267void snd_soc_unregister_component(struct device *dev)
3268{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003269 while (__snd_soc_unregister_component(dev))
3270 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003271}
3272EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3273
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003274struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3275 const char *driver_name)
3276{
3277 struct snd_soc_component *component;
3278 struct snd_soc_component *ret;
3279
3280 ret = NULL;
3281 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003282 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003283 if (dev != component->dev)
3284 continue;
3285
3286 if (driver_name &&
3287 (driver_name != component->driver->name) &&
3288 (strcmp(component->driver->name, driver_name) != 0))
3289 continue;
3290
3291 ret = component;
3292 break;
3293 }
3294 mutex_unlock(&client_mutex);
3295
3296 return ret;
3297}
3298EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3299
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003300/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003301int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3302 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003303{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003304 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003305 int ret;
3306
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303307 if (!card->dev) {
3308 pr_err("card->dev is not set before calling %s\n", __func__);
3309 return -EINVAL;
3310 }
3311
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003312 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303313
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003314 ret = of_property_read_string_index(np, propname, 0, &card->name);
3315 /*
3316 * EINVAL means the property does not exist. This is fine providing
3317 * card->name was previously set, which is checked later in
3318 * snd_soc_register_card.
3319 */
3320 if (ret < 0 && ret != -EINVAL) {
3321 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003322 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003323 propname, ret);
3324 return ret;
3325 }
3326
3327 return 0;
3328}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003329EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003330
Xiubo Li9a6d4862014-02-08 15:59:52 +08003331static const struct snd_soc_dapm_widget simple_widgets[] = {
3332 SND_SOC_DAPM_MIC("Microphone", NULL),
3333 SND_SOC_DAPM_LINE("Line", NULL),
3334 SND_SOC_DAPM_HP("Headphone", NULL),
3335 SND_SOC_DAPM_SPK("Speaker", NULL),
3336};
3337
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003338int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003339 const char *propname)
3340{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003341 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003342 struct snd_soc_dapm_widget *widgets;
3343 const char *template, *wname;
3344 int i, j, num_widgets, ret;
3345
3346 num_widgets = of_property_count_strings(np, propname);
3347 if (num_widgets < 0) {
3348 dev_err(card->dev,
3349 "ASoC: Property '%s' does not exist\n", propname);
3350 return -EINVAL;
3351 }
3352 if (num_widgets & 1) {
3353 dev_err(card->dev,
3354 "ASoC: Property '%s' length is not even\n", propname);
3355 return -EINVAL;
3356 }
3357
3358 num_widgets /= 2;
3359 if (!num_widgets) {
3360 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3361 propname);
3362 return -EINVAL;
3363 }
3364
3365 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3366 GFP_KERNEL);
3367 if (!widgets) {
3368 dev_err(card->dev,
3369 "ASoC: Could not allocate memory for widgets\n");
3370 return -ENOMEM;
3371 }
3372
3373 for (i = 0; i < num_widgets; i++) {
3374 ret = of_property_read_string_index(np, propname,
3375 2 * i, &template);
3376 if (ret) {
3377 dev_err(card->dev,
3378 "ASoC: Property '%s' index %d read error:%d\n",
3379 propname, 2 * i, ret);
3380 return -EINVAL;
3381 }
3382
3383 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3384 if (!strncmp(template, simple_widgets[j].name,
3385 strlen(simple_widgets[j].name))) {
3386 widgets[i] = simple_widgets[j];
3387 break;
3388 }
3389 }
3390
3391 if (j >= ARRAY_SIZE(simple_widgets)) {
3392 dev_err(card->dev,
3393 "ASoC: DAPM widget '%s' is not supported\n",
3394 template);
3395 return -EINVAL;
3396 }
3397
3398 ret = of_property_read_string_index(np, propname,
3399 (2 * i) + 1,
3400 &wname);
3401 if (ret) {
3402 dev_err(card->dev,
3403 "ASoC: Property '%s' index %d read error:%d\n",
3404 propname, (2 * i) + 1, ret);
3405 return -EINVAL;
3406 }
3407
3408 widgets[i].name = wname;
3409 }
3410
Nicolin Chenf23e8602015-02-14 17:22:49 -08003411 card->of_dapm_widgets = widgets;
3412 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003413
3414 return 0;
3415}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003416EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003417
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003418int snd_soc_of_get_slot_mask(struct device_node *np,
3419 const char *prop_name,
3420 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003421{
3422 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003423 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003424 int i;
3425
3426 if (!of_slot_mask)
3427 return 0;
3428 val /= sizeof(u32);
3429 for (i = 0; i < val; i++)
3430 if (be32_to_cpup(&of_slot_mask[i]))
3431 *mask |= (1 << i);
3432
3433 return val;
3434}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003435EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003436
Xiubo Li89c67852014-02-14 09:34:35 +08003437int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003438 unsigned int *tx_mask,
3439 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003440 unsigned int *slots,
3441 unsigned int *slot_width)
3442{
3443 u32 val;
3444 int ret;
3445
Jyri Sarha61310842015-09-09 21:27:43 +03003446 if (tx_mask)
3447 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3448 if (rx_mask)
3449 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3450
Xiubo Li89c67852014-02-14 09:34:35 +08003451 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3452 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3453 if (ret)
3454 return ret;
3455
3456 if (slots)
3457 *slots = val;
3458 }
3459
3460 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3461 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3462 if (ret)
3463 return ret;
3464
3465 if (slot_width)
3466 *slot_width = val;
3467 }
3468
3469 return 0;
3470}
3471EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3472
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003473void snd_soc_of_parse_node_prefix(struct device_node *np,
3474 struct snd_soc_codec_conf *codec_conf,
3475 struct device_node *of_node,
3476 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003477{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003478 const char *str;
3479 int ret;
3480
3481 ret = of_property_read_string(np, propname, &str);
3482 if (ret < 0) {
3483 /* no prefix is not error */
3484 return;
3485 }
3486
3487 codec_conf->of_node = of_node;
3488 codec_conf->name_prefix = str;
3489}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003490EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003491
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003492int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003493 const char *propname)
3494{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003495 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003496 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003497 struct snd_soc_dapm_route *routes;
3498 int i, ret;
3499
3500 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003501 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003502 dev_err(card->dev,
3503 "ASoC: Property '%s' does not exist or its length is not even\n",
3504 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003505 return -EINVAL;
3506 }
3507 num_routes /= 2;
3508 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003509 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003510 propname);
3511 return -EINVAL;
3512 }
3513
Kees Cooka86854d2018-06-12 14:07:58 -07003514 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003515 GFP_KERNEL);
3516 if (!routes) {
3517 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003518 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003519 return -EINVAL;
3520 }
3521
3522 for (i = 0; i < num_routes; i++) {
3523 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003524 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003525 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003526 dev_err(card->dev,
3527 "ASoC: Property '%s' index %d could not be read: %d\n",
3528 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003529 return -EINVAL;
3530 }
3531 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003532 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003533 if (ret) {
3534 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003535 "ASoC: Property '%s' index %d could not be read: %d\n",
3536 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003537 return -EINVAL;
3538 }
3539 }
3540
Nicolin Chenf23e8602015-02-14 17:22:49 -08003541 card->num_of_dapm_routes = num_routes;
3542 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003543
3544 return 0;
3545}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003546EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003547
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003548unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003549 const char *prefix,
3550 struct device_node **bitclkmaster,
3551 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003552{
3553 int ret, i;
3554 char prop[128];
3555 unsigned int format = 0;
3556 int bit, frame;
3557 const char *str;
3558 struct {
3559 char *name;
3560 unsigned int val;
3561 } of_fmt_table[] = {
3562 { "i2s", SND_SOC_DAIFMT_I2S },
3563 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3564 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3565 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3566 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3567 { "ac97", SND_SOC_DAIFMT_AC97 },
3568 { "pdm", SND_SOC_DAIFMT_PDM},
3569 { "msb", SND_SOC_DAIFMT_MSB },
3570 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003571 };
3572
3573 if (!prefix)
3574 prefix = "";
3575
3576 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003577 * check "dai-format = xxx"
3578 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003579 * SND_SOC_DAIFMT_FORMAT_MASK area
3580 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003581 ret = of_property_read_string(np, "dai-format", &str);
3582 if (ret < 0) {
3583 snprintf(prop, sizeof(prop), "%sformat", prefix);
3584 ret = of_property_read_string(np, prop, &str);
3585 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003586 if (ret == 0) {
3587 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3588 if (strcmp(str, of_fmt_table[i].name) == 0) {
3589 format |= of_fmt_table[i].val;
3590 break;
3591 }
3592 }
3593 }
3594
3595 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003596 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003597 * SND_SOC_DAIFMT_CLOCK_MASK area
3598 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003599 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003600 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003601 format |= SND_SOC_DAIFMT_CONT;
3602 else
3603 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003604
3605 /*
3606 * check "[prefix]bitclock-inversion"
3607 * check "[prefix]frame-inversion"
3608 * SND_SOC_DAIFMT_INV_MASK area
3609 */
3610 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3611 bit = !!of_get_property(np, prop, NULL);
3612
3613 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3614 frame = !!of_get_property(np, prop, NULL);
3615
3616 switch ((bit << 4) + frame) {
3617 case 0x11:
3618 format |= SND_SOC_DAIFMT_IB_IF;
3619 break;
3620 case 0x10:
3621 format |= SND_SOC_DAIFMT_IB_NF;
3622 break;
3623 case 0x01:
3624 format |= SND_SOC_DAIFMT_NB_IF;
3625 break;
3626 default:
3627 /* SND_SOC_DAIFMT_NB_NF is default */
3628 break;
3629 }
3630
3631 /*
3632 * check "[prefix]bitclock-master"
3633 * check "[prefix]frame-master"
3634 * SND_SOC_DAIFMT_MASTER_MASK area
3635 */
3636 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3637 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003638 if (bit && bitclkmaster)
3639 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003640
3641 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3642 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003643 if (frame && framemaster)
3644 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003645
3646 switch ((bit << 4) + frame) {
3647 case 0x11:
3648 format |= SND_SOC_DAIFMT_CBM_CFM;
3649 break;
3650 case 0x10:
3651 format |= SND_SOC_DAIFMT_CBM_CFS;
3652 break;
3653 case 0x01:
3654 format |= SND_SOC_DAIFMT_CBS_CFM;
3655 break;
3656 default:
3657 format |= SND_SOC_DAIFMT_CBS_CFS;
3658 break;
3659 }
3660
3661 return format;
3662}
3663EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3664
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003665int snd_soc_get_dai_id(struct device_node *ep)
3666{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003667 struct snd_soc_component *component;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003668 struct device_node *node;
3669 int ret;
3670
3671 node = of_graph_get_port_parent(ep);
3672
3673 /*
3674 * For example HDMI case, HDMI has video/sound port,
3675 * but ALSA SoC needs sound port number only.
3676 * Thus counting HDMI DT port/endpoint doesn't work.
3677 * Then, it should have .of_xlate_dai_id
3678 */
3679 ret = -ENOTSUPP;
3680 mutex_lock(&client_mutex);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003681 component = soc_find_component(node, NULL);
3682 if (component &&
3683 component->driver->of_xlate_dai_id)
3684 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003685 mutex_unlock(&client_mutex);
3686
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003687 of_node_put(node);
3688
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003689 return ret;
3690}
3691EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3692
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003693int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003694 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003695{
3696 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003697 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003698 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003699
3700 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003701 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003702 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003703
3704 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003705 continue;
3706
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003707 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003708 ret = pos->driver->of_xlate_dai_name(pos,
3709 args,
3710 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003711 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003712 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003713 int id = -1;
3714
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003715 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003716 case 0:
3717 id = 0; /* same as dai_drv[0] */
3718 break;
3719 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003720 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003721 break;
3722 default:
3723 /* not supported */
3724 break;
3725 }
3726
3727 if (id < 0 || id >= pos->num_dai) {
3728 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003729 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003730 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003731
3732 ret = 0;
3733
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003734 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003735 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003736 if (id == 0)
3737 break;
3738 id--;
3739 }
3740
3741 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003742 if (!*dai_name)
3743 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003744 }
3745
Kuninori Morimotocb470082013-09-10 17:39:56 -07003746 break;
3747 }
3748 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003749 return ret;
3750}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003751EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003752
3753int snd_soc_of_get_dai_name(struct device_node *of_node,
3754 const char **dai_name)
3755{
3756 struct of_phandle_args args;
3757 int ret;
3758
3759 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3760 "#sound-dai-cells", 0, &args);
3761 if (ret)
3762 return ret;
3763
3764 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003765
3766 of_node_put(args.np);
3767
3768 return ret;
3769}
3770EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3771
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003772/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003773 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3774 * @dai_link: DAI link
3775 *
3776 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3777 */
3778void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3779{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003780 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003781 int index;
3782
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003783 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003784 if (!component->of_node)
3785 break;
3786 of_node_put(component->of_node);
3787 component->of_node = NULL;
3788 }
3789}
3790EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3791
3792/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003793 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3794 * @dev: Card device
3795 * @of_node: Device node
3796 * @dai_link: DAI link
3797 *
3798 * Builds an array of CODEC DAI components from the DAI link property
3799 * 'sound-dai'.
3800 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003801 * The device nodes in the array (of_node) must be dereferenced by calling
3802 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003803 *
3804 * Returns 0 for success
3805 */
3806int snd_soc_of_get_dai_link_codecs(struct device *dev,
3807 struct device_node *of_node,
3808 struct snd_soc_dai_link *dai_link)
3809{
3810 struct of_phandle_args args;
3811 struct snd_soc_dai_link_component *component;
3812 char *name;
3813 int index, num_codecs, ret;
3814
3815 /* Count the number of CODECs */
3816 name = "sound-dai";
3817 num_codecs = of_count_phandle_with_args(of_node, name,
3818 "#sound-dai-cells");
3819 if (num_codecs <= 0) {
3820 if (num_codecs == -ENOENT)
3821 dev_err(dev, "No 'sound-dai' property\n");
3822 else
3823 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3824 return num_codecs;
3825 }
Kees Cooka86854d2018-06-12 14:07:58 -07003826 component = devm_kcalloc(dev,
3827 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003828 GFP_KERNEL);
3829 if (!component)
3830 return -ENOMEM;
3831 dai_link->codecs = component;
3832 dai_link->num_codecs = num_codecs;
3833
3834 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003835 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003836 ret = of_parse_phandle_with_args(of_node, name,
3837 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003838 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003839 if (ret)
3840 goto err;
3841 component->of_node = args.np;
3842 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3843 if (ret < 0)
3844 goto err;
3845 }
3846 return 0;
3847err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003848 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003849 dai_link->codecs = NULL;
3850 dai_link->num_codecs = 0;
3851 return ret;
3852}
3853EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3854
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003855static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003856{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003857 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003858 snd_soc_util_init();
3859
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003860 return platform_driver_register(&soc_driver);
3861}
Mark Brown4abe8e12010-10-12 17:41:03 +01003862module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003863
Mark Brown7d8c16a2008-11-30 22:11:24 +00003864static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003865{
Mark Brownfb257892011-04-28 10:57:54 +01003866 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003867 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003868
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003869 platform_driver_unregister(&soc_driver);
3870}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003871module_exit(snd_soc_exit);
3872
3873/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003874MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003875MODULE_DESCRIPTION("ALSA SoC Core");
3876MODULE_LICENSE("GPL");
3877MODULE_ALIAS("platform:soc-audio");