blob: 6a422ddae13032a11608a21640ff5a366e8ccccc [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 Morimotoe40fadb2019-07-26 13:51:13 +0900506 if (!snd_soc_component_is_suspended(component)) {
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 Morimotoe40fadb2019-07-26 13:51:13 +0900595 if (snd_soc_component_is_suspended(component))
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900596 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100597 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000599 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Mengdong Lin1a497982015-11-18 02:34:11 -0500601 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Mengdong Lin1a497982015-11-18 02:34:11 -0500604 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000605 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800606 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607
Mengdong Lin1a497982015-11-18 02:34:11 -0500608 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000609 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800610 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611 }
612
Mark Brown3ff3f642008-05-19 12:32:25 +0200613 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000614 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000615 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100616
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100618 continue;
619
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000620 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900621 if (dai->playback_active)
622 snd_soc_dai_digital_mute(dai, 0,
623 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200624 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625 }
626
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000627 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500628 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100629
Mengdong Lin1a497982015-11-18 02:34:11 -0500630 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100631 continue;
632
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900633 if (!cpu_dai->driver->bus_control)
634 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635 }
636
Mark Brown87506542008-11-18 20:50:34 +0000637 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000638 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000640 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100641
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200642 /* Recheck all endpoints too, their state is affected by suspend */
643 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700644 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530645
646 /* userspace can access us now we are back as we were before */
647 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100648}
649
650/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000651int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100652{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000653 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100654 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900656 struct snd_soc_dai *codec_dai;
657 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200658
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200659 /* If the card is not initialized yet there is nothing to do */
660 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800661 return 0;
662
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800663 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000664 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200666
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800667 if (cpu_dai->active)
668 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200669
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900670 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200671 if (codec_dai->active)
672 pinctrl_pm_select_default_state(codec_dai->dev);
673 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800674 }
675
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100676 /*
677 * DAIs that also act as the control bus master might have other drivers
678 * hanging off them so need to resume immediately. Other drivers don't
679 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100680 * due to I/O costs and anti-pop so handle them out of line.
681 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000682 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500683 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200684
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100685 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600686 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100687 if (bus_control) {
688 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600689 soc_resume_deferred(&card->deferred_resume_work);
690 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000691 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600692 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000693 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100694 }
Andy Green6ed25972008-06-13 16:24:05 +0100695
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696 return 0;
697}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000698EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000700#define snd_soc_suspend NULL
701#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702#endif
703
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100704static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800705};
706
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900707static struct device_node
708*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100709{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900710 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100711
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900712 of_node = component->dev->of_node;
713 if (!of_node && component->dev->parent)
714 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100715
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900716 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100717}
718
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000719static int snd_soc_is_matching_component(
720 const struct snd_soc_dai_link_component *dlc,
721 struct snd_soc_component *component)
722{
723 struct device_node *component_of_node;
724
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900725 if (!dlc)
726 return 0;
727
728 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000729
730 if (dlc->of_node && component_of_node != dlc->of_node)
731 return 0;
732 if (dlc->name && strcmp(component->name, dlc->name))
733 return 0;
734
735 return 1;
736}
737
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900739 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740{
741 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742
743 lockdep_assert_held(&client_mutex);
744
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900745 /*
746 * NOTE
747 *
748 * It returns *1st* found component, but some driver
749 * has few components by same of_node/name
750 * ex)
751 * CPU component and generic DMAEngine component
752 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900753 for_each_component(component)
754 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756
757 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100758}
759
Mengdong Linfbb88b52016-04-22 12:25:33 +0800760/**
761 * snd_soc_find_dai - Find a registered DAI
762 *
Jeffy Chen49584712017-08-22 15:57:21 +0800763 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800764 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700765 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800766 * find the DAI of the same name. The component's of_node and name
767 * should also match if being specified.
768 *
769 * Return: pointer of DAI, or NULL if not found.
770 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800771struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200772 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100773{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200774 struct snd_soc_component *component;
775 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100776
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100777 lockdep_assert_held(&client_mutex);
778
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200779 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000780 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000781 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200782 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000783 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800784 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800785 && (!dai->driver->name
786 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100787 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100788
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200789 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100790 }
791 }
792
793 return NULL;
794}
Mengdong Lin305e9022016-04-19 13:12:25 +0800795EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100796
Mengdong Lin17fb17552016-11-03 01:04:12 +0800797/**
798 * snd_soc_find_dai_link - Find a DAI link
799 *
800 * @card: soc card
801 * @id: DAI link ID to match
802 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000803 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800804 *
805 * This function will search all existing DAI links of the soc card to
806 * find the link of the same ID. Since DAI links may not have their
807 * unique ID, so name and stream name should also match if being
808 * specified.
809 *
810 * Return: pointer of DAI link, or NULL if not found.
811 */
812struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
813 int id, const char *name,
814 const char *stream_name)
815{
816 struct snd_soc_dai_link *link, *_link;
817
818 lockdep_assert_held(&client_mutex);
819
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000820 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800821 if (link->id != id)
822 continue;
823
824 if (name && (!link->name || strcmp(name, link->name)))
825 continue;
826
827 if (stream_name && (!link->stream_name
828 || strcmp(stream_name, link->stream_name)))
829 continue;
830
831 return link;
832 }
833
834 return NULL;
835}
836EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
837
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800838static bool soc_is_dai_link_bound(struct snd_soc_card *card,
839 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200840{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800841 struct snd_soc_pcm_runtime *rtd;
842
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000843 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800844 if (rtd->dai_link == dai_link)
845 return true;
846 }
847
848 return false;
849}
850
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500851static int soc_bind_dai_link(struct snd_soc_card *card,
852 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853{
Mengdong Lin1a497982015-11-18 02:34:11 -0500854 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +0200855 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000856 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200857 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858
Liam Girdwooda655de82018-07-02 16:59:54 +0100859 if (dai_link->ignore)
860 return 0;
861
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500862 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000863
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800864 if (soc_is_dai_link_bound(card, dai_link)) {
865 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
866 dai_link->name);
867 return 0;
868 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200869
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530870 rtd = soc_new_pcm_runtime(card, dai_link);
871 if (!rtd)
872 return -ENOMEM;
873
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900874 /* FIXME: we need multi CPU support in the future */
875 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000876 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100877 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900878 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500879 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000880 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000881 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000882
Benoit Cousson88bd8702014-07-08 23:19:34 +0200883 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900884 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +0200885 for_each_link_codecs(dai_link, i, codec) {
886 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900887 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100888 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +0200889 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500890 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200891 }
Jerome Brunet34614732019-06-27 14:13:50 +0200892
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900893 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000894 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100895
Benoit Cousson88bd8702014-07-08 23:19:34 +0200896 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900897 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100898
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900899 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +0200900 for_each_link_platforms(dai_link, i, platform) {
901 for_each_component(component) {
902 if (!snd_soc_is_matching_component(platform, component))
903 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000904
Jerome Brunet34614732019-06-27 14:13:50 +0200905 snd_soc_rtdcom_add(rtd, component);
906 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000907 }
908
Mengdong Lin1a497982015-11-18 02:34:11 -0500909 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000910 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500911
912_err_defer:
913 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200914 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915}
916
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900917static void soc_cleanup_component(struct snd_soc_component *component)
918{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200919 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900920 list_del(&component->card_list);
921 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
922 soc_cleanup_component_debugfs(component);
923 component->card = NULL;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900924 snd_soc_component_module_put_when_close(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900925}
926
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200927static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600928{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200929 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200930 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600931
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000932 if (component->driver->remove)
933 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600934
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900935 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600936}
937
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200938static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200939{
940 int err;
941
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600942 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000943 dai->driver->remove_order != order)
944 return;
945
Kuninori Morimotodcdab582019-07-22 10:35:05 +0900946 err = snd_soc_dai_remove(dai);
947 if (err < 0)
948 dev_err(dai->dev,
949 "ASoC: failed to remove %s: %d\n",
950 dai->name, err);
951
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000952 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100953}
954
Mengdong Lin1a497982015-11-18 02:34:11 -0500955static void soc_remove_link_dais(struct snd_soc_card *card,
956 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200958 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000959 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960
961 /* unregister the rtd device */
962 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800963 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 rtd->dev_registered = 0;
965 }
966
967 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000968 for_each_rtd_codec_dai(rtd, i, codec_dai)
969 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000972}
973
Mengdong Lin1a497982015-11-18 02:34:11 -0500974static void soc_remove_link_components(struct snd_soc_card *card,
975 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600976{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200977 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000978 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600979
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000980 for_each_rtdcom(rtd, rtdcom) {
981 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600982
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200983 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200984 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600985 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600986}
987
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900988static void soc_remove_dai_links(struct snd_soc_card *card)
989{
Mengdong Lin1a497982015-11-18 02:34:11 -0500990 int order;
991 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800992 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900993
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +0000994 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000995 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -0500996 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +0100997 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +0000999 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001000 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001001 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001002 }
1003
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001004 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001005 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1006 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1007 link->name);
1008
1009 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001010 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001011}
1012
Mengdong Lin923c5e612015-11-23 11:01:49 -05001013static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001014 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001015{
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001016 int i;
Jerome Brunet34614732019-06-27 14:13:50 +02001017 struct snd_soc_dai_link_component *codec, *platform;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001018
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001019 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001020 /*
1021 * Codec must be specified by 1 of name or OF node,
1022 * not both or neither.
1023 */
Jerome Brunet34614732019-06-27 14:13:50 +02001024 if (!!codec->name == !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001025 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1026 link->name);
1027 return -EINVAL;
1028 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001029
Mengdong Lin923c5e612015-11-23 11:01:49 -05001030 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001031 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001032 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1033 link->name);
1034 return -EINVAL;
1035 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001036
1037 /*
1038 * Defer card registration if codec component is not added to
1039 * component list.
1040 */
1041 if (!soc_find_component(codec))
1042 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001043 }
1044
Jerome Brunet34614732019-06-27 14:13:50 +02001045 for_each_link_platforms(link, i, platform) {
1046 /*
1047 * Platform may be specified by either name or OF node, but it
1048 * can be left unspecified, then no components will be inserted
1049 * in the rtdcom list
1050 */
1051 if (!!platform->name == !!platform->of_node) {
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001052 dev_err(card->dev,
Jerome Brunet34614732019-06-27 14:13:50 +02001053 "ASoC: Neither/both platform name/of_node are set for %s\n",
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001054 link->name);
1055 return -EINVAL;
1056 }
1057
1058 /*
Jerome Brunet34614732019-06-27 14:13:50 +02001059 * Defer card registration if platform component is not added to
1060 * component list.
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001061 */
Jerome Brunet34614732019-06-27 14:13:50 +02001062 if (!soc_find_component(platform))
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001063 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001064 }
1065
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001066 /* FIXME */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001067 if (link->num_cpus > 1) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001068 dev_err(card->dev,
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001069 "ASoC: multi cpu is not yet supported %s\n",
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001070 link->name);
1071 return -EINVAL;
1072 }
1073
Mengdong Lin923c5e612015-11-23 11:01:49 -05001074 /*
Mengdong Lin923c5e612015-11-23 11:01:49 -05001075 * CPU device may be specified by either name or OF node, but
1076 * can be left unspecified, and will be matched based on DAI
1077 * name alone..
1078 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001079 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001080 dev_err(card->dev,
1081 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1082 link->name);
1083 return -EINVAL;
1084 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301085
1086 /*
1087 * Defer card registartion if cpu dai component is not added to
1088 * component list.
1089 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001090 if ((link->cpus->of_node || link->cpus->name) &&
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001091 !soc_find_component(link->cpus))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301092 return -EPROBE_DEFER;
1093
Mengdong Lin923c5e612015-11-23 11:01:49 -05001094 /*
1095 * At least one of CPU DAI name or CPU device name/node must be
1096 * specified
1097 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001098 if (!link->cpus->dai_name &&
1099 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001100 dev_err(card->dev,
1101 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1102 link->name);
1103 return -EINVAL;
1104 }
1105
1106 return 0;
1107}
1108
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001109void snd_soc_disconnect_sync(struct device *dev)
1110{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001111 struct snd_soc_component *component =
1112 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001113
1114 if (!component || !component->card)
1115 return;
1116
1117 snd_card_disconnect_sync(component->card->snd_card);
1118}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001119EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001120
Mengdong Linf8f80362015-12-02 14:11:22 +08001121/**
1122 * snd_soc_add_dai_link - Add a DAI link dynamically
1123 * @card: The ASoC card to which the DAI link is added
1124 * @dai_link: The new DAI link to add
1125 *
1126 * This function adds a DAI link to the ASoC card's link list.
1127 *
1128 * Note: Topology can use this API to add DAI links when probing the
1129 * topology component. And machine drivers can still define static
1130 * DAI links in dai_link array.
1131 */
1132int snd_soc_add_dai_link(struct snd_soc_card *card,
1133 struct snd_soc_dai_link *dai_link)
1134{
1135 if (dai_link->dobj.type
1136 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1137 dev_err(card->dev, "Invalid dai link type %d\n",
1138 dai_link->dobj.type);
1139 return -EINVAL;
1140 }
1141
1142 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001143 /*
1144 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001145 * on the link created by topology.
1146 */
1147 if (dai_link->dobj.type && card->add_dai_link)
1148 card->add_dai_link(card, dai_link);
1149
Mengdong Linf8f80362015-12-02 14:11:22 +08001150 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001151
1152 return 0;
1153}
1154EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1155
1156/**
1157 * snd_soc_remove_dai_link - Remove a DAI link from the list
1158 * @card: The ASoC card that owns the link
1159 * @dai_link: The DAI link to remove
1160 *
1161 * This function removes a DAI link from the ASoC card's link list.
1162 *
1163 * For DAI links previously added by topology, topology should
1164 * remove them by using the dobj embedded in the link.
1165 */
1166void snd_soc_remove_dai_link(struct snd_soc_card *card,
1167 struct snd_soc_dai_link *dai_link)
1168{
1169 struct snd_soc_dai_link *link, *_link;
1170
1171 if (dai_link->dobj.type
1172 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1173 dev_err(card->dev, "Invalid dai link type %d\n",
1174 dai_link->dobj.type);
1175 return;
1176 }
1177
1178 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001179 /*
1180 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001181 * on the link created by topology.
1182 */
1183 if (dai_link->dobj.type && card->remove_dai_link)
1184 card->remove_dai_link(card, dai_link);
1185
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001186 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001187 if (link == dai_link) {
1188 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001189 return;
1190 }
1191 }
1192}
1193EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1194
Jerome Brunetaefba452018-07-13 14:50:43 +02001195static void soc_set_of_name_prefix(struct snd_soc_component *component)
1196{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001197 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001198 const char *str;
1199 int ret;
1200
Jerome Brunetaefba452018-07-13 14:50:43 +02001201 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1202 &str);
1203 if (!ret)
1204 component->name_prefix = str;
1205}
1206
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001207static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001208 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001209{
1210 int i;
1211
Jerome Brunetaefba452018-07-13 14:50:43 +02001212 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001213 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001214 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001215
1216 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001217 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001218 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001219 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001220 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001221 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001222 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001223
1224 /*
1225 * If there is no configuration table or no match in the table,
1226 * check if a prefix is provided in the node
1227 */
1228 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001229}
1230
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001231static int soc_probe_component(struct snd_soc_card *card,
1232 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001233{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001234 struct snd_soc_dapm_context *dapm =
1235 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001236 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001237 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001238
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001239 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001240 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001241
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001242 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001243 if (component->card != card) {
1244 dev_err(component->dev,
1245 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1246 card->name, component->card->name);
1247 return -ENODEV;
1248 }
1249 return 0;
1250 }
1251
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +09001252 ret = snd_soc_component_module_get_when_probe(component);
1253 if (ret < 0)
1254 return ret;
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001255
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001256 component->card = card;
1257 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001258 INIT_LIST_HEAD(&component->card_list);
1259 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001260 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001261
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001262 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001263
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001264 if (component->driver->dapm_widgets) {
1265 ret = snd_soc_dapm_new_controls(dapm,
1266 component->driver->dapm_widgets,
1267 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001268
1269 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001270 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001271 "Failed to create new controls %d\n", ret);
1272 goto err_probe;
1273 }
1274 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001275
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001276 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001277 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001278 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001279 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001280 "Failed to create DAI widgets %d\n", ret);
1281 goto err_probe;
1282 }
1283 }
Mark Brown888df392012-02-16 19:37:51 -08001284
Kuninori Morimoto08e837d2019-07-26 13:51:17 +09001285 ret = snd_soc_component_probe(component);
1286 if (ret < 0) {
1287 dev_err(component->dev,
1288 "ASoC: failed to probe component %d\n", ret);
1289 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001291 WARN(dapm->idle_bias_off &&
1292 dapm->bias_level != SND_SOC_BIAS_OFF,
1293 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1294 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001296 /* machine specific init */
1297 if (component->init) {
1298 ret = component->init(component);
1299 if (ret < 0) {
1300 dev_err(component->dev,
1301 "Failed to do machine specific init %d\n", ret);
1302 goto err_probe;
1303 }
1304 }
1305
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001306 if (component->driver->controls)
1307 snd_soc_add_component_controls(component,
1308 component->driver->controls,
1309 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001310 if (component->driver->dapm_routes)
1311 snd_soc_dapm_add_routes(dapm,
1312 component->driver->dapm_routes,
1313 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001314
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001315 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001316 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001317 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001318
Jarkko Nikula70d293312011-01-27 16:24:22 +02001319err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001320 if (ret < 0)
1321 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001322
1323 return ret;
1324}
1325
Mark Brown36ae1a92012-01-06 17:12:45 -08001326static void rtd_release(struct device *dev)
1327{
1328 kfree(dev);
1329}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001331static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1332 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001333{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001334 int ret = 0;
1335
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001337 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1338 if (!rtd->dev)
1339 return -ENOMEM;
1340 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001341 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001342 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001343 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001344 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001345 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001346 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001347 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1348 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1349 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1350 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001351 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001352 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001353 /* calling put_device() here to free the rtd->dev */
1354 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001355 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001356 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001357 return ret;
1358 }
1359 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001360 return 0;
1361}
1362
Mengdong Lin1a497982015-11-18 02:34:11 -05001363static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001364 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001365{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001366 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001367 struct snd_soc_rtdcom_list *rtdcom;
1368 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001369
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001370 for_each_rtdcom(rtd, rtdcom) {
1371 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001372
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001373 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001374 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001375 if (ret < 0)
1376 return ret;
1377 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001378 }
1379
Stephen Warren62ae68f2012-06-08 12:34:23 -06001380 return 0;
1381}
1382
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001383static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001384{
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001385 int ret;
1386
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001387 if (dai->probed ||
1388 dai->driver->probe_order != order)
1389 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001390
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001391 ret = snd_soc_dai_probe(dai);
1392 if (ret < 0) {
1393 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1394 dai->name, ret);
1395 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001396 }
1397
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001398 dai->probed = 1;
1399
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001400 return 0;
1401}
1402
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001403static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1404 struct snd_soc_pcm_runtime *rtd)
1405{
1406 int i, ret = 0;
1407
1408 for (i = 0; i < num_dais; ++i) {
1409 struct snd_soc_dai_driver *drv = dais[i]->driver;
1410
Rohit kumarde17f142018-11-01 18:08:49 +05301411 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001412 ret = drv->pcm_new(rtd, dais[i]);
1413 if (ret < 0) {
1414 dev_err(dais[i]->dev,
1415 "ASoC: Failed to bind %s with pcm device\n",
1416 dais[i]->name);
1417 return ret;
1418 }
1419 }
1420
1421 return 0;
1422}
1423
Mengdong Lin1a497982015-11-18 02:34:11 -05001424static int soc_probe_link_dais(struct snd_soc_card *card,
1425 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426{
Mengdong Lin1a497982015-11-18 02:34:11 -05001427 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001428 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001429 struct snd_soc_rtdcom_list *rtdcom;
1430 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001431 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001432 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001434 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001435 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001436
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437 /* set default power off timeout */
1438 rtd->pmdown_time = pmdown_time;
1439
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001440 ret = soc_probe_dai(cpu_dai, order);
1441 if (ret)
1442 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001444 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001445 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1446 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001447 if (ret)
1448 return ret;
1449 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001450
Liam Girdwood0168bf02011-06-07 16:08:05 +01001451 /* complete DAI probe during last probe */
1452 if (order != SND_SOC_COMP_ORDER_LAST)
1453 return 0;
1454
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001455 /* do machine specific initialization */
1456 if (dai_link->init) {
1457 ret = dai_link->init(rtd);
1458 if (ret < 0) {
1459 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1460 dai_link->name, ret);
1461 return ret;
1462 }
1463 }
1464
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001465 if (dai_link->dai_fmt) {
1466 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1467 if (ret)
1468 return ret;
1469 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001470
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001471 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001472 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001473 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001474
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001475#ifdef CONFIG_DEBUG_FS
1476 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001477 if (dai_link->dynamic)
1478 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001479#endif
1480
Liam Girdwooda655de82018-07-02 16:59:54 +01001481 num = rtd->num;
1482
1483 /*
1484 * most drivers will register their PCMs using DAI link ordering but
1485 * topology based drivers can use the DAI link id field to set PCM
1486 * device number and then use rtd + a base offset of the BEs.
1487 */
1488 for_each_rtdcom(rtd, rtdcom) {
1489 component = rtdcom->component;
1490
1491 if (!component->driver->use_dai_pcm_id)
1492 continue;
1493
1494 if (rtd->dai_link->no_pcm)
1495 num += component->driver->be_pcm_base;
1496 else
1497 num = rtd->dai_link->id;
1498 }
1499
Kuninori Morimotob423c422019-07-22 10:35:29 +09001500 /* create compress_device if possible */
1501 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1502 if (ret != -ENOTSUPP) {
1503 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001504 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301505 dai_link->stream_name);
Kuninori Morimotob423c422019-07-22 10:35:29 +09001506 return ret;
1507 }
1508
Jerome Bruneta3420312019-07-25 18:59:47 +02001509 /* create the pcm */
1510 ret = soc_new_pcm(rtd, num);
1511 if (ret < 0) {
1512 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1513 dai_link->stream_name, ret);
1514 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515 }
Jerome Bruneta3420312019-07-25 18:59:47 +02001516 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1517 if (ret < 0)
1518 return ret;
1519 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1520 rtd->num_codecs, rtd);
1521 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522}
1523
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001524static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001525{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001526 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001527 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001528 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001529
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001530 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1531 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001532 dlc.name = aux_dev->codec_name;
1533 dlc.of_node = aux_dev->codec_of_node;
1534 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001535 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001536 if (dlc.of_node)
1537 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001538 goto err_defer;
1539 }
1540 } else if (aux_dev->name) {
1541 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001542 dlc.name = aux_dev->name;
1543 dlc.of_node = NULL;
1544 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001545 if (!component)
1546 goto err_defer;
1547 } else {
1548 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1549 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001550 }
1551
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001552 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001553 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001554
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001555 return 0;
1556
1557err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001558 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001559 return -EPROBE_DEFER;
1560}
1561
1562static int soc_probe_aux_devices(struct snd_soc_card *card)
1563{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001564 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001565 int order;
1566 int ret;
1567
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001568 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001569 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001570 if (comp->driver->probe_order == order) {
1571 ret = soc_probe_component(card, comp);
1572 if (ret < 0) {
1573 dev_err(card->dev,
1574 "ASoC: failed to probe aux component %s %d\n",
1575 comp->name, ret);
1576 return ret;
1577 }
1578 }
1579 }
1580 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001581
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001582 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001583}
1584
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001585static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001586{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001587 struct snd_soc_component *comp, *_comp;
1588 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001589
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001590 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001591 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001592 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001593
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001594 if (comp->driver->remove_order == order) {
1595 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001596 /* remove it from the card's aux_comp_list */
1597 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001598 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001599 }
1600 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001601}
1602
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001603/**
1604 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1605 * @rtd: The runtime for which the DAI link format should be changed
1606 * @dai_fmt: The new DAI link format
1607 *
1608 * This function updates the DAI link format for all DAIs connected to the DAI
1609 * link for the specified runtime.
1610 *
1611 * Note: For setups with a static format set the dai_fmt field in the
1612 * corresponding snd_dai_link struct instead of using this function.
1613 *
1614 * Returns 0 on success, otherwise a negative error code.
1615 */
1616int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1617 unsigned int dai_fmt)
1618{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001619 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001620 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001621 unsigned int i;
1622 int ret;
1623
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001624 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001625 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1626 if (ret != 0 && ret != -ENOTSUPP) {
1627 dev_warn(codec_dai->dev,
1628 "ASoC: Failed to set DAI format: %d\n", ret);
1629 return ret;
1630 }
1631 }
1632
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001633 /*
1634 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1635 * the component which has non_legacy_dai_naming is Codec
1636 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001637 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001638 unsigned int inv_dai_fmt;
1639
1640 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1641 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1642 case SND_SOC_DAIFMT_CBM_CFM:
1643 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1644 break;
1645 case SND_SOC_DAIFMT_CBM_CFS:
1646 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1647 break;
1648 case SND_SOC_DAIFMT_CBS_CFM:
1649 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1650 break;
1651 case SND_SOC_DAIFMT_CBS_CFS:
1652 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1653 break;
1654 }
1655
1656 dai_fmt = inv_dai_fmt;
1657 }
1658
1659 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1660 if (ret != 0 && ret != -ENOTSUPP) {
1661 dev_warn(cpu_dai->dev,
1662 "ASoC: Failed to set DAI format: %d\n", ret);
1663 return ret;
1664 }
1665
1666 return 0;
1667}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001668EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001669
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001670#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001671/*
1672 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001673 * separate different DMI fields in the card long name. Only number and
1674 * alphabet characters and a few separator characters are kept.
1675 */
1676static void cleanup_dmi_name(char *name)
1677{
1678 int i, j = 0;
1679
1680 for (i = 0; name[i]; i++) {
1681 if (isalnum(name[i]) || (name[i] == '.')
1682 || (name[i] == '_'))
1683 name[j++] = name[i];
1684 else if (name[i] == '-')
1685 name[j++] = '_';
1686 }
1687
1688 name[j] = '\0';
1689}
1690
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001691/*
1692 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001693 * in the black list.
1694 */
1695static int is_dmi_valid(const char *field)
1696{
1697 int i = 0;
1698
1699 while (dmi_blacklist[i]) {
1700 if (strstr(field, dmi_blacklist[i]))
1701 return 0;
1702 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001703 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001704
1705 return 1;
1706}
1707
Liam Girdwood345233d2017-01-14 16:13:02 +08001708/**
1709 * snd_soc_set_dmi_name() - Register DMI names to card
1710 * @card: The card to register DMI names
1711 * @flavour: The flavour "differentiator" for the card amongst its peers.
1712 *
1713 * An Intel machine driver may be used by many different devices but are
1714 * difficult for userspace to differentiate, since machine drivers ususally
1715 * use their own name as the card short name and leave the card long name
1716 * blank. To differentiate such devices and fix bugs due to lack of
1717 * device-specific configurations, this function allows DMI info to be used
1718 * as the sound card long name, in the format of
1719 * "vendor-product-version-board"
1720 * (Character '-' is used to separate different DMI fields here).
1721 * This will help the user space to load the device-specific Use Case Manager
1722 * (UCM) configurations for the card.
1723 *
1724 * Possible card long names may be:
1725 * DellInc.-XPS139343-01-0310JH
1726 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1727 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1728 *
1729 * This function also supports flavoring the card longname to provide
1730 * the extra differentiation, like "vendor-product-version-board-flavor".
1731 *
1732 * We only keep number and alphabet characters and a few separator characters
1733 * in the card long name since UCM in the user space uses the card long names
1734 * as card configuration directory names and AudoConf cannot support special
1735 * charactors like SPACE.
1736 *
1737 * Returns 0 on success, otherwise a negative error code.
1738 */
1739int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1740{
1741 const char *vendor, *product, *product_version, *board;
1742 size_t longname_buf_size = sizeof(card->snd_card->longname);
1743 size_t len;
1744
1745 if (card->long_name)
1746 return 0; /* long name already set by driver or from DMI */
1747
1748 /* make up dmi long name as: vendor.product.version.board */
1749 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001750 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001751 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1752 return 0;
1753 }
1754
1755 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1756 "%s", vendor);
1757 cleanup_dmi_name(card->dmi_longname);
1758
1759 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001760 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001761 len = strlen(card->dmi_longname);
1762 snprintf(card->dmi_longname + len,
1763 longname_buf_size - len,
1764 "-%s", product);
1765
1766 len++; /* skip the separator "-" */
1767 if (len < longname_buf_size)
1768 cleanup_dmi_name(card->dmi_longname + len);
1769
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001770 /*
1771 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001772 * name in the product version field
1773 */
1774 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001775 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001776 len = strlen(card->dmi_longname);
1777 snprintf(card->dmi_longname + len,
1778 longname_buf_size - len,
1779 "-%s", product_version);
1780
1781 len++;
1782 if (len < longname_buf_size)
1783 cleanup_dmi_name(card->dmi_longname + len);
1784 }
1785 }
1786
1787 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001788 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001789 len = strlen(card->dmi_longname);
1790 snprintf(card->dmi_longname + len,
1791 longname_buf_size - len,
1792 "-%s", board);
1793
1794 len++;
1795 if (len < longname_buf_size)
1796 cleanup_dmi_name(card->dmi_longname + len);
1797 } else if (!product) {
1798 /* fall back to using legacy name */
1799 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1800 return 0;
1801 }
1802
1803 /* Add flavour to dmi long name */
1804 if (flavour) {
1805 len = strlen(card->dmi_longname);
1806 snprintf(card->dmi_longname + len,
1807 longname_buf_size - len,
1808 "-%s", flavour);
1809
1810 len++;
1811 if (len < longname_buf_size)
1812 cleanup_dmi_name(card->dmi_longname + len);
1813 }
1814
1815 /* set the card long name */
1816 card->long_name = card->dmi_longname;
1817
1818 return 0;
1819}
1820EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001821#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001822
Liam Girdwooda655de82018-07-02 16:59:54 +01001823static void soc_check_tplg_fes(struct snd_soc_card *card)
1824{
1825 struct snd_soc_component *component;
1826 const struct snd_soc_component_driver *comp_drv;
1827 struct snd_soc_dai_link *dai_link;
1828 int i;
1829
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001830 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001831
1832 /* does this component override FEs ? */
1833 if (!component->driver->ignore_machine)
1834 continue;
1835
1836 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001837 if (!strcmp(component->driver->ignore_machine,
1838 card->dev->driver->name))
1839 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001840 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001841 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001842 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001843match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001844 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001845 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001846
1847 /* ignore this FE */
1848 if (dai_link->dynamic) {
1849 dai_link->ignore = true;
1850 continue;
1851 }
1852
1853 dev_info(card->dev, "info: override FE DAI link %s\n",
1854 card->dai_link[i].name);
1855
1856 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001857 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001858 dev_err(card->dev, "init platform error");
1859 continue;
1860 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001861 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001862
1863 /* convert non BE into BE */
1864 dai_link->no_pcm = 1;
1865
1866 /* override any BE fixups */
1867 dai_link->be_hw_params_fixup =
1868 component->driver->be_hw_params_fixup;
1869
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001870 /*
1871 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001872 * dai link name if it's NULL to help bind widgets.
1873 */
1874 if (!dai_link->stream_name)
1875 dai_link->stream_name = dai_link->name;
1876 }
1877
1878 /* Inform userspace we are using alternate topology */
1879 if (component->driver->topology_name_prefix) {
1880
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001881 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001882 if (!card->topology_shortname_created) {
1883 comp_drv = component->driver;
1884
1885 snprintf(card->topology_shortname, 32, "%s-%s",
1886 comp_drv->topology_name_prefix,
1887 card->name);
1888 card->topology_shortname_created = true;
1889 }
1890
1891 /* use topology shortname */
1892 card->name = card->topology_shortname;
1893 }
1894 }
1895}
1896
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001897static int soc_cleanup_card_resources(struct snd_soc_card *card)
1898{
1899 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001900 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001901 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001902 card->snd_card = NULL;
1903 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001904
1905 /* remove and free each DAI */
1906 soc_remove_dai_links(card);
1907 soc_remove_pcm_runtimes(card);
1908
1909 /* remove auxiliary devices */
1910 soc_remove_aux_devices(card);
1911
1912 snd_soc_dapm_free(&card->dapm);
1913 soc_cleanup_card_debugfs(card);
1914
1915 /* remove the card */
1916 if (card->remove)
1917 card->remove(card);
1918
1919 return 0;
1920}
1921
Mark Brownb19e6e72012-03-14 21:18:39 +00001922static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923{
Mengdong Lin1a497982015-11-18 02:34:11 -05001924 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001925 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001926 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001927
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001928 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001929 for_each_card_prelinks(card, i, dai_link) {
1930 ret = soc_init_dai_link(card, dai_link);
1931 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001932 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1933 dai_link->name, ret);
1934 mutex_unlock(&client_mutex);
1935 return ret;
1936 }
1937 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001938 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001939
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001940 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1941 card->dapm.dev = card->dev;
1942 card->dapm.card = card;
1943 list_add(&card->dapm.list, &card->dapm_list);
1944
Liam Girdwooda655de82018-07-02 16:59:54 +01001945 /* check whether any platform is ignore machine FE and using topology */
1946 soc_check_tplg_fes(card);
1947
Mark Brownb19e6e72012-03-14 21:18:39 +00001948 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001949 for_each_card_prelinks(card, i, dai_link) {
1950 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001951 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001952 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 }
1954
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001955 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001956 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001957 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001958 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001959 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 }
1961
Mengdong Linf8f80362015-12-02 14:11:22 +08001962 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001963 for_each_card_prelinks(card, i, dai_link)
1964 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001965
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001966 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001967 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968 card->owner, 0, &card->snd_card);
1969 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001970 dev_err(card->dev,
1971 "ASoC: can't create sound card for card %s: %d\n",
1972 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001973 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001974 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001976 soc_init_card_debugfs(card);
1977
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001978#ifdef CONFIG_DEBUG_FS
1979 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1980#endif
1981
Mark Brown88ee1c62011-02-02 10:43:26 +00001982#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001983 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001984 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001985#endif
Andy Green6ed25972008-06-13 16:24:05 +01001986
Mark Brown9a841eb2011-04-12 17:51:37 -07001987 if (card->dapm_widgets)
1988 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1989 card->num_dapm_widgets);
1990
Nicolin Chenf23e8602015-02-14 17:22:49 -08001991 if (card->of_dapm_widgets)
1992 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1993 card->num_of_dapm_widgets);
1994
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995 /* initialise the sound card only once */
1996 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001997 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001999 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000 }
2001
Stephen Warren62ae68f2012-06-08 12:34:23 -06002002 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002003 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002004 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002005 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002006 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002007 dev_err(card->dev,
2008 "ASoC: failed to instantiate card %d\n",
2009 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002010 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002011 }
2012 }
2013 }
2014
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002015 /* probe auxiliary components */
2016 ret = soc_probe_aux_devices(card);
2017 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002018 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002019
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002020 /*
2021 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002022 * Components with topology may bring new DAIs and DAI links.
2023 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002024 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002025 if (soc_is_dai_link_bound(card, dai_link))
2026 continue;
2027
2028 ret = soc_init_dai_link(card, dai_link);
2029 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002030 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002031 ret = soc_bind_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 }
2035
Stephen Warren62ae68f2012-06-08 12:34:23 -06002036 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002037 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002038 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002039 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002040 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002041 dev_err(card->dev,
2042 "ASoC: failed to instantiate card %d\n",
2043 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002044 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002045 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002046 }
2047 }
2048
Mark Brown888df392012-02-16 19:37:51 -08002049 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002050 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002051
Mark Brownb7af1da2011-04-07 19:18:44 +09002052 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002053 snd_soc_add_card_controls(card, card->controls,
2054 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002055
Mark Brownb8ad29d2011-03-02 18:35:51 +00002056 if (card->dapm_routes)
2057 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2058 card->num_dapm_routes);
2059
Nicolin Chenf23e8602015-02-14 17:22:49 -08002060 if (card->of_dapm_routes)
2061 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2062 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002063
Takashi Iwai861886d2017-04-24 08:54:42 +02002064 /* try to set some sane longname if DMI is available */
2065 snd_soc_set_dmi_name(card, NULL);
2066
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002067 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002068 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002069 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2070 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002071 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2072 "%s", card->driver_name ? card->driver_name : card->name);
2073 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2074 switch (card->snd_card->driver[i]) {
2075 case '_':
2076 case '-':
2077 case '\0':
2078 break;
2079 default:
2080 if (!isalnum(card->snd_card->driver[i]))
2081 card->snd_card->driver[i] = '_';
2082 break;
2083 }
2084 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002085
Mark Brown28e9ad92011-03-02 18:36:34 +00002086 if (card->late_probe) {
2087 ret = card->late_probe(card);
2088 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002089 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002090 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002091 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002092 }
2093 }
2094
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002095 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002096
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097 ret = snd_card_register(card->snd_card);
2098 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002099 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2100 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002101 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002102 }
2103
Mark Brown435c5e22008-12-04 15:32:53 +00002104 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002105 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002106 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002107
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002108probe_end:
2109 if (ret < 0)
2110 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002112 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002113 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114
Mark Brownb19e6e72012-03-14 21:18:39 +00002115 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002116}
2117
2118/* probes a new socdev */
2119static int soc_probe(struct platform_device *pdev)
2120{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002121 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002122
Vinod Koul70a7ca32011-01-14 19:22:48 +05302123 /*
2124 * no card, so machine driver should be registering card
2125 * we should not be here in that case so ret error
2126 */
2127 if (!card)
2128 return -EINVAL;
2129
Mark Brownfe4085e2012-03-02 13:07:41 +00002130 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002131 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002132 card->name);
2133
Mark Brown435c5e22008-12-04 15:32:53 +00002134 /* Bodge while we unpick instantiation */
2135 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002136
Mark Brown28d528c2012-08-09 18:45:23 +01002137 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138}
2139
2140/* removes a socdev */
2141static int soc_remove(struct platform_device *pdev)
2142{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002143 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144
Mark Brownc5af3a22008-11-28 13:29:45 +00002145 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146 return 0;
2147}
2148
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002149int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002150{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002151 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002152 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002153
2154 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002155 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002156
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002157 /*
2158 * Flush out pmdown_time work - we actually do want to run it
2159 * now, we're shutting down so no imminent restart.
2160 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002161 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002163 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002164
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002165 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002166 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002167 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002168 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002169 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002170
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002171 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002172 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002173 pinctrl_pm_select_sleep_state(codec_dai->dev);
2174 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002175 }
2176
Mark Brown416356f2009-06-30 19:05:15 +01002177 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002178}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002179EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002180
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002181const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302182 .suspend = snd_soc_suspend,
2183 .resume = snd_soc_resume,
2184 .freeze = snd_soc_suspend,
2185 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002186 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302187 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002188};
Stephen Warrendeb26072011-04-05 19:35:30 -06002189EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002190
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002191/* ASoC platform driver */
2192static struct platform_driver soc_driver = {
2193 .driver = {
2194 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002195 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196 },
2197 .probe = soc_probe,
2198 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199};
2200
Mark Brown096e49d2009-07-05 15:12:22 +01002201/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202 * snd_soc_cnew - create new control
2203 * @_template: control template
2204 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002205 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002206 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002207 *
2208 * Create a new mixer control from a template control.
2209 *
2210 * Returns 0 for success, else error.
2211 */
2212struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002213 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002214 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215{
2216 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002217 struct snd_kcontrol *kcontrol;
2218 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219
2220 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221 template.index = 0;
2222
Mark Brownefb7ac32011-03-08 17:23:24 +00002223 if (!long_name)
2224 long_name = template.name;
2225
2226 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002227 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002228 if (!name)
2229 return NULL;
2230
Mark Brownefb7ac32011-03-08 17:23:24 +00002231 template.name = name;
2232 } else {
2233 template.name = long_name;
2234 }
2235
2236 kcontrol = snd_ctl_new1(&template, data);
2237
2238 kfree(name);
2239
2240 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241}
2242EXPORT_SYMBOL_GPL(snd_soc_cnew);
2243
Liam Girdwood022658b2012-02-03 17:43:09 +00002244static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2245 const struct snd_kcontrol_new *controls, int num_controls,
2246 const char *prefix, void *data)
2247{
2248 int err, i;
2249
2250 for (i = 0; i < num_controls; i++) {
2251 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002252
Liam Girdwood022658b2012-02-03 17:43:09 +00002253 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2254 control->name, prefix));
2255 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002256 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2257 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002258 return err;
2259 }
2260 }
2261
2262 return 0;
2263}
2264
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002265struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2266 const char *name)
2267{
2268 struct snd_card *card = soc_card->snd_card;
2269 struct snd_kcontrol *kctl;
2270
2271 if (unlikely(!name))
2272 return NULL;
2273
2274 list_for_each_entry(kctl, &card->controls, list)
2275 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2276 return kctl;
2277 return NULL;
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2280
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002282 * snd_soc_add_component_controls - Add an array of controls to a component.
2283 *
2284 * @component: Component to add controls to
2285 * @controls: Array of controls to add
2286 * @num_controls: Number of elements in the array
2287 *
2288 * Return: 0 for success, else error.
2289 */
2290int snd_soc_add_component_controls(struct snd_soc_component *component,
2291 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2292{
2293 struct snd_card *card = component->card->snd_card;
2294
2295 return snd_soc_add_controls(card, component->dev, controls,
2296 num_controls, component->name_prefix, component);
2297}
2298EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2299
2300/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002301 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2302 * Convenience function to add a list of controls.
2303 *
2304 * @soc_card: SoC card to add controls to
2305 * @controls: array of controls to add
2306 * @num_controls: number of elements in the array
2307 *
2308 * Return 0 for success, else error.
2309 */
2310int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2311 const struct snd_kcontrol_new *controls, int num_controls)
2312{
2313 struct snd_card *card = soc_card->snd_card;
2314
2315 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2316 NULL, soc_card);
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2319
2320/**
2321 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2322 * Convienience function to add a list of controls.
2323 *
2324 * @dai: DAI to add controls to
2325 * @controls: array of controls to add
2326 * @num_controls: number of elements in the array
2327 *
2328 * Return 0 for success, else error.
2329 */
2330int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2331 const struct snd_kcontrol_new *controls, int num_controls)
2332{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002333 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002334
2335 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2336 NULL, dai);
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2339
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002340static int snd_soc_bind_card(struct snd_soc_card *card)
2341{
2342 struct snd_soc_pcm_runtime *rtd;
2343 int ret;
2344
2345 ret = snd_soc_instantiate_card(card);
2346 if (ret != 0)
2347 return ret;
2348
2349 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002350 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002351 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2352 struct snd_soc_dai *codec_dai;
2353 int j;
2354
2355 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2356 if (!codec_dai->active)
2357 pinctrl_pm_select_sleep_state(codec_dai->dev);
2358 }
2359
2360 if (!cpu_dai->active)
2361 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2362 }
2363
2364 return ret;
2365}
2366
Mark Brownc5af3a22008-11-28 13:29:45 +00002367/**
2368 * snd_soc_register_card - Register a card with the ASoC core
2369 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002370 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002371 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002372 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302373int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002374{
2375 if (!card->name || !card->dev)
2376 return -EINVAL;
2377
Mark Browned77cc12011-05-03 18:25:34 +01002378 dev_set_drvdata(card->dev, card);
2379
Stephen Warren111c6412011-01-28 14:26:35 -07002380 snd_soc_initialize_card_lists(card);
2381
Mengdong Linf8f80362015-12-02 14:11:22 +08002382 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002383
Mengdong Lin1a497982015-11-18 02:34:11 -05002384 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002385 card->num_rtd = 0;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002386
Mark Browndb432b42011-10-03 21:06:40 +01002387 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002388 INIT_LIST_HEAD(&card->dobj_list);
Mark Browndee89c42008-11-18 22:11:38 +00002389 card->instantiated = 0;
2390 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002391 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002392 spin_lock_init(&card->dpcm_lock);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002393
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002394 return snd_soc_bind_card(card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002395}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302396EXPORT_SYMBOL_GPL(snd_soc_register_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002397
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002398static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2399{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002400 struct snd_soc_pcm_runtime *rtd;
2401 int order;
2402
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002403 if (card->instantiated) {
2404 card->instantiated = false;
2405 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002406 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002407
2408 /* remove all components used by DAI links on this card */
2409 for_each_comp_order(order) {
2410 for_each_card_rtds(card, rtd) {
2411 soc_remove_link_components(card, rtd, order);
2412 }
2413 }
2414
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002415 soc_cleanup_card_resources(card);
2416 if (!unregister)
2417 list_add(&card->list, &unbind_card_list);
2418 } else {
2419 if (unregister)
2420 list_del(&card->list);
2421 }
2422}
2423
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002424/**
2425 * snd_soc_unregister_card - Unregister a card with the ASoC core
2426 *
Mark Browndee89c42008-11-18 22:11:38 +00002427 * @card: Card to unregister
2428 *
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002429 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302430int snd_soc_unregister_card(struct snd_soc_card *card)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002431{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002432 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002433 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002434 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002435 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002436
2437 return 0;
2438}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302439EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002440
2441/*
2442 * Simplify DAI link configuration by removing ".-1" from device names
2443 * and sanitizing names.
2444 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002445static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002446{
2447 char *found, name[NAME_SIZE];
2448 int id1, id2;
2449
2450 if (dev_name(dev) == NULL)
2451 return NULL;
2452
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002453 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002454
2455 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002456 found = strstr(name, dev->driver->name);
2457 if (found) {
2458 /* get ID */
2459 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2460
2461 /* discard ID from name if ID == -1 */
2462 if (*id == -1)
2463 found[strlen(dev->driver->name)] = '\0';
2464 }
2465
2466 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002467 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002468 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2469 char tmp[NAME_SIZE];
2470
2471 /* create unique ID number from I2C addr and bus */
2472 *id = ((id1 & 0xffff) << 16) + id2;
2473
2474 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002475 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2476 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002477 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002478 } else
2479 *id = 0;
2480 }
2481
2482 return kstrdup(name, GFP_KERNEL);
2483}
2484
2485/*
2486 * Simplify DAI link naming for single devices with multiple DAIs by removing
2487 * any ".-1" and using the DAI name (instead of device name).
2488 */
2489static inline char *fmt_multiple_name(struct device *dev,
2490 struct snd_soc_dai_driver *dai_drv)
2491{
2492 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002493 dev_err(dev,
2494 "ASoC: error - multiple DAI %s registered with no name\n",
2495 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002496 return NULL;
2497 }
2498
2499 return kstrdup(dai_drv->name, GFP_KERNEL);
2500}
2501
2502/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002503 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002504 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002505 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002506 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002507static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002508{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002509 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002510
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002511 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002512 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2513 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002514 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002515 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002516 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002517 }
Mark Brown91151712008-11-30 23:31:24 +00002518}
Mark Brown91151712008-11-30 23:31:24 +00002519
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002520/* Create a DAI and add it to the component's DAI list */
2521static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2522 struct snd_soc_dai_driver *dai_drv,
2523 bool legacy_dai_naming)
2524{
2525 struct device *dev = component->dev;
2526 struct snd_soc_dai *dai;
2527
2528 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2529
2530 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2531 if (dai == NULL)
2532 return NULL;
2533
2534 /*
2535 * Back in the old days when we still had component-less DAIs,
2536 * instead of having a static name, component-less DAIs would
2537 * inherit the name of the parent device so it is possible to
2538 * register multiple instances of the DAI. We still need to keep
2539 * the same naming style even though those DAIs are not
2540 * component-less anymore.
2541 */
2542 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002543 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002544 dai->name = fmt_single_name(dev, &dai->id);
2545 } else {
2546 dai->name = fmt_multiple_name(dev, dai_drv);
2547 if (dai_drv->id)
2548 dai->id = dai_drv->id;
2549 else
2550 dai->id = component->num_dai;
2551 }
2552 if (dai->name == NULL) {
2553 kfree(dai);
2554 return NULL;
2555 }
2556
2557 dai->component = component;
2558 dai->dev = dev;
2559 dai->driver = dai_drv;
2560 if (!dai->driver->ops)
2561 dai->driver->ops = &null_dai_ops;
2562
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002563 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002564 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002565 component->num_dai++;
2566
2567 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2568 return dai;
2569}
2570
Mark Brown91151712008-11-30 23:31:24 +00002571/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002572 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002573 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002574 * @component: The component the DAIs are registered for
2575 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002576 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002577 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002578static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002579 struct snd_soc_dai_driver *dai_drv,
2580 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002581{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002582 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002583 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002584 unsigned int i;
2585 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002586
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002587 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002588
2589 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002590
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002591 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2592 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002593 if (dai == NULL) {
2594 ret = -ENOMEM;
2595 goto err;
2596 }
Mark Brown91151712008-11-30 23:31:24 +00002597 }
2598
2599 return 0;
2600
2601err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002602 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002603
2604 return ret;
2605}
Mark Brown91151712008-11-30 23:31:24 +00002606
Mengdong Lin68003e62015-12-31 16:40:43 +08002607/**
2608 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2609 *
2610 * @component: The component the DAIs are registered for
2611 * @dai_drv: DAI driver to use for the DAI
2612 *
2613 * Topology can use this API to register DAIs when probing a component.
2614 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2615 * will be freed in the component cleanup.
2616 */
2617int snd_soc_register_dai(struct snd_soc_component *component,
2618 struct snd_soc_dai_driver *dai_drv)
2619{
2620 struct snd_soc_dapm_context *dapm =
2621 snd_soc_component_get_dapm(component);
2622 struct snd_soc_dai *dai;
2623 int ret;
2624
2625 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2626 dev_err(component->dev, "Invalid dai type %d\n",
2627 dai_drv->dobj.type);
2628 return -EINVAL;
2629 }
2630
2631 lockdep_assert_held(&client_mutex);
2632 dai = soc_add_dai(component, dai_drv, false);
2633 if (!dai)
2634 return -ENOMEM;
2635
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002636 /*
2637 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002638 * also add routes that need these widgets as source or sink.
2639 */
2640 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2641 if (ret != 0) {
2642 dev_err(component->dev,
2643 "Failed to create DAI widgets %d\n", ret);
2644 }
2645
2646 return ret;
2647}
2648EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2649
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002650static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2651 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002652{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002653 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002654
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002655 component->driver->seq_notifier(component, type, subseq);
2656}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002657
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002658static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2659 int event)
2660{
2661 struct snd_soc_component *component = dapm->component;
2662
2663 return component->driver->stream_event(component, event);
2664}
2665
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002666static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2667 enum snd_soc_bias_level level)
2668{
2669 struct snd_soc_component *component = dapm->component;
2670
2671 return component->driver->set_bias_level(component, level);
2672}
2673
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002674static int snd_soc_component_initialize(struct snd_soc_component *component,
2675 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002676{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002677 struct snd_soc_dapm_context *dapm;
2678
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002679 component->name = fmt_single_name(dev, &component->id);
2680 if (!component->name) {
2681 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002682 return -ENOMEM;
2683 }
2684
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002685 component->dev = dev;
2686 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002687
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002688 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002689 dapm->dev = dev;
2690 dapm->component = component;
2691 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002692 dapm->idle_bias_off = !driver->idle_bias_on;
2693 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002694 if (driver->seq_notifier)
2695 dapm->seq_notifier = snd_soc_component_seq_notifier;
2696 if (driver->stream_event)
2697 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002698 if (driver->set_bias_level)
2699 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002700
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002701 INIT_LIST_HEAD(&component->dai_list);
2702 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002703
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002704 return 0;
2705}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002706
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002707static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002708{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002709 int val_bytes = regmap_get_val_bytes(component->regmap);
2710
2711 /* Errors are legitimate for non-integer byte multiples */
2712 if (val_bytes > 0)
2713 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002714}
2715
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002716#ifdef CONFIG_REGMAP
2717
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002718/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002719 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2720 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002721 * @component: The component for which to initialize the regmap instance
2722 * @regmap: The regmap instance that should be used by the component
2723 *
2724 * This function allows deferred assignment of the regmap instance that is
2725 * associated with the component. Only use this if the regmap instance is not
2726 * yet ready when the component is registered. The function must also be called
2727 * before the first IO attempt of the component.
2728 */
2729void snd_soc_component_init_regmap(struct snd_soc_component *component,
2730 struct regmap *regmap)
2731{
2732 component->regmap = regmap;
2733 snd_soc_component_setup_regmap(component);
2734}
2735EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2736
2737/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002738 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2739 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002740 * @component: The component for which to de-initialize the regmap instance
2741 *
2742 * Calls regmap_exit() on the regmap instance associated to the component and
2743 * removes the regmap instance from the component.
2744 *
2745 * This function should only be used if snd_soc_component_init_regmap() was used
2746 * to initialize the regmap instance.
2747 */
2748void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2749{
2750 regmap_exit(component->regmap);
2751 component->regmap = NULL;
2752}
2753EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2754
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002755#endif
2756
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002757static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002758{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002759 mutex_lock(&client_mutex);
2760
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002761 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002762 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002763 component->regmap = dev_get_regmap(component->dev,
2764 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002765 if (component->regmap)
2766 snd_soc_component_setup_regmap(component);
2767 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002768
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002769 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002770 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002771 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002772
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002773 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002774}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002775
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002776static void snd_soc_component_cleanup(struct snd_soc_component *component)
2777{
2778 snd_soc_unregister_dais(component);
2779 kfree(component->name);
2780}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002781
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002782static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2783{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002784 struct snd_soc_card *card = component->card;
2785
2786 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002787 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002788
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002789 list_del(&component->list);
2790}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002791
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002792#define ENDIANNESS_MAP(name) \
2793 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2794static u64 endianness_format_map[] = {
2795 ENDIANNESS_MAP(S16_),
2796 ENDIANNESS_MAP(U16_),
2797 ENDIANNESS_MAP(S24_),
2798 ENDIANNESS_MAP(U24_),
2799 ENDIANNESS_MAP(S32_),
2800 ENDIANNESS_MAP(U32_),
2801 ENDIANNESS_MAP(S24_3),
2802 ENDIANNESS_MAP(U24_3),
2803 ENDIANNESS_MAP(S20_3),
2804 ENDIANNESS_MAP(U20_3),
2805 ENDIANNESS_MAP(S18_3),
2806 ENDIANNESS_MAP(U18_3),
2807 ENDIANNESS_MAP(FLOAT_),
2808 ENDIANNESS_MAP(FLOAT64_),
2809 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2810};
2811
2812/*
2813 * Fix up the DAI formats for endianness: codecs don't actually see
2814 * the endianness of the data but we're using the CPU format
2815 * definitions which do need to include endianness so we ensure that
2816 * codec DAIs always have both big and little endian variants set.
2817 */
2818static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2819{
2820 int i;
2821
2822 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2823 if (stream->formats & endianness_format_map[i])
2824 stream->formats |= endianness_format_map[i];
2825}
2826
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002827static void snd_soc_try_rebind_card(void)
2828{
2829 struct snd_soc_card *card, *c;
2830
2831 if (!list_empty(&unbind_card_list)) {
2832 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2833 if (!snd_soc_bind_card(card))
2834 list_del(&card->list);
2835 }
2836 }
2837}
2838
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002839int snd_soc_add_component(struct device *dev,
2840 struct snd_soc_component *component,
2841 const struct snd_soc_component_driver *component_driver,
2842 struct snd_soc_dai_driver *dai_drv,
2843 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002844{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002845 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002846 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002847
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002848 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002849 if (ret)
2850 goto err_free;
2851
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002852 if (component_driver->endianness) {
2853 for (i = 0; i < num_dai; i++) {
2854 convert_endianness_formats(&dai_drv[i].playback);
2855 convert_endianness_formats(&dai_drv[i].capture);
2856 }
2857 }
2858
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002859 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002860 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002861 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002862 goto err_cleanup;
2863 }
2864
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002865 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002866 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002867
2868 return 0;
2869
2870err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002871 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002872err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002873 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002874}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002875EXPORT_SYMBOL_GPL(snd_soc_add_component);
2876
2877int snd_soc_register_component(struct device *dev,
2878 const struct snd_soc_component_driver *component_driver,
2879 struct snd_soc_dai_driver *dai_drv,
2880 int num_dai)
2881{
2882 struct snd_soc_component *component;
2883
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002884 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002885 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002886 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002887
2888 return snd_soc_add_component(dev, component, component_driver,
2889 dai_drv, num_dai);
2890}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002891EXPORT_SYMBOL_GPL(snd_soc_register_component);
2892
2893/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002894 * snd_soc_unregister_component - Unregister all related component
2895 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002896 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002897 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002898 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002899static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002900{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002901 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002902 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002903
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002904 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002905 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002906 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002907 continue;
2908
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002909 snd_soc_tplg_component_remove(component,
2910 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002911 snd_soc_component_del_unlocked(component);
2912 found = 1;
2913 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002914 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002915 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002916
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002917 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002918 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002919
2920 return found;
2921}
2922
2923void snd_soc_unregister_component(struct device *dev)
2924{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002925 while (__snd_soc_unregister_component(dev))
2926 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002927}
2928EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2929
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002930struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
2931 const char *driver_name)
2932{
2933 struct snd_soc_component *component;
2934 struct snd_soc_component *ret;
2935
2936 ret = NULL;
2937 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002938 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002939 if (dev != component->dev)
2940 continue;
2941
2942 if (driver_name &&
2943 (driver_name != component->driver->name) &&
2944 (strcmp(component->driver->name, driver_name) != 0))
2945 continue;
2946
2947 ret = component;
2948 break;
2949 }
2950 mutex_unlock(&client_mutex);
2951
2952 return ret;
2953}
2954EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
2955
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002956/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002957int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2958 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002959{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002960 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002961 int ret;
2962
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302963 if (!card->dev) {
2964 pr_err("card->dev is not set before calling %s\n", __func__);
2965 return -EINVAL;
2966 }
2967
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002968 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302969
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002970 ret = of_property_read_string_index(np, propname, 0, &card->name);
2971 /*
2972 * EINVAL means the property does not exist. This is fine providing
2973 * card->name was previously set, which is checked later in
2974 * snd_soc_register_card.
2975 */
2976 if (ret < 0 && ret != -EINVAL) {
2977 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002978 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002979 propname, ret);
2980 return ret;
2981 }
2982
2983 return 0;
2984}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002985EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002986
Xiubo Li9a6d4862014-02-08 15:59:52 +08002987static const struct snd_soc_dapm_widget simple_widgets[] = {
2988 SND_SOC_DAPM_MIC("Microphone", NULL),
2989 SND_SOC_DAPM_LINE("Line", NULL),
2990 SND_SOC_DAPM_HP("Headphone", NULL),
2991 SND_SOC_DAPM_SPK("Speaker", NULL),
2992};
2993
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002994int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002995 const char *propname)
2996{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002997 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002998 struct snd_soc_dapm_widget *widgets;
2999 const char *template, *wname;
3000 int i, j, num_widgets, ret;
3001
3002 num_widgets = of_property_count_strings(np, propname);
3003 if (num_widgets < 0) {
3004 dev_err(card->dev,
3005 "ASoC: Property '%s' does not exist\n", propname);
3006 return -EINVAL;
3007 }
3008 if (num_widgets & 1) {
3009 dev_err(card->dev,
3010 "ASoC: Property '%s' length is not even\n", propname);
3011 return -EINVAL;
3012 }
3013
3014 num_widgets /= 2;
3015 if (!num_widgets) {
3016 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3017 propname);
3018 return -EINVAL;
3019 }
3020
3021 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3022 GFP_KERNEL);
3023 if (!widgets) {
3024 dev_err(card->dev,
3025 "ASoC: Could not allocate memory for widgets\n");
3026 return -ENOMEM;
3027 }
3028
3029 for (i = 0; i < num_widgets; i++) {
3030 ret = of_property_read_string_index(np, propname,
3031 2 * i, &template);
3032 if (ret) {
3033 dev_err(card->dev,
3034 "ASoC: Property '%s' index %d read error:%d\n",
3035 propname, 2 * i, ret);
3036 return -EINVAL;
3037 }
3038
3039 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3040 if (!strncmp(template, simple_widgets[j].name,
3041 strlen(simple_widgets[j].name))) {
3042 widgets[i] = simple_widgets[j];
3043 break;
3044 }
3045 }
3046
3047 if (j >= ARRAY_SIZE(simple_widgets)) {
3048 dev_err(card->dev,
3049 "ASoC: DAPM widget '%s' is not supported\n",
3050 template);
3051 return -EINVAL;
3052 }
3053
3054 ret = of_property_read_string_index(np, propname,
3055 (2 * i) + 1,
3056 &wname);
3057 if (ret) {
3058 dev_err(card->dev,
3059 "ASoC: Property '%s' index %d read error:%d\n",
3060 propname, (2 * i) + 1, ret);
3061 return -EINVAL;
3062 }
3063
3064 widgets[i].name = wname;
3065 }
3066
Nicolin Chenf23e8602015-02-14 17:22:49 -08003067 card->of_dapm_widgets = widgets;
3068 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003069
3070 return 0;
3071}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003072EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003073
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003074int snd_soc_of_get_slot_mask(struct device_node *np,
3075 const char *prop_name,
3076 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003077{
3078 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003079 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003080 int i;
3081
3082 if (!of_slot_mask)
3083 return 0;
3084 val /= sizeof(u32);
3085 for (i = 0; i < val; i++)
3086 if (be32_to_cpup(&of_slot_mask[i]))
3087 *mask |= (1 << i);
3088
3089 return val;
3090}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003091EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003092
Xiubo Li89c67852014-02-14 09:34:35 +08003093int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003094 unsigned int *tx_mask,
3095 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003096 unsigned int *slots,
3097 unsigned int *slot_width)
3098{
3099 u32 val;
3100 int ret;
3101
Jyri Sarha61310842015-09-09 21:27:43 +03003102 if (tx_mask)
3103 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3104 if (rx_mask)
3105 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3106
Xiubo Li89c67852014-02-14 09:34:35 +08003107 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3108 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3109 if (ret)
3110 return ret;
3111
3112 if (slots)
3113 *slots = val;
3114 }
3115
3116 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3117 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3118 if (ret)
3119 return ret;
3120
3121 if (slot_width)
3122 *slot_width = val;
3123 }
3124
3125 return 0;
3126}
3127EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3128
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003129void snd_soc_of_parse_node_prefix(struct device_node *np,
3130 struct snd_soc_codec_conf *codec_conf,
3131 struct device_node *of_node,
3132 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003133{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003134 const char *str;
3135 int ret;
3136
3137 ret = of_property_read_string(np, propname, &str);
3138 if (ret < 0) {
3139 /* no prefix is not error */
3140 return;
3141 }
3142
3143 codec_conf->of_node = of_node;
3144 codec_conf->name_prefix = str;
3145}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003146EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003147
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003148int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003149 const char *propname)
3150{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003151 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003152 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003153 struct snd_soc_dapm_route *routes;
3154 int i, ret;
3155
3156 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003157 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003158 dev_err(card->dev,
3159 "ASoC: Property '%s' does not exist or its length is not even\n",
3160 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003161 return -EINVAL;
3162 }
3163 num_routes /= 2;
3164 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003165 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003166 propname);
3167 return -EINVAL;
3168 }
3169
Kees Cooka86854d2018-06-12 14:07:58 -07003170 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003171 GFP_KERNEL);
3172 if (!routes) {
3173 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003174 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003175 return -EINVAL;
3176 }
3177
3178 for (i = 0; i < num_routes; i++) {
3179 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003180 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003181 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003182 dev_err(card->dev,
3183 "ASoC: Property '%s' index %d could not be read: %d\n",
3184 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003185 return -EINVAL;
3186 }
3187 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003188 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003189 if (ret) {
3190 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003191 "ASoC: Property '%s' index %d could not be read: %d\n",
3192 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003193 return -EINVAL;
3194 }
3195 }
3196
Nicolin Chenf23e8602015-02-14 17:22:49 -08003197 card->num_of_dapm_routes = num_routes;
3198 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003199
3200 return 0;
3201}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003202EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003203
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003204unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003205 const char *prefix,
3206 struct device_node **bitclkmaster,
3207 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003208{
3209 int ret, i;
3210 char prop[128];
3211 unsigned int format = 0;
3212 int bit, frame;
3213 const char *str;
3214 struct {
3215 char *name;
3216 unsigned int val;
3217 } of_fmt_table[] = {
3218 { "i2s", SND_SOC_DAIFMT_I2S },
3219 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3220 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3221 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3222 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3223 { "ac97", SND_SOC_DAIFMT_AC97 },
3224 { "pdm", SND_SOC_DAIFMT_PDM},
3225 { "msb", SND_SOC_DAIFMT_MSB },
3226 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003227 };
3228
3229 if (!prefix)
3230 prefix = "";
3231
3232 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003233 * check "dai-format = xxx"
3234 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003235 * SND_SOC_DAIFMT_FORMAT_MASK area
3236 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003237 ret = of_property_read_string(np, "dai-format", &str);
3238 if (ret < 0) {
3239 snprintf(prop, sizeof(prop), "%sformat", prefix);
3240 ret = of_property_read_string(np, prop, &str);
3241 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003242 if (ret == 0) {
3243 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3244 if (strcmp(str, of_fmt_table[i].name) == 0) {
3245 format |= of_fmt_table[i].val;
3246 break;
3247 }
3248 }
3249 }
3250
3251 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003252 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003253 * SND_SOC_DAIFMT_CLOCK_MASK area
3254 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003255 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003256 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003257 format |= SND_SOC_DAIFMT_CONT;
3258 else
3259 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003260
3261 /*
3262 * check "[prefix]bitclock-inversion"
3263 * check "[prefix]frame-inversion"
3264 * SND_SOC_DAIFMT_INV_MASK area
3265 */
3266 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3267 bit = !!of_get_property(np, prop, NULL);
3268
3269 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3270 frame = !!of_get_property(np, prop, NULL);
3271
3272 switch ((bit << 4) + frame) {
3273 case 0x11:
3274 format |= SND_SOC_DAIFMT_IB_IF;
3275 break;
3276 case 0x10:
3277 format |= SND_SOC_DAIFMT_IB_NF;
3278 break;
3279 case 0x01:
3280 format |= SND_SOC_DAIFMT_NB_IF;
3281 break;
3282 default:
3283 /* SND_SOC_DAIFMT_NB_NF is default */
3284 break;
3285 }
3286
3287 /*
3288 * check "[prefix]bitclock-master"
3289 * check "[prefix]frame-master"
3290 * SND_SOC_DAIFMT_MASTER_MASK area
3291 */
3292 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3293 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003294 if (bit && bitclkmaster)
3295 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003296
3297 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3298 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003299 if (frame && framemaster)
3300 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003301
3302 switch ((bit << 4) + frame) {
3303 case 0x11:
3304 format |= SND_SOC_DAIFMT_CBM_CFM;
3305 break;
3306 case 0x10:
3307 format |= SND_SOC_DAIFMT_CBM_CFS;
3308 break;
3309 case 0x01:
3310 format |= SND_SOC_DAIFMT_CBS_CFM;
3311 break;
3312 default:
3313 format |= SND_SOC_DAIFMT_CBS_CFS;
3314 break;
3315 }
3316
3317 return format;
3318}
3319EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3320
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003321int snd_soc_get_dai_id(struct device_node *ep)
3322{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003323 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003324 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003325 int ret;
3326
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003327 dlc.of_node = of_graph_get_port_parent(ep);
3328 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003329 /*
3330 * For example HDMI case, HDMI has video/sound port,
3331 * but ALSA SoC needs sound port number only.
3332 * Thus counting HDMI DT port/endpoint doesn't work.
3333 * Then, it should have .of_xlate_dai_id
3334 */
3335 ret = -ENOTSUPP;
3336 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003337 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003338 if (component &&
3339 component->driver->of_xlate_dai_id)
3340 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003341 mutex_unlock(&client_mutex);
3342
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003343 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003344
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003345 return ret;
3346}
3347EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3348
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003349int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003350 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003351{
3352 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003353 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003354 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003355
3356 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003357 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003358 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003359
3360 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003361 continue;
3362
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003363 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003364 ret = pos->driver->of_xlate_dai_name(pos,
3365 args,
3366 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003367 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003368 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003369 int id = -1;
3370
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003371 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003372 case 0:
3373 id = 0; /* same as dai_drv[0] */
3374 break;
3375 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003376 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003377 break;
3378 default:
3379 /* not supported */
3380 break;
3381 }
3382
3383 if (id < 0 || id >= pos->num_dai) {
3384 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003385 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003386 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003387
3388 ret = 0;
3389
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003390 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003391 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003392 if (id == 0)
3393 break;
3394 id--;
3395 }
3396
3397 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003398 if (!*dai_name)
3399 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003400 }
3401
Kuninori Morimotocb470082013-09-10 17:39:56 -07003402 break;
3403 }
3404 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003405 return ret;
3406}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003407EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003408
3409int snd_soc_of_get_dai_name(struct device_node *of_node,
3410 const char **dai_name)
3411{
3412 struct of_phandle_args args;
3413 int ret;
3414
3415 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3416 "#sound-dai-cells", 0, &args);
3417 if (ret)
3418 return ret;
3419
3420 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003421
3422 of_node_put(args.np);
3423
3424 return ret;
3425}
3426EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3427
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003428/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003429 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3430 * @dai_link: DAI link
3431 *
3432 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3433 */
3434void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3435{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003436 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003437 int index;
3438
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003439 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003440 if (!component->of_node)
3441 break;
3442 of_node_put(component->of_node);
3443 component->of_node = NULL;
3444 }
3445}
3446EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3447
3448/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003449 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3450 * @dev: Card device
3451 * @of_node: Device node
3452 * @dai_link: DAI link
3453 *
3454 * Builds an array of CODEC DAI components from the DAI link property
3455 * 'sound-dai'.
3456 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003457 * The device nodes in the array (of_node) must be dereferenced by calling
3458 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003459 *
3460 * Returns 0 for success
3461 */
3462int snd_soc_of_get_dai_link_codecs(struct device *dev,
3463 struct device_node *of_node,
3464 struct snd_soc_dai_link *dai_link)
3465{
3466 struct of_phandle_args args;
3467 struct snd_soc_dai_link_component *component;
3468 char *name;
3469 int index, num_codecs, ret;
3470
3471 /* Count the number of CODECs */
3472 name = "sound-dai";
3473 num_codecs = of_count_phandle_with_args(of_node, name,
3474 "#sound-dai-cells");
3475 if (num_codecs <= 0) {
3476 if (num_codecs == -ENOENT)
3477 dev_err(dev, "No 'sound-dai' property\n");
3478 else
3479 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3480 return num_codecs;
3481 }
Kees Cooka86854d2018-06-12 14:07:58 -07003482 component = devm_kcalloc(dev,
3483 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003484 GFP_KERNEL);
3485 if (!component)
3486 return -ENOMEM;
3487 dai_link->codecs = component;
3488 dai_link->num_codecs = num_codecs;
3489
3490 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003491 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003492 ret = of_parse_phandle_with_args(of_node, name,
3493 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003494 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003495 if (ret)
3496 goto err;
3497 component->of_node = args.np;
3498 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3499 if (ret < 0)
3500 goto err;
3501 }
3502 return 0;
3503err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003504 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003505 dai_link->codecs = NULL;
3506 dai_link->num_codecs = 0;
3507 return ret;
3508}
3509EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3510
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003511static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003512{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003513 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003514 snd_soc_util_init();
3515
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003516 return platform_driver_register(&soc_driver);
3517}
Mark Brown4abe8e12010-10-12 17:41:03 +01003518module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003519
Mark Brown7d8c16a2008-11-30 22:11:24 +00003520static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003521{
Mark Brownfb257892011-04-28 10:57:54 +01003522 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003523 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003524
Mark Brown3ff3f642008-05-19 12:32:25 +02003525 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003526}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003527module_exit(snd_soc_exit);
3528
3529/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003530MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003531MODULE_DESCRIPTION("ALSA SoC Core");
3532MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003533MODULE_ALIAS("platform:soc-audio");