blob: 4e9367aacc0c9c259d6c2cd547330cabfe35193f [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
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200945 if (dai && dai->probed &&
946 dai->driver->remove_order == order) {
947 if (dai->driver->remove) {
948 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100949 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200950 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100951 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200952 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100953 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200954 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100955 }
956}
957
Mengdong Lin1a497982015-11-18 02:34:11 -0500958static void soc_remove_link_dais(struct snd_soc_card *card,
959 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200961 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000962 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800966 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000967 rtd->dev_registered = 0;
968 }
969
970 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000971 for_each_rtd_codec_dai(rtd, i, codec_dai)
972 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200974 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975}
976
Mengdong Lin1a497982015-11-18 02:34:11 -0500977static void soc_remove_link_components(struct snd_soc_card *card,
978 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600979{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200980 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000981 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600982
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000983 for_each_rtdcom(rtd, rtdcom) {
984 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600985
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200986 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200987 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600988 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989}
990
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900991static void soc_remove_dai_links(struct snd_soc_card *card)
992{
Mengdong Lin1a497982015-11-18 02:34:11 -0500993 int order;
994 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800995 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900996
Liam Girdwood0168bf02011-06-07 16:08:05 +0100997 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
998 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500999 list_for_each_entry(rtd, &card->rtd_list, list)
1000 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001001 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001002
1003 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1004 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001005 list_for_each_entry(rtd, &card->rtd_list, list)
1006 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007 }
1008
Mengdong Linf8f80362015-12-02 14:11:22 +08001009 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1010 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1011 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1012 link->name);
1013
1014 list_del(&link->list);
1015 card->num_dai_links--;
1016 }
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);
1184 card->num_dai_links++;
1185
1186 return 0;
1187}
1188EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1189
1190/**
1191 * snd_soc_remove_dai_link - Remove a DAI link from the list
1192 * @card: The ASoC card that owns the link
1193 * @dai_link: The DAI link to remove
1194 *
1195 * This function removes a DAI link from the ASoC card's link list.
1196 *
1197 * For DAI links previously added by topology, topology should
1198 * remove them by using the dobj embedded in the link.
1199 */
1200void snd_soc_remove_dai_link(struct snd_soc_card *card,
1201 struct snd_soc_dai_link *dai_link)
1202{
1203 struct snd_soc_dai_link *link, *_link;
1204
1205 if (dai_link->dobj.type
1206 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1207 dev_err(card->dev, "Invalid dai link type %d\n",
1208 dai_link->dobj.type);
1209 return;
1210 }
1211
1212 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001213 /* Notify the machine driver for extra destruction
1214 * on the link created by topology.
1215 */
1216 if (dai_link->dobj.type && card->remove_dai_link)
1217 card->remove_dai_link(card, dai_link);
1218
Mengdong Linf8f80362015-12-02 14:11:22 +08001219 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1220 if (link == dai_link) {
1221 list_del(&link->list);
1222 card->num_dai_links--;
1223 return;
1224 }
1225 }
1226}
1227EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1228
Jerome Brunetaefba452018-07-13 14:50:43 +02001229static void soc_set_of_name_prefix(struct snd_soc_component *component)
1230{
1231 struct device_node *component_of_node = component->dev->of_node;
1232 const char *str;
1233 int ret;
1234
1235 if (!component_of_node && component->dev->parent)
1236 component_of_node = component->dev->parent->of_node;
1237
1238 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1239 &str);
1240 if (!ret)
1241 component->name_prefix = str;
1242}
1243
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001244static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001245 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001246{
1247 int i;
1248
Jerome Brunetaefba452018-07-13 14:50:43 +02001249 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001250 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001251 struct device_node *component_of_node = component->dev->of_node;
1252
1253 if (!component_of_node && component->dev->parent)
1254 component_of_node = component->dev->parent->of_node;
1255
1256 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001257 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001258 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001259 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001260 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001261 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001262 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001263
1264 /*
1265 * If there is no configuration table or no match in the table,
1266 * check if a prefix is provided in the node
1267 */
1268 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001269}
1270
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001271static int soc_probe_component(struct snd_soc_card *card,
1272 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001273{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001274 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001275 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001276 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001277
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001278 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001279 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001280
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001281 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001282 if (component->card != card) {
1283 dev_err(component->dev,
1284 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1285 card->name, component->card->name);
1286 return -ENODEV;
1287 }
1288 return 0;
1289 }
1290
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001291 if (!try_module_get(component->dev->driver->owner))
1292 return -ENODEV;
1293
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001294 component->card = card;
1295 dapm->card = card;
1296 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001297
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001298 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001299
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001300 if (component->driver->dapm_widgets) {
1301 ret = snd_soc_dapm_new_controls(dapm,
1302 component->driver->dapm_widgets,
1303 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001304
1305 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001306 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001307 "Failed to create new controls %d\n", ret);
1308 goto err_probe;
1309 }
1310 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001311
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001312 list_for_each_entry(dai, &component->dai_list, list) {
1313 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001314 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001315 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001316 "Failed to create DAI widgets %d\n", ret);
1317 goto err_probe;
1318 }
1319 }
Mark Brown888df392012-02-16 19:37:51 -08001320
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001321 if (component->driver->probe) {
1322 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001323 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001324 dev_err(component->dev,
1325 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001326 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001327 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001328
1329 WARN(dapm->idle_bias_off &&
1330 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001331 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001332 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333 }
1334
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001335 /* machine specific init */
1336 if (component->init) {
1337 ret = component->init(component);
1338 if (ret < 0) {
1339 dev_err(component->dev,
1340 "Failed to do machine specific init %d\n", ret);
1341 goto err_probe;
1342 }
1343 }
1344
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001345 if (component->driver->controls)
1346 snd_soc_add_component_controls(component,
1347 component->driver->controls,
1348 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001349 if (component->driver->dapm_routes)
1350 snd_soc_dapm_add_routes(dapm,
1351 component->driver->dapm_routes,
1352 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001353
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001354 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001355 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001356
Jarkko Nikula70d293312011-01-27 16:24:22 +02001357 return 0;
1358
1359err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001360 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001361 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001362 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001363
1364 return ret;
1365}
1366
Mark Brown36ae1a92012-01-06 17:12:45 -08001367static void rtd_release(struct device *dev)
1368{
1369 kfree(dev);
1370}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001371
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001372static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1373 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001374{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001375 int ret = 0;
1376
Jarkko Nikula589c3562010-12-06 16:27:07 +02001377 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001378 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1379 if (!rtd->dev)
1380 return -ENOMEM;
1381 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001382 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001383 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001384 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001385 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001386 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001387 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001388 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1389 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1390 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1391 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001392 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001393 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001394 /* calling put_device() here to free the rtd->dev */
1395 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001396 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001397 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001398 return ret;
1399 }
1400 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001401 return 0;
1402}
1403
Mengdong Lin1a497982015-11-18 02:34:11 -05001404static int soc_probe_link_components(struct snd_soc_card *card,
1405 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001406 int order)
1407{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001408 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001409 struct snd_soc_rtdcom_list *rtdcom;
1410 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001411
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001412 for_each_rtdcom(rtd, rtdcom) {
1413 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001414
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001415 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001416 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001417 if (ret < 0)
1418 return ret;
1419 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001420 }
1421
Stephen Warren62ae68f2012-06-08 12:34:23 -06001422 return 0;
1423}
1424
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001425static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001426{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001427 if (dai->probed ||
1428 dai->driver->probe_order != order)
1429 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001430
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001431 if (dai->driver->probe) {
1432 int ret = dai->driver->probe(dai);
1433 if (ret < 0) {
1434 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1435 dai->name, ret);
1436 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001437 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001438 }
1439
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001440 dai->probed = 1;
1441
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442 return 0;
1443}
1444
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001445static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1446 struct snd_soc_pcm_runtime *rtd)
1447{
1448 int i, ret = 0;
1449
1450 for (i = 0; i < num_dais; ++i) {
1451 struct snd_soc_dai_driver *drv = dais[i]->driver;
1452
1453 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1454 ret = drv->pcm_new(rtd, dais[i]);
1455 if (ret < 0) {
1456 dev_err(dais[i]->dev,
1457 "ASoC: Failed to bind %s with pcm device\n",
1458 dais[i]->name);
1459 return ret;
1460 }
1461 }
1462
1463 return 0;
1464}
1465
Mengdong Lin1a497982015-11-18 02:34:11 -05001466static int soc_probe_link_dais(struct snd_soc_card *card,
1467 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468{
Mengdong Lin1a497982015-11-18 02:34:11 -05001469 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001470 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001471 struct snd_soc_rtdcom_list *rtdcom;
1472 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001473 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001474 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001476 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001477 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001479 /* set default power off timeout */
1480 rtd->pmdown_time = pmdown_time;
1481
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001482 ret = soc_probe_dai(cpu_dai, order);
1483 if (ret)
1484 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001487 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1488 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001489 if (ret)
1490 return ret;
1491 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001492
Liam Girdwood0168bf02011-06-07 16:08:05 +01001493 /* complete DAI probe during last probe */
1494 if (order != SND_SOC_COMP_ORDER_LAST)
1495 return 0;
1496
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001497 /* do machine specific initialization */
1498 if (dai_link->init) {
1499 ret = dai_link->init(rtd);
1500 if (ret < 0) {
1501 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1502 dai_link->name, ret);
1503 return ret;
1504 }
1505 }
1506
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001507 if (dai_link->dai_fmt)
1508 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1509
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001510 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001511 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001512 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001513
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001514#ifdef CONFIG_DEBUG_FS
1515 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001516 if (dai_link->dynamic)
1517 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001518#endif
1519
Liam Girdwooda655de82018-07-02 16:59:54 +01001520 num = rtd->num;
1521
1522 /*
1523 * most drivers will register their PCMs using DAI link ordering but
1524 * topology based drivers can use the DAI link id field to set PCM
1525 * device number and then use rtd + a base offset of the BEs.
1526 */
1527 for_each_rtdcom(rtd, rtdcom) {
1528 component = rtdcom->component;
1529
1530 if (!component->driver->use_dai_pcm_id)
1531 continue;
1532
1533 if (rtd->dai_link->no_pcm)
1534 num += component->driver->be_pcm_base;
1535 else
1536 num = rtd->dai_link->id;
1537 }
1538
Jie Yang6f0c4222015-10-13 23:41:00 +08001539 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301540 /*create compress_device"*/
Liam Girdwooda655de82018-07-02 16:59:54 +01001541 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001542 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001543 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301544 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001545 return ret;
1546 }
1547 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301548
1549 if (!dai_link->params) {
1550 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001551 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301552 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001553 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301554 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001555 return ret;
1556 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001557 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1558 if (ret < 0)
1559 return ret;
1560 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1561 rtd->num_codecs, rtd);
1562 if (ret < 0)
1563 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301564 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001565 INIT_DELAYED_WORK(&rtd->delayed_work,
1566 codec2codec_close_delayed_work);
Mark Brownc74184e2012-04-04 22:12:09 +01001567 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001568 }
1569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570 return 0;
1571}
1572
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001573static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001574{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001575 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001576 struct snd_soc_component *component;
1577 const char *name;
1578 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001579
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001580 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1581 /* codecs, usually analog devices */
1582 name = aux_dev->codec_name;
1583 codec_of_node = aux_dev->codec_of_node;
1584 component = soc_find_component(codec_of_node, name);
1585 if (!component) {
1586 if (codec_of_node)
1587 name = of_node_full_name(codec_of_node);
1588 goto err_defer;
1589 }
1590 } else if (aux_dev->name) {
1591 /* generic components */
1592 name = aux_dev->name;
1593 component = soc_find_component(NULL, name);
1594 if (!component)
1595 goto err_defer;
1596 } else {
1597 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1598 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001599 }
1600
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001601 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001602 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001603
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001604 return 0;
1605
1606err_defer:
1607 dev_err(card->dev, "ASoC: %s not registered\n", name);
1608 return -EPROBE_DEFER;
1609}
1610
1611static int soc_probe_aux_devices(struct snd_soc_card *card)
1612{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001613 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001614 int order;
1615 int ret;
1616
1617 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1618 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001619 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001620 if (comp->driver->probe_order == order) {
1621 ret = soc_probe_component(card, comp);
1622 if (ret < 0) {
1623 dev_err(card->dev,
1624 "ASoC: failed to probe aux component %s %d\n",
1625 comp->name, ret);
1626 return ret;
1627 }
1628 }
1629 }
1630 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001631
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001632 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001633}
1634
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001635static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001636{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001637 struct snd_soc_component *comp, *_comp;
1638 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001639
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001640 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1641 order++) {
1642 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001643 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001644
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001645 if (comp->driver->remove_order == order) {
1646 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001647 /* remove it from the card's aux_comp_list */
1648 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001649 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001650 }
1651 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001652}
1653
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001654/**
1655 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1656 * @rtd: The runtime for which the DAI link format should be changed
1657 * @dai_fmt: The new DAI link format
1658 *
1659 * This function updates the DAI link format for all DAIs connected to the DAI
1660 * link for the specified runtime.
1661 *
1662 * Note: For setups with a static format set the dai_fmt field in the
1663 * corresponding snd_dai_link struct instead of using this function.
1664 *
1665 * Returns 0 on success, otherwise a negative error code.
1666 */
1667int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1668 unsigned int dai_fmt)
1669{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001671 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001672 unsigned int i;
1673 int ret;
1674
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001675 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001676 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1677 if (ret != 0 && ret != -ENOTSUPP) {
1678 dev_warn(codec_dai->dev,
1679 "ASoC: Failed to set DAI format: %d\n", ret);
1680 return ret;
1681 }
1682 }
1683
1684 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001685 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001686 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001687 unsigned int inv_dai_fmt;
1688
1689 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1690 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1691 case SND_SOC_DAIFMT_CBM_CFM:
1692 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1693 break;
1694 case SND_SOC_DAIFMT_CBM_CFS:
1695 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1696 break;
1697 case SND_SOC_DAIFMT_CBS_CFM:
1698 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1699 break;
1700 case SND_SOC_DAIFMT_CBS_CFS:
1701 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1702 break;
1703 }
1704
1705 dai_fmt = inv_dai_fmt;
1706 }
1707
1708 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1709 if (ret != 0 && ret != -ENOTSUPP) {
1710 dev_warn(cpu_dai->dev,
1711 "ASoC: Failed to set DAI format: %d\n", ret);
1712 return ret;
1713 }
1714
1715 return 0;
1716}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001717EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001718
Liam Girdwood345233d2017-01-14 16:13:02 +08001719
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001720#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001721/* Trim special characters, and replace '-' with '_' since '-' is used to
1722 * separate different DMI fields in the card long name. Only number and
1723 * alphabet characters and a few separator characters are kept.
1724 */
1725static void cleanup_dmi_name(char *name)
1726{
1727 int i, j = 0;
1728
1729 for (i = 0; name[i]; i++) {
1730 if (isalnum(name[i]) || (name[i] == '.')
1731 || (name[i] == '_'))
1732 name[j++] = name[i];
1733 else if (name[i] == '-')
1734 name[j++] = '_';
1735 }
1736
1737 name[j] = '\0';
1738}
1739
Mengdong Lin98faf432017-06-28 15:01:39 +08001740/* Check if a DMI field is valid, i.e. not containing any string
1741 * in the black list.
1742 */
1743static int is_dmi_valid(const char *field)
1744{
1745 int i = 0;
1746
1747 while (dmi_blacklist[i]) {
1748 if (strstr(field, dmi_blacklist[i]))
1749 return 0;
1750 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001751 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001752
1753 return 1;
1754}
1755
Liam Girdwood345233d2017-01-14 16:13:02 +08001756/**
1757 * snd_soc_set_dmi_name() - Register DMI names to card
1758 * @card: The card to register DMI names
1759 * @flavour: The flavour "differentiator" for the card amongst its peers.
1760 *
1761 * An Intel machine driver may be used by many different devices but are
1762 * difficult for userspace to differentiate, since machine drivers ususally
1763 * use their own name as the card short name and leave the card long name
1764 * blank. To differentiate such devices and fix bugs due to lack of
1765 * device-specific configurations, this function allows DMI info to be used
1766 * as the sound card long name, in the format of
1767 * "vendor-product-version-board"
1768 * (Character '-' is used to separate different DMI fields here).
1769 * This will help the user space to load the device-specific Use Case Manager
1770 * (UCM) configurations for the card.
1771 *
1772 * Possible card long names may be:
1773 * DellInc.-XPS139343-01-0310JH
1774 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1775 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1776 *
1777 * This function also supports flavoring the card longname to provide
1778 * the extra differentiation, like "vendor-product-version-board-flavor".
1779 *
1780 * We only keep number and alphabet characters and a few separator characters
1781 * in the card long name since UCM in the user space uses the card long names
1782 * as card configuration directory names and AudoConf cannot support special
1783 * charactors like SPACE.
1784 *
1785 * Returns 0 on success, otherwise a negative error code.
1786 */
1787int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1788{
1789 const char *vendor, *product, *product_version, *board;
1790 size_t longname_buf_size = sizeof(card->snd_card->longname);
1791 size_t len;
1792
1793 if (card->long_name)
1794 return 0; /* long name already set by driver or from DMI */
1795
1796 /* make up dmi long name as: vendor.product.version.board */
1797 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001798 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001799 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1800 return 0;
1801 }
1802
Mengdong Lin98faf432017-06-28 15:01:39 +08001803
Liam Girdwood345233d2017-01-14 16:13:02 +08001804 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1805 "%s", vendor);
1806 cleanup_dmi_name(card->dmi_longname);
1807
1808 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001809 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001810 len = strlen(card->dmi_longname);
1811 snprintf(card->dmi_longname + len,
1812 longname_buf_size - len,
1813 "-%s", product);
1814
1815 len++; /* skip the separator "-" */
1816 if (len < longname_buf_size)
1817 cleanup_dmi_name(card->dmi_longname + len);
1818
1819 /* some vendors like Lenovo may only put a self-explanatory
1820 * name in the product version field
1821 */
1822 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001823 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001824 len = strlen(card->dmi_longname);
1825 snprintf(card->dmi_longname + len,
1826 longname_buf_size - len,
1827 "-%s", product_version);
1828
1829 len++;
1830 if (len < longname_buf_size)
1831 cleanup_dmi_name(card->dmi_longname + len);
1832 }
1833 }
1834
1835 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001836 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001837 len = strlen(card->dmi_longname);
1838 snprintf(card->dmi_longname + len,
1839 longname_buf_size - len,
1840 "-%s", board);
1841
1842 len++;
1843 if (len < longname_buf_size)
1844 cleanup_dmi_name(card->dmi_longname + len);
1845 } else if (!product) {
1846 /* fall back to using legacy name */
1847 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1848 return 0;
1849 }
1850
1851 /* Add flavour to dmi long name */
1852 if (flavour) {
1853 len = strlen(card->dmi_longname);
1854 snprintf(card->dmi_longname + len,
1855 longname_buf_size - len,
1856 "-%s", flavour);
1857
1858 len++;
1859 if (len < longname_buf_size)
1860 cleanup_dmi_name(card->dmi_longname + len);
1861 }
1862
1863 /* set the card long name */
1864 card->long_name = card->dmi_longname;
1865
1866 return 0;
1867}
1868EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001869#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001870
Liam Girdwooda655de82018-07-02 16:59:54 +01001871static void soc_check_tplg_fes(struct snd_soc_card *card)
1872{
1873 struct snd_soc_component *component;
1874 const struct snd_soc_component_driver *comp_drv;
1875 struct snd_soc_dai_link *dai_link;
1876 int i;
1877
1878 list_for_each_entry(component, &component_list, list) {
1879
1880 /* does this component override FEs ? */
1881 if (!component->driver->ignore_machine)
1882 continue;
1883
1884 /* for this machine ? */
1885 if (strcmp(component->driver->ignore_machine,
1886 card->dev->driver->name))
1887 continue;
1888
1889 /* machine matches, so override the rtd data */
1890 for (i = 0; i < card->num_links; i++) {
1891
1892 dai_link = &card->dai_link[i];
1893
1894 /* ignore this FE */
1895 if (dai_link->dynamic) {
1896 dai_link->ignore = true;
1897 continue;
1898 }
1899
1900 dev_info(card->dev, "info: override FE DAI link %s\n",
1901 card->dai_link[i].name);
1902
1903 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001904 if (snd_soc_init_platform(card, dai_link) < 0) {
1905 dev_err(card->dev, "init platform error");
1906 continue;
1907 }
1908 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001909
1910 /* convert non BE into BE */
1911 dai_link->no_pcm = 1;
1912
1913 /* override any BE fixups */
1914 dai_link->be_hw_params_fixup =
1915 component->driver->be_hw_params_fixup;
1916
1917 /* most BE links don't set stream name, so set it to
1918 * dai link name if it's NULL to help bind widgets.
1919 */
1920 if (!dai_link->stream_name)
1921 dai_link->stream_name = dai_link->name;
1922 }
1923
1924 /* Inform userspace we are using alternate topology */
1925 if (component->driver->topology_name_prefix) {
1926
1927 /* topology shortname created ? */
1928 if (!card->topology_shortname_created) {
1929 comp_drv = component->driver;
1930
1931 snprintf(card->topology_shortname, 32, "%s-%s",
1932 comp_drv->topology_name_prefix,
1933 card->name);
1934 card->topology_shortname_created = true;
1935 }
1936
1937 /* use topology shortname */
1938 card->name = card->topology_shortname;
1939 }
1940 }
1941}
1942
Mark Brownb19e6e72012-03-14 21:18:39 +00001943static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001944{
Mengdong Lin1a497982015-11-18 02:34:11 -05001945 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001946 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001947 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001949 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001950 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001951
Liam Girdwooda655de82018-07-02 16:59:54 +01001952 /* check whether any platform is ignore machine FE and using topology */
1953 soc_check_tplg_fes(card);
1954
Mark Brownb19e6e72012-03-14 21:18:39 +00001955 /* bind DAIs */
1956 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001957 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00001958 if (ret != 0)
1959 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 }
1961
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001962 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001963 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001964 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001965 if (ret != 0)
1966 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001967 }
1968
Mengdong Linf8f80362015-12-02 14:11:22 +08001969 /* add predefined DAI links to the list */
1970 for (i = 0; i < card->num_links; i++)
1971 snd_soc_add_dai_link(card, card->dai_link+i);
1972
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001974 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975 card->owner, 0, &card->snd_card);
1976 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001977 dev_err(card->dev,
1978 "ASoC: can't create sound card for card %s: %d\n",
1979 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001980 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001981 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001983 soc_init_card_debugfs(card);
1984
Mark Browne37a4972011-03-02 18:21:57 +00001985 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1986 card->dapm.dev = card->dev;
1987 card->dapm.card = card;
1988 list_add(&card->dapm.list, &card->dapm_list);
1989
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001990#ifdef CONFIG_DEBUG_FS
1991 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1992#endif
1993
Mark Brown88ee1c62011-02-02 10:43:26 +00001994#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001995 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001996 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001997#endif
Andy Green6ed25972008-06-13 16:24:05 +01001998
Mark Brown9a841eb2011-04-12 17:51:37 -07001999 if (card->dapm_widgets)
2000 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2001 card->num_dapm_widgets);
2002
Nicolin Chenf23e8602015-02-14 17:22:49 -08002003 if (card->of_dapm_widgets)
2004 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2005 card->num_of_dapm_widgets);
2006
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007 /* initialise the sound card only once */
2008 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002009 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002010 if (ret < 0)
2011 goto card_probe_error;
2012 }
2013
Stephen Warren62ae68f2012-06-08 12:34:23 -06002014 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002015 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2016 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002017 list_for_each_entry(rtd, &card->rtd_list, list) {
2018 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002019 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002020 dev_err(card->dev,
2021 "ASoC: failed to instantiate card %d\n",
2022 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002023 goto probe_dai_err;
2024 }
2025 }
2026 }
2027
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002028 /* probe auxiliary components */
2029 ret = soc_probe_aux_devices(card);
2030 if (ret < 0)
2031 goto probe_dai_err;
2032
Mengdong Lin61b00882015-12-02 14:11:48 +08002033 /* Find new DAI links added during probing components and bind them.
2034 * Components with topology may bring new DAIs and DAI links.
2035 */
2036 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2037 if (soc_is_dai_link_bound(card, dai_link))
2038 continue;
2039
2040 ret = soc_init_dai_link(card, dai_link);
2041 if (ret)
2042 goto probe_dai_err;
2043 ret = soc_bind_dai_link(card, dai_link);
2044 if (ret)
2045 goto probe_dai_err;
2046 }
2047
Stephen Warren62ae68f2012-06-08 12:34:23 -06002048 /* probe all DAI links on this card */
2049 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2050 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002051 list_for_each_entry(rtd, &card->rtd_list, list) {
2052 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002053 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002054 dev_err(card->dev,
2055 "ASoC: failed to instantiate card %d\n",
2056 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002057 goto probe_dai_err;
2058 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059 }
2060 }
2061
Mark Brown888df392012-02-16 19:37:51 -08002062 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002063 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002064
Mark Brownb7af1da2011-04-07 19:18:44 +09002065 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002066 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002067
Mark Brownb8ad29d2011-03-02 18:35:51 +00002068 if (card->dapm_routes)
2069 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2070 card->num_dapm_routes);
2071
Nicolin Chenf23e8602015-02-14 17:22:49 -08002072 if (card->of_dapm_routes)
2073 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2074 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002075
Takashi Iwai861886d2017-04-24 08:54:42 +02002076 /* try to set some sane longname if DMI is available */
2077 snd_soc_set_dmi_name(card, NULL);
2078
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002079 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002080 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002081 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2082 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002083 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2084 "%s", card->driver_name ? card->driver_name : card->name);
2085 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2086 switch (card->snd_card->driver[i]) {
2087 case '_':
2088 case '-':
2089 case '\0':
2090 break;
2091 default:
2092 if (!isalnum(card->snd_card->driver[i]))
2093 card->snd_card->driver[i] = '_';
2094 break;
2095 }
2096 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097
Mark Brown28e9ad92011-03-02 18:36:34 +00002098 if (card->late_probe) {
2099 ret = card->late_probe(card);
2100 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002101 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002102 card->name, ret);
2103 goto probe_aux_dev_err;
2104 }
2105 }
2106
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002107 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002109 ret = snd_card_register(card->snd_card);
2110 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002111 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2112 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002113 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114 }
2115
Mark Brown435c5e22008-12-04 15:32:53 +00002116 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002117 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002118 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002119 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002120
2121 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002122
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002123probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002124 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002127 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002128
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002129card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002130 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002131 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002132
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002133 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002134 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002135 snd_card_free(card->snd_card);
2136
Mark Brownb19e6e72012-03-14 21:18:39 +00002137base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002138 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002139 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002140 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141
Mark Brownb19e6e72012-03-14 21:18:39 +00002142 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002143}
2144
2145/* probes a new socdev */
2146static int soc_probe(struct platform_device *pdev)
2147{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002148 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002149
Vinod Koul70a7ca32011-01-14 19:22:48 +05302150 /*
2151 * no card, so machine driver should be registering card
2152 * we should not be here in that case so ret error
2153 */
2154 if (!card)
2155 return -EINVAL;
2156
Mark Brownfe4085e2012-03-02 13:07:41 +00002157 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002158 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002159 card->name);
2160
Mark Brown435c5e22008-12-04 15:32:53 +00002161 /* Bodge while we unpick instantiation */
2162 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002163
Mark Brown28d528c2012-08-09 18:45:23 +01002164 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165}
2166
Vinod Koulb0e26482011-01-13 22:48:02 +05302167static int soc_cleanup_card_resources(struct snd_soc_card *card)
2168{
Mengdong Lin1a497982015-11-18 02:34:11 -05002169 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302170
2171 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002172 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002173 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302174
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002175 /* free the ALSA card at first; this syncs with pending operations */
2176 snd_card_free(card->snd_card);
2177
Vinod Koulb0e26482011-01-13 22:48:02 +05302178 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002179 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002180 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302181
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002182 /* remove auxiliary devices */
2183 soc_remove_aux_devices(card);
2184
Mark Brownd1e81422016-08-18 19:32:59 +01002185 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302186 soc_cleanup_card_debugfs(card);
2187
2188 /* remove the card */
2189 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002190 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302191
Vinod Koulb0e26482011-01-13 22:48:02 +05302192 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302193}
2194
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195/* removes a socdev */
2196static int soc_remove(struct platform_device *pdev)
2197{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002198 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199
Mark Brownc5af3a22008-11-28 13:29:45 +00002200 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201 return 0;
2202}
2203
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002204int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002205{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002206 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002207 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002208
2209 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002210 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002211
2212 /* Flush out pmdown_time work - we actually do want to run it
2213 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002214 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002215 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002216
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002217 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002218
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002219 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002220 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002221 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002222 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002223 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002224
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002225 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002226 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002227 pinctrl_pm_select_sleep_state(codec_dai->dev);
2228 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002229 }
2230
Mark Brown416356f2009-06-30 19:05:15 +01002231 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002232}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002233EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002234
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002235const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302236 .suspend = snd_soc_suspend,
2237 .resume = snd_soc_resume,
2238 .freeze = snd_soc_suspend,
2239 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002240 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302241 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002242};
Stephen Warrendeb26072011-04-05 19:35:30 -06002243EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002244
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245/* ASoC platform driver */
2246static struct platform_driver soc_driver = {
2247 .driver = {
2248 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002249 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250 },
2251 .probe = soc_probe,
2252 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253};
2254
Mark Brown096e49d2009-07-05 15:12:22 +01002255/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 * snd_soc_cnew - create new control
2257 * @_template: control template
2258 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002259 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002260 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 *
2262 * Create a new mixer control from a template control.
2263 *
2264 * Returns 0 for success, else error.
2265 */
2266struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002267 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002268 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002269{
2270 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002271 struct snd_kcontrol *kcontrol;
2272 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273
2274 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275 template.index = 0;
2276
Mark Brownefb7ac32011-03-08 17:23:24 +00002277 if (!long_name)
2278 long_name = template.name;
2279
2280 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002281 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002282 if (!name)
2283 return NULL;
2284
Mark Brownefb7ac32011-03-08 17:23:24 +00002285 template.name = name;
2286 } else {
2287 template.name = long_name;
2288 }
2289
2290 kcontrol = snd_ctl_new1(&template, data);
2291
2292 kfree(name);
2293
2294 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295}
2296EXPORT_SYMBOL_GPL(snd_soc_cnew);
2297
Liam Girdwood022658b2012-02-03 17:43:09 +00002298static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2299 const struct snd_kcontrol_new *controls, int num_controls,
2300 const char *prefix, void *data)
2301{
2302 int err, i;
2303
2304 for (i = 0; i < num_controls; i++) {
2305 const struct snd_kcontrol_new *control = &controls[i];
2306 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2307 control->name, prefix));
2308 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002309 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2310 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002311 return err;
2312 }
2313 }
2314
2315 return 0;
2316}
2317
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002318struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2319 const char *name)
2320{
2321 struct snd_card *card = soc_card->snd_card;
2322 struct snd_kcontrol *kctl;
2323
2324 if (unlikely(!name))
2325 return NULL;
2326
2327 list_for_each_entry(kctl, &card->controls, list)
2328 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2329 return kctl;
2330 return NULL;
2331}
2332EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2333
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002335 * snd_soc_add_component_controls - Add an array of controls to a component.
2336 *
2337 * @component: Component to add controls to
2338 * @controls: Array of controls to add
2339 * @num_controls: Number of elements in the array
2340 *
2341 * Return: 0 for success, else error.
2342 */
2343int snd_soc_add_component_controls(struct snd_soc_component *component,
2344 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2345{
2346 struct snd_card *card = component->card->snd_card;
2347
2348 return snd_soc_add_controls(card, component->dev, controls,
2349 num_controls, component->name_prefix, component);
2350}
2351EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2352
2353/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002354 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2355 * Convenience function to add a list of controls.
2356 *
2357 * @soc_card: SoC card to add controls to
2358 * @controls: array of controls to add
2359 * @num_controls: number of elements in the array
2360 *
2361 * Return 0 for success, else error.
2362 */
2363int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2364 const struct snd_kcontrol_new *controls, int num_controls)
2365{
2366 struct snd_card *card = soc_card->snd_card;
2367
2368 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2369 NULL, soc_card);
2370}
2371EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2372
2373/**
2374 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2375 * Convienience function to add a list of controls.
2376 *
2377 * @dai: DAI to add controls to
2378 * @controls: array of controls to add
2379 * @num_controls: number of elements in the array
2380 *
2381 * Return 0 for success, else error.
2382 */
2383int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2384 const struct snd_kcontrol_new *controls, int num_controls)
2385{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002386 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002387
2388 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2389 NULL, dai);
2390}
2391EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2392
2393/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002394 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2395 * @dai: DAI
2396 * @clk_id: DAI specific clock ID
2397 * @freq: new clock frequency in Hz
2398 * @dir: new clock direction - input/output.
2399 *
2400 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2401 */
2402int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2403 unsigned int freq, int dir)
2404{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002405 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002406 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002407
2408 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2409 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002410}
2411EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2412
2413/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002414 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2415 * @component: COMPONENT
2416 * @clk_id: DAI specific clock ID
2417 * @source: Source for the clock
2418 * @freq: new clock frequency in Hz
2419 * @dir: new clock direction - input/output.
2420 *
2421 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2422 */
2423int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2424 int source, unsigned int freq, int dir)
2425{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002426 if (component->driver->set_sysclk)
2427 return component->driver->set_sysclk(component, clk_id, source,
2428 freq, dir);
2429
2430 return -ENOTSUPP;
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2433
2434/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002435 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2436 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002437 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002438 * @div: new clock divisor.
2439 *
2440 * Configures the clock dividers. This is used to derive the best DAI bit and
2441 * frame clocks from the system or master clock. It's best to set the DAI bit
2442 * and frame clocks as low as possible to save system power.
2443 */
2444int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2445 int div_id, int div)
2446{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002447 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002448 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002449 else
2450 return -EINVAL;
2451}
2452EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2453
2454/**
2455 * snd_soc_dai_set_pll - configure DAI PLL.
2456 * @dai: DAI
2457 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002458 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002459 * @freq_in: PLL input clock frequency in Hz
2460 * @freq_out: requested PLL output clock frequency in Hz
2461 *
2462 * Configures and enables PLL to generate output clock based on input clock.
2463 */
Mark Brown85488032009-09-05 18:52:16 +01002464int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2465 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002466{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002467 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002468 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002469 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002470
2471 return snd_soc_component_set_pll(dai->component, pll_id, source,
2472 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002473}
2474EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2475
Mark Brownec4ee522011-03-07 20:58:11 +00002476/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002477 * snd_soc_component_set_pll - configure component PLL.
2478 * @component: COMPONENT
2479 * @pll_id: DAI specific PLL ID
2480 * @source: DAI specific source for the PLL
2481 * @freq_in: PLL input clock frequency in Hz
2482 * @freq_out: requested PLL output clock frequency in Hz
2483 *
2484 * Configures and enables PLL to generate output clock based on input clock.
2485 */
2486int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2487 int source, unsigned int freq_in,
2488 unsigned int freq_out)
2489{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002490 if (component->driver->set_pll)
2491 return component->driver->set_pll(component, pll_id, source,
2492 freq_in, freq_out);
2493
2494 return -EINVAL;
2495}
2496EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2497
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002498/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002499 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2500 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002501 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002502 *
2503 * Configures the DAI for a preset BCLK to sample rate ratio.
2504 */
2505int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2506{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002507 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002508 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2509 else
2510 return -EINVAL;
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2513
2514/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002515 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2516 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002517 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002518 *
2519 * Configures the DAI hardware format and clocking.
2520 */
2521int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2522{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002523 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002524 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002525 if (dai->driver->ops->set_fmt == NULL)
2526 return -ENOTSUPP;
2527 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002528}
2529EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2530
2531/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002532 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002533 * @slots: Number of slots in use.
2534 * @tx_mask: bitmask representing active TX slots.
2535 * @rx_mask: bitmask representing active RX slots.
2536 *
2537 * Generates the TDM tx and rx slot default masks for DAI.
2538 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002539static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002540 unsigned int *tx_mask,
2541 unsigned int *rx_mask)
2542{
2543 if (*tx_mask || *rx_mask)
2544 return 0;
2545
2546 if (!slots)
2547 return -EINVAL;
2548
2549 *tx_mask = (1 << slots) - 1;
2550 *rx_mask = (1 << slots) - 1;
2551
2552 return 0;
2553}
2554
2555/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002556 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2557 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002558 * @tx_mask: bitmask representing active TX slots.
2559 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002560 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002561 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002562 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002563 * This function configures the specified DAI for TDM operation. @slot contains
2564 * the total number of slots of the TDM stream and @slot_with the width of each
2565 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2566 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2567 * DAI should write to or read from. If a bit is set the corresponding slot is
2568 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2569 * the first slot, bit 1 to the second slot and so on. The first active slot
2570 * maps to the first channel of the DAI, the second active slot to the second
2571 * channel and so on.
2572 *
2573 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2574 * @rx_mask and @slot_width will be ignored.
2575 *
2576 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002577 */
2578int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002579 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002580{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002581 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002582 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002583 &tx_mask, &rx_mask);
2584 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002585 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002586
Benoit Cousson88bd8702014-07-08 23:19:34 +02002587 dai->tx_mask = tx_mask;
2588 dai->rx_mask = rx_mask;
2589
Kuninori Morimoto46471922017-09-25 01:38:34 +00002590 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002591 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002592 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002593 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002594 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002595}
2596EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2597
2598/**
Barry Song472df3c2009-09-12 01:16:29 +08002599 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2600 * @dai: DAI
2601 * @tx_num: how many TX channels
2602 * @tx_slot: pointer to an array which imply the TX slot number channel
2603 * 0~num-1 uses
2604 * @rx_num: how many RX channels
2605 * @rx_slot: pointer to an array which imply the RX slot number channel
2606 * 0~num-1 uses
2607 *
2608 * configure the relationship between channel number and TDM slot number.
2609 */
2610int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2611 unsigned int tx_num, unsigned int *tx_slot,
2612 unsigned int rx_num, unsigned int *rx_slot)
2613{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002614 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002615 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002616 rx_num, rx_slot);
2617 else
2618 return -EINVAL;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2621
2622/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002623 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2624 * @dai: DAI
2625 * @tx_num: how many TX channels
2626 * @tx_slot: pointer to an array which imply the TX slot number channel
2627 * 0~num-1 uses
2628 * @rx_num: how many RX channels
2629 * @rx_slot: pointer to an array which imply the RX slot number channel
2630 * 0~num-1 uses
2631 */
2632int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2633 unsigned int *tx_num, unsigned int *tx_slot,
2634 unsigned int *rx_num, unsigned int *rx_slot)
2635{
2636 if (dai->driver->ops->get_channel_map)
2637 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2638 rx_num, rx_slot);
2639 else
2640 return -ENOTSUPP;
2641}
2642EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2643
2644/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002645 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2646 * @dai: DAI
2647 * @tristate: tristate enable
2648 *
2649 * Tristates the DAI so that others can use it.
2650 */
2651int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2652{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002653 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002654 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002655 else
2656 return -EINVAL;
2657}
2658EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2659
2660/**
2661 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2662 * @dai: DAI
2663 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002664 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002665 *
2666 * Mutes the DAI DAC.
2667 */
Mark Brownda183962013-02-06 15:44:07 +00002668int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2669 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002670{
Mark Brownda183962013-02-06 15:44:07 +00002671 if (!dai->driver)
2672 return -ENOTSUPP;
2673
2674 if (dai->driver->ops->mute_stream)
2675 return dai->driver->ops->mute_stream(dai, mute, direction);
2676 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2677 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002678 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002679 else
Mark Brown04570c62012-04-13 19:16:03 +01002680 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002681}
2682EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2683
Mark Brownc5af3a22008-11-28 13:29:45 +00002684/**
2685 * snd_soc_register_card - Register a card with the ASoC core
2686 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002687 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002688 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002689 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302690int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002691{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002692 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002693 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002694
Mark Brownc5af3a22008-11-28 13:29:45 +00002695 if (!card->name || !card->dev)
2696 return -EINVAL;
2697
Stephen Warren5a504962011-12-21 10:40:59 -07002698 for (i = 0; i < card->num_links; i++) {
2699 struct snd_soc_dai_link *link = &card->dai_link[i];
2700
Mengdong Lin923c5e612015-11-23 11:01:49 -05002701 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002702 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002703 dev_err(card->dev, "ASoC: failed to init link %s\n",
2704 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002705 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002706 }
Stephen Warren5a504962011-12-21 10:40:59 -07002707 }
2708
Mark Browned77cc12011-05-03 18:25:34 +01002709 dev_set_drvdata(card->dev, card);
2710
Stephen Warren111c6412011-01-28 14:26:35 -07002711 snd_soc_initialize_card_lists(card);
2712
Mengdong Linf8f80362015-12-02 14:11:22 +08002713 INIT_LIST_HEAD(&card->dai_link_list);
2714 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002715
Mengdong Lin1a497982015-11-18 02:34:11 -05002716 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002717 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718
Mark Browndb432b42011-10-03 21:06:40 +01002719 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002720 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 card->instantiated = 0;
2722 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002723 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002724
Mark Brownb19e6e72012-03-14 21:18:39 +00002725 ret = snd_soc_instantiate_card(card);
2726 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002727 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002729 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002730 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002731 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002732 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002733 int j;
2734
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002735 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002736 if (!codec_dai->active)
2737 pinctrl_pm_select_sleep_state(codec_dai->dev);
2738 }
2739
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002740 if (!cpu_dai->active)
2741 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2742 }
2743
Mark Brownb19e6e72012-03-14 21:18:39 +00002744 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002745}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302746EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747
2748/**
2749 * snd_soc_unregister_card - Unregister a card with the ASoC core
2750 *
2751 * @card: Card to unregister
2752 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002753 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302754int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002755{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002756 if (card->instantiated) {
2757 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002758 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302759 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002760 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002761 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002762
2763 return 0;
2764}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302765EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002766
2767/*
2768 * Simplify DAI link configuration by removing ".-1" from device names
2769 * and sanitizing names.
2770 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002771static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002772{
2773 char *found, name[NAME_SIZE];
2774 int id1, id2;
2775
2776 if (dev_name(dev) == NULL)
2777 return NULL;
2778
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002779 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002780
2781 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002782 found = strstr(name, dev->driver->name);
2783 if (found) {
2784 /* get ID */
2785 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2786
2787 /* discard ID from name if ID == -1 */
2788 if (*id == -1)
2789 found[strlen(dev->driver->name)] = '\0';
2790 }
2791
2792 } else {
2793 /* I2C component devices are named "bus-addr" */
2794 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2795 char tmp[NAME_SIZE];
2796
2797 /* create unique ID number from I2C addr and bus */
2798 *id = ((id1 & 0xffff) << 16) + id2;
2799
2800 /* sanitize component name for DAI link creation */
2801 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002802 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002803 } else
2804 *id = 0;
2805 }
2806
2807 return kstrdup(name, GFP_KERNEL);
2808}
2809
2810/*
2811 * Simplify DAI link naming for single devices with multiple DAIs by removing
2812 * any ".-1" and using the DAI name (instead of device name).
2813 */
2814static inline char *fmt_multiple_name(struct device *dev,
2815 struct snd_soc_dai_driver *dai_drv)
2816{
2817 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002818 dev_err(dev,
2819 "ASoC: error - multiple DAI %s registered with no name\n",
2820 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002821 return NULL;
2822 }
2823
2824 return kstrdup(dai_drv->name, GFP_KERNEL);
2825}
2826
2827/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002828 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002829 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002830 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002831 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002832static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002833{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002834 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002835
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002836 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002837 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2838 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002839 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002840 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002841 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002842 }
Mark Brown91151712008-11-30 23:31:24 +00002843}
Mark Brown91151712008-11-30 23:31:24 +00002844
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002845/* Create a DAI and add it to the component's DAI list */
2846static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2847 struct snd_soc_dai_driver *dai_drv,
2848 bool legacy_dai_naming)
2849{
2850 struct device *dev = component->dev;
2851 struct snd_soc_dai *dai;
2852
2853 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2854
2855 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2856 if (dai == NULL)
2857 return NULL;
2858
2859 /*
2860 * Back in the old days when we still had component-less DAIs,
2861 * instead of having a static name, component-less DAIs would
2862 * inherit the name of the parent device so it is possible to
2863 * register multiple instances of the DAI. We still need to keep
2864 * the same naming style even though those DAIs are not
2865 * component-less anymore.
2866 */
2867 if (legacy_dai_naming &&
2868 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2869 dai->name = fmt_single_name(dev, &dai->id);
2870 } else {
2871 dai->name = fmt_multiple_name(dev, dai_drv);
2872 if (dai_drv->id)
2873 dai->id = dai_drv->id;
2874 else
2875 dai->id = component->num_dai;
2876 }
2877 if (dai->name == NULL) {
2878 kfree(dai);
2879 return NULL;
2880 }
2881
2882 dai->component = component;
2883 dai->dev = dev;
2884 dai->driver = dai_drv;
2885 if (!dai->driver->ops)
2886 dai->driver->ops = &null_dai_ops;
2887
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002888 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002889 component->num_dai++;
2890
2891 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2892 return dai;
2893}
2894
Mark Brown91151712008-11-30 23:31:24 +00002895/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002896 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002897 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002898 * @component: The component the DAIs are registered for
2899 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002900 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002901 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002902static int snd_soc_register_dais(struct snd_soc_component *component,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002903 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002904{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002905 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002906 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002907 unsigned int i;
2908 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002909
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002910 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002911
2912 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002913
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002914 dai = soc_add_dai(component, dai_drv + i,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002915 count == 1 && !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002916 if (dai == NULL) {
2917 ret = -ENOMEM;
2918 goto err;
2919 }
Mark Brown91151712008-11-30 23:31:24 +00002920 }
2921
2922 return 0;
2923
2924err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002925 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002926
2927 return ret;
2928}
Mark Brown91151712008-11-30 23:31:24 +00002929
Mengdong Lin68003e62015-12-31 16:40:43 +08002930/**
2931 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2932 *
2933 * @component: The component the DAIs are registered for
2934 * @dai_drv: DAI driver to use for the DAI
2935 *
2936 * Topology can use this API to register DAIs when probing a component.
2937 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2938 * will be freed in the component cleanup.
2939 */
2940int snd_soc_register_dai(struct snd_soc_component *component,
2941 struct snd_soc_dai_driver *dai_drv)
2942{
2943 struct snd_soc_dapm_context *dapm =
2944 snd_soc_component_get_dapm(component);
2945 struct snd_soc_dai *dai;
2946 int ret;
2947
2948 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2949 dev_err(component->dev, "Invalid dai type %d\n",
2950 dai_drv->dobj.type);
2951 return -EINVAL;
2952 }
2953
2954 lockdep_assert_held(&client_mutex);
2955 dai = soc_add_dai(component, dai_drv, false);
2956 if (!dai)
2957 return -ENOMEM;
2958
2959 /* Create the DAI widgets here. After adding DAIs, topology may
2960 * also add routes that need these widgets as source or sink.
2961 */
2962 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2963 if (ret != 0) {
2964 dev_err(component->dev,
2965 "Failed to create DAI widgets %d\n", ret);
2966 }
2967
2968 return ret;
2969}
2970EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2971
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002972static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2973 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002974{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002975 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002976
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002977 component->driver->seq_notifier(component, type, subseq);
2978}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002979
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002980static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2981 int event)
2982{
2983 struct snd_soc_component *component = dapm->component;
2984
2985 return component->driver->stream_event(component, event);
2986}
2987
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002988static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2989 enum snd_soc_bias_level level)
2990{
2991 struct snd_soc_component *component = dapm->component;
2992
2993 return component->driver->set_bias_level(component, level);
2994}
2995
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002996static int snd_soc_component_initialize(struct snd_soc_component *component,
2997 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002998{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002999 struct snd_soc_dapm_context *dapm;
3000
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003001 component->name = fmt_single_name(dev, &component->id);
3002 if (!component->name) {
3003 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003004 return -ENOMEM;
3005 }
3006
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003007 component->dev = dev;
3008 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003009
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003010 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003011 dapm->dev = dev;
3012 dapm->component = component;
3013 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003014 dapm->idle_bias_off = !driver->idle_bias_on;
3015 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003016 if (driver->seq_notifier)
3017 dapm->seq_notifier = snd_soc_component_seq_notifier;
3018 if (driver->stream_event)
3019 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003020 if (driver->set_bias_level)
3021 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003022
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003023 INIT_LIST_HEAD(&component->dai_list);
3024 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003025
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003026 return 0;
3027}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003028
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003029static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003030{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003031 int val_bytes = regmap_get_val_bytes(component->regmap);
3032
3033 /* Errors are legitimate for non-integer byte multiples */
3034 if (val_bytes > 0)
3035 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003036}
3037
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003038#ifdef CONFIG_REGMAP
3039
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003040/**
3041 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3042 * @component: The component for which to initialize the regmap instance
3043 * @regmap: The regmap instance that should be used by the component
3044 *
3045 * This function allows deferred assignment of the regmap instance that is
3046 * associated with the component. Only use this if the regmap instance is not
3047 * yet ready when the component is registered. The function must also be called
3048 * before the first IO attempt of the component.
3049 */
3050void snd_soc_component_init_regmap(struct snd_soc_component *component,
3051 struct regmap *regmap)
3052{
3053 component->regmap = regmap;
3054 snd_soc_component_setup_regmap(component);
3055}
3056EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3057
3058/**
3059 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3060 * @component: The component for which to de-initialize the regmap instance
3061 *
3062 * Calls regmap_exit() on the regmap instance associated to the component and
3063 * removes the regmap instance from the component.
3064 *
3065 * This function should only be used if snd_soc_component_init_regmap() was used
3066 * to initialize the regmap instance.
3067 */
3068void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3069{
3070 regmap_exit(component->regmap);
3071 component->regmap = NULL;
3072}
3073EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3074
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003075#endif
3076
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003077static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003078{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003079 mutex_lock(&client_mutex);
3080
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003081 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003082 if (!component->regmap)
3083 component->regmap = dev_get_regmap(component->dev, NULL);
3084 if (component->regmap)
3085 snd_soc_component_setup_regmap(component);
3086 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003087
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003088 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003089 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003090
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003091 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003092}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003093
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003094static void snd_soc_component_cleanup(struct snd_soc_component *component)
3095{
3096 snd_soc_unregister_dais(component);
3097 kfree(component->name);
3098}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003099
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003100static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3101{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003102 struct snd_soc_card *card = component->card;
3103
3104 if (card)
3105 snd_soc_unregister_card(card);
3106
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003107 list_del(&component->list);
3108}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003109
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003110#define ENDIANNESS_MAP(name) \
3111 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3112static u64 endianness_format_map[] = {
3113 ENDIANNESS_MAP(S16_),
3114 ENDIANNESS_MAP(U16_),
3115 ENDIANNESS_MAP(S24_),
3116 ENDIANNESS_MAP(U24_),
3117 ENDIANNESS_MAP(S32_),
3118 ENDIANNESS_MAP(U32_),
3119 ENDIANNESS_MAP(S24_3),
3120 ENDIANNESS_MAP(U24_3),
3121 ENDIANNESS_MAP(S20_3),
3122 ENDIANNESS_MAP(U20_3),
3123 ENDIANNESS_MAP(S18_3),
3124 ENDIANNESS_MAP(U18_3),
3125 ENDIANNESS_MAP(FLOAT_),
3126 ENDIANNESS_MAP(FLOAT64_),
3127 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3128};
3129
3130/*
3131 * Fix up the DAI formats for endianness: codecs don't actually see
3132 * the endianness of the data but we're using the CPU format
3133 * definitions which do need to include endianness so we ensure that
3134 * codec DAIs always have both big and little endian variants set.
3135 */
3136static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3137{
3138 int i;
3139
3140 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3141 if (stream->formats & endianness_format_map[i])
3142 stream->formats |= endianness_format_map[i];
3143}
3144
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003145int snd_soc_add_component(struct device *dev,
3146 struct snd_soc_component *component,
3147 const struct snd_soc_component_driver *component_driver,
3148 struct snd_soc_dai_driver *dai_drv,
3149 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003150{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003151 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003152 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003153
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003154 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003155 if (ret)
3156 goto err_free;
3157
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003158 if (component_driver->endianness) {
3159 for (i = 0; i < num_dai; i++) {
3160 convert_endianness_formats(&dai_drv[i].playback);
3161 convert_endianness_formats(&dai_drv[i].capture);
3162 }
3163 }
3164
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003165 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003166 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003167 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003168 goto err_cleanup;
3169 }
3170
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003171 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003172
3173 return 0;
3174
3175err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003176 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003177err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003178 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003179}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003180EXPORT_SYMBOL_GPL(snd_soc_add_component);
3181
3182int snd_soc_register_component(struct device *dev,
3183 const struct snd_soc_component_driver *component_driver,
3184 struct snd_soc_dai_driver *dai_drv,
3185 int num_dai)
3186{
3187 struct snd_soc_component *component;
3188
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003189 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003190 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003191 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003192
3193 return snd_soc_add_component(dev, component, component_driver,
3194 dai_drv, num_dai);
3195}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003196EXPORT_SYMBOL_GPL(snd_soc_register_component);
3197
3198/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003199 * snd_soc_unregister_component - Unregister all related component
3200 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003201 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003202 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003203 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003204static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003205{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003206 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003207 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003208
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003209 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003210 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003211 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003212 continue;
3213
3214 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3215 snd_soc_component_del_unlocked(component);
3216 found = 1;
3217 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003218 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003219 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003220
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003221 if (found) {
3222 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003223 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003224
3225 return found;
3226}
3227
3228void snd_soc_unregister_component(struct device *dev)
3229{
3230 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003231}
3232EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3233
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003234struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3235 const char *driver_name)
3236{
3237 struct snd_soc_component *component;
3238 struct snd_soc_component *ret;
3239
3240 ret = NULL;
3241 mutex_lock(&client_mutex);
3242 list_for_each_entry(component, &component_list, list) {
3243 if (dev != component->dev)
3244 continue;
3245
3246 if (driver_name &&
3247 (driver_name != component->driver->name) &&
3248 (strcmp(component->driver->name, driver_name) != 0))
3249 continue;
3250
3251 ret = component;
3252 break;
3253 }
3254 mutex_unlock(&client_mutex);
3255
3256 return ret;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3259
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003260/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003261int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3262 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003263{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003264 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003265 int ret;
3266
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303267 if (!card->dev) {
3268 pr_err("card->dev is not set before calling %s\n", __func__);
3269 return -EINVAL;
3270 }
3271
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003272 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303273
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003274 ret = of_property_read_string_index(np, propname, 0, &card->name);
3275 /*
3276 * EINVAL means the property does not exist. This is fine providing
3277 * card->name was previously set, which is checked later in
3278 * snd_soc_register_card.
3279 */
3280 if (ret < 0 && ret != -EINVAL) {
3281 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003282 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003283 propname, ret);
3284 return ret;
3285 }
3286
3287 return 0;
3288}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003289EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003290
Xiubo Li9a6d4862014-02-08 15:59:52 +08003291static const struct snd_soc_dapm_widget simple_widgets[] = {
3292 SND_SOC_DAPM_MIC("Microphone", NULL),
3293 SND_SOC_DAPM_LINE("Line", NULL),
3294 SND_SOC_DAPM_HP("Headphone", NULL),
3295 SND_SOC_DAPM_SPK("Speaker", NULL),
3296};
3297
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003298int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003299 const char *propname)
3300{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003301 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003302 struct snd_soc_dapm_widget *widgets;
3303 const char *template, *wname;
3304 int i, j, num_widgets, ret;
3305
3306 num_widgets = of_property_count_strings(np, propname);
3307 if (num_widgets < 0) {
3308 dev_err(card->dev,
3309 "ASoC: Property '%s' does not exist\n", propname);
3310 return -EINVAL;
3311 }
3312 if (num_widgets & 1) {
3313 dev_err(card->dev,
3314 "ASoC: Property '%s' length is not even\n", propname);
3315 return -EINVAL;
3316 }
3317
3318 num_widgets /= 2;
3319 if (!num_widgets) {
3320 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3321 propname);
3322 return -EINVAL;
3323 }
3324
3325 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3326 GFP_KERNEL);
3327 if (!widgets) {
3328 dev_err(card->dev,
3329 "ASoC: Could not allocate memory for widgets\n");
3330 return -ENOMEM;
3331 }
3332
3333 for (i = 0; i < num_widgets; i++) {
3334 ret = of_property_read_string_index(np, propname,
3335 2 * i, &template);
3336 if (ret) {
3337 dev_err(card->dev,
3338 "ASoC: Property '%s' index %d read error:%d\n",
3339 propname, 2 * i, ret);
3340 return -EINVAL;
3341 }
3342
3343 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3344 if (!strncmp(template, simple_widgets[j].name,
3345 strlen(simple_widgets[j].name))) {
3346 widgets[i] = simple_widgets[j];
3347 break;
3348 }
3349 }
3350
3351 if (j >= ARRAY_SIZE(simple_widgets)) {
3352 dev_err(card->dev,
3353 "ASoC: DAPM widget '%s' is not supported\n",
3354 template);
3355 return -EINVAL;
3356 }
3357
3358 ret = of_property_read_string_index(np, propname,
3359 (2 * i) + 1,
3360 &wname);
3361 if (ret) {
3362 dev_err(card->dev,
3363 "ASoC: Property '%s' index %d read error:%d\n",
3364 propname, (2 * i) + 1, ret);
3365 return -EINVAL;
3366 }
3367
3368 widgets[i].name = wname;
3369 }
3370
Nicolin Chenf23e8602015-02-14 17:22:49 -08003371 card->of_dapm_widgets = widgets;
3372 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003373
3374 return 0;
3375}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003376EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003377
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003378int snd_soc_of_get_slot_mask(struct device_node *np,
3379 const char *prop_name,
3380 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003381{
3382 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003383 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003384 int i;
3385
3386 if (!of_slot_mask)
3387 return 0;
3388 val /= sizeof(u32);
3389 for (i = 0; i < val; i++)
3390 if (be32_to_cpup(&of_slot_mask[i]))
3391 *mask |= (1 << i);
3392
3393 return val;
3394}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003395EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003396
Xiubo Li89c67852014-02-14 09:34:35 +08003397int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003398 unsigned int *tx_mask,
3399 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003400 unsigned int *slots,
3401 unsigned int *slot_width)
3402{
3403 u32 val;
3404 int ret;
3405
Jyri Sarha61310842015-09-09 21:27:43 +03003406 if (tx_mask)
3407 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3408 if (rx_mask)
3409 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3410
Xiubo Li89c67852014-02-14 09:34:35 +08003411 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3412 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3413 if (ret)
3414 return ret;
3415
3416 if (slots)
3417 *slots = val;
3418 }
3419
3420 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3421 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3422 if (ret)
3423 return ret;
3424
3425 if (slot_width)
3426 *slot_width = val;
3427 }
3428
3429 return 0;
3430}
3431EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3432
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003433void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003434 struct snd_soc_codec_conf *codec_conf,
3435 struct device_node *of_node,
3436 const char *propname)
3437{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003438 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003439 const char *str;
3440 int ret;
3441
3442 ret = of_property_read_string(np, propname, &str);
3443 if (ret < 0) {
3444 /* no prefix is not error */
3445 return;
3446 }
3447
3448 codec_conf->of_node = of_node;
3449 codec_conf->name_prefix = str;
3450}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003451EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003452
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003453int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003454 const char *propname)
3455{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003456 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003457 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003458 struct snd_soc_dapm_route *routes;
3459 int i, ret;
3460
3461 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003462 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003463 dev_err(card->dev,
3464 "ASoC: Property '%s' does not exist or its length is not even\n",
3465 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003466 return -EINVAL;
3467 }
3468 num_routes /= 2;
3469 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003470 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003471 propname);
3472 return -EINVAL;
3473 }
3474
Kees Cooka86854d2018-06-12 14:07:58 -07003475 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003476 GFP_KERNEL);
3477 if (!routes) {
3478 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003479 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003480 return -EINVAL;
3481 }
3482
3483 for (i = 0; i < num_routes; i++) {
3484 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003485 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003486 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003487 dev_err(card->dev,
3488 "ASoC: Property '%s' index %d could not be read: %d\n",
3489 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003490 return -EINVAL;
3491 }
3492 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003493 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003494 if (ret) {
3495 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003496 "ASoC: Property '%s' index %d could not be read: %d\n",
3497 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003498 return -EINVAL;
3499 }
3500 }
3501
Nicolin Chenf23e8602015-02-14 17:22:49 -08003502 card->num_of_dapm_routes = num_routes;
3503 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003504
3505 return 0;
3506}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003507EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003508
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003509unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003510 const char *prefix,
3511 struct device_node **bitclkmaster,
3512 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003513{
3514 int ret, i;
3515 char prop[128];
3516 unsigned int format = 0;
3517 int bit, frame;
3518 const char *str;
3519 struct {
3520 char *name;
3521 unsigned int val;
3522 } of_fmt_table[] = {
3523 { "i2s", SND_SOC_DAIFMT_I2S },
3524 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3525 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3526 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3527 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3528 { "ac97", SND_SOC_DAIFMT_AC97 },
3529 { "pdm", SND_SOC_DAIFMT_PDM},
3530 { "msb", SND_SOC_DAIFMT_MSB },
3531 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003532 };
3533
3534 if (!prefix)
3535 prefix = "";
3536
3537 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003538 * check "dai-format = xxx"
3539 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003540 * SND_SOC_DAIFMT_FORMAT_MASK area
3541 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003542 ret = of_property_read_string(np, "dai-format", &str);
3543 if (ret < 0) {
3544 snprintf(prop, sizeof(prop), "%sformat", prefix);
3545 ret = of_property_read_string(np, prop, &str);
3546 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003547 if (ret == 0) {
3548 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3549 if (strcmp(str, of_fmt_table[i].name) == 0) {
3550 format |= of_fmt_table[i].val;
3551 break;
3552 }
3553 }
3554 }
3555
3556 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003557 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003558 * SND_SOC_DAIFMT_CLOCK_MASK area
3559 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003560 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003561 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003562 format |= SND_SOC_DAIFMT_CONT;
3563 else
3564 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003565
3566 /*
3567 * check "[prefix]bitclock-inversion"
3568 * check "[prefix]frame-inversion"
3569 * SND_SOC_DAIFMT_INV_MASK area
3570 */
3571 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3572 bit = !!of_get_property(np, prop, NULL);
3573
3574 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3575 frame = !!of_get_property(np, prop, NULL);
3576
3577 switch ((bit << 4) + frame) {
3578 case 0x11:
3579 format |= SND_SOC_DAIFMT_IB_IF;
3580 break;
3581 case 0x10:
3582 format |= SND_SOC_DAIFMT_IB_NF;
3583 break;
3584 case 0x01:
3585 format |= SND_SOC_DAIFMT_NB_IF;
3586 break;
3587 default:
3588 /* SND_SOC_DAIFMT_NB_NF is default */
3589 break;
3590 }
3591
3592 /*
3593 * check "[prefix]bitclock-master"
3594 * check "[prefix]frame-master"
3595 * SND_SOC_DAIFMT_MASTER_MASK area
3596 */
3597 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3598 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003599 if (bit && bitclkmaster)
3600 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003601
3602 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3603 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003604 if (frame && framemaster)
3605 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003606
3607 switch ((bit << 4) + frame) {
3608 case 0x11:
3609 format |= SND_SOC_DAIFMT_CBM_CFM;
3610 break;
3611 case 0x10:
3612 format |= SND_SOC_DAIFMT_CBM_CFS;
3613 break;
3614 case 0x01:
3615 format |= SND_SOC_DAIFMT_CBS_CFM;
3616 break;
3617 default:
3618 format |= SND_SOC_DAIFMT_CBS_CFS;
3619 break;
3620 }
3621
3622 return format;
3623}
3624EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3625
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003626int snd_soc_get_dai_id(struct device_node *ep)
3627{
3628 struct snd_soc_component *pos;
3629 struct device_node *node;
3630 int ret;
3631
3632 node = of_graph_get_port_parent(ep);
3633
3634 /*
3635 * For example HDMI case, HDMI has video/sound port,
3636 * but ALSA SoC needs sound port number only.
3637 * Thus counting HDMI DT port/endpoint doesn't work.
3638 * Then, it should have .of_xlate_dai_id
3639 */
3640 ret = -ENOTSUPP;
3641 mutex_lock(&client_mutex);
3642 list_for_each_entry(pos, &component_list, list) {
3643 struct device_node *component_of_node = pos->dev->of_node;
3644
3645 if (!component_of_node && pos->dev->parent)
3646 component_of_node = pos->dev->parent->of_node;
3647
3648 if (component_of_node != node)
3649 continue;
3650
3651 if (pos->driver->of_xlate_dai_id)
3652 ret = pos->driver->of_xlate_dai_id(pos, ep);
3653
3654 break;
3655 }
3656 mutex_unlock(&client_mutex);
3657
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003658 of_node_put(node);
3659
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003660 return ret;
3661}
3662EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3663
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003664int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003665 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003666{
3667 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003668 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003669 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003670
3671 mutex_lock(&client_mutex);
3672 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003673 component_of_node = pos->dev->of_node;
3674 if (!component_of_node && pos->dev->parent)
3675 component_of_node = pos->dev->parent->of_node;
3676
3677 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003678 continue;
3679
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003680 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003681 ret = pos->driver->of_xlate_dai_name(pos,
3682 args,
3683 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003684 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003685 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003686 int id = -1;
3687
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003688 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003689 case 0:
3690 id = 0; /* same as dai_drv[0] */
3691 break;
3692 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003693 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003694 break;
3695 default:
3696 /* not supported */
3697 break;
3698 }
3699
3700 if (id < 0 || id >= pos->num_dai) {
3701 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003702 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003703 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003704
3705 ret = 0;
3706
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003707 /* find target DAI */
3708 list_for_each_entry(dai, &pos->dai_list, list) {
3709 if (id == 0)
3710 break;
3711 id--;
3712 }
3713
3714 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003715 if (!*dai_name)
3716 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003717 }
3718
Kuninori Morimotocb470082013-09-10 17:39:56 -07003719 break;
3720 }
3721 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003722 return ret;
3723}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003724EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003725
3726int snd_soc_of_get_dai_name(struct device_node *of_node,
3727 const char **dai_name)
3728{
3729 struct of_phandle_args args;
3730 int ret;
3731
3732 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3733 "#sound-dai-cells", 0, &args);
3734 if (ret)
3735 return ret;
3736
3737 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003738
3739 of_node_put(args.np);
3740
3741 return ret;
3742}
3743EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3744
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003745/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003746 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3747 * @dai_link: DAI link
3748 *
3749 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3750 */
3751void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3752{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003753 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003754 int index;
3755
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003756 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003757 if (!component->of_node)
3758 break;
3759 of_node_put(component->of_node);
3760 component->of_node = NULL;
3761 }
3762}
3763EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3764
3765/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003766 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3767 * @dev: Card device
3768 * @of_node: Device node
3769 * @dai_link: DAI link
3770 *
3771 * Builds an array of CODEC DAI components from the DAI link property
3772 * 'sound-dai'.
3773 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003774 * The device nodes in the array (of_node) must be dereferenced by calling
3775 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003776 *
3777 * Returns 0 for success
3778 */
3779int snd_soc_of_get_dai_link_codecs(struct device *dev,
3780 struct device_node *of_node,
3781 struct snd_soc_dai_link *dai_link)
3782{
3783 struct of_phandle_args args;
3784 struct snd_soc_dai_link_component *component;
3785 char *name;
3786 int index, num_codecs, ret;
3787
3788 /* Count the number of CODECs */
3789 name = "sound-dai";
3790 num_codecs = of_count_phandle_with_args(of_node, name,
3791 "#sound-dai-cells");
3792 if (num_codecs <= 0) {
3793 if (num_codecs == -ENOENT)
3794 dev_err(dev, "No 'sound-dai' property\n");
3795 else
3796 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3797 return num_codecs;
3798 }
Kees Cooka86854d2018-06-12 14:07:58 -07003799 component = devm_kcalloc(dev,
3800 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003801 GFP_KERNEL);
3802 if (!component)
3803 return -ENOMEM;
3804 dai_link->codecs = component;
3805 dai_link->num_codecs = num_codecs;
3806
3807 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003808 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003809 ret = of_parse_phandle_with_args(of_node, name,
3810 "#sound-dai-cells",
3811 index, &args);
3812 if (ret)
3813 goto err;
3814 component->of_node = args.np;
3815 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3816 if (ret < 0)
3817 goto err;
3818 }
3819 return 0;
3820err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003821 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003822 dai_link->codecs = NULL;
3823 dai_link->num_codecs = 0;
3824 return ret;
3825}
3826EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3827
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003828static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003829{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003830 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003831 snd_soc_util_init();
3832
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003833 return platform_driver_register(&soc_driver);
3834}
Mark Brown4abe8e12010-10-12 17:41:03 +01003835module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003836
Mark Brown7d8c16a2008-11-30 22:11:24 +00003837static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003838{
Mark Brownfb257892011-04-28 10:57:54 +01003839 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003840 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003841
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003842 platform_driver_unregister(&soc_driver);
3843}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003844module_exit(snd_soc_exit);
3845
3846/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003847MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003848MODULE_DESCRIPTION("ALSA SoC Core");
3849MODULE_LICENSE("GPL");
3850MODULE_ALIAS("platform:soc-audio");