blob: ea93edd328a2d9a6d30591bb522aeeef180d830a [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 Morimoto999f7f52018-05-08 03:20:24 +00001285 if (component->driver->probe) {
1286 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001288 dev_err(component->dev,
1289 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001290 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001291 }
1292 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001293 WARN(dapm->idle_bias_off &&
1294 dapm->bias_level != SND_SOC_BIAS_OFF,
1295 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1296 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001297
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001298 /* machine specific init */
1299 if (component->init) {
1300 ret = component->init(component);
1301 if (ret < 0) {
1302 dev_err(component->dev,
1303 "Failed to do machine specific init %d\n", ret);
1304 goto err_probe;
1305 }
1306 }
1307
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001308 if (component->driver->controls)
1309 snd_soc_add_component_controls(component,
1310 component->driver->controls,
1311 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001312 if (component->driver->dapm_routes)
1313 snd_soc_dapm_add_routes(dapm,
1314 component->driver->dapm_routes,
1315 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001316
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001317 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001318 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001319 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001320
Jarkko Nikula70d293312011-01-27 16:24:22 +02001321err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001322 if (ret < 0)
1323 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001324
1325 return ret;
1326}
1327
Mark Brown36ae1a92012-01-06 17:12:45 -08001328static void rtd_release(struct device *dev)
1329{
1330 kfree(dev);
1331}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001332
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001333static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1334 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001335{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 int ret = 0;
1337
Jarkko Nikula589c3562010-12-06 16:27:07 +02001338 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001339 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1340 if (!rtd->dev)
1341 return -ENOMEM;
1342 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001343 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001344 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001345 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001346 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001347 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001348 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001349 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1350 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1351 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1352 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001353 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001354 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001355 /* calling put_device() here to free the rtd->dev */
1356 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001357 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001358 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001359 return ret;
1360 }
1361 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001362 return 0;
1363}
1364
Mengdong Lin1a497982015-11-18 02:34:11 -05001365static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001366 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001367{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001368 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001369 struct snd_soc_rtdcom_list *rtdcom;
1370 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001371
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001372 for_each_rtdcom(rtd, rtdcom) {
1373 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001374
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001375 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001376 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001377 if (ret < 0)
1378 return ret;
1379 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001380 }
1381
Stephen Warren62ae68f2012-06-08 12:34:23 -06001382 return 0;
1383}
1384
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001385static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001386{
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001387 int ret;
1388
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001389 if (dai->probed ||
1390 dai->driver->probe_order != order)
1391 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001392
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001393 ret = snd_soc_dai_probe(dai);
1394 if (ret < 0) {
1395 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1396 dai->name, ret);
1397 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001398 }
1399
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001400 dai->probed = 1;
1401
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001402 return 0;
1403}
1404
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001405static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1406 struct snd_soc_pcm_runtime *rtd)
1407{
1408 int i, ret = 0;
1409
1410 for (i = 0; i < num_dais; ++i) {
1411 struct snd_soc_dai_driver *drv = dais[i]->driver;
1412
Rohit kumarde17f142018-11-01 18:08:49 +05301413 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001414 ret = drv->pcm_new(rtd, dais[i]);
1415 if (ret < 0) {
1416 dev_err(dais[i]->dev,
1417 "ASoC: Failed to bind %s with pcm device\n",
1418 dais[i]->name);
1419 return ret;
1420 }
1421 }
1422
1423 return 0;
1424}
1425
Mengdong Lin1a497982015-11-18 02:34:11 -05001426static int soc_probe_link_dais(struct snd_soc_card *card,
1427 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428{
Mengdong Lin1a497982015-11-18 02:34:11 -05001429 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001430 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001431 struct snd_soc_rtdcom_list *rtdcom;
1432 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001433 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001434 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001436 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001437 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001438
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001439 /* set default power off timeout */
1440 rtd->pmdown_time = pmdown_time;
1441
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001442 ret = soc_probe_dai(cpu_dai, order);
1443 if (ret)
1444 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001446 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001447 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1448 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001449 if (ret)
1450 return ret;
1451 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001452
Liam Girdwood0168bf02011-06-07 16:08:05 +01001453 /* complete DAI probe during last probe */
1454 if (order != SND_SOC_COMP_ORDER_LAST)
1455 return 0;
1456
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001457 /* do machine specific initialization */
1458 if (dai_link->init) {
1459 ret = dai_link->init(rtd);
1460 if (ret < 0) {
1461 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1462 dai_link->name, ret);
1463 return ret;
1464 }
1465 }
1466
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001467 if (dai_link->dai_fmt) {
1468 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1469 if (ret)
1470 return ret;
1471 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001472
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001473 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001474 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001476
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001477#ifdef CONFIG_DEBUG_FS
1478 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001479 if (dai_link->dynamic)
1480 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001481#endif
1482
Liam Girdwooda655de82018-07-02 16:59:54 +01001483 num = rtd->num;
1484
1485 /*
1486 * most drivers will register their PCMs using DAI link ordering but
1487 * topology based drivers can use the DAI link id field to set PCM
1488 * device number and then use rtd + a base offset of the BEs.
1489 */
1490 for_each_rtdcom(rtd, rtdcom) {
1491 component = rtdcom->component;
1492
1493 if (!component->driver->use_dai_pcm_id)
1494 continue;
1495
1496 if (rtd->dai_link->no_pcm)
1497 num += component->driver->be_pcm_base;
1498 else
1499 num = rtd->dai_link->id;
1500 }
1501
Kuninori Morimotob423c422019-07-22 10:35:29 +09001502 /* create compress_device if possible */
1503 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1504 if (ret != -ENOTSUPP) {
1505 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001506 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301507 dai_link->stream_name);
Kuninori Morimotob423c422019-07-22 10:35:29 +09001508 return ret;
1509 }
1510
Jerome Bruneta3420312019-07-25 18:59:47 +02001511 /* create the pcm */
1512 ret = soc_new_pcm(rtd, num);
1513 if (ret < 0) {
1514 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1515 dai_link->stream_name, ret);
1516 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 }
Jerome Bruneta3420312019-07-25 18:59:47 +02001518 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1519 if (ret < 0)
1520 return ret;
1521 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1522 rtd->num_codecs, rtd);
1523 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524}
1525
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001526static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001527{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001528 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001529 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001530 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001531
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001532 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1533 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001534 dlc.name = aux_dev->codec_name;
1535 dlc.of_node = aux_dev->codec_of_node;
1536 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001537 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001538 if (dlc.of_node)
1539 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001540 goto err_defer;
1541 }
1542 } else if (aux_dev->name) {
1543 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001544 dlc.name = aux_dev->name;
1545 dlc.of_node = NULL;
1546 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001547 if (!component)
1548 goto err_defer;
1549 } else {
1550 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1551 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001552 }
1553
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001554 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001555 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001556
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001557 return 0;
1558
1559err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001560 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001561 return -EPROBE_DEFER;
1562}
1563
1564static int soc_probe_aux_devices(struct snd_soc_card *card)
1565{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001566 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001567 int order;
1568 int ret;
1569
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001570 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001571 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001572 if (comp->driver->probe_order == order) {
1573 ret = soc_probe_component(card, comp);
1574 if (ret < 0) {
1575 dev_err(card->dev,
1576 "ASoC: failed to probe aux component %s %d\n",
1577 comp->name, ret);
1578 return ret;
1579 }
1580 }
1581 }
1582 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001583
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001584 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001585}
1586
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001587static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001588{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001589 struct snd_soc_component *comp, *_comp;
1590 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001591
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001592 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001593 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001594 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001595
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001596 if (comp->driver->remove_order == order) {
1597 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001598 /* remove it from the card's aux_comp_list */
1599 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001600 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001601 }
1602 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001603}
1604
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001605/**
1606 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1607 * @rtd: The runtime for which the DAI link format should be changed
1608 * @dai_fmt: The new DAI link format
1609 *
1610 * This function updates the DAI link format for all DAIs connected to the DAI
1611 * link for the specified runtime.
1612 *
1613 * Note: For setups with a static format set the dai_fmt field in the
1614 * corresponding snd_dai_link struct instead of using this function.
1615 *
1616 * Returns 0 on success, otherwise a negative error code.
1617 */
1618int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1619 unsigned int dai_fmt)
1620{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001621 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001622 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001623 unsigned int i;
1624 int ret;
1625
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001626 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001627 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1628 if (ret != 0 && ret != -ENOTSUPP) {
1629 dev_warn(codec_dai->dev,
1630 "ASoC: Failed to set DAI format: %d\n", ret);
1631 return ret;
1632 }
1633 }
1634
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001635 /*
1636 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1637 * the component which has non_legacy_dai_naming is Codec
1638 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001639 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001640 unsigned int inv_dai_fmt;
1641
1642 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1643 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1644 case SND_SOC_DAIFMT_CBM_CFM:
1645 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1646 break;
1647 case SND_SOC_DAIFMT_CBM_CFS:
1648 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1649 break;
1650 case SND_SOC_DAIFMT_CBS_CFM:
1651 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1652 break;
1653 case SND_SOC_DAIFMT_CBS_CFS:
1654 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1655 break;
1656 }
1657
1658 dai_fmt = inv_dai_fmt;
1659 }
1660
1661 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1662 if (ret != 0 && ret != -ENOTSUPP) {
1663 dev_warn(cpu_dai->dev,
1664 "ASoC: Failed to set DAI format: %d\n", ret);
1665 return ret;
1666 }
1667
1668 return 0;
1669}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001670EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001671
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001672#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001673/*
1674 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001675 * separate different DMI fields in the card long name. Only number and
1676 * alphabet characters and a few separator characters are kept.
1677 */
1678static void cleanup_dmi_name(char *name)
1679{
1680 int i, j = 0;
1681
1682 for (i = 0; name[i]; i++) {
1683 if (isalnum(name[i]) || (name[i] == '.')
1684 || (name[i] == '_'))
1685 name[j++] = name[i];
1686 else if (name[i] == '-')
1687 name[j++] = '_';
1688 }
1689
1690 name[j] = '\0';
1691}
1692
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001693/*
1694 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001695 * in the black list.
1696 */
1697static int is_dmi_valid(const char *field)
1698{
1699 int i = 0;
1700
1701 while (dmi_blacklist[i]) {
1702 if (strstr(field, dmi_blacklist[i]))
1703 return 0;
1704 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001705 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001706
1707 return 1;
1708}
1709
Liam Girdwood345233d2017-01-14 16:13:02 +08001710/**
1711 * snd_soc_set_dmi_name() - Register DMI names to card
1712 * @card: The card to register DMI names
1713 * @flavour: The flavour "differentiator" for the card amongst its peers.
1714 *
1715 * An Intel machine driver may be used by many different devices but are
1716 * difficult for userspace to differentiate, since machine drivers ususally
1717 * use their own name as the card short name and leave the card long name
1718 * blank. To differentiate such devices and fix bugs due to lack of
1719 * device-specific configurations, this function allows DMI info to be used
1720 * as the sound card long name, in the format of
1721 * "vendor-product-version-board"
1722 * (Character '-' is used to separate different DMI fields here).
1723 * This will help the user space to load the device-specific Use Case Manager
1724 * (UCM) configurations for the card.
1725 *
1726 * Possible card long names may be:
1727 * DellInc.-XPS139343-01-0310JH
1728 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1729 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1730 *
1731 * This function also supports flavoring the card longname to provide
1732 * the extra differentiation, like "vendor-product-version-board-flavor".
1733 *
1734 * We only keep number and alphabet characters and a few separator characters
1735 * in the card long name since UCM in the user space uses the card long names
1736 * as card configuration directory names and AudoConf cannot support special
1737 * charactors like SPACE.
1738 *
1739 * Returns 0 on success, otherwise a negative error code.
1740 */
1741int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1742{
1743 const char *vendor, *product, *product_version, *board;
1744 size_t longname_buf_size = sizeof(card->snd_card->longname);
1745 size_t len;
1746
1747 if (card->long_name)
1748 return 0; /* long name already set by driver or from DMI */
1749
1750 /* make up dmi long name as: vendor.product.version.board */
1751 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001752 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001753 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1754 return 0;
1755 }
1756
1757 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1758 "%s", vendor);
1759 cleanup_dmi_name(card->dmi_longname);
1760
1761 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001762 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001763 len = strlen(card->dmi_longname);
1764 snprintf(card->dmi_longname + len,
1765 longname_buf_size - len,
1766 "-%s", product);
1767
1768 len++; /* skip the separator "-" */
1769 if (len < longname_buf_size)
1770 cleanup_dmi_name(card->dmi_longname + len);
1771
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001772 /*
1773 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001774 * name in the product version field
1775 */
1776 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001777 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001778 len = strlen(card->dmi_longname);
1779 snprintf(card->dmi_longname + len,
1780 longname_buf_size - len,
1781 "-%s", product_version);
1782
1783 len++;
1784 if (len < longname_buf_size)
1785 cleanup_dmi_name(card->dmi_longname + len);
1786 }
1787 }
1788
1789 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001790 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001791 len = strlen(card->dmi_longname);
1792 snprintf(card->dmi_longname + len,
1793 longname_buf_size - len,
1794 "-%s", board);
1795
1796 len++;
1797 if (len < longname_buf_size)
1798 cleanup_dmi_name(card->dmi_longname + len);
1799 } else if (!product) {
1800 /* fall back to using legacy name */
1801 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1802 return 0;
1803 }
1804
1805 /* Add flavour to dmi long name */
1806 if (flavour) {
1807 len = strlen(card->dmi_longname);
1808 snprintf(card->dmi_longname + len,
1809 longname_buf_size - len,
1810 "-%s", flavour);
1811
1812 len++;
1813 if (len < longname_buf_size)
1814 cleanup_dmi_name(card->dmi_longname + len);
1815 }
1816
1817 /* set the card long name */
1818 card->long_name = card->dmi_longname;
1819
1820 return 0;
1821}
1822EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001823#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001824
Liam Girdwooda655de82018-07-02 16:59:54 +01001825static void soc_check_tplg_fes(struct snd_soc_card *card)
1826{
1827 struct snd_soc_component *component;
1828 const struct snd_soc_component_driver *comp_drv;
1829 struct snd_soc_dai_link *dai_link;
1830 int i;
1831
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001832 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001833
1834 /* does this component override FEs ? */
1835 if (!component->driver->ignore_machine)
1836 continue;
1837
1838 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001839 if (!strcmp(component->driver->ignore_machine,
1840 card->dev->driver->name))
1841 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001842 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001843 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001844 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001845match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001846 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001847 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001848
1849 /* ignore this FE */
1850 if (dai_link->dynamic) {
1851 dai_link->ignore = true;
1852 continue;
1853 }
1854
1855 dev_info(card->dev, "info: override FE DAI link %s\n",
1856 card->dai_link[i].name);
1857
1858 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001859 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001860 dev_err(card->dev, "init platform error");
1861 continue;
1862 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001863 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001864
1865 /* convert non BE into BE */
1866 dai_link->no_pcm = 1;
1867
1868 /* override any BE fixups */
1869 dai_link->be_hw_params_fixup =
1870 component->driver->be_hw_params_fixup;
1871
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001872 /*
1873 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001874 * dai link name if it's NULL to help bind widgets.
1875 */
1876 if (!dai_link->stream_name)
1877 dai_link->stream_name = dai_link->name;
1878 }
1879
1880 /* Inform userspace we are using alternate topology */
1881 if (component->driver->topology_name_prefix) {
1882
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001883 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001884 if (!card->topology_shortname_created) {
1885 comp_drv = component->driver;
1886
1887 snprintf(card->topology_shortname, 32, "%s-%s",
1888 comp_drv->topology_name_prefix,
1889 card->name);
1890 card->topology_shortname_created = true;
1891 }
1892
1893 /* use topology shortname */
1894 card->name = card->topology_shortname;
1895 }
1896 }
1897}
1898
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001899static int soc_cleanup_card_resources(struct snd_soc_card *card)
1900{
1901 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001902 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001903 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001904 card->snd_card = NULL;
1905 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001906
1907 /* remove and free each DAI */
1908 soc_remove_dai_links(card);
1909 soc_remove_pcm_runtimes(card);
1910
1911 /* remove auxiliary devices */
1912 soc_remove_aux_devices(card);
1913
1914 snd_soc_dapm_free(&card->dapm);
1915 soc_cleanup_card_debugfs(card);
1916
1917 /* remove the card */
1918 if (card->remove)
1919 card->remove(card);
1920
1921 return 0;
1922}
1923
Mark Brownb19e6e72012-03-14 21:18:39 +00001924static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925{
Mengdong Lin1a497982015-11-18 02:34:11 -05001926 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001927 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001928 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001930 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001931 for_each_card_prelinks(card, i, dai_link) {
1932 ret = soc_init_dai_link(card, dai_link);
1933 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001934 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1935 dai_link->name, ret);
1936 mutex_unlock(&client_mutex);
1937 return ret;
1938 }
1939 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001940 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001942 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1943 card->dapm.dev = card->dev;
1944 card->dapm.card = card;
1945 list_add(&card->dapm.list, &card->dapm_list);
1946
Liam Girdwooda655de82018-07-02 16:59:54 +01001947 /* check whether any platform is ignore machine FE and using topology */
1948 soc_check_tplg_fes(card);
1949
Mark Brownb19e6e72012-03-14 21:18:39 +00001950 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001951 for_each_card_prelinks(card, i, dai_link) {
1952 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001953 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001954 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001955 }
1956
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001957 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001958 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001959 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001960 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001961 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001962 }
1963
Mengdong Linf8f80362015-12-02 14:11:22 +08001964 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001965 for_each_card_prelinks(card, i, dai_link)
1966 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001967
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001969 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001970 card->owner, 0, &card->snd_card);
1971 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001972 dev_err(card->dev,
1973 "ASoC: can't create sound card for card %s: %d\n",
1974 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001975 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001978 soc_init_card_debugfs(card);
1979
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001980#ifdef CONFIG_DEBUG_FS
1981 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1982#endif
1983
Mark Brown88ee1c62011-02-02 10:43:26 +00001984#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001985 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001986 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001987#endif
Andy Green6ed25972008-06-13 16:24:05 +01001988
Mark Brown9a841eb2011-04-12 17:51:37 -07001989 if (card->dapm_widgets)
1990 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1991 card->num_dapm_widgets);
1992
Nicolin Chenf23e8602015-02-14 17:22:49 -08001993 if (card->of_dapm_widgets)
1994 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1995 card->num_of_dapm_widgets);
1996
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001997 /* initialise the sound card only once */
1998 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001999 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002001 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002 }
2003
Stephen Warren62ae68f2012-06-08 12:34:23 -06002004 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002005 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002006 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002007 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002008 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002009 dev_err(card->dev,
2010 "ASoC: failed to instantiate card %d\n",
2011 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002012 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002013 }
2014 }
2015 }
2016
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002017 /* probe auxiliary components */
2018 ret = soc_probe_aux_devices(card);
2019 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002020 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002021
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002022 /*
2023 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002024 * Components with topology may bring new DAIs and DAI links.
2025 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002026 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002027 if (soc_is_dai_link_bound(card, dai_link))
2028 continue;
2029
2030 ret = soc_init_dai_link(card, dai_link);
2031 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002032 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002033 ret = soc_bind_dai_link(card, dai_link);
2034 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002035 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002036 }
2037
Stephen Warren62ae68f2012-06-08 12:34:23 -06002038 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002039 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002040 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002041 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002042 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002043 dev_err(card->dev,
2044 "ASoC: failed to instantiate card %d\n",
2045 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002046 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002047 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002048 }
2049 }
2050
Mark Brown888df392012-02-16 19:37:51 -08002051 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002052 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002053
Mark Brownb7af1da2011-04-07 19:18:44 +09002054 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002055 snd_soc_add_card_controls(card, card->controls,
2056 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002057
Mark Brownb8ad29d2011-03-02 18:35:51 +00002058 if (card->dapm_routes)
2059 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2060 card->num_dapm_routes);
2061
Nicolin Chenf23e8602015-02-14 17:22:49 -08002062 if (card->of_dapm_routes)
2063 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2064 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002065
Takashi Iwai861886d2017-04-24 08:54:42 +02002066 /* try to set some sane longname if DMI is available */
2067 snd_soc_set_dmi_name(card, NULL);
2068
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002069 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002071 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2072 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002073 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2074 "%s", card->driver_name ? card->driver_name : card->name);
2075 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2076 switch (card->snd_card->driver[i]) {
2077 case '_':
2078 case '-':
2079 case '\0':
2080 break;
2081 default:
2082 if (!isalnum(card->snd_card->driver[i]))
2083 card->snd_card->driver[i] = '_';
2084 break;
2085 }
2086 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002087
Mark Brown28e9ad92011-03-02 18:36:34 +00002088 if (card->late_probe) {
2089 ret = card->late_probe(card);
2090 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002091 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002092 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002093 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002094 }
2095 }
2096
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002097 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002098
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002099 ret = snd_card_register(card->snd_card);
2100 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002101 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2102 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002103 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002104 }
2105
Mark Brown435c5e22008-12-04 15:32:53 +00002106 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002107 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002108 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002109
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002110probe_end:
2111 if (ret < 0)
2112 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002115 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116
Mark Brownb19e6e72012-03-14 21:18:39 +00002117 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002118}
2119
2120/* probes a new socdev */
2121static int soc_probe(struct platform_device *pdev)
2122{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002123 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002124
Vinod Koul70a7ca32011-01-14 19:22:48 +05302125 /*
2126 * no card, so machine driver should be registering card
2127 * we should not be here in that case so ret error
2128 */
2129 if (!card)
2130 return -EINVAL;
2131
Mark Brownfe4085e2012-03-02 13:07:41 +00002132 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002133 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002134 card->name);
2135
Mark Brown435c5e22008-12-04 15:32:53 +00002136 /* Bodge while we unpick instantiation */
2137 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002138
Mark Brown28d528c2012-08-09 18:45:23 +01002139 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140}
2141
2142/* removes a socdev */
2143static int soc_remove(struct platform_device *pdev)
2144{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002145 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146
Mark Brownc5af3a22008-11-28 13:29:45 +00002147 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148 return 0;
2149}
2150
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002151int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002152{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002153 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002154 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002155
2156 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002157 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002158
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002159 /*
2160 * Flush out pmdown_time work - we actually do want to run it
2161 * now, we're shutting down so no imminent restart.
2162 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002163 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002164
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002165 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002166
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002167 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002168 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002169 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002170 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002171 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002172
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002173 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002174 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002175 pinctrl_pm_select_sleep_state(codec_dai->dev);
2176 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002177 }
2178
Mark Brown416356f2009-06-30 19:05:15 +01002179 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002180}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002181EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002182
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002183const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302184 .suspend = snd_soc_suspend,
2185 .resume = snd_soc_resume,
2186 .freeze = snd_soc_suspend,
2187 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002188 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302189 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002190};
Stephen Warrendeb26072011-04-05 19:35:30 -06002191EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002192
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193/* ASoC platform driver */
2194static struct platform_driver soc_driver = {
2195 .driver = {
2196 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002197 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198 },
2199 .probe = soc_probe,
2200 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201};
2202
Mark Brown096e49d2009-07-05 15:12:22 +01002203/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002204 * snd_soc_cnew - create new control
2205 * @_template: control template
2206 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002207 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002208 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 *
2210 * Create a new mixer control from a template control.
2211 *
2212 * Returns 0 for success, else error.
2213 */
2214struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002215 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002216 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217{
2218 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002219 struct snd_kcontrol *kcontrol;
2220 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221
2222 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002223 template.index = 0;
2224
Mark Brownefb7ac32011-03-08 17:23:24 +00002225 if (!long_name)
2226 long_name = template.name;
2227
2228 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002229 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002230 if (!name)
2231 return NULL;
2232
Mark Brownefb7ac32011-03-08 17:23:24 +00002233 template.name = name;
2234 } else {
2235 template.name = long_name;
2236 }
2237
2238 kcontrol = snd_ctl_new1(&template, data);
2239
2240 kfree(name);
2241
2242 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002243}
2244EXPORT_SYMBOL_GPL(snd_soc_cnew);
2245
Liam Girdwood022658b2012-02-03 17:43:09 +00002246static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2247 const struct snd_kcontrol_new *controls, int num_controls,
2248 const char *prefix, void *data)
2249{
2250 int err, i;
2251
2252 for (i = 0; i < num_controls; i++) {
2253 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002254
Liam Girdwood022658b2012-02-03 17:43:09 +00002255 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2256 control->name, prefix));
2257 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002258 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2259 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002260 return err;
2261 }
2262 }
2263
2264 return 0;
2265}
2266
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002267struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2268 const char *name)
2269{
2270 struct snd_card *card = soc_card->snd_card;
2271 struct snd_kcontrol *kctl;
2272
2273 if (unlikely(!name))
2274 return NULL;
2275
2276 list_for_each_entry(kctl, &card->controls, list)
2277 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2278 return kctl;
2279 return NULL;
2280}
2281EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2282
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002283/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002284 * snd_soc_add_component_controls - Add an array of controls to a component.
2285 *
2286 * @component: Component to add controls to
2287 * @controls: Array of controls to add
2288 * @num_controls: Number of elements in the array
2289 *
2290 * Return: 0 for success, else error.
2291 */
2292int snd_soc_add_component_controls(struct snd_soc_component *component,
2293 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2294{
2295 struct snd_card *card = component->card->snd_card;
2296
2297 return snd_soc_add_controls(card, component->dev, controls,
2298 num_controls, component->name_prefix, component);
2299}
2300EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2301
2302/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002303 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2304 * Convenience function to add a list of controls.
2305 *
2306 * @soc_card: SoC card to add controls to
2307 * @controls: array of controls to add
2308 * @num_controls: number of elements in the array
2309 *
2310 * Return 0 for success, else error.
2311 */
2312int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2313 const struct snd_kcontrol_new *controls, int num_controls)
2314{
2315 struct snd_card *card = soc_card->snd_card;
2316
2317 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2318 NULL, soc_card);
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2321
2322/**
2323 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2324 * Convienience function to add a list of controls.
2325 *
2326 * @dai: DAI to add controls to
2327 * @controls: array of controls to add
2328 * @num_controls: number of elements in the array
2329 *
2330 * Return 0 for success, else error.
2331 */
2332int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2333 const struct snd_kcontrol_new *controls, int num_controls)
2334{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002335 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002336
2337 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2338 NULL, dai);
2339}
2340EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2341
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002342static int snd_soc_bind_card(struct snd_soc_card *card)
2343{
2344 struct snd_soc_pcm_runtime *rtd;
2345 int ret;
2346
2347 ret = snd_soc_instantiate_card(card);
2348 if (ret != 0)
2349 return ret;
2350
2351 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002352 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002353 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2354 struct snd_soc_dai *codec_dai;
2355 int j;
2356
2357 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2358 if (!codec_dai->active)
2359 pinctrl_pm_select_sleep_state(codec_dai->dev);
2360 }
2361
2362 if (!cpu_dai->active)
2363 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2364 }
2365
2366 return ret;
2367}
2368
Mark Brownc5af3a22008-11-28 13:29:45 +00002369/**
2370 * snd_soc_register_card - Register a card with the ASoC core
2371 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002372 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002373 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002374 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302375int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002376{
2377 if (!card->name || !card->dev)
2378 return -EINVAL;
2379
Mark Browned77cc12011-05-03 18:25:34 +01002380 dev_set_drvdata(card->dev, card);
2381
Stephen Warren111c6412011-01-28 14:26:35 -07002382 snd_soc_initialize_card_lists(card);
2383
Mengdong Linf8f80362015-12-02 14:11:22 +08002384 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002385
Mengdong Lin1a497982015-11-18 02:34:11 -05002386 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002387 card->num_rtd = 0;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002388
Mark Browndb432b42011-10-03 21:06:40 +01002389 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002390 INIT_LIST_HEAD(&card->dobj_list);
Mark Browndee89c42008-11-18 22:11:38 +00002391 card->instantiated = 0;
2392 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002393 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002394 spin_lock_init(&card->dpcm_lock);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002395
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002396 return snd_soc_bind_card(card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002397}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302398EXPORT_SYMBOL_GPL(snd_soc_register_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002399
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002400static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2401{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002402 struct snd_soc_pcm_runtime *rtd;
2403 int order;
2404
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002405 if (card->instantiated) {
2406 card->instantiated = false;
2407 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002408 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002409
2410 /* remove all components used by DAI links on this card */
2411 for_each_comp_order(order) {
2412 for_each_card_rtds(card, rtd) {
2413 soc_remove_link_components(card, rtd, order);
2414 }
2415 }
2416
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002417 soc_cleanup_card_resources(card);
2418 if (!unregister)
2419 list_add(&card->list, &unbind_card_list);
2420 } else {
2421 if (unregister)
2422 list_del(&card->list);
2423 }
2424}
2425
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002426/**
2427 * snd_soc_unregister_card - Unregister a card with the ASoC core
2428 *
Mark Browndee89c42008-11-18 22:11:38 +00002429 * @card: Card to unregister
2430 *
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002431 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302432int snd_soc_unregister_card(struct snd_soc_card *card)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002433{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002434 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002435 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002436 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002437 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002438
2439 return 0;
2440}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302441EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002442
2443/*
2444 * Simplify DAI link configuration by removing ".-1" from device names
2445 * and sanitizing names.
2446 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002447static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002448{
2449 char *found, name[NAME_SIZE];
2450 int id1, id2;
2451
2452 if (dev_name(dev) == NULL)
2453 return NULL;
2454
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002455 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002456
2457 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002458 found = strstr(name, dev->driver->name);
2459 if (found) {
2460 /* get ID */
2461 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2462
2463 /* discard ID from name if ID == -1 */
2464 if (*id == -1)
2465 found[strlen(dev->driver->name)] = '\0';
2466 }
2467
2468 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002469 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002470 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2471 char tmp[NAME_SIZE];
2472
2473 /* create unique ID number from I2C addr and bus */
2474 *id = ((id1 & 0xffff) << 16) + id2;
2475
2476 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002477 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2478 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002479 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002480 } else
2481 *id = 0;
2482 }
2483
2484 return kstrdup(name, GFP_KERNEL);
2485}
2486
2487/*
2488 * Simplify DAI link naming for single devices with multiple DAIs by removing
2489 * any ".-1" and using the DAI name (instead of device name).
2490 */
2491static inline char *fmt_multiple_name(struct device *dev,
2492 struct snd_soc_dai_driver *dai_drv)
2493{
2494 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002495 dev_err(dev,
2496 "ASoC: error - multiple DAI %s registered with no name\n",
2497 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002498 return NULL;
2499 }
2500
2501 return kstrdup(dai_drv->name, GFP_KERNEL);
2502}
2503
2504/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002505 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002506 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002507 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002508 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002509static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002510{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002511 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002512
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002513 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002514 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2515 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002516 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002517 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002518 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002519 }
Mark Brown91151712008-11-30 23:31:24 +00002520}
Mark Brown91151712008-11-30 23:31:24 +00002521
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002522/* Create a DAI and add it to the component's DAI list */
2523static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2524 struct snd_soc_dai_driver *dai_drv,
2525 bool legacy_dai_naming)
2526{
2527 struct device *dev = component->dev;
2528 struct snd_soc_dai *dai;
2529
2530 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2531
2532 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2533 if (dai == NULL)
2534 return NULL;
2535
2536 /*
2537 * Back in the old days when we still had component-less DAIs,
2538 * instead of having a static name, component-less DAIs would
2539 * inherit the name of the parent device so it is possible to
2540 * register multiple instances of the DAI. We still need to keep
2541 * the same naming style even though those DAIs are not
2542 * component-less anymore.
2543 */
2544 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002545 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002546 dai->name = fmt_single_name(dev, &dai->id);
2547 } else {
2548 dai->name = fmt_multiple_name(dev, dai_drv);
2549 if (dai_drv->id)
2550 dai->id = dai_drv->id;
2551 else
2552 dai->id = component->num_dai;
2553 }
2554 if (dai->name == NULL) {
2555 kfree(dai);
2556 return NULL;
2557 }
2558
2559 dai->component = component;
2560 dai->dev = dev;
2561 dai->driver = dai_drv;
2562 if (!dai->driver->ops)
2563 dai->driver->ops = &null_dai_ops;
2564
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002565 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002566 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002567 component->num_dai++;
2568
2569 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2570 return dai;
2571}
2572
Mark Brown91151712008-11-30 23:31:24 +00002573/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002574 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002575 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002576 * @component: The component the DAIs are registered for
2577 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002578 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002579 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002580static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002581 struct snd_soc_dai_driver *dai_drv,
2582 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002583{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002584 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002585 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002586 unsigned int i;
2587 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002588
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002589 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002590
2591 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002592
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002593 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2594 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002595 if (dai == NULL) {
2596 ret = -ENOMEM;
2597 goto err;
2598 }
Mark Brown91151712008-11-30 23:31:24 +00002599 }
2600
2601 return 0;
2602
2603err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002604 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002605
2606 return ret;
2607}
Mark Brown91151712008-11-30 23:31:24 +00002608
Mengdong Lin68003e62015-12-31 16:40:43 +08002609/**
2610 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2611 *
2612 * @component: The component the DAIs are registered for
2613 * @dai_drv: DAI driver to use for the DAI
2614 *
2615 * Topology can use this API to register DAIs when probing a component.
2616 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2617 * will be freed in the component cleanup.
2618 */
2619int snd_soc_register_dai(struct snd_soc_component *component,
2620 struct snd_soc_dai_driver *dai_drv)
2621{
2622 struct snd_soc_dapm_context *dapm =
2623 snd_soc_component_get_dapm(component);
2624 struct snd_soc_dai *dai;
2625 int ret;
2626
2627 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2628 dev_err(component->dev, "Invalid dai type %d\n",
2629 dai_drv->dobj.type);
2630 return -EINVAL;
2631 }
2632
2633 lockdep_assert_held(&client_mutex);
2634 dai = soc_add_dai(component, dai_drv, false);
2635 if (!dai)
2636 return -ENOMEM;
2637
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002638 /*
2639 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002640 * also add routes that need these widgets as source or sink.
2641 */
2642 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2643 if (ret != 0) {
2644 dev_err(component->dev,
2645 "Failed to create DAI widgets %d\n", ret);
2646 }
2647
2648 return ret;
2649}
2650EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2651
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002652static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2653 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002654{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002655 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002656
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002657 component->driver->seq_notifier(component, type, subseq);
2658}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002659
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002660static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2661 int event)
2662{
2663 struct snd_soc_component *component = dapm->component;
2664
2665 return component->driver->stream_event(component, event);
2666}
2667
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002668static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2669 enum snd_soc_bias_level level)
2670{
2671 struct snd_soc_component *component = dapm->component;
2672
2673 return component->driver->set_bias_level(component, level);
2674}
2675
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002676static int snd_soc_component_initialize(struct snd_soc_component *component,
2677 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002678{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002679 struct snd_soc_dapm_context *dapm;
2680
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002681 component->name = fmt_single_name(dev, &component->id);
2682 if (!component->name) {
2683 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002684 return -ENOMEM;
2685 }
2686
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002687 component->dev = dev;
2688 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002689
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002690 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002691 dapm->dev = dev;
2692 dapm->component = component;
2693 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002694 dapm->idle_bias_off = !driver->idle_bias_on;
2695 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002696 if (driver->seq_notifier)
2697 dapm->seq_notifier = snd_soc_component_seq_notifier;
2698 if (driver->stream_event)
2699 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002700 if (driver->set_bias_level)
2701 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002702
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002703 INIT_LIST_HEAD(&component->dai_list);
2704 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002705
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002706 return 0;
2707}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002708
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002709static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002710{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002711 int val_bytes = regmap_get_val_bytes(component->regmap);
2712
2713 /* Errors are legitimate for non-integer byte multiples */
2714 if (val_bytes > 0)
2715 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002716}
2717
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002718#ifdef CONFIG_REGMAP
2719
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002720/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002721 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2722 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002723 * @component: The component for which to initialize the regmap instance
2724 * @regmap: The regmap instance that should be used by the component
2725 *
2726 * This function allows deferred assignment of the regmap instance that is
2727 * associated with the component. Only use this if the regmap instance is not
2728 * yet ready when the component is registered. The function must also be called
2729 * before the first IO attempt of the component.
2730 */
2731void snd_soc_component_init_regmap(struct snd_soc_component *component,
2732 struct regmap *regmap)
2733{
2734 component->regmap = regmap;
2735 snd_soc_component_setup_regmap(component);
2736}
2737EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2738
2739/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002740 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2741 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002742 * @component: The component for which to de-initialize the regmap instance
2743 *
2744 * Calls regmap_exit() on the regmap instance associated to the component and
2745 * removes the regmap instance from the component.
2746 *
2747 * This function should only be used if snd_soc_component_init_regmap() was used
2748 * to initialize the regmap instance.
2749 */
2750void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2751{
2752 regmap_exit(component->regmap);
2753 component->regmap = NULL;
2754}
2755EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2756
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002757#endif
2758
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002759static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002760{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002761 mutex_lock(&client_mutex);
2762
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002763 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002764 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002765 component->regmap = dev_get_regmap(component->dev,
2766 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002767 if (component->regmap)
2768 snd_soc_component_setup_regmap(component);
2769 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002770
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002771 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002772 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002773 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002774
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002775 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002776}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002777
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002778static void snd_soc_component_cleanup(struct snd_soc_component *component)
2779{
2780 snd_soc_unregister_dais(component);
2781 kfree(component->name);
2782}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002783
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002784static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2785{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002786 struct snd_soc_card *card = component->card;
2787
2788 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002789 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002790
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002791 list_del(&component->list);
2792}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002793
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002794#define ENDIANNESS_MAP(name) \
2795 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2796static u64 endianness_format_map[] = {
2797 ENDIANNESS_MAP(S16_),
2798 ENDIANNESS_MAP(U16_),
2799 ENDIANNESS_MAP(S24_),
2800 ENDIANNESS_MAP(U24_),
2801 ENDIANNESS_MAP(S32_),
2802 ENDIANNESS_MAP(U32_),
2803 ENDIANNESS_MAP(S24_3),
2804 ENDIANNESS_MAP(U24_3),
2805 ENDIANNESS_MAP(S20_3),
2806 ENDIANNESS_MAP(U20_3),
2807 ENDIANNESS_MAP(S18_3),
2808 ENDIANNESS_MAP(U18_3),
2809 ENDIANNESS_MAP(FLOAT_),
2810 ENDIANNESS_MAP(FLOAT64_),
2811 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2812};
2813
2814/*
2815 * Fix up the DAI formats for endianness: codecs don't actually see
2816 * the endianness of the data but we're using the CPU format
2817 * definitions which do need to include endianness so we ensure that
2818 * codec DAIs always have both big and little endian variants set.
2819 */
2820static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2821{
2822 int i;
2823
2824 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2825 if (stream->formats & endianness_format_map[i])
2826 stream->formats |= endianness_format_map[i];
2827}
2828
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002829static void snd_soc_try_rebind_card(void)
2830{
2831 struct snd_soc_card *card, *c;
2832
2833 if (!list_empty(&unbind_card_list)) {
2834 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2835 if (!snd_soc_bind_card(card))
2836 list_del(&card->list);
2837 }
2838 }
2839}
2840
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002841int snd_soc_add_component(struct device *dev,
2842 struct snd_soc_component *component,
2843 const struct snd_soc_component_driver *component_driver,
2844 struct snd_soc_dai_driver *dai_drv,
2845 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002846{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002847 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002848 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002849
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002850 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002851 if (ret)
2852 goto err_free;
2853
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002854 if (component_driver->endianness) {
2855 for (i = 0; i < num_dai; i++) {
2856 convert_endianness_formats(&dai_drv[i].playback);
2857 convert_endianness_formats(&dai_drv[i].capture);
2858 }
2859 }
2860
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002861 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002862 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002863 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002864 goto err_cleanup;
2865 }
2866
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002867 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002868 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002869
2870 return 0;
2871
2872err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002873 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002874err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002875 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002876}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002877EXPORT_SYMBOL_GPL(snd_soc_add_component);
2878
2879int snd_soc_register_component(struct device *dev,
2880 const struct snd_soc_component_driver *component_driver,
2881 struct snd_soc_dai_driver *dai_drv,
2882 int num_dai)
2883{
2884 struct snd_soc_component *component;
2885
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002886 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002887 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002888 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002889
2890 return snd_soc_add_component(dev, component, component_driver,
2891 dai_drv, num_dai);
2892}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002893EXPORT_SYMBOL_GPL(snd_soc_register_component);
2894
2895/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002896 * snd_soc_unregister_component - Unregister all related component
2897 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002898 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002899 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002900 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002901static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002902{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002903 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002904 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002905
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002906 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002907 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002908 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002909 continue;
2910
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002911 snd_soc_tplg_component_remove(component,
2912 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002913 snd_soc_component_del_unlocked(component);
2914 found = 1;
2915 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002916 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002917 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002918
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002919 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002920 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002921
2922 return found;
2923}
2924
2925void snd_soc_unregister_component(struct device *dev)
2926{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002927 while (__snd_soc_unregister_component(dev))
2928 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002929}
2930EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2931
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002932struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
2933 const char *driver_name)
2934{
2935 struct snd_soc_component *component;
2936 struct snd_soc_component *ret;
2937
2938 ret = NULL;
2939 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002940 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002941 if (dev != component->dev)
2942 continue;
2943
2944 if (driver_name &&
2945 (driver_name != component->driver->name) &&
2946 (strcmp(component->driver->name, driver_name) != 0))
2947 continue;
2948
2949 ret = component;
2950 break;
2951 }
2952 mutex_unlock(&client_mutex);
2953
2954 return ret;
2955}
2956EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
2957
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002958/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002959int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2960 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002961{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002962 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002963 int ret;
2964
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302965 if (!card->dev) {
2966 pr_err("card->dev is not set before calling %s\n", __func__);
2967 return -EINVAL;
2968 }
2969
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002970 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302971
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002972 ret = of_property_read_string_index(np, propname, 0, &card->name);
2973 /*
2974 * EINVAL means the property does not exist. This is fine providing
2975 * card->name was previously set, which is checked later in
2976 * snd_soc_register_card.
2977 */
2978 if (ret < 0 && ret != -EINVAL) {
2979 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002980 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002981 propname, ret);
2982 return ret;
2983 }
2984
2985 return 0;
2986}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002987EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002988
Xiubo Li9a6d4862014-02-08 15:59:52 +08002989static const struct snd_soc_dapm_widget simple_widgets[] = {
2990 SND_SOC_DAPM_MIC("Microphone", NULL),
2991 SND_SOC_DAPM_LINE("Line", NULL),
2992 SND_SOC_DAPM_HP("Headphone", NULL),
2993 SND_SOC_DAPM_SPK("Speaker", NULL),
2994};
2995
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002996int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002997 const char *propname)
2998{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002999 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003000 struct snd_soc_dapm_widget *widgets;
3001 const char *template, *wname;
3002 int i, j, num_widgets, ret;
3003
3004 num_widgets = of_property_count_strings(np, propname);
3005 if (num_widgets < 0) {
3006 dev_err(card->dev,
3007 "ASoC: Property '%s' does not exist\n", propname);
3008 return -EINVAL;
3009 }
3010 if (num_widgets & 1) {
3011 dev_err(card->dev,
3012 "ASoC: Property '%s' length is not even\n", propname);
3013 return -EINVAL;
3014 }
3015
3016 num_widgets /= 2;
3017 if (!num_widgets) {
3018 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3019 propname);
3020 return -EINVAL;
3021 }
3022
3023 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3024 GFP_KERNEL);
3025 if (!widgets) {
3026 dev_err(card->dev,
3027 "ASoC: Could not allocate memory for widgets\n");
3028 return -ENOMEM;
3029 }
3030
3031 for (i = 0; i < num_widgets; i++) {
3032 ret = of_property_read_string_index(np, propname,
3033 2 * i, &template);
3034 if (ret) {
3035 dev_err(card->dev,
3036 "ASoC: Property '%s' index %d read error:%d\n",
3037 propname, 2 * i, ret);
3038 return -EINVAL;
3039 }
3040
3041 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3042 if (!strncmp(template, simple_widgets[j].name,
3043 strlen(simple_widgets[j].name))) {
3044 widgets[i] = simple_widgets[j];
3045 break;
3046 }
3047 }
3048
3049 if (j >= ARRAY_SIZE(simple_widgets)) {
3050 dev_err(card->dev,
3051 "ASoC: DAPM widget '%s' is not supported\n",
3052 template);
3053 return -EINVAL;
3054 }
3055
3056 ret = of_property_read_string_index(np, propname,
3057 (2 * i) + 1,
3058 &wname);
3059 if (ret) {
3060 dev_err(card->dev,
3061 "ASoC: Property '%s' index %d read error:%d\n",
3062 propname, (2 * i) + 1, ret);
3063 return -EINVAL;
3064 }
3065
3066 widgets[i].name = wname;
3067 }
3068
Nicolin Chenf23e8602015-02-14 17:22:49 -08003069 card->of_dapm_widgets = widgets;
3070 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003071
3072 return 0;
3073}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003074EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003075
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003076int snd_soc_of_get_slot_mask(struct device_node *np,
3077 const char *prop_name,
3078 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003079{
3080 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003081 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003082 int i;
3083
3084 if (!of_slot_mask)
3085 return 0;
3086 val /= sizeof(u32);
3087 for (i = 0; i < val; i++)
3088 if (be32_to_cpup(&of_slot_mask[i]))
3089 *mask |= (1 << i);
3090
3091 return val;
3092}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003093EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003094
Xiubo Li89c67852014-02-14 09:34:35 +08003095int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003096 unsigned int *tx_mask,
3097 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003098 unsigned int *slots,
3099 unsigned int *slot_width)
3100{
3101 u32 val;
3102 int ret;
3103
Jyri Sarha61310842015-09-09 21:27:43 +03003104 if (tx_mask)
3105 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3106 if (rx_mask)
3107 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3108
Xiubo Li89c67852014-02-14 09:34:35 +08003109 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3110 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3111 if (ret)
3112 return ret;
3113
3114 if (slots)
3115 *slots = val;
3116 }
3117
3118 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3119 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3120 if (ret)
3121 return ret;
3122
3123 if (slot_width)
3124 *slot_width = val;
3125 }
3126
3127 return 0;
3128}
3129EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3130
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003131void snd_soc_of_parse_node_prefix(struct device_node *np,
3132 struct snd_soc_codec_conf *codec_conf,
3133 struct device_node *of_node,
3134 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003135{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003136 const char *str;
3137 int ret;
3138
3139 ret = of_property_read_string(np, propname, &str);
3140 if (ret < 0) {
3141 /* no prefix is not error */
3142 return;
3143 }
3144
3145 codec_conf->of_node = of_node;
3146 codec_conf->name_prefix = str;
3147}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003148EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003149
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003150int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003151 const char *propname)
3152{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003153 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003154 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003155 struct snd_soc_dapm_route *routes;
3156 int i, ret;
3157
3158 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003159 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003160 dev_err(card->dev,
3161 "ASoC: Property '%s' does not exist or its length is not even\n",
3162 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003163 return -EINVAL;
3164 }
3165 num_routes /= 2;
3166 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003167 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003168 propname);
3169 return -EINVAL;
3170 }
3171
Kees Cooka86854d2018-06-12 14:07:58 -07003172 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003173 GFP_KERNEL);
3174 if (!routes) {
3175 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003176 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003177 return -EINVAL;
3178 }
3179
3180 for (i = 0; i < num_routes; i++) {
3181 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003182 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003183 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003184 dev_err(card->dev,
3185 "ASoC: Property '%s' index %d could not be read: %d\n",
3186 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003187 return -EINVAL;
3188 }
3189 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003190 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003191 if (ret) {
3192 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003193 "ASoC: Property '%s' index %d could not be read: %d\n",
3194 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003195 return -EINVAL;
3196 }
3197 }
3198
Nicolin Chenf23e8602015-02-14 17:22:49 -08003199 card->num_of_dapm_routes = num_routes;
3200 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003201
3202 return 0;
3203}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003204EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003205
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003206unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003207 const char *prefix,
3208 struct device_node **bitclkmaster,
3209 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003210{
3211 int ret, i;
3212 char prop[128];
3213 unsigned int format = 0;
3214 int bit, frame;
3215 const char *str;
3216 struct {
3217 char *name;
3218 unsigned int val;
3219 } of_fmt_table[] = {
3220 { "i2s", SND_SOC_DAIFMT_I2S },
3221 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3222 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3223 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3224 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3225 { "ac97", SND_SOC_DAIFMT_AC97 },
3226 { "pdm", SND_SOC_DAIFMT_PDM},
3227 { "msb", SND_SOC_DAIFMT_MSB },
3228 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003229 };
3230
3231 if (!prefix)
3232 prefix = "";
3233
3234 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003235 * check "dai-format = xxx"
3236 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003237 * SND_SOC_DAIFMT_FORMAT_MASK area
3238 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003239 ret = of_property_read_string(np, "dai-format", &str);
3240 if (ret < 0) {
3241 snprintf(prop, sizeof(prop), "%sformat", prefix);
3242 ret = of_property_read_string(np, prop, &str);
3243 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003244 if (ret == 0) {
3245 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3246 if (strcmp(str, of_fmt_table[i].name) == 0) {
3247 format |= of_fmt_table[i].val;
3248 break;
3249 }
3250 }
3251 }
3252
3253 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003254 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003255 * SND_SOC_DAIFMT_CLOCK_MASK area
3256 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003257 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003258 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003259 format |= SND_SOC_DAIFMT_CONT;
3260 else
3261 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003262
3263 /*
3264 * check "[prefix]bitclock-inversion"
3265 * check "[prefix]frame-inversion"
3266 * SND_SOC_DAIFMT_INV_MASK area
3267 */
3268 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3269 bit = !!of_get_property(np, prop, NULL);
3270
3271 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3272 frame = !!of_get_property(np, prop, NULL);
3273
3274 switch ((bit << 4) + frame) {
3275 case 0x11:
3276 format |= SND_SOC_DAIFMT_IB_IF;
3277 break;
3278 case 0x10:
3279 format |= SND_SOC_DAIFMT_IB_NF;
3280 break;
3281 case 0x01:
3282 format |= SND_SOC_DAIFMT_NB_IF;
3283 break;
3284 default:
3285 /* SND_SOC_DAIFMT_NB_NF is default */
3286 break;
3287 }
3288
3289 /*
3290 * check "[prefix]bitclock-master"
3291 * check "[prefix]frame-master"
3292 * SND_SOC_DAIFMT_MASTER_MASK area
3293 */
3294 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3295 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003296 if (bit && bitclkmaster)
3297 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003298
3299 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3300 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003301 if (frame && framemaster)
3302 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003303
3304 switch ((bit << 4) + frame) {
3305 case 0x11:
3306 format |= SND_SOC_DAIFMT_CBM_CFM;
3307 break;
3308 case 0x10:
3309 format |= SND_SOC_DAIFMT_CBM_CFS;
3310 break;
3311 case 0x01:
3312 format |= SND_SOC_DAIFMT_CBS_CFM;
3313 break;
3314 default:
3315 format |= SND_SOC_DAIFMT_CBS_CFS;
3316 break;
3317 }
3318
3319 return format;
3320}
3321EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3322
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003323int snd_soc_get_dai_id(struct device_node *ep)
3324{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003325 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003326 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003327 int ret;
3328
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003329 dlc.of_node = of_graph_get_port_parent(ep);
3330 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003331 /*
3332 * For example HDMI case, HDMI has video/sound port,
3333 * but ALSA SoC needs sound port number only.
3334 * Thus counting HDMI DT port/endpoint doesn't work.
3335 * Then, it should have .of_xlate_dai_id
3336 */
3337 ret = -ENOTSUPP;
3338 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003339 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003340 if (component &&
3341 component->driver->of_xlate_dai_id)
3342 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003343 mutex_unlock(&client_mutex);
3344
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003345 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003346
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003347 return ret;
3348}
3349EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3350
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003351int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003352 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003353{
3354 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003355 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003356 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003357
3358 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003359 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003360 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003361
3362 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003363 continue;
3364
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003365 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003366 ret = pos->driver->of_xlate_dai_name(pos,
3367 args,
3368 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003369 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003370 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003371 int id = -1;
3372
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003373 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003374 case 0:
3375 id = 0; /* same as dai_drv[0] */
3376 break;
3377 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003378 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003379 break;
3380 default:
3381 /* not supported */
3382 break;
3383 }
3384
3385 if (id < 0 || id >= pos->num_dai) {
3386 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003387 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003388 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003389
3390 ret = 0;
3391
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003392 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003393 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003394 if (id == 0)
3395 break;
3396 id--;
3397 }
3398
3399 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003400 if (!*dai_name)
3401 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003402 }
3403
Kuninori Morimotocb470082013-09-10 17:39:56 -07003404 break;
3405 }
3406 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003407 return ret;
3408}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003409EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003410
3411int snd_soc_of_get_dai_name(struct device_node *of_node,
3412 const char **dai_name)
3413{
3414 struct of_phandle_args args;
3415 int ret;
3416
3417 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3418 "#sound-dai-cells", 0, &args);
3419 if (ret)
3420 return ret;
3421
3422 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003423
3424 of_node_put(args.np);
3425
3426 return ret;
3427}
3428EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3429
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003430/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003431 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3432 * @dai_link: DAI link
3433 *
3434 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3435 */
3436void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3437{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003438 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003439 int index;
3440
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003441 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003442 if (!component->of_node)
3443 break;
3444 of_node_put(component->of_node);
3445 component->of_node = NULL;
3446 }
3447}
3448EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3449
3450/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003451 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3452 * @dev: Card device
3453 * @of_node: Device node
3454 * @dai_link: DAI link
3455 *
3456 * Builds an array of CODEC DAI components from the DAI link property
3457 * 'sound-dai'.
3458 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003459 * The device nodes in the array (of_node) must be dereferenced by calling
3460 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003461 *
3462 * Returns 0 for success
3463 */
3464int snd_soc_of_get_dai_link_codecs(struct device *dev,
3465 struct device_node *of_node,
3466 struct snd_soc_dai_link *dai_link)
3467{
3468 struct of_phandle_args args;
3469 struct snd_soc_dai_link_component *component;
3470 char *name;
3471 int index, num_codecs, ret;
3472
3473 /* Count the number of CODECs */
3474 name = "sound-dai";
3475 num_codecs = of_count_phandle_with_args(of_node, name,
3476 "#sound-dai-cells");
3477 if (num_codecs <= 0) {
3478 if (num_codecs == -ENOENT)
3479 dev_err(dev, "No 'sound-dai' property\n");
3480 else
3481 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3482 return num_codecs;
3483 }
Kees Cooka86854d2018-06-12 14:07:58 -07003484 component = devm_kcalloc(dev,
3485 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003486 GFP_KERNEL);
3487 if (!component)
3488 return -ENOMEM;
3489 dai_link->codecs = component;
3490 dai_link->num_codecs = num_codecs;
3491
3492 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003493 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003494 ret = of_parse_phandle_with_args(of_node, name,
3495 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003496 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003497 if (ret)
3498 goto err;
3499 component->of_node = args.np;
3500 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3501 if (ret < 0)
3502 goto err;
3503 }
3504 return 0;
3505err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003506 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003507 dai_link->codecs = NULL;
3508 dai_link->num_codecs = 0;
3509 return ret;
3510}
3511EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3512
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003513static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003514{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003515 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003516 snd_soc_util_init();
3517
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003518 return platform_driver_register(&soc_driver);
3519}
Mark Brown4abe8e12010-10-12 17:41:03 +01003520module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003521
Mark Brown7d8c16a2008-11-30 22:11:24 +00003522static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003523{
Mark Brownfb257892011-04-28 10:57:54 +01003524 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003525 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003526
Mark Brown3ff3f642008-05-19 12:32:25 +02003527 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003528}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003529module_exit(snd_soc_exit);
3530
3531/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003532MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003533MODULE_DESCRIPTION("ALSA SoC Core");
3534MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003535MODULE_ALIAS("platform:soc-audio");