blob: 6cdfe7b2fe068d52152618ca87e5a727aa31b791 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090061 * This is used if driver don't need to have CPU/Codec/Platform
62 * dai_link. see soc.h
63 */
64struct snd_soc_dai_link_component null_dailink_component[0];
65EXPORT_SYMBOL_GPL(null_dailink_component);
66
67/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020068 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
71 */
72static int pmdown_time = 5000;
73module_param(pmdown_time, int, 0);
74MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020076/*
77 * If a DMI filed contain strings in this blacklist (e.g.
78 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080079 * as invalid and dropped when setting the card long name from DMI info.
80 */
81static const char * const dmi_blacklist[] = {
82 "To be filled by OEM",
83 "TBD by OEM",
84 "Default String",
85 "Board Manufacturer",
86 "Board Vendor Name",
87 "Board Product Name",
88 NULL, /* terminator */
89};
90
Mark Browndbe21402010-02-12 11:37:24 +000091static ssize_t pmdown_time_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
Mark Brown36ae1a92012-01-06 17:12:45 -080094 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000097}
98
99static ssize_t pmdown_time_set(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
Mark Brown36ae1a92012-01-06 17:12:45 -0800103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700104 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000105
Jingoo Hanb785a492013-07-19 16:24:59 +0900106 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700107 if (ret)
108 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000109
110 return count;
111}
112
113static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114
Takashi Iwaid29697d2015-01-30 20:16:37 +0100115static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100116 &dev_attr_pmdown_time.attr,
117 NULL
118};
119
120static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121 struct attribute *attr, int idx)
122{
123 struct device *dev = kobj_to_dev(kobj);
124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125
126 if (attr == &dev_attr_pmdown_time.attr)
127 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000128 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100129}
130
131static const struct attribute_group soc_dapm_dev_group = {
132 .attrs = soc_dapm_dev_attrs,
133 .is_visible = soc_dev_attr_is_visible,
134};
135
Mark Brownf7e73b262018-03-09 12:46:27 +0000136static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 .attrs = soc_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
141static const struct attribute_group *soc_dev_attr_groups[] = {
142 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000143 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100144 NULL
145};
146
Mark Brown2624d5f2009-11-03 21:56:13 +0000147#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200148static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100149{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200150 if (!component->card->debugfs_card_root)
151 return;
152
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153 if (component->debugfs_prefix) {
154 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 name = kasprintf(GFP_KERNEL, "%s:%s",
157 component->debugfs_prefix, component->name);
158 if (name) {
159 component->debugfs_root = debugfs_create_dir(name,
160 component->card->debugfs_card_root);
161 kfree(name);
162 }
163 } else {
164 component->debugfs_root = debugfs_create_dir(component->name,
165 component->card->debugfs_card_root);
166 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100167
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200168 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
169 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200170}
171
172static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
173{
174 debugfs_remove_recursive(component->debugfs_root);
175}
176
Peng Donglinc15b2a12018-02-14 22:48:07 +0800177static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100178{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100179 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100180 struct snd_soc_dai *dai;
181
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100182 mutex_lock(&client_mutex);
183
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000184 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000185 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800186 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100187
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100188 mutex_unlock(&client_mutex);
189
Donglin Peng700c17c2018-01-18 13:31:26 +0800190 return 0;
191}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800192DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100193
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000194static int component_list_show(struct seq_file *m, void *v)
195{
196 struct snd_soc_component *component;
197
198 mutex_lock(&client_mutex);
199
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000200 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000201 seq_printf(m, "%s\n", component->name);
202
203 mutex_unlock(&client_mutex);
204
205 return 0;
206}
207DEFINE_SHOW_ATTRIBUTE(component_list);
208
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200209static void soc_init_card_debugfs(struct snd_soc_card *card)
210{
211 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000212 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200213
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200214 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
215 &card->pop_time);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216}
217
218static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
219{
220 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900221 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200222}
223
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200224static void snd_soc_debugfs_init(void)
225{
226 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200227
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200228 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
229 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000230
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200231 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
232 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200233}
234
235static void snd_soc_debugfs_exit(void)
236{
237 debugfs_remove_recursive(snd_soc_debugfs_root);
238}
239
Mark Brown2624d5f2009-11-03 21:56:13 +0000240#else
241
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200242static inline void soc_init_component_debugfs(
243 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000244{
245}
246
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200247static inline void soc_cleanup_component_debugfs(
248 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000249{
250}
251
Axel Linb95fccb2010-11-09 17:06:44 +0800252static inline void soc_init_card_debugfs(struct snd_soc_card *card)
253{
254}
255
256static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
257{
258}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200259
260static inline void snd_soc_debugfs_init(void)
261{
262}
263
264static inline void snd_soc_debugfs_exit(void)
265{
266}
267
Mark Brown2624d5f2009-11-03 21:56:13 +0000268#endif
269
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000270static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
271 struct snd_soc_component *component)
272{
273 struct snd_soc_rtdcom_list *rtdcom;
274 struct snd_soc_rtdcom_list *new_rtdcom;
275
276 for_each_rtdcom(rtd, rtdcom) {
277 /* already connected */
278 if (rtdcom->component == component)
279 return 0;
280 }
281
282 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
283 if (!new_rtdcom)
284 return -ENOMEM;
285
286 new_rtdcom->component = component;
287 INIT_LIST_HEAD(&new_rtdcom->list);
288
289 list_add_tail(&new_rtdcom->list, &rtd->component_list);
290
291 return 0;
292}
293
294static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
295{
296 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
297
298 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
299 kfree(rtdcom1);
300
301 INIT_LIST_HEAD(&rtd->component_list);
302}
303
304struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
305 const char *driver_name)
306{
307 struct snd_soc_rtdcom_list *rtdcom;
308
Kuninori Morimoto971da242018-01-23 00:41:24 +0000309 if (!driver_name)
310 return NULL;
311
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000312 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000313 const char *component_name = rtdcom->component->driver->name;
314
315 if (!component_name)
316 continue;
317
318 if ((component_name == driver_name) ||
319 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000320 return rtdcom->component;
321 }
322
323 return NULL;
324}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000325EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000326
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100327struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
328 const char *dai_link, int stream)
329{
Mengdong Lin1a497982015-11-18 02:34:11 -0500330 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100331
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000332 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500333 if (rtd->dai_link->no_pcm &&
334 !strcmp(rtd->dai_link->name, dai_link))
335 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100336 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000337 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100338 return NULL;
339}
340EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
341
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000342static const struct snd_soc_ops null_snd_soc_ops;
343
Mengdong Lin1a497982015-11-18 02:34:11 -0500344static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
345 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
346{
347 struct snd_soc_pcm_runtime *rtd;
348
349 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
350 if (!rtd)
351 return NULL;
352
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000353 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500354 rtd->card = card;
355 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000356 if (!rtd->dai_link->ops)
357 rtd->dai_link->ops = &null_snd_soc_ops;
358
Kees Cook6396bb22018-06-12 14:03:40 -0700359 rtd->codec_dais = kcalloc(dai_link->num_codecs,
360 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500361 GFP_KERNEL);
362 if (!rtd->codec_dais) {
363 kfree(rtd);
364 return NULL;
365 }
366
367 return rtd;
368}
369
370static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
371{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000372 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000373 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500374 kfree(rtd);
375}
376
377static void soc_add_pcm_runtime(struct snd_soc_card *card,
378 struct snd_soc_pcm_runtime *rtd)
379{
380 list_add_tail(&rtd->list, &card->rtd_list);
381 rtd->num = card->num_rtd;
382 card->num_rtd++;
383}
384
385static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
386{
387 struct snd_soc_pcm_runtime *rtd, *_rtd;
388
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000389 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500390 list_del(&rtd->list);
391 soc_free_pcm_runtime(rtd);
392 }
393
394 card->num_rtd = 0;
395}
396
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100397struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
398 const char *dai_link)
399{
Mengdong Lin1a497982015-11-18 02:34:11 -0500400 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100401
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000402 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500403 if (!strcmp(rtd->dai_link->name, dai_link))
404 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100405 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000406 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100407 return NULL;
408}
409EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
410
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900411static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
412{
413 struct snd_soc_pcm_runtime *rtd;
414
415 for_each_card_rtds(card, rtd)
416 flush_delayed_work(&rtd->delayed_work);
417}
418
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000419#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200420/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000421int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200422{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000423 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000424 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500425 struct snd_soc_pcm_runtime *rtd;
426 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200427
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200428 /* If the card is not initialized yet there is nothing to do */
429 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200430 return 0;
431
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200432 /*
433 * Due to the resume being scheduled into a workqueue we could
434 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100435 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000436 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100437
438 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000439 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100440
Mark Browna00f90f2010-12-02 16:24:24 +0000441 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000442 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000443 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100444
Mengdong Lin1a497982015-11-18 02:34:11 -0500445 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100446 continue;
447
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000448 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900449 if (dai->playback_active)
450 snd_soc_dai_digital_mute(dai, 1,
451 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200452 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200453 }
454
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100455 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000456 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500457 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100458 continue;
459
Mengdong Lin1a497982015-11-18 02:34:11 -0500460 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100461 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100462
Mark Brown87506542008-11-18 20:50:34 +0000463 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000464 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000466 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500467 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100468
Mengdong Lin1a497982015-11-18 02:34:11 -0500469 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100470 continue;
471
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900472 if (!cpu_dai->driver->bus_control)
473 snd_soc_dai_suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100474 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200476 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900477 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100478
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000479 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480
Mengdong Lin1a497982015-11-18 02:34:11 -0500481 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100482 continue;
483
Mengdong Lin1a497982015-11-18 02:34:11 -0500484 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800485 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800486 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800489 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800490 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 }
492
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200493 /* Recheck all endpoints too, their state is affected by suspend */
494 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700495 snd_soc_dapm_sync(&card->dapm);
496
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000497 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000498 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200499 struct snd_soc_dapm_context *dapm =
500 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000501
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200502 /*
503 * If there are paths active then the COMPONENT will be held
504 * with bias _ON and should not be suspended.
505 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000506 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200507 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000509 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000510 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000511 * bias off then being in STANDBY
512 * means it's doing something,
513 * otherwise fall through.
514 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200515 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000516 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200517 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000518 break;
519 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500520 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 case SND_SOC_BIAS_OFF:
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900523 snd_soc_component_suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000524 if (component->regmap)
525 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800526 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000527 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 break;
529 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000530 dev_dbg(component->dev,
531 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 break;
533 }
534 }
535 }
536
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000537 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500538 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539
Mengdong Lin1a497982015-11-18 02:34:11 -0500540 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 continue;
542
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900543 if (cpu_dai->driver->bus_control)
544 snd_soc_dai_suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800545
546 /* deactivate pins to sleep state */
547 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548 }
549
Mark Brown87506542008-11-18 20:50:34 +0000550 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000551 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
553 return 0;
554}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000555EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200556
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200557/*
558 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100559 * setting our codec back up, which can be very slow on I2C
560 */
561static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200564 container_of(work, struct snd_soc_card,
565 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500566 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000567 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200570 /*
571 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100572 * so userspace apps are blocked from touching us
573 */
574
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000575 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100576
Mark Brown9949788b2010-05-07 20:24:05 +0100577 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100579
Mark Brown87506542008-11-18 20:50:34 +0000580 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000581 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100583 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000584 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500585 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100586
Mengdong Lin1a497982015-11-18 02:34:11 -0500587 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100588 continue;
589
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900590 if (cpu_dai->driver->bus_control)
591 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 }
593
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000594 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000595 if (component->suspended) {
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900596 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100597 }
598 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000600 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100601
Mengdong Lin1a497982015-11-18 02:34:11 -0500602 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100603 continue;
604
Mengdong Lin1a497982015-11-18 02:34:11 -0500605 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000606 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608
Mengdong Lin1a497982015-11-18 02:34:11 -0500609 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000610 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800611 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 }
613
Mark Brown3ff3f642008-05-19 12:32:25 +0200614 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000615 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000616 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100617
Mengdong Lin1a497982015-11-18 02:34:11 -0500618 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100619 continue;
620
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000621 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900622 if (dai->playback_active)
623 snd_soc_dai_digital_mute(dai, 0,
624 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200625 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626 }
627
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000628 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500629 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100630
Mengdong Lin1a497982015-11-18 02:34:11 -0500631 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100632 continue;
633
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900634 if (!cpu_dai->driver->bus_control)
635 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 }
637
Mark Brown87506542008-11-18 20:50:34 +0000638 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000639 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000641 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100642
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200643 /* Recheck all endpoints too, their state is affected by suspend */
644 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700645 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530646
647 /* userspace can access us now we are back as we were before */
648 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100649}
650
651/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000652int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100653{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000654 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100655 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500656 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900657 struct snd_soc_dai *codec_dai;
658 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200659
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200660 /* If the card is not initialized yet there is nothing to do */
661 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800662 return 0;
663
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000665 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200667
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800668 if (cpu_dai->active)
669 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200670
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900671 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200672 if (codec_dai->active)
673 pinctrl_pm_select_default_state(codec_dai->dev);
674 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800675 }
676
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100677 /*
678 * DAIs that also act as the control bus master might have other drivers
679 * hanging off them so need to resume immediately. Other drivers don't
680 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100681 * due to I/O costs and anti-pop so handle them out of line.
682 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000683 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200685
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100686 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600687 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100688 if (bus_control) {
689 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600690 soc_resume_deferred(&card->deferred_resume_work);
691 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000692 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600693 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000694 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100695 }
Andy Green6ed25972008-06-13 16:24:05 +0100696
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697 return 0;
698}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000699EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000701#define snd_soc_suspend NULL
702#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703#endif
704
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100705static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800706};
707
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900708static struct device_node
709*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100710{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900711 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100712
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900713 of_node = component->dev->of_node;
714 if (!of_node && component->dev->parent)
715 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100716
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900717 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100718}
719
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000720static int snd_soc_is_matching_component(
721 const struct snd_soc_dai_link_component *dlc,
722 struct snd_soc_component *component)
723{
724 struct device_node *component_of_node;
725
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900726 if (!dlc)
727 return 0;
728
729 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000730
731 if (dlc->of_node && component_of_node != dlc->of_node)
732 return 0;
733 if (dlc->name && strcmp(component->name, dlc->name))
734 return 0;
735
736 return 1;
737}
738
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900740 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741{
742 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743
744 lockdep_assert_held(&client_mutex);
745
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900746 /*
747 * NOTE
748 *
749 * It returns *1st* found component, but some driver
750 * has few components by same of_node/name
751 * ex)
752 * CPU component and generic DMAEngine component
753 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900754 for_each_component(component)
755 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757
758 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100759}
760
Mengdong Linfbb88b52016-04-22 12:25:33 +0800761/**
762 * snd_soc_find_dai - Find a registered DAI
763 *
Jeffy Chen49584712017-08-22 15:57:21 +0800764 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800765 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700766 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800767 * find the DAI of the same name. The component's of_node and name
768 * should also match if being specified.
769 *
770 * Return: pointer of DAI, or NULL if not found.
771 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800772struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200773 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100774{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200775 struct snd_soc_component *component;
776 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100777
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100778 lockdep_assert_held(&client_mutex);
779
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200780 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000781 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000782 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200783 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000784 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800785 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800786 && (!dai->driver->name
787 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100788 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100789
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200790 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100791 }
792 }
793
794 return NULL;
795}
Mengdong Lin305e9022016-04-19 13:12:25 +0800796EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100797
Mengdong Lin17fb17552016-11-03 01:04:12 +0800798/**
799 * snd_soc_find_dai_link - Find a DAI link
800 *
801 * @card: soc card
802 * @id: DAI link ID to match
803 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000804 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800805 *
806 * This function will search all existing DAI links of the soc card to
807 * find the link of the same ID. Since DAI links may not have their
808 * unique ID, so name and stream name should also match if being
809 * specified.
810 *
811 * Return: pointer of DAI link, or NULL if not found.
812 */
813struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
814 int id, const char *name,
815 const char *stream_name)
816{
817 struct snd_soc_dai_link *link, *_link;
818
819 lockdep_assert_held(&client_mutex);
820
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000821 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800822 if (link->id != id)
823 continue;
824
825 if (name && (!link->name || strcmp(name, link->name)))
826 continue;
827
828 if (stream_name && (!link->stream_name
829 || strcmp(stream_name, link->stream_name)))
830 continue;
831
832 return link;
833 }
834
835 return NULL;
836}
837EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
838
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800839static bool soc_is_dai_link_bound(struct snd_soc_card *card,
840 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800842 struct snd_soc_pcm_runtime *rtd;
843
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000844 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800845 if (rtd->dai_link == dai_link)
846 return true;
847 }
848
849 return false;
850}
851
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500852static int soc_bind_dai_link(struct snd_soc_card *card,
853 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854{
Mengdong Lin1a497982015-11-18 02:34:11 -0500855 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +0200856 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000857 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200858 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859
Liam Girdwooda655de82018-07-02 16:59:54 +0100860 if (dai_link->ignore)
861 return 0;
862
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500863 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000864
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800865 if (soc_is_dai_link_bound(card, dai_link)) {
866 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
867 dai_link->name);
868 return 0;
869 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200870
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530871 rtd = soc_new_pcm_runtime(card, dai_link);
872 if (!rtd)
873 return -ENOMEM;
874
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900875 /* FIXME: we need multi CPU support in the future */
876 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100878 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900879 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500880 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000881 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000882 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000883
Benoit Cousson88bd8702014-07-08 23:19:34 +0200884 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900885 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +0200886 for_each_link_codecs(dai_link, i, codec) {
887 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900888 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100889 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +0200890 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500891 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200892 }
Jerome Brunet34614732019-06-27 14:13:50 +0200893
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900894 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000895 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100896
Benoit Cousson88bd8702014-07-08 23:19:34 +0200897 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900898 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100899
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900900 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +0200901 for_each_link_platforms(dai_link, i, platform) {
902 for_each_component(component) {
903 if (!snd_soc_is_matching_component(platform, component))
904 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000905
Jerome Brunet34614732019-06-27 14:13:50 +0200906 snd_soc_rtdcom_add(rtd, component);
907 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000908 }
909
Mengdong Lin1a497982015-11-18 02:34:11 -0500910 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500912
913_err_defer:
914 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200915 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916}
917
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900918static void soc_cleanup_component(struct snd_soc_component *component)
919{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200920 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900921 list_del(&component->card_list);
922 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
923 soc_cleanup_component_debugfs(component);
924 component->card = NULL;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900925 snd_soc_component_module_put_when_close(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900926}
927
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200928static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600929{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200930 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200931 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600932
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000933 if (component->driver->remove)
934 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600935
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900936 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600937}
938
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200939static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200940{
941 int err;
942
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600943 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000944 dai->driver->remove_order != order)
945 return;
946
Kuninori Morimotodcdab582019-07-22 10:35:05 +0900947 err = snd_soc_dai_remove(dai);
948 if (err < 0)
949 dev_err(dai->dev,
950 "ASoC: failed to remove %s: %d\n",
951 dai->name, err);
952
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000953 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100954}
955
Mengdong Lin1a497982015-11-18 02:34:11 -0500956static void soc_remove_link_dais(struct snd_soc_card *card,
957 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200959 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000960 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961
962 /* unregister the rtd device */
963 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800964 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 rtd->dev_registered = 0;
966 }
967
968 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000969 for_each_rtd_codec_dai(rtd, i, codec_dai)
970 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200972 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973}
974
Mengdong Lin1a497982015-11-18 02:34:11 -0500975static void soc_remove_link_components(struct snd_soc_card *card,
976 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600977{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200978 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000979 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600980
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000981 for_each_rtdcom(rtd, rtdcom) {
982 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600983
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200984 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200985 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600986 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600987}
988
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900989static void soc_remove_dai_links(struct snd_soc_card *card)
990{
Mengdong Lin1a497982015-11-18 02:34:11 -0500991 int order;
992 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800993 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900994
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +0000995 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000996 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -0500997 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +0100998 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001000 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001001 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001002 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001003 }
1004
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001005 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001006 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1007 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1008 link->name);
1009
1010 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001011 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001012}
1013
Mengdong Lin923c5e612015-11-23 11:01:49 -05001014static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001015 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001016{
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001017 int i;
Jerome Brunet34614732019-06-27 14:13:50 +02001018 struct snd_soc_dai_link_component *codec, *platform;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001019
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001020 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001021 /*
1022 * Codec must be specified by 1 of name or OF node,
1023 * not both or neither.
1024 */
Jerome Brunet34614732019-06-27 14:13:50 +02001025 if (!!codec->name == !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001026 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1027 link->name);
1028 return -EINVAL;
1029 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001030
Mengdong Lin923c5e612015-11-23 11:01:49 -05001031 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001032 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001033 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1034 link->name);
1035 return -EINVAL;
1036 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001037
1038 /*
1039 * Defer card registration if codec component is not added to
1040 * component list.
1041 */
1042 if (!soc_find_component(codec))
1043 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001044 }
1045
Jerome Brunet34614732019-06-27 14:13:50 +02001046 for_each_link_platforms(link, i, platform) {
1047 /*
1048 * Platform may be specified by either name or OF node, but it
1049 * can be left unspecified, then no components will be inserted
1050 * in the rtdcom list
1051 */
1052 if (!!platform->name == !!platform->of_node) {
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001053 dev_err(card->dev,
Jerome Brunet34614732019-06-27 14:13:50 +02001054 "ASoC: Neither/both platform name/of_node are set for %s\n",
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001055 link->name);
1056 return -EINVAL;
1057 }
1058
1059 /*
Jerome Brunet34614732019-06-27 14:13:50 +02001060 * Defer card registration if platform component is not added to
1061 * component list.
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001062 */
Jerome Brunet34614732019-06-27 14:13:50 +02001063 if (!soc_find_component(platform))
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001064 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001065 }
1066
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001067 /* FIXME */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001068 if (link->num_cpus > 1) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001069 dev_err(card->dev,
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001070 "ASoC: multi cpu is not yet supported %s\n",
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001071 link->name);
1072 return -EINVAL;
1073 }
1074
Mengdong Lin923c5e612015-11-23 11:01:49 -05001075 /*
Mengdong Lin923c5e612015-11-23 11:01:49 -05001076 * CPU device may be specified by either name or OF node, but
1077 * can be left unspecified, and will be matched based on DAI
1078 * name alone..
1079 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001080 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001081 dev_err(card->dev,
1082 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1083 link->name);
1084 return -EINVAL;
1085 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301086
1087 /*
1088 * Defer card registartion if cpu dai component is not added to
1089 * component list.
1090 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001091 if ((link->cpus->of_node || link->cpus->name) &&
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001092 !soc_find_component(link->cpus))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301093 return -EPROBE_DEFER;
1094
Mengdong Lin923c5e612015-11-23 11:01:49 -05001095 /*
1096 * At least one of CPU DAI name or CPU device name/node must be
1097 * specified
1098 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001099 if (!link->cpus->dai_name &&
1100 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001101 dev_err(card->dev,
1102 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1103 link->name);
1104 return -EINVAL;
1105 }
1106
1107 return 0;
1108}
1109
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001110void snd_soc_disconnect_sync(struct device *dev)
1111{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001112 struct snd_soc_component *component =
1113 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001114
1115 if (!component || !component->card)
1116 return;
1117
1118 snd_card_disconnect_sync(component->card->snd_card);
1119}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001120EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001121
Mengdong Linf8f80362015-12-02 14:11:22 +08001122/**
1123 * snd_soc_add_dai_link - Add a DAI link dynamically
1124 * @card: The ASoC card to which the DAI link is added
1125 * @dai_link: The new DAI link to add
1126 *
1127 * This function adds a DAI link to the ASoC card's link list.
1128 *
1129 * Note: Topology can use this API to add DAI links when probing the
1130 * topology component. And machine drivers can still define static
1131 * DAI links in dai_link array.
1132 */
1133int snd_soc_add_dai_link(struct snd_soc_card *card,
1134 struct snd_soc_dai_link *dai_link)
1135{
1136 if (dai_link->dobj.type
1137 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1138 dev_err(card->dev, "Invalid dai link type %d\n",
1139 dai_link->dobj.type);
1140 return -EINVAL;
1141 }
1142
1143 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001144 /*
1145 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001146 * on the link created by topology.
1147 */
1148 if (dai_link->dobj.type && card->add_dai_link)
1149 card->add_dai_link(card, dai_link);
1150
Mengdong Linf8f80362015-12-02 14:11:22 +08001151 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001152
1153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1156
1157/**
1158 * snd_soc_remove_dai_link - Remove a DAI link from the list
1159 * @card: The ASoC card that owns the link
1160 * @dai_link: The DAI link to remove
1161 *
1162 * This function removes a DAI link from the ASoC card's link list.
1163 *
1164 * For DAI links previously added by topology, topology should
1165 * remove them by using the dobj embedded in the link.
1166 */
1167void snd_soc_remove_dai_link(struct snd_soc_card *card,
1168 struct snd_soc_dai_link *dai_link)
1169{
1170 struct snd_soc_dai_link *link, *_link;
1171
1172 if (dai_link->dobj.type
1173 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1174 dev_err(card->dev, "Invalid dai link type %d\n",
1175 dai_link->dobj.type);
1176 return;
1177 }
1178
1179 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001180 /*
1181 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001182 * on the link created by topology.
1183 */
1184 if (dai_link->dobj.type && card->remove_dai_link)
1185 card->remove_dai_link(card, dai_link);
1186
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001187 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001188 if (link == dai_link) {
1189 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001190 return;
1191 }
1192 }
1193}
1194EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1195
Jerome Brunetaefba452018-07-13 14:50:43 +02001196static void soc_set_of_name_prefix(struct snd_soc_component *component)
1197{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001198 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001199 const char *str;
1200 int ret;
1201
Jerome Brunetaefba452018-07-13 14:50:43 +02001202 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1203 &str);
1204 if (!ret)
1205 component->name_prefix = str;
1206}
1207
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001208static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001209 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001210{
1211 int i;
1212
Jerome Brunetaefba452018-07-13 14:50:43 +02001213 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001214 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001215 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001216
1217 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001218 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001219 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001220 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001221 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001222 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001223 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001224
1225 /*
1226 * If there is no configuration table or no match in the table,
1227 * check if a prefix is provided in the node
1228 */
1229 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001230}
1231
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001232static int soc_probe_component(struct snd_soc_card *card,
1233 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001234{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001235 struct snd_soc_dapm_context *dapm =
1236 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001237 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001238 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001239
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001240 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001241 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001242
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001243 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001244 if (component->card != card) {
1245 dev_err(component->dev,
1246 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1247 card->name, component->card->name);
1248 return -ENODEV;
1249 }
1250 return 0;
1251 }
1252
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +09001253 ret = snd_soc_component_module_get_when_probe(component);
1254 if (ret < 0)
1255 return ret;
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001256
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001257 component->card = card;
1258 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001259 INIT_LIST_HEAD(&component->card_list);
1260 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001261 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001262
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001263 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001264
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001265 if (component->driver->dapm_widgets) {
1266 ret = snd_soc_dapm_new_controls(dapm,
1267 component->driver->dapm_widgets,
1268 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001269
1270 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001271 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001272 "Failed to create new controls %d\n", ret);
1273 goto err_probe;
1274 }
1275 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001276
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001277 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001278 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001279 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001280 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001281 "Failed to create DAI widgets %d\n", ret);
1282 goto err_probe;
1283 }
1284 }
Mark Brown888df392012-02-16 19:37:51 -08001285
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001286 if (component->driver->probe) {
1287 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001289 dev_err(component->dev,
1290 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001291 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001292 }
1293 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001294 WARN(dapm->idle_bias_off &&
1295 dapm->bias_level != SND_SOC_BIAS_OFF,
1296 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1297 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001298
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001299 /* machine specific init */
1300 if (component->init) {
1301 ret = component->init(component);
1302 if (ret < 0) {
1303 dev_err(component->dev,
1304 "Failed to do machine specific init %d\n", ret);
1305 goto err_probe;
1306 }
1307 }
1308
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001309 if (component->driver->controls)
1310 snd_soc_add_component_controls(component,
1311 component->driver->controls,
1312 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001313 if (component->driver->dapm_routes)
1314 snd_soc_dapm_add_routes(dapm,
1315 component->driver->dapm_routes,
1316 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001317
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001318 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001319 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001320 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321
Jarkko Nikula70d293312011-01-27 16:24:22 +02001322err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001323 if (ret < 0)
1324 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001325
1326 return ret;
1327}
1328
Mark Brown36ae1a92012-01-06 17:12:45 -08001329static void rtd_release(struct device *dev)
1330{
1331 kfree(dev);
1332}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001333
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001334static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1335 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001336{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 int ret = 0;
1338
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001340 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1341 if (!rtd->dev)
1342 return -ENOMEM;
1343 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001344 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001345 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001346 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001347 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001348 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001349 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001350 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1351 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1352 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1353 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001354 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001356 /* calling put_device() here to free the rtd->dev */
1357 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001358 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001359 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001360 return ret;
1361 }
1362 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001363 return 0;
1364}
1365
Mengdong Lin1a497982015-11-18 02:34:11 -05001366static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001367 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001368{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001369 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001370 struct snd_soc_rtdcom_list *rtdcom;
1371 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001372
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001373 for_each_rtdcom(rtd, rtdcom) {
1374 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001375
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001376 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001377 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001378 if (ret < 0)
1379 return ret;
1380 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001381 }
1382
Stephen Warren62ae68f2012-06-08 12:34:23 -06001383 return 0;
1384}
1385
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001386static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001387{
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001388 int ret;
1389
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001390 if (dai->probed ||
1391 dai->driver->probe_order != order)
1392 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001393
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001394 ret = snd_soc_dai_probe(dai);
1395 if (ret < 0) {
1396 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1397 dai->name, ret);
1398 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001399 }
1400
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001401 dai->probed = 1;
1402
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001403 return 0;
1404}
1405
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001406static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1407 struct snd_soc_pcm_runtime *rtd)
1408{
1409 int i, ret = 0;
1410
1411 for (i = 0; i < num_dais; ++i) {
1412 struct snd_soc_dai_driver *drv = dais[i]->driver;
1413
Rohit kumarde17f142018-11-01 18:08:49 +05301414 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001415 ret = drv->pcm_new(rtd, dais[i]);
1416 if (ret < 0) {
1417 dev_err(dais[i]->dev,
1418 "ASoC: Failed to bind %s with pcm device\n",
1419 dais[i]->name);
1420 return ret;
1421 }
1422 }
1423
1424 return 0;
1425}
1426
Mengdong Lin1a497982015-11-18 02:34:11 -05001427static int soc_probe_link_dais(struct snd_soc_card *card,
1428 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001429{
Mengdong Lin1a497982015-11-18 02:34:11 -05001430 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001431 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001432 struct snd_soc_rtdcom_list *rtdcom;
1433 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001434 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001435 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001436
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001437 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001438 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001439
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440 /* set default power off timeout */
1441 rtd->pmdown_time = pmdown_time;
1442
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001443 ret = soc_probe_dai(cpu_dai, order);
1444 if (ret)
1445 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001446
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001447 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001448 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1449 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001450 if (ret)
1451 return ret;
1452 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001453
Liam Girdwood0168bf02011-06-07 16:08:05 +01001454 /* complete DAI probe during last probe */
1455 if (order != SND_SOC_COMP_ORDER_LAST)
1456 return 0;
1457
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001458 /* do machine specific initialization */
1459 if (dai_link->init) {
1460 ret = dai_link->init(rtd);
1461 if (ret < 0) {
1462 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1463 dai_link->name, ret);
1464 return ret;
1465 }
1466 }
1467
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001468 if (dai_link->dai_fmt) {
1469 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1470 if (ret)
1471 return ret;
1472 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001473
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001474 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001475 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001477
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001478#ifdef CONFIG_DEBUG_FS
1479 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001480 if (dai_link->dynamic)
1481 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001482#endif
1483
Liam Girdwooda655de82018-07-02 16:59:54 +01001484 num = rtd->num;
1485
1486 /*
1487 * most drivers will register their PCMs using DAI link ordering but
1488 * topology based drivers can use the DAI link id field to set PCM
1489 * device number and then use rtd + a base offset of the BEs.
1490 */
1491 for_each_rtdcom(rtd, rtdcom) {
1492 component = rtdcom->component;
1493
1494 if (!component->driver->use_dai_pcm_id)
1495 continue;
1496
1497 if (rtd->dai_link->no_pcm)
1498 num += component->driver->be_pcm_base;
1499 else
1500 num = rtd->dai_link->id;
1501 }
1502
Kuninori Morimotob423c422019-07-22 10:35:29 +09001503 /* create compress_device if possible */
1504 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1505 if (ret != -ENOTSUPP) {
1506 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001507 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301508 dai_link->stream_name);
Kuninori Morimotob423c422019-07-22 10:35:29 +09001509 return ret;
1510 }
1511
Jerome Bruneta3420312019-07-25 18:59:47 +02001512 /* create the pcm */
1513 ret = soc_new_pcm(rtd, num);
1514 if (ret < 0) {
1515 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1516 dai_link->stream_name, ret);
1517 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518 }
Jerome Bruneta3420312019-07-25 18:59:47 +02001519 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1520 if (ret < 0)
1521 return ret;
1522 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1523 rtd->num_codecs, rtd);
1524 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525}
1526
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001527static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001528{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001529 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001530 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001531 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001532
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001533 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1534 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001535 dlc.name = aux_dev->codec_name;
1536 dlc.of_node = aux_dev->codec_of_node;
1537 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001538 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001539 if (dlc.of_node)
1540 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001541 goto err_defer;
1542 }
1543 } else if (aux_dev->name) {
1544 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001545 dlc.name = aux_dev->name;
1546 dlc.of_node = NULL;
1547 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001548 if (!component)
1549 goto err_defer;
1550 } else {
1551 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1552 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001553 }
1554
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001555 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001556 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001557
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001558 return 0;
1559
1560err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001561 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001562 return -EPROBE_DEFER;
1563}
1564
1565static int soc_probe_aux_devices(struct snd_soc_card *card)
1566{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001567 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001568 int order;
1569 int ret;
1570
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001571 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001572 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001573 if (comp->driver->probe_order == order) {
1574 ret = soc_probe_component(card, comp);
1575 if (ret < 0) {
1576 dev_err(card->dev,
1577 "ASoC: failed to probe aux component %s %d\n",
1578 comp->name, ret);
1579 return ret;
1580 }
1581 }
1582 }
1583 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001584
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001585 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001586}
1587
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001588static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001589{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001590 struct snd_soc_component *comp, *_comp;
1591 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001592
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001593 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001594 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001595 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001596
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001597 if (comp->driver->remove_order == order) {
1598 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001599 /* remove it from the card's aux_comp_list */
1600 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001601 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001602 }
1603 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001604}
1605
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001606/**
1607 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1608 * @rtd: The runtime for which the DAI link format should be changed
1609 * @dai_fmt: The new DAI link format
1610 *
1611 * This function updates the DAI link format for all DAIs connected to the DAI
1612 * link for the specified runtime.
1613 *
1614 * Note: For setups with a static format set the dai_fmt field in the
1615 * corresponding snd_dai_link struct instead of using this function.
1616 *
1617 * Returns 0 on success, otherwise a negative error code.
1618 */
1619int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1620 unsigned int dai_fmt)
1621{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001622 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001623 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001624 unsigned int i;
1625 int ret;
1626
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001627 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001628 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1629 if (ret != 0 && ret != -ENOTSUPP) {
1630 dev_warn(codec_dai->dev,
1631 "ASoC: Failed to set DAI format: %d\n", ret);
1632 return ret;
1633 }
1634 }
1635
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001636 /*
1637 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1638 * the component which has non_legacy_dai_naming is Codec
1639 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001640 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001641 unsigned int inv_dai_fmt;
1642
1643 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1644 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1645 case SND_SOC_DAIFMT_CBM_CFM:
1646 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1647 break;
1648 case SND_SOC_DAIFMT_CBM_CFS:
1649 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1650 break;
1651 case SND_SOC_DAIFMT_CBS_CFM:
1652 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1653 break;
1654 case SND_SOC_DAIFMT_CBS_CFS:
1655 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1656 break;
1657 }
1658
1659 dai_fmt = inv_dai_fmt;
1660 }
1661
1662 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1663 if (ret != 0 && ret != -ENOTSUPP) {
1664 dev_warn(cpu_dai->dev,
1665 "ASoC: Failed to set DAI format: %d\n", ret);
1666 return ret;
1667 }
1668
1669 return 0;
1670}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001671EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001672
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001673#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001674/*
1675 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001676 * separate different DMI fields in the card long name. Only number and
1677 * alphabet characters and a few separator characters are kept.
1678 */
1679static void cleanup_dmi_name(char *name)
1680{
1681 int i, j = 0;
1682
1683 for (i = 0; name[i]; i++) {
1684 if (isalnum(name[i]) || (name[i] == '.')
1685 || (name[i] == '_'))
1686 name[j++] = name[i];
1687 else if (name[i] == '-')
1688 name[j++] = '_';
1689 }
1690
1691 name[j] = '\0';
1692}
1693
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001694/*
1695 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001696 * in the black list.
1697 */
1698static int is_dmi_valid(const char *field)
1699{
1700 int i = 0;
1701
1702 while (dmi_blacklist[i]) {
1703 if (strstr(field, dmi_blacklist[i]))
1704 return 0;
1705 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001706 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001707
1708 return 1;
1709}
1710
Liam Girdwood345233d2017-01-14 16:13:02 +08001711/**
1712 * snd_soc_set_dmi_name() - Register DMI names to card
1713 * @card: The card to register DMI names
1714 * @flavour: The flavour "differentiator" for the card amongst its peers.
1715 *
1716 * An Intel machine driver may be used by many different devices but are
1717 * difficult for userspace to differentiate, since machine drivers ususally
1718 * use their own name as the card short name and leave the card long name
1719 * blank. To differentiate such devices and fix bugs due to lack of
1720 * device-specific configurations, this function allows DMI info to be used
1721 * as the sound card long name, in the format of
1722 * "vendor-product-version-board"
1723 * (Character '-' is used to separate different DMI fields here).
1724 * This will help the user space to load the device-specific Use Case Manager
1725 * (UCM) configurations for the card.
1726 *
1727 * Possible card long names may be:
1728 * DellInc.-XPS139343-01-0310JH
1729 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1730 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1731 *
1732 * This function also supports flavoring the card longname to provide
1733 * the extra differentiation, like "vendor-product-version-board-flavor".
1734 *
1735 * We only keep number and alphabet characters and a few separator characters
1736 * in the card long name since UCM in the user space uses the card long names
1737 * as card configuration directory names and AudoConf cannot support special
1738 * charactors like SPACE.
1739 *
1740 * Returns 0 on success, otherwise a negative error code.
1741 */
1742int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1743{
1744 const char *vendor, *product, *product_version, *board;
1745 size_t longname_buf_size = sizeof(card->snd_card->longname);
1746 size_t len;
1747
1748 if (card->long_name)
1749 return 0; /* long name already set by driver or from DMI */
1750
1751 /* make up dmi long name as: vendor.product.version.board */
1752 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001753 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001754 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1755 return 0;
1756 }
1757
1758 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1759 "%s", vendor);
1760 cleanup_dmi_name(card->dmi_longname);
1761
1762 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001763 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001764 len = strlen(card->dmi_longname);
1765 snprintf(card->dmi_longname + len,
1766 longname_buf_size - len,
1767 "-%s", product);
1768
1769 len++; /* skip the separator "-" */
1770 if (len < longname_buf_size)
1771 cleanup_dmi_name(card->dmi_longname + len);
1772
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001773 /*
1774 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001775 * name in the product version field
1776 */
1777 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001778 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001779 len = strlen(card->dmi_longname);
1780 snprintf(card->dmi_longname + len,
1781 longname_buf_size - len,
1782 "-%s", product_version);
1783
1784 len++;
1785 if (len < longname_buf_size)
1786 cleanup_dmi_name(card->dmi_longname + len);
1787 }
1788 }
1789
1790 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001791 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001792 len = strlen(card->dmi_longname);
1793 snprintf(card->dmi_longname + len,
1794 longname_buf_size - len,
1795 "-%s", board);
1796
1797 len++;
1798 if (len < longname_buf_size)
1799 cleanup_dmi_name(card->dmi_longname + len);
1800 } else if (!product) {
1801 /* fall back to using legacy name */
1802 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1803 return 0;
1804 }
1805
1806 /* Add flavour to dmi long name */
1807 if (flavour) {
1808 len = strlen(card->dmi_longname);
1809 snprintf(card->dmi_longname + len,
1810 longname_buf_size - len,
1811 "-%s", flavour);
1812
1813 len++;
1814 if (len < longname_buf_size)
1815 cleanup_dmi_name(card->dmi_longname + len);
1816 }
1817
1818 /* set the card long name */
1819 card->long_name = card->dmi_longname;
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001824#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001825
Liam Girdwooda655de82018-07-02 16:59:54 +01001826static void soc_check_tplg_fes(struct snd_soc_card *card)
1827{
1828 struct snd_soc_component *component;
1829 const struct snd_soc_component_driver *comp_drv;
1830 struct snd_soc_dai_link *dai_link;
1831 int i;
1832
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001833 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001834
1835 /* does this component override FEs ? */
1836 if (!component->driver->ignore_machine)
1837 continue;
1838
1839 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001840 if (!strcmp(component->driver->ignore_machine,
1841 card->dev->driver->name))
1842 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001843 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001844 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001845 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001846match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001847 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001848 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001849
1850 /* ignore this FE */
1851 if (dai_link->dynamic) {
1852 dai_link->ignore = true;
1853 continue;
1854 }
1855
1856 dev_info(card->dev, "info: override FE DAI link %s\n",
1857 card->dai_link[i].name);
1858
1859 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001860 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001861 dev_err(card->dev, "init platform error");
1862 continue;
1863 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001864 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001865
1866 /* convert non BE into BE */
1867 dai_link->no_pcm = 1;
1868
1869 /* override any BE fixups */
1870 dai_link->be_hw_params_fixup =
1871 component->driver->be_hw_params_fixup;
1872
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001873 /*
1874 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001875 * dai link name if it's NULL to help bind widgets.
1876 */
1877 if (!dai_link->stream_name)
1878 dai_link->stream_name = dai_link->name;
1879 }
1880
1881 /* Inform userspace we are using alternate topology */
1882 if (component->driver->topology_name_prefix) {
1883
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001884 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001885 if (!card->topology_shortname_created) {
1886 comp_drv = component->driver;
1887
1888 snprintf(card->topology_shortname, 32, "%s-%s",
1889 comp_drv->topology_name_prefix,
1890 card->name);
1891 card->topology_shortname_created = true;
1892 }
1893
1894 /* use topology shortname */
1895 card->name = card->topology_shortname;
1896 }
1897 }
1898}
1899
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001900static int soc_cleanup_card_resources(struct snd_soc_card *card)
1901{
1902 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001903 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001904 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001905 card->snd_card = NULL;
1906 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001907
1908 /* remove and free each DAI */
1909 soc_remove_dai_links(card);
1910 soc_remove_pcm_runtimes(card);
1911
1912 /* remove auxiliary devices */
1913 soc_remove_aux_devices(card);
1914
1915 snd_soc_dapm_free(&card->dapm);
1916 soc_cleanup_card_debugfs(card);
1917
1918 /* remove the card */
1919 if (card->remove)
1920 card->remove(card);
1921
1922 return 0;
1923}
1924
Mark Brownb19e6e72012-03-14 21:18:39 +00001925static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001926{
Mengdong Lin1a497982015-11-18 02:34:11 -05001927 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001928 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001929 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001930
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001931 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001932 for_each_card_prelinks(card, i, dai_link) {
1933 ret = soc_init_dai_link(card, dai_link);
1934 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001935 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1936 dai_link->name, ret);
1937 mutex_unlock(&client_mutex);
1938 return ret;
1939 }
1940 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001941 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001943 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1944 card->dapm.dev = card->dev;
1945 card->dapm.card = card;
1946 list_add(&card->dapm.list, &card->dapm_list);
1947
Liam Girdwooda655de82018-07-02 16:59:54 +01001948 /* check whether any platform is ignore machine FE and using topology */
1949 soc_check_tplg_fes(card);
1950
Mark Brownb19e6e72012-03-14 21:18:39 +00001951 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001952 for_each_card_prelinks(card, i, dai_link) {
1953 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001954 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001955 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001956 }
1957
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001958 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001959 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001960 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001961 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001962 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001963 }
1964
Mengdong Linf8f80362015-12-02 14:11:22 +08001965 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001966 for_each_card_prelinks(card, i, dai_link)
1967 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001970 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 card->owner, 0, &card->snd_card);
1972 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001973 dev_err(card->dev,
1974 "ASoC: can't create sound card for card %s: %d\n",
1975 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001976 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001979 soc_init_card_debugfs(card);
1980
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001981#ifdef CONFIG_DEBUG_FS
1982 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1983#endif
1984
Mark Brown88ee1c62011-02-02 10:43:26 +00001985#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001986 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001987 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001988#endif
Andy Green6ed25972008-06-13 16:24:05 +01001989
Mark Brown9a841eb2011-04-12 17:51:37 -07001990 if (card->dapm_widgets)
1991 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1992 card->num_dapm_widgets);
1993
Nicolin Chenf23e8602015-02-14 17:22:49 -08001994 if (card->of_dapm_widgets)
1995 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1996 card->num_of_dapm_widgets);
1997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 /* initialise the sound card only once */
1999 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002000 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002001 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002002 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002003 }
2004
Stephen Warren62ae68f2012-06-08 12:34:23 -06002005 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002006 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002007 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002008 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002009 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002010 dev_err(card->dev,
2011 "ASoC: failed to instantiate card %d\n",
2012 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002013 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002014 }
2015 }
2016 }
2017
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002018 /* probe auxiliary components */
2019 ret = soc_probe_aux_devices(card);
2020 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002021 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002022
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002023 /*
2024 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002025 * Components with topology may bring new DAIs and DAI links.
2026 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002027 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002028 if (soc_is_dai_link_bound(card, dai_link))
2029 continue;
2030
2031 ret = soc_init_dai_link(card, dai_link);
2032 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002033 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002034 ret = soc_bind_dai_link(card, dai_link);
2035 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002036 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002037 }
2038
Stephen Warren62ae68f2012-06-08 12:34:23 -06002039 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002040 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002041 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002042 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002043 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002044 dev_err(card->dev,
2045 "ASoC: failed to instantiate card %d\n",
2046 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002047 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002048 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002049 }
2050 }
2051
Mark Brown888df392012-02-16 19:37:51 -08002052 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002053 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002054
Mark Brownb7af1da2011-04-07 19:18:44 +09002055 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002056 snd_soc_add_card_controls(card, card->controls,
2057 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002058
Mark Brownb8ad29d2011-03-02 18:35:51 +00002059 if (card->dapm_routes)
2060 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2061 card->num_dapm_routes);
2062
Nicolin Chenf23e8602015-02-14 17:22:49 -08002063 if (card->of_dapm_routes)
2064 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2065 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002066
Takashi Iwai861886d2017-04-24 08:54:42 +02002067 /* try to set some sane longname if DMI is available */
2068 snd_soc_set_dmi_name(card, NULL);
2069
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002071 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002072 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2073 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002074 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2075 "%s", card->driver_name ? card->driver_name : card->name);
2076 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2077 switch (card->snd_card->driver[i]) {
2078 case '_':
2079 case '-':
2080 case '\0':
2081 break;
2082 default:
2083 if (!isalnum(card->snd_card->driver[i]))
2084 card->snd_card->driver[i] = '_';
2085 break;
2086 }
2087 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088
Mark Brown28e9ad92011-03-02 18:36:34 +00002089 if (card->late_probe) {
2090 ret = card->late_probe(card);
2091 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002092 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002093 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002094 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002095 }
2096 }
2097
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002098 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002099
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002100 ret = snd_card_register(card->snd_card);
2101 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002102 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2103 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002104 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002105 }
2106
Mark Brown435c5e22008-12-04 15:32:53 +00002107 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002108 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002109 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002110
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002111probe_end:
2112 if (ret < 0)
2113 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002115 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002116 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117
Mark Brownb19e6e72012-03-14 21:18:39 +00002118 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002119}
2120
2121/* probes a new socdev */
2122static int soc_probe(struct platform_device *pdev)
2123{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002124 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002125
Vinod Koul70a7ca32011-01-14 19:22:48 +05302126 /*
2127 * no card, so machine driver should be registering card
2128 * we should not be here in that case so ret error
2129 */
2130 if (!card)
2131 return -EINVAL;
2132
Mark Brownfe4085e2012-03-02 13:07:41 +00002133 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002134 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002135 card->name);
2136
Mark Brown435c5e22008-12-04 15:32:53 +00002137 /* Bodge while we unpick instantiation */
2138 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002139
Mark Brown28d528c2012-08-09 18:45:23 +01002140 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141}
2142
2143/* removes a socdev */
2144static int soc_remove(struct platform_device *pdev)
2145{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002146 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147
Mark Brownc5af3a22008-11-28 13:29:45 +00002148 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002149 return 0;
2150}
2151
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002152int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002153{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002154 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002155 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002156
2157 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002158 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002159
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002160 /*
2161 * Flush out pmdown_time work - we actually do want to run it
2162 * now, we're shutting down so no imminent restart.
2163 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002164 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002165
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002166 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002167
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002168 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002169 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002170 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002171 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002172 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002173
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002174 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002175 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002176 pinctrl_pm_select_sleep_state(codec_dai->dev);
2177 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002178 }
2179
Mark Brown416356f2009-06-30 19:05:15 +01002180 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002181}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002182EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002183
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002184const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302185 .suspend = snd_soc_suspend,
2186 .resume = snd_soc_resume,
2187 .freeze = snd_soc_suspend,
2188 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002189 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302190 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002191};
Stephen Warrendeb26072011-04-05 19:35:30 -06002192EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002193
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194/* ASoC platform driver */
2195static struct platform_driver soc_driver = {
2196 .driver = {
2197 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002198 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 },
2200 .probe = soc_probe,
2201 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202};
2203
Mark Brown096e49d2009-07-05 15:12:22 +01002204/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205 * snd_soc_cnew - create new control
2206 * @_template: control template
2207 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002208 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002209 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002210 *
2211 * Create a new mixer control from a template control.
2212 *
2213 * Returns 0 for success, else error.
2214 */
2215struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002216 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002217 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002218{
2219 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002220 struct snd_kcontrol *kcontrol;
2221 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222
2223 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002224 template.index = 0;
2225
Mark Brownefb7ac32011-03-08 17:23:24 +00002226 if (!long_name)
2227 long_name = template.name;
2228
2229 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002230 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002231 if (!name)
2232 return NULL;
2233
Mark Brownefb7ac32011-03-08 17:23:24 +00002234 template.name = name;
2235 } else {
2236 template.name = long_name;
2237 }
2238
2239 kcontrol = snd_ctl_new1(&template, data);
2240
2241 kfree(name);
2242
2243 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244}
2245EXPORT_SYMBOL_GPL(snd_soc_cnew);
2246
Liam Girdwood022658b2012-02-03 17:43:09 +00002247static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2248 const struct snd_kcontrol_new *controls, int num_controls,
2249 const char *prefix, void *data)
2250{
2251 int err, i;
2252
2253 for (i = 0; i < num_controls; i++) {
2254 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002255
Liam Girdwood022658b2012-02-03 17:43:09 +00002256 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2257 control->name, prefix));
2258 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002259 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2260 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002261 return err;
2262 }
2263 }
2264
2265 return 0;
2266}
2267
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002268struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2269 const char *name)
2270{
2271 struct snd_card *card = soc_card->snd_card;
2272 struct snd_kcontrol *kctl;
2273
2274 if (unlikely(!name))
2275 return NULL;
2276
2277 list_for_each_entry(kctl, &card->controls, list)
2278 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2279 return kctl;
2280 return NULL;
2281}
2282EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2283
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002285 * snd_soc_add_component_controls - Add an array of controls to a component.
2286 *
2287 * @component: Component to add controls to
2288 * @controls: Array of controls to add
2289 * @num_controls: Number of elements in the array
2290 *
2291 * Return: 0 for success, else error.
2292 */
2293int snd_soc_add_component_controls(struct snd_soc_component *component,
2294 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2295{
2296 struct snd_card *card = component->card->snd_card;
2297
2298 return snd_soc_add_controls(card, component->dev, controls,
2299 num_controls, component->name_prefix, component);
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2302
2303/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002304 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2305 * Convenience function to add a list of controls.
2306 *
2307 * @soc_card: SoC card to add controls to
2308 * @controls: array of controls to add
2309 * @num_controls: number of elements in the array
2310 *
2311 * Return 0 for success, else error.
2312 */
2313int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2314 const struct snd_kcontrol_new *controls, int num_controls)
2315{
2316 struct snd_card *card = soc_card->snd_card;
2317
2318 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2319 NULL, soc_card);
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2322
2323/**
2324 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2325 * Convienience function to add a list of controls.
2326 *
2327 * @dai: DAI to add controls to
2328 * @controls: array of controls to add
2329 * @num_controls: number of elements in the array
2330 *
2331 * Return 0 for success, else error.
2332 */
2333int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2334 const struct snd_kcontrol_new *controls, int num_controls)
2335{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002336 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002337
2338 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2339 NULL, dai);
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2342
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002343static int snd_soc_bind_card(struct snd_soc_card *card)
2344{
2345 struct snd_soc_pcm_runtime *rtd;
2346 int ret;
2347
2348 ret = snd_soc_instantiate_card(card);
2349 if (ret != 0)
2350 return ret;
2351
2352 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002353 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002354 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2355 struct snd_soc_dai *codec_dai;
2356 int j;
2357
2358 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2359 if (!codec_dai->active)
2360 pinctrl_pm_select_sleep_state(codec_dai->dev);
2361 }
2362
2363 if (!cpu_dai->active)
2364 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2365 }
2366
2367 return ret;
2368}
2369
Mark Brownc5af3a22008-11-28 13:29:45 +00002370/**
2371 * snd_soc_register_card - Register a card with the ASoC core
2372 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002373 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002374 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002375 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302376int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002377{
2378 if (!card->name || !card->dev)
2379 return -EINVAL;
2380
Mark Browned77cc12011-05-03 18:25:34 +01002381 dev_set_drvdata(card->dev, card);
2382
Stephen Warren111c6412011-01-28 14:26:35 -07002383 snd_soc_initialize_card_lists(card);
2384
Mengdong Linf8f80362015-12-02 14:11:22 +08002385 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002386
Mengdong Lin1a497982015-11-18 02:34:11 -05002387 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002388 card->num_rtd = 0;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002389
Mark Browndb432b42011-10-03 21:06:40 +01002390 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002391 INIT_LIST_HEAD(&card->dobj_list);
Mark Browndee89c42008-11-18 22:11:38 +00002392 card->instantiated = 0;
2393 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002394 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002395 spin_lock_init(&card->dpcm_lock);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002396
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002397 return snd_soc_bind_card(card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002398}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302399EXPORT_SYMBOL_GPL(snd_soc_register_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002400
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002401static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2402{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002403 struct snd_soc_pcm_runtime *rtd;
2404 int order;
2405
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002406 if (card->instantiated) {
2407 card->instantiated = false;
2408 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002409 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002410
2411 /* remove all components used by DAI links on this card */
2412 for_each_comp_order(order) {
2413 for_each_card_rtds(card, rtd) {
2414 soc_remove_link_components(card, rtd, order);
2415 }
2416 }
2417
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002418 soc_cleanup_card_resources(card);
2419 if (!unregister)
2420 list_add(&card->list, &unbind_card_list);
2421 } else {
2422 if (unregister)
2423 list_del(&card->list);
2424 }
2425}
2426
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002427/**
2428 * snd_soc_unregister_card - Unregister a card with the ASoC core
2429 *
Mark Browndee89c42008-11-18 22:11:38 +00002430 * @card: Card to unregister
2431 *
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002432 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302433int snd_soc_unregister_card(struct snd_soc_card *card)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002434{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002435 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002436 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002437 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002438 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002439
2440 return 0;
2441}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302442EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002443
2444/*
2445 * Simplify DAI link configuration by removing ".-1" from device names
2446 * and sanitizing names.
2447 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002448static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002449{
2450 char *found, name[NAME_SIZE];
2451 int id1, id2;
2452
2453 if (dev_name(dev) == NULL)
2454 return NULL;
2455
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002456 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002457
2458 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002459 found = strstr(name, dev->driver->name);
2460 if (found) {
2461 /* get ID */
2462 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2463
2464 /* discard ID from name if ID == -1 */
2465 if (*id == -1)
2466 found[strlen(dev->driver->name)] = '\0';
2467 }
2468
2469 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002470 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002471 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2472 char tmp[NAME_SIZE];
2473
2474 /* create unique ID number from I2C addr and bus */
2475 *id = ((id1 & 0xffff) << 16) + id2;
2476
2477 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002478 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2479 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002480 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002481 } else
2482 *id = 0;
2483 }
2484
2485 return kstrdup(name, GFP_KERNEL);
2486}
2487
2488/*
2489 * Simplify DAI link naming for single devices with multiple DAIs by removing
2490 * any ".-1" and using the DAI name (instead of device name).
2491 */
2492static inline char *fmt_multiple_name(struct device *dev,
2493 struct snd_soc_dai_driver *dai_drv)
2494{
2495 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002496 dev_err(dev,
2497 "ASoC: error - multiple DAI %s registered with no name\n",
2498 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002499 return NULL;
2500 }
2501
2502 return kstrdup(dai_drv->name, GFP_KERNEL);
2503}
2504
2505/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002506 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002507 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002508 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002509 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002510static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002511{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002512 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002513
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002514 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002515 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2516 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002517 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002518 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002519 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002520 }
Mark Brown91151712008-11-30 23:31:24 +00002521}
Mark Brown91151712008-11-30 23:31:24 +00002522
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002523/* Create a DAI and add it to the component's DAI list */
2524static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2525 struct snd_soc_dai_driver *dai_drv,
2526 bool legacy_dai_naming)
2527{
2528 struct device *dev = component->dev;
2529 struct snd_soc_dai *dai;
2530
2531 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2532
2533 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2534 if (dai == NULL)
2535 return NULL;
2536
2537 /*
2538 * Back in the old days when we still had component-less DAIs,
2539 * instead of having a static name, component-less DAIs would
2540 * inherit the name of the parent device so it is possible to
2541 * register multiple instances of the DAI. We still need to keep
2542 * the same naming style even though those DAIs are not
2543 * component-less anymore.
2544 */
2545 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002546 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002547 dai->name = fmt_single_name(dev, &dai->id);
2548 } else {
2549 dai->name = fmt_multiple_name(dev, dai_drv);
2550 if (dai_drv->id)
2551 dai->id = dai_drv->id;
2552 else
2553 dai->id = component->num_dai;
2554 }
2555 if (dai->name == NULL) {
2556 kfree(dai);
2557 return NULL;
2558 }
2559
2560 dai->component = component;
2561 dai->dev = dev;
2562 dai->driver = dai_drv;
2563 if (!dai->driver->ops)
2564 dai->driver->ops = &null_dai_ops;
2565
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002566 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002567 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002568 component->num_dai++;
2569
2570 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2571 return dai;
2572}
2573
Mark Brown91151712008-11-30 23:31:24 +00002574/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002575 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002576 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002577 * @component: The component the DAIs are registered for
2578 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002579 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002580 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002581static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002582 struct snd_soc_dai_driver *dai_drv,
2583 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002584{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002585 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002586 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002587 unsigned int i;
2588 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002589
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002590 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002591
2592 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002593
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002594 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2595 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002596 if (dai == NULL) {
2597 ret = -ENOMEM;
2598 goto err;
2599 }
Mark Brown91151712008-11-30 23:31:24 +00002600 }
2601
2602 return 0;
2603
2604err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002605 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002606
2607 return ret;
2608}
Mark Brown91151712008-11-30 23:31:24 +00002609
Mengdong Lin68003e62015-12-31 16:40:43 +08002610/**
2611 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2612 *
2613 * @component: The component the DAIs are registered for
2614 * @dai_drv: DAI driver to use for the DAI
2615 *
2616 * Topology can use this API to register DAIs when probing a component.
2617 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2618 * will be freed in the component cleanup.
2619 */
2620int snd_soc_register_dai(struct snd_soc_component *component,
2621 struct snd_soc_dai_driver *dai_drv)
2622{
2623 struct snd_soc_dapm_context *dapm =
2624 snd_soc_component_get_dapm(component);
2625 struct snd_soc_dai *dai;
2626 int ret;
2627
2628 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2629 dev_err(component->dev, "Invalid dai type %d\n",
2630 dai_drv->dobj.type);
2631 return -EINVAL;
2632 }
2633
2634 lockdep_assert_held(&client_mutex);
2635 dai = soc_add_dai(component, dai_drv, false);
2636 if (!dai)
2637 return -ENOMEM;
2638
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002639 /*
2640 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002641 * also add routes that need these widgets as source or sink.
2642 */
2643 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2644 if (ret != 0) {
2645 dev_err(component->dev,
2646 "Failed to create DAI widgets %d\n", ret);
2647 }
2648
2649 return ret;
2650}
2651EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2652
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002653static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2654 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002655{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002656 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002657
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002658 component->driver->seq_notifier(component, type, subseq);
2659}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002660
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002661static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2662 int event)
2663{
2664 struct snd_soc_component *component = dapm->component;
2665
2666 return component->driver->stream_event(component, event);
2667}
2668
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002669static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2670 enum snd_soc_bias_level level)
2671{
2672 struct snd_soc_component *component = dapm->component;
2673
2674 return component->driver->set_bias_level(component, level);
2675}
2676
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002677static int snd_soc_component_initialize(struct snd_soc_component *component,
2678 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002679{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002680 struct snd_soc_dapm_context *dapm;
2681
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002682 component->name = fmt_single_name(dev, &component->id);
2683 if (!component->name) {
2684 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002685 return -ENOMEM;
2686 }
2687
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002688 component->dev = dev;
2689 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002690
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002691 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002692 dapm->dev = dev;
2693 dapm->component = component;
2694 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002695 dapm->idle_bias_off = !driver->idle_bias_on;
2696 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002697 if (driver->seq_notifier)
2698 dapm->seq_notifier = snd_soc_component_seq_notifier;
2699 if (driver->stream_event)
2700 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002701 if (driver->set_bias_level)
2702 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002703
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002704 INIT_LIST_HEAD(&component->dai_list);
2705 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002706
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002707 return 0;
2708}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002709
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002710static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002711{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002712 int val_bytes = regmap_get_val_bytes(component->regmap);
2713
2714 /* Errors are legitimate for non-integer byte multiples */
2715 if (val_bytes > 0)
2716 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002717}
2718
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002719#ifdef CONFIG_REGMAP
2720
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002721/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002722 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2723 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002724 * @component: The component for which to initialize the regmap instance
2725 * @regmap: The regmap instance that should be used by the component
2726 *
2727 * This function allows deferred assignment of the regmap instance that is
2728 * associated with the component. Only use this if the regmap instance is not
2729 * yet ready when the component is registered. The function must also be called
2730 * before the first IO attempt of the component.
2731 */
2732void snd_soc_component_init_regmap(struct snd_soc_component *component,
2733 struct regmap *regmap)
2734{
2735 component->regmap = regmap;
2736 snd_soc_component_setup_regmap(component);
2737}
2738EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2739
2740/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002741 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2742 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002743 * @component: The component for which to de-initialize the regmap instance
2744 *
2745 * Calls regmap_exit() on the regmap instance associated to the component and
2746 * removes the regmap instance from the component.
2747 *
2748 * This function should only be used if snd_soc_component_init_regmap() was used
2749 * to initialize the regmap instance.
2750 */
2751void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2752{
2753 regmap_exit(component->regmap);
2754 component->regmap = NULL;
2755}
2756EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2757
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002758#endif
2759
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002760static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002761{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002762 mutex_lock(&client_mutex);
2763
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002764 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002765 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002766 component->regmap = dev_get_regmap(component->dev,
2767 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002768 if (component->regmap)
2769 snd_soc_component_setup_regmap(component);
2770 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002771
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002772 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002773 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002774 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002775
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002776 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002777}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002778
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002779static void snd_soc_component_cleanup(struct snd_soc_component *component)
2780{
2781 snd_soc_unregister_dais(component);
2782 kfree(component->name);
2783}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002784
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002785static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2786{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002787 struct snd_soc_card *card = component->card;
2788
2789 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002790 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002791
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002792 list_del(&component->list);
2793}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002794
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002795#define ENDIANNESS_MAP(name) \
2796 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2797static u64 endianness_format_map[] = {
2798 ENDIANNESS_MAP(S16_),
2799 ENDIANNESS_MAP(U16_),
2800 ENDIANNESS_MAP(S24_),
2801 ENDIANNESS_MAP(U24_),
2802 ENDIANNESS_MAP(S32_),
2803 ENDIANNESS_MAP(U32_),
2804 ENDIANNESS_MAP(S24_3),
2805 ENDIANNESS_MAP(U24_3),
2806 ENDIANNESS_MAP(S20_3),
2807 ENDIANNESS_MAP(U20_3),
2808 ENDIANNESS_MAP(S18_3),
2809 ENDIANNESS_MAP(U18_3),
2810 ENDIANNESS_MAP(FLOAT_),
2811 ENDIANNESS_MAP(FLOAT64_),
2812 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2813};
2814
2815/*
2816 * Fix up the DAI formats for endianness: codecs don't actually see
2817 * the endianness of the data but we're using the CPU format
2818 * definitions which do need to include endianness so we ensure that
2819 * codec DAIs always have both big and little endian variants set.
2820 */
2821static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2822{
2823 int i;
2824
2825 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2826 if (stream->formats & endianness_format_map[i])
2827 stream->formats |= endianness_format_map[i];
2828}
2829
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002830static void snd_soc_try_rebind_card(void)
2831{
2832 struct snd_soc_card *card, *c;
2833
2834 if (!list_empty(&unbind_card_list)) {
2835 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2836 if (!snd_soc_bind_card(card))
2837 list_del(&card->list);
2838 }
2839 }
2840}
2841
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002842int snd_soc_add_component(struct device *dev,
2843 struct snd_soc_component *component,
2844 const struct snd_soc_component_driver *component_driver,
2845 struct snd_soc_dai_driver *dai_drv,
2846 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002847{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002848 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002849 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002850
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002851 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002852 if (ret)
2853 goto err_free;
2854
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002855 if (component_driver->endianness) {
2856 for (i = 0; i < num_dai; i++) {
2857 convert_endianness_formats(&dai_drv[i].playback);
2858 convert_endianness_formats(&dai_drv[i].capture);
2859 }
2860 }
2861
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002862 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002863 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002864 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002865 goto err_cleanup;
2866 }
2867
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002868 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002869 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002870
2871 return 0;
2872
2873err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002874 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002875err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002876 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002877}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002878EXPORT_SYMBOL_GPL(snd_soc_add_component);
2879
2880int snd_soc_register_component(struct device *dev,
2881 const struct snd_soc_component_driver *component_driver,
2882 struct snd_soc_dai_driver *dai_drv,
2883 int num_dai)
2884{
2885 struct snd_soc_component *component;
2886
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002887 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002888 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002889 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002890
2891 return snd_soc_add_component(dev, component, component_driver,
2892 dai_drv, num_dai);
2893}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002894EXPORT_SYMBOL_GPL(snd_soc_register_component);
2895
2896/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002897 * snd_soc_unregister_component - Unregister all related component
2898 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002899 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002900 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002901 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002902static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002903{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002904 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00002905 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002906
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002907 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002908 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002909 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00002910 continue;
2911
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002912 snd_soc_tplg_component_remove(component,
2913 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00002914 snd_soc_component_del_unlocked(component);
2915 found = 1;
2916 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002917 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002918 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002919
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002920 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00002921 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002922
2923 return found;
2924}
2925
2926void snd_soc_unregister_component(struct device *dev)
2927{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002928 while (__snd_soc_unregister_component(dev))
2929 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002930}
2931EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2932
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002933struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
2934 const char *driver_name)
2935{
2936 struct snd_soc_component *component;
2937 struct snd_soc_component *ret;
2938
2939 ret = NULL;
2940 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002941 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002942 if (dev != component->dev)
2943 continue;
2944
2945 if (driver_name &&
2946 (driver_name != component->driver->name) &&
2947 (strcmp(component->driver->name, driver_name) != 0))
2948 continue;
2949
2950 ret = component;
2951 break;
2952 }
2953 mutex_unlock(&client_mutex);
2954
2955 return ret;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
2958
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002959/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002960int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2961 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002962{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002963 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002964 int ret;
2965
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302966 if (!card->dev) {
2967 pr_err("card->dev is not set before calling %s\n", __func__);
2968 return -EINVAL;
2969 }
2970
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002971 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302972
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002973 ret = of_property_read_string_index(np, propname, 0, &card->name);
2974 /*
2975 * EINVAL means the property does not exist. This is fine providing
2976 * card->name was previously set, which is checked later in
2977 * snd_soc_register_card.
2978 */
2979 if (ret < 0 && ret != -EINVAL) {
2980 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002981 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002982 propname, ret);
2983 return ret;
2984 }
2985
2986 return 0;
2987}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002988EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002989
Xiubo Li9a6d4862014-02-08 15:59:52 +08002990static const struct snd_soc_dapm_widget simple_widgets[] = {
2991 SND_SOC_DAPM_MIC("Microphone", NULL),
2992 SND_SOC_DAPM_LINE("Line", NULL),
2993 SND_SOC_DAPM_HP("Headphone", NULL),
2994 SND_SOC_DAPM_SPK("Speaker", NULL),
2995};
2996
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002997int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002998 const char *propname)
2999{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003000 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003001 struct snd_soc_dapm_widget *widgets;
3002 const char *template, *wname;
3003 int i, j, num_widgets, ret;
3004
3005 num_widgets = of_property_count_strings(np, propname);
3006 if (num_widgets < 0) {
3007 dev_err(card->dev,
3008 "ASoC: Property '%s' does not exist\n", propname);
3009 return -EINVAL;
3010 }
3011 if (num_widgets & 1) {
3012 dev_err(card->dev,
3013 "ASoC: Property '%s' length is not even\n", propname);
3014 return -EINVAL;
3015 }
3016
3017 num_widgets /= 2;
3018 if (!num_widgets) {
3019 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3020 propname);
3021 return -EINVAL;
3022 }
3023
3024 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3025 GFP_KERNEL);
3026 if (!widgets) {
3027 dev_err(card->dev,
3028 "ASoC: Could not allocate memory for widgets\n");
3029 return -ENOMEM;
3030 }
3031
3032 for (i = 0; i < num_widgets; i++) {
3033 ret = of_property_read_string_index(np, propname,
3034 2 * i, &template);
3035 if (ret) {
3036 dev_err(card->dev,
3037 "ASoC: Property '%s' index %d read error:%d\n",
3038 propname, 2 * i, ret);
3039 return -EINVAL;
3040 }
3041
3042 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3043 if (!strncmp(template, simple_widgets[j].name,
3044 strlen(simple_widgets[j].name))) {
3045 widgets[i] = simple_widgets[j];
3046 break;
3047 }
3048 }
3049
3050 if (j >= ARRAY_SIZE(simple_widgets)) {
3051 dev_err(card->dev,
3052 "ASoC: DAPM widget '%s' is not supported\n",
3053 template);
3054 return -EINVAL;
3055 }
3056
3057 ret = of_property_read_string_index(np, propname,
3058 (2 * i) + 1,
3059 &wname);
3060 if (ret) {
3061 dev_err(card->dev,
3062 "ASoC: Property '%s' index %d read error:%d\n",
3063 propname, (2 * i) + 1, ret);
3064 return -EINVAL;
3065 }
3066
3067 widgets[i].name = wname;
3068 }
3069
Nicolin Chenf23e8602015-02-14 17:22:49 -08003070 card->of_dapm_widgets = widgets;
3071 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003072
3073 return 0;
3074}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003075EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003076
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003077int snd_soc_of_get_slot_mask(struct device_node *np,
3078 const char *prop_name,
3079 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003080{
3081 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003082 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003083 int i;
3084
3085 if (!of_slot_mask)
3086 return 0;
3087 val /= sizeof(u32);
3088 for (i = 0; i < val; i++)
3089 if (be32_to_cpup(&of_slot_mask[i]))
3090 *mask |= (1 << i);
3091
3092 return val;
3093}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003094EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003095
Xiubo Li89c67852014-02-14 09:34:35 +08003096int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003097 unsigned int *tx_mask,
3098 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003099 unsigned int *slots,
3100 unsigned int *slot_width)
3101{
3102 u32 val;
3103 int ret;
3104
Jyri Sarha61310842015-09-09 21:27:43 +03003105 if (tx_mask)
3106 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3107 if (rx_mask)
3108 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3109
Xiubo Li89c67852014-02-14 09:34:35 +08003110 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3111 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3112 if (ret)
3113 return ret;
3114
3115 if (slots)
3116 *slots = val;
3117 }
3118
3119 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3120 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3121 if (ret)
3122 return ret;
3123
3124 if (slot_width)
3125 *slot_width = val;
3126 }
3127
3128 return 0;
3129}
3130EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3131
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003132void snd_soc_of_parse_node_prefix(struct device_node *np,
3133 struct snd_soc_codec_conf *codec_conf,
3134 struct device_node *of_node,
3135 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003136{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003137 const char *str;
3138 int ret;
3139
3140 ret = of_property_read_string(np, propname, &str);
3141 if (ret < 0) {
3142 /* no prefix is not error */
3143 return;
3144 }
3145
3146 codec_conf->of_node = of_node;
3147 codec_conf->name_prefix = str;
3148}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003149EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003150
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003151int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003152 const char *propname)
3153{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003154 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003155 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003156 struct snd_soc_dapm_route *routes;
3157 int i, ret;
3158
3159 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003160 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003161 dev_err(card->dev,
3162 "ASoC: Property '%s' does not exist or its length is not even\n",
3163 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003164 return -EINVAL;
3165 }
3166 num_routes /= 2;
3167 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003168 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003169 propname);
3170 return -EINVAL;
3171 }
3172
Kees Cooka86854d2018-06-12 14:07:58 -07003173 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003174 GFP_KERNEL);
3175 if (!routes) {
3176 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003177 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003178 return -EINVAL;
3179 }
3180
3181 for (i = 0; i < num_routes; i++) {
3182 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003183 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003184 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003185 dev_err(card->dev,
3186 "ASoC: Property '%s' index %d could not be read: %d\n",
3187 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003188 return -EINVAL;
3189 }
3190 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003191 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003192 if (ret) {
3193 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003194 "ASoC: Property '%s' index %d could not be read: %d\n",
3195 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003196 return -EINVAL;
3197 }
3198 }
3199
Nicolin Chenf23e8602015-02-14 17:22:49 -08003200 card->num_of_dapm_routes = num_routes;
3201 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003202
3203 return 0;
3204}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003205EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003206
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003207unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003208 const char *prefix,
3209 struct device_node **bitclkmaster,
3210 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003211{
3212 int ret, i;
3213 char prop[128];
3214 unsigned int format = 0;
3215 int bit, frame;
3216 const char *str;
3217 struct {
3218 char *name;
3219 unsigned int val;
3220 } of_fmt_table[] = {
3221 { "i2s", SND_SOC_DAIFMT_I2S },
3222 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3223 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3224 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3225 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3226 { "ac97", SND_SOC_DAIFMT_AC97 },
3227 { "pdm", SND_SOC_DAIFMT_PDM},
3228 { "msb", SND_SOC_DAIFMT_MSB },
3229 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003230 };
3231
3232 if (!prefix)
3233 prefix = "";
3234
3235 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003236 * check "dai-format = xxx"
3237 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003238 * SND_SOC_DAIFMT_FORMAT_MASK area
3239 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003240 ret = of_property_read_string(np, "dai-format", &str);
3241 if (ret < 0) {
3242 snprintf(prop, sizeof(prop), "%sformat", prefix);
3243 ret = of_property_read_string(np, prop, &str);
3244 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003245 if (ret == 0) {
3246 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3247 if (strcmp(str, of_fmt_table[i].name) == 0) {
3248 format |= of_fmt_table[i].val;
3249 break;
3250 }
3251 }
3252 }
3253
3254 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003255 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003256 * SND_SOC_DAIFMT_CLOCK_MASK area
3257 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003258 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003259 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003260 format |= SND_SOC_DAIFMT_CONT;
3261 else
3262 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003263
3264 /*
3265 * check "[prefix]bitclock-inversion"
3266 * check "[prefix]frame-inversion"
3267 * SND_SOC_DAIFMT_INV_MASK area
3268 */
3269 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3270 bit = !!of_get_property(np, prop, NULL);
3271
3272 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3273 frame = !!of_get_property(np, prop, NULL);
3274
3275 switch ((bit << 4) + frame) {
3276 case 0x11:
3277 format |= SND_SOC_DAIFMT_IB_IF;
3278 break;
3279 case 0x10:
3280 format |= SND_SOC_DAIFMT_IB_NF;
3281 break;
3282 case 0x01:
3283 format |= SND_SOC_DAIFMT_NB_IF;
3284 break;
3285 default:
3286 /* SND_SOC_DAIFMT_NB_NF is default */
3287 break;
3288 }
3289
3290 /*
3291 * check "[prefix]bitclock-master"
3292 * check "[prefix]frame-master"
3293 * SND_SOC_DAIFMT_MASTER_MASK area
3294 */
3295 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3296 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003297 if (bit && bitclkmaster)
3298 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003299
3300 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3301 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003302 if (frame && framemaster)
3303 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003304
3305 switch ((bit << 4) + frame) {
3306 case 0x11:
3307 format |= SND_SOC_DAIFMT_CBM_CFM;
3308 break;
3309 case 0x10:
3310 format |= SND_SOC_DAIFMT_CBM_CFS;
3311 break;
3312 case 0x01:
3313 format |= SND_SOC_DAIFMT_CBS_CFM;
3314 break;
3315 default:
3316 format |= SND_SOC_DAIFMT_CBS_CFS;
3317 break;
3318 }
3319
3320 return format;
3321}
3322EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3323
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003324int snd_soc_get_dai_id(struct device_node *ep)
3325{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003326 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003327 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003328 int ret;
3329
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003330 dlc.of_node = of_graph_get_port_parent(ep);
3331 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003332 /*
3333 * For example HDMI case, HDMI has video/sound port,
3334 * but ALSA SoC needs sound port number only.
3335 * Thus counting HDMI DT port/endpoint doesn't work.
3336 * Then, it should have .of_xlate_dai_id
3337 */
3338 ret = -ENOTSUPP;
3339 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003340 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003341 if (component &&
3342 component->driver->of_xlate_dai_id)
3343 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003344 mutex_unlock(&client_mutex);
3345
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003346 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003347
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003348 return ret;
3349}
3350EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3351
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003352int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003353 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003354{
3355 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003356 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003357 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003358
3359 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003360 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003361 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003362
3363 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003364 continue;
3365
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003366 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003367 ret = pos->driver->of_xlate_dai_name(pos,
3368 args,
3369 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003370 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003371 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003372 int id = -1;
3373
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003374 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003375 case 0:
3376 id = 0; /* same as dai_drv[0] */
3377 break;
3378 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003379 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003380 break;
3381 default:
3382 /* not supported */
3383 break;
3384 }
3385
3386 if (id < 0 || id >= pos->num_dai) {
3387 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003388 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003389 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003390
3391 ret = 0;
3392
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003393 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003394 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003395 if (id == 0)
3396 break;
3397 id--;
3398 }
3399
3400 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003401 if (!*dai_name)
3402 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003403 }
3404
Kuninori Morimotocb470082013-09-10 17:39:56 -07003405 break;
3406 }
3407 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003408 return ret;
3409}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003410EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003411
3412int snd_soc_of_get_dai_name(struct device_node *of_node,
3413 const char **dai_name)
3414{
3415 struct of_phandle_args args;
3416 int ret;
3417
3418 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3419 "#sound-dai-cells", 0, &args);
3420 if (ret)
3421 return ret;
3422
3423 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003424
3425 of_node_put(args.np);
3426
3427 return ret;
3428}
3429EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3430
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003431/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003432 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3433 * @dai_link: DAI link
3434 *
3435 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3436 */
3437void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3438{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003439 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003440 int index;
3441
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003442 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003443 if (!component->of_node)
3444 break;
3445 of_node_put(component->of_node);
3446 component->of_node = NULL;
3447 }
3448}
3449EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3450
3451/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003452 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3453 * @dev: Card device
3454 * @of_node: Device node
3455 * @dai_link: DAI link
3456 *
3457 * Builds an array of CODEC DAI components from the DAI link property
3458 * 'sound-dai'.
3459 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003460 * The device nodes in the array (of_node) must be dereferenced by calling
3461 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003462 *
3463 * Returns 0 for success
3464 */
3465int snd_soc_of_get_dai_link_codecs(struct device *dev,
3466 struct device_node *of_node,
3467 struct snd_soc_dai_link *dai_link)
3468{
3469 struct of_phandle_args args;
3470 struct snd_soc_dai_link_component *component;
3471 char *name;
3472 int index, num_codecs, ret;
3473
3474 /* Count the number of CODECs */
3475 name = "sound-dai";
3476 num_codecs = of_count_phandle_with_args(of_node, name,
3477 "#sound-dai-cells");
3478 if (num_codecs <= 0) {
3479 if (num_codecs == -ENOENT)
3480 dev_err(dev, "No 'sound-dai' property\n");
3481 else
3482 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3483 return num_codecs;
3484 }
Kees Cooka86854d2018-06-12 14:07:58 -07003485 component = devm_kcalloc(dev,
3486 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003487 GFP_KERNEL);
3488 if (!component)
3489 return -ENOMEM;
3490 dai_link->codecs = component;
3491 dai_link->num_codecs = num_codecs;
3492
3493 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003494 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003495 ret = of_parse_phandle_with_args(of_node, name,
3496 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003497 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003498 if (ret)
3499 goto err;
3500 component->of_node = args.np;
3501 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3502 if (ret < 0)
3503 goto err;
3504 }
3505 return 0;
3506err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003507 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003508 dai_link->codecs = NULL;
3509 dai_link->num_codecs = 0;
3510 return ret;
3511}
3512EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3513
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003514static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003515{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003516 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003517 snd_soc_util_init();
3518
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003519 return platform_driver_register(&soc_driver);
3520}
Mark Brown4abe8e12010-10-12 17:41:03 +01003521module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003522
Mark Brown7d8c16a2008-11-30 22:11:24 +00003523static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003524{
Mark Brownfb257892011-04-28 10:57:54 +01003525 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003526 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003527
Mark Brown3ff3f642008-05-19 12:32:25 +02003528 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003529}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003530module_exit(snd_soc_exit);
3531
3532/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003533MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003534MODULE_DESCRIPTION("ALSA SoC Core");
3535MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003536MODULE_ALIAS("platform:soc-audio");