blob: ad5b0ef16d82d10b11f636dc66b062cdc045778c [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);
Mark Brownc5af3a22008-11-28 13:29:45 +000055
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056/*
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
60 */
61static int pmdown_time = 5000;
62module_param(pmdown_time, int, 0);
63MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64
Mengdong Lin98faf432017-06-28 15:01:39 +080065/* If a DMI filed contain strings in this blacklist (e.g.
66 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
67 * as invalid and dropped when setting the card long name from DMI info.
68 */
69static const char * const dmi_blacklist[] = {
70 "To be filled by OEM",
71 "TBD by OEM",
72 "Default String",
73 "Board Manufacturer",
74 "Board Vendor Name",
75 "Board Product Name",
76 NULL, /* terminator */
77};
78
Mark Browndbe21402010-02-12 11:37:24 +000079static ssize_t pmdown_time_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
Mark Brown36ae1a92012-01-06 17:12:45 -080082 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000083
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000085}
86
87static ssize_t pmdown_time_set(struct device *dev,
88 struct device_attribute *attr,
89 const char *buf, size_t count)
90{
Mark Brown36ae1a92012-01-06 17:12:45 -080091 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070092 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000093
Jingoo Hanb785a492013-07-19 16:24:59 +090094 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -070095 if (ret)
96 return ret;
Mark Browndbe21402010-02-12 11:37:24 +000097
98 return count;
99}
100
101static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
102
Takashi Iwaid29697d2015-01-30 20:16:37 +0100103static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100104 &dev_attr_pmdown_time.attr,
105 NULL
106};
107
108static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
109 struct attribute *attr, int idx)
110{
111 struct device *dev = kobj_to_dev(kobj);
112 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
113
114 if (attr == &dev_attr_pmdown_time.attr)
115 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000116 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100117}
118
119static const struct attribute_group soc_dapm_dev_group = {
120 .attrs = soc_dapm_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
122};
123
Mark Brownf7e73b262018-03-09 12:46:27 +0000124static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100125 .attrs = soc_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
129static const struct attribute_group *soc_dev_attr_groups[] = {
130 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000131 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100132 NULL
133};
134
Mark Brown2624d5f2009-11-03 21:56:13 +0000135#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200136static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100137{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200138 if (!component->card->debugfs_card_root)
139 return;
140
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141 if (component->debugfs_prefix) {
142 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100143
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200144 name = kasprintf(GFP_KERNEL, "%s:%s",
145 component->debugfs_prefix, component->name);
146 if (name) {
147 component->debugfs_root = debugfs_create_dir(name,
148 component->card->debugfs_card_root);
149 kfree(name);
150 }
151 } else {
152 component->debugfs_root = debugfs_create_dir(component->name,
153 component->card->debugfs_card_root);
154 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 if (!component->debugfs_root) {
157 dev_warn(component->dev,
158 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000159 return;
160 }
161
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200162 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
163 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200164}
165
166static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
167{
168 debugfs_remove_recursive(component->debugfs_root);
169}
170
Peng Donglinc15b2a12018-02-14 22:48:07 +0800171static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100172{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100173 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100174 struct snd_soc_dai *dai;
175
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100176 mutex_lock(&client_mutex);
177
Donglin Peng700c17c2018-01-18 13:31:26 +0800178 list_for_each_entry(component, &component_list, list)
179 list_for_each_entry(dai, &component->dai_list, list)
180 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100181
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100182 mutex_unlock(&client_mutex);
183
Donglin Peng700c17c2018-01-18 13:31:26 +0800184 return 0;
185}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800186DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100187
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000188static int component_list_show(struct seq_file *m, void *v)
189{
190 struct snd_soc_component *component;
191
192 mutex_lock(&client_mutex);
193
194 list_for_each_entry(component, &component_list, list)
195 seq_printf(m, "%s\n", component->name);
196
197 mutex_unlock(&client_mutex);
198
199 return 0;
200}
201DEFINE_SHOW_ATTRIBUTE(component_list);
202
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200203static void soc_init_card_debugfs(struct snd_soc_card *card)
204{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200205 if (!snd_soc_debugfs_root)
206 return;
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000209 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200210 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200211 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100212 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200213 return;
214 }
215
216 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
217 card->debugfs_card_root,
218 &card->pop_time);
219 if (!card->debugfs_pop_time)
220 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000221 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200222}
223
224static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
225{
226 debugfs_remove_recursive(card->debugfs_card_root);
227}
228
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200229static void snd_soc_debugfs_init(void)
230{
231 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300232 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200233 pr_warn("ASoC: Failed to create debugfs directory\n");
234 snd_soc_debugfs_root = NULL;
235 return;
236 }
237
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239 &dai_list_fops))
240 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000241
242 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
243 &component_list_fops))
244 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200245}
246
247static void snd_soc_debugfs_exit(void)
248{
249 debugfs_remove_recursive(snd_soc_debugfs_root);
250}
251
Mark Brown2624d5f2009-11-03 21:56:13 +0000252#else
253
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200254static inline void soc_init_component_debugfs(
255 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000256{
257}
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_cleanup_component_debugfs(
260 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000261{
262}
263
Axel Linb95fccb2010-11-09 17:06:44 +0800264static inline void soc_init_card_debugfs(struct snd_soc_card *card)
265{
266}
267
268static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
269{
270}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200271
272static inline void snd_soc_debugfs_init(void)
273{
274}
275
276static inline void snd_soc_debugfs_exit(void)
277{
278}
279
Mark Brown2624d5f2009-11-03 21:56:13 +0000280#endif
281
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +0100282static int snd_soc_card_comp_compare(struct device *dev, void *data)
283{
284 struct snd_soc_component *component;
285
286 lockdep_assert_held(&client_mutex);
287 list_for_each_entry(component, &component_list, list) {
288 if (dev == component->dev) {
289 if (!strcmp(component->name, data))
290 return 1;
291 break;
292 }
293 }
294
295 return 0;
296}
297
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000298static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
299 struct snd_soc_component *component)
300{
301 struct snd_soc_rtdcom_list *rtdcom;
302 struct snd_soc_rtdcom_list *new_rtdcom;
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +0100303 char *cname;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000304
305 for_each_rtdcom(rtd, rtdcom) {
306 /* already connected */
307 if (rtdcom->component == component)
308 return 0;
309 }
310
311 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
312 if (!new_rtdcom)
313 return -ENOMEM;
314
315 new_rtdcom->component = component;
316 INIT_LIST_HEAD(&new_rtdcom->list);
317
318 list_add_tail(&new_rtdcom->list, &rtd->component_list);
319
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +0100320 if (rtd->card->auto_bind && !rtd->card->components_added) {
321 cname = devm_kasprintf(rtd->card->dev, GFP_KERNEL,
322 "%s", component->name);
323 component_match_add(rtd->card->dev, &rtd->card->match,
324 snd_soc_card_comp_compare, cname);
325 }
326
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000327 return 0;
328}
329
330static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
331{
332 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
333
334 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
335 kfree(rtdcom1);
336
337 INIT_LIST_HEAD(&rtd->component_list);
338}
339
340struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
341 const char *driver_name)
342{
343 struct snd_soc_rtdcom_list *rtdcom;
344
Kuninori Morimoto971da242018-01-23 00:41:24 +0000345 if (!driver_name)
346 return NULL;
347
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000348 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000349 const char *component_name = rtdcom->component->driver->name;
350
351 if (!component_name)
352 continue;
353
354 if ((component_name == driver_name) ||
355 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000356 return rtdcom->component;
357 }
358
359 return NULL;
360}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000361EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000362
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100363struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
364 const char *dai_link, int stream)
365{
Mengdong Lin1a497982015-11-18 02:34:11 -0500366 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100367
Mengdong Lin1a497982015-11-18 02:34:11 -0500368 list_for_each_entry(rtd, &card->rtd_list, list) {
369 if (rtd->dai_link->no_pcm &&
370 !strcmp(rtd->dai_link->name, dai_link))
371 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100372 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000373 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100374 return NULL;
375}
376EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
377
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000378static const struct snd_soc_ops null_snd_soc_ops;
379
Mengdong Lin1a497982015-11-18 02:34:11 -0500380static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
381 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
382{
383 struct snd_soc_pcm_runtime *rtd;
384
385 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
386 if (!rtd)
387 return NULL;
388
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000389 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500390 rtd->card = card;
391 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000392 if (!rtd->dai_link->ops)
393 rtd->dai_link->ops = &null_snd_soc_ops;
394
Kees Cook6396bb22018-06-12 14:03:40 -0700395 rtd->codec_dais = kcalloc(dai_link->num_codecs,
396 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500397 GFP_KERNEL);
398 if (!rtd->codec_dais) {
399 kfree(rtd);
400 return NULL;
401 }
402
403 return rtd;
404}
405
406static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
407{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000408 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000409 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500410 kfree(rtd);
411}
412
413static void soc_add_pcm_runtime(struct snd_soc_card *card,
414 struct snd_soc_pcm_runtime *rtd)
415{
416 list_add_tail(&rtd->list, &card->rtd_list);
417 rtd->num = card->num_rtd;
418 card->num_rtd++;
419}
420
421static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
422{
423 struct snd_soc_pcm_runtime *rtd, *_rtd;
424
425 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
426 list_del(&rtd->list);
427 soc_free_pcm_runtime(rtd);
428 }
429
430 card->num_rtd = 0;
431}
432
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100433struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
434 const char *dai_link)
435{
Mengdong Lin1a497982015-11-18 02:34:11 -0500436 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100437
Mengdong Lin1a497982015-11-18 02:34:11 -0500438 list_for_each_entry(rtd, &card->rtd_list, list) {
439 if (!strcmp(rtd->dai_link->name, dai_link))
440 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100441 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000442 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100443 return NULL;
444}
445EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
446
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100447static void codec2codec_close_delayed_work(struct work_struct *work)
448{
449 /* Currently nothing to do for c2c links
450 * 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
Andy Green6ed25972008-06-13 16:24:05 +0100469 /* Due to the resume being scheduled into a workqueue we could
470 * suspend before that's finished - wait for it to complete.
471 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100473
474 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100476
Mark Browna00f90f2010-12-02 16:24:24 +0000477 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500478 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100479
Mengdong Lin1a497982015-11-18 02:34:11 -0500480 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100481 continue;
482
Mengdong Lin1a497982015-11-18 02:34:11 -0500483 for (i = 0; i < rtd->num_codecs; i++) {
484 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200485 struct snd_soc_dai_driver *drv = dai->driver;
486
487 if (drv->ops->digital_mute && dai->playback_active)
488 drv->ops->digital_mute(dai, 1);
489 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 }
491
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100492 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500493 list_for_each_entry(rtd, &card->rtd_list, list) {
494 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100495 continue;
496
Mengdong Lin1a497982015-11-18 02:34:11 -0500497 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100498 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100499
Mark Brown87506542008-11-18 20:50:34 +0000500 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000501 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502
Mengdong Lin1a497982015-11-18 02:34:11 -0500503 list_for_each_entry(rtd, &card->rtd_list, list) {
504 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100505
Mengdong Lin1a497982015-11-18 02:34:11 -0500506 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100507 continue;
508
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100509 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100511 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200513 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500514 list_for_each_entry(rtd, &card->rtd_list, list)
515 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100516
Mengdong Lin1a497982015-11-18 02:34:11 -0500517 list_for_each_entry(rtd, &card->rtd_list, list) {
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 Morimotod9fc4062016-11-30 06:22:36 +0000536 list_for_each_entry(component, &card->component_dev_list, card_list) {
537 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000538
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000539 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000541 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200542 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000544 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000545 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000546 * bias off then being in STANDBY
547 * means it's doing something,
548 * otherwise fall through.
549 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200550 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000551 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200552 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000553 break;
554 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200555
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000557 if (component->driver->suspend)
558 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000559 component->suspended = 1;
560 if (component->regmap)
561 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800562 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000563 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 break;
565 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000566 dev_dbg(component->dev,
567 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 break;
569 }
570 }
571 }
572
Mengdong Lin1a497982015-11-18 02:34:11 -0500573 list_for_each_entry(rtd, &card->rtd_list, list) {
574 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575
Mengdong Lin1a497982015-11-18 02:34:11 -0500576 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 continue;
578
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100579 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800581
582 /* deactivate pins to sleep state */
583 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Mark Brown87506542008-11-18 20:50:34 +0000586 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000587 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200588
589 return 0;
590}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000591EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Andy Green6ed25972008-06-13 16:24:05 +0100593/* deferred resume work, so resume can complete before we finished
594 * setting our codec back up, which can be very slow on I2C
595 */
596static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 struct snd_soc_card *card =
599 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500600 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000601 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500602 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603
Andy Green6ed25972008-06-13 16:24:05 +0100604 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
605 * so userspace apps are blocked from touching us
606 */
607
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000608 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100609
Mark Brown9949788b2010-05-07 20:24:05 +0100610 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100612
Mark Brown87506542008-11-18 20:50:34 +0000613 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000614 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100616 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 list_for_each_entry(rtd, &card->rtd_list, list) {
618 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100619
Mengdong Lin1a497982015-11-18 02:34:11 -0500620 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100621 continue;
622
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100623 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625 }
626
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000627 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000628 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000629 if (component->driver->resume)
630 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000631 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100632 }
633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Mengdong Lin1a497982015-11-18 02:34:11 -0500635 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100636
Mengdong Lin1a497982015-11-18 02:34:11 -0500637 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100638 continue;
639
Mengdong Lin1a497982015-11-18 02:34:11 -0500640 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000641 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800642 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643
Mengdong Lin1a497982015-11-18 02:34:11 -0500644 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000645 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800646 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 }
648
Mark Brown3ff3f642008-05-19 12:32:25 +0200649 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500650 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100651
Mengdong Lin1a497982015-11-18 02:34:11 -0500652 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100653 continue;
654
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 for (i = 0; i < rtd->num_codecs; i++) {
656 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200657 struct snd_soc_dai_driver *drv = dai->driver;
658
659 if (drv->ops->digital_mute && dai->playback_active)
660 drv->ops->digital_mute(dai, 0);
661 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 list_for_each_entry(rtd, &card->rtd_list, list) {
665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100666
Mengdong Lin1a497982015-11-18 02:34:11 -0500667 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100668 continue;
669
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100670 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200672 }
673
Mark Brown87506542008-11-18 20:50:34 +0000674 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000675 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000677 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100678
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200679 /* Recheck all endpoints too, their state is affected by suspend */
680 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700681 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530682
683 /* userspace can access us now we are back as we were before */
684 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100685}
686
687/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000688int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100689{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000690 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100691 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500692 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200693
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200694 /* If the card is not initialized yet there is nothing to do */
695 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800696 return 0;
697
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800698 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500699 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200700 struct snd_soc_dai **codec_dais = rtd->codec_dais;
701 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
702 int j;
703
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800704 if (cpu_dai->active)
705 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200706
707 for (j = 0; j < rtd->num_codecs; j++) {
708 struct snd_soc_dai *codec_dai = codec_dais[j];
709 if (codec_dai->active)
710 pinctrl_pm_select_default_state(codec_dai->dev);
711 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800712 }
713
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100714 /*
715 * DAIs that also act as the control bus master might have other drivers
716 * hanging off them so need to resume immediately. Other drivers don't
717 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100718 * due to I/O costs and anti-pop so handle them out of line.
719 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500720 list_for_each_entry(rtd, &card->rtd_list, list) {
721 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100722 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600723 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100724 if (bus_control) {
725 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 soc_resume_deferred(&card->deferred_resume_work);
727 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000728 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600729 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000730 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100731 }
Andy Green6ed25972008-06-13 16:24:05 +0100732
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733 return 0;
734}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000735EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000737#define snd_soc_suspend NULL
738#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739#endif
740
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100741static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800742};
743
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200744static struct snd_soc_component *soc_find_component(
745 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100746{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200747 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100748
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100749 lockdep_assert_held(&client_mutex);
750
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200751 list_for_each_entry(component, &component_list, list) {
752 if (of_node) {
753 if (component->dev->of_node == of_node)
754 return component;
755 } else if (strcmp(component->name, name) == 0) {
756 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100757 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100758 }
759
760 return NULL;
761}
762
Mengdong Linfbb88b52016-04-22 12:25:33 +0800763/**
764 * snd_soc_find_dai - Find a registered DAI
765 *
Jeffy Chen49584712017-08-22 15:57:21 +0800766 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800767 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700768 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800769 * find the DAI of the same name. The component's of_node and name
770 * should also match if being specified.
771 *
772 * Return: pointer of DAI, or NULL if not found.
773 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800774struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200775 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100776{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200777 struct snd_soc_component *component;
778 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300779 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100780
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100781 lockdep_assert_held(&client_mutex);
782
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200783 /* Find CPU DAI from registered DAIs*/
784 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300785 component_of_node = component->dev->of_node;
786 if (!component_of_node && component->dev->parent)
787 component_of_node = component->dev->parent->of_node;
788
789 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200790 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100791 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200792 continue;
793 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800794 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800795 && (!dai->driver->name
796 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100797 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100798
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200799 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100800 }
801 }
802
803 return NULL;
804}
Mengdong Lin305e9022016-04-19 13:12:25 +0800805EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100806
Mengdong Lin17fb17552016-11-03 01:04:12 +0800807
808/**
809 * snd_soc_find_dai_link - Find a DAI link
810 *
811 * @card: soc card
812 * @id: DAI link ID to match
813 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000814 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800815 *
816 * This function will search all existing DAI links of the soc card to
817 * find the link of the same ID. Since DAI links may not have their
818 * unique ID, so name and stream name should also match if being
819 * specified.
820 *
821 * Return: pointer of DAI link, or NULL if not found.
822 */
823struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
824 int id, const char *name,
825 const char *stream_name)
826{
827 struct snd_soc_dai_link *link, *_link;
828
829 lockdep_assert_held(&client_mutex);
830
831 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
832 if (link->id != id)
833 continue;
834
835 if (name && (!link->name || strcmp(name, link->name)))
836 continue;
837
838 if (stream_name && (!link->stream_name
839 || strcmp(stream_name, link->stream_name)))
840 continue;
841
842 return link;
843 }
844
845 return NULL;
846}
847EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
848
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800849static bool soc_is_dai_link_bound(struct snd_soc_card *card,
850 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800852 struct snd_soc_pcm_runtime *rtd;
853
854 list_for_each_entry(rtd, &card->rtd_list, list) {
855 if (rtd->dai_link == dai_link)
856 return true;
857 }
858
859 return false;
860}
861
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +0100862static int snd_soc_card_comp_bind(struct device *dev)
863{
864 struct snd_soc_card *card = dev_get_drvdata(dev);
865
866 if (card->instantiated)
867 return 0;
868
869 return snd_soc_register_card(card);
870}
871
872static void snd_soc_card_comp_unbind(struct device *dev)
873{
874}
875
876static const struct component_master_ops snd_soc_card_comp_ops = {
877 .bind = snd_soc_card_comp_bind,
878 .unbind = snd_soc_card_comp_unbind,
879};
880
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500881static int soc_bind_dai_link(struct snd_soc_card *card,
882 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200883{
Mengdong Lin1a497982015-11-18 02:34:11 -0500884 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200885 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200886 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000887 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500888 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +0000889 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +0100890 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200891 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
Liam Girdwooda655de82018-07-02 16:59:54 +0100893 if (dai_link->ignore)
894 return 0;
895
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500896 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000897
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800898 if (soc_is_dai_link_bound(card, dai_link)) {
899 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
900 dai_link->name);
901 return 0;
902 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200903
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530904 rtd = soc_new_pcm_runtime(card, dai_link);
905 if (!rtd)
906 return -ENOMEM;
907
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200908 cpu_dai_component.name = dai_link->cpu_name;
909 cpu_dai_component.of_node = dai_link->cpu_of_node;
910 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
911 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000912 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100913 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
914 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500915 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000916 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000917 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000918
Benoit Cousson88bd8702014-07-08 23:19:34 +0200919 rtd->num_codecs = dai_link->num_codecs;
920
921 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500922 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200923 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200924 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 if (!codec_dais[i]) {
926 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
927 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500928 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200929 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000930 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000931 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100932
Benoit Cousson88bd8702014-07-08 23:19:34 +0200933 /* Single codec links expect codec and codec_dai in runtime data */
934 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100935
Mark Brown848dd8b2011-04-27 18:16:32 +0100936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700938 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100939 platform_name = "snd-soc-dummy";
940
Mark Brownb19e6e72012-03-14 21:18:39 +0000941 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000942 list_for_each_entry(component, &component_list, list) {
943 platform_of_node = component->dev->of_node;
944 if (!platform_of_node && component->dev->parent->of_node)
945 platform_of_node = component->dev->parent->of_node;
946
947 if (dai_link->platform_of_node) {
948 if (platform_of_node != dai_link->platform_of_node)
949 continue;
950 } else {
951 if (strcmp(component->name, platform_name))
952 continue;
953 }
954
955 snd_soc_rtdcom_add(rtd, component);
956 }
957
Mengdong Lin1a497982015-11-18 02:34:11 -0500958 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000959 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500960
961_err_defer:
962 soc_free_pcm_runtime(rtd);
963 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964}
965
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200966static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600967{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200968 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200969 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600970
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000971 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600972
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000973 if (component->driver->remove)
974 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600975
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200976 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600977
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200978 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200979 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200980 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600981}
982
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200983static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200984{
985 int err;
986
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200987 if (dai && dai->probed &&
988 dai->driver->remove_order == order) {
989 if (dai->driver->remove) {
990 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100991 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200992 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100993 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200994 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100995 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200996 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100997 }
998}
999
Mengdong Lin1a497982015-11-18 02:34:11 -05001000static void soc_remove_link_dais(struct snd_soc_card *card,
1001 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001003 int i;
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 */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001012 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001013 soc_remove_dai(rtd->codec_dais[i], 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 Clausen61aca562014-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
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001024 for_each_rtdcom(rtd, rtdcom) {
1025 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001026
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001027 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001028 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001030}
1031
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001032static void soc_remove_dai_links(struct snd_soc_card *card)
1033{
Mengdong Lin1a497982015-11-18 02:34:11 -05001034 int order;
1035 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001036 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001037
Liam Girdwood0168bf02011-06-07 16:08:05 +01001038 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1039 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001040 list_for_each_entry(rtd, &card->rtd_list, list)
1041 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001042 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001043
1044 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1045 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001046 list_for_each_entry(rtd, &card->rtd_list, list)
1047 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001048 }
1049
Mengdong Linf8f80362015-12-02 14:11:22 +08001050 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1051 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);
1056 card->num_dai_links--;
1057 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001058}
1059
Mengdong Lin923c5e612015-11-23 11:01:49 -05001060static int snd_soc_init_multicodec(struct snd_soc_card *card,
1061 struct snd_soc_dai_link *dai_link)
1062{
1063 /* Legacy codec/codec_dai link is a single entry in multicodec */
1064 if (dai_link->codec_name || dai_link->codec_of_node ||
1065 dai_link->codec_dai_name) {
1066 dai_link->num_codecs = 1;
1067
1068 dai_link->codecs = devm_kzalloc(card->dev,
1069 sizeof(struct snd_soc_dai_link_component),
1070 GFP_KERNEL);
1071 if (!dai_link->codecs)
1072 return -ENOMEM;
1073
1074 dai_link->codecs[0].name = dai_link->codec_name;
1075 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1076 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1077 }
1078
1079 if (!dai_link->codecs) {
1080 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1081 return -EINVAL;
1082 }
1083
1084 return 0;
1085}
1086
1087static int soc_init_dai_link(struct snd_soc_card *card,
1088 struct snd_soc_dai_link *link)
1089{
1090 int i, ret;
1091
1092 ret = snd_soc_init_multicodec(card, link);
1093 if (ret) {
1094 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1095 return ret;
1096 }
1097
1098 for (i = 0; i < link->num_codecs; i++) {
1099 /*
1100 * Codec must be specified by 1 of name or OF node,
1101 * not both or neither.
1102 */
1103 if (!!link->codecs[i].name ==
1104 !!link->codecs[i].of_node) {
1105 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1106 link->name);
1107 return -EINVAL;
1108 }
1109 /* Codec DAI name must be specified */
1110 if (!link->codecs[i].dai_name) {
1111 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1112 link->name);
1113 return -EINVAL;
1114 }
1115 }
1116
1117 /*
1118 * Platform may be specified by either name or OF node, but
1119 * can be left unspecified, and a dummy platform will be used.
1120 */
1121 if (link->platform_name && link->platform_of_node) {
1122 dev_err(card->dev,
1123 "ASoC: Both platform name/of_node are set for %s\n",
1124 link->name);
1125 return -EINVAL;
1126 }
1127
1128 /*
1129 * CPU device may be specified by either name or OF node, but
1130 * can be left unspecified, and will be matched based on DAI
1131 * name alone..
1132 */
1133 if (link->cpu_name && link->cpu_of_node) {
1134 dev_err(card->dev,
1135 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1136 link->name);
1137 return -EINVAL;
1138 }
1139 /*
1140 * At least one of CPU DAI name or CPU device name/node must be
1141 * specified
1142 */
1143 if (!link->cpu_dai_name &&
1144 !(link->cpu_name || link->cpu_of_node)) {
1145 dev_err(card->dev,
1146 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1147 link->name);
1148 return -EINVAL;
1149 }
1150
1151 return 0;
1152}
1153
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001154void snd_soc_disconnect_sync(struct device *dev)
1155{
1156 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1157
1158 if (!component || !component->card)
1159 return;
1160
1161 snd_card_disconnect_sync(component->card->snd_card);
1162}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001163EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001164
Mengdong Linf8f80362015-12-02 14:11:22 +08001165/**
1166 * snd_soc_add_dai_link - Add a DAI link dynamically
1167 * @card: The ASoC card to which the DAI link is added
1168 * @dai_link: The new DAI link to add
1169 *
1170 * This function adds a DAI link to the ASoC card's link list.
1171 *
1172 * Note: Topology can use this API to add DAI links when probing the
1173 * topology component. And machine drivers can still define static
1174 * DAI links in dai_link array.
1175 */
1176int snd_soc_add_dai_link(struct snd_soc_card *card,
1177 struct snd_soc_dai_link *dai_link)
1178{
1179 if (dai_link->dobj.type
1180 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1181 dev_err(card->dev, "Invalid dai link type %d\n",
1182 dai_link->dobj.type);
1183 return -EINVAL;
1184 }
1185
1186 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001187 /* Notify the machine driver for extra initialization
1188 * on the link created by topology.
1189 */
1190 if (dai_link->dobj.type && card->add_dai_link)
1191 card->add_dai_link(card, dai_link);
1192
Mengdong Linf8f80362015-12-02 14:11:22 +08001193 list_add_tail(&dai_link->list, &card->dai_link_list);
1194 card->num_dai_links++;
1195
1196 return 0;
1197}
1198EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1199
1200/**
1201 * snd_soc_remove_dai_link - Remove a DAI link from the list
1202 * @card: The ASoC card that owns the link
1203 * @dai_link: The DAI link to remove
1204 *
1205 * This function removes a DAI link from the ASoC card's link list.
1206 *
1207 * For DAI links previously added by topology, topology should
1208 * remove them by using the dobj embedded in the link.
1209 */
1210void snd_soc_remove_dai_link(struct snd_soc_card *card,
1211 struct snd_soc_dai_link *dai_link)
1212{
1213 struct snd_soc_dai_link *link, *_link;
1214
1215 if (dai_link->dobj.type
1216 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1217 dev_err(card->dev, "Invalid dai link type %d\n",
1218 dai_link->dobj.type);
1219 return;
1220 }
1221
1222 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001223 /* Notify the machine driver for extra destruction
1224 * on the link created by topology.
1225 */
1226 if (dai_link->dobj.type && card->remove_dai_link)
1227 card->remove_dai_link(card, dai_link);
1228
Mengdong Linf8f80362015-12-02 14:11:22 +08001229 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1230 if (link == dai_link) {
1231 list_del(&link->list);
1232 card->num_dai_links--;
1233 return;
1234 }
1235 }
1236}
1237EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1238
Jerome Brunetaefba452018-07-13 14:50:43 +02001239static void soc_set_of_name_prefix(struct snd_soc_component *component)
1240{
1241 struct device_node *component_of_node = component->dev->of_node;
1242 const char *str;
1243 int ret;
1244
1245 if (!component_of_node && component->dev->parent)
1246 component_of_node = component->dev->parent->of_node;
1247
1248 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1249 &str);
1250 if (!ret)
1251 component->name_prefix = str;
1252}
1253
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001254static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001255 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001256{
1257 int i;
1258
Jerome Brunetaefba452018-07-13 14:50:43 +02001259 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001260 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001261 struct device_node *component_of_node = component->dev->of_node;
1262
1263 if (!component_of_node && component->dev->parent)
1264 component_of_node = component->dev->parent->of_node;
1265
1266 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001267 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001268 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001269 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001270 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001271 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001272 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001273
1274 /*
1275 * If there is no configuration table or no match in the table,
1276 * check if a prefix is provided in the node
1277 */
1278 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001279}
1280
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001281static int soc_probe_component(struct snd_soc_card *card,
1282 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001283{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001284 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001285 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001286 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001288 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001289 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001291 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001292 if (component->card != card) {
1293 dev_err(component->dev,
1294 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1295 card->name, component->card->name);
1296 return -ENODEV;
1297 }
1298 return 0;
1299 }
1300
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001301 if (!try_module_get(component->dev->driver->owner))
1302 return -ENODEV;
1303
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001304 component->card = card;
1305 dapm->card = card;
1306 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001308 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001309
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001310 if (component->driver->dapm_widgets) {
1311 ret = snd_soc_dapm_new_controls(dapm,
1312 component->driver->dapm_widgets,
1313 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001314
1315 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001316 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001317 "Failed to create new controls %d\n", ret);
1318 goto err_probe;
1319 }
1320 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001321
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001322 list_for_each_entry(dai, &component->dai_list, list) {
1323 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001324 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001325 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001326 "Failed to create DAI widgets %d\n", ret);
1327 goto err_probe;
1328 }
1329 }
Mark Brown888df392012-02-16 19:37:51 -08001330
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001331 if (component->driver->probe) {
1332 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001334 dev_err(component->dev,
1335 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001336 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001338
1339 WARN(dapm->idle_bias_off &&
1340 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001341 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001342 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 }
1344
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001345 /* machine specific init */
1346 if (component->init) {
1347 ret = component->init(component);
1348 if (ret < 0) {
1349 dev_err(component->dev,
1350 "Failed to do machine specific init %d\n", ret);
1351 goto err_probe;
1352 }
1353 }
1354
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001355 if (component->driver->controls)
1356 snd_soc_add_component_controls(component,
1357 component->driver->controls,
1358 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001359 if (component->driver->dapm_routes)
1360 snd_soc_dapm_add_routes(dapm,
1361 component->driver->dapm_routes,
1362 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001363
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001364 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001365 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001366
Jarkko Nikula70d293312011-01-27 16:24:22 +02001367 return 0;
1368
1369err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001370 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001371 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001372 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001373
1374 return ret;
1375}
1376
Mark Brown36ae1a92012-01-06 17:12:45 -08001377static void rtd_release(struct device *dev)
1378{
1379 kfree(dev);
1380}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001382static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1383 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001384{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001385 int ret = 0;
1386
Jarkko Nikula589c3562010-12-06 16:27:07 +02001387 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001388 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1389 if (!rtd->dev)
1390 return -ENOMEM;
1391 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001392 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001393 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001394 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001395 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001396 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001397 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1399 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1400 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1401 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001402 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001403 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001404 /* calling put_device() here to free the rtd->dev */
1405 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001406 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001407 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001408 return ret;
1409 }
1410 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001411 return 0;
1412}
1413
Mengdong Lin1a497982015-11-18 02:34:11 -05001414static int soc_probe_link_components(struct snd_soc_card *card,
1415 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001416 int order)
1417{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001418 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001419 struct snd_soc_rtdcom_list *rtdcom;
1420 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001421
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001422 for_each_rtdcom(rtd, rtdcom) {
1423 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001424
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001425 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001426 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001427 if (ret < 0)
1428 return ret;
1429 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001430 }
1431
Stephen Warren62ae68f2012-06-08 12:34:23 -06001432 return 0;
1433}
1434
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001435static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001436{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001437 if (dai->probed ||
1438 dai->driver->probe_order != order)
1439 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001440
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001441 if (dai->driver->probe) {
1442 int ret = dai->driver->probe(dai);
1443 if (ret < 0) {
1444 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1445 dai->name, ret);
1446 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001447 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001448 }
1449
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001450 dai->probed = 1;
1451
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001452 return 0;
1453}
1454
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001455static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1456 struct snd_soc_pcm_runtime *rtd)
1457{
1458 int i, ret = 0;
1459
1460 for (i = 0; i < num_dais; ++i) {
1461 struct snd_soc_dai_driver *drv = dais[i]->driver;
1462
1463 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1464 ret = drv->pcm_new(rtd, dais[i]);
1465 if (ret < 0) {
1466 dev_err(dais[i]->dev,
1467 "ASoC: Failed to bind %s with pcm device\n",
1468 dais[i]->name);
1469 return ret;
1470 }
1471 }
1472
1473 return 0;
1474}
1475
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001476static int soc_link_dai_widgets(struct snd_soc_card *card,
1477 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001478 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001479{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1481 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301482 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001483 int ret;
1484
Benoit Cousson88bd8702014-07-08 23:19:34 +02001485 if (rtd->num_codecs > 1)
1486 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1487
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001488 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301489 sink = codec_dai->playback_widget;
1490 source = cpu_dai->capture_widget;
1491 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001492 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301493 dai_link->num_params,
1494 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001495 if (ret != 0) {
1496 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301497 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001498 return ret;
1499 }
1500 }
1501
Vinod Koul3de7c422015-11-09 23:19:58 +05301502 sink = cpu_dai->playback_widget;
1503 source = codec_dai->capture_widget;
1504 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001505 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301506 dai_link->num_params,
1507 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001508 if (ret != 0) {
1509 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301510 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001511 return ret;
1512 }
1513 }
1514
1515 return 0;
1516}
1517
Mengdong Lin1a497982015-11-18 02:34:11 -05001518static int soc_probe_link_dais(struct snd_soc_card *card,
1519 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520{
Mengdong Lin1a497982015-11-18 02:34:11 -05001521 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001523 struct snd_soc_rtdcom_list *rtdcom;
1524 struct snd_soc_component *component;
1525 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001527 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001528 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530 /* set default power off timeout */
1531 rtd->pmdown_time = pmdown_time;
1532
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001533 ret = soc_probe_dai(cpu_dai, order);
1534 if (ret)
1535 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001538 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001539 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001540 if (ret)
1541 return ret;
1542 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543
Liam Girdwood0168bf02011-06-07 16:08:05 +01001544 /* complete DAI probe during last probe */
1545 if (order != SND_SOC_COMP_ORDER_LAST)
1546 return 0;
1547
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001548 /* do machine specific initialization */
1549 if (dai_link->init) {
1550 ret = dai_link->init(rtd);
1551 if (ret < 0) {
1552 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1553 dai_link->name, ret);
1554 return ret;
1555 }
1556 }
1557
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001558 if (dai_link->dai_fmt)
1559 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1560
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001561 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001562 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001564
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001565#ifdef CONFIG_DEBUG_FS
1566 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001567 if (dai_link->dynamic)
1568 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001569#endif
1570
Liam Girdwooda655de82018-07-02 16:59:54 +01001571 num = rtd->num;
1572
1573 /*
1574 * most drivers will register their PCMs using DAI link ordering but
1575 * topology based drivers can use the DAI link id field to set PCM
1576 * device number and then use rtd + a base offset of the BEs.
1577 */
1578 for_each_rtdcom(rtd, rtdcom) {
1579 component = rtdcom->component;
1580
1581 if (!component->driver->use_dai_pcm_id)
1582 continue;
1583
1584 if (rtd->dai_link->no_pcm)
1585 num += component->driver->be_pcm_base;
1586 else
1587 num = rtd->dai_link->id;
1588 }
1589
Jie Yang6f0c4222015-10-13 23:41:00 +08001590 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301591 /*create compress_device"*/
Liam Girdwooda655de82018-07-02 16:59:54 +01001592 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001593 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001594 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301595 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001596 return ret;
1597 }
1598 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301599
1600 if (!dai_link->params) {
1601 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001602 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301603 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001604 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301605 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001606 return ret;
1607 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001608 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1609 if (ret < 0)
1610 return ret;
1611 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1612 rtd->num_codecs, rtd);
1613 if (ret < 0)
1614 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301615 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001616 INIT_DELAYED_WORK(&rtd->delayed_work,
1617 codec2codec_close_delayed_work);
1618
Namarta Kohli1245b702012-08-16 17:10:41 +05301619 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001620 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001621 if (ret)
1622 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001623 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624 }
1625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 return 0;
1627}
1628
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001629static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001630{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001631 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001632 struct snd_soc_component *component;
1633 const char *name;
1634 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001635
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001636 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1637 /* codecs, usually analog devices */
1638 name = aux_dev->codec_name;
1639 codec_of_node = aux_dev->codec_of_node;
1640 component = soc_find_component(codec_of_node, name);
1641 if (!component) {
1642 if (codec_of_node)
1643 name = of_node_full_name(codec_of_node);
1644 goto err_defer;
1645 }
1646 } else if (aux_dev->name) {
1647 /* generic components */
1648 name = aux_dev->name;
1649 component = soc_find_component(NULL, name);
1650 if (!component)
1651 goto err_defer;
1652 } else {
1653 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1654 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001655 }
1656
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001657 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001658 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001659
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001660 return 0;
1661
1662err_defer:
1663 dev_err(card->dev, "ASoC: %s not registered\n", name);
1664 return -EPROBE_DEFER;
1665}
1666
1667static int soc_probe_aux_devices(struct snd_soc_card *card)
1668{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001669 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001670 int order;
1671 int ret;
1672
1673 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1674 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001675 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001676 if (comp->driver->probe_order == order) {
1677 ret = soc_probe_component(card, comp);
1678 if (ret < 0) {
1679 dev_err(card->dev,
1680 "ASoC: failed to probe aux component %s %d\n",
1681 comp->name, ret);
1682 return ret;
1683 }
1684 }
1685 }
1686 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001687
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001688 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001689}
1690
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001691static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001692{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001693 struct snd_soc_component *comp, *_comp;
1694 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001695
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001696 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1697 order++) {
1698 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001699 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001700
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001701 if (comp->driver->remove_order == order) {
1702 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001703 /* remove it from the card's aux_comp_list */
1704 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001705 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001706 }
1707 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001708}
1709
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001710/**
1711 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1712 * @rtd: The runtime for which the DAI link format should be changed
1713 * @dai_fmt: The new DAI link format
1714 *
1715 * This function updates the DAI link format for all DAIs connected to the DAI
1716 * link for the specified runtime.
1717 *
1718 * Note: For setups with a static format set the dai_fmt field in the
1719 * corresponding snd_dai_link struct instead of using this function.
1720 *
1721 * Returns 0 on success, otherwise a negative error code.
1722 */
1723int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1724 unsigned int dai_fmt)
1725{
1726 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1727 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1728 unsigned int i;
1729 int ret;
1730
1731 for (i = 0; i < rtd->num_codecs; i++) {
1732 struct snd_soc_dai *codec_dai = codec_dais[i];
1733
1734 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1735 if (ret != 0 && ret != -ENOTSUPP) {
1736 dev_warn(codec_dai->dev,
1737 "ASoC: Failed to set DAI format: %d\n", ret);
1738 return ret;
1739 }
1740 }
1741
1742 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001743 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001744 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001745 unsigned int inv_dai_fmt;
1746
1747 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1748 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1749 case SND_SOC_DAIFMT_CBM_CFM:
1750 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1751 break;
1752 case SND_SOC_DAIFMT_CBM_CFS:
1753 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1754 break;
1755 case SND_SOC_DAIFMT_CBS_CFM:
1756 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1757 break;
1758 case SND_SOC_DAIFMT_CBS_CFS:
1759 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1760 break;
1761 }
1762
1763 dai_fmt = inv_dai_fmt;
1764 }
1765
1766 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1767 if (ret != 0 && ret != -ENOTSUPP) {
1768 dev_warn(cpu_dai->dev,
1769 "ASoC: Failed to set DAI format: %d\n", ret);
1770 return ret;
1771 }
1772
1773 return 0;
1774}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001775EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001776
Liam Girdwood345233d2017-01-14 16:13:02 +08001777
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001778#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001779/* Trim special characters, and replace '-' with '_' since '-' is used to
1780 * separate different DMI fields in the card long name. Only number and
1781 * alphabet characters and a few separator characters are kept.
1782 */
1783static void cleanup_dmi_name(char *name)
1784{
1785 int i, j = 0;
1786
1787 for (i = 0; name[i]; i++) {
1788 if (isalnum(name[i]) || (name[i] == '.')
1789 || (name[i] == '_'))
1790 name[j++] = name[i];
1791 else if (name[i] == '-')
1792 name[j++] = '_';
1793 }
1794
1795 name[j] = '\0';
1796}
1797
Mengdong Lin98faf432017-06-28 15:01:39 +08001798/* Check if a DMI field is valid, i.e. not containing any string
1799 * in the black list.
1800 */
1801static int is_dmi_valid(const char *field)
1802{
1803 int i = 0;
1804
1805 while (dmi_blacklist[i]) {
1806 if (strstr(field, dmi_blacklist[i]))
1807 return 0;
1808 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001809 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001810
1811 return 1;
1812}
1813
Liam Girdwood345233d2017-01-14 16:13:02 +08001814/**
1815 * snd_soc_set_dmi_name() - Register DMI names to card
1816 * @card: The card to register DMI names
1817 * @flavour: The flavour "differentiator" for the card amongst its peers.
1818 *
1819 * An Intel machine driver may be used by many different devices but are
1820 * difficult for userspace to differentiate, since machine drivers ususally
1821 * use their own name as the card short name and leave the card long name
1822 * blank. To differentiate such devices and fix bugs due to lack of
1823 * device-specific configurations, this function allows DMI info to be used
1824 * as the sound card long name, in the format of
1825 * "vendor-product-version-board"
1826 * (Character '-' is used to separate different DMI fields here).
1827 * This will help the user space to load the device-specific Use Case Manager
1828 * (UCM) configurations for the card.
1829 *
1830 * Possible card long names may be:
1831 * DellInc.-XPS139343-01-0310JH
1832 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1833 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1834 *
1835 * This function also supports flavoring the card longname to provide
1836 * the extra differentiation, like "vendor-product-version-board-flavor".
1837 *
1838 * We only keep number and alphabet characters and a few separator characters
1839 * in the card long name since UCM in the user space uses the card long names
1840 * as card configuration directory names and AudoConf cannot support special
1841 * charactors like SPACE.
1842 *
1843 * Returns 0 on success, otherwise a negative error code.
1844 */
1845int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1846{
1847 const char *vendor, *product, *product_version, *board;
1848 size_t longname_buf_size = sizeof(card->snd_card->longname);
1849 size_t len;
1850
1851 if (card->long_name)
1852 return 0; /* long name already set by driver or from DMI */
1853
1854 /* make up dmi long name as: vendor.product.version.board */
1855 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001856 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001857 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1858 return 0;
1859 }
1860
Mengdong Lin98faf432017-06-28 15:01:39 +08001861
Liam Girdwood345233d2017-01-14 16:13:02 +08001862 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1863 "%s", vendor);
1864 cleanup_dmi_name(card->dmi_longname);
1865
1866 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001867 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001868 len = strlen(card->dmi_longname);
1869 snprintf(card->dmi_longname + len,
1870 longname_buf_size - len,
1871 "-%s", product);
1872
1873 len++; /* skip the separator "-" */
1874 if (len < longname_buf_size)
1875 cleanup_dmi_name(card->dmi_longname + len);
1876
1877 /* some vendors like Lenovo may only put a self-explanatory
1878 * name in the product version field
1879 */
1880 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001881 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001882 len = strlen(card->dmi_longname);
1883 snprintf(card->dmi_longname + len,
1884 longname_buf_size - len,
1885 "-%s", product_version);
1886
1887 len++;
1888 if (len < longname_buf_size)
1889 cleanup_dmi_name(card->dmi_longname + len);
1890 }
1891 }
1892
1893 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001894 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001895 len = strlen(card->dmi_longname);
1896 snprintf(card->dmi_longname + len,
1897 longname_buf_size - len,
1898 "-%s", board);
1899
1900 len++;
1901 if (len < longname_buf_size)
1902 cleanup_dmi_name(card->dmi_longname + len);
1903 } else if (!product) {
1904 /* fall back to using legacy name */
1905 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1906 return 0;
1907 }
1908
1909 /* Add flavour to dmi long name */
1910 if (flavour) {
1911 len = strlen(card->dmi_longname);
1912 snprintf(card->dmi_longname + len,
1913 longname_buf_size - len,
1914 "-%s", flavour);
1915
1916 len++;
1917 if (len < longname_buf_size)
1918 cleanup_dmi_name(card->dmi_longname + len);
1919 }
1920
1921 /* set the card long name */
1922 card->long_name = card->dmi_longname;
1923
1924 return 0;
1925}
1926EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001927#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001928
Liam Girdwooda655de82018-07-02 16:59:54 +01001929static void soc_check_tplg_fes(struct snd_soc_card *card)
1930{
1931 struct snd_soc_component *component;
1932 const struct snd_soc_component_driver *comp_drv;
1933 struct snd_soc_dai_link *dai_link;
1934 int i;
1935
1936 list_for_each_entry(component, &component_list, list) {
1937
1938 /* does this component override FEs ? */
1939 if (!component->driver->ignore_machine)
1940 continue;
1941
1942 /* for this machine ? */
1943 if (strcmp(component->driver->ignore_machine,
1944 card->dev->driver->name))
1945 continue;
1946
1947 /* machine matches, so override the rtd data */
1948 for (i = 0; i < card->num_links; i++) {
1949
1950 dai_link = &card->dai_link[i];
1951
1952 /* ignore this FE */
1953 if (dai_link->dynamic) {
1954 dai_link->ignore = true;
1955 continue;
1956 }
1957
1958 dev_info(card->dev, "info: override FE DAI link %s\n",
1959 card->dai_link[i].name);
1960
1961 /* override platform component */
1962 dai_link->platform_name = component->name;
1963
1964 /* convert non BE into BE */
1965 dai_link->no_pcm = 1;
1966
1967 /* override any BE fixups */
1968 dai_link->be_hw_params_fixup =
1969 component->driver->be_hw_params_fixup;
1970
1971 /* most BE links don't set stream name, so set it to
1972 * dai link name if it's NULL to help bind widgets.
1973 */
1974 if (!dai_link->stream_name)
1975 dai_link->stream_name = dai_link->name;
1976 }
1977
1978 /* Inform userspace we are using alternate topology */
1979 if (component->driver->topology_name_prefix) {
1980
1981 /* topology shortname created ? */
1982 if (!card->topology_shortname_created) {
1983 comp_drv = component->driver;
1984
1985 snprintf(card->topology_shortname, 32, "%s-%s",
1986 comp_drv->topology_name_prefix,
1987 card->name);
1988 card->topology_shortname_created = true;
1989 }
1990
1991 /* use topology shortname */
1992 card->name = card->topology_shortname;
1993 }
1994 }
1995}
1996
Mark Brownb19e6e72012-03-14 21:18:39 +00001997static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998{
Mengdong Lin1a497982015-11-18 02:34:11 -05001999 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002000 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002001 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002003 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002004 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002005
Liam Girdwooda655de82018-07-02 16:59:54 +01002006 /* check whether any platform is ignore machine FE and using topology */
2007 soc_check_tplg_fes(card);
2008
Mark Brownb19e6e72012-03-14 21:18:39 +00002009 /* bind DAIs */
2010 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002011 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002012 if (ret != 0)
2013 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002014 }
2015
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002016 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002017 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002018 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002019 if (ret != 0)
2020 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002021 }
2022
Mengdong Linf8f80362015-12-02 14:11:22 +08002023 /* add predefined DAI links to the list */
2024 for (i = 0; i < card->num_links; i++)
2025 snd_soc_add_dai_link(card, card->dai_link+i);
2026
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002028 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002029 card->owner, 0, &card->snd_card);
2030 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002031 dev_err(card->dev,
2032 "ASoC: can't create sound card for card %s: %d\n",
2033 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002034 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002035 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002036
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002037 soc_init_card_debugfs(card);
2038
Mark Browne37a4972011-03-02 18:21:57 +00002039 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2040 card->dapm.dev = card->dev;
2041 card->dapm.card = card;
2042 list_add(&card->dapm.list, &card->dapm_list);
2043
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002044#ifdef CONFIG_DEBUG_FS
2045 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2046#endif
2047
Mark Brown88ee1c62011-02-02 10:43:26 +00002048#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002049 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002050 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002051#endif
Andy Green6ed25972008-06-13 16:24:05 +01002052
Mark Brown9a841eb2011-04-12 17:51:37 -07002053 if (card->dapm_widgets)
2054 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2055 card->num_dapm_widgets);
2056
Nicolin Chenf23e8602015-02-14 17:22:49 -08002057 if (card->of_dapm_widgets)
2058 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2059 card->num_of_dapm_widgets);
2060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002061 /* initialise the sound card only once */
2062 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002063 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002064 if (ret < 0)
2065 goto card_probe_error;
2066 }
2067
Stephen Warren62ae68f2012-06-08 12:34:23 -06002068 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002069 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2070 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002071 list_for_each_entry(rtd, &card->rtd_list, list) {
2072 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002073 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002074 dev_err(card->dev,
2075 "ASoC: failed to instantiate card %d\n",
2076 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002077 goto probe_dai_err;
2078 }
2079 }
2080 }
2081
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002082 /* probe auxiliary components */
2083 ret = soc_probe_aux_devices(card);
2084 if (ret < 0)
2085 goto probe_dai_err;
2086
Mengdong Lin61b00882015-12-02 14:11:48 +08002087 /* Find new DAI links added during probing components and bind them.
2088 * Components with topology may bring new DAIs and DAI links.
2089 */
2090 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2091 if (soc_is_dai_link_bound(card, dai_link))
2092 continue;
2093
2094 ret = soc_init_dai_link(card, dai_link);
2095 if (ret)
2096 goto probe_dai_err;
2097 ret = soc_bind_dai_link(card, dai_link);
2098 if (ret)
2099 goto probe_dai_err;
2100 }
2101
Stephen Warren62ae68f2012-06-08 12:34:23 -06002102 /* probe all DAI links on this card */
2103 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2104 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002105 list_for_each_entry(rtd, &card->rtd_list, list) {
2106 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002107 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002108 dev_err(card->dev,
2109 "ASoC: failed to instantiate card %d\n",
2110 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002111 goto probe_dai_err;
2112 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002113 }
2114 }
2115
Mark Brown888df392012-02-16 19:37:51 -08002116 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002117 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002118
Mark Brownb7af1da2011-04-07 19:18:44 +09002119 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002120 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002121
Mark Brownb8ad29d2011-03-02 18:35:51 +00002122 if (card->dapm_routes)
2123 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2124 card->num_dapm_routes);
2125
Nicolin Chenf23e8602015-02-14 17:22:49 -08002126 if (card->of_dapm_routes)
2127 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2128 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002129
Takashi Iwai861886d2017-04-24 08:54:42 +02002130 /* try to set some sane longname if DMI is available */
2131 snd_soc_set_dmi_name(card, NULL);
2132
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002133 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002134 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002135 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2136 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002137 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2138 "%s", card->driver_name ? card->driver_name : card->name);
2139 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2140 switch (card->snd_card->driver[i]) {
2141 case '_':
2142 case '-':
2143 case '\0':
2144 break;
2145 default:
2146 if (!isalnum(card->snd_card->driver[i]))
2147 card->snd_card->driver[i] = '_';
2148 break;
2149 }
2150 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002151
Mark Brown28e9ad92011-03-02 18:36:34 +00002152 if (card->late_probe) {
2153 ret = card->late_probe(card);
2154 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002155 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002156 card->name, ret);
2157 goto probe_aux_dev_err;
2158 }
2159 }
2160
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002161 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002163 ret = snd_card_register(card->snd_card);
2164 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002165 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2166 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002167 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002168 }
2169
Mark Brown435c5e22008-12-04 15:32:53 +00002170 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002171 snd_soc_dapm_sync(&card->dapm);
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +01002172 if (card->auto_bind && !card->components_added) {
2173 component_master_add_with_match(card->dev,
2174 &snd_soc_card_comp_ops,
2175 card->match);
2176 card->components_added = true;
2177 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002178 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002179 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002180
2181 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002183probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002184 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002186probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002187 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002189card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002190 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002191 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002192
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002193 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002194 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002195 snd_card_free(card->snd_card);
2196
Mark Brownb19e6e72012-03-14 21:18:39 +00002197base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002198 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002200 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201
Mark Brownb19e6e72012-03-14 21:18:39 +00002202 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002203}
2204
2205/* probes a new socdev */
2206static int soc_probe(struct platform_device *pdev)
2207{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002208 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002209
Vinod Koul70a7ca32011-01-14 19:22:48 +05302210 /*
2211 * no card, so machine driver should be registering card
2212 * we should not be here in that case so ret error
2213 */
2214 if (!card)
2215 return -EINVAL;
2216
Mark Brownfe4085e2012-03-02 13:07:41 +00002217 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002218 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002219 card->name);
2220
Mark Brown435c5e22008-12-04 15:32:53 +00002221 /* Bodge while we unpick instantiation */
2222 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002223
Mark Brown28d528c2012-08-09 18:45:23 +01002224 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002225}
2226
Vinod Koulb0e26482011-01-13 22:48:02 +05302227static int soc_cleanup_card_resources(struct snd_soc_card *card)
2228{
Mengdong Lin1a497982015-11-18 02:34:11 -05002229 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302230
2231 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002232 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002233 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302234
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002235 /* free the ALSA card at first; this syncs with pending operations */
2236 snd_card_free(card->snd_card);
2237
Vinod Koulb0e26482011-01-13 22:48:02 +05302238 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002239 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002240 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302241
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002242 /* remove auxiliary devices */
2243 soc_remove_aux_devices(card);
2244
Mark Brownd1e81422016-08-18 19:32:59 +01002245 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302246 soc_cleanup_card_debugfs(card);
2247
2248 /* remove the card */
2249 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002250 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302251
Vinod Koulb0e26482011-01-13 22:48:02 +05302252 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302253}
2254
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255/* removes a socdev */
2256static int soc_remove(struct platform_device *pdev)
2257{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259
Mark Brownc5af3a22008-11-28 13:29:45 +00002260 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 return 0;
2262}
2263
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002264int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002265{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002266 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002267 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002268
2269 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002270 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002271
2272 /* Flush out pmdown_time work - we actually do want to run it
2273 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002274 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002275 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002276
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002277 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002278
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002279 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002280 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002281 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002282 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002283
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002284 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002285 for (i = 0; i < rtd->num_codecs; i++) {
2286 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002287 pinctrl_pm_select_sleep_state(codec_dai->dev);
2288 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002289 }
2290
Mark Brown416356f2009-06-30 19:05:15 +01002291 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002292}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002293EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002294
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002295const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302296 .suspend = snd_soc_suspend,
2297 .resume = snd_soc_resume,
2298 .freeze = snd_soc_suspend,
2299 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002300 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302301 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002302};
Stephen Warrendeb26072011-04-05 19:35:30 -06002303EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002304
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305/* ASoC platform driver */
2306static struct platform_driver soc_driver = {
2307 .driver = {
2308 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002309 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 },
2311 .probe = soc_probe,
2312 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002313};
2314
Mark Brown096e49d2009-07-05 15:12:22 +01002315/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 * snd_soc_cnew - create new control
2317 * @_template: control template
2318 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002319 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002320 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321 *
2322 * Create a new mixer control from a template control.
2323 *
2324 * Returns 0 for success, else error.
2325 */
2326struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002327 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002328 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329{
2330 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002331 struct snd_kcontrol *kcontrol;
2332 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333
2334 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335 template.index = 0;
2336
Mark Brownefb7ac32011-03-08 17:23:24 +00002337 if (!long_name)
2338 long_name = template.name;
2339
2340 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002341 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002342 if (!name)
2343 return NULL;
2344
Mark Brownefb7ac32011-03-08 17:23:24 +00002345 template.name = name;
2346 } else {
2347 template.name = long_name;
2348 }
2349
2350 kcontrol = snd_ctl_new1(&template, data);
2351
2352 kfree(name);
2353
2354 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002355}
2356EXPORT_SYMBOL_GPL(snd_soc_cnew);
2357
Liam Girdwood022658b2012-02-03 17:43:09 +00002358static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2359 const struct snd_kcontrol_new *controls, int num_controls,
2360 const char *prefix, void *data)
2361{
2362 int err, i;
2363
2364 for (i = 0; i < num_controls; i++) {
2365 const struct snd_kcontrol_new *control = &controls[i];
2366 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2367 control->name, prefix));
2368 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002369 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2370 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002371 return err;
2372 }
2373 }
2374
2375 return 0;
2376}
2377
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002378struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2379 const char *name)
2380{
2381 struct snd_card *card = soc_card->snd_card;
2382 struct snd_kcontrol *kctl;
2383
2384 if (unlikely(!name))
2385 return NULL;
2386
2387 list_for_each_entry(kctl, &card->controls, list)
2388 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2389 return kctl;
2390 return NULL;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2393
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002395 * snd_soc_add_component_controls - Add an array of controls to a component.
2396 *
2397 * @component: Component to add controls to
2398 * @controls: Array of controls to add
2399 * @num_controls: Number of elements in the array
2400 *
2401 * Return: 0 for success, else error.
2402 */
2403int snd_soc_add_component_controls(struct snd_soc_component *component,
2404 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2405{
2406 struct snd_card *card = component->card->snd_card;
2407
2408 return snd_soc_add_controls(card, component->dev, controls,
2409 num_controls, component->name_prefix, component);
2410}
2411EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2412
2413/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002414 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2415 * Convenience function to add a list of controls.
2416 *
2417 * @soc_card: SoC card to add controls to
2418 * @controls: array of controls to add
2419 * @num_controls: number of elements in the array
2420 *
2421 * Return 0 for success, else error.
2422 */
2423int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2424 const struct snd_kcontrol_new *controls, int num_controls)
2425{
2426 struct snd_card *card = soc_card->snd_card;
2427
2428 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2429 NULL, soc_card);
2430}
2431EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2432
2433/**
2434 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2435 * Convienience function to add a list of controls.
2436 *
2437 * @dai: DAI to add controls to
2438 * @controls: array of controls to add
2439 * @num_controls: number of elements in the array
2440 *
2441 * Return 0 for success, else error.
2442 */
2443int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2444 const struct snd_kcontrol_new *controls, int num_controls)
2445{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002446 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002447
2448 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2449 NULL, dai);
2450}
2451EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2452
2453/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002454 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2455 * @dai: DAI
2456 * @clk_id: DAI specific clock ID
2457 * @freq: new clock frequency in Hz
2458 * @dir: new clock direction - input/output.
2459 *
2460 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2461 */
2462int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2463 unsigned int freq, int dir)
2464{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002465 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002466 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002467
2468 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2469 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002470}
2471EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2472
2473/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002474 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2475 * @component: COMPONENT
2476 * @clk_id: DAI specific clock ID
2477 * @source: Source for the clock
2478 * @freq: new clock frequency in Hz
2479 * @dir: new clock direction - input/output.
2480 *
2481 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2482 */
2483int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2484 int source, unsigned int freq, int dir)
2485{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002486 if (component->driver->set_sysclk)
2487 return component->driver->set_sysclk(component, clk_id, source,
2488 freq, dir);
2489
2490 return -ENOTSUPP;
2491}
2492EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2493
2494/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002495 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2496 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002497 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002498 * @div: new clock divisor.
2499 *
2500 * Configures the clock dividers. This is used to derive the best DAI bit and
2501 * frame clocks from the system or master clock. It's best to set the DAI bit
2502 * and frame clocks as low as possible to save system power.
2503 */
2504int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2505 int div_id, int div)
2506{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002507 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002508 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002509 else
2510 return -EINVAL;
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2513
2514/**
2515 * snd_soc_dai_set_pll - configure DAI PLL.
2516 * @dai: DAI
2517 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002518 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002519 * @freq_in: PLL input clock frequency in Hz
2520 * @freq_out: requested PLL output clock frequency in Hz
2521 *
2522 * Configures and enables PLL to generate output clock based on input clock.
2523 */
Mark Brown85488032009-09-05 18:52:16 +01002524int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2525 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002526{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002527 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002528 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002529 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002530
2531 return snd_soc_component_set_pll(dai->component, pll_id, source,
2532 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002533}
2534EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2535
Mark Brownec4ee522011-03-07 20:58:11 +00002536/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002537 * snd_soc_component_set_pll - configure component PLL.
2538 * @component: COMPONENT
2539 * @pll_id: DAI specific PLL ID
2540 * @source: DAI specific source for the PLL
2541 * @freq_in: PLL input clock frequency in Hz
2542 * @freq_out: requested PLL output clock frequency in Hz
2543 *
2544 * Configures and enables PLL to generate output clock based on input clock.
2545 */
2546int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2547 int source, unsigned int freq_in,
2548 unsigned int freq_out)
2549{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002550 if (component->driver->set_pll)
2551 return component->driver->set_pll(component, pll_id, source,
2552 freq_in, freq_out);
2553
2554 return -EINVAL;
2555}
2556EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2557
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002558/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002559 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2560 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002561 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002562 *
2563 * Configures the DAI for a preset BCLK to sample rate ratio.
2564 */
2565int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2566{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002567 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002568 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2569 else
2570 return -EINVAL;
2571}
2572EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2573
2574/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002575 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2576 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002577 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002578 *
2579 * Configures the DAI hardware format and clocking.
2580 */
2581int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2582{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002583 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002584 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002585 if (dai->driver->ops->set_fmt == NULL)
2586 return -ENOTSUPP;
2587 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002588}
2589EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2590
2591/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002592 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002593 * @slots: Number of slots in use.
2594 * @tx_mask: bitmask representing active TX slots.
2595 * @rx_mask: bitmask representing active RX slots.
2596 *
2597 * Generates the TDM tx and rx slot default masks for DAI.
2598 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002599static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002600 unsigned int *tx_mask,
2601 unsigned int *rx_mask)
2602{
2603 if (*tx_mask || *rx_mask)
2604 return 0;
2605
2606 if (!slots)
2607 return -EINVAL;
2608
2609 *tx_mask = (1 << slots) - 1;
2610 *rx_mask = (1 << slots) - 1;
2611
2612 return 0;
2613}
2614
2615/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002616 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2617 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002618 * @tx_mask: bitmask representing active TX slots.
2619 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002620 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002621 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002622 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002623 * This function configures the specified DAI for TDM operation. @slot contains
2624 * the total number of slots of the TDM stream and @slot_with the width of each
2625 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2626 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2627 * DAI should write to or read from. If a bit is set the corresponding slot is
2628 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2629 * the first slot, bit 1 to the second slot and so on. The first active slot
2630 * maps to the first channel of the DAI, the second active slot to the second
2631 * channel and so on.
2632 *
2633 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2634 * @rx_mask and @slot_width will be ignored.
2635 *
2636 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002637 */
2638int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002639 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002640{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002641 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002642 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002643 &tx_mask, &rx_mask);
2644 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002645 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002646
Benoit Cousson88bd8702014-07-08 23:19:34 +02002647 dai->tx_mask = tx_mask;
2648 dai->rx_mask = rx_mask;
2649
Kuninori Morimoto46471922017-09-25 01:38:34 +00002650 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002651 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002652 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002653 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002654 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002655}
2656EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2657
2658/**
Barry Song472df3c2009-09-12 01:16:29 +08002659 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2660 * @dai: DAI
2661 * @tx_num: how many TX channels
2662 * @tx_slot: pointer to an array which imply the TX slot number channel
2663 * 0~num-1 uses
2664 * @rx_num: how many RX channels
2665 * @rx_slot: pointer to an array which imply the RX slot number channel
2666 * 0~num-1 uses
2667 *
2668 * configure the relationship between channel number and TDM slot number.
2669 */
2670int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2671 unsigned int tx_num, unsigned int *tx_slot,
2672 unsigned int rx_num, unsigned int *rx_slot)
2673{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002674 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002675 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002676 rx_num, rx_slot);
2677 else
2678 return -EINVAL;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2681
2682/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002683 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2684 * @dai: DAI
2685 * @tristate: tristate enable
2686 *
2687 * Tristates the DAI so that others can use it.
2688 */
2689int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2690{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002691 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002692 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002693 else
2694 return -EINVAL;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2697
2698/**
2699 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2700 * @dai: DAI
2701 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002702 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002703 *
2704 * Mutes the DAI DAC.
2705 */
Mark Brownda183962013-02-06 15:44:07 +00002706int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2707 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002708{
Mark Brownda183962013-02-06 15:44:07 +00002709 if (!dai->driver)
2710 return -ENOTSUPP;
2711
2712 if (dai->driver->ops->mute_stream)
2713 return dai->driver->ops->mute_stream(dai, mute, direction);
2714 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2715 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002716 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002717 else
Mark Brown04570c62012-04-13 19:16:03 +01002718 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002719}
2720EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2721
Mark Brownc5af3a22008-11-28 13:29:45 +00002722/**
2723 * snd_soc_register_card - Register a card with the ASoC core
2724 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002725 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002726 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002727 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302728int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002729{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002730 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002731 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002732
Mark Brownc5af3a22008-11-28 13:29:45 +00002733 if (!card->name || !card->dev)
2734 return -EINVAL;
2735
Stephen Warren5a504962011-12-21 10:40:59 -07002736 for (i = 0; i < card->num_links; i++) {
2737 struct snd_soc_dai_link *link = &card->dai_link[i];
2738
Mengdong Lin923c5e612015-11-23 11:01:49 -05002739 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002740 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002741 dev_err(card->dev, "ASoC: failed to init link %s\n",
2742 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002743 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002744 }
Stephen Warren5a504962011-12-21 10:40:59 -07002745 }
2746
Mark Browned77cc12011-05-03 18:25:34 +01002747 dev_set_drvdata(card->dev, card);
2748
Stephen Warren111c6412011-01-28 14:26:35 -07002749 snd_soc_initialize_card_lists(card);
2750
Mengdong Linf8f80362015-12-02 14:11:22 +08002751 INIT_LIST_HEAD(&card->dai_link_list);
2752 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002753
Mengdong Lin1a497982015-11-18 02:34:11 -05002754 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002755 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002756
Mark Browndb432b42011-10-03 21:06:40 +01002757 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002758 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759 card->instantiated = 0;
2760 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002761 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002762
Mark Brownb19e6e72012-03-14 21:18:39 +00002763 ret = snd_soc_instantiate_card(card);
2764 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002765 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002766
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002767 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002768 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002769 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2770 int j;
2771
2772 for (j = 0; j < rtd->num_codecs; j++) {
2773 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2774 if (!codec_dai->active)
2775 pinctrl_pm_select_sleep_state(codec_dai->dev);
2776 }
2777
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002778 if (!cpu_dai->active)
2779 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2780 }
2781
Mark Brownb19e6e72012-03-14 21:18:39 +00002782 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002783}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302784EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002785
2786/**
2787 * snd_soc_unregister_card - Unregister a card with the ASoC core
2788 *
2789 * @card: Card to unregister
2790 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002791 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302792int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002793{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002794 if (card->instantiated) {
2795 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002796 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302797 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002798 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002799 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002800
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +01002801 if (!card->auto_bind && card->components_added)
2802 component_master_del(card->dev, &snd_soc_card_comp_ops);
2803
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002804 return 0;
2805}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302806EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002807
2808/*
2809 * Simplify DAI link configuration by removing ".-1" from device names
2810 * and sanitizing names.
2811 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002812static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002813{
2814 char *found, name[NAME_SIZE];
2815 int id1, id2;
2816
2817 if (dev_name(dev) == NULL)
2818 return NULL;
2819
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002820 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002821
2822 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002823 found = strstr(name, dev->driver->name);
2824 if (found) {
2825 /* get ID */
2826 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2827
2828 /* discard ID from name if ID == -1 */
2829 if (*id == -1)
2830 found[strlen(dev->driver->name)] = '\0';
2831 }
2832
2833 } else {
2834 /* I2C component devices are named "bus-addr" */
2835 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2836 char tmp[NAME_SIZE];
2837
2838 /* create unique ID number from I2C addr and bus */
2839 *id = ((id1 & 0xffff) << 16) + id2;
2840
2841 /* sanitize component name for DAI link creation */
2842 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002843 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002844 } else
2845 *id = 0;
2846 }
2847
2848 return kstrdup(name, GFP_KERNEL);
2849}
2850
2851/*
2852 * Simplify DAI link naming for single devices with multiple DAIs by removing
2853 * any ".-1" and using the DAI name (instead of device name).
2854 */
2855static inline char *fmt_multiple_name(struct device *dev,
2856 struct snd_soc_dai_driver *dai_drv)
2857{
2858 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002859 dev_err(dev,
2860 "ASoC: error - multiple DAI %s registered with no name\n",
2861 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002862 return NULL;
2863 }
2864
2865 return kstrdup(dai_drv->name, GFP_KERNEL);
2866}
2867
2868/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002869 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002870 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002871 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002872 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002873static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002874{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002875 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002876
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002877 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002878 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2879 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002880 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002881 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002882 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002883 }
Mark Brown91151712008-11-30 23:31:24 +00002884}
Mark Brown91151712008-11-30 23:31:24 +00002885
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002886/* Create a DAI and add it to the component's DAI list */
2887static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2888 struct snd_soc_dai_driver *dai_drv,
2889 bool legacy_dai_naming)
2890{
2891 struct device *dev = component->dev;
2892 struct snd_soc_dai *dai;
2893
2894 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2895
2896 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2897 if (dai == NULL)
2898 return NULL;
2899
2900 /*
2901 * Back in the old days when we still had component-less DAIs,
2902 * instead of having a static name, component-less DAIs would
2903 * inherit the name of the parent device so it is possible to
2904 * register multiple instances of the DAI. We still need to keep
2905 * the same naming style even though those DAIs are not
2906 * component-less anymore.
2907 */
2908 if (legacy_dai_naming &&
2909 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2910 dai->name = fmt_single_name(dev, &dai->id);
2911 } else {
2912 dai->name = fmt_multiple_name(dev, dai_drv);
2913 if (dai_drv->id)
2914 dai->id = dai_drv->id;
2915 else
2916 dai->id = component->num_dai;
2917 }
2918 if (dai->name == NULL) {
2919 kfree(dai);
2920 return NULL;
2921 }
2922
2923 dai->component = component;
2924 dai->dev = dev;
2925 dai->driver = dai_drv;
2926 if (!dai->driver->ops)
2927 dai->driver->ops = &null_dai_ops;
2928
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002929 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002930 component->num_dai++;
2931
2932 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2933 return dai;
2934}
2935
Mark Brown91151712008-11-30 23:31:24 +00002936/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002937 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002938 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002939 * @component: The component the DAIs are registered for
2940 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002941 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002942 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2943 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002944 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002945static int snd_soc_register_dais(struct snd_soc_component *component,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002946 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002947{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002948 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002950 unsigned int i;
2951 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002952
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002953 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002954
2955 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002956
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002957 dai = soc_add_dai(component, dai_drv + i,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002958 count == 1 && !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002959 if (dai == NULL) {
2960 ret = -ENOMEM;
2961 goto err;
2962 }
Mark Brown91151712008-11-30 23:31:24 +00002963 }
2964
2965 return 0;
2966
2967err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002968 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002969
2970 return ret;
2971}
Mark Brown91151712008-11-30 23:31:24 +00002972
Mengdong Lin68003e62015-12-31 16:40:43 +08002973/**
2974 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2975 *
2976 * @component: The component the DAIs are registered for
2977 * @dai_drv: DAI driver to use for the DAI
2978 *
2979 * Topology can use this API to register DAIs when probing a component.
2980 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2981 * will be freed in the component cleanup.
2982 */
2983int snd_soc_register_dai(struct snd_soc_component *component,
2984 struct snd_soc_dai_driver *dai_drv)
2985{
2986 struct snd_soc_dapm_context *dapm =
2987 snd_soc_component_get_dapm(component);
2988 struct snd_soc_dai *dai;
2989 int ret;
2990
2991 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2992 dev_err(component->dev, "Invalid dai type %d\n",
2993 dai_drv->dobj.type);
2994 return -EINVAL;
2995 }
2996
2997 lockdep_assert_held(&client_mutex);
2998 dai = soc_add_dai(component, dai_drv, false);
2999 if (!dai)
3000 return -ENOMEM;
3001
3002 /* Create the DAI widgets here. After adding DAIs, topology may
3003 * also add routes that need these widgets as source or sink.
3004 */
3005 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3006 if (ret != 0) {
3007 dev_err(component->dev,
3008 "Failed to create DAI widgets %d\n", ret);
3009 }
3010
3011 return ret;
3012}
3013EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3014
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003015static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3016 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003017{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003018 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003019
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003020 component->driver->seq_notifier(component, type, subseq);
3021}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003022
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003023static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3024 int event)
3025{
3026 struct snd_soc_component *component = dapm->component;
3027
3028 return component->driver->stream_event(component, event);
3029}
3030
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003031static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3032 enum snd_soc_bias_level level)
3033{
3034 struct snd_soc_component *component = dapm->component;
3035
3036 return component->driver->set_bias_level(component, level);
3037}
3038
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003039static int snd_soc_component_initialize(struct snd_soc_component *component,
3040 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003041{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003042 struct snd_soc_dapm_context *dapm;
3043
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003044 component->name = fmt_single_name(dev, &component->id);
3045 if (!component->name) {
3046 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003047 return -ENOMEM;
3048 }
3049
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003050 component->dev = dev;
3051 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003052
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003053 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003054 dapm->dev = dev;
3055 dapm->component = component;
3056 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003057 dapm->idle_bias_off = !driver->idle_bias_on;
3058 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003059 if (driver->seq_notifier)
3060 dapm->seq_notifier = snd_soc_component_seq_notifier;
3061 if (driver->stream_event)
3062 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003063 if (driver->set_bias_level)
3064 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003065
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003066 INIT_LIST_HEAD(&component->dai_list);
3067 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003068
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003069 return 0;
3070}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003071
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003072static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003073{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003074 int val_bytes = regmap_get_val_bytes(component->regmap);
3075
3076 /* Errors are legitimate for non-integer byte multiples */
3077 if (val_bytes > 0)
3078 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003079}
3080
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003081#ifdef CONFIG_REGMAP
3082
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003083/**
3084 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3085 * @component: The component for which to initialize the regmap instance
3086 * @regmap: The regmap instance that should be used by the component
3087 *
3088 * This function allows deferred assignment of the regmap instance that is
3089 * associated with the component. Only use this if the regmap instance is not
3090 * yet ready when the component is registered. The function must also be called
3091 * before the first IO attempt of the component.
3092 */
3093void snd_soc_component_init_regmap(struct snd_soc_component *component,
3094 struct regmap *regmap)
3095{
3096 component->regmap = regmap;
3097 snd_soc_component_setup_regmap(component);
3098}
3099EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3100
3101/**
3102 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3103 * @component: The component for which to de-initialize the regmap instance
3104 *
3105 * Calls regmap_exit() on the regmap instance associated to the component and
3106 * removes the regmap instance from the component.
3107 *
3108 * This function should only be used if snd_soc_component_init_regmap() was used
3109 * to initialize the regmap instance.
3110 */
3111void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3112{
3113 regmap_exit(component->regmap);
3114 component->regmap = NULL;
3115}
3116EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3117
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003118#endif
3119
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003120static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003121{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003122 mutex_lock(&client_mutex);
3123
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003124 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003125 if (!component->regmap)
3126 component->regmap = dev_get_regmap(component->dev, NULL);
3127 if (component->regmap)
3128 snd_soc_component_setup_regmap(component);
3129 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003130
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003131 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003132 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003133
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003134 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003135}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003136
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003137static void snd_soc_component_cleanup(struct snd_soc_component *component)
3138{
3139 snd_soc_unregister_dais(component);
3140 kfree(component->name);
3141}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003142
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003143static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3144{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003145 struct snd_soc_card *card = component->card;
3146
3147 if (card)
3148 snd_soc_unregister_card(card);
3149
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003150 list_del(&component->list);
3151}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003152
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003153#define ENDIANNESS_MAP(name) \
3154 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3155static u64 endianness_format_map[] = {
3156 ENDIANNESS_MAP(S16_),
3157 ENDIANNESS_MAP(U16_),
3158 ENDIANNESS_MAP(S24_),
3159 ENDIANNESS_MAP(U24_),
3160 ENDIANNESS_MAP(S32_),
3161 ENDIANNESS_MAP(U32_),
3162 ENDIANNESS_MAP(S24_3),
3163 ENDIANNESS_MAP(U24_3),
3164 ENDIANNESS_MAP(S20_3),
3165 ENDIANNESS_MAP(U20_3),
3166 ENDIANNESS_MAP(S18_3),
3167 ENDIANNESS_MAP(U18_3),
3168 ENDIANNESS_MAP(FLOAT_),
3169 ENDIANNESS_MAP(FLOAT64_),
3170 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3171};
3172
3173/*
3174 * Fix up the DAI formats for endianness: codecs don't actually see
3175 * the endianness of the data but we're using the CPU format
3176 * definitions which do need to include endianness so we ensure that
3177 * codec DAIs always have both big and little endian variants set.
3178 */
3179static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3180{
3181 int i;
3182
3183 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3184 if (stream->formats & endianness_format_map[i])
3185 stream->formats |= endianness_format_map[i];
3186}
3187
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003188int snd_soc_add_component(struct device *dev,
3189 struct snd_soc_component *component,
3190 const struct snd_soc_component_driver *component_driver,
3191 struct snd_soc_dai_driver *dai_drv,
3192 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003193{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003194 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003195 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003196
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003197 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003198 if (ret)
3199 goto err_free;
3200
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003201 if (component_driver->endianness) {
3202 for (i = 0; i < num_dai; i++) {
3203 convert_endianness_formats(&dai_drv[i].playback);
3204 convert_endianness_formats(&dai_drv[i].capture);
3205 }
3206 }
3207
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003208 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003209 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003210 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003211 goto err_cleanup;
3212 }
3213
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003214 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003215
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +01003216 ret = component_add(dev, NULL);
3217 if (ret < 0) {
3218 dev_err(dev, "ASoC: Failed to add Component: %d\n", ret);
3219 goto err_comp;
3220 }
3221
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003222 return 0;
3223
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +01003224err_comp:
3225 soc_remove_component(component);
3226 snd_soc_unregister_dais(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003227err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003228 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003229err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003230 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003231}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003232EXPORT_SYMBOL_GPL(snd_soc_add_component);
3233
3234int snd_soc_register_component(struct device *dev,
3235 const struct snd_soc_component_driver *component_driver,
3236 struct snd_soc_dai_driver *dai_drv,
3237 int num_dai)
3238{
3239 struct snd_soc_component *component;
3240
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003241 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003242 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003243 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003244
3245 return snd_soc_add_component(dev, component, component_driver,
3246 dai_drv, num_dai);
3247}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003248EXPORT_SYMBOL_GPL(snd_soc_register_component);
3249
3250/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003251 * snd_soc_unregister_component - Unregister all related component
3252 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003253 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003254 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003255 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003256static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003257{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003258 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003259 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003260
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003261 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003262 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003263 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003264 continue;
3265
3266 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3267 snd_soc_component_del_unlocked(component);
3268 found = 1;
3269 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003270 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003271 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003272
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003273 if (found) {
Srinivas Kandagatlabb4b8942018-07-13 16:36:28 +01003274 component_del(dev, NULL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003275 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003276 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003277
3278 return found;
3279}
3280
3281void snd_soc_unregister_component(struct device *dev)
3282{
3283 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003284}
3285EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3286
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003287struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3288 const char *driver_name)
3289{
3290 struct snd_soc_component *component;
3291 struct snd_soc_component *ret;
3292
3293 ret = NULL;
3294 mutex_lock(&client_mutex);
3295 list_for_each_entry(component, &component_list, list) {
3296 if (dev != component->dev)
3297 continue;
3298
3299 if (driver_name &&
3300 (driver_name != component->driver->name) &&
3301 (strcmp(component->driver->name, driver_name) != 0))
3302 continue;
3303
3304 ret = component;
3305 break;
3306 }
3307 mutex_unlock(&client_mutex);
3308
3309 return ret;
3310}
3311EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3312
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003313/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003314int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3315 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003316{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003317 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003318 int ret;
3319
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303320 if (!card->dev) {
3321 pr_err("card->dev is not set before calling %s\n", __func__);
3322 return -EINVAL;
3323 }
3324
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003325 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303326
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003327 ret = of_property_read_string_index(np, propname, 0, &card->name);
3328 /*
3329 * EINVAL means the property does not exist. This is fine providing
3330 * card->name was previously set, which is checked later in
3331 * snd_soc_register_card.
3332 */
3333 if (ret < 0 && ret != -EINVAL) {
3334 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003335 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003336 propname, ret);
3337 return ret;
3338 }
3339
3340 return 0;
3341}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003342EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003343
Xiubo Li9a6d4862014-02-08 15:59:52 +08003344static const struct snd_soc_dapm_widget simple_widgets[] = {
3345 SND_SOC_DAPM_MIC("Microphone", NULL),
3346 SND_SOC_DAPM_LINE("Line", NULL),
3347 SND_SOC_DAPM_HP("Headphone", NULL),
3348 SND_SOC_DAPM_SPK("Speaker", NULL),
3349};
3350
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003351int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003352 const char *propname)
3353{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003354 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003355 struct snd_soc_dapm_widget *widgets;
3356 const char *template, *wname;
3357 int i, j, num_widgets, ret;
3358
3359 num_widgets = of_property_count_strings(np, propname);
3360 if (num_widgets < 0) {
3361 dev_err(card->dev,
3362 "ASoC: Property '%s' does not exist\n", propname);
3363 return -EINVAL;
3364 }
3365 if (num_widgets & 1) {
3366 dev_err(card->dev,
3367 "ASoC: Property '%s' length is not even\n", propname);
3368 return -EINVAL;
3369 }
3370
3371 num_widgets /= 2;
3372 if (!num_widgets) {
3373 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3374 propname);
3375 return -EINVAL;
3376 }
3377
3378 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3379 GFP_KERNEL);
3380 if (!widgets) {
3381 dev_err(card->dev,
3382 "ASoC: Could not allocate memory for widgets\n");
3383 return -ENOMEM;
3384 }
3385
3386 for (i = 0; i < num_widgets; i++) {
3387 ret = of_property_read_string_index(np, propname,
3388 2 * i, &template);
3389 if (ret) {
3390 dev_err(card->dev,
3391 "ASoC: Property '%s' index %d read error:%d\n",
3392 propname, 2 * i, ret);
3393 return -EINVAL;
3394 }
3395
3396 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3397 if (!strncmp(template, simple_widgets[j].name,
3398 strlen(simple_widgets[j].name))) {
3399 widgets[i] = simple_widgets[j];
3400 break;
3401 }
3402 }
3403
3404 if (j >= ARRAY_SIZE(simple_widgets)) {
3405 dev_err(card->dev,
3406 "ASoC: DAPM widget '%s' is not supported\n",
3407 template);
3408 return -EINVAL;
3409 }
3410
3411 ret = of_property_read_string_index(np, propname,
3412 (2 * i) + 1,
3413 &wname);
3414 if (ret) {
3415 dev_err(card->dev,
3416 "ASoC: Property '%s' index %d read error:%d\n",
3417 propname, (2 * i) + 1, ret);
3418 return -EINVAL;
3419 }
3420
3421 widgets[i].name = wname;
3422 }
3423
Nicolin Chenf23e8602015-02-14 17:22:49 -08003424 card->of_dapm_widgets = widgets;
3425 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003426
3427 return 0;
3428}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003429EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003430
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003431int snd_soc_of_get_slot_mask(struct device_node *np,
3432 const char *prop_name,
3433 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003434{
3435 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003436 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003437 int i;
3438
3439 if (!of_slot_mask)
3440 return 0;
3441 val /= sizeof(u32);
3442 for (i = 0; i < val; i++)
3443 if (be32_to_cpup(&of_slot_mask[i]))
3444 *mask |= (1 << i);
3445
3446 return val;
3447}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003448EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003449
Xiubo Li89c67852014-02-14 09:34:35 +08003450int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003451 unsigned int *tx_mask,
3452 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003453 unsigned int *slots,
3454 unsigned int *slot_width)
3455{
3456 u32 val;
3457 int ret;
3458
Jyri Sarha61310842015-09-09 21:27:43 +03003459 if (tx_mask)
3460 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3461 if (rx_mask)
3462 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3463
Xiubo Li89c67852014-02-14 09:34:35 +08003464 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3465 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3466 if (ret)
3467 return ret;
3468
3469 if (slots)
3470 *slots = val;
3471 }
3472
3473 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3474 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3475 if (ret)
3476 return ret;
3477
3478 if (slot_width)
3479 *slot_width = val;
3480 }
3481
3482 return 0;
3483}
3484EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3485
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003486void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003487 struct snd_soc_codec_conf *codec_conf,
3488 struct device_node *of_node,
3489 const char *propname)
3490{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003491 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003492 const char *str;
3493 int ret;
3494
3495 ret = of_property_read_string(np, propname, &str);
3496 if (ret < 0) {
3497 /* no prefix is not error */
3498 return;
3499 }
3500
3501 codec_conf->of_node = of_node;
3502 codec_conf->name_prefix = str;
3503}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003504EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003505
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003506int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003507 const char *propname)
3508{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003509 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003510 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003511 struct snd_soc_dapm_route *routes;
3512 int i, ret;
3513
3514 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003515 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003516 dev_err(card->dev,
3517 "ASoC: Property '%s' does not exist or its length is not even\n",
3518 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003519 return -EINVAL;
3520 }
3521 num_routes /= 2;
3522 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003523 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003524 propname);
3525 return -EINVAL;
3526 }
3527
Kees Cooka86854d2018-06-12 14:07:58 -07003528 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003529 GFP_KERNEL);
3530 if (!routes) {
3531 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003532 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003533 return -EINVAL;
3534 }
3535
3536 for (i = 0; i < num_routes; i++) {
3537 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003538 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003539 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003540 dev_err(card->dev,
3541 "ASoC: Property '%s' index %d could not be read: %d\n",
3542 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003543 return -EINVAL;
3544 }
3545 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003546 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003547 if (ret) {
3548 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003549 "ASoC: Property '%s' index %d could not be read: %d\n",
3550 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003551 return -EINVAL;
3552 }
3553 }
3554
Nicolin Chenf23e8602015-02-14 17:22:49 -08003555 card->num_of_dapm_routes = num_routes;
3556 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003557
3558 return 0;
3559}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003560EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003561
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003562unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003563 const char *prefix,
3564 struct device_node **bitclkmaster,
3565 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003566{
3567 int ret, i;
3568 char prop[128];
3569 unsigned int format = 0;
3570 int bit, frame;
3571 const char *str;
3572 struct {
3573 char *name;
3574 unsigned int val;
3575 } of_fmt_table[] = {
3576 { "i2s", SND_SOC_DAIFMT_I2S },
3577 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3578 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3579 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3580 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3581 { "ac97", SND_SOC_DAIFMT_AC97 },
3582 { "pdm", SND_SOC_DAIFMT_PDM},
3583 { "msb", SND_SOC_DAIFMT_MSB },
3584 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003585 };
3586
3587 if (!prefix)
3588 prefix = "";
3589
3590 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003591 * check "dai-format = xxx"
3592 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003593 * SND_SOC_DAIFMT_FORMAT_MASK area
3594 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003595 ret = of_property_read_string(np, "dai-format", &str);
3596 if (ret < 0) {
3597 snprintf(prop, sizeof(prop), "%sformat", prefix);
3598 ret = of_property_read_string(np, prop, &str);
3599 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003600 if (ret == 0) {
3601 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3602 if (strcmp(str, of_fmt_table[i].name) == 0) {
3603 format |= of_fmt_table[i].val;
3604 break;
3605 }
3606 }
3607 }
3608
3609 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003610 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003611 * SND_SOC_DAIFMT_CLOCK_MASK area
3612 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003613 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003614 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003615 format |= SND_SOC_DAIFMT_CONT;
3616 else
3617 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003618
3619 /*
3620 * check "[prefix]bitclock-inversion"
3621 * check "[prefix]frame-inversion"
3622 * SND_SOC_DAIFMT_INV_MASK area
3623 */
3624 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3625 bit = !!of_get_property(np, prop, NULL);
3626
3627 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3628 frame = !!of_get_property(np, prop, NULL);
3629
3630 switch ((bit << 4) + frame) {
3631 case 0x11:
3632 format |= SND_SOC_DAIFMT_IB_IF;
3633 break;
3634 case 0x10:
3635 format |= SND_SOC_DAIFMT_IB_NF;
3636 break;
3637 case 0x01:
3638 format |= SND_SOC_DAIFMT_NB_IF;
3639 break;
3640 default:
3641 /* SND_SOC_DAIFMT_NB_NF is default */
3642 break;
3643 }
3644
3645 /*
3646 * check "[prefix]bitclock-master"
3647 * check "[prefix]frame-master"
3648 * SND_SOC_DAIFMT_MASTER_MASK area
3649 */
3650 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3651 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003652 if (bit && bitclkmaster)
3653 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003654
3655 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3656 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003657 if (frame && framemaster)
3658 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003659
3660 switch ((bit << 4) + frame) {
3661 case 0x11:
3662 format |= SND_SOC_DAIFMT_CBM_CFM;
3663 break;
3664 case 0x10:
3665 format |= SND_SOC_DAIFMT_CBM_CFS;
3666 break;
3667 case 0x01:
3668 format |= SND_SOC_DAIFMT_CBS_CFM;
3669 break;
3670 default:
3671 format |= SND_SOC_DAIFMT_CBS_CFS;
3672 break;
3673 }
3674
3675 return format;
3676}
3677EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3678
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003679int snd_soc_get_dai_id(struct device_node *ep)
3680{
3681 struct snd_soc_component *pos;
3682 struct device_node *node;
3683 int ret;
3684
3685 node = of_graph_get_port_parent(ep);
3686
3687 /*
3688 * For example HDMI case, HDMI has video/sound port,
3689 * but ALSA SoC needs sound port number only.
3690 * Thus counting HDMI DT port/endpoint doesn't work.
3691 * Then, it should have .of_xlate_dai_id
3692 */
3693 ret = -ENOTSUPP;
3694 mutex_lock(&client_mutex);
3695 list_for_each_entry(pos, &component_list, list) {
3696 struct device_node *component_of_node = pos->dev->of_node;
3697
3698 if (!component_of_node && pos->dev->parent)
3699 component_of_node = pos->dev->parent->of_node;
3700
3701 if (component_of_node != node)
3702 continue;
3703
3704 if (pos->driver->of_xlate_dai_id)
3705 ret = pos->driver->of_xlate_dai_id(pos, ep);
3706
3707 break;
3708 }
3709 mutex_unlock(&client_mutex);
3710
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003711 of_node_put(node);
3712
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003713 return ret;
3714}
3715EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3716
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003717int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003718 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003719{
3720 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003721 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003722 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003723
3724 mutex_lock(&client_mutex);
3725 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003726 component_of_node = pos->dev->of_node;
3727 if (!component_of_node && pos->dev->parent)
3728 component_of_node = pos->dev->parent->of_node;
3729
3730 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003731 continue;
3732
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003733 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003734 ret = pos->driver->of_xlate_dai_name(pos,
3735 args,
3736 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003737 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003738 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003739 int id = -1;
3740
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003741 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003742 case 0:
3743 id = 0; /* same as dai_drv[0] */
3744 break;
3745 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003746 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003747 break;
3748 default:
3749 /* not supported */
3750 break;
3751 }
3752
3753 if (id < 0 || id >= pos->num_dai) {
3754 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003755 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003756 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003757
3758 ret = 0;
3759
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003760 /* find target DAI */
3761 list_for_each_entry(dai, &pos->dai_list, list) {
3762 if (id == 0)
3763 break;
3764 id--;
3765 }
3766
3767 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003768 if (!*dai_name)
3769 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003770 }
3771
Kuninori Morimotocb470082013-09-10 17:39:56 -07003772 break;
3773 }
3774 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003775 return ret;
3776}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003777EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003778
3779int snd_soc_of_get_dai_name(struct device_node *of_node,
3780 const char **dai_name)
3781{
3782 struct of_phandle_args args;
3783 int ret;
3784
3785 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3786 "#sound-dai-cells", 0, &args);
3787 if (ret)
3788 return ret;
3789
3790 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003791
3792 of_node_put(args.np);
3793
3794 return ret;
3795}
3796EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3797
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003798/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003799 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3800 * @dai_link: DAI link
3801 *
3802 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3803 */
3804void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3805{
3806 struct snd_soc_dai_link_component *component = dai_link->codecs;
3807 int index;
3808
3809 for (index = 0; index < dai_link->num_codecs; index++, component++) {
3810 if (!component->of_node)
3811 break;
3812 of_node_put(component->of_node);
3813 component->of_node = NULL;
3814 }
3815}
3816EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3817
3818/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003819 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3820 * @dev: Card device
3821 * @of_node: Device node
3822 * @dai_link: DAI link
3823 *
3824 * Builds an array of CODEC DAI components from the DAI link property
3825 * 'sound-dai'.
3826 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003827 * The device nodes in the array (of_node) must be dereferenced by calling
3828 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003829 *
3830 * Returns 0 for success
3831 */
3832int snd_soc_of_get_dai_link_codecs(struct device *dev,
3833 struct device_node *of_node,
3834 struct snd_soc_dai_link *dai_link)
3835{
3836 struct of_phandle_args args;
3837 struct snd_soc_dai_link_component *component;
3838 char *name;
3839 int index, num_codecs, ret;
3840
3841 /* Count the number of CODECs */
3842 name = "sound-dai";
3843 num_codecs = of_count_phandle_with_args(of_node, name,
3844 "#sound-dai-cells");
3845 if (num_codecs <= 0) {
3846 if (num_codecs == -ENOENT)
3847 dev_err(dev, "No 'sound-dai' property\n");
3848 else
3849 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3850 return num_codecs;
3851 }
Kees Cooka86854d2018-06-12 14:07:58 -07003852 component = devm_kcalloc(dev,
3853 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003854 GFP_KERNEL);
3855 if (!component)
3856 return -ENOMEM;
3857 dai_link->codecs = component;
3858 dai_link->num_codecs = num_codecs;
3859
3860 /* Parse the list */
3861 for (index = 0, component = dai_link->codecs;
3862 index < dai_link->num_codecs;
3863 index++, component++) {
3864 ret = of_parse_phandle_with_args(of_node, name,
3865 "#sound-dai-cells",
3866 index, &args);
3867 if (ret)
3868 goto err;
3869 component->of_node = args.np;
3870 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3871 if (ret < 0)
3872 goto err;
3873 }
3874 return 0;
3875err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003876 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003877 dai_link->codecs = NULL;
3878 dai_link->num_codecs = 0;
3879 return ret;
3880}
3881EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3882
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003883static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003884{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003885 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003886 snd_soc_util_init();
3887
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003888 return platform_driver_register(&soc_driver);
3889}
Mark Brown4abe8e12010-10-12 17:41:03 +01003890module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003891
Mark Brown7d8c16a2008-11-30 22:11:24 +00003892static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003893{
Mark Brownfb257892011-04-28 10:57:54 +01003894 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003895 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003896
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003897 platform_driver_unregister(&soc_driver);
3898}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003899module_exit(snd_soc_exit);
3900
3901/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003902MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003903MODULE_DESCRIPTION("ALSA SoC Core");
3904MODULE_LICENSE("GPL");
3905MODULE_ALIAS("platform:soc-audio");