blob: 7e18937c22144361f313b428fd1977913ac29049 [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
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000282static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
283 struct snd_soc_component *component)
284{
285 struct snd_soc_rtdcom_list *rtdcom;
286 struct snd_soc_rtdcom_list *new_rtdcom;
287
288 for_each_rtdcom(rtd, rtdcom) {
289 /* already connected */
290 if (rtdcom->component == component)
291 return 0;
292 }
293
294 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
295 if (!new_rtdcom)
296 return -ENOMEM;
297
298 new_rtdcom->component = component;
299 INIT_LIST_HEAD(&new_rtdcom->list);
300
301 list_add_tail(&new_rtdcom->list, &rtd->component_list);
302
303 return 0;
304}
305
306static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
307{
308 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
309
310 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
311 kfree(rtdcom1);
312
313 INIT_LIST_HEAD(&rtd->component_list);
314}
315
316struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
317 const char *driver_name)
318{
319 struct snd_soc_rtdcom_list *rtdcom;
320
Kuninori Morimoto971da242018-01-23 00:41:24 +0000321 if (!driver_name)
322 return NULL;
323
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000324 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000325 const char *component_name = rtdcom->component->driver->name;
326
327 if (!component_name)
328 continue;
329
330 if ((component_name == driver_name) ||
331 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000332 return rtdcom->component;
333 }
334
335 return NULL;
336}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000337EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000338
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100339struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
340 const char *dai_link, int stream)
341{
Mengdong Lin1a497982015-11-18 02:34:11 -0500342 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100343
Mengdong Lin1a497982015-11-18 02:34:11 -0500344 list_for_each_entry(rtd, &card->rtd_list, list) {
345 if (rtd->dai_link->no_pcm &&
346 !strcmp(rtd->dai_link->name, dai_link))
347 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000349 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100350 return NULL;
351}
352EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
353
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000354static const struct snd_soc_ops null_snd_soc_ops;
355
Mengdong Lin1a497982015-11-18 02:34:11 -0500356static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
357 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
358{
359 struct snd_soc_pcm_runtime *rtd;
360
361 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
362 if (!rtd)
363 return NULL;
364
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000365 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500366 rtd->card = card;
367 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000368 if (!rtd->dai_link->ops)
369 rtd->dai_link->ops = &null_snd_soc_ops;
370
Kees Cook6396bb22018-06-12 14:03:40 -0700371 rtd->codec_dais = kcalloc(dai_link->num_codecs,
372 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500373 GFP_KERNEL);
374 if (!rtd->codec_dais) {
375 kfree(rtd);
376 return NULL;
377 }
378
379 return rtd;
380}
381
382static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
383{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000384 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000385 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500386 kfree(rtd);
387}
388
389static void soc_add_pcm_runtime(struct snd_soc_card *card,
390 struct snd_soc_pcm_runtime *rtd)
391{
392 list_add_tail(&rtd->list, &card->rtd_list);
393 rtd->num = card->num_rtd;
394 card->num_rtd++;
395}
396
397static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
398{
399 struct snd_soc_pcm_runtime *rtd, *_rtd;
400
401 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
402 list_del(&rtd->list);
403 soc_free_pcm_runtime(rtd);
404 }
405
406 card->num_rtd = 0;
407}
408
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100409struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
410 const char *dai_link)
411{
Mengdong Lin1a497982015-11-18 02:34:11 -0500412 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100413
Mengdong Lin1a497982015-11-18 02:34:11 -0500414 list_for_each_entry(rtd, &card->rtd_list, list) {
415 if (!strcmp(rtd->dai_link->name, dai_link))
416 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100417 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000418 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100419 return NULL;
420}
421EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
422
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100423static void codec2codec_close_delayed_work(struct work_struct *work)
424{
425 /* Currently nothing to do for c2c links
426 * Since c2c links are internal nodes in the DAPM graph and
427 * don't interface with the outside world or application layer
428 * we don't have to do any special handling on close.
429 */
430}
431
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000432#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000434int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200435{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000436 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000437 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500438 struct snd_soc_pcm_runtime *rtd;
439 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200440
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200441 /* If the card is not initialized yet there is nothing to do */
442 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200443 return 0;
444
Andy Green6ed25972008-06-13 16:24:05 +0100445 /* Due to the resume being scheduled into a workqueue we could
446 * suspend before that's finished - wait for it to complete.
447 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100449
450 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100452
Mark Browna00f90f2010-12-02 16:24:24 +0000453 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500454 list_for_each_entry(rtd, &card->rtd_list, list) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000455 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100456
Mengdong Lin1a497982015-11-18 02:34:11 -0500457 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100458 continue;
459
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000460 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200461 struct snd_soc_dai_driver *drv = dai->driver;
462
463 if (drv->ops->digital_mute && dai->playback_active)
464 drv->ops->digital_mute(dai, 1);
465 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200466 }
467
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100468 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500469 list_for_each_entry(rtd, &card->rtd_list, list) {
470 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100471 continue;
472
Mengdong Lin1a497982015-11-18 02:34:11 -0500473 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100474 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100475
Mark Brown87506542008-11-18 20:50:34 +0000476 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000477 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200478
Mengdong Lin1a497982015-11-18 02:34:11 -0500479 list_for_each_entry(rtd, &card->rtd_list, list) {
480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100481
Mengdong Lin1a497982015-11-18 02:34:11 -0500482 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100483 continue;
484
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100485 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100487 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200489 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500490 list_for_each_entry(rtd, &card->rtd_list, list)
491 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100492
Mengdong Lin1a497982015-11-18 02:34:11 -0500493 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100496 continue;
497
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800499 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800500 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501
Mengdong Lin1a497982015-11-18 02:34:11 -0500502 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800503 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800504 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 }
506
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200507 /* Recheck all endpoints too, their state is affected by suspend */
508 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700509 snd_soc_dapm_sync(&card->dapm);
510
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000511 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000512 list_for_each_entry(component, &card->component_dev_list, card_list) {
513 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000514
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000515 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000517 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200518 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000520 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000521 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000522 * bias off then being in STANDBY
523 * means it's doing something,
524 * otherwise fall through.
525 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200526 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000527 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200528 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000529 break;
530 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500531 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200532
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000534 if (component->driver->suspend)
535 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000536 component->suspended = 1;
537 if (component->regmap)
538 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800539 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000540 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 break;
542 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000543 dev_dbg(component->dev,
544 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 break;
546 }
547 }
548 }
549
Mengdong Lin1a497982015-11-18 02:34:11 -0500550 list_for_each_entry(rtd, &card->rtd_list, list) {
551 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552
Mengdong Lin1a497982015-11-18 02:34:11 -0500553 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 continue;
555
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100556 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800558
559 /* deactivate pins to sleep state */
560 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561 }
562
Mark Brown87506542008-11-18 20:50:34 +0000563 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000564 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565
566 return 0;
567}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000568EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569
Andy Green6ed25972008-06-13 16:24:05 +0100570/* deferred resume work, so resume can complete before we finished
571 * setting our codec back up, which can be very slow on I2C
572 */
573static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 struct snd_soc_card *card =
576 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500577 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000578 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500579 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580
Andy Green6ed25972008-06-13 16:24:05 +0100581 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
582 * so userspace apps are blocked from touching us
583 */
584
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000585 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100586
Mark Brown9949788b2010-05-07 20:24:05 +0100587 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100589
Mark Brown87506542008-11-18 20:50:34 +0000590 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000591 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100593 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500594 list_for_each_entry(rtd, &card->rtd_list, list) {
595 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100596
Mengdong Lin1a497982015-11-18 02:34:11 -0500597 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100598 continue;
599
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100600 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602 }
603
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000604 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000605 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000606 if (component->driver->resume)
607 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000608 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Mengdong Lin1a497982015-11-18 02:34:11 -0500612 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100613
Mengdong Lin1a497982015-11-18 02:34:11 -0500614 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100615 continue;
616
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000618 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800619 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620
Mengdong Lin1a497982015-11-18 02:34:11 -0500621 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000622 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800623 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624 }
625
Mark Brown3ff3f642008-05-19 12:32:25 +0200626 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500627 list_for_each_entry(rtd, &card->rtd_list, list) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000628 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100629
Mengdong Lin1a497982015-11-18 02:34:11 -0500630 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100631 continue;
632
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000633 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200634 struct snd_soc_dai_driver *drv = dai->driver;
635
636 if (drv->ops->digital_mute && dai->playback_active)
637 drv->ops->digital_mute(dai, 0);
638 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 }
640
Mengdong Lin1a497982015-11-18 02:34:11 -0500641 list_for_each_entry(rtd, &card->rtd_list, list) {
642 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100643
Mengdong Lin1a497982015-11-18 02:34:11 -0500644 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100645 continue;
646
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100647 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649 }
650
Mark Brown87506542008-11-18 20:50:34 +0000651 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000652 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000654 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100655
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200656 /* Recheck all endpoints too, their state is affected by suspend */
657 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700658 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530659
660 /* userspace can access us now we are back as we were before */
661 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100662}
663
664/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000665int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100666{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000667 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100668 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500669 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200670
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200671 /* If the card is not initialized yet there is nothing to do */
672 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800673 return 0;
674
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800675 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500676 list_for_each_entry(rtd, &card->rtd_list, list) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000677 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
679 int j;
680
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800681 if (cpu_dai->active)
682 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200683
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000684 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200685 if (codec_dai->active)
686 pinctrl_pm_select_default_state(codec_dai->dev);
687 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800688 }
689
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100690 /*
691 * DAIs that also act as the control bus master might have other drivers
692 * hanging off them so need to resume immediately. Other drivers don't
693 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100694 * due to I/O costs and anti-pop so handle them out of line.
695 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500696 list_for_each_entry(rtd, &card->rtd_list, list) {
697 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100698 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600699 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100700 if (bus_control) {
701 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600702 soc_resume_deferred(&card->deferred_resume_work);
703 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000704 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600705 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000706 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100707 }
Andy Green6ed25972008-06-13 16:24:05 +0100708
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 return 0;
710}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000711EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000713#define snd_soc_suspend NULL
714#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715#endif
716
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100717static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800718};
719
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200720static struct snd_soc_component *soc_find_component(
721 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100722{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200723 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100724
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100725 lockdep_assert_held(&client_mutex);
726
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200727 list_for_each_entry(component, &component_list, list) {
728 if (of_node) {
729 if (component->dev->of_node == of_node)
730 return component;
731 } else if (strcmp(component->name, name) == 0) {
732 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100733 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100734 }
735
736 return NULL;
737}
738
Mengdong Linfbb88b52016-04-22 12:25:33 +0800739/**
740 * snd_soc_find_dai - Find a registered DAI
741 *
Jeffy Chen49584712017-08-22 15:57:21 +0800742 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800743 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700744 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800745 * find the DAI of the same name. The component's of_node and name
746 * should also match if being specified.
747 *
748 * Return: pointer of DAI, or NULL if not found.
749 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800750struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200751 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100752{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200753 struct snd_soc_component *component;
754 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300755 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100756
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100757 lockdep_assert_held(&client_mutex);
758
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200759 /* Find CPU DAI from registered DAIs*/
760 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300761 component_of_node = component->dev->of_node;
762 if (!component_of_node && component->dev->parent)
763 component_of_node = component->dev->parent->of_node;
764
765 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200766 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100767 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200768 continue;
769 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800770 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800771 && (!dai->driver->name
772 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100773 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100774
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200775 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100776 }
777 }
778
779 return NULL;
780}
Mengdong Lin305e9022016-04-19 13:12:25 +0800781EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100782
Mengdong Lin17fb17552016-11-03 01:04:12 +0800783
784/**
785 * snd_soc_find_dai_link - Find a DAI link
786 *
787 * @card: soc card
788 * @id: DAI link ID to match
789 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000790 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800791 *
792 * This function will search all existing DAI links of the soc card to
793 * find the link of the same ID. Since DAI links may not have their
794 * unique ID, so name and stream name should also match if being
795 * specified.
796 *
797 * Return: pointer of DAI link, or NULL if not found.
798 */
799struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
800 int id, const char *name,
801 const char *stream_name)
802{
803 struct snd_soc_dai_link *link, *_link;
804
805 lockdep_assert_held(&client_mutex);
806
807 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
808 if (link->id != id)
809 continue;
810
811 if (name && (!link->name || strcmp(name, link->name)))
812 continue;
813
814 if (stream_name && (!link->stream_name
815 || strcmp(stream_name, link->stream_name)))
816 continue;
817
818 return link;
819 }
820
821 return NULL;
822}
823EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
824
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800825static bool soc_is_dai_link_bound(struct snd_soc_card *card,
826 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800828 struct snd_soc_pcm_runtime *rtd;
829
830 list_for_each_entry(rtd, &card->rtd_list, list) {
831 if (rtd->dai_link == dai_link)
832 return true;
833 }
834
835 return false;
836}
837
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500838static int soc_bind_dai_link(struct snd_soc_card *card,
839 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200840{
Mengdong Lin1a497982015-11-18 02:34:11 -0500841 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200842 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200843 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000844 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500845 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +0000846 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +0100847 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200848 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
Liam Girdwooda655de82018-07-02 16:59:54 +0100850 if (dai_link->ignore)
851 return 0;
852
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500853 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000854
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800855 if (soc_is_dai_link_bound(card, dai_link)) {
856 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
857 dai_link->name);
858 return 0;
859 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530861 rtd = soc_new_pcm_runtime(card, dai_link);
862 if (!rtd)
863 return -ENOMEM;
864
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200865 cpu_dai_component.name = dai_link->cpu_name;
866 cpu_dai_component.of_node = dai_link->cpu_of_node;
867 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
868 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000869 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100870 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
871 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500872 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000873 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000874 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000875
Benoit Cousson88bd8702014-07-08 23:19:34 +0200876 rtd->num_codecs = dai_link->num_codecs;
877
878 /* Find CODEC from registered CODECs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000879 /* we can use for_each_rtd_codec_dai() after this */
Mengdong Lin1a497982015-11-18 02:34:11 -0500880 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200881 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200882 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200883 if (!codec_dais[i]) {
884 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
885 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500886 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200887 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000888 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000889 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100890
Benoit Cousson88bd8702014-07-08 23:19:34 +0200891 /* Single codec links expect codec and codec_dai in runtime data */
892 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100893
Mark Brown848dd8b2011-04-27 18:16:32 +0100894 /* if there's no platform we match on the empty platform */
Kuninori Morimotodaecf462018-08-31 03:10:08 +0000895 platform_name = dai_link->platform->name;
896 if (!platform_name && !dai_link->platform->of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100897 platform_name = "snd-soc-dummy";
898
Mark Brownb19e6e72012-03-14 21:18:39 +0000899 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000900 list_for_each_entry(component, &component_list, list) {
901 platform_of_node = component->dev->of_node;
902 if (!platform_of_node && component->dev->parent->of_node)
903 platform_of_node = component->dev->parent->of_node;
904
Kuninori Morimotodaecf462018-08-31 03:10:08 +0000905 if (dai_link->platform->of_node) {
906 if (platform_of_node != dai_link->platform->of_node)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000907 continue;
908 } else {
909 if (strcmp(component->name, platform_name))
910 continue;
911 }
912
913 snd_soc_rtdcom_add(rtd, component);
914 }
915
Mengdong Lin1a497982015-11-18 02:34:11 -0500916 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500918
919_err_defer:
920 soc_free_pcm_runtime(rtd);
921 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922}
923
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200924static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600925{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200926 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200927 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600928
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000929 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600930
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000931 if (component->driver->remove)
932 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600933
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200934 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600935
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200936 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200937 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200938 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600939}
940
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200941static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200942{
943 int err;
944
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000945 if (!dai || !dai->probed ||
946 dai->driver->remove_order != order)
947 return;
948
949 if (dai->driver->remove) {
950 err = dai->driver->remove(dai);
951 if (err < 0)
952 dev_err(dai->dev,
953 "ASoC: failed to remove %s: %d\n",
954 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100955 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000956 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100957}
958
Mengdong Lin1a497982015-11-18 02:34:11 -0500959static void soc_remove_link_dais(struct snd_soc_card *card,
960 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200962 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000963 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964
965 /* unregister the rtd device */
966 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800967 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 rtd->dev_registered = 0;
969 }
970
971 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000972 for_each_rtd_codec_dai(rtd, i, codec_dai)
973 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200975 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976}
977
Mengdong Lin1a497982015-11-18 02:34:11 -0500978static void soc_remove_link_components(struct snd_soc_card *card,
979 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600980{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200981 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000982 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600983
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000984 for_each_rtdcom(rtd, rtdcom) {
985 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600986
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200987 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200988 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600990}
991
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900992static void soc_remove_dai_links(struct snd_soc_card *card)
993{
Mengdong Lin1a497982015-11-18 02:34:11 -0500994 int order;
995 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800996 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900997
Liam Girdwood0168bf02011-06-07 16:08:05 +0100998 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
999 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001000 list_for_each_entry(rtd, &card->rtd_list, list)
1001 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001002 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001003
1004 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1005 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001006 list_for_each_entry(rtd, &card->rtd_list, list)
1007 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001008 }
1009
Mengdong Linf8f80362015-12-02 14:11:22 +08001010 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1011 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1012 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1013 link->name);
1014
1015 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001016 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001017}
1018
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001019static int snd_soc_init_platform(struct snd_soc_card *card,
1020 struct snd_soc_dai_link *dai_link)
1021{
1022 /*
1023 * FIXME
1024 *
1025 * this function should be removed in the future
1026 */
1027 /* convert Legacy platform link */
1028 if (dai_link->platform)
1029 return 0;
1030
1031 dai_link->platform = devm_kzalloc(card->dev,
1032 sizeof(struct snd_soc_dai_link_component),
1033 GFP_KERNEL);
1034 if (!dai_link->platform)
1035 return -ENOMEM;
1036
1037 dai_link->platform->name = dai_link->platform_name;
1038 dai_link->platform->of_node = dai_link->platform_of_node;
1039 dai_link->platform->dai_name = NULL;
1040
1041 return 0;
1042}
1043
Mengdong Lin923c5e612015-11-23 11:01:49 -05001044static int snd_soc_init_multicodec(struct snd_soc_card *card,
1045 struct snd_soc_dai_link *dai_link)
1046{
1047 /* Legacy codec/codec_dai link is a single entry in multicodec */
1048 if (dai_link->codec_name || dai_link->codec_of_node ||
1049 dai_link->codec_dai_name) {
1050 dai_link->num_codecs = 1;
1051
1052 dai_link->codecs = devm_kzalloc(card->dev,
1053 sizeof(struct snd_soc_dai_link_component),
1054 GFP_KERNEL);
1055 if (!dai_link->codecs)
1056 return -ENOMEM;
1057
1058 dai_link->codecs[0].name = dai_link->codec_name;
1059 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1060 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1061 }
1062
1063 if (!dai_link->codecs) {
1064 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1065 return -EINVAL;
1066 }
1067
1068 return 0;
1069}
1070
1071static int soc_init_dai_link(struct snd_soc_card *card,
1072 struct snd_soc_dai_link *link)
1073{
1074 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001075 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001076
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001077 ret = snd_soc_init_platform(card, link);
1078 if (ret) {
1079 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1080 return ret;
1081 }
1082
Mengdong Lin923c5e612015-11-23 11:01:49 -05001083 ret = snd_soc_init_multicodec(card, link);
1084 if (ret) {
1085 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1086 return ret;
1087 }
1088
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001089 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001090 /*
1091 * Codec must be specified by 1 of name or OF node,
1092 * not both or neither.
1093 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001094 if (!!codec->name ==
1095 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001096 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1097 link->name);
1098 return -EINVAL;
1099 }
1100 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001101 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001102 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1103 link->name);
1104 return -EINVAL;
1105 }
1106 }
1107
1108 /*
1109 * Platform may be specified by either name or OF node, but
1110 * can be left unspecified, and a dummy platform will be used.
1111 */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001112 if (link->platform->name && link->platform->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001113 dev_err(card->dev,
1114 "ASoC: Both platform name/of_node are set for %s\n",
1115 link->name);
1116 return -EINVAL;
1117 }
Mengdong Lin923c5e612015-11-23 11:01:49 -05001118 /*
1119 * CPU device may be specified by either name or OF node, but
1120 * can be left unspecified, and will be matched based on DAI
1121 * name alone..
1122 */
1123 if (link->cpu_name && link->cpu_of_node) {
1124 dev_err(card->dev,
1125 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1126 link->name);
1127 return -EINVAL;
1128 }
1129 /*
1130 * At least one of CPU DAI name or CPU device name/node must be
1131 * specified
1132 */
1133 if (!link->cpu_dai_name &&
1134 !(link->cpu_name || link->cpu_of_node)) {
1135 dev_err(card->dev,
1136 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1137 link->name);
1138 return -EINVAL;
1139 }
1140
1141 return 0;
1142}
1143
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001144void snd_soc_disconnect_sync(struct device *dev)
1145{
1146 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1147
1148 if (!component || !component->card)
1149 return;
1150
1151 snd_card_disconnect_sync(component->card->snd_card);
1152}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001153EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001154
Mengdong Linf8f80362015-12-02 14:11:22 +08001155/**
1156 * snd_soc_add_dai_link - Add a DAI link dynamically
1157 * @card: The ASoC card to which the DAI link is added
1158 * @dai_link: The new DAI link to add
1159 *
1160 * This function adds a DAI link to the ASoC card's link list.
1161 *
1162 * Note: Topology can use this API to add DAI links when probing the
1163 * topology component. And machine drivers can still define static
1164 * DAI links in dai_link array.
1165 */
1166int snd_soc_add_dai_link(struct snd_soc_card *card,
1167 struct snd_soc_dai_link *dai_link)
1168{
1169 if (dai_link->dobj.type
1170 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1171 dev_err(card->dev, "Invalid dai link type %d\n",
1172 dai_link->dobj.type);
1173 return -EINVAL;
1174 }
1175
1176 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001177 /* Notify the machine driver for extra initialization
1178 * on the link created by topology.
1179 */
1180 if (dai_link->dobj.type && card->add_dai_link)
1181 card->add_dai_link(card, dai_link);
1182
Mengdong Linf8f80362015-12-02 14:11:22 +08001183 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001184
1185 return 0;
1186}
1187EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1188
1189/**
1190 * snd_soc_remove_dai_link - Remove a DAI link from the list
1191 * @card: The ASoC card that owns the link
1192 * @dai_link: The DAI link to remove
1193 *
1194 * This function removes a DAI link from the ASoC card's link list.
1195 *
1196 * For DAI links previously added by topology, topology should
1197 * remove them by using the dobj embedded in the link.
1198 */
1199void snd_soc_remove_dai_link(struct snd_soc_card *card,
1200 struct snd_soc_dai_link *dai_link)
1201{
1202 struct snd_soc_dai_link *link, *_link;
1203
1204 if (dai_link->dobj.type
1205 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1206 dev_err(card->dev, "Invalid dai link type %d\n",
1207 dai_link->dobj.type);
1208 return;
1209 }
1210
1211 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001212 /* Notify the machine driver for extra destruction
1213 * on the link created by topology.
1214 */
1215 if (dai_link->dobj.type && card->remove_dai_link)
1216 card->remove_dai_link(card, dai_link);
1217
Mengdong Linf8f80362015-12-02 14:11:22 +08001218 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1219 if (link == dai_link) {
1220 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001221 return;
1222 }
1223 }
1224}
1225EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1226
Jerome Brunetaefba452018-07-13 14:50:43 +02001227static void soc_set_of_name_prefix(struct snd_soc_component *component)
1228{
1229 struct device_node *component_of_node = component->dev->of_node;
1230 const char *str;
1231 int ret;
1232
1233 if (!component_of_node && component->dev->parent)
1234 component_of_node = component->dev->parent->of_node;
1235
1236 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1237 &str);
1238 if (!ret)
1239 component->name_prefix = str;
1240}
1241
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001242static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001243 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001244{
1245 int i;
1246
Jerome Brunetaefba452018-07-13 14:50:43 +02001247 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001248 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001249 struct device_node *component_of_node = component->dev->of_node;
1250
1251 if (!component_of_node && component->dev->parent)
1252 component_of_node = component->dev->parent->of_node;
1253
1254 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001255 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001256 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001257 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001258 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001259 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001260 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001261
1262 /*
1263 * If there is no configuration table or no match in the table,
1264 * check if a prefix is provided in the node
1265 */
1266 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001267}
1268
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001269static int soc_probe_component(struct snd_soc_card *card,
1270 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001271{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001272 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001273 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001274 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001276 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001277 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001279 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001280 if (component->card != card) {
1281 dev_err(component->dev,
1282 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1283 card->name, component->card->name);
1284 return -ENODEV;
1285 }
1286 return 0;
1287 }
1288
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001289 if (!try_module_get(component->dev->driver->owner))
1290 return -ENODEV;
1291
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001292 component->card = card;
1293 dapm->card = card;
1294 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001296 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001297
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001298 if (component->driver->dapm_widgets) {
1299 ret = snd_soc_dapm_new_controls(dapm,
1300 component->driver->dapm_widgets,
1301 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001302
1303 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001304 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001305 "Failed to create new controls %d\n", ret);
1306 goto err_probe;
1307 }
1308 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001309
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001310 list_for_each_entry(dai, &component->dai_list, list) {
1311 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001312 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001313 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001314 "Failed to create DAI widgets %d\n", ret);
1315 goto err_probe;
1316 }
1317 }
Mark Brown888df392012-02-16 19:37:51 -08001318
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001319 if (component->driver->probe) {
1320 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 dev_err(component->dev,
1323 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001324 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001325 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001326
1327 WARN(dapm->idle_bias_off &&
1328 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001329 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001330 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001331 }
1332
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001333 /* machine specific init */
1334 if (component->init) {
1335 ret = component->init(component);
1336 if (ret < 0) {
1337 dev_err(component->dev,
1338 "Failed to do machine specific init %d\n", ret);
1339 goto err_probe;
1340 }
1341 }
1342
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001343 if (component->driver->controls)
1344 snd_soc_add_component_controls(component,
1345 component->driver->controls,
1346 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001347 if (component->driver->dapm_routes)
1348 snd_soc_dapm_add_routes(dapm,
1349 component->driver->dapm_routes,
1350 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001351
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001352 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001353 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001354
Jarkko Nikula70d293312011-01-27 16:24:22 +02001355 return 0;
1356
1357err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001358 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001359 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001360 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001361
1362 return ret;
1363}
1364
Mark Brown36ae1a92012-01-06 17:12:45 -08001365static void rtd_release(struct device *dev)
1366{
1367 kfree(dev);
1368}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001369
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001370static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1371 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001372{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373 int ret = 0;
1374
Jarkko Nikula589c3562010-12-06 16:27:07 +02001375 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001376 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1377 if (!rtd->dev)
1378 return -ENOMEM;
1379 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001380 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001381 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001382 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001383 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001384 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001385 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1387 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1388 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1389 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001390 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001391 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001392 /* calling put_device() here to free the rtd->dev */
1393 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001394 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001395 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001396 return ret;
1397 }
1398 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001399 return 0;
1400}
1401
Mengdong Lin1a497982015-11-18 02:34:11 -05001402static int soc_probe_link_components(struct snd_soc_card *card,
1403 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001404 int order)
1405{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001406 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001407 struct snd_soc_rtdcom_list *rtdcom;
1408 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001409
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001410 for_each_rtdcom(rtd, rtdcom) {
1411 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001412
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001413 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001414 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001415 if (ret < 0)
1416 return ret;
1417 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001418 }
1419
Stephen Warren62ae68f2012-06-08 12:34:23 -06001420 return 0;
1421}
1422
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001423static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001424{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001425 if (dai->probed ||
1426 dai->driver->probe_order != order)
1427 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001428
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001429 if (dai->driver->probe) {
1430 int ret = dai->driver->probe(dai);
1431 if (ret < 0) {
1432 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1433 dai->name, ret);
1434 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001435 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001436 }
1437
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001438 dai->probed = 1;
1439
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001440 return 0;
1441}
1442
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001443static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1444 struct snd_soc_pcm_runtime *rtd)
1445{
1446 int i, ret = 0;
1447
1448 for (i = 0; i < num_dais; ++i) {
1449 struct snd_soc_dai_driver *drv = dais[i]->driver;
1450
1451 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1452 ret = drv->pcm_new(rtd, dais[i]);
1453 if (ret < 0) {
1454 dev_err(dais[i]->dev,
1455 "ASoC: Failed to bind %s with pcm device\n",
1456 dais[i]->name);
1457 return ret;
1458 }
1459 }
1460
1461 return 0;
1462}
1463
Mengdong Lin1a497982015-11-18 02:34:11 -05001464static int soc_probe_link_dais(struct snd_soc_card *card,
1465 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466{
Mengdong Lin1a497982015-11-18 02:34:11 -05001467 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001468 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001469 struct snd_soc_rtdcom_list *rtdcom;
1470 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001471 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001472 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001473
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001474 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001475 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477 /* set default power off timeout */
1478 rtd->pmdown_time = pmdown_time;
1479
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001480 ret = soc_probe_dai(cpu_dai, order);
1481 if (ret)
1482 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001485 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1486 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001487 if (ret)
1488 return ret;
1489 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001490
Liam Girdwood0168bf02011-06-07 16:08:05 +01001491 /* complete DAI probe during last probe */
1492 if (order != SND_SOC_COMP_ORDER_LAST)
1493 return 0;
1494
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001495 /* do machine specific initialization */
1496 if (dai_link->init) {
1497 ret = dai_link->init(rtd);
1498 if (ret < 0) {
1499 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1500 dai_link->name, ret);
1501 return ret;
1502 }
1503 }
1504
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001505 if (dai_link->dai_fmt)
1506 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1507
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001508 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001509 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001511
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001512#ifdef CONFIG_DEBUG_FS
1513 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001514 if (dai_link->dynamic)
1515 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001516#endif
1517
Liam Girdwooda655de82018-07-02 16:59:54 +01001518 num = rtd->num;
1519
1520 /*
1521 * most drivers will register their PCMs using DAI link ordering but
1522 * topology based drivers can use the DAI link id field to set PCM
1523 * device number and then use rtd + a base offset of the BEs.
1524 */
1525 for_each_rtdcom(rtd, rtdcom) {
1526 component = rtdcom->component;
1527
1528 if (!component->driver->use_dai_pcm_id)
1529 continue;
1530
1531 if (rtd->dai_link->no_pcm)
1532 num += component->driver->be_pcm_base;
1533 else
1534 num = rtd->dai_link->id;
1535 }
1536
Jie Yang6f0c4222015-10-13 23:41:00 +08001537 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301538 /*create compress_device"*/
Liam Girdwooda655de82018-07-02 16:59:54 +01001539 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001540 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001541 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301542 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001543 return ret;
1544 }
1545 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301546
1547 if (!dai_link->params) {
1548 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001549 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301550 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001551 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301552 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001553 return ret;
1554 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001555 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1556 if (ret < 0)
1557 return ret;
1558 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1559 rtd->num_codecs, rtd);
1560 if (ret < 0)
1561 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301562 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001563 INIT_DELAYED_WORK(&rtd->delayed_work,
1564 codec2codec_close_delayed_work);
Mark Brownc74184e2012-04-04 22:12:09 +01001565 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566 }
1567
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001568 return 0;
1569}
1570
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001571static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001572{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001573 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001574 struct snd_soc_component *component;
1575 const char *name;
1576 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001577
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001578 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1579 /* codecs, usually analog devices */
1580 name = aux_dev->codec_name;
1581 codec_of_node = aux_dev->codec_of_node;
1582 component = soc_find_component(codec_of_node, name);
1583 if (!component) {
1584 if (codec_of_node)
1585 name = of_node_full_name(codec_of_node);
1586 goto err_defer;
1587 }
1588 } else if (aux_dev->name) {
1589 /* generic components */
1590 name = aux_dev->name;
1591 component = soc_find_component(NULL, name);
1592 if (!component)
1593 goto err_defer;
1594 } else {
1595 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1596 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001597 }
1598
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001599 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001600 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001601
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001602 return 0;
1603
1604err_defer:
1605 dev_err(card->dev, "ASoC: %s not registered\n", name);
1606 return -EPROBE_DEFER;
1607}
1608
1609static int soc_probe_aux_devices(struct snd_soc_card *card)
1610{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001611 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001612 int order;
1613 int ret;
1614
1615 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1616 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001617 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001618 if (comp->driver->probe_order == order) {
1619 ret = soc_probe_component(card, comp);
1620 if (ret < 0) {
1621 dev_err(card->dev,
1622 "ASoC: failed to probe aux component %s %d\n",
1623 comp->name, ret);
1624 return ret;
1625 }
1626 }
1627 }
1628 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001629
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001630 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001631}
1632
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001633static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001634{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001635 struct snd_soc_component *comp, *_comp;
1636 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001637
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001638 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1639 order++) {
1640 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001641 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001642
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001643 if (comp->driver->remove_order == order) {
1644 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001645 /* remove it from the card's aux_comp_list */
1646 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001647 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001648 }
1649 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001650}
1651
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001652/**
1653 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1654 * @rtd: The runtime for which the DAI link format should be changed
1655 * @dai_fmt: The new DAI link format
1656 *
1657 * This function updates the DAI link format for all DAIs connected to the DAI
1658 * link for the specified runtime.
1659 *
1660 * Note: For setups with a static format set the dai_fmt field in the
1661 * corresponding snd_dai_link struct instead of using this function.
1662 *
1663 * Returns 0 on success, otherwise a negative error code.
1664 */
1665int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1666 unsigned int dai_fmt)
1667{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001668 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001669 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001670 unsigned int i;
1671 int ret;
1672
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001673 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001674 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1675 if (ret != 0 && ret != -ENOTSUPP) {
1676 dev_warn(codec_dai->dev,
1677 "ASoC: Failed to set DAI format: %d\n", ret);
1678 return ret;
1679 }
1680 }
1681
1682 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001683 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001684 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001685 unsigned int inv_dai_fmt;
1686
1687 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1688 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1689 case SND_SOC_DAIFMT_CBM_CFM:
1690 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1691 break;
1692 case SND_SOC_DAIFMT_CBM_CFS:
1693 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1694 break;
1695 case SND_SOC_DAIFMT_CBS_CFM:
1696 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1697 break;
1698 case SND_SOC_DAIFMT_CBS_CFS:
1699 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1700 break;
1701 }
1702
1703 dai_fmt = inv_dai_fmt;
1704 }
1705
1706 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1707 if (ret != 0 && ret != -ENOTSUPP) {
1708 dev_warn(cpu_dai->dev,
1709 "ASoC: Failed to set DAI format: %d\n", ret);
1710 return ret;
1711 }
1712
1713 return 0;
1714}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001715EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001716
Liam Girdwood345233d2017-01-14 16:13:02 +08001717
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001718#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001719/* Trim special characters, and replace '-' with '_' since '-' is used to
1720 * separate different DMI fields in the card long name. Only number and
1721 * alphabet characters and a few separator characters are kept.
1722 */
1723static void cleanup_dmi_name(char *name)
1724{
1725 int i, j = 0;
1726
1727 for (i = 0; name[i]; i++) {
1728 if (isalnum(name[i]) || (name[i] == '.')
1729 || (name[i] == '_'))
1730 name[j++] = name[i];
1731 else if (name[i] == '-')
1732 name[j++] = '_';
1733 }
1734
1735 name[j] = '\0';
1736}
1737
Mengdong Lin98faf432017-06-28 15:01:39 +08001738/* Check if a DMI field is valid, i.e. not containing any string
1739 * in the black list.
1740 */
1741static int is_dmi_valid(const char *field)
1742{
1743 int i = 0;
1744
1745 while (dmi_blacklist[i]) {
1746 if (strstr(field, dmi_blacklist[i]))
1747 return 0;
1748 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001749 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001750
1751 return 1;
1752}
1753
Liam Girdwood345233d2017-01-14 16:13:02 +08001754/**
1755 * snd_soc_set_dmi_name() - Register DMI names to card
1756 * @card: The card to register DMI names
1757 * @flavour: The flavour "differentiator" for the card amongst its peers.
1758 *
1759 * An Intel machine driver may be used by many different devices but are
1760 * difficult for userspace to differentiate, since machine drivers ususally
1761 * use their own name as the card short name and leave the card long name
1762 * blank. To differentiate such devices and fix bugs due to lack of
1763 * device-specific configurations, this function allows DMI info to be used
1764 * as the sound card long name, in the format of
1765 * "vendor-product-version-board"
1766 * (Character '-' is used to separate different DMI fields here).
1767 * This will help the user space to load the device-specific Use Case Manager
1768 * (UCM) configurations for the card.
1769 *
1770 * Possible card long names may be:
1771 * DellInc.-XPS139343-01-0310JH
1772 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1773 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1774 *
1775 * This function also supports flavoring the card longname to provide
1776 * the extra differentiation, like "vendor-product-version-board-flavor".
1777 *
1778 * We only keep number and alphabet characters and a few separator characters
1779 * in the card long name since UCM in the user space uses the card long names
1780 * as card configuration directory names and AudoConf cannot support special
1781 * charactors like SPACE.
1782 *
1783 * Returns 0 on success, otherwise a negative error code.
1784 */
1785int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1786{
1787 const char *vendor, *product, *product_version, *board;
1788 size_t longname_buf_size = sizeof(card->snd_card->longname);
1789 size_t len;
1790
1791 if (card->long_name)
1792 return 0; /* long name already set by driver or from DMI */
1793
1794 /* make up dmi long name as: vendor.product.version.board */
1795 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001796 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001797 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1798 return 0;
1799 }
1800
Mengdong Lin98faf432017-06-28 15:01:39 +08001801
Liam Girdwood345233d2017-01-14 16:13:02 +08001802 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1803 "%s", vendor);
1804 cleanup_dmi_name(card->dmi_longname);
1805
1806 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001807 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001808 len = strlen(card->dmi_longname);
1809 snprintf(card->dmi_longname + len,
1810 longname_buf_size - len,
1811 "-%s", product);
1812
1813 len++; /* skip the separator "-" */
1814 if (len < longname_buf_size)
1815 cleanup_dmi_name(card->dmi_longname + len);
1816
1817 /* some vendors like Lenovo may only put a self-explanatory
1818 * name in the product version field
1819 */
1820 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001821 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001822 len = strlen(card->dmi_longname);
1823 snprintf(card->dmi_longname + len,
1824 longname_buf_size - len,
1825 "-%s", product_version);
1826
1827 len++;
1828 if (len < longname_buf_size)
1829 cleanup_dmi_name(card->dmi_longname + len);
1830 }
1831 }
1832
1833 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001834 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001835 len = strlen(card->dmi_longname);
1836 snprintf(card->dmi_longname + len,
1837 longname_buf_size - len,
1838 "-%s", board);
1839
1840 len++;
1841 if (len < longname_buf_size)
1842 cleanup_dmi_name(card->dmi_longname + len);
1843 } else if (!product) {
1844 /* fall back to using legacy name */
1845 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1846 return 0;
1847 }
1848
1849 /* Add flavour to dmi long name */
1850 if (flavour) {
1851 len = strlen(card->dmi_longname);
1852 snprintf(card->dmi_longname + len,
1853 longname_buf_size - len,
1854 "-%s", flavour);
1855
1856 len++;
1857 if (len < longname_buf_size)
1858 cleanup_dmi_name(card->dmi_longname + len);
1859 }
1860
1861 /* set the card long name */
1862 card->long_name = card->dmi_longname;
1863
1864 return 0;
1865}
1866EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001867#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001868
Liam Girdwooda655de82018-07-02 16:59:54 +01001869static void soc_check_tplg_fes(struct snd_soc_card *card)
1870{
1871 struct snd_soc_component *component;
1872 const struct snd_soc_component_driver *comp_drv;
1873 struct snd_soc_dai_link *dai_link;
1874 int i;
1875
1876 list_for_each_entry(component, &component_list, list) {
1877
1878 /* does this component override FEs ? */
1879 if (!component->driver->ignore_machine)
1880 continue;
1881
1882 /* for this machine ? */
1883 if (strcmp(component->driver->ignore_machine,
1884 card->dev->driver->name))
1885 continue;
1886
1887 /* machine matches, so override the rtd data */
1888 for (i = 0; i < card->num_links; i++) {
1889
1890 dai_link = &card->dai_link[i];
1891
1892 /* ignore this FE */
1893 if (dai_link->dynamic) {
1894 dai_link->ignore = true;
1895 continue;
1896 }
1897
1898 dev_info(card->dev, "info: override FE DAI link %s\n",
1899 card->dai_link[i].name);
1900
1901 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001902 if (snd_soc_init_platform(card, dai_link) < 0) {
1903 dev_err(card->dev, "init platform error");
1904 continue;
1905 }
1906 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001907
1908 /* convert non BE into BE */
1909 dai_link->no_pcm = 1;
1910
1911 /* override any BE fixups */
1912 dai_link->be_hw_params_fixup =
1913 component->driver->be_hw_params_fixup;
1914
1915 /* most BE links don't set stream name, so set it to
1916 * dai link name if it's NULL to help bind widgets.
1917 */
1918 if (!dai_link->stream_name)
1919 dai_link->stream_name = dai_link->name;
1920 }
1921
1922 /* Inform userspace we are using alternate topology */
1923 if (component->driver->topology_name_prefix) {
1924
1925 /* topology shortname created ? */
1926 if (!card->topology_shortname_created) {
1927 comp_drv = component->driver;
1928
1929 snprintf(card->topology_shortname, 32, "%s-%s",
1930 comp_drv->topology_name_prefix,
1931 card->name);
1932 card->topology_shortname_created = true;
1933 }
1934
1935 /* use topology shortname */
1936 card->name = card->topology_shortname;
1937 }
1938 }
1939}
1940
Mark Brownb19e6e72012-03-14 21:18:39 +00001941static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942{
Mengdong Lin1a497982015-11-18 02:34:11 -05001943 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001944 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001945 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001946
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001947 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001948 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001949
Liam Girdwooda655de82018-07-02 16:59:54 +01001950 /* check whether any platform is ignore machine FE and using topology */
1951 soc_check_tplg_fes(card);
1952
Mark Brownb19e6e72012-03-14 21:18:39 +00001953 /* bind DAIs */
1954 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001955 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00001956 if (ret != 0)
1957 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001958 }
1959
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001960 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001961 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001962 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001963 if (ret != 0)
1964 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001965 }
1966
Mengdong Linf8f80362015-12-02 14:11:22 +08001967 /* add predefined DAI links to the list */
1968 for (i = 0; i < card->num_links; i++)
1969 snd_soc_add_dai_link(card, card->dai_link+i);
1970
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001972 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973 card->owner, 0, &card->snd_card);
1974 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001975 dev_err(card->dev,
1976 "ASoC: can't create sound card for card %s: %d\n",
1977 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001978 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001980
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001981 soc_init_card_debugfs(card);
1982
Mark Browne37a4972011-03-02 18:21:57 +00001983 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1984 card->dapm.dev = card->dev;
1985 card->dapm.card = card;
1986 list_add(&card->dapm.list, &card->dapm_list);
1987
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001988#ifdef CONFIG_DEBUG_FS
1989 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1990#endif
1991
Mark Brown88ee1c62011-02-02 10:43:26 +00001992#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001993 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001994 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001995#endif
Andy Green6ed25972008-06-13 16:24:05 +01001996
Mark Brown9a841eb2011-04-12 17:51:37 -07001997 if (card->dapm_widgets)
1998 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1999 card->num_dapm_widgets);
2000
Nicolin Chenf23e8602015-02-14 17:22:49 -08002001 if (card->of_dapm_widgets)
2002 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2003 card->num_of_dapm_widgets);
2004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002005 /* initialise the sound card only once */
2006 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002007 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002008 if (ret < 0)
2009 goto card_probe_error;
2010 }
2011
Stephen Warren62ae68f2012-06-08 12:34:23 -06002012 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002013 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2014 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002015 list_for_each_entry(rtd, &card->rtd_list, list) {
2016 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002017 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002018 dev_err(card->dev,
2019 "ASoC: failed to instantiate card %d\n",
2020 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002021 goto probe_dai_err;
2022 }
2023 }
2024 }
2025
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002026 /* probe auxiliary components */
2027 ret = soc_probe_aux_devices(card);
2028 if (ret < 0)
2029 goto probe_dai_err;
2030
Mengdong Lin61b00882015-12-02 14:11:48 +08002031 /* Find new DAI links added during probing components and bind them.
2032 * Components with topology may bring new DAIs and DAI links.
2033 */
2034 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2035 if (soc_is_dai_link_bound(card, dai_link))
2036 continue;
2037
2038 ret = soc_init_dai_link(card, dai_link);
2039 if (ret)
2040 goto probe_dai_err;
2041 ret = soc_bind_dai_link(card, dai_link);
2042 if (ret)
2043 goto probe_dai_err;
2044 }
2045
Stephen Warren62ae68f2012-06-08 12:34:23 -06002046 /* probe all DAI links on this card */
2047 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2048 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002049 list_for_each_entry(rtd, &card->rtd_list, list) {
2050 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002051 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002052 dev_err(card->dev,
2053 "ASoC: failed to instantiate card %d\n",
2054 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002055 goto probe_dai_err;
2056 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 }
2058 }
2059
Mark Brown888df392012-02-16 19:37:51 -08002060 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002061 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002062
Mark Brownb7af1da2011-04-07 19:18:44 +09002063 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002064 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002065
Mark Brownb8ad29d2011-03-02 18:35:51 +00002066 if (card->dapm_routes)
2067 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2068 card->num_dapm_routes);
2069
Nicolin Chenf23e8602015-02-14 17:22:49 -08002070 if (card->of_dapm_routes)
2071 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2072 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002073
Takashi Iwai861886d2017-04-24 08:54:42 +02002074 /* try to set some sane longname if DMI is available */
2075 snd_soc_set_dmi_name(card, NULL);
2076
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002077 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002078 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002079 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2080 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002081 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2082 "%s", card->driver_name ? card->driver_name : card->name);
2083 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2084 switch (card->snd_card->driver[i]) {
2085 case '_':
2086 case '-':
2087 case '\0':
2088 break;
2089 default:
2090 if (!isalnum(card->snd_card->driver[i]))
2091 card->snd_card->driver[i] = '_';
2092 break;
2093 }
2094 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002095
Mark Brown28e9ad92011-03-02 18:36:34 +00002096 if (card->late_probe) {
2097 ret = card->late_probe(card);
2098 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002099 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002100 card->name, ret);
2101 goto probe_aux_dev_err;
2102 }
2103 }
2104
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002105 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002106
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002107 ret = snd_card_register(card->snd_card);
2108 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002109 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2110 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002111 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002112 }
2113
Mark Brown435c5e22008-12-04 15:32:53 +00002114 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002115 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002116 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002117 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002118
2119 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002120
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002121probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002122 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002124probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002125 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002126
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002128 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002129 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002130
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002131 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002132 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002133 snd_card_free(card->snd_card);
2134
Mark Brownb19e6e72012-03-14 21:18:39 +00002135base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002136 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002137 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002138 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002139
Mark Brownb19e6e72012-03-14 21:18:39 +00002140 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002141}
2142
2143/* probes a new socdev */
2144static int soc_probe(struct platform_device *pdev)
2145{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002146 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002147
Vinod Koul70a7ca32011-01-14 19:22:48 +05302148 /*
2149 * no card, so machine driver should be registering card
2150 * we should not be here in that case so ret error
2151 */
2152 if (!card)
2153 return -EINVAL;
2154
Mark Brownfe4085e2012-03-02 13:07:41 +00002155 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002156 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002157 card->name);
2158
Mark Brown435c5e22008-12-04 15:32:53 +00002159 /* Bodge while we unpick instantiation */
2160 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002161
Mark Brown28d528c2012-08-09 18:45:23 +01002162 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002163}
2164
Vinod Koulb0e26482011-01-13 22:48:02 +05302165static int soc_cleanup_card_resources(struct snd_soc_card *card)
2166{
Mengdong Lin1a497982015-11-18 02:34:11 -05002167 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302168
2169 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002170 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002171 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302172
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002173 /* free the ALSA card at first; this syncs with pending operations */
2174 snd_card_free(card->snd_card);
2175
Vinod Koulb0e26482011-01-13 22:48:02 +05302176 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002177 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002178 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302179
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002180 /* remove auxiliary devices */
2181 soc_remove_aux_devices(card);
2182
Mark Brownd1e81422016-08-18 19:32:59 +01002183 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302184 soc_cleanup_card_debugfs(card);
2185
2186 /* remove the card */
2187 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002188 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302189
Vinod Koulb0e26482011-01-13 22:48:02 +05302190 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302191}
2192
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193/* removes a socdev */
2194static int soc_remove(struct platform_device *pdev)
2195{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002196 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197
Mark Brownc5af3a22008-11-28 13:29:45 +00002198 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 return 0;
2200}
2201
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002202int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002203{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002204 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002205 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002206
2207 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002208 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002209
2210 /* Flush out pmdown_time work - we actually do want to run it
2211 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002212 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002213 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002214
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002215 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002216
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002217 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002218 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002219 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002220 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002221 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002222
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002223 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002224 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002225 pinctrl_pm_select_sleep_state(codec_dai->dev);
2226 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002227 }
2228
Mark Brown416356f2009-06-30 19:05:15 +01002229 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002230}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002231EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002232
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002233const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302234 .suspend = snd_soc_suspend,
2235 .resume = snd_soc_resume,
2236 .freeze = snd_soc_suspend,
2237 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002238 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302239 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002240};
Stephen Warrendeb26072011-04-05 19:35:30 -06002241EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002242
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002243/* ASoC platform driver */
2244static struct platform_driver soc_driver = {
2245 .driver = {
2246 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002247 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 },
2249 .probe = soc_probe,
2250 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251};
2252
Mark Brown096e49d2009-07-05 15:12:22 +01002253/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 * snd_soc_cnew - create new control
2255 * @_template: control template
2256 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002257 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002258 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 *
2260 * Create a new mixer control from a template control.
2261 *
2262 * Returns 0 for success, else error.
2263 */
2264struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002265 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267{
2268 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002269 struct snd_kcontrol *kcontrol;
2270 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271
2272 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273 template.index = 0;
2274
Mark Brownefb7ac32011-03-08 17:23:24 +00002275 if (!long_name)
2276 long_name = template.name;
2277
2278 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002279 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002280 if (!name)
2281 return NULL;
2282
Mark Brownefb7ac32011-03-08 17:23:24 +00002283 template.name = name;
2284 } else {
2285 template.name = long_name;
2286 }
2287
2288 kcontrol = snd_ctl_new1(&template, data);
2289
2290 kfree(name);
2291
2292 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293}
2294EXPORT_SYMBOL_GPL(snd_soc_cnew);
2295
Liam Girdwood022658b2012-02-03 17:43:09 +00002296static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2297 const struct snd_kcontrol_new *controls, int num_controls,
2298 const char *prefix, void *data)
2299{
2300 int err, i;
2301
2302 for (i = 0; i < num_controls; i++) {
2303 const struct snd_kcontrol_new *control = &controls[i];
2304 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2305 control->name, prefix));
2306 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002307 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2308 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002309 return err;
2310 }
2311 }
2312
2313 return 0;
2314}
2315
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002316struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2317 const char *name)
2318{
2319 struct snd_card *card = soc_card->snd_card;
2320 struct snd_kcontrol *kctl;
2321
2322 if (unlikely(!name))
2323 return NULL;
2324
2325 list_for_each_entry(kctl, &card->controls, list)
2326 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2327 return kctl;
2328 return NULL;
2329}
2330EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2331
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002333 * snd_soc_add_component_controls - Add an array of controls to a component.
2334 *
2335 * @component: Component to add controls to
2336 * @controls: Array of controls to add
2337 * @num_controls: Number of elements in the array
2338 *
2339 * Return: 0 for success, else error.
2340 */
2341int snd_soc_add_component_controls(struct snd_soc_component *component,
2342 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2343{
2344 struct snd_card *card = component->card->snd_card;
2345
2346 return snd_soc_add_controls(card, component->dev, controls,
2347 num_controls, component->name_prefix, component);
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2350
2351/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002352 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2353 * Convenience function to add a list of controls.
2354 *
2355 * @soc_card: SoC card to add controls to
2356 * @controls: array of controls to add
2357 * @num_controls: number of elements in the array
2358 *
2359 * Return 0 for success, else error.
2360 */
2361int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2362 const struct snd_kcontrol_new *controls, int num_controls)
2363{
2364 struct snd_card *card = soc_card->snd_card;
2365
2366 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2367 NULL, soc_card);
2368}
2369EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2370
2371/**
2372 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2373 * Convienience function to add a list of controls.
2374 *
2375 * @dai: DAI to add controls to
2376 * @controls: array of controls to add
2377 * @num_controls: number of elements in the array
2378 *
2379 * Return 0 for success, else error.
2380 */
2381int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2382 const struct snd_kcontrol_new *controls, int num_controls)
2383{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002384 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002385
2386 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2387 NULL, dai);
2388}
2389EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2390
2391/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002392 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2393 * @dai: DAI
2394 * @clk_id: DAI specific clock ID
2395 * @freq: new clock frequency in Hz
2396 * @dir: new clock direction - input/output.
2397 *
2398 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2399 */
2400int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2401 unsigned int freq, int dir)
2402{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002403 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002404 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002405
2406 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2407 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002408}
2409EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2410
2411/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002412 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2413 * @component: COMPONENT
2414 * @clk_id: DAI specific clock ID
2415 * @source: Source for the clock
2416 * @freq: new clock frequency in Hz
2417 * @dir: new clock direction - input/output.
2418 *
2419 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2420 */
2421int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2422 int source, unsigned int freq, int dir)
2423{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002424 if (component->driver->set_sysclk)
2425 return component->driver->set_sysclk(component, clk_id, source,
2426 freq, dir);
2427
2428 return -ENOTSUPP;
2429}
2430EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2431
2432/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002433 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2434 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002435 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002436 * @div: new clock divisor.
2437 *
2438 * Configures the clock dividers. This is used to derive the best DAI bit and
2439 * frame clocks from the system or master clock. It's best to set the DAI bit
2440 * and frame clocks as low as possible to save system power.
2441 */
2442int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2443 int div_id, int div)
2444{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002445 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002446 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002447 else
2448 return -EINVAL;
2449}
2450EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2451
2452/**
2453 * snd_soc_dai_set_pll - configure DAI PLL.
2454 * @dai: DAI
2455 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002456 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002457 * @freq_in: PLL input clock frequency in Hz
2458 * @freq_out: requested PLL output clock frequency in Hz
2459 *
2460 * Configures and enables PLL to generate output clock based on input clock.
2461 */
Mark Brown85488032009-09-05 18:52:16 +01002462int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2463 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002464{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002465 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002466 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002467 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002468
2469 return snd_soc_component_set_pll(dai->component, pll_id, source,
2470 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002471}
2472EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2473
Mark Brownec4ee522011-03-07 20:58:11 +00002474/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002475 * snd_soc_component_set_pll - configure component PLL.
2476 * @component: COMPONENT
2477 * @pll_id: DAI specific PLL ID
2478 * @source: DAI specific source for the PLL
2479 * @freq_in: PLL input clock frequency in Hz
2480 * @freq_out: requested PLL output clock frequency in Hz
2481 *
2482 * Configures and enables PLL to generate output clock based on input clock.
2483 */
2484int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2485 int source, unsigned int freq_in,
2486 unsigned int freq_out)
2487{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002488 if (component->driver->set_pll)
2489 return component->driver->set_pll(component, pll_id, source,
2490 freq_in, freq_out);
2491
2492 return -EINVAL;
2493}
2494EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2495
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002496/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002497 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2498 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002499 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002500 *
2501 * Configures the DAI for a preset BCLK to sample rate ratio.
2502 */
2503int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2504{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002505 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002506 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2507 else
2508 return -EINVAL;
2509}
2510EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2511
2512/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002513 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2514 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002515 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002516 *
2517 * Configures the DAI hardware format and clocking.
2518 */
2519int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2520{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002521 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002522 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002523 if (dai->driver->ops->set_fmt == NULL)
2524 return -ENOTSUPP;
2525 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002526}
2527EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2528
2529/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002530 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002531 * @slots: Number of slots in use.
2532 * @tx_mask: bitmask representing active TX slots.
2533 * @rx_mask: bitmask representing active RX slots.
2534 *
2535 * Generates the TDM tx and rx slot default masks for DAI.
2536 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002537static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002538 unsigned int *tx_mask,
2539 unsigned int *rx_mask)
2540{
2541 if (*tx_mask || *rx_mask)
2542 return 0;
2543
2544 if (!slots)
2545 return -EINVAL;
2546
2547 *tx_mask = (1 << slots) - 1;
2548 *rx_mask = (1 << slots) - 1;
2549
2550 return 0;
2551}
2552
2553/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002554 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2555 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002556 * @tx_mask: bitmask representing active TX slots.
2557 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002558 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002559 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002560 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002561 * This function configures the specified DAI for TDM operation. @slot contains
2562 * the total number of slots of the TDM stream and @slot_with the width of each
2563 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2564 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2565 * DAI should write to or read from. If a bit is set the corresponding slot is
2566 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2567 * the first slot, bit 1 to the second slot and so on. The first active slot
2568 * maps to the first channel of the DAI, the second active slot to the second
2569 * channel and so on.
2570 *
2571 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2572 * @rx_mask and @slot_width will be ignored.
2573 *
2574 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002575 */
2576int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002577 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002578{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002579 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002580 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002581 &tx_mask, &rx_mask);
2582 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002583 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002584
Benoit Cousson88bd8702014-07-08 23:19:34 +02002585 dai->tx_mask = tx_mask;
2586 dai->rx_mask = rx_mask;
2587
Kuninori Morimoto46471922017-09-25 01:38:34 +00002588 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002589 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002590 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002591 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002592 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002593}
2594EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2595
2596/**
Barry Song472df3c2009-09-12 01:16:29 +08002597 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2598 * @dai: DAI
2599 * @tx_num: how many TX channels
2600 * @tx_slot: pointer to an array which imply the TX slot number channel
2601 * 0~num-1 uses
2602 * @rx_num: how many RX channels
2603 * @rx_slot: pointer to an array which imply the RX slot number channel
2604 * 0~num-1 uses
2605 *
2606 * configure the relationship between channel number and TDM slot number.
2607 */
2608int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2609 unsigned int tx_num, unsigned int *tx_slot,
2610 unsigned int rx_num, unsigned int *rx_slot)
2611{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002612 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002613 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002614 rx_num, rx_slot);
2615 else
2616 return -EINVAL;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2619
2620/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002621 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2622 * @dai: DAI
2623 * @tx_num: how many TX channels
2624 * @tx_slot: pointer to an array which imply the TX slot number channel
2625 * 0~num-1 uses
2626 * @rx_num: how many RX channels
2627 * @rx_slot: pointer to an array which imply the RX slot number channel
2628 * 0~num-1 uses
2629 */
2630int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2631 unsigned int *tx_num, unsigned int *tx_slot,
2632 unsigned int *rx_num, unsigned int *rx_slot)
2633{
2634 if (dai->driver->ops->get_channel_map)
2635 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2636 rx_num, rx_slot);
2637 else
2638 return -ENOTSUPP;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2641
2642/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002643 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2644 * @dai: DAI
2645 * @tristate: tristate enable
2646 *
2647 * Tristates the DAI so that others can use it.
2648 */
2649int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2650{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002651 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002652 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002653 else
2654 return -EINVAL;
2655}
2656EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2657
2658/**
2659 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2660 * @dai: DAI
2661 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002662 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002663 *
2664 * Mutes the DAI DAC.
2665 */
Mark Brownda183962013-02-06 15:44:07 +00002666int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2667 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002668{
Mark Brownda183962013-02-06 15:44:07 +00002669 if (!dai->driver)
2670 return -ENOTSUPP;
2671
2672 if (dai->driver->ops->mute_stream)
2673 return dai->driver->ops->mute_stream(dai, mute, direction);
2674 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2675 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002676 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002677 else
Mark Brown04570c62012-04-13 19:16:03 +01002678 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002679}
2680EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2681
Mark Brownc5af3a22008-11-28 13:29:45 +00002682/**
2683 * snd_soc_register_card - Register a card with the ASoC core
2684 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002685 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002686 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002687 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302688int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002689{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002690 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002691 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002692
Mark Brownc5af3a22008-11-28 13:29:45 +00002693 if (!card->name || !card->dev)
2694 return -EINVAL;
2695
Stephen Warren5a504962011-12-21 10:40:59 -07002696 for (i = 0; i < card->num_links; i++) {
2697 struct snd_soc_dai_link *link = &card->dai_link[i];
2698
Mengdong Lin923c5e612015-11-23 11:01:49 -05002699 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002700 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002701 dev_err(card->dev, "ASoC: failed to init link %s\n",
2702 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002703 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002704 }
Stephen Warren5a504962011-12-21 10:40:59 -07002705 }
2706
Mark Browned77cc12011-05-03 18:25:34 +01002707 dev_set_drvdata(card->dev, card);
2708
Stephen Warren111c6412011-01-28 14:26:35 -07002709 snd_soc_initialize_card_lists(card);
2710
Mengdong Linf8f80362015-12-02 14:11:22 +08002711 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002712
Mengdong Lin1a497982015-11-18 02:34:11 -05002713 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002714 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715
Mark Browndb432b42011-10-03 21:06:40 +01002716 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002717 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718 card->instantiated = 0;
2719 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002720 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721
Mark Brownb19e6e72012-03-14 21:18:39 +00002722 ret = snd_soc_instantiate_card(card);
2723 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002724 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002726 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002727 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002728 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002729 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002730 int j;
2731
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002732 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002733 if (!codec_dai->active)
2734 pinctrl_pm_select_sleep_state(codec_dai->dev);
2735 }
2736
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002737 if (!cpu_dai->active)
2738 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2739 }
2740
Mark Brownb19e6e72012-03-14 21:18:39 +00002741 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002742}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302743EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002744
2745/**
2746 * snd_soc_unregister_card - Unregister a card with the ASoC core
2747 *
2748 * @card: Card to unregister
2749 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002750 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302751int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002752{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002753 if (card->instantiated) {
2754 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002755 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302756 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002757 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002758 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759
2760 return 0;
2761}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302762EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002763
2764/*
2765 * Simplify DAI link configuration by removing ".-1" from device names
2766 * and sanitizing names.
2767 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002768static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002769{
2770 char *found, name[NAME_SIZE];
2771 int id1, id2;
2772
2773 if (dev_name(dev) == NULL)
2774 return NULL;
2775
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002776 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002777
2778 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002779 found = strstr(name, dev->driver->name);
2780 if (found) {
2781 /* get ID */
2782 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2783
2784 /* discard ID from name if ID == -1 */
2785 if (*id == -1)
2786 found[strlen(dev->driver->name)] = '\0';
2787 }
2788
2789 } else {
2790 /* I2C component devices are named "bus-addr" */
2791 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2792 char tmp[NAME_SIZE];
2793
2794 /* create unique ID number from I2C addr and bus */
2795 *id = ((id1 & 0xffff) << 16) + id2;
2796
2797 /* sanitize component name for DAI link creation */
2798 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002799 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002800 } else
2801 *id = 0;
2802 }
2803
2804 return kstrdup(name, GFP_KERNEL);
2805}
2806
2807/*
2808 * Simplify DAI link naming for single devices with multiple DAIs by removing
2809 * any ".-1" and using the DAI name (instead of device name).
2810 */
2811static inline char *fmt_multiple_name(struct device *dev,
2812 struct snd_soc_dai_driver *dai_drv)
2813{
2814 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002815 dev_err(dev,
2816 "ASoC: error - multiple DAI %s registered with no name\n",
2817 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002818 return NULL;
2819 }
2820
2821 return kstrdup(dai_drv->name, GFP_KERNEL);
2822}
2823
2824/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002825 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002826 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002827 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002828 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002829static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002830{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002831 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002832
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002833 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002834 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2835 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002836 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002837 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002838 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002839 }
Mark Brown91151712008-11-30 23:31:24 +00002840}
Mark Brown91151712008-11-30 23:31:24 +00002841
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002842/* Create a DAI and add it to the component's DAI list */
2843static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2844 struct snd_soc_dai_driver *dai_drv,
2845 bool legacy_dai_naming)
2846{
2847 struct device *dev = component->dev;
2848 struct snd_soc_dai *dai;
2849
2850 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2851
2852 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2853 if (dai == NULL)
2854 return NULL;
2855
2856 /*
2857 * Back in the old days when we still had component-less DAIs,
2858 * instead of having a static name, component-less DAIs would
2859 * inherit the name of the parent device so it is possible to
2860 * register multiple instances of the DAI. We still need to keep
2861 * the same naming style even though those DAIs are not
2862 * component-less anymore.
2863 */
2864 if (legacy_dai_naming &&
2865 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2866 dai->name = fmt_single_name(dev, &dai->id);
2867 } else {
2868 dai->name = fmt_multiple_name(dev, dai_drv);
2869 if (dai_drv->id)
2870 dai->id = dai_drv->id;
2871 else
2872 dai->id = component->num_dai;
2873 }
2874 if (dai->name == NULL) {
2875 kfree(dai);
2876 return NULL;
2877 }
2878
2879 dai->component = component;
2880 dai->dev = dev;
2881 dai->driver = dai_drv;
2882 if (!dai->driver->ops)
2883 dai->driver->ops = &null_dai_ops;
2884
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002885 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002886 component->num_dai++;
2887
2888 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2889 return dai;
2890}
2891
Mark Brown91151712008-11-30 23:31:24 +00002892/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002893 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002894 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002895 * @component: The component the DAIs are registered for
2896 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002897 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002898 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002899static int snd_soc_register_dais(struct snd_soc_component *component,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002900 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002901{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002902 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002903 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002904 unsigned int i;
2905 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002906
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002907 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002908
2909 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002910
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002911 dai = soc_add_dai(component, dai_drv + i,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002912 count == 1 && !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002913 if (dai == NULL) {
2914 ret = -ENOMEM;
2915 goto err;
2916 }
Mark Brown91151712008-11-30 23:31:24 +00002917 }
2918
2919 return 0;
2920
2921err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002922 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002923
2924 return ret;
2925}
Mark Brown91151712008-11-30 23:31:24 +00002926
Mengdong Lin68003e62015-12-31 16:40:43 +08002927/**
2928 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2929 *
2930 * @component: The component the DAIs are registered for
2931 * @dai_drv: DAI driver to use for the DAI
2932 *
2933 * Topology can use this API to register DAIs when probing a component.
2934 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2935 * will be freed in the component cleanup.
2936 */
2937int snd_soc_register_dai(struct snd_soc_component *component,
2938 struct snd_soc_dai_driver *dai_drv)
2939{
2940 struct snd_soc_dapm_context *dapm =
2941 snd_soc_component_get_dapm(component);
2942 struct snd_soc_dai *dai;
2943 int ret;
2944
2945 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2946 dev_err(component->dev, "Invalid dai type %d\n",
2947 dai_drv->dobj.type);
2948 return -EINVAL;
2949 }
2950
2951 lockdep_assert_held(&client_mutex);
2952 dai = soc_add_dai(component, dai_drv, false);
2953 if (!dai)
2954 return -ENOMEM;
2955
2956 /* Create the DAI widgets here. After adding DAIs, topology may
2957 * also add routes that need these widgets as source or sink.
2958 */
2959 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2960 if (ret != 0) {
2961 dev_err(component->dev,
2962 "Failed to create DAI widgets %d\n", ret);
2963 }
2964
2965 return ret;
2966}
2967EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2968
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002969static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2970 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002971{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002972 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002973
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002974 component->driver->seq_notifier(component, type, subseq);
2975}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002976
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002977static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2978 int event)
2979{
2980 struct snd_soc_component *component = dapm->component;
2981
2982 return component->driver->stream_event(component, event);
2983}
2984
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002985static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2986 enum snd_soc_bias_level level)
2987{
2988 struct snd_soc_component *component = dapm->component;
2989
2990 return component->driver->set_bias_level(component, level);
2991}
2992
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002993static int snd_soc_component_initialize(struct snd_soc_component *component,
2994 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002995{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002996 struct snd_soc_dapm_context *dapm;
2997
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002998 component->name = fmt_single_name(dev, &component->id);
2999 if (!component->name) {
3000 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003001 return -ENOMEM;
3002 }
3003
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003004 component->dev = dev;
3005 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003006
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003007 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003008 dapm->dev = dev;
3009 dapm->component = component;
3010 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003011 dapm->idle_bias_off = !driver->idle_bias_on;
3012 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003013 if (driver->seq_notifier)
3014 dapm->seq_notifier = snd_soc_component_seq_notifier;
3015 if (driver->stream_event)
3016 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003017 if (driver->set_bias_level)
3018 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003019
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003020 INIT_LIST_HEAD(&component->dai_list);
3021 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003022
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003023 return 0;
3024}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003025
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003026static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003027{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003028 int val_bytes = regmap_get_val_bytes(component->regmap);
3029
3030 /* Errors are legitimate for non-integer byte multiples */
3031 if (val_bytes > 0)
3032 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003033}
3034
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003035#ifdef CONFIG_REGMAP
3036
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003037/**
3038 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3039 * @component: The component for which to initialize the regmap instance
3040 * @regmap: The regmap instance that should be used by the component
3041 *
3042 * This function allows deferred assignment of the regmap instance that is
3043 * associated with the component. Only use this if the regmap instance is not
3044 * yet ready when the component is registered. The function must also be called
3045 * before the first IO attempt of the component.
3046 */
3047void snd_soc_component_init_regmap(struct snd_soc_component *component,
3048 struct regmap *regmap)
3049{
3050 component->regmap = regmap;
3051 snd_soc_component_setup_regmap(component);
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3054
3055/**
3056 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3057 * @component: The component for which to de-initialize the regmap instance
3058 *
3059 * Calls regmap_exit() on the regmap instance associated to the component and
3060 * removes the regmap instance from the component.
3061 *
3062 * This function should only be used if snd_soc_component_init_regmap() was used
3063 * to initialize the regmap instance.
3064 */
3065void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3066{
3067 regmap_exit(component->regmap);
3068 component->regmap = NULL;
3069}
3070EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3071
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003072#endif
3073
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003074static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003075{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003076 mutex_lock(&client_mutex);
3077
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003078 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003079 if (!component->regmap)
3080 component->regmap = dev_get_regmap(component->dev, NULL);
3081 if (component->regmap)
3082 snd_soc_component_setup_regmap(component);
3083 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003084
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003085 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003086 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003087
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003088 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003089}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003090
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003091static void snd_soc_component_cleanup(struct snd_soc_component *component)
3092{
3093 snd_soc_unregister_dais(component);
3094 kfree(component->name);
3095}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003096
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003097static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3098{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003099 struct snd_soc_card *card = component->card;
3100
3101 if (card)
3102 snd_soc_unregister_card(card);
3103
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003104 list_del(&component->list);
3105}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003106
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003107#define ENDIANNESS_MAP(name) \
3108 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3109static u64 endianness_format_map[] = {
3110 ENDIANNESS_MAP(S16_),
3111 ENDIANNESS_MAP(U16_),
3112 ENDIANNESS_MAP(S24_),
3113 ENDIANNESS_MAP(U24_),
3114 ENDIANNESS_MAP(S32_),
3115 ENDIANNESS_MAP(U32_),
3116 ENDIANNESS_MAP(S24_3),
3117 ENDIANNESS_MAP(U24_3),
3118 ENDIANNESS_MAP(S20_3),
3119 ENDIANNESS_MAP(U20_3),
3120 ENDIANNESS_MAP(S18_3),
3121 ENDIANNESS_MAP(U18_3),
3122 ENDIANNESS_MAP(FLOAT_),
3123 ENDIANNESS_MAP(FLOAT64_),
3124 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3125};
3126
3127/*
3128 * Fix up the DAI formats for endianness: codecs don't actually see
3129 * the endianness of the data but we're using the CPU format
3130 * definitions which do need to include endianness so we ensure that
3131 * codec DAIs always have both big and little endian variants set.
3132 */
3133static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3134{
3135 int i;
3136
3137 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3138 if (stream->formats & endianness_format_map[i])
3139 stream->formats |= endianness_format_map[i];
3140}
3141
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003142int snd_soc_add_component(struct device *dev,
3143 struct snd_soc_component *component,
3144 const struct snd_soc_component_driver *component_driver,
3145 struct snd_soc_dai_driver *dai_drv,
3146 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003147{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003148 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003149 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003150
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003151 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003152 if (ret)
3153 goto err_free;
3154
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003155 if (component_driver->endianness) {
3156 for (i = 0; i < num_dai; i++) {
3157 convert_endianness_formats(&dai_drv[i].playback);
3158 convert_endianness_formats(&dai_drv[i].capture);
3159 }
3160 }
3161
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003162 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003163 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003164 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003165 goto err_cleanup;
3166 }
3167
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003168 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003169
3170 return 0;
3171
3172err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003173 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003174err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003175 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003176}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003177EXPORT_SYMBOL_GPL(snd_soc_add_component);
3178
3179int snd_soc_register_component(struct device *dev,
3180 const struct snd_soc_component_driver *component_driver,
3181 struct snd_soc_dai_driver *dai_drv,
3182 int num_dai)
3183{
3184 struct snd_soc_component *component;
3185
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003186 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003187 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003188 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003189
3190 return snd_soc_add_component(dev, component, component_driver,
3191 dai_drv, num_dai);
3192}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003193EXPORT_SYMBOL_GPL(snd_soc_register_component);
3194
3195/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003196 * snd_soc_unregister_component - Unregister all related component
3197 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003198 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003199 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003200 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003201static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003202{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003203 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003204 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003205
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003206 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003207 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003208 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003209 continue;
3210
3211 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3212 snd_soc_component_del_unlocked(component);
3213 found = 1;
3214 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003215 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003216 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003217
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003218 if (found) {
3219 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003220 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003221
3222 return found;
3223}
3224
3225void snd_soc_unregister_component(struct device *dev)
3226{
3227 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003228}
3229EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3230
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003231struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3232 const char *driver_name)
3233{
3234 struct snd_soc_component *component;
3235 struct snd_soc_component *ret;
3236
3237 ret = NULL;
3238 mutex_lock(&client_mutex);
3239 list_for_each_entry(component, &component_list, list) {
3240 if (dev != component->dev)
3241 continue;
3242
3243 if (driver_name &&
3244 (driver_name != component->driver->name) &&
3245 (strcmp(component->driver->name, driver_name) != 0))
3246 continue;
3247
3248 ret = component;
3249 break;
3250 }
3251 mutex_unlock(&client_mutex);
3252
3253 return ret;
3254}
3255EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3256
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003257/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003258int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3259 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003260{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003261 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003262 int ret;
3263
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303264 if (!card->dev) {
3265 pr_err("card->dev is not set before calling %s\n", __func__);
3266 return -EINVAL;
3267 }
3268
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003269 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303270
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003271 ret = of_property_read_string_index(np, propname, 0, &card->name);
3272 /*
3273 * EINVAL means the property does not exist. This is fine providing
3274 * card->name was previously set, which is checked later in
3275 * snd_soc_register_card.
3276 */
3277 if (ret < 0 && ret != -EINVAL) {
3278 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003279 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003280 propname, ret);
3281 return ret;
3282 }
3283
3284 return 0;
3285}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003286EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003287
Xiubo Li9a6d4862014-02-08 15:59:52 +08003288static const struct snd_soc_dapm_widget simple_widgets[] = {
3289 SND_SOC_DAPM_MIC("Microphone", NULL),
3290 SND_SOC_DAPM_LINE("Line", NULL),
3291 SND_SOC_DAPM_HP("Headphone", NULL),
3292 SND_SOC_DAPM_SPK("Speaker", NULL),
3293};
3294
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003295int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003296 const char *propname)
3297{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003298 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003299 struct snd_soc_dapm_widget *widgets;
3300 const char *template, *wname;
3301 int i, j, num_widgets, ret;
3302
3303 num_widgets = of_property_count_strings(np, propname);
3304 if (num_widgets < 0) {
3305 dev_err(card->dev,
3306 "ASoC: Property '%s' does not exist\n", propname);
3307 return -EINVAL;
3308 }
3309 if (num_widgets & 1) {
3310 dev_err(card->dev,
3311 "ASoC: Property '%s' length is not even\n", propname);
3312 return -EINVAL;
3313 }
3314
3315 num_widgets /= 2;
3316 if (!num_widgets) {
3317 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3318 propname);
3319 return -EINVAL;
3320 }
3321
3322 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3323 GFP_KERNEL);
3324 if (!widgets) {
3325 dev_err(card->dev,
3326 "ASoC: Could not allocate memory for widgets\n");
3327 return -ENOMEM;
3328 }
3329
3330 for (i = 0; i < num_widgets; i++) {
3331 ret = of_property_read_string_index(np, propname,
3332 2 * i, &template);
3333 if (ret) {
3334 dev_err(card->dev,
3335 "ASoC: Property '%s' index %d read error:%d\n",
3336 propname, 2 * i, ret);
3337 return -EINVAL;
3338 }
3339
3340 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3341 if (!strncmp(template, simple_widgets[j].name,
3342 strlen(simple_widgets[j].name))) {
3343 widgets[i] = simple_widgets[j];
3344 break;
3345 }
3346 }
3347
3348 if (j >= ARRAY_SIZE(simple_widgets)) {
3349 dev_err(card->dev,
3350 "ASoC: DAPM widget '%s' is not supported\n",
3351 template);
3352 return -EINVAL;
3353 }
3354
3355 ret = of_property_read_string_index(np, propname,
3356 (2 * i) + 1,
3357 &wname);
3358 if (ret) {
3359 dev_err(card->dev,
3360 "ASoC: Property '%s' index %d read error:%d\n",
3361 propname, (2 * i) + 1, ret);
3362 return -EINVAL;
3363 }
3364
3365 widgets[i].name = wname;
3366 }
3367
Nicolin Chenf23e8602015-02-14 17:22:49 -08003368 card->of_dapm_widgets = widgets;
3369 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003370
3371 return 0;
3372}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003373EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003374
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003375int snd_soc_of_get_slot_mask(struct device_node *np,
3376 const char *prop_name,
3377 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003378{
3379 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003380 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003381 int i;
3382
3383 if (!of_slot_mask)
3384 return 0;
3385 val /= sizeof(u32);
3386 for (i = 0; i < val; i++)
3387 if (be32_to_cpup(&of_slot_mask[i]))
3388 *mask |= (1 << i);
3389
3390 return val;
3391}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003392EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003393
Xiubo Li89c67852014-02-14 09:34:35 +08003394int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003395 unsigned int *tx_mask,
3396 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003397 unsigned int *slots,
3398 unsigned int *slot_width)
3399{
3400 u32 val;
3401 int ret;
3402
Jyri Sarha61310842015-09-09 21:27:43 +03003403 if (tx_mask)
3404 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3405 if (rx_mask)
3406 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3407
Xiubo Li89c67852014-02-14 09:34:35 +08003408 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3409 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3410 if (ret)
3411 return ret;
3412
3413 if (slots)
3414 *slots = val;
3415 }
3416
3417 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3418 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3419 if (ret)
3420 return ret;
3421
3422 if (slot_width)
3423 *slot_width = val;
3424 }
3425
3426 return 0;
3427}
3428EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3429
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003430void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003431 struct snd_soc_codec_conf *codec_conf,
3432 struct device_node *of_node,
3433 const char *propname)
3434{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003435 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003436 const char *str;
3437 int ret;
3438
3439 ret = of_property_read_string(np, propname, &str);
3440 if (ret < 0) {
3441 /* no prefix is not error */
3442 return;
3443 }
3444
3445 codec_conf->of_node = of_node;
3446 codec_conf->name_prefix = str;
3447}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003448EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003449
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003450int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003451 const char *propname)
3452{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003453 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003454 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003455 struct snd_soc_dapm_route *routes;
3456 int i, ret;
3457
3458 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003459 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003460 dev_err(card->dev,
3461 "ASoC: Property '%s' does not exist or its length is not even\n",
3462 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003463 return -EINVAL;
3464 }
3465 num_routes /= 2;
3466 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003467 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003468 propname);
3469 return -EINVAL;
3470 }
3471
Kees Cooka86854d2018-06-12 14:07:58 -07003472 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003473 GFP_KERNEL);
3474 if (!routes) {
3475 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003476 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003477 return -EINVAL;
3478 }
3479
3480 for (i = 0; i < num_routes; i++) {
3481 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003482 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003483 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003484 dev_err(card->dev,
3485 "ASoC: Property '%s' index %d could not be read: %d\n",
3486 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003487 return -EINVAL;
3488 }
3489 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003490 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003491 if (ret) {
3492 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003493 "ASoC: Property '%s' index %d could not be read: %d\n",
3494 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003495 return -EINVAL;
3496 }
3497 }
3498
Nicolin Chenf23e8602015-02-14 17:22:49 -08003499 card->num_of_dapm_routes = num_routes;
3500 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003501
3502 return 0;
3503}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003504EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003505
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003506unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003507 const char *prefix,
3508 struct device_node **bitclkmaster,
3509 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003510{
3511 int ret, i;
3512 char prop[128];
3513 unsigned int format = 0;
3514 int bit, frame;
3515 const char *str;
3516 struct {
3517 char *name;
3518 unsigned int val;
3519 } of_fmt_table[] = {
3520 { "i2s", SND_SOC_DAIFMT_I2S },
3521 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3522 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3523 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3524 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3525 { "ac97", SND_SOC_DAIFMT_AC97 },
3526 { "pdm", SND_SOC_DAIFMT_PDM},
3527 { "msb", SND_SOC_DAIFMT_MSB },
3528 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003529 };
3530
3531 if (!prefix)
3532 prefix = "";
3533
3534 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003535 * check "dai-format = xxx"
3536 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003537 * SND_SOC_DAIFMT_FORMAT_MASK area
3538 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003539 ret = of_property_read_string(np, "dai-format", &str);
3540 if (ret < 0) {
3541 snprintf(prop, sizeof(prop), "%sformat", prefix);
3542 ret = of_property_read_string(np, prop, &str);
3543 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003544 if (ret == 0) {
3545 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3546 if (strcmp(str, of_fmt_table[i].name) == 0) {
3547 format |= of_fmt_table[i].val;
3548 break;
3549 }
3550 }
3551 }
3552
3553 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003554 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003555 * SND_SOC_DAIFMT_CLOCK_MASK area
3556 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003557 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003558 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003559 format |= SND_SOC_DAIFMT_CONT;
3560 else
3561 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003562
3563 /*
3564 * check "[prefix]bitclock-inversion"
3565 * check "[prefix]frame-inversion"
3566 * SND_SOC_DAIFMT_INV_MASK area
3567 */
3568 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3569 bit = !!of_get_property(np, prop, NULL);
3570
3571 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3572 frame = !!of_get_property(np, prop, NULL);
3573
3574 switch ((bit << 4) + frame) {
3575 case 0x11:
3576 format |= SND_SOC_DAIFMT_IB_IF;
3577 break;
3578 case 0x10:
3579 format |= SND_SOC_DAIFMT_IB_NF;
3580 break;
3581 case 0x01:
3582 format |= SND_SOC_DAIFMT_NB_IF;
3583 break;
3584 default:
3585 /* SND_SOC_DAIFMT_NB_NF is default */
3586 break;
3587 }
3588
3589 /*
3590 * check "[prefix]bitclock-master"
3591 * check "[prefix]frame-master"
3592 * SND_SOC_DAIFMT_MASTER_MASK area
3593 */
3594 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3595 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003596 if (bit && bitclkmaster)
3597 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003598
3599 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3600 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003601 if (frame && framemaster)
3602 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003603
3604 switch ((bit << 4) + frame) {
3605 case 0x11:
3606 format |= SND_SOC_DAIFMT_CBM_CFM;
3607 break;
3608 case 0x10:
3609 format |= SND_SOC_DAIFMT_CBM_CFS;
3610 break;
3611 case 0x01:
3612 format |= SND_SOC_DAIFMT_CBS_CFM;
3613 break;
3614 default:
3615 format |= SND_SOC_DAIFMT_CBS_CFS;
3616 break;
3617 }
3618
3619 return format;
3620}
3621EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3622
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003623int snd_soc_get_dai_id(struct device_node *ep)
3624{
3625 struct snd_soc_component *pos;
3626 struct device_node *node;
3627 int ret;
3628
3629 node = of_graph_get_port_parent(ep);
3630
3631 /*
3632 * For example HDMI case, HDMI has video/sound port,
3633 * but ALSA SoC needs sound port number only.
3634 * Thus counting HDMI DT port/endpoint doesn't work.
3635 * Then, it should have .of_xlate_dai_id
3636 */
3637 ret = -ENOTSUPP;
3638 mutex_lock(&client_mutex);
3639 list_for_each_entry(pos, &component_list, list) {
3640 struct device_node *component_of_node = pos->dev->of_node;
3641
3642 if (!component_of_node && pos->dev->parent)
3643 component_of_node = pos->dev->parent->of_node;
3644
3645 if (component_of_node != node)
3646 continue;
3647
3648 if (pos->driver->of_xlate_dai_id)
3649 ret = pos->driver->of_xlate_dai_id(pos, ep);
3650
3651 break;
3652 }
3653 mutex_unlock(&client_mutex);
3654
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003655 of_node_put(node);
3656
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003657 return ret;
3658}
3659EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3660
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003661int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003662 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003663{
3664 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003665 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003666 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003667
3668 mutex_lock(&client_mutex);
3669 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003670 component_of_node = pos->dev->of_node;
3671 if (!component_of_node && pos->dev->parent)
3672 component_of_node = pos->dev->parent->of_node;
3673
3674 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003675 continue;
3676
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003677 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003678 ret = pos->driver->of_xlate_dai_name(pos,
3679 args,
3680 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003681 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003682 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003683 int id = -1;
3684
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003685 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003686 case 0:
3687 id = 0; /* same as dai_drv[0] */
3688 break;
3689 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003690 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003691 break;
3692 default:
3693 /* not supported */
3694 break;
3695 }
3696
3697 if (id < 0 || id >= pos->num_dai) {
3698 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003699 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003700 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003701
3702 ret = 0;
3703
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003704 /* find target DAI */
3705 list_for_each_entry(dai, &pos->dai_list, list) {
3706 if (id == 0)
3707 break;
3708 id--;
3709 }
3710
3711 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003712 if (!*dai_name)
3713 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003714 }
3715
Kuninori Morimotocb470082013-09-10 17:39:56 -07003716 break;
3717 }
3718 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003719 return ret;
3720}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003721EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003722
3723int snd_soc_of_get_dai_name(struct device_node *of_node,
3724 const char **dai_name)
3725{
3726 struct of_phandle_args args;
3727 int ret;
3728
3729 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3730 "#sound-dai-cells", 0, &args);
3731 if (ret)
3732 return ret;
3733
3734 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003735
3736 of_node_put(args.np);
3737
3738 return ret;
3739}
3740EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3741
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003742/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003743 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3744 * @dai_link: DAI link
3745 *
3746 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3747 */
3748void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3749{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003750 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003751 int index;
3752
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003753 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003754 if (!component->of_node)
3755 break;
3756 of_node_put(component->of_node);
3757 component->of_node = NULL;
3758 }
3759}
3760EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3761
3762/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003763 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3764 * @dev: Card device
3765 * @of_node: Device node
3766 * @dai_link: DAI link
3767 *
3768 * Builds an array of CODEC DAI components from the DAI link property
3769 * 'sound-dai'.
3770 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003771 * The device nodes in the array (of_node) must be dereferenced by calling
3772 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003773 *
3774 * Returns 0 for success
3775 */
3776int snd_soc_of_get_dai_link_codecs(struct device *dev,
3777 struct device_node *of_node,
3778 struct snd_soc_dai_link *dai_link)
3779{
3780 struct of_phandle_args args;
3781 struct snd_soc_dai_link_component *component;
3782 char *name;
3783 int index, num_codecs, ret;
3784
3785 /* Count the number of CODECs */
3786 name = "sound-dai";
3787 num_codecs = of_count_phandle_with_args(of_node, name,
3788 "#sound-dai-cells");
3789 if (num_codecs <= 0) {
3790 if (num_codecs == -ENOENT)
3791 dev_err(dev, "No 'sound-dai' property\n");
3792 else
3793 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3794 return num_codecs;
3795 }
Kees Cooka86854d2018-06-12 14:07:58 -07003796 component = devm_kcalloc(dev,
3797 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003798 GFP_KERNEL);
3799 if (!component)
3800 return -ENOMEM;
3801 dai_link->codecs = component;
3802 dai_link->num_codecs = num_codecs;
3803
3804 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003805 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003806 ret = of_parse_phandle_with_args(of_node, name,
3807 "#sound-dai-cells",
3808 index, &args);
3809 if (ret)
3810 goto err;
3811 component->of_node = args.np;
3812 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3813 if (ret < 0)
3814 goto err;
3815 }
3816 return 0;
3817err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003818 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003819 dai_link->codecs = NULL;
3820 dai_link->num_codecs = 0;
3821 return ret;
3822}
3823EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3824
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003825static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003826{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003827 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003828 snd_soc_util_init();
3829
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003830 return platform_driver_register(&soc_driver);
3831}
Mark Brown4abe8e12010-10-12 17:41:03 +01003832module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003833
Mark Brown7d8c16a2008-11-30 22:11:24 +00003834static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003835{
Mark Brownfb257892011-04-28 10:57:54 +01003836 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003837 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003838
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003839 platform_driver_unregister(&soc_driver);
3840}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003841module_exit(snd_soc_exit);
3842
3843/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003844MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003845MODULE_DESCRIPTION("ALSA SoC Core");
3846MODULE_LICENSE("GPL");
3847MODULE_ALIAS("platform:soc-audio");