blob: eeab492e954fd8022efabcc209accdc9906d5cbb [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) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100455
Mengdong Lin1a497982015-11-18 02:34:11 -0500456 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100457 continue;
458
Mengdong Lin1a497982015-11-18 02:34:11 -0500459 for (i = 0; i < rtd->num_codecs; i++) {
460 struct snd_soc_dai *dai = rtd->codec_dais[i];
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) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100628
Mengdong Lin1a497982015-11-18 02:34:11 -0500629 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100630 continue;
631
Mengdong Lin1a497982015-11-18 02:34:11 -0500632 for (i = 0; i < rtd->num_codecs; i++) {
633 struct snd_soc_dai *dai = rtd->codec_dais[i];
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) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200677 struct snd_soc_dai **codec_dais = rtd->codec_dais;
678 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
684 for (j = 0; j < rtd->num_codecs; j++) {
685 struct snd_soc_dai *codec_dai = codec_dais[j];
686 if (codec_dai->active)
687 pinctrl_pm_select_default_state(codec_dai->dev);
688 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800689 }
690
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100691 /*
692 * DAIs that also act as the control bus master might have other drivers
693 * hanging off them so need to resume immediately. Other drivers don't
694 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100695 * due to I/O costs and anti-pop so handle them out of line.
696 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500697 list_for_each_entry(rtd, &card->rtd_list, list) {
698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100699 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600700 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100701 if (bus_control) {
702 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600703 soc_resume_deferred(&card->deferred_resume_work);
704 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000705 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600706 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000707 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100708 }
Andy Green6ed25972008-06-13 16:24:05 +0100709
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 return 0;
711}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000712EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000714#define snd_soc_suspend NULL
715#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716#endif
717
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100718static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800719};
720
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200721static struct snd_soc_component *soc_find_component(
722 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100723{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200724 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100725
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100726 lockdep_assert_held(&client_mutex);
727
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200728 list_for_each_entry(component, &component_list, list) {
729 if (of_node) {
730 if (component->dev->of_node == of_node)
731 return component;
732 } else if (strcmp(component->name, name) == 0) {
733 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100734 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100735 }
736
737 return NULL;
738}
739
Mengdong Linfbb88b52016-04-22 12:25:33 +0800740/**
741 * snd_soc_find_dai - Find a registered DAI
742 *
Jeffy Chen49584712017-08-22 15:57:21 +0800743 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800744 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700745 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800746 * find the DAI of the same name. The component's of_node and name
747 * should also match if being specified.
748 *
749 * Return: pointer of DAI, or NULL if not found.
750 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800751struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200752 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100753{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200754 struct snd_soc_component *component;
755 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300756 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100757
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100758 lockdep_assert_held(&client_mutex);
759
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200760 /* Find CPU DAI from registered DAIs*/
761 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300762 component_of_node = component->dev->of_node;
763 if (!component_of_node && component->dev->parent)
764 component_of_node = component->dev->parent->of_node;
765
766 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200767 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100768 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200769 continue;
770 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800771 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800772 && (!dai->driver->name
773 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100774 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100775
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200776 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100777 }
778 }
779
780 return NULL;
781}
Mengdong Lin305e9022016-04-19 13:12:25 +0800782EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100783
Mengdong Lin17fb17552016-11-03 01:04:12 +0800784
785/**
786 * snd_soc_find_dai_link - Find a DAI link
787 *
788 * @card: soc card
789 * @id: DAI link ID to match
790 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000791 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800792 *
793 * This function will search all existing DAI links of the soc card to
794 * find the link of the same ID. Since DAI links may not have their
795 * unique ID, so name and stream name should also match if being
796 * specified.
797 *
798 * Return: pointer of DAI link, or NULL if not found.
799 */
800struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
801 int id, const char *name,
802 const char *stream_name)
803{
804 struct snd_soc_dai_link *link, *_link;
805
806 lockdep_assert_held(&client_mutex);
807
808 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
809 if (link->id != id)
810 continue;
811
812 if (name && (!link->name || strcmp(name, link->name)))
813 continue;
814
815 if (stream_name && (!link->stream_name
816 || strcmp(stream_name, link->stream_name)))
817 continue;
818
819 return link;
820 }
821
822 return NULL;
823}
824EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
825
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800826static bool soc_is_dai_link_bound(struct snd_soc_card *card,
827 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800829 struct snd_soc_pcm_runtime *rtd;
830
831 list_for_each_entry(rtd, &card->rtd_list, list) {
832 if (rtd->dai_link == dai_link)
833 return true;
834 }
835
836 return false;
837}
838
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500839static int soc_bind_dai_link(struct snd_soc_card *card,
840 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841{
Mengdong Lin1a497982015-11-18 02:34:11 -0500842 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200843 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200844 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000845 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500846 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +0000847 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +0100848 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200849 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200850
Liam Girdwooda655de82018-07-02 16:59:54 +0100851 if (dai_link->ignore)
852 return 0;
853
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500854 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000855
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800856 if (soc_is_dai_link_bound(card, dai_link)) {
857 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
858 dai_link->name);
859 return 0;
860 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530862 rtd = soc_new_pcm_runtime(card, dai_link);
863 if (!rtd)
864 return -ENOMEM;
865
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200866 cpu_dai_component.name = dai_link->cpu_name;
867 cpu_dai_component.of_node = dai_link->cpu_of_node;
868 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
869 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000870 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100871 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
872 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500873 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000874 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000875 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000876
Benoit Cousson88bd8702014-07-08 23:19:34 +0200877 rtd->num_codecs = dai_link->num_codecs;
878
879 /* Find CODEC from registered CODECs */
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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962
963 /* unregister the rtd device */
964 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800965 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 rtd->dev_registered = 0;
967 }
968
969 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200970 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000972
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200973 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974}
975
Mengdong Lin1a497982015-11-18 02:34:11 -0500976static void soc_remove_link_components(struct snd_soc_card *card,
977 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600978{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200979 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000980 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600981
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000982 for_each_rtdcom(rtd, rtdcom) {
983 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600984
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200985 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200986 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600987 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600988}
989
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900990static void soc_remove_dai_links(struct snd_soc_card *card)
991{
Mengdong Lin1a497982015-11-18 02:34:11 -0500992 int order;
993 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800994 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900995
Liam Girdwood0168bf02011-06-07 16:08:05 +0100996 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
997 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500998 list_for_each_entry(rtd, &card->rtd_list, list)
999 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001000 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001
1002 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1003 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001004 list_for_each_entry(rtd, &card->rtd_list, list)
1005 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001006 }
1007
Mengdong Linf8f80362015-12-02 14:11:22 +08001008 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1009 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1010 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1011 link->name);
1012
1013 list_del(&link->list);
1014 card->num_dai_links--;
1015 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001016}
1017
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001018static int snd_soc_init_platform(struct snd_soc_card *card,
1019 struct snd_soc_dai_link *dai_link)
1020{
1021 /*
1022 * FIXME
1023 *
1024 * this function should be removed in the future
1025 */
1026 /* convert Legacy platform link */
1027 if (dai_link->platform)
1028 return 0;
1029
1030 dai_link->platform = devm_kzalloc(card->dev,
1031 sizeof(struct snd_soc_dai_link_component),
1032 GFP_KERNEL);
1033 if (!dai_link->platform)
1034 return -ENOMEM;
1035
1036 dai_link->platform->name = dai_link->platform_name;
1037 dai_link->platform->of_node = dai_link->platform_of_node;
1038 dai_link->platform->dai_name = NULL;
1039
1040 return 0;
1041}
1042
Mengdong Lin923c5e612015-11-23 11:01:49 -05001043static int snd_soc_init_multicodec(struct snd_soc_card *card,
1044 struct snd_soc_dai_link *dai_link)
1045{
1046 /* Legacy codec/codec_dai link is a single entry in multicodec */
1047 if (dai_link->codec_name || dai_link->codec_of_node ||
1048 dai_link->codec_dai_name) {
1049 dai_link->num_codecs = 1;
1050
1051 dai_link->codecs = devm_kzalloc(card->dev,
1052 sizeof(struct snd_soc_dai_link_component),
1053 GFP_KERNEL);
1054 if (!dai_link->codecs)
1055 return -ENOMEM;
1056
1057 dai_link->codecs[0].name = dai_link->codec_name;
1058 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1059 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1060 }
1061
1062 if (!dai_link->codecs) {
1063 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1064 return -EINVAL;
1065 }
1066
1067 return 0;
1068}
1069
1070static int soc_init_dai_link(struct snd_soc_card *card,
1071 struct snd_soc_dai_link *link)
1072{
1073 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001074 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001075
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001076 ret = snd_soc_init_platform(card, link);
1077 if (ret) {
1078 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1079 return ret;
1080 }
1081
Mengdong Lin923c5e612015-11-23 11:01:49 -05001082 ret = snd_soc_init_multicodec(card, link);
1083 if (ret) {
1084 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1085 return ret;
1086 }
1087
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001088 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001089 /*
1090 * Codec must be specified by 1 of name or OF node,
1091 * not both or neither.
1092 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001093 if (!!codec->name ==
1094 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001095 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1096 link->name);
1097 return -EINVAL;
1098 }
1099 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001100 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001101 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1102 link->name);
1103 return -EINVAL;
1104 }
1105 }
1106
1107 /*
1108 * Platform may be specified by either name or OF node, but
1109 * can be left unspecified, and a dummy platform will be used.
1110 */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001111 if (link->platform->name && link->platform->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001112 dev_err(card->dev,
1113 "ASoC: Both platform name/of_node are set for %s\n",
1114 link->name);
1115 return -EINVAL;
1116 }
Mengdong Lin923c5e612015-11-23 11:01:49 -05001117 /*
1118 * CPU device may be specified by either name or OF node, but
1119 * can be left unspecified, and will be matched based on DAI
1120 * name alone..
1121 */
1122 if (link->cpu_name && link->cpu_of_node) {
1123 dev_err(card->dev,
1124 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1125 link->name);
1126 return -EINVAL;
1127 }
1128 /*
1129 * At least one of CPU DAI name or CPU device name/node must be
1130 * specified
1131 */
1132 if (!link->cpu_dai_name &&
1133 !(link->cpu_name || link->cpu_of_node)) {
1134 dev_err(card->dev,
1135 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1136 link->name);
1137 return -EINVAL;
1138 }
1139
1140 return 0;
1141}
1142
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001143void snd_soc_disconnect_sync(struct device *dev)
1144{
1145 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1146
1147 if (!component || !component->card)
1148 return;
1149
1150 snd_card_disconnect_sync(component->card->snd_card);
1151}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001152EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001153
Mengdong Linf8f80362015-12-02 14:11:22 +08001154/**
1155 * snd_soc_add_dai_link - Add a DAI link dynamically
1156 * @card: The ASoC card to which the DAI link is added
1157 * @dai_link: The new DAI link to add
1158 *
1159 * This function adds a DAI link to the ASoC card's link list.
1160 *
1161 * Note: Topology can use this API to add DAI links when probing the
1162 * topology component. And machine drivers can still define static
1163 * DAI links in dai_link array.
1164 */
1165int snd_soc_add_dai_link(struct snd_soc_card *card,
1166 struct snd_soc_dai_link *dai_link)
1167{
1168 if (dai_link->dobj.type
1169 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1170 dev_err(card->dev, "Invalid dai link type %d\n",
1171 dai_link->dobj.type);
1172 return -EINVAL;
1173 }
1174
1175 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001176 /* Notify the machine driver for extra initialization
1177 * on the link created by topology.
1178 */
1179 if (dai_link->dobj.type && card->add_dai_link)
1180 card->add_dai_link(card, dai_link);
1181
Mengdong Linf8f80362015-12-02 14:11:22 +08001182 list_add_tail(&dai_link->list, &card->dai_link_list);
1183 card->num_dai_links++;
1184
1185 return 0;
1186}
1187EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1188
1189/**
1190 * snd_soc_remove_dai_link - Remove a DAI link from the list
1191 * @card: The ASoC card that owns the link
1192 * @dai_link: The DAI link to remove
1193 *
1194 * This function removes a DAI link from the ASoC card's link list.
1195 *
1196 * For DAI links previously added by topology, topology should
1197 * remove them by using the dobj embedded in the link.
1198 */
1199void snd_soc_remove_dai_link(struct snd_soc_card *card,
1200 struct snd_soc_dai_link *dai_link)
1201{
1202 struct snd_soc_dai_link *link, *_link;
1203
1204 if (dai_link->dobj.type
1205 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1206 dev_err(card->dev, "Invalid dai link type %d\n",
1207 dai_link->dobj.type);
1208 return;
1209 }
1210
1211 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001212 /* Notify the machine driver for extra destruction
1213 * on the link created by topology.
1214 */
1215 if (dai_link->dobj.type && card->remove_dai_link)
1216 card->remove_dai_link(card, dai_link);
1217
Mengdong Linf8f80362015-12-02 14:11:22 +08001218 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1219 if (link == dai_link) {
1220 list_del(&link->list);
1221 card->num_dai_links--;
1222 return;
1223 }
1224 }
1225}
1226EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1227
Jerome Brunetaefba452018-07-13 14:50:43 +02001228static void soc_set_of_name_prefix(struct snd_soc_component *component)
1229{
1230 struct device_node *component_of_node = component->dev->of_node;
1231 const char *str;
1232 int ret;
1233
1234 if (!component_of_node && component->dev->parent)
1235 component_of_node = component->dev->parent->of_node;
1236
1237 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1238 &str);
1239 if (!ret)
1240 component->name_prefix = str;
1241}
1242
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001243static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001244 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001245{
1246 int i;
1247
Jerome Brunetaefba452018-07-13 14:50:43 +02001248 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001249 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001250 struct device_node *component_of_node = component->dev->of_node;
1251
1252 if (!component_of_node && component->dev->parent)
1253 component_of_node = component->dev->parent->of_node;
1254
1255 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001256 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001257 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001258 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001259 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001260 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001261 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001262
1263 /*
1264 * If there is no configuration table or no match in the table,
1265 * check if a prefix is provided in the node
1266 */
1267 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001268}
1269
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001270static int soc_probe_component(struct snd_soc_card *card,
1271 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001273 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001274 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001275 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001277 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001278 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001280 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001281 if (component->card != card) {
1282 dev_err(component->dev,
1283 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1284 card->name, component->card->name);
1285 return -ENODEV;
1286 }
1287 return 0;
1288 }
1289
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001290 if (!try_module_get(component->dev->driver->owner))
1291 return -ENODEV;
1292
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001293 component->card = card;
1294 dapm->card = card;
1295 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001296
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001297 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001298
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001299 if (component->driver->dapm_widgets) {
1300 ret = snd_soc_dapm_new_controls(dapm,
1301 component->driver->dapm_widgets,
1302 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001303
1304 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001305 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001306 "Failed to create new controls %d\n", ret);
1307 goto err_probe;
1308 }
1309 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001310
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001311 list_for_each_entry(dai, &component->dai_list, list) {
1312 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001313 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001314 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001315 "Failed to create DAI widgets %d\n", ret);
1316 goto err_probe;
1317 }
1318 }
Mark Brown888df392012-02-16 19:37:51 -08001319
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001320 if (component->driver->probe) {
1321 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001323 dev_err(component->dev,
1324 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001325 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001326 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001327
1328 WARN(dapm->idle_bias_off &&
1329 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001330 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001331 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001332 }
1333
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001334 /* machine specific init */
1335 if (component->init) {
1336 ret = component->init(component);
1337 if (ret < 0) {
1338 dev_err(component->dev,
1339 "Failed to do machine specific init %d\n", ret);
1340 goto err_probe;
1341 }
1342 }
1343
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001344 if (component->driver->controls)
1345 snd_soc_add_component_controls(component,
1346 component->driver->controls,
1347 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001348 if (component->driver->dapm_routes)
1349 snd_soc_dapm_add_routes(dapm,
1350 component->driver->dapm_routes,
1351 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001352
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001353 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001354 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355
Jarkko Nikula70d293312011-01-27 16:24:22 +02001356 return 0;
1357
1358err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001359 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001360 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001361 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001362
1363 return ret;
1364}
1365
Mark Brown36ae1a92012-01-06 17:12:45 -08001366static void rtd_release(struct device *dev)
1367{
1368 kfree(dev);
1369}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001371static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1372 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001373{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001374 int ret = 0;
1375
Jarkko Nikula589c3562010-12-06 16:27:07 +02001376 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001377 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1378 if (!rtd->dev)
1379 return -ENOMEM;
1380 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001381 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001382 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001383 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001384 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001385 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001386 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001387 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1388 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1389 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1390 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001391 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001392 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001393 /* calling put_device() here to free the rtd->dev */
1394 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001395 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001396 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001397 return ret;
1398 }
1399 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001400 return 0;
1401}
1402
Mengdong Lin1a497982015-11-18 02:34:11 -05001403static int soc_probe_link_components(struct snd_soc_card *card,
1404 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001405 int order)
1406{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001407 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001408 struct snd_soc_rtdcom_list *rtdcom;
1409 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001410
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001411 for_each_rtdcom(rtd, rtdcom) {
1412 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001413
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001414 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001415 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001416 if (ret < 0)
1417 return ret;
1418 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001419 }
1420
Stephen Warren62ae68f2012-06-08 12:34:23 -06001421 return 0;
1422}
1423
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001424static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001425{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001426 if (dai->probed ||
1427 dai->driver->probe_order != order)
1428 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001429
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001430 if (dai->driver->probe) {
1431 int ret = dai->driver->probe(dai);
1432 if (ret < 0) {
1433 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1434 dai->name, ret);
1435 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001436 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001437 }
1438
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001439 dai->probed = 1;
1440
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001441 return 0;
1442}
1443
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001444static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1445 struct snd_soc_pcm_runtime *rtd)
1446{
1447 int i, ret = 0;
1448
1449 for (i = 0; i < num_dais; ++i) {
1450 struct snd_soc_dai_driver *drv = dais[i]->driver;
1451
1452 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1453 ret = drv->pcm_new(rtd, dais[i]);
1454 if (ret < 0) {
1455 dev_err(dais[i]->dev,
1456 "ASoC: Failed to bind %s with pcm device\n",
1457 dais[i]->name);
1458 return ret;
1459 }
1460 }
1461
1462 return 0;
1463}
1464
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001465static int soc_link_dai_widgets(struct snd_soc_card *card,
1466 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001467 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001468{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001469 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1470 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301471 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001472 int ret;
1473
Benoit Cousson88bd8702014-07-08 23:19:34 +02001474 if (rtd->num_codecs > 1)
1475 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1476
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001477 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301478 sink = codec_dai->playback_widget;
1479 source = cpu_dai->capture_widget;
1480 if (sink && source) {
Charles Keepax249dc492018-08-15 13:11:35 +01001481 ret = snd_soc_dapm_new_pcm(card, rtd, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301482 dai_link->num_params,
1483 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001484 if (ret != 0) {
1485 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301486 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001487 return ret;
1488 }
1489 }
1490
Vinod Koul3de7c422015-11-09 23:19:58 +05301491 sink = cpu_dai->playback_widget;
1492 source = codec_dai->capture_widget;
1493 if (sink && source) {
Charles Keepax249dc492018-08-15 13:11:35 +01001494 ret = snd_soc_dapm_new_pcm(card, rtd, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301495 dai_link->num_params,
1496 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001497 if (ret != 0) {
1498 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301499 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001500 return ret;
1501 }
1502 }
1503
1504 return 0;
1505}
1506
Mengdong Lin1a497982015-11-18 02:34:11 -05001507static int soc_probe_link_dais(struct snd_soc_card *card,
1508 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001509{
Mengdong Lin1a497982015-11-18 02:34:11 -05001510 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001511 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001512 struct snd_soc_rtdcom_list *rtdcom;
1513 struct snd_soc_component *component;
1514 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001516 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001517 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 /* set default power off timeout */
1520 rtd->pmdown_time = pmdown_time;
1521
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001522 ret = soc_probe_dai(cpu_dai, order);
1523 if (ret)
1524 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001527 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001528 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001529 if (ret)
1530 return ret;
1531 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001532
Liam Girdwood0168bf02011-06-07 16:08:05 +01001533 /* complete DAI probe during last probe */
1534 if (order != SND_SOC_COMP_ORDER_LAST)
1535 return 0;
1536
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001537 /* do machine specific initialization */
1538 if (dai_link->init) {
1539 ret = dai_link->init(rtd);
1540 if (ret < 0) {
1541 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1542 dai_link->name, ret);
1543 return ret;
1544 }
1545 }
1546
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001547 if (dai_link->dai_fmt)
1548 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1549
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001550 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001551 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001552 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001553
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001554#ifdef CONFIG_DEBUG_FS
1555 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001556 if (dai_link->dynamic)
1557 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001558#endif
1559
Liam Girdwooda655de82018-07-02 16:59:54 +01001560 num = rtd->num;
1561
1562 /*
1563 * most drivers will register their PCMs using DAI link ordering but
1564 * topology based drivers can use the DAI link id field to set PCM
1565 * device number and then use rtd + a base offset of the BEs.
1566 */
1567 for_each_rtdcom(rtd, rtdcom) {
1568 component = rtdcom->component;
1569
1570 if (!component->driver->use_dai_pcm_id)
1571 continue;
1572
1573 if (rtd->dai_link->no_pcm)
1574 num += component->driver->be_pcm_base;
1575 else
1576 num = rtd->dai_link->id;
1577 }
1578
Jie Yang6f0c4222015-10-13 23:41:00 +08001579 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301580 /*create compress_device"*/
Liam Girdwooda655de82018-07-02 16:59:54 +01001581 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001582 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001583 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301584 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001585 return ret;
1586 }
1587 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301588
1589 if (!dai_link->params) {
1590 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001591 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301592 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001593 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301594 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001595 return ret;
1596 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001597 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1598 if (ret < 0)
1599 return ret;
1600 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1601 rtd->num_codecs, rtd);
1602 if (ret < 0)
1603 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301604 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001605 INIT_DELAYED_WORK(&rtd->delayed_work,
1606 codec2codec_close_delayed_work);
1607
Namarta Kohli1245b702012-08-16 17:10:41 +05301608 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001609 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001610 if (ret)
1611 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001612 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001613 }
1614
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615 return 0;
1616}
1617
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001618static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001619{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001620 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001621 struct snd_soc_component *component;
1622 const char *name;
1623 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001624
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001625 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1626 /* codecs, usually analog devices */
1627 name = aux_dev->codec_name;
1628 codec_of_node = aux_dev->codec_of_node;
1629 component = soc_find_component(codec_of_node, name);
1630 if (!component) {
1631 if (codec_of_node)
1632 name = of_node_full_name(codec_of_node);
1633 goto err_defer;
1634 }
1635 } else if (aux_dev->name) {
1636 /* generic components */
1637 name = aux_dev->name;
1638 component = soc_find_component(NULL, name);
1639 if (!component)
1640 goto err_defer;
1641 } else {
1642 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1643 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001644 }
1645
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001646 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001647 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001648
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001649 return 0;
1650
1651err_defer:
1652 dev_err(card->dev, "ASoC: %s not registered\n", name);
1653 return -EPROBE_DEFER;
1654}
1655
1656static int soc_probe_aux_devices(struct snd_soc_card *card)
1657{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001658 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001659 int order;
1660 int ret;
1661
1662 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1663 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001664 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001665 if (comp->driver->probe_order == order) {
1666 ret = soc_probe_component(card, comp);
1667 if (ret < 0) {
1668 dev_err(card->dev,
1669 "ASoC: failed to probe aux component %s %d\n",
1670 comp->name, ret);
1671 return ret;
1672 }
1673 }
1674 }
1675 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001676
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001677 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001678}
1679
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001680static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001681{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001682 struct snd_soc_component *comp, *_comp;
1683 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001684
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001685 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1686 order++) {
1687 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001688 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001689
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001690 if (comp->driver->remove_order == order) {
1691 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001692 /* remove it from the card's aux_comp_list */
1693 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001694 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001695 }
1696 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001697}
1698
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001699/**
1700 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1701 * @rtd: The runtime for which the DAI link format should be changed
1702 * @dai_fmt: The new DAI link format
1703 *
1704 * This function updates the DAI link format for all DAIs connected to the DAI
1705 * link for the specified runtime.
1706 *
1707 * Note: For setups with a static format set the dai_fmt field in the
1708 * corresponding snd_dai_link struct instead of using this function.
1709 *
1710 * Returns 0 on success, otherwise a negative error code.
1711 */
1712int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1713 unsigned int dai_fmt)
1714{
1715 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1716 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1717 unsigned int i;
1718 int ret;
1719
1720 for (i = 0; i < rtd->num_codecs; i++) {
1721 struct snd_soc_dai *codec_dai = codec_dais[i];
1722
1723 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1724 if (ret != 0 && ret != -ENOTSUPP) {
1725 dev_warn(codec_dai->dev,
1726 "ASoC: Failed to set DAI format: %d\n", ret);
1727 return ret;
1728 }
1729 }
1730
1731 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001732 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001733 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001734 unsigned int inv_dai_fmt;
1735
1736 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1737 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1738 case SND_SOC_DAIFMT_CBM_CFM:
1739 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1740 break;
1741 case SND_SOC_DAIFMT_CBM_CFS:
1742 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1743 break;
1744 case SND_SOC_DAIFMT_CBS_CFM:
1745 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1746 break;
1747 case SND_SOC_DAIFMT_CBS_CFS:
1748 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1749 break;
1750 }
1751
1752 dai_fmt = inv_dai_fmt;
1753 }
1754
1755 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1756 if (ret != 0 && ret != -ENOTSUPP) {
1757 dev_warn(cpu_dai->dev,
1758 "ASoC: Failed to set DAI format: %d\n", ret);
1759 return ret;
1760 }
1761
1762 return 0;
1763}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001764EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001765
Liam Girdwood345233d2017-01-14 16:13:02 +08001766
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001767#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001768/* Trim special characters, and replace '-' with '_' since '-' is used to
1769 * separate different DMI fields in the card long name. Only number and
1770 * alphabet characters and a few separator characters are kept.
1771 */
1772static void cleanup_dmi_name(char *name)
1773{
1774 int i, j = 0;
1775
1776 for (i = 0; name[i]; i++) {
1777 if (isalnum(name[i]) || (name[i] == '.')
1778 || (name[i] == '_'))
1779 name[j++] = name[i];
1780 else if (name[i] == '-')
1781 name[j++] = '_';
1782 }
1783
1784 name[j] = '\0';
1785}
1786
Mengdong Lin98faf432017-06-28 15:01:39 +08001787/* Check if a DMI field is valid, i.e. not containing any string
1788 * in the black list.
1789 */
1790static int is_dmi_valid(const char *field)
1791{
1792 int i = 0;
1793
1794 while (dmi_blacklist[i]) {
1795 if (strstr(field, dmi_blacklist[i]))
1796 return 0;
1797 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001798 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001799
1800 return 1;
1801}
1802
Liam Girdwood345233d2017-01-14 16:13:02 +08001803/**
1804 * snd_soc_set_dmi_name() - Register DMI names to card
1805 * @card: The card to register DMI names
1806 * @flavour: The flavour "differentiator" for the card amongst its peers.
1807 *
1808 * An Intel machine driver may be used by many different devices but are
1809 * difficult for userspace to differentiate, since machine drivers ususally
1810 * use their own name as the card short name and leave the card long name
1811 * blank. To differentiate such devices and fix bugs due to lack of
1812 * device-specific configurations, this function allows DMI info to be used
1813 * as the sound card long name, in the format of
1814 * "vendor-product-version-board"
1815 * (Character '-' is used to separate different DMI fields here).
1816 * This will help the user space to load the device-specific Use Case Manager
1817 * (UCM) configurations for the card.
1818 *
1819 * Possible card long names may be:
1820 * DellInc.-XPS139343-01-0310JH
1821 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1822 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1823 *
1824 * This function also supports flavoring the card longname to provide
1825 * the extra differentiation, like "vendor-product-version-board-flavor".
1826 *
1827 * We only keep number and alphabet characters and a few separator characters
1828 * in the card long name since UCM in the user space uses the card long names
1829 * as card configuration directory names and AudoConf cannot support special
1830 * charactors like SPACE.
1831 *
1832 * Returns 0 on success, otherwise a negative error code.
1833 */
1834int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1835{
1836 const char *vendor, *product, *product_version, *board;
1837 size_t longname_buf_size = sizeof(card->snd_card->longname);
1838 size_t len;
1839
1840 if (card->long_name)
1841 return 0; /* long name already set by driver or from DMI */
1842
1843 /* make up dmi long name as: vendor.product.version.board */
1844 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001845 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001846 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1847 return 0;
1848 }
1849
Mengdong Lin98faf432017-06-28 15:01:39 +08001850
Liam Girdwood345233d2017-01-14 16:13:02 +08001851 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1852 "%s", vendor);
1853 cleanup_dmi_name(card->dmi_longname);
1854
1855 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001856 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001857 len = strlen(card->dmi_longname);
1858 snprintf(card->dmi_longname + len,
1859 longname_buf_size - len,
1860 "-%s", product);
1861
1862 len++; /* skip the separator "-" */
1863 if (len < longname_buf_size)
1864 cleanup_dmi_name(card->dmi_longname + len);
1865
1866 /* some vendors like Lenovo may only put a self-explanatory
1867 * name in the product version field
1868 */
1869 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001870 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001871 len = strlen(card->dmi_longname);
1872 snprintf(card->dmi_longname + len,
1873 longname_buf_size - len,
1874 "-%s", product_version);
1875
1876 len++;
1877 if (len < longname_buf_size)
1878 cleanup_dmi_name(card->dmi_longname + len);
1879 }
1880 }
1881
1882 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001883 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001884 len = strlen(card->dmi_longname);
1885 snprintf(card->dmi_longname + len,
1886 longname_buf_size - len,
1887 "-%s", board);
1888
1889 len++;
1890 if (len < longname_buf_size)
1891 cleanup_dmi_name(card->dmi_longname + len);
1892 } else if (!product) {
1893 /* fall back to using legacy name */
1894 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1895 return 0;
1896 }
1897
1898 /* Add flavour to dmi long name */
1899 if (flavour) {
1900 len = strlen(card->dmi_longname);
1901 snprintf(card->dmi_longname + len,
1902 longname_buf_size - len,
1903 "-%s", flavour);
1904
1905 len++;
1906 if (len < longname_buf_size)
1907 cleanup_dmi_name(card->dmi_longname + len);
1908 }
1909
1910 /* set the card long name */
1911 card->long_name = card->dmi_longname;
1912
1913 return 0;
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001916#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001917
Liam Girdwooda655de82018-07-02 16:59:54 +01001918static void soc_check_tplg_fes(struct snd_soc_card *card)
1919{
1920 struct snd_soc_component *component;
1921 const struct snd_soc_component_driver *comp_drv;
1922 struct snd_soc_dai_link *dai_link;
1923 int i;
1924
1925 list_for_each_entry(component, &component_list, list) {
1926
1927 /* does this component override FEs ? */
1928 if (!component->driver->ignore_machine)
1929 continue;
1930
1931 /* for this machine ? */
1932 if (strcmp(component->driver->ignore_machine,
1933 card->dev->driver->name))
1934 continue;
1935
1936 /* machine matches, so override the rtd data */
1937 for (i = 0; i < card->num_links; i++) {
1938
1939 dai_link = &card->dai_link[i];
1940
1941 /* ignore this FE */
1942 if (dai_link->dynamic) {
1943 dai_link->ignore = true;
1944 continue;
1945 }
1946
1947 dev_info(card->dev, "info: override FE DAI link %s\n",
1948 card->dai_link[i].name);
1949
1950 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001951 if (snd_soc_init_platform(card, dai_link) < 0) {
1952 dev_err(card->dev, "init platform error");
1953 continue;
1954 }
1955 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001956
1957 /* convert non BE into BE */
1958 dai_link->no_pcm = 1;
1959
1960 /* override any BE fixups */
1961 dai_link->be_hw_params_fixup =
1962 component->driver->be_hw_params_fixup;
1963
1964 /* most BE links don't set stream name, so set it to
1965 * dai link name if it's NULL to help bind widgets.
1966 */
1967 if (!dai_link->stream_name)
1968 dai_link->stream_name = dai_link->name;
1969 }
1970
1971 /* Inform userspace we are using alternate topology */
1972 if (component->driver->topology_name_prefix) {
1973
1974 /* topology shortname created ? */
1975 if (!card->topology_shortname_created) {
1976 comp_drv = component->driver;
1977
1978 snprintf(card->topology_shortname, 32, "%s-%s",
1979 comp_drv->topology_name_prefix,
1980 card->name);
1981 card->topology_shortname_created = true;
1982 }
1983
1984 /* use topology shortname */
1985 card->name = card->topology_shortname;
1986 }
1987 }
1988}
1989
Mark Brownb19e6e72012-03-14 21:18:39 +00001990static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001991{
Mengdong Lin1a497982015-11-18 02:34:11 -05001992 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001993 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001994 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001996 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001997 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998
Liam Girdwooda655de82018-07-02 16:59:54 +01001999 /* check whether any platform is ignore machine FE and using topology */
2000 soc_check_tplg_fes(card);
2001
Mark Brownb19e6e72012-03-14 21:18:39 +00002002 /* bind DAIs */
2003 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002004 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002005 if (ret != 0)
2006 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007 }
2008
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002009 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002010 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002011 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002012 if (ret != 0)
2013 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002014 }
2015
Mengdong Linf8f80362015-12-02 14:11:22 +08002016 /* add predefined DAI links to the list */
2017 for (i = 0; i < card->num_links; i++)
2018 snd_soc_add_dai_link(card, card->dai_link+i);
2019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002021 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002022 card->owner, 0, &card->snd_card);
2023 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002024 dev_err(card->dev,
2025 "ASoC: can't create sound card for card %s: %d\n",
2026 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002027 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002028 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002029
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002030 soc_init_card_debugfs(card);
2031
Mark Browne37a4972011-03-02 18:21:57 +00002032 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2033 card->dapm.dev = card->dev;
2034 card->dapm.card = card;
2035 list_add(&card->dapm.list, &card->dapm_list);
2036
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002037#ifdef CONFIG_DEBUG_FS
2038 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2039#endif
2040
Mark Brown88ee1c62011-02-02 10:43:26 +00002041#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002042 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002043 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002044#endif
Andy Green6ed25972008-06-13 16:24:05 +01002045
Mark Brown9a841eb2011-04-12 17:51:37 -07002046 if (card->dapm_widgets)
2047 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2048 card->num_dapm_widgets);
2049
Nicolin Chenf23e8602015-02-14 17:22:49 -08002050 if (card->of_dapm_widgets)
2051 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2052 card->num_of_dapm_widgets);
2053
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002054 /* initialise the sound card only once */
2055 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002056 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 if (ret < 0)
2058 goto card_probe_error;
2059 }
2060
Stephen Warren62ae68f2012-06-08 12:34:23 -06002061 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002062 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2063 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002064 list_for_each_entry(rtd, &card->rtd_list, list) {
2065 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002066 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002067 dev_err(card->dev,
2068 "ASoC: failed to instantiate card %d\n",
2069 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002070 goto probe_dai_err;
2071 }
2072 }
2073 }
2074
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002075 /* probe auxiliary components */
2076 ret = soc_probe_aux_devices(card);
2077 if (ret < 0)
2078 goto probe_dai_err;
2079
Mengdong Lin61b00882015-12-02 14:11:48 +08002080 /* Find new DAI links added during probing components and bind them.
2081 * Components with topology may bring new DAIs and DAI links.
2082 */
2083 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2084 if (soc_is_dai_link_bound(card, dai_link))
2085 continue;
2086
2087 ret = soc_init_dai_link(card, dai_link);
2088 if (ret)
2089 goto probe_dai_err;
2090 ret = soc_bind_dai_link(card, dai_link);
2091 if (ret)
2092 goto probe_dai_err;
2093 }
2094
Stephen Warren62ae68f2012-06-08 12:34:23 -06002095 /* probe all DAI links on this card */
2096 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2097 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002098 list_for_each_entry(rtd, &card->rtd_list, list) {
2099 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002100 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002101 dev_err(card->dev,
2102 "ASoC: failed to instantiate card %d\n",
2103 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002104 goto probe_dai_err;
2105 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002106 }
2107 }
2108
Mark Brown888df392012-02-16 19:37:51 -08002109 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002110 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002111
Mark Brownb7af1da2011-04-07 19:18:44 +09002112 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002113 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002114
Mark Brownb8ad29d2011-03-02 18:35:51 +00002115 if (card->dapm_routes)
2116 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2117 card->num_dapm_routes);
2118
Nicolin Chenf23e8602015-02-14 17:22:49 -08002119 if (card->of_dapm_routes)
2120 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2121 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002122
Takashi Iwai861886d2017-04-24 08:54:42 +02002123 /* try to set some sane longname if DMI is available */
2124 snd_soc_set_dmi_name(card, NULL);
2125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002128 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2129 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002130 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2131 "%s", card->driver_name ? card->driver_name : card->name);
2132 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2133 switch (card->snd_card->driver[i]) {
2134 case '_':
2135 case '-':
2136 case '\0':
2137 break;
2138 default:
2139 if (!isalnum(card->snd_card->driver[i]))
2140 card->snd_card->driver[i] = '_';
2141 break;
2142 }
2143 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002144
Mark Brown28e9ad92011-03-02 18:36:34 +00002145 if (card->late_probe) {
2146 ret = card->late_probe(card);
2147 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002148 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002149 card->name, ret);
2150 goto probe_aux_dev_err;
2151 }
2152 }
2153
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002154 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002156 ret = snd_card_register(card->snd_card);
2157 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002158 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2159 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002160 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002161 }
2162
Mark Brown435c5e22008-12-04 15:32:53 +00002163 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002164 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002165 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002166 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002167
2168 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002169
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002170probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002171 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002173probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002174 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002175
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002176card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002177 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002178 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002179
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002180 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002181 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002182 snd_card_free(card->snd_card);
2183
Mark Brownb19e6e72012-03-14 21:18:39 +00002184base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002185 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002186 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002187 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002188
Mark Brownb19e6e72012-03-14 21:18:39 +00002189 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002190}
2191
2192/* probes a new socdev */
2193static int soc_probe(struct platform_device *pdev)
2194{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002195 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002196
Vinod Koul70a7ca32011-01-14 19:22:48 +05302197 /*
2198 * no card, so machine driver should be registering card
2199 * we should not be here in that case so ret error
2200 */
2201 if (!card)
2202 return -EINVAL;
2203
Mark Brownfe4085e2012-03-02 13:07:41 +00002204 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002205 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002206 card->name);
2207
Mark Brown435c5e22008-12-04 15:32:53 +00002208 /* Bodge while we unpick instantiation */
2209 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002210
Mark Brown28d528c2012-08-09 18:45:23 +01002211 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002212}
2213
Vinod Koulb0e26482011-01-13 22:48:02 +05302214static int soc_cleanup_card_resources(struct snd_soc_card *card)
2215{
Mengdong Lin1a497982015-11-18 02:34:11 -05002216 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302217
2218 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002219 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002220 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302221
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002222 /* free the ALSA card at first; this syncs with pending operations */
2223 snd_card_free(card->snd_card);
2224
Vinod Koulb0e26482011-01-13 22:48:02 +05302225 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002226 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002227 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302228
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002229 /* remove auxiliary devices */
2230 soc_remove_aux_devices(card);
2231
Mark Brownd1e81422016-08-18 19:32:59 +01002232 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302233 soc_cleanup_card_debugfs(card);
2234
2235 /* remove the card */
2236 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002237 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302238
Vinod Koulb0e26482011-01-13 22:48:02 +05302239 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302240}
2241
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242/* removes a socdev */
2243static int soc_remove(struct platform_device *pdev)
2244{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002245 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002246
Mark Brownc5af3a22008-11-28 13:29:45 +00002247 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 return 0;
2249}
2250
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002251int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002252{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002253 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002254 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002255
2256 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002257 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002258
2259 /* Flush out pmdown_time work - we actually do want to run it
2260 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002261 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002262 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002263
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002264 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002265
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002266 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002267 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002268 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002269 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002270
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002271 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002272 for (i = 0; i < rtd->num_codecs; i++) {
2273 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002274 pinctrl_pm_select_sleep_state(codec_dai->dev);
2275 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002276 }
2277
Mark Brown416356f2009-06-30 19:05:15 +01002278 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002279}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002280EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002281
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002282const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302283 .suspend = snd_soc_suspend,
2284 .resume = snd_soc_resume,
2285 .freeze = snd_soc_suspend,
2286 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002287 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302288 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002289};
Stephen Warrendeb26072011-04-05 19:35:30 -06002290EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002291
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002292/* ASoC platform driver */
2293static struct platform_driver soc_driver = {
2294 .driver = {
2295 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002296 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 },
2298 .probe = soc_probe,
2299 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002300};
2301
Mark Brown096e49d2009-07-05 15:12:22 +01002302/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002303 * snd_soc_cnew - create new control
2304 * @_template: control template
2305 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002306 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002307 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308 *
2309 * Create a new mixer control from a template control.
2310 *
2311 * Returns 0 for success, else error.
2312 */
2313struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002314 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002315 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316{
2317 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002318 struct snd_kcontrol *kcontrol;
2319 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002320
2321 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 template.index = 0;
2323
Mark Brownefb7ac32011-03-08 17:23:24 +00002324 if (!long_name)
2325 long_name = template.name;
2326
2327 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002328 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002329 if (!name)
2330 return NULL;
2331
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 template.name = name;
2333 } else {
2334 template.name = long_name;
2335 }
2336
2337 kcontrol = snd_ctl_new1(&template, data);
2338
2339 kfree(name);
2340
2341 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342}
2343EXPORT_SYMBOL_GPL(snd_soc_cnew);
2344
Liam Girdwood022658b2012-02-03 17:43:09 +00002345static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2346 const struct snd_kcontrol_new *controls, int num_controls,
2347 const char *prefix, void *data)
2348{
2349 int err, i;
2350
2351 for (i = 0; i < num_controls; i++) {
2352 const struct snd_kcontrol_new *control = &controls[i];
2353 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2354 control->name, prefix));
2355 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002356 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2357 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002358 return err;
2359 }
2360 }
2361
2362 return 0;
2363}
2364
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002365struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2366 const char *name)
2367{
2368 struct snd_card *card = soc_card->snd_card;
2369 struct snd_kcontrol *kctl;
2370
2371 if (unlikely(!name))
2372 return NULL;
2373
2374 list_for_each_entry(kctl, &card->controls, list)
2375 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2376 return kctl;
2377 return NULL;
2378}
2379EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2380
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002382 * snd_soc_add_component_controls - Add an array of controls to a component.
2383 *
2384 * @component: Component to add controls to
2385 * @controls: Array of controls to add
2386 * @num_controls: Number of elements in the array
2387 *
2388 * Return: 0 for success, else error.
2389 */
2390int snd_soc_add_component_controls(struct snd_soc_component *component,
2391 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2392{
2393 struct snd_card *card = component->card->snd_card;
2394
2395 return snd_soc_add_controls(card, component->dev, controls,
2396 num_controls, component->name_prefix, component);
2397}
2398EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2399
2400/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002401 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2402 * Convenience function to add a list of controls.
2403 *
2404 * @soc_card: SoC card to add controls to
2405 * @controls: array of controls to add
2406 * @num_controls: number of elements in the array
2407 *
2408 * Return 0 for success, else error.
2409 */
2410int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2411 const struct snd_kcontrol_new *controls, int num_controls)
2412{
2413 struct snd_card *card = soc_card->snd_card;
2414
2415 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2416 NULL, soc_card);
2417}
2418EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2419
2420/**
2421 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2422 * Convienience function to add a list of controls.
2423 *
2424 * @dai: DAI to add controls to
2425 * @controls: array of controls to add
2426 * @num_controls: number of elements in the array
2427 *
2428 * Return 0 for success, else error.
2429 */
2430int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2431 const struct snd_kcontrol_new *controls, int num_controls)
2432{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002433 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002434
2435 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2436 NULL, dai);
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2439
2440/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002441 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2442 * @dai: DAI
2443 * @clk_id: DAI specific clock ID
2444 * @freq: new clock frequency in Hz
2445 * @dir: new clock direction - input/output.
2446 *
2447 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2448 */
2449int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2450 unsigned int freq, int dir)
2451{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002452 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002453 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002454
2455 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2456 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002457}
2458EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2459
2460/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002461 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2462 * @component: COMPONENT
2463 * @clk_id: DAI specific clock ID
2464 * @source: Source for the clock
2465 * @freq: new clock frequency in Hz
2466 * @dir: new clock direction - input/output.
2467 *
2468 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2469 */
2470int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2471 int source, unsigned int freq, int dir)
2472{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002473 if (component->driver->set_sysclk)
2474 return component->driver->set_sysclk(component, clk_id, source,
2475 freq, dir);
2476
2477 return -ENOTSUPP;
2478}
2479EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2480
2481/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002482 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2483 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002484 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002485 * @div: new clock divisor.
2486 *
2487 * Configures the clock dividers. This is used to derive the best DAI bit and
2488 * frame clocks from the system or master clock. It's best to set the DAI bit
2489 * and frame clocks as low as possible to save system power.
2490 */
2491int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2492 int div_id, int div)
2493{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002494 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002495 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002496 else
2497 return -EINVAL;
2498}
2499EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2500
2501/**
2502 * snd_soc_dai_set_pll - configure DAI PLL.
2503 * @dai: DAI
2504 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002505 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002506 * @freq_in: PLL input clock frequency in Hz
2507 * @freq_out: requested PLL output clock frequency in Hz
2508 *
2509 * Configures and enables PLL to generate output clock based on input clock.
2510 */
Mark Brown85488032009-09-05 18:52:16 +01002511int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2512 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002513{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002514 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002515 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002516 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002517
2518 return snd_soc_component_set_pll(dai->component, pll_id, source,
2519 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002520}
2521EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2522
Mark Brownec4ee522011-03-07 20:58:11 +00002523/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002524 * snd_soc_component_set_pll - configure component PLL.
2525 * @component: COMPONENT
2526 * @pll_id: DAI specific PLL ID
2527 * @source: DAI specific source for the PLL
2528 * @freq_in: PLL input clock frequency in Hz
2529 * @freq_out: requested PLL output clock frequency in Hz
2530 *
2531 * Configures and enables PLL to generate output clock based on input clock.
2532 */
2533int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2534 int source, unsigned int freq_in,
2535 unsigned int freq_out)
2536{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002537 if (component->driver->set_pll)
2538 return component->driver->set_pll(component, pll_id, source,
2539 freq_in, freq_out);
2540
2541 return -EINVAL;
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2544
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002545/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002546 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2547 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002548 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002549 *
2550 * Configures the DAI for a preset BCLK to sample rate ratio.
2551 */
2552int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2553{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002554 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002555 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2556 else
2557 return -EINVAL;
2558}
2559EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2560
2561/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002562 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2563 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002564 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002565 *
2566 * Configures the DAI hardware format and clocking.
2567 */
2568int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2569{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002570 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002571 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002572 if (dai->driver->ops->set_fmt == NULL)
2573 return -ENOTSUPP;
2574 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002575}
2576EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2577
2578/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002579 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002580 * @slots: Number of slots in use.
2581 * @tx_mask: bitmask representing active TX slots.
2582 * @rx_mask: bitmask representing active RX slots.
2583 *
2584 * Generates the TDM tx and rx slot default masks for DAI.
2585 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002586static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002587 unsigned int *tx_mask,
2588 unsigned int *rx_mask)
2589{
2590 if (*tx_mask || *rx_mask)
2591 return 0;
2592
2593 if (!slots)
2594 return -EINVAL;
2595
2596 *tx_mask = (1 << slots) - 1;
2597 *rx_mask = (1 << slots) - 1;
2598
2599 return 0;
2600}
2601
2602/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002603 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2604 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002605 * @tx_mask: bitmask representing active TX slots.
2606 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002607 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002608 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002609 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002610 * This function configures the specified DAI for TDM operation. @slot contains
2611 * the total number of slots of the TDM stream and @slot_with the width of each
2612 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2613 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2614 * DAI should write to or read from. If a bit is set the corresponding slot is
2615 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2616 * the first slot, bit 1 to the second slot and so on. The first active slot
2617 * maps to the first channel of the DAI, the second active slot to the second
2618 * channel and so on.
2619 *
2620 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2621 * @rx_mask and @slot_width will be ignored.
2622 *
2623 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002624 */
2625int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002626 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002627{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002628 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002629 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002630 &tx_mask, &rx_mask);
2631 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002632 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002633
Benoit Cousson88bd8702014-07-08 23:19:34 +02002634 dai->tx_mask = tx_mask;
2635 dai->rx_mask = rx_mask;
2636
Kuninori Morimoto46471922017-09-25 01:38:34 +00002637 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002638 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002639 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002640 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002641 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002642}
2643EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2644
2645/**
Barry Song472df3c2009-09-12 01:16:29 +08002646 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2647 * @dai: DAI
2648 * @tx_num: how many TX channels
2649 * @tx_slot: pointer to an array which imply the TX slot number channel
2650 * 0~num-1 uses
2651 * @rx_num: how many RX channels
2652 * @rx_slot: pointer to an array which imply the RX slot number channel
2653 * 0~num-1 uses
2654 *
2655 * configure the relationship between channel number and TDM slot number.
2656 */
2657int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2658 unsigned int tx_num, unsigned int *tx_slot,
2659 unsigned int rx_num, unsigned int *rx_slot)
2660{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002661 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002662 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002663 rx_num, rx_slot);
2664 else
2665 return -EINVAL;
2666}
2667EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2668
2669/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002670 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2671 * @dai: DAI
2672 * @tx_num: how many TX channels
2673 * @tx_slot: pointer to an array which imply the TX slot number channel
2674 * 0~num-1 uses
2675 * @rx_num: how many RX channels
2676 * @rx_slot: pointer to an array which imply the RX slot number channel
2677 * 0~num-1 uses
2678 */
2679int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2680 unsigned int *tx_num, unsigned int *tx_slot,
2681 unsigned int *rx_num, unsigned int *rx_slot)
2682{
2683 if (dai->driver->ops->get_channel_map)
2684 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2685 rx_num, rx_slot);
2686 else
2687 return -ENOTSUPP;
2688}
2689EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2690
2691/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002692 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2693 * @dai: DAI
2694 * @tristate: tristate enable
2695 *
2696 * Tristates the DAI so that others can use it.
2697 */
2698int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2699{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002700 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002701 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002702 else
2703 return -EINVAL;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2706
2707/**
2708 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2709 * @dai: DAI
2710 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002711 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002712 *
2713 * Mutes the DAI DAC.
2714 */
Mark Brownda183962013-02-06 15:44:07 +00002715int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2716 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002717{
Mark Brownda183962013-02-06 15:44:07 +00002718 if (!dai->driver)
2719 return -ENOTSUPP;
2720
2721 if (dai->driver->ops->mute_stream)
2722 return dai->driver->ops->mute_stream(dai, mute, direction);
2723 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2724 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002725 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726 else
Mark Brown04570c62012-04-13 19:16:03 +01002727 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002728}
2729EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2730
Mark Brownc5af3a22008-11-28 13:29:45 +00002731/**
2732 * snd_soc_register_card - Register a card with the ASoC core
2733 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002734 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002735 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002736 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302737int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002738{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002739 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002740 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002741
Mark Brownc5af3a22008-11-28 13:29:45 +00002742 if (!card->name || !card->dev)
2743 return -EINVAL;
2744
Stephen Warren5a504962011-12-21 10:40:59 -07002745 for (i = 0; i < card->num_links; i++) {
2746 struct snd_soc_dai_link *link = &card->dai_link[i];
2747
Mengdong Lin923c5e612015-11-23 11:01:49 -05002748 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002749 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002750 dev_err(card->dev, "ASoC: failed to init link %s\n",
2751 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002752 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002753 }
Stephen Warren5a504962011-12-21 10:40:59 -07002754 }
2755
Mark Browned77cc12011-05-03 18:25:34 +01002756 dev_set_drvdata(card->dev, card);
2757
Stephen Warren111c6412011-01-28 14:26:35 -07002758 snd_soc_initialize_card_lists(card);
2759
Mengdong Linf8f80362015-12-02 14:11:22 +08002760 INIT_LIST_HEAD(&card->dai_link_list);
2761 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002762
Mengdong Lin1a497982015-11-18 02:34:11 -05002763 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002764 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765
Mark Browndb432b42011-10-03 21:06:40 +01002766 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002767 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002768 card->instantiated = 0;
2769 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002770 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002771
Mark Brownb19e6e72012-03-14 21:18:39 +00002772 ret = snd_soc_instantiate_card(card);
2773 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002774 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002775
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002776 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002777 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002778 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2779 int j;
2780
2781 for (j = 0; j < rtd->num_codecs; j++) {
2782 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2783 if (!codec_dai->active)
2784 pinctrl_pm_select_sleep_state(codec_dai->dev);
2785 }
2786
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002787 if (!cpu_dai->active)
2788 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2789 }
2790
Mark Brownb19e6e72012-03-14 21:18:39 +00002791 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002792}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302793EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002794
2795/**
2796 * snd_soc_unregister_card - Unregister a card with the ASoC core
2797 *
2798 * @card: Card to unregister
2799 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002800 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302801int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002802{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002803 if (card->instantiated) {
2804 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002805 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302806 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002807 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002808 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002809
2810 return 0;
2811}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302812EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002813
2814/*
2815 * Simplify DAI link configuration by removing ".-1" from device names
2816 * and sanitizing names.
2817 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002818static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002819{
2820 char *found, name[NAME_SIZE];
2821 int id1, id2;
2822
2823 if (dev_name(dev) == NULL)
2824 return NULL;
2825
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002826 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002827
2828 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002829 found = strstr(name, dev->driver->name);
2830 if (found) {
2831 /* get ID */
2832 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2833
2834 /* discard ID from name if ID == -1 */
2835 if (*id == -1)
2836 found[strlen(dev->driver->name)] = '\0';
2837 }
2838
2839 } else {
2840 /* I2C component devices are named "bus-addr" */
2841 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2842 char tmp[NAME_SIZE];
2843
2844 /* create unique ID number from I2C addr and bus */
2845 *id = ((id1 & 0xffff) << 16) + id2;
2846
2847 /* sanitize component name for DAI link creation */
2848 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002849 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002850 } else
2851 *id = 0;
2852 }
2853
2854 return kstrdup(name, GFP_KERNEL);
2855}
2856
2857/*
2858 * Simplify DAI link naming for single devices with multiple DAIs by removing
2859 * any ".-1" and using the DAI name (instead of device name).
2860 */
2861static inline char *fmt_multiple_name(struct device *dev,
2862 struct snd_soc_dai_driver *dai_drv)
2863{
2864 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002865 dev_err(dev,
2866 "ASoC: error - multiple DAI %s registered with no name\n",
2867 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002868 return NULL;
2869 }
2870
2871 return kstrdup(dai_drv->name, GFP_KERNEL);
2872}
2873
2874/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002875 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002876 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002877 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002878 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002879static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002880{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002881 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002882
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002883 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002884 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2885 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002886 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002887 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002888 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002889 }
Mark Brown91151712008-11-30 23:31:24 +00002890}
Mark Brown91151712008-11-30 23:31:24 +00002891
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002892/* Create a DAI and add it to the component's DAI list */
2893static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2894 struct snd_soc_dai_driver *dai_drv,
2895 bool legacy_dai_naming)
2896{
2897 struct device *dev = component->dev;
2898 struct snd_soc_dai *dai;
2899
2900 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2901
2902 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2903 if (dai == NULL)
2904 return NULL;
2905
2906 /*
2907 * Back in the old days when we still had component-less DAIs,
2908 * instead of having a static name, component-less DAIs would
2909 * inherit the name of the parent device so it is possible to
2910 * register multiple instances of the DAI. We still need to keep
2911 * the same naming style even though those DAIs are not
2912 * component-less anymore.
2913 */
2914 if (legacy_dai_naming &&
2915 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2916 dai->name = fmt_single_name(dev, &dai->id);
2917 } else {
2918 dai->name = fmt_multiple_name(dev, dai_drv);
2919 if (dai_drv->id)
2920 dai->id = dai_drv->id;
2921 else
2922 dai->id = component->num_dai;
2923 }
2924 if (dai->name == NULL) {
2925 kfree(dai);
2926 return NULL;
2927 }
2928
2929 dai->component = component;
2930 dai->dev = dev;
2931 dai->driver = dai_drv;
2932 if (!dai->driver->ops)
2933 dai->driver->ops = &null_dai_ops;
2934
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002935 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002936 component->num_dai++;
2937
2938 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2939 return dai;
2940}
2941
Mark Brown91151712008-11-30 23:31:24 +00002942/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002943 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002944 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002945 * @component: The component the DAIs are registered for
2946 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002947 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002948 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002949static int snd_soc_register_dais(struct snd_soc_component *component,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002950 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002951{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002952 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002954 unsigned int i;
2955 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002956
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002957 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002958
2959 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002960
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002961 dai = soc_add_dai(component, dai_drv + i,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002962 count == 1 && !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002963 if (dai == NULL) {
2964 ret = -ENOMEM;
2965 goto err;
2966 }
Mark Brown91151712008-11-30 23:31:24 +00002967 }
2968
2969 return 0;
2970
2971err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002972 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002973
2974 return ret;
2975}
Mark Brown91151712008-11-30 23:31:24 +00002976
Mengdong Lin68003e62015-12-31 16:40:43 +08002977/**
2978 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2979 *
2980 * @component: The component the DAIs are registered for
2981 * @dai_drv: DAI driver to use for the DAI
2982 *
2983 * Topology can use this API to register DAIs when probing a component.
2984 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2985 * will be freed in the component cleanup.
2986 */
2987int snd_soc_register_dai(struct snd_soc_component *component,
2988 struct snd_soc_dai_driver *dai_drv)
2989{
2990 struct snd_soc_dapm_context *dapm =
2991 snd_soc_component_get_dapm(component);
2992 struct snd_soc_dai *dai;
2993 int ret;
2994
2995 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2996 dev_err(component->dev, "Invalid dai type %d\n",
2997 dai_drv->dobj.type);
2998 return -EINVAL;
2999 }
3000
3001 lockdep_assert_held(&client_mutex);
3002 dai = soc_add_dai(component, dai_drv, false);
3003 if (!dai)
3004 return -ENOMEM;
3005
3006 /* Create the DAI widgets here. After adding DAIs, topology may
3007 * also add routes that need these widgets as source or sink.
3008 */
3009 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3010 if (ret != 0) {
3011 dev_err(component->dev,
3012 "Failed to create DAI widgets %d\n", ret);
3013 }
3014
3015 return ret;
3016}
3017EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3018
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003019static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3020 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003021{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003022 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003023
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003024 component->driver->seq_notifier(component, type, subseq);
3025}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003026
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003027static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3028 int event)
3029{
3030 struct snd_soc_component *component = dapm->component;
3031
3032 return component->driver->stream_event(component, event);
3033}
3034
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003035static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3036 enum snd_soc_bias_level level)
3037{
3038 struct snd_soc_component *component = dapm->component;
3039
3040 return component->driver->set_bias_level(component, level);
3041}
3042
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003043static int snd_soc_component_initialize(struct snd_soc_component *component,
3044 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003045{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003046 struct snd_soc_dapm_context *dapm;
3047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003048 component->name = fmt_single_name(dev, &component->id);
3049 if (!component->name) {
3050 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003051 return -ENOMEM;
3052 }
3053
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003054 component->dev = dev;
3055 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003056
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003057 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003058 dapm->dev = dev;
3059 dapm->component = component;
3060 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003061 dapm->idle_bias_off = !driver->idle_bias_on;
3062 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003063 if (driver->seq_notifier)
3064 dapm->seq_notifier = snd_soc_component_seq_notifier;
3065 if (driver->stream_event)
3066 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003067 if (driver->set_bias_level)
3068 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003069
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003070 INIT_LIST_HEAD(&component->dai_list);
3071 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003072
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003073 return 0;
3074}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003075
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003076static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003077{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003078 int val_bytes = regmap_get_val_bytes(component->regmap);
3079
3080 /* Errors are legitimate for non-integer byte multiples */
3081 if (val_bytes > 0)
3082 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003083}
3084
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003085#ifdef CONFIG_REGMAP
3086
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003087/**
3088 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3089 * @component: The component for which to initialize the regmap instance
3090 * @regmap: The regmap instance that should be used by the component
3091 *
3092 * This function allows deferred assignment of the regmap instance that is
3093 * associated with the component. Only use this if the regmap instance is not
3094 * yet ready when the component is registered. The function must also be called
3095 * before the first IO attempt of the component.
3096 */
3097void snd_soc_component_init_regmap(struct snd_soc_component *component,
3098 struct regmap *regmap)
3099{
3100 component->regmap = regmap;
3101 snd_soc_component_setup_regmap(component);
3102}
3103EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3104
3105/**
3106 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3107 * @component: The component for which to de-initialize the regmap instance
3108 *
3109 * Calls regmap_exit() on the regmap instance associated to the component and
3110 * removes the regmap instance from the component.
3111 *
3112 * This function should only be used if snd_soc_component_init_regmap() was used
3113 * to initialize the regmap instance.
3114 */
3115void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3116{
3117 regmap_exit(component->regmap);
3118 component->regmap = NULL;
3119}
3120EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3121
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003122#endif
3123
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003124static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003125{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003126 mutex_lock(&client_mutex);
3127
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003128 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003129 if (!component->regmap)
3130 component->regmap = dev_get_regmap(component->dev, NULL);
3131 if (component->regmap)
3132 snd_soc_component_setup_regmap(component);
3133 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003134
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003135 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003136 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003137
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003138 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003139}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003140
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003141static void snd_soc_component_cleanup(struct snd_soc_component *component)
3142{
3143 snd_soc_unregister_dais(component);
3144 kfree(component->name);
3145}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003146
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003147static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3148{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003149 struct snd_soc_card *card = component->card;
3150
3151 if (card)
3152 snd_soc_unregister_card(card);
3153
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003154 list_del(&component->list);
3155}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003156
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003157#define ENDIANNESS_MAP(name) \
3158 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3159static u64 endianness_format_map[] = {
3160 ENDIANNESS_MAP(S16_),
3161 ENDIANNESS_MAP(U16_),
3162 ENDIANNESS_MAP(S24_),
3163 ENDIANNESS_MAP(U24_),
3164 ENDIANNESS_MAP(S32_),
3165 ENDIANNESS_MAP(U32_),
3166 ENDIANNESS_MAP(S24_3),
3167 ENDIANNESS_MAP(U24_3),
3168 ENDIANNESS_MAP(S20_3),
3169 ENDIANNESS_MAP(U20_3),
3170 ENDIANNESS_MAP(S18_3),
3171 ENDIANNESS_MAP(U18_3),
3172 ENDIANNESS_MAP(FLOAT_),
3173 ENDIANNESS_MAP(FLOAT64_),
3174 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3175};
3176
3177/*
3178 * Fix up the DAI formats for endianness: codecs don't actually see
3179 * the endianness of the data but we're using the CPU format
3180 * definitions which do need to include endianness so we ensure that
3181 * codec DAIs always have both big and little endian variants set.
3182 */
3183static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3184{
3185 int i;
3186
3187 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3188 if (stream->formats & endianness_format_map[i])
3189 stream->formats |= endianness_format_map[i];
3190}
3191
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003192int snd_soc_add_component(struct device *dev,
3193 struct snd_soc_component *component,
3194 const struct snd_soc_component_driver *component_driver,
3195 struct snd_soc_dai_driver *dai_drv,
3196 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003197{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003198 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003199 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003200
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003201 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003202 if (ret)
3203 goto err_free;
3204
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003205 if (component_driver->endianness) {
3206 for (i = 0; i < num_dai; i++) {
3207 convert_endianness_formats(&dai_drv[i].playback);
3208 convert_endianness_formats(&dai_drv[i].capture);
3209 }
3210 }
3211
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003212 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003213 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003214 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003215 goto err_cleanup;
3216 }
3217
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003218 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003219
3220 return 0;
3221
3222err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003223 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003224err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003225 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003226}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003227EXPORT_SYMBOL_GPL(snd_soc_add_component);
3228
3229int snd_soc_register_component(struct device *dev,
3230 const struct snd_soc_component_driver *component_driver,
3231 struct snd_soc_dai_driver *dai_drv,
3232 int num_dai)
3233{
3234 struct snd_soc_component *component;
3235
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003236 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003237 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003238 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003239
3240 return snd_soc_add_component(dev, component, component_driver,
3241 dai_drv, num_dai);
3242}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003243EXPORT_SYMBOL_GPL(snd_soc_register_component);
3244
3245/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003246 * snd_soc_unregister_component - Unregister all related component
3247 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003248 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003249 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003250 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003251static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003252{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003253 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003254 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003255
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003256 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003257 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003258 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003259 continue;
3260
3261 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3262 snd_soc_component_del_unlocked(component);
3263 found = 1;
3264 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003265 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003266 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003267
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003268 if (found) {
3269 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003270 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003271
3272 return found;
3273}
3274
3275void snd_soc_unregister_component(struct device *dev)
3276{
3277 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003278}
3279EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3280
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003281struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3282 const char *driver_name)
3283{
3284 struct snd_soc_component *component;
3285 struct snd_soc_component *ret;
3286
3287 ret = NULL;
3288 mutex_lock(&client_mutex);
3289 list_for_each_entry(component, &component_list, list) {
3290 if (dev != component->dev)
3291 continue;
3292
3293 if (driver_name &&
3294 (driver_name != component->driver->name) &&
3295 (strcmp(component->driver->name, driver_name) != 0))
3296 continue;
3297
3298 ret = component;
3299 break;
3300 }
3301 mutex_unlock(&client_mutex);
3302
3303 return ret;
3304}
3305EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3306
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003307/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003308int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3309 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003310{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003311 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003312 int ret;
3313
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303314 if (!card->dev) {
3315 pr_err("card->dev is not set before calling %s\n", __func__);
3316 return -EINVAL;
3317 }
3318
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003319 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303320
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003321 ret = of_property_read_string_index(np, propname, 0, &card->name);
3322 /*
3323 * EINVAL means the property does not exist. This is fine providing
3324 * card->name was previously set, which is checked later in
3325 * snd_soc_register_card.
3326 */
3327 if (ret < 0 && ret != -EINVAL) {
3328 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003329 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003330 propname, ret);
3331 return ret;
3332 }
3333
3334 return 0;
3335}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003336EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003337
Xiubo Li9a6d4862014-02-08 15:59:52 +08003338static const struct snd_soc_dapm_widget simple_widgets[] = {
3339 SND_SOC_DAPM_MIC("Microphone", NULL),
3340 SND_SOC_DAPM_LINE("Line", NULL),
3341 SND_SOC_DAPM_HP("Headphone", NULL),
3342 SND_SOC_DAPM_SPK("Speaker", NULL),
3343};
3344
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003345int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003346 const char *propname)
3347{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003348 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003349 struct snd_soc_dapm_widget *widgets;
3350 const char *template, *wname;
3351 int i, j, num_widgets, ret;
3352
3353 num_widgets = of_property_count_strings(np, propname);
3354 if (num_widgets < 0) {
3355 dev_err(card->dev,
3356 "ASoC: Property '%s' does not exist\n", propname);
3357 return -EINVAL;
3358 }
3359 if (num_widgets & 1) {
3360 dev_err(card->dev,
3361 "ASoC: Property '%s' length is not even\n", propname);
3362 return -EINVAL;
3363 }
3364
3365 num_widgets /= 2;
3366 if (!num_widgets) {
3367 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3368 propname);
3369 return -EINVAL;
3370 }
3371
3372 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3373 GFP_KERNEL);
3374 if (!widgets) {
3375 dev_err(card->dev,
3376 "ASoC: Could not allocate memory for widgets\n");
3377 return -ENOMEM;
3378 }
3379
3380 for (i = 0; i < num_widgets; i++) {
3381 ret = of_property_read_string_index(np, propname,
3382 2 * i, &template);
3383 if (ret) {
3384 dev_err(card->dev,
3385 "ASoC: Property '%s' index %d read error:%d\n",
3386 propname, 2 * i, ret);
3387 return -EINVAL;
3388 }
3389
3390 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3391 if (!strncmp(template, simple_widgets[j].name,
3392 strlen(simple_widgets[j].name))) {
3393 widgets[i] = simple_widgets[j];
3394 break;
3395 }
3396 }
3397
3398 if (j >= ARRAY_SIZE(simple_widgets)) {
3399 dev_err(card->dev,
3400 "ASoC: DAPM widget '%s' is not supported\n",
3401 template);
3402 return -EINVAL;
3403 }
3404
3405 ret = of_property_read_string_index(np, propname,
3406 (2 * i) + 1,
3407 &wname);
3408 if (ret) {
3409 dev_err(card->dev,
3410 "ASoC: Property '%s' index %d read error:%d\n",
3411 propname, (2 * i) + 1, ret);
3412 return -EINVAL;
3413 }
3414
3415 widgets[i].name = wname;
3416 }
3417
Nicolin Chenf23e8602015-02-14 17:22:49 -08003418 card->of_dapm_widgets = widgets;
3419 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003420
3421 return 0;
3422}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003423EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003424
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003425int snd_soc_of_get_slot_mask(struct device_node *np,
3426 const char *prop_name,
3427 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003428{
3429 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003430 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003431 int i;
3432
3433 if (!of_slot_mask)
3434 return 0;
3435 val /= sizeof(u32);
3436 for (i = 0; i < val; i++)
3437 if (be32_to_cpup(&of_slot_mask[i]))
3438 *mask |= (1 << i);
3439
3440 return val;
3441}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003442EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003443
Xiubo Li89c67852014-02-14 09:34:35 +08003444int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003445 unsigned int *tx_mask,
3446 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003447 unsigned int *slots,
3448 unsigned int *slot_width)
3449{
3450 u32 val;
3451 int ret;
3452
Jyri Sarha61310842015-09-09 21:27:43 +03003453 if (tx_mask)
3454 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3455 if (rx_mask)
3456 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3457
Xiubo Li89c67852014-02-14 09:34:35 +08003458 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3459 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3460 if (ret)
3461 return ret;
3462
3463 if (slots)
3464 *slots = val;
3465 }
3466
3467 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3468 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3469 if (ret)
3470 return ret;
3471
3472 if (slot_width)
3473 *slot_width = val;
3474 }
3475
3476 return 0;
3477}
3478EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3479
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003480void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003481 struct snd_soc_codec_conf *codec_conf,
3482 struct device_node *of_node,
3483 const char *propname)
3484{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003485 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003486 const char *str;
3487 int ret;
3488
3489 ret = of_property_read_string(np, propname, &str);
3490 if (ret < 0) {
3491 /* no prefix is not error */
3492 return;
3493 }
3494
3495 codec_conf->of_node = of_node;
3496 codec_conf->name_prefix = str;
3497}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003498EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003499
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003500int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003501 const char *propname)
3502{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003503 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003504 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003505 struct snd_soc_dapm_route *routes;
3506 int i, ret;
3507
3508 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003509 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003510 dev_err(card->dev,
3511 "ASoC: Property '%s' does not exist or its length is not even\n",
3512 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003513 return -EINVAL;
3514 }
3515 num_routes /= 2;
3516 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003517 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003518 propname);
3519 return -EINVAL;
3520 }
3521
Kees Cooka86854d2018-06-12 14:07:58 -07003522 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003523 GFP_KERNEL);
3524 if (!routes) {
3525 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003526 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003527 return -EINVAL;
3528 }
3529
3530 for (i = 0; i < num_routes; i++) {
3531 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003532 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003533 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003534 dev_err(card->dev,
3535 "ASoC: Property '%s' index %d could not be read: %d\n",
3536 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003537 return -EINVAL;
3538 }
3539 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003540 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003541 if (ret) {
3542 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003543 "ASoC: Property '%s' index %d could not be read: %d\n",
3544 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003545 return -EINVAL;
3546 }
3547 }
3548
Nicolin Chenf23e8602015-02-14 17:22:49 -08003549 card->num_of_dapm_routes = num_routes;
3550 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003551
3552 return 0;
3553}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003554EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003555
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003556unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003557 const char *prefix,
3558 struct device_node **bitclkmaster,
3559 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003560{
3561 int ret, i;
3562 char prop[128];
3563 unsigned int format = 0;
3564 int bit, frame;
3565 const char *str;
3566 struct {
3567 char *name;
3568 unsigned int val;
3569 } of_fmt_table[] = {
3570 { "i2s", SND_SOC_DAIFMT_I2S },
3571 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3572 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3573 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3574 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3575 { "ac97", SND_SOC_DAIFMT_AC97 },
3576 { "pdm", SND_SOC_DAIFMT_PDM},
3577 { "msb", SND_SOC_DAIFMT_MSB },
3578 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003579 };
3580
3581 if (!prefix)
3582 prefix = "";
3583
3584 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003585 * check "dai-format = xxx"
3586 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003587 * SND_SOC_DAIFMT_FORMAT_MASK area
3588 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003589 ret = of_property_read_string(np, "dai-format", &str);
3590 if (ret < 0) {
3591 snprintf(prop, sizeof(prop), "%sformat", prefix);
3592 ret = of_property_read_string(np, prop, &str);
3593 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003594 if (ret == 0) {
3595 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3596 if (strcmp(str, of_fmt_table[i].name) == 0) {
3597 format |= of_fmt_table[i].val;
3598 break;
3599 }
3600 }
3601 }
3602
3603 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003604 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003605 * SND_SOC_DAIFMT_CLOCK_MASK area
3606 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003607 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003608 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003609 format |= SND_SOC_DAIFMT_CONT;
3610 else
3611 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003612
3613 /*
3614 * check "[prefix]bitclock-inversion"
3615 * check "[prefix]frame-inversion"
3616 * SND_SOC_DAIFMT_INV_MASK area
3617 */
3618 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3619 bit = !!of_get_property(np, prop, NULL);
3620
3621 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3622 frame = !!of_get_property(np, prop, NULL);
3623
3624 switch ((bit << 4) + frame) {
3625 case 0x11:
3626 format |= SND_SOC_DAIFMT_IB_IF;
3627 break;
3628 case 0x10:
3629 format |= SND_SOC_DAIFMT_IB_NF;
3630 break;
3631 case 0x01:
3632 format |= SND_SOC_DAIFMT_NB_IF;
3633 break;
3634 default:
3635 /* SND_SOC_DAIFMT_NB_NF is default */
3636 break;
3637 }
3638
3639 /*
3640 * check "[prefix]bitclock-master"
3641 * check "[prefix]frame-master"
3642 * SND_SOC_DAIFMT_MASTER_MASK area
3643 */
3644 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3645 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003646 if (bit && bitclkmaster)
3647 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003648
3649 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3650 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003651 if (frame && framemaster)
3652 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003653
3654 switch ((bit << 4) + frame) {
3655 case 0x11:
3656 format |= SND_SOC_DAIFMT_CBM_CFM;
3657 break;
3658 case 0x10:
3659 format |= SND_SOC_DAIFMT_CBM_CFS;
3660 break;
3661 case 0x01:
3662 format |= SND_SOC_DAIFMT_CBS_CFM;
3663 break;
3664 default:
3665 format |= SND_SOC_DAIFMT_CBS_CFS;
3666 break;
3667 }
3668
3669 return format;
3670}
3671EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3672
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003673int snd_soc_get_dai_id(struct device_node *ep)
3674{
3675 struct snd_soc_component *pos;
3676 struct device_node *node;
3677 int ret;
3678
3679 node = of_graph_get_port_parent(ep);
3680
3681 /*
3682 * For example HDMI case, HDMI has video/sound port,
3683 * but ALSA SoC needs sound port number only.
3684 * Thus counting HDMI DT port/endpoint doesn't work.
3685 * Then, it should have .of_xlate_dai_id
3686 */
3687 ret = -ENOTSUPP;
3688 mutex_lock(&client_mutex);
3689 list_for_each_entry(pos, &component_list, list) {
3690 struct device_node *component_of_node = pos->dev->of_node;
3691
3692 if (!component_of_node && pos->dev->parent)
3693 component_of_node = pos->dev->parent->of_node;
3694
3695 if (component_of_node != node)
3696 continue;
3697
3698 if (pos->driver->of_xlate_dai_id)
3699 ret = pos->driver->of_xlate_dai_id(pos, ep);
3700
3701 break;
3702 }
3703 mutex_unlock(&client_mutex);
3704
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003705 of_node_put(node);
3706
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003707 return ret;
3708}
3709EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3710
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003711int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003712 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003713{
3714 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003715 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003716 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003717
3718 mutex_lock(&client_mutex);
3719 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003720 component_of_node = pos->dev->of_node;
3721 if (!component_of_node && pos->dev->parent)
3722 component_of_node = pos->dev->parent->of_node;
3723
3724 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003725 continue;
3726
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003727 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003728 ret = pos->driver->of_xlate_dai_name(pos,
3729 args,
3730 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003731 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003732 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003733 int id = -1;
3734
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003735 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003736 case 0:
3737 id = 0; /* same as dai_drv[0] */
3738 break;
3739 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003740 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003741 break;
3742 default:
3743 /* not supported */
3744 break;
3745 }
3746
3747 if (id < 0 || id >= pos->num_dai) {
3748 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003749 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003750 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003751
3752 ret = 0;
3753
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003754 /* find target DAI */
3755 list_for_each_entry(dai, &pos->dai_list, list) {
3756 if (id == 0)
3757 break;
3758 id--;
3759 }
3760
3761 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003762 if (!*dai_name)
3763 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003764 }
3765
Kuninori Morimotocb470082013-09-10 17:39:56 -07003766 break;
3767 }
3768 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003769 return ret;
3770}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003771EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003772
3773int snd_soc_of_get_dai_name(struct device_node *of_node,
3774 const char **dai_name)
3775{
3776 struct of_phandle_args args;
3777 int ret;
3778
3779 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3780 "#sound-dai-cells", 0, &args);
3781 if (ret)
3782 return ret;
3783
3784 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003785
3786 of_node_put(args.np);
3787
3788 return ret;
3789}
3790EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3791
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003792/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003793 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3794 * @dai_link: DAI link
3795 *
3796 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3797 */
3798void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3799{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003800 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003801 int index;
3802
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003803 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003804 if (!component->of_node)
3805 break;
3806 of_node_put(component->of_node);
3807 component->of_node = NULL;
3808 }
3809}
3810EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3811
3812/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003813 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3814 * @dev: Card device
3815 * @of_node: Device node
3816 * @dai_link: DAI link
3817 *
3818 * Builds an array of CODEC DAI components from the DAI link property
3819 * 'sound-dai'.
3820 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003821 * The device nodes in the array (of_node) must be dereferenced by calling
3822 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003823 *
3824 * Returns 0 for success
3825 */
3826int snd_soc_of_get_dai_link_codecs(struct device *dev,
3827 struct device_node *of_node,
3828 struct snd_soc_dai_link *dai_link)
3829{
3830 struct of_phandle_args args;
3831 struct snd_soc_dai_link_component *component;
3832 char *name;
3833 int index, num_codecs, ret;
3834
3835 /* Count the number of CODECs */
3836 name = "sound-dai";
3837 num_codecs = of_count_phandle_with_args(of_node, name,
3838 "#sound-dai-cells");
3839 if (num_codecs <= 0) {
3840 if (num_codecs == -ENOENT)
3841 dev_err(dev, "No 'sound-dai' property\n");
3842 else
3843 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3844 return num_codecs;
3845 }
Kees Cooka86854d2018-06-12 14:07:58 -07003846 component = devm_kcalloc(dev,
3847 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003848 GFP_KERNEL);
3849 if (!component)
3850 return -ENOMEM;
3851 dai_link->codecs = component;
3852 dai_link->num_codecs = num_codecs;
3853
3854 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003855 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003856 ret = of_parse_phandle_with_args(of_node, name,
3857 "#sound-dai-cells",
3858 index, &args);
3859 if (ret)
3860 goto err;
3861 component->of_node = args.np;
3862 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3863 if (ret < 0)
3864 goto err;
3865 }
3866 return 0;
3867err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003868 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003869 dai_link->codecs = NULL;
3870 dai_link->num_codecs = 0;
3871 return ret;
3872}
3873EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3874
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003875static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003876{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003877 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003878 snd_soc_util_init();
3879
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003880 return platform_driver_register(&soc_driver);
3881}
Mark Brown4abe8e12010-10-12 17:41:03 +01003882module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003883
Mark Brown7d8c16a2008-11-30 22:11:24 +00003884static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003885{
Mark Brownfb257892011-04-28 10:57:54 +01003886 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003887 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003888
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003889 platform_driver_unregister(&soc_driver);
3890}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003891module_exit(snd_soc_exit);
3892
3893/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003894MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003895MODULE_DESCRIPTION("ALSA SoC Core");
3896MODULE_LICENSE("GPL");
3897MODULE_ALIAS("platform:soc-audio");