blob: 9ba19efcc56c9904190ac4b39d8e6f13337bc707 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090061 * This is used if driver don't need to have CPU/Codec/Platform
62 * dai_link. see soc.h
63 */
64struct snd_soc_dai_link_component null_dailink_component[0];
65EXPORT_SYMBOL_GPL(null_dailink_component);
66
67/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020068 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
71 */
72static int pmdown_time = 5000;
73module_param(pmdown_time, int, 0);
74MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020076/*
77 * If a DMI filed contain strings in this blacklist (e.g.
78 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080079 * as invalid and dropped when setting the card long name from DMI info.
80 */
81static const char * const dmi_blacklist[] = {
82 "To be filled by OEM",
83 "TBD by OEM",
84 "Default String",
85 "Board Manufacturer",
86 "Board Vendor Name",
87 "Board Product Name",
88 NULL, /* terminator */
89};
90
Mark Browndbe21402010-02-12 11:37:24 +000091static ssize_t pmdown_time_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
Mark Brown36ae1a92012-01-06 17:12:45 -080094 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000097}
98
99static ssize_t pmdown_time_set(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
Mark Brown36ae1a92012-01-06 17:12:45 -0800103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700104 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000105
Jingoo Hanb785a492013-07-19 16:24:59 +0900106 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700107 if (ret)
108 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000109
110 return count;
111}
112
113static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114
Takashi Iwaid29697d2015-01-30 20:16:37 +0100115static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100116 &dev_attr_pmdown_time.attr,
117 NULL
118};
119
120static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121 struct attribute *attr, int idx)
122{
123 struct device *dev = kobj_to_dev(kobj);
124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125
126 if (attr == &dev_attr_pmdown_time.attr)
127 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000128 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100129}
130
131static const struct attribute_group soc_dapm_dev_group = {
132 .attrs = soc_dapm_dev_attrs,
133 .is_visible = soc_dev_attr_is_visible,
134};
135
Mark Brownf7e73b262018-03-09 12:46:27 +0000136static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 .attrs = soc_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
141static const struct attribute_group *soc_dev_attr_groups[] = {
142 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000143 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100144 NULL
145};
146
Mark Brown2624d5f2009-11-03 21:56:13 +0000147#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200148static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100149{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200150 if (!component->card->debugfs_card_root)
151 return;
152
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153 if (component->debugfs_prefix) {
154 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 name = kasprintf(GFP_KERNEL, "%s:%s",
157 component->debugfs_prefix, component->name);
158 if (name) {
159 component->debugfs_root = debugfs_create_dir(name,
160 component->card->debugfs_card_root);
161 kfree(name);
162 }
163 } else {
164 component->debugfs_root = debugfs_create_dir(component->name,
165 component->card->debugfs_card_root);
166 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100167
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200168 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
169 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200170}
171
172static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
173{
174 debugfs_remove_recursive(component->debugfs_root);
175}
176
Peng Donglinc15b2a12018-02-14 22:48:07 +0800177static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100178{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100179 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100180 struct snd_soc_dai *dai;
181
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100182 mutex_lock(&client_mutex);
183
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000184 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000185 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800186 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100187
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100188 mutex_unlock(&client_mutex);
189
Donglin Peng700c17c2018-01-18 13:31:26 +0800190 return 0;
191}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800192DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100193
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000194static int component_list_show(struct seq_file *m, void *v)
195{
196 struct snd_soc_component *component;
197
198 mutex_lock(&client_mutex);
199
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000200 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000201 seq_printf(m, "%s\n", component->name);
202
203 mutex_unlock(&client_mutex);
204
205 return 0;
206}
207DEFINE_SHOW_ATTRIBUTE(component_list);
208
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200209static void soc_init_card_debugfs(struct snd_soc_card *card)
210{
211 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000212 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200213
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200214 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
215 &card->pop_time);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216}
217
218static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
219{
220 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900221 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200222}
223
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200224static void snd_soc_debugfs_init(void)
225{
226 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200227
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200228 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
229 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000230
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200231 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
232 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200233}
234
235static void snd_soc_debugfs_exit(void)
236{
237 debugfs_remove_recursive(snd_soc_debugfs_root);
238}
239
Mark Brown2624d5f2009-11-03 21:56:13 +0000240#else
241
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200242static inline void soc_init_component_debugfs(
243 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000244{
245}
246
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200247static inline void soc_cleanup_component_debugfs(
248 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000249{
250}
251
Axel Linb95fccb2010-11-09 17:06:44 +0800252static inline void soc_init_card_debugfs(struct snd_soc_card *card)
253{
254}
255
256static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
257{
258}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200259
260static inline void snd_soc_debugfs_init(void)
261{
262}
263
264static inline void snd_soc_debugfs_exit(void)
265{
266}
267
Mark Brown2624d5f2009-11-03 21:56:13 +0000268#endif
269
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000270static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
271 struct snd_soc_component *component)
272{
273 struct snd_soc_rtdcom_list *rtdcom;
274 struct snd_soc_rtdcom_list *new_rtdcom;
275
276 for_each_rtdcom(rtd, rtdcom) {
277 /* already connected */
278 if (rtdcom->component == component)
279 return 0;
280 }
281
282 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
283 if (!new_rtdcom)
284 return -ENOMEM;
285
286 new_rtdcom->component = component;
287 INIT_LIST_HEAD(&new_rtdcom->list);
288
289 list_add_tail(&new_rtdcom->list, &rtd->component_list);
290
291 return 0;
292}
293
294static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
295{
296 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
297
298 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
299 kfree(rtdcom1);
300
301 INIT_LIST_HEAD(&rtd->component_list);
302}
303
304struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
305 const char *driver_name)
306{
307 struct snd_soc_rtdcom_list *rtdcom;
308
Kuninori Morimoto971da242018-01-23 00:41:24 +0000309 if (!driver_name)
310 return NULL;
311
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000312 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000313 const char *component_name = rtdcom->component->driver->name;
314
315 if (!component_name)
316 continue;
317
318 if ((component_name == driver_name) ||
319 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000320 return rtdcom->component;
321 }
322
323 return NULL;
324}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000325EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000326
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100327struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
328 const char *dai_link, int stream)
329{
Mengdong Lin1a497982015-11-18 02:34:11 -0500330 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100331
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000332 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500333 if (rtd->dai_link->no_pcm &&
334 !strcmp(rtd->dai_link->name, dai_link))
335 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100336 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000337 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100338 return NULL;
339}
340EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
341
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000342static const struct snd_soc_ops null_snd_soc_ops;
343
Mengdong Lin1a497982015-11-18 02:34:11 -0500344static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
345 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
346{
347 struct snd_soc_pcm_runtime *rtd;
348
349 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
350 if (!rtd)
351 return NULL;
352
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000353 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500354 rtd->card = card;
355 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000356 if (!rtd->dai_link->ops)
357 rtd->dai_link->ops = &null_snd_soc_ops;
358
Kees Cook6396bb22018-06-12 14:03:40 -0700359 rtd->codec_dais = kcalloc(dai_link->num_codecs,
360 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500361 GFP_KERNEL);
362 if (!rtd->codec_dais) {
363 kfree(rtd);
364 return NULL;
365 }
366
367 return rtd;
368}
369
370static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
371{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000372 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000373 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500374 kfree(rtd);
375}
376
377static void soc_add_pcm_runtime(struct snd_soc_card *card,
378 struct snd_soc_pcm_runtime *rtd)
379{
380 list_add_tail(&rtd->list, &card->rtd_list);
381 rtd->num = card->num_rtd;
382 card->num_rtd++;
383}
384
385static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
386{
387 struct snd_soc_pcm_runtime *rtd, *_rtd;
388
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000389 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500390 list_del(&rtd->list);
391 soc_free_pcm_runtime(rtd);
392 }
393
394 card->num_rtd = 0;
395}
396
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100397struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
398 const char *dai_link)
399{
Mengdong Lin1a497982015-11-18 02:34:11 -0500400 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100401
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000402 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500403 if (!strcmp(rtd->dai_link->name, dai_link))
404 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100405 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000406 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100407 return NULL;
408}
409EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
410
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900411static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
412{
413 struct snd_soc_pcm_runtime *rtd;
414
415 for_each_card_rtds(card, rtd)
416 flush_delayed_work(&rtd->delayed_work);
417}
418
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000419#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200420/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000421int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200422{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000423 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000424 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500425 struct snd_soc_pcm_runtime *rtd;
426 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200427
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200428 /* If the card is not initialized yet there is nothing to do */
429 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200430 return 0;
431
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200432 /*
433 * Due to the resume being scheduled into a workqueue we could
434 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100435 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000436 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100437
438 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000439 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100440
Mark Browna00f90f2010-12-02 16:24:24 +0000441 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000442 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000443 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100444
Mengdong Lin1a497982015-11-18 02:34:11 -0500445 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100446 continue;
447
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000448 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900449 if (dai->playback_active)
450 snd_soc_dai_digital_mute(dai, 1,
451 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200452 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200453 }
454
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100455 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000456 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500457 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100458 continue;
459
Mengdong Lin1a497982015-11-18 02:34:11 -0500460 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100461 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100462
Mark Brown87506542008-11-18 20:50:34 +0000463 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000464 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000466 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500467 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100468
Mengdong Lin1a497982015-11-18 02:34:11 -0500469 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100470 continue;
471
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900472 if (!cpu_dai->driver->bus_control)
473 snd_soc_dai_suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100474 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200476 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900477 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100478
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000479 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480
Mengdong Lin1a497982015-11-18 02:34:11 -0500481 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100482 continue;
483
Mengdong Lin1a497982015-11-18 02:34:11 -0500484 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800485 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800486 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800489 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800490 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 }
492
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200493 /* Recheck all endpoints too, their state is affected by suspend */
494 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700495 snd_soc_dapm_sync(&card->dapm);
496
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000497 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000498 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200499 struct snd_soc_dapm_context *dapm =
500 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000501
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200502 /*
503 * If there are paths active then the COMPONENT will be held
504 * with bias _ON and should not be suspended.
505 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000506 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200507 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000509 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000510 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000511 * bias off then being in STANDBY
512 * means it's doing something,
513 * otherwise fall through.
514 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200515 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000516 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200517 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000518 break;
519 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500520 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000523 if (component->driver->suspend)
524 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000525 component->suspended = 1;
526 if (component->regmap)
527 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800528 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000529 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 break;
531 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000532 dev_dbg(component->dev,
533 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 break;
535 }
536 }
537 }
538
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000539 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500540 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541
Mengdong Lin1a497982015-11-18 02:34:11 -0500542 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 continue;
544
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900545 if (cpu_dai->driver->bus_control)
546 snd_soc_dai_suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800547
548 /* deactivate pins to sleep state */
549 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 }
551
Mark Brown87506542008-11-18 20:50:34 +0000552 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000553 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
555 return 0;
556}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000557EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200559/*
560 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100561 * setting our codec back up, which can be very slow on I2C
562 */
563static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200566 container_of(work, struct snd_soc_card,
567 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000569 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500570 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200572 /*
573 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100574 * so userspace apps are blocked from touching us
575 */
576
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000577 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100578
Mark Brown9949788b2010-05-07 20:24:05 +0100579 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100581
Mark Brown87506542008-11-18 20:50:34 +0000582 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000583 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100585 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000586 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500587 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100588
Mengdong Lin1a497982015-11-18 02:34:11 -0500589 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100590 continue;
591
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900592 if (cpu_dai->driver->bus_control)
593 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594 }
595
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000596 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000597 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000598 if (component->driver->resume)
599 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000600 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100601 }
602 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000604 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100605
Mengdong Lin1a497982015-11-18 02:34:11 -0500606 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100607 continue;
608
Mengdong Lin1a497982015-11-18 02:34:11 -0500609 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000610 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800611 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612
Mengdong Lin1a497982015-11-18 02:34:11 -0500613 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000614 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800615 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200616 }
617
Mark Brown3ff3f642008-05-19 12:32:25 +0200618 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000619 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000620 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Mengdong Lin1a497982015-11-18 02:34:11 -0500622 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100623 continue;
624
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000625 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900626 if (dai->playback_active)
627 snd_soc_dai_digital_mute(dai, 0,
628 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200629 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 }
631
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000632 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500633 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100634
Mengdong Lin1a497982015-11-18 02:34:11 -0500635 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100636 continue;
637
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900638 if (!cpu_dai->driver->bus_control)
639 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640 }
641
Mark Brown87506542008-11-18 20:50:34 +0000642 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000643 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000645 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100646
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200647 /* Recheck all endpoints too, their state is affected by suspend */
648 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700649 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530650
651 /* userspace can access us now we are back as we were before */
652 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100653}
654
655/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000656int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100657{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000658 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100659 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500660 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900661 struct snd_soc_dai *codec_dai;
662 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200663
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200664 /* If the card is not initialized yet there is nothing to do */
665 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800666 return 0;
667
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800668 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000669 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200671
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800672 if (cpu_dai->active)
673 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200674
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900675 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200676 if (codec_dai->active)
677 pinctrl_pm_select_default_state(codec_dai->dev);
678 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800679 }
680
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100681 /*
682 * DAIs that also act as the control bus master might have other drivers
683 * hanging off them so need to resume immediately. Other drivers don't
684 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100685 * due to I/O costs and anti-pop so handle them out of line.
686 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000687 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500688 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200689
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100690 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600691 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100692 if (bus_control) {
693 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600694 soc_resume_deferred(&card->deferred_resume_work);
695 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000696 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600697 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000698 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100699 }
Andy Green6ed25972008-06-13 16:24:05 +0100700
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 return 0;
702}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000703EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000705#define snd_soc_suspend NULL
706#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707#endif
708
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100709static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800710};
711
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900712static struct device_node
713*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100714{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900715 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100716
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900717 of_node = component->dev->of_node;
718 if (!of_node && component->dev->parent)
719 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100720
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900721 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100722}
723
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000724static int snd_soc_is_matching_component(
725 const struct snd_soc_dai_link_component *dlc,
726 struct snd_soc_component *component)
727{
728 struct device_node *component_of_node;
729
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900730 if (!dlc)
731 return 0;
732
733 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000734
735 if (dlc->of_node && component_of_node != dlc->of_node)
736 return 0;
737 if (dlc->name && strcmp(component->name, dlc->name))
738 return 0;
739
740 return 1;
741}
742
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900744 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745{
746 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
748 lockdep_assert_held(&client_mutex);
749
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900750 /*
751 * NOTE
752 *
753 * It returns *1st* found component, but some driver
754 * has few components by same of_node/name
755 * ex)
756 * CPU component and generic DMAEngine component
757 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900758 for_each_component(component)
759 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761
762 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100763}
764
Mengdong Linfbb88b52016-04-22 12:25:33 +0800765/**
766 * snd_soc_find_dai - Find a registered DAI
767 *
Jeffy Chen49584712017-08-22 15:57:21 +0800768 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800769 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700770 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800771 * find the DAI of the same name. The component's of_node and name
772 * should also match if being specified.
773 *
774 * Return: pointer of DAI, or NULL if not found.
775 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800776struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200777 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100778{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200779 struct snd_soc_component *component;
780 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100781
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100782 lockdep_assert_held(&client_mutex);
783
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200784 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000785 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000786 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200787 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000788 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800789 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800790 && (!dai->driver->name
791 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100792 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100793
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200794 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100795 }
796 }
797
798 return NULL;
799}
Mengdong Lin305e9022016-04-19 13:12:25 +0800800EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100801
Mengdong Lin17fb17552016-11-03 01:04:12 +0800802/**
803 * snd_soc_find_dai_link - Find a DAI link
804 *
805 * @card: soc card
806 * @id: DAI link ID to match
807 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000808 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800809 *
810 * This function will search all existing DAI links of the soc card to
811 * find the link of the same ID. Since DAI links may not have their
812 * unique ID, so name and stream name should also match if being
813 * specified.
814 *
815 * Return: pointer of DAI link, or NULL if not found.
816 */
817struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
818 int id, const char *name,
819 const char *stream_name)
820{
821 struct snd_soc_dai_link *link, *_link;
822
823 lockdep_assert_held(&client_mutex);
824
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000825 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800826 if (link->id != id)
827 continue;
828
829 if (name && (!link->name || strcmp(name, link->name)))
830 continue;
831
832 if (stream_name && (!link->stream_name
833 || strcmp(stream_name, link->stream_name)))
834 continue;
835
836 return link;
837 }
838
839 return NULL;
840}
841EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
842
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800843static bool soc_is_dai_link_bound(struct snd_soc_card *card,
844 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800846 struct snd_soc_pcm_runtime *rtd;
847
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000848 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800849 if (rtd->dai_link == dai_link)
850 return true;
851 }
852
853 return false;
854}
855
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500856static int soc_bind_dai_link(struct snd_soc_card *card,
857 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858{
Mengdong Lin1a497982015-11-18 02:34:11 -0500859 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +0200860 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000861 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200862 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863
Liam Girdwooda655de82018-07-02 16:59:54 +0100864 if (dai_link->ignore)
865 return 0;
866
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500867 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000868
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800869 if (soc_is_dai_link_bound(card, dai_link)) {
870 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
871 dai_link->name);
872 return 0;
873 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530875 rtd = soc_new_pcm_runtime(card, dai_link);
876 if (!rtd)
877 return -ENOMEM;
878
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900879 /* FIXME: we need multi CPU support in the future */
880 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000881 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100882 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900883 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500884 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000885 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000886 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000887
Benoit Cousson88bd8702014-07-08 23:19:34 +0200888 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900889 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +0200890 for_each_link_codecs(dai_link, i, codec) {
891 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900892 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100893 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +0200894 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500895 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200896 }
Jerome Brunet34614732019-06-27 14:13:50 +0200897
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900898 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000899 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100900
Benoit Cousson88bd8702014-07-08 23:19:34 +0200901 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900902 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100903
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900904 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +0200905 for_each_link_platforms(dai_link, i, platform) {
906 for_each_component(component) {
907 if (!snd_soc_is_matching_component(platform, component))
908 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000909
Jerome Brunet34614732019-06-27 14:13:50 +0200910 snd_soc_rtdcom_add(rtd, component);
911 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000912 }
913
Mengdong Lin1a497982015-11-18 02:34:11 -0500914 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500916
917_err_defer:
918 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200919 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920}
921
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900922static void soc_cleanup_component(struct snd_soc_component *component)
923{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200924 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900925 list_del(&component->card_list);
926 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
927 soc_cleanup_component_debugfs(component);
928 component->card = NULL;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900929 snd_soc_component_module_put_when_close(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900930}
931
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200932static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600933{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200934 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200935 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600936
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000937 if (component->driver->remove)
938 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600939
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900940 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600941}
942
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200943static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200944{
945 int err;
946
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600947 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000948 dai->driver->remove_order != order)
949 return;
950
Kuninori Morimotodcdab582019-07-22 10:35:05 +0900951 err = snd_soc_dai_remove(dai);
952 if (err < 0)
953 dev_err(dai->dev,
954 "ASoC: failed to remove %s: %d\n",
955 dai->name, err);
956
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000957 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100958}
959
Mengdong Lin1a497982015-11-18 02:34:11 -0500960static void soc_remove_link_dais(struct snd_soc_card *card,
961 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200963 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000964 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965
966 /* unregister the rtd device */
967 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800968 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 rtd->dev_registered = 0;
970 }
971
972 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000973 for_each_rtd_codec_dai(rtd, i, codec_dai)
974 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200976 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977}
978
Mengdong Lin1a497982015-11-18 02:34:11 -0500979static void soc_remove_link_components(struct snd_soc_card *card,
980 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600981{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200982 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000983 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600984
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000985 for_each_rtdcom(rtd, rtdcom) {
986 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600987
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200988 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200989 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600990 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600991}
992
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900993static void soc_remove_dai_links(struct snd_soc_card *card)
994{
Mengdong Lin1a497982015-11-18 02:34:11 -0500995 int order;
996 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800997 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900998
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_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001002 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001003
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001004 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001005 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001006 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007 }
1008
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001009 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001010 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1011 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1012 link->name);
1013
1014 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001015 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001016}
1017
Mengdong Lin923c5e612015-11-23 11:01:49 -05001018static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001019 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001020{
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001021 int i;
Jerome Brunet34614732019-06-27 14:13:50 +02001022 struct snd_soc_dai_link_component *codec, *platform;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001023
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001024 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001025 /*
1026 * Codec must be specified by 1 of name or OF node,
1027 * not both or neither.
1028 */
Jerome Brunet34614732019-06-27 14:13:50 +02001029 if (!!codec->name == !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001030 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1031 link->name);
1032 return -EINVAL;
1033 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001034
Mengdong Lin923c5e612015-11-23 11:01:49 -05001035 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001036 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001037 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1038 link->name);
1039 return -EINVAL;
1040 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001041
1042 /*
1043 * Defer card registration if codec component is not added to
1044 * component list.
1045 */
1046 if (!soc_find_component(codec))
1047 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001048 }
1049
Jerome Brunet34614732019-06-27 14:13:50 +02001050 for_each_link_platforms(link, i, platform) {
1051 /*
1052 * Platform may be specified by either name or OF node, but it
1053 * can be left unspecified, then no components will be inserted
1054 * in the rtdcom list
1055 */
1056 if (!!platform->name == !!platform->of_node) {
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001057 dev_err(card->dev,
Jerome Brunet34614732019-06-27 14:13:50 +02001058 "ASoC: Neither/both platform name/of_node are set for %s\n",
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001059 link->name);
1060 return -EINVAL;
1061 }
1062
1063 /*
Jerome Brunet34614732019-06-27 14:13:50 +02001064 * Defer card registration if platform component is not added to
1065 * component list.
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001066 */
Jerome Brunet34614732019-06-27 14:13:50 +02001067 if (!soc_find_component(platform))
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001068 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001069 }
1070
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001071 /* FIXME */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001072 if (link->num_cpus > 1) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001073 dev_err(card->dev,
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001074 "ASoC: multi cpu is not yet supported %s\n",
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001075 link->name);
1076 return -EINVAL;
1077 }
1078
Mengdong Lin923c5e612015-11-23 11:01:49 -05001079 /*
Mengdong Lin923c5e612015-11-23 11:01:49 -05001080 * CPU device may be specified by either name or OF node, but
1081 * can be left unspecified, and will be matched based on DAI
1082 * name alone..
1083 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001084 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001085 dev_err(card->dev,
1086 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1087 link->name);
1088 return -EINVAL;
1089 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301090
1091 /*
1092 * Defer card registartion if cpu dai component is not added to
1093 * component list.
1094 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001095 if ((link->cpus->of_node || link->cpus->name) &&
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001096 !soc_find_component(link->cpus))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301097 return -EPROBE_DEFER;
1098
Mengdong Lin923c5e612015-11-23 11:01:49 -05001099 /*
1100 * At least one of CPU DAI name or CPU device name/node must be
1101 * specified
1102 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001103 if (!link->cpus->dai_name &&
1104 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001105 dev_err(card->dev,
1106 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1107 link->name);
1108 return -EINVAL;
1109 }
1110
1111 return 0;
1112}
1113
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001114void snd_soc_disconnect_sync(struct device *dev)
1115{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001116 struct snd_soc_component *component =
1117 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001118
1119 if (!component || !component->card)
1120 return;
1121
1122 snd_card_disconnect_sync(component->card->snd_card);
1123}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001124EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001125
Mengdong Linf8f80362015-12-02 14:11:22 +08001126/**
1127 * snd_soc_add_dai_link - Add a DAI link dynamically
1128 * @card: The ASoC card to which the DAI link is added
1129 * @dai_link: The new DAI link to add
1130 *
1131 * This function adds a DAI link to the ASoC card's link list.
1132 *
1133 * Note: Topology can use this API to add DAI links when probing the
1134 * topology component. And machine drivers can still define static
1135 * DAI links in dai_link array.
1136 */
1137int snd_soc_add_dai_link(struct snd_soc_card *card,
1138 struct snd_soc_dai_link *dai_link)
1139{
1140 if (dai_link->dobj.type
1141 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1142 dev_err(card->dev, "Invalid dai link type %d\n",
1143 dai_link->dobj.type);
1144 return -EINVAL;
1145 }
1146
1147 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001148 /*
1149 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001150 * on the link created by topology.
1151 */
1152 if (dai_link->dobj.type && card->add_dai_link)
1153 card->add_dai_link(card, dai_link);
1154
Mengdong Linf8f80362015-12-02 14:11:22 +08001155 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001156
1157 return 0;
1158}
1159EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1160
1161/**
1162 * snd_soc_remove_dai_link - Remove a DAI link from the list
1163 * @card: The ASoC card that owns the link
1164 * @dai_link: The DAI link to remove
1165 *
1166 * This function removes a DAI link from the ASoC card's link list.
1167 *
1168 * For DAI links previously added by topology, topology should
1169 * remove them by using the dobj embedded in the link.
1170 */
1171void snd_soc_remove_dai_link(struct snd_soc_card *card,
1172 struct snd_soc_dai_link *dai_link)
1173{
1174 struct snd_soc_dai_link *link, *_link;
1175
1176 if (dai_link->dobj.type
1177 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1178 dev_err(card->dev, "Invalid dai link type %d\n",
1179 dai_link->dobj.type);
1180 return;
1181 }
1182
1183 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001184 /*
1185 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001186 * on the link created by topology.
1187 */
1188 if (dai_link->dobj.type && card->remove_dai_link)
1189 card->remove_dai_link(card, dai_link);
1190
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001191 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001192 if (link == dai_link) {
1193 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001194 return;
1195 }
1196 }
1197}
1198EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1199
Jerome Brunetaefba452018-07-13 14:50:43 +02001200static void soc_set_of_name_prefix(struct snd_soc_component *component)
1201{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001202 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001203 const char *str;
1204 int ret;
1205
Jerome Brunetaefba452018-07-13 14:50:43 +02001206 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1207 &str);
1208 if (!ret)
1209 component->name_prefix = str;
1210}
1211
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001212static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001213 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001214{
1215 int i;
1216
Jerome Brunetaefba452018-07-13 14:50:43 +02001217 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001218 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001219 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001220
1221 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001222 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001223 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001224 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001225 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001226 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001227 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001228
1229 /*
1230 * If there is no configuration table or no match in the table,
1231 * check if a prefix is provided in the node
1232 */
1233 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001234}
1235
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001236static int soc_probe_component(struct snd_soc_card *card,
1237 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001238{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001239 struct snd_soc_dapm_context *dapm =
1240 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001241 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001242 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001243
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001244 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001245 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001246
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001247 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001248 if (component->card != card) {
1249 dev_err(component->dev,
1250 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1251 card->name, component->card->name);
1252 return -ENODEV;
1253 }
1254 return 0;
1255 }
1256
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +09001257 ret = snd_soc_component_module_get_when_probe(component);
1258 if (ret < 0)
1259 return ret;
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001260
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001261 component->card = card;
1262 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001263 INIT_LIST_HEAD(&component->card_list);
1264 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001265 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001267 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001268
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001269 if (component->driver->dapm_widgets) {
1270 ret = snd_soc_dapm_new_controls(dapm,
1271 component->driver->dapm_widgets,
1272 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001273
1274 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001275 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001276 "Failed to create new controls %d\n", ret);
1277 goto err_probe;
1278 }
1279 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001280
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001281 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001282 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001283 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001284 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001285 "Failed to create DAI widgets %d\n", ret);
1286 goto err_probe;
1287 }
1288 }
Mark Brown888df392012-02-16 19:37:51 -08001289
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001290 if (component->driver->probe) {
1291 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001292 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001293 dev_err(component->dev,
1294 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001295 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001296 }
1297 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001298 WARN(dapm->idle_bias_off &&
1299 dapm->bias_level != SND_SOC_BIAS_OFF,
1300 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1301 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001302
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001303 /* machine specific init */
1304 if (component->init) {
1305 ret = component->init(component);
1306 if (ret < 0) {
1307 dev_err(component->dev,
1308 "Failed to do machine specific init %d\n", ret);
1309 goto err_probe;
1310 }
1311 }
1312
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001313 if (component->driver->controls)
1314 snd_soc_add_component_controls(component,
1315 component->driver->controls,
1316 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001317 if (component->driver->dapm_routes)
1318 snd_soc_dapm_add_routes(dapm,
1319 component->driver->dapm_routes,
1320 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001321
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001323 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001324 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001325
Jarkko Nikula70d293312011-01-27 16:24:22 +02001326err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001327 if (ret < 0)
1328 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001329
1330 return ret;
1331}
1332
Mark Brown36ae1a92012-01-06 17:12:45 -08001333static void rtd_release(struct device *dev)
1334{
1335 kfree(dev);
1336}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001337
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001338static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1339 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001340{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001341 int ret = 0;
1342
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001344 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1345 if (!rtd->dev)
1346 return -ENOMEM;
1347 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001348 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001349 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001350 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001351 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001352 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001353 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001354 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1355 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1356 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1357 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001358 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001359 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001360 /* calling put_device() here to free the rtd->dev */
1361 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001362 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001363 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001364 return ret;
1365 }
1366 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001367 return 0;
1368}
1369
Mengdong Lin1a497982015-11-18 02:34:11 -05001370static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001371 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001372{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001373 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001374 struct snd_soc_rtdcom_list *rtdcom;
1375 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001376
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001377 for_each_rtdcom(rtd, rtdcom) {
1378 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001379
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001380 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001381 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001382 if (ret < 0)
1383 return ret;
1384 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001385 }
1386
Stephen Warren62ae68f2012-06-08 12:34:23 -06001387 return 0;
1388}
1389
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001390static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001391{
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001392 int ret;
1393
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001394 if (dai->probed ||
1395 dai->driver->probe_order != order)
1396 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001397
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001398 ret = snd_soc_dai_probe(dai);
1399 if (ret < 0) {
1400 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1401 dai->name, ret);
1402 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001403 }
1404
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001405 dai->probed = 1;
1406
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001407 return 0;
1408}
1409
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001410static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1411 struct snd_soc_pcm_runtime *rtd)
1412{
1413 int i, ret = 0;
1414
1415 for (i = 0; i < num_dais; ++i) {
1416 struct snd_soc_dai_driver *drv = dais[i]->driver;
1417
Rohit kumarde17f142018-11-01 18:08:49 +05301418 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001419 ret = drv->pcm_new(rtd, dais[i]);
1420 if (ret < 0) {
1421 dev_err(dais[i]->dev,
1422 "ASoC: Failed to bind %s with pcm device\n",
1423 dais[i]->name);
1424 return ret;
1425 }
1426 }
1427
1428 return 0;
1429}
1430
Mengdong Lin1a497982015-11-18 02:34:11 -05001431static int soc_probe_link_dais(struct snd_soc_card *card,
1432 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433{
Mengdong Lin1a497982015-11-18 02:34:11 -05001434 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001435 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001436 struct snd_soc_rtdcom_list *rtdcom;
1437 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001438 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001439 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001441 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001442 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001444 /* set default power off timeout */
1445 rtd->pmdown_time = pmdown_time;
1446
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001447 ret = soc_probe_dai(cpu_dai, order);
1448 if (ret)
1449 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001452 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1453 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001454 if (ret)
1455 return ret;
1456 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001457
Liam Girdwood0168bf02011-06-07 16:08:05 +01001458 /* complete DAI probe during last probe */
1459 if (order != SND_SOC_COMP_ORDER_LAST)
1460 return 0;
1461
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001462 /* do machine specific initialization */
1463 if (dai_link->init) {
1464 ret = dai_link->init(rtd);
1465 if (ret < 0) {
1466 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1467 dai_link->name, ret);
1468 return ret;
1469 }
1470 }
1471
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001472 if (dai_link->dai_fmt) {
1473 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1474 if (ret)
1475 return ret;
1476 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001477
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001478 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001479 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001480 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001481
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001482#ifdef CONFIG_DEBUG_FS
1483 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001484 if (dai_link->dynamic)
1485 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001486#endif
1487
Liam Girdwooda655de82018-07-02 16:59:54 +01001488 num = rtd->num;
1489
1490 /*
1491 * most drivers will register their PCMs using DAI link ordering but
1492 * topology based drivers can use the DAI link id field to set PCM
1493 * device number and then use rtd + a base offset of the BEs.
1494 */
1495 for_each_rtdcom(rtd, rtdcom) {
1496 component = rtdcom->component;
1497
1498 if (!component->driver->use_dai_pcm_id)
1499 continue;
1500
1501 if (rtd->dai_link->no_pcm)
1502 num += component->driver->be_pcm_base;
1503 else
1504 num = rtd->dai_link->id;
1505 }
1506
Kuninori Morimotob423c422019-07-22 10:35:29 +09001507 /* create compress_device if possible */
1508 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1509 if (ret != -ENOTSUPP) {
1510 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001511 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301512 dai_link->stream_name);
Kuninori Morimotob423c422019-07-22 10:35:29 +09001513 return ret;
1514 }
1515
Jerome Bruneta3420312019-07-25 18:59:47 +02001516 /* create the pcm */
1517 ret = soc_new_pcm(rtd, num);
1518 if (ret < 0) {
1519 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1520 dai_link->stream_name, ret);
1521 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522 }
Jerome Bruneta3420312019-07-25 18:59:47 +02001523 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1524 if (ret < 0)
1525 return ret;
1526 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1527 rtd->num_codecs, rtd);
1528 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529}
1530
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001531static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001532{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001533 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001534 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001535 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001536
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001537 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1538 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001539 dlc.name = aux_dev->codec_name;
1540 dlc.of_node = aux_dev->codec_of_node;
1541 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001542 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001543 if (dlc.of_node)
1544 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001545 goto err_defer;
1546 }
1547 } else if (aux_dev->name) {
1548 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001549 dlc.name = aux_dev->name;
1550 dlc.of_node = NULL;
1551 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001552 if (!component)
1553 goto err_defer;
1554 } else {
1555 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1556 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001557 }
1558
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001559 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001560 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001561
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001562 return 0;
1563
1564err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001565 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001566 return -EPROBE_DEFER;
1567}
1568
1569static int soc_probe_aux_devices(struct snd_soc_card *card)
1570{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001571 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001572 int order;
1573 int ret;
1574
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001575 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001576 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001577 if (comp->driver->probe_order == order) {
1578 ret = soc_probe_component(card, comp);
1579 if (ret < 0) {
1580 dev_err(card->dev,
1581 "ASoC: failed to probe aux component %s %d\n",
1582 comp->name, ret);
1583 return ret;
1584 }
1585 }
1586 }
1587 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001588
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001589 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001590}
1591
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001592static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001593{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001594 struct snd_soc_component *comp, *_comp;
1595 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001596
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001597 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001598 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001599 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001600
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001601 if (comp->driver->remove_order == order) {
1602 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001603 /* remove it from the card's aux_comp_list */
1604 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001605 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001606 }
1607 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001608}
1609
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001610/**
1611 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1612 * @rtd: The runtime for which the DAI link format should be changed
1613 * @dai_fmt: The new DAI link format
1614 *
1615 * This function updates the DAI link format for all DAIs connected to the DAI
1616 * link for the specified runtime.
1617 *
1618 * Note: For setups with a static format set the dai_fmt field in the
1619 * corresponding snd_dai_link struct instead of using this function.
1620 *
1621 * Returns 0 on success, otherwise a negative error code.
1622 */
1623int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1624 unsigned int dai_fmt)
1625{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001626 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001627 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001628 unsigned int i;
1629 int ret;
1630
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001631 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001632 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1633 if (ret != 0 && ret != -ENOTSUPP) {
1634 dev_warn(codec_dai->dev,
1635 "ASoC: Failed to set DAI format: %d\n", ret);
1636 return ret;
1637 }
1638 }
1639
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001640 /*
1641 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1642 * the component which has non_legacy_dai_naming is Codec
1643 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001644 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001645 unsigned int inv_dai_fmt;
1646
1647 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1648 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1649 case SND_SOC_DAIFMT_CBM_CFM:
1650 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1651 break;
1652 case SND_SOC_DAIFMT_CBM_CFS:
1653 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1654 break;
1655 case SND_SOC_DAIFMT_CBS_CFM:
1656 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1657 break;
1658 case SND_SOC_DAIFMT_CBS_CFS:
1659 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1660 break;
1661 }
1662
1663 dai_fmt = inv_dai_fmt;
1664 }
1665
1666 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1667 if (ret != 0 && ret != -ENOTSUPP) {
1668 dev_warn(cpu_dai->dev,
1669 "ASoC: Failed to set DAI format: %d\n", ret);
1670 return ret;
1671 }
1672
1673 return 0;
1674}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001675EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001676
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001677#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001678/*
1679 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001680 * separate different DMI fields in the card long name. Only number and
1681 * alphabet characters and a few separator characters are kept.
1682 */
1683static void cleanup_dmi_name(char *name)
1684{
1685 int i, j = 0;
1686
1687 for (i = 0; name[i]; i++) {
1688 if (isalnum(name[i]) || (name[i] == '.')
1689 || (name[i] == '_'))
1690 name[j++] = name[i];
1691 else if (name[i] == '-')
1692 name[j++] = '_';
1693 }
1694
1695 name[j] = '\0';
1696}
1697
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001698/*
1699 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001700 * in the black list.
1701 */
1702static int is_dmi_valid(const char *field)
1703{
1704 int i = 0;
1705
1706 while (dmi_blacklist[i]) {
1707 if (strstr(field, dmi_blacklist[i]))
1708 return 0;
1709 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001710 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001711
1712 return 1;
1713}
1714
Liam Girdwood345233d2017-01-14 16:13:02 +08001715/**
1716 * snd_soc_set_dmi_name() - Register DMI names to card
1717 * @card: The card to register DMI names
1718 * @flavour: The flavour "differentiator" for the card amongst its peers.
1719 *
1720 * An Intel machine driver may be used by many different devices but are
1721 * difficult for userspace to differentiate, since machine drivers ususally
1722 * use their own name as the card short name and leave the card long name
1723 * blank. To differentiate such devices and fix bugs due to lack of
1724 * device-specific configurations, this function allows DMI info to be used
1725 * as the sound card long name, in the format of
1726 * "vendor-product-version-board"
1727 * (Character '-' is used to separate different DMI fields here).
1728 * This will help the user space to load the device-specific Use Case Manager
1729 * (UCM) configurations for the card.
1730 *
1731 * Possible card long names may be:
1732 * DellInc.-XPS139343-01-0310JH
1733 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1734 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1735 *
1736 * This function also supports flavoring the card longname to provide
1737 * the extra differentiation, like "vendor-product-version-board-flavor".
1738 *
1739 * We only keep number and alphabet characters and a few separator characters
1740 * in the card long name since UCM in the user space uses the card long names
1741 * as card configuration directory names and AudoConf cannot support special
1742 * charactors like SPACE.
1743 *
1744 * Returns 0 on success, otherwise a negative error code.
1745 */
1746int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1747{
1748 const char *vendor, *product, *product_version, *board;
1749 size_t longname_buf_size = sizeof(card->snd_card->longname);
1750 size_t len;
1751
1752 if (card->long_name)
1753 return 0; /* long name already set by driver or from DMI */
1754
1755 /* make up dmi long name as: vendor.product.version.board */
1756 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001757 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001758 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1759 return 0;
1760 }
1761
1762 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1763 "%s", vendor);
1764 cleanup_dmi_name(card->dmi_longname);
1765
1766 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001767 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001768 len = strlen(card->dmi_longname);
1769 snprintf(card->dmi_longname + len,
1770 longname_buf_size - len,
1771 "-%s", product);
1772
1773 len++; /* skip the separator "-" */
1774 if (len < longname_buf_size)
1775 cleanup_dmi_name(card->dmi_longname + len);
1776
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001777 /*
1778 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001779 * name in the product version field
1780 */
1781 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001782 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001783 len = strlen(card->dmi_longname);
1784 snprintf(card->dmi_longname + len,
1785 longname_buf_size - len,
1786 "-%s", product_version);
1787
1788 len++;
1789 if (len < longname_buf_size)
1790 cleanup_dmi_name(card->dmi_longname + len);
1791 }
1792 }
1793
1794 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001795 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001796 len = strlen(card->dmi_longname);
1797 snprintf(card->dmi_longname + len,
1798 longname_buf_size - len,
1799 "-%s", board);
1800
1801 len++;
1802 if (len < longname_buf_size)
1803 cleanup_dmi_name(card->dmi_longname + len);
1804 } else if (!product) {
1805 /* fall back to using legacy name */
1806 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1807 return 0;
1808 }
1809
1810 /* Add flavour to dmi long name */
1811 if (flavour) {
1812 len = strlen(card->dmi_longname);
1813 snprintf(card->dmi_longname + len,
1814 longname_buf_size - len,
1815 "-%s", flavour);
1816
1817 len++;
1818 if (len < longname_buf_size)
1819 cleanup_dmi_name(card->dmi_longname + len);
1820 }
1821
1822 /* set the card long name */
1823 card->long_name = card->dmi_longname;
1824
1825 return 0;
1826}
1827EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001828#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001829
Liam Girdwooda655de82018-07-02 16:59:54 +01001830static void soc_check_tplg_fes(struct snd_soc_card *card)
1831{
1832 struct snd_soc_component *component;
1833 const struct snd_soc_component_driver *comp_drv;
1834 struct snd_soc_dai_link *dai_link;
1835 int i;
1836
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001837 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001838
1839 /* does this component override FEs ? */
1840 if (!component->driver->ignore_machine)
1841 continue;
1842
1843 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001844 if (!strcmp(component->driver->ignore_machine,
1845 card->dev->driver->name))
1846 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001847 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001848 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001849 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001850match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001851 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001852 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001853
1854 /* ignore this FE */
1855 if (dai_link->dynamic) {
1856 dai_link->ignore = true;
1857 continue;
1858 }
1859
1860 dev_info(card->dev, "info: override FE DAI link %s\n",
1861 card->dai_link[i].name);
1862
1863 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001864 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001865 dev_err(card->dev, "init platform error");
1866 continue;
1867 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001868 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001869
1870 /* convert non BE into BE */
1871 dai_link->no_pcm = 1;
1872
1873 /* override any BE fixups */
1874 dai_link->be_hw_params_fixup =
1875 component->driver->be_hw_params_fixup;
1876
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001877 /*
1878 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001879 * dai link name if it's NULL to help bind widgets.
1880 */
1881 if (!dai_link->stream_name)
1882 dai_link->stream_name = dai_link->name;
1883 }
1884
1885 /* Inform userspace we are using alternate topology */
1886 if (component->driver->topology_name_prefix) {
1887
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001888 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001889 if (!card->topology_shortname_created) {
1890 comp_drv = component->driver;
1891
1892 snprintf(card->topology_shortname, 32, "%s-%s",
1893 comp_drv->topology_name_prefix,
1894 card->name);
1895 card->topology_shortname_created = true;
1896 }
1897
1898 /* use topology shortname */
1899 card->name = card->topology_shortname;
1900 }
1901 }
1902}
1903
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001904static int soc_cleanup_card_resources(struct snd_soc_card *card)
1905{
1906 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001907 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001908 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001909 card->snd_card = NULL;
1910 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001911
1912 /* remove and free each DAI */
1913 soc_remove_dai_links(card);
1914 soc_remove_pcm_runtimes(card);
1915
1916 /* remove auxiliary devices */
1917 soc_remove_aux_devices(card);
1918
1919 snd_soc_dapm_free(&card->dapm);
1920 soc_cleanup_card_debugfs(card);
1921
1922 /* remove the card */
1923 if (card->remove)
1924 card->remove(card);
1925
1926 return 0;
1927}
1928
Mark Brownb19e6e72012-03-14 21:18:39 +00001929static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001930{
Mengdong Lin1a497982015-11-18 02:34:11 -05001931 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001932 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001933 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001934
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001935 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001936 for_each_card_prelinks(card, i, dai_link) {
1937 ret = soc_init_dai_link(card, dai_link);
1938 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001939 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1940 dai_link->name, ret);
1941 mutex_unlock(&client_mutex);
1942 return ret;
1943 }
1944 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001945 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001946
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001947 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1948 card->dapm.dev = card->dev;
1949 card->dapm.card = card;
1950 list_add(&card->dapm.list, &card->dapm_list);
1951
Liam Girdwooda655de82018-07-02 16:59:54 +01001952 /* check whether any platform is ignore machine FE and using topology */
1953 soc_check_tplg_fes(card);
1954
Mark Brownb19e6e72012-03-14 21:18:39 +00001955 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001956 for_each_card_prelinks(card, i, dai_link) {
1957 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001958 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001959 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 }
1961
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001962 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001963 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001964 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001965 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001966 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001967 }
1968
Mengdong Linf8f80362015-12-02 14:11:22 +08001969 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001970 for_each_card_prelinks(card, i, dai_link)
1971 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001972
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001974 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975 card->owner, 0, &card->snd_card);
1976 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001977 dev_err(card->dev,
1978 "ASoC: can't create sound card for card %s: %d\n",
1979 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001980 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001981 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001983 soc_init_card_debugfs(card);
1984
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001985#ifdef CONFIG_DEBUG_FS
1986 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1987#endif
1988
Mark Brown88ee1c62011-02-02 10:43:26 +00001989#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001990 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001991 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001992#endif
Andy Green6ed25972008-06-13 16:24:05 +01001993
Mark Brown9a841eb2011-04-12 17:51:37 -07001994 if (card->dapm_widgets)
1995 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1996 card->num_dapm_widgets);
1997
Nicolin Chenf23e8602015-02-14 17:22:49 -08001998 if (card->of_dapm_widgets)
1999 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2000 card->num_of_dapm_widgets);
2001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002 /* initialise the sound card only once */
2003 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002004 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002005 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002006 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007 }
2008
Stephen Warren62ae68f2012-06-08 12:34:23 -06002009 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002010 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002011 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002012 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002013 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002014 dev_err(card->dev,
2015 "ASoC: failed to instantiate card %d\n",
2016 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002017 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002018 }
2019 }
2020 }
2021
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002022 /* probe auxiliary components */
2023 ret = soc_probe_aux_devices(card);
2024 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002025 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002026
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002027 /*
2028 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002029 * Components with topology may bring new DAIs and DAI links.
2030 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002031 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002032 if (soc_is_dai_link_bound(card, dai_link))
2033 continue;
2034
2035 ret = soc_init_dai_link(card, dai_link);
2036 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002037 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002038 ret = soc_bind_dai_link(card, dai_link);
2039 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002040 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002041 }
2042
Stephen Warren62ae68f2012-06-08 12:34:23 -06002043 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002044 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002045 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002046 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002047 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002048 dev_err(card->dev,
2049 "ASoC: failed to instantiate card %d\n",
2050 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002051 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002052 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002053 }
2054 }
2055
Mark Brown888df392012-02-16 19:37:51 -08002056 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002057 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002058
Mark Brownb7af1da2011-04-07 19:18:44 +09002059 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002060 snd_soc_add_card_controls(card, card->controls,
2061 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002062
Mark Brownb8ad29d2011-03-02 18:35:51 +00002063 if (card->dapm_routes)
2064 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2065 card->num_dapm_routes);
2066
Nicolin Chenf23e8602015-02-14 17:22:49 -08002067 if (card->of_dapm_routes)
2068 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2069 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002070
Takashi Iwai861886d2017-04-24 08:54:42 +02002071 /* try to set some sane longname if DMI is available */
2072 snd_soc_set_dmi_name(card, NULL);
2073
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002074 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002075 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002076 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2077 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002078 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2079 "%s", card->driver_name ? card->driver_name : card->name);
2080 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2081 switch (card->snd_card->driver[i]) {
2082 case '_':
2083 case '-':
2084 case '\0':
2085 break;
2086 default:
2087 if (!isalnum(card->snd_card->driver[i]))
2088 card->snd_card->driver[i] = '_';
2089 break;
2090 }
2091 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002092
Mark Brown28e9ad92011-03-02 18:36:34 +00002093 if (card->late_probe) {
2094 ret = card->late_probe(card);
2095 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002096 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002097 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002098 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002099 }
2100 }
2101
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002102 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002103
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002104 ret = snd_card_register(card->snd_card);
2105 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002106 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2107 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002108 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002109 }
2110
Mark Brown435c5e22008-12-04 15:32:53 +00002111 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002112 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002113 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002114
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002115probe_end:
2116 if (ret < 0)
2117 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002118
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002119 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002120 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002121
Mark Brownb19e6e72012-03-14 21:18:39 +00002122 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002123}
2124
2125/* probes a new socdev */
2126static int soc_probe(struct platform_device *pdev)
2127{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002128 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002129
Vinod Koul70a7ca32011-01-14 19:22:48 +05302130 /*
2131 * no card, so machine driver should be registering card
2132 * we should not be here in that case so ret error
2133 */
2134 if (!card)
2135 return -EINVAL;
2136
Mark Brownfe4085e2012-03-02 13:07:41 +00002137 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002138 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002139 card->name);
2140
Mark Brown435c5e22008-12-04 15:32:53 +00002141 /* Bodge while we unpick instantiation */
2142 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002143
Mark Brown28d528c2012-08-09 18:45:23 +01002144 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145}
2146
2147/* removes a socdev */
2148static int soc_remove(struct platform_device *pdev)
2149{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002150 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151
Mark Brownc5af3a22008-11-28 13:29:45 +00002152 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153 return 0;
2154}
2155
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002156int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002157{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002158 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002159 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002160
2161 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002162 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002163
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002164 /*
2165 * Flush out pmdown_time work - we actually do want to run it
2166 * now, we're shutting down so no imminent restart.
2167 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002168 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002170 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002171
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002172 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002173 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002174 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002175 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002176 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002177
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002178 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002179 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002180 pinctrl_pm_select_sleep_state(codec_dai->dev);
2181 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002182 }
2183
Mark Brown416356f2009-06-30 19:05:15 +01002184 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002185}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002186EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002187
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002188const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302189 .suspend = snd_soc_suspend,
2190 .resume = snd_soc_resume,
2191 .freeze = snd_soc_suspend,
2192 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002193 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302194 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002195};
Stephen Warrendeb26072011-04-05 19:35:30 -06002196EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002197
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198/* ASoC platform driver */
2199static struct platform_driver soc_driver = {
2200 .driver = {
2201 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002202 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203 },
2204 .probe = soc_probe,
2205 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206};
2207
Mark Brown096e49d2009-07-05 15:12:22 +01002208/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 * snd_soc_cnew - create new control
2210 * @_template: control template
2211 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002212 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002213 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 *
2215 * Create a new mixer control from a template control.
2216 *
2217 * Returns 0 for success, else error.
2218 */
2219struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002220 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222{
2223 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002224 struct snd_kcontrol *kcontrol;
2225 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226
2227 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002228 template.index = 0;
2229
Mark Brownefb7ac32011-03-08 17:23:24 +00002230 if (!long_name)
2231 long_name = template.name;
2232
2233 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002234 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002235 if (!name)
2236 return NULL;
2237
Mark Brownefb7ac32011-03-08 17:23:24 +00002238 template.name = name;
2239 } else {
2240 template.name = long_name;
2241 }
2242
2243 kcontrol = snd_ctl_new1(&template, data);
2244
2245 kfree(name);
2246
2247 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248}
2249EXPORT_SYMBOL_GPL(snd_soc_cnew);
2250
Liam Girdwood022658b2012-02-03 17:43:09 +00002251static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2252 const struct snd_kcontrol_new *controls, int num_controls,
2253 const char *prefix, void *data)
2254{
2255 int err, i;
2256
2257 for (i = 0; i < num_controls; i++) {
2258 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002259
Liam Girdwood022658b2012-02-03 17:43:09 +00002260 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2261 control->name, prefix));
2262 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002263 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2264 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002265 return err;
2266 }
2267 }
2268
2269 return 0;
2270}
2271
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002272struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2273 const char *name)
2274{
2275 struct snd_card *card = soc_card->snd_card;
2276 struct snd_kcontrol *kctl;
2277
2278 if (unlikely(!name))
2279 return NULL;
2280
2281 list_for_each_entry(kctl, &card->controls, list)
2282 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2283 return kctl;
2284 return NULL;
2285}
2286EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2287
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002288/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002289 * snd_soc_add_component_controls - Add an array of controls to a component.
2290 *
2291 * @component: Component to add controls to
2292 * @controls: Array of controls to add
2293 * @num_controls: Number of elements in the array
2294 *
2295 * Return: 0 for success, else error.
2296 */
2297int snd_soc_add_component_controls(struct snd_soc_component *component,
2298 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2299{
2300 struct snd_card *card = component->card->snd_card;
2301
2302 return snd_soc_add_controls(card, component->dev, controls,
2303 num_controls, component->name_prefix, component);
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2306
2307/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002308 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2309 * Convenience function to add a list of controls.
2310 *
2311 * @soc_card: SoC card to add controls to
2312 * @controls: array of controls to add
2313 * @num_controls: number of elements in the array
2314 *
2315 * Return 0 for success, else error.
2316 */
2317int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2318 const struct snd_kcontrol_new *controls, int num_controls)
2319{
2320 struct snd_card *card = soc_card->snd_card;
2321
2322 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2323 NULL, soc_card);
2324}
2325EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2326
2327/**
2328 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2329 * Convienience function to add a list of controls.
2330 *
2331 * @dai: DAI to add controls to
2332 * @controls: array of controls to add
2333 * @num_controls: number of elements in the array
2334 *
2335 * Return 0 for success, else error.
2336 */
2337int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2338 const struct snd_kcontrol_new *controls, int num_controls)
2339{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002340 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002341
2342 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2343 NULL, dai);
2344}
2345EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2346
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002347static int snd_soc_bind_card(struct snd_soc_card *card)
2348{
2349 struct snd_soc_pcm_runtime *rtd;
2350 int ret;
2351
2352 ret = snd_soc_instantiate_card(card);
2353 if (ret != 0)
2354 return ret;
2355
2356 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002357 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002358 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2359 struct snd_soc_dai *codec_dai;
2360 int j;
2361
2362 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2363 if (!codec_dai->active)
2364 pinctrl_pm_select_sleep_state(codec_dai->dev);
2365 }
2366
2367 if (!cpu_dai->active)
2368 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2369 }
2370
2371 return ret;
2372}
2373
Mark Brownc5af3a22008-11-28 13:29:45 +00002374/**
2375 * snd_soc_register_card - Register a card with the ASoC core
2376 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002377 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002378 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002379 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302380int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002381{
2382 if (!card->name || !card->dev)
2383 return -EINVAL;
2384
Mark Browned77cc12011-05-03 18:25:34 +01002385 dev_set_drvdata(card->dev, card);
2386
Stephen Warren111c6412011-01-28 14:26:35 -07002387 snd_soc_initialize_card_lists(card);
2388
Mengdong Linf8f80362015-12-02 14:11:22 +08002389 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002390
Mengdong Lin1a497982015-11-18 02:34:11 -05002391 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002392 card->num_rtd = 0;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002393
Mark Browndb432b42011-10-03 21:06:40 +01002394 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002395 INIT_LIST_HEAD(&card->dobj_list);
Mark Browndee89c42008-11-18 22:11:38 +00002396 card->instantiated = 0;
2397 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002398 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002399 spin_lock_init(&card->dpcm_lock);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002400
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002401 return snd_soc_bind_card(card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002402}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302403EXPORT_SYMBOL_GPL(snd_soc_register_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002404
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002405static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2406{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002407 struct snd_soc_pcm_runtime *rtd;
2408 int order;
2409
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002410 if (card->instantiated) {
2411 card->instantiated = false;
2412 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002413 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002414
2415 /* remove all components used by DAI links on this card */
2416 for_each_comp_order(order) {
2417 for_each_card_rtds(card, rtd) {
2418 soc_remove_link_components(card, rtd, order);
2419 }
2420 }
2421
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002422 soc_cleanup_card_resources(card);
2423 if (!unregister)
2424 list_add(&card->list, &unbind_card_list);
2425 } else {
2426 if (unregister)
2427 list_del(&card->list);
2428 }
2429}
2430
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002431/**
2432 * snd_soc_unregister_card - Unregister a card with the ASoC core
2433 *
Mark Browndee89c42008-11-18 22:11:38 +00002434 * @card: Card to unregister
2435 *
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002436 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302437int snd_soc_unregister_card(struct snd_soc_card *card)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002438{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002439 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002440 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002441 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002442 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002443
2444 return 0;
2445}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302446EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002447
2448/*
2449 * Simplify DAI link configuration by removing ".-1" from device names
2450 * and sanitizing names.
2451 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002452static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002453{
2454 char *found, name[NAME_SIZE];
2455 int id1, id2;
2456
2457 if (dev_name(dev) == NULL)
2458 return NULL;
2459
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002460 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002461
2462 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002463 found = strstr(name, dev->driver->name);
2464 if (found) {
2465 /* get ID */
2466 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2467
2468 /* discard ID from name if ID == -1 */
2469 if (*id == -1)
2470 found[strlen(dev->driver->name)] = '\0';
2471 }
2472
2473 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002474 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002475 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2476 char tmp[NAME_SIZE];
2477
2478 /* create unique ID number from I2C addr and bus */
2479 *id = ((id1 & 0xffff) << 16) + id2;
2480
2481 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002482 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2483 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002484 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002485 } else
2486 *id = 0;
2487 }
2488
2489 return kstrdup(name, GFP_KERNEL);
2490}
2491
2492/*
2493 * Simplify DAI link naming for single devices with multiple DAIs by removing
2494 * any ".-1" and using the DAI name (instead of device name).
2495 */
2496static inline char *fmt_multiple_name(struct device *dev,
2497 struct snd_soc_dai_driver *dai_drv)
2498{
2499 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002500 dev_err(dev,
2501 "ASoC: error - multiple DAI %s registered with no name\n",
2502 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002503 return NULL;
2504 }
2505
2506 return kstrdup(dai_drv->name, GFP_KERNEL);
2507}
2508
2509/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002510 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002511 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002512 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002513 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002514static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002515{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002516 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002517
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002518 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002519 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2520 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002521 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002522 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002523 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002524 }
Mark Brown91151712008-11-30 23:31:24 +00002525}
Mark Brown91151712008-11-30 23:31:24 +00002526
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002527/* Create a DAI and add it to the component's DAI list */
2528static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2529 struct snd_soc_dai_driver *dai_drv,
2530 bool legacy_dai_naming)
2531{
2532 struct device *dev = component->dev;
2533 struct snd_soc_dai *dai;
2534
2535 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2536
2537 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2538 if (dai == NULL)
2539 return NULL;
2540
2541 /*
2542 * Back in the old days when we still had component-less DAIs,
2543 * instead of having a static name, component-less DAIs would
2544 * inherit the name of the parent device so it is possible to
2545 * register multiple instances of the DAI. We still need to keep
2546 * the same naming style even though those DAIs are not
2547 * component-less anymore.
2548 */
2549 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002550 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002551 dai->name = fmt_single_name(dev, &dai->id);
2552 } else {
2553 dai->name = fmt_multiple_name(dev, dai_drv);
2554 if (dai_drv->id)
2555 dai->id = dai_drv->id;
2556 else
2557 dai->id = component->num_dai;
2558 }
2559 if (dai->name == NULL) {
2560 kfree(dai);
2561 return NULL;
2562 }
2563
2564 dai->component = component;
2565 dai->dev = dev;
2566 dai->driver = dai_drv;
2567 if (!dai->driver->ops)
2568 dai->driver->ops = &null_dai_ops;
2569
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002570 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002571 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002572 component->num_dai++;
2573
2574 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2575 return dai;
2576}
2577
Mark Brown91151712008-11-30 23:31:24 +00002578/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002579 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002580 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002581 * @component: The component the DAIs are registered for
2582 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002583 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002584 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002585static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002586 struct snd_soc_dai_driver *dai_drv,
2587 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002588{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002589 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002590 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002591 unsigned int i;
2592 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002593
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002594 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002595
2596 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002597
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002598 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2599 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002600 if (dai == NULL) {
2601 ret = -ENOMEM;
2602 goto err;
2603 }
Mark Brown91151712008-11-30 23:31:24 +00002604 }
2605
2606 return 0;
2607
2608err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002609 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002610
2611 return ret;
2612}
Mark Brown91151712008-11-30 23:31:24 +00002613
Mengdong Lin68003e62015-12-31 16:40:43 +08002614/**
2615 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2616 *
2617 * @component: The component the DAIs are registered for
2618 * @dai_drv: DAI driver to use for the DAI
2619 *
2620 * Topology can use this API to register DAIs when probing a component.
2621 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2622 * will be freed in the component cleanup.
2623 */
2624int snd_soc_register_dai(struct snd_soc_component *component,
2625 struct snd_soc_dai_driver *dai_drv)
2626{
2627 struct snd_soc_dapm_context *dapm =
2628 snd_soc_component_get_dapm(component);
2629 struct snd_soc_dai *dai;
2630 int ret;
2631
2632 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2633 dev_err(component->dev, "Invalid dai type %d\n",
2634 dai_drv->dobj.type);
2635 return -EINVAL;
2636 }
2637
2638 lockdep_assert_held(&client_mutex);
2639 dai = soc_add_dai(component, dai_drv, false);
2640 if (!dai)
2641 return -ENOMEM;
2642
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002643 /*
2644 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002645 * also add routes that need these widgets as source or sink.
2646 */
2647 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2648 if (ret != 0) {
2649 dev_err(component->dev,
2650 "Failed to create DAI widgets %d\n", ret);
2651 }
2652
2653 return ret;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2656
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002657static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2658 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002659{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002660 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002661
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002662 component->driver->seq_notifier(component, type, subseq);
2663}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002664
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002665static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2666 int event)
2667{
2668 struct snd_soc_component *component = dapm->component;
2669
2670 return component->driver->stream_event(component, event);
2671}
2672
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002673static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2674 enum snd_soc_bias_level level)
2675{
2676 struct snd_soc_component *component = dapm->component;
2677
2678 return component->driver->set_bias_level(component, level);
2679}
2680
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002681static int snd_soc_component_initialize(struct snd_soc_component *component,
2682 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002683{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002684 struct snd_soc_dapm_context *dapm;
2685
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002686 component->name = fmt_single_name(dev, &component->id);
2687 if (!component->name) {
2688 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002689 return -ENOMEM;
2690 }
2691
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002692 component->dev = dev;
2693 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002694
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002695 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002696 dapm->dev = dev;
2697 dapm->component = component;
2698 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002699 dapm->idle_bias_off = !driver->idle_bias_on;
2700 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002701 if (driver->seq_notifier)
2702 dapm->seq_notifier = snd_soc_component_seq_notifier;
2703 if (driver->stream_event)
2704 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002705 if (driver->set_bias_level)
2706 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002707
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002708 INIT_LIST_HEAD(&component->dai_list);
2709 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002710
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002711 return 0;
2712}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002713
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002714static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002715{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002716 int val_bytes = regmap_get_val_bytes(component->regmap);
2717
2718 /* Errors are legitimate for non-integer byte multiples */
2719 if (val_bytes > 0)
2720 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002721}
2722
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002723#ifdef CONFIG_REGMAP
2724
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002725/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002726 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2727 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002728 * @component: The component for which to initialize the regmap instance
2729 * @regmap: The regmap instance that should be used by the component
2730 *
2731 * This function allows deferred assignment of the regmap instance that is
2732 * associated with the component. Only use this if the regmap instance is not
2733 * yet ready when the component is registered. The function must also be called
2734 * before the first IO attempt of the component.
2735 */
2736void snd_soc_component_init_regmap(struct snd_soc_component *component,
2737 struct regmap *regmap)
2738{
2739 component->regmap = regmap;
2740 snd_soc_component_setup_regmap(component);
2741}
2742EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2743
2744/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002745 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2746 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002747 * @component: The component for which to de-initialize the regmap instance
2748 *
2749 * Calls regmap_exit() on the regmap instance associated to the component and
2750 * removes the regmap instance from the component.
2751 *
2752 * This function should only be used if snd_soc_component_init_regmap() was used
2753 * to initialize the regmap instance.
2754 */
2755void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2756{
2757 regmap_exit(component->regmap);
2758 component->regmap = NULL;
2759}
2760EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2761
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002762#endif
2763
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002764static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002765{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002766 mutex_lock(&client_mutex);
2767
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002768 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002769 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002770 component->regmap = dev_get_regmap(component->dev,
2771 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002772 if (component->regmap)
2773 snd_soc_component_setup_regmap(component);
2774 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002775
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002776 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002777 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002778 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002779
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002780 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002781}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002782
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002783static void snd_soc_component_cleanup(struct snd_soc_component *component)
2784{
2785 snd_soc_unregister_dais(component);
2786 kfree(component->name);
2787}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002788
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002789static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2790{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002791 struct snd_soc_card *card = component->card;
2792
2793 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002794 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002795
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002796 list_del(&component->list);
2797}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002798
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002799#define ENDIANNESS_MAP(name) \
2800 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2801static u64 endianness_format_map[] = {
2802 ENDIANNESS_MAP(S16_),
2803 ENDIANNESS_MAP(U16_),
2804 ENDIANNESS_MAP(S24_),
2805 ENDIANNESS_MAP(U24_),
2806 ENDIANNESS_MAP(S32_),
2807 ENDIANNESS_MAP(U32_),
2808 ENDIANNESS_MAP(S24_3),
2809 ENDIANNESS_MAP(U24_3),
2810 ENDIANNESS_MAP(S20_3),
2811 ENDIANNESS_MAP(U20_3),
2812 ENDIANNESS_MAP(S18_3),
2813 ENDIANNESS_MAP(U18_3),
2814 ENDIANNESS_MAP(FLOAT_),
2815 ENDIANNESS_MAP(FLOAT64_),
2816 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2817};
2818
2819/*
2820 * Fix up the DAI formats for endianness: codecs don't actually see
2821 * the endianness of the data but we're using the CPU format
2822 * definitions which do need to include endianness so we ensure that
2823 * codec DAIs always have both big and little endian variants set.
2824 */
2825static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2826{
2827 int i;
2828
2829 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2830 if (stream->formats & endianness_format_map[i])
2831 stream->formats |= endianness_format_map[i];
2832}
2833
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002834static void snd_soc_try_rebind_card(void)
2835{
2836 struct snd_soc_card *card, *c;
2837
2838 if (!list_empty(&unbind_card_list)) {
2839 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2840 if (!snd_soc_bind_card(card))
2841 list_del(&card->list);
2842 }
2843 }
2844}
2845
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002846int snd_soc_add_component(struct device *dev,
2847 struct snd_soc_component *component,
2848 const struct snd_soc_component_driver *component_driver,
2849 struct snd_soc_dai_driver *dai_drv,
2850 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002851{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002852 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002853 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002854
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002855 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002856 if (ret)
2857 goto err_free;
2858
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002859 if (component_driver->endianness) {
2860 for (i = 0; i < num_dai; i++) {
2861 convert_endianness_formats(&dai_drv[i].playback);
2862 convert_endianness_formats(&dai_drv[i].capture);
2863 }
2864 }
2865
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002866 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002867 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002868 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002869 goto err_cleanup;
2870 }
2871
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002872 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002873 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002874
2875 return 0;
2876
2877err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002878 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002879err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002880 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002881}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002882EXPORT_SYMBOL_GPL(snd_soc_add_component);
2883
2884int snd_soc_register_component(struct device *dev,
2885 const struct snd_soc_component_driver *component_driver,
2886 struct snd_soc_dai_driver *dai_drv,
2887 int num_dai)
2888{
2889 struct snd_soc_component *component;
2890
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002891 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002892 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002893 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002894
2895 return snd_soc_add_component(dev, component, component_driver,
2896 dai_drv, num_dai);
2897}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002898EXPORT_SYMBOL_GPL(snd_soc_register_component);
2899
2900/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002901 * snd_soc_unregister_component - Unregister all related component
2902 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002903 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002904 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002905 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002906static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002907{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002908 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002909 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002910
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002911 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002912 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002913 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002914 continue;
2915
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002916 snd_soc_tplg_component_remove(component,
2917 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002918 snd_soc_component_del_unlocked(component);
2919 found = 1;
2920 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002921 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002922 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002923
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002924 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00002925 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002926
2927 return found;
2928}
2929
2930void snd_soc_unregister_component(struct device *dev)
2931{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002932 while (__snd_soc_unregister_component(dev))
2933 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002934}
2935EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2936
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002937struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
2938 const char *driver_name)
2939{
2940 struct snd_soc_component *component;
2941 struct snd_soc_component *ret;
2942
2943 ret = NULL;
2944 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002945 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00002946 if (dev != component->dev)
2947 continue;
2948
2949 if (driver_name &&
2950 (driver_name != component->driver->name) &&
2951 (strcmp(component->driver->name, driver_name) != 0))
2952 continue;
2953
2954 ret = component;
2955 break;
2956 }
2957 mutex_unlock(&client_mutex);
2958
2959 return ret;
2960}
2961EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
2962
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002963/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002964int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2965 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002966{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002967 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002968 int ret;
2969
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302970 if (!card->dev) {
2971 pr_err("card->dev is not set before calling %s\n", __func__);
2972 return -EINVAL;
2973 }
2974
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002975 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302976
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002977 ret = of_property_read_string_index(np, propname, 0, &card->name);
2978 /*
2979 * EINVAL means the property does not exist. This is fine providing
2980 * card->name was previously set, which is checked later in
2981 * snd_soc_register_card.
2982 */
2983 if (ret < 0 && ret != -EINVAL) {
2984 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002985 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002986 propname, ret);
2987 return ret;
2988 }
2989
2990 return 0;
2991}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002992EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002993
Xiubo Li9a6d4862014-02-08 15:59:52 +08002994static const struct snd_soc_dapm_widget simple_widgets[] = {
2995 SND_SOC_DAPM_MIC("Microphone", NULL),
2996 SND_SOC_DAPM_LINE("Line", NULL),
2997 SND_SOC_DAPM_HP("Headphone", NULL),
2998 SND_SOC_DAPM_SPK("Speaker", NULL),
2999};
3000
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003001int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003002 const char *propname)
3003{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003004 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003005 struct snd_soc_dapm_widget *widgets;
3006 const char *template, *wname;
3007 int i, j, num_widgets, ret;
3008
3009 num_widgets = of_property_count_strings(np, propname);
3010 if (num_widgets < 0) {
3011 dev_err(card->dev,
3012 "ASoC: Property '%s' does not exist\n", propname);
3013 return -EINVAL;
3014 }
3015 if (num_widgets & 1) {
3016 dev_err(card->dev,
3017 "ASoC: Property '%s' length is not even\n", propname);
3018 return -EINVAL;
3019 }
3020
3021 num_widgets /= 2;
3022 if (!num_widgets) {
3023 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3024 propname);
3025 return -EINVAL;
3026 }
3027
3028 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3029 GFP_KERNEL);
3030 if (!widgets) {
3031 dev_err(card->dev,
3032 "ASoC: Could not allocate memory for widgets\n");
3033 return -ENOMEM;
3034 }
3035
3036 for (i = 0; i < num_widgets; i++) {
3037 ret = of_property_read_string_index(np, propname,
3038 2 * i, &template);
3039 if (ret) {
3040 dev_err(card->dev,
3041 "ASoC: Property '%s' index %d read error:%d\n",
3042 propname, 2 * i, ret);
3043 return -EINVAL;
3044 }
3045
3046 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3047 if (!strncmp(template, simple_widgets[j].name,
3048 strlen(simple_widgets[j].name))) {
3049 widgets[i] = simple_widgets[j];
3050 break;
3051 }
3052 }
3053
3054 if (j >= ARRAY_SIZE(simple_widgets)) {
3055 dev_err(card->dev,
3056 "ASoC: DAPM widget '%s' is not supported\n",
3057 template);
3058 return -EINVAL;
3059 }
3060
3061 ret = of_property_read_string_index(np, propname,
3062 (2 * i) + 1,
3063 &wname);
3064 if (ret) {
3065 dev_err(card->dev,
3066 "ASoC: Property '%s' index %d read error:%d\n",
3067 propname, (2 * i) + 1, ret);
3068 return -EINVAL;
3069 }
3070
3071 widgets[i].name = wname;
3072 }
3073
Nicolin Chenf23e8602015-02-14 17:22:49 -08003074 card->of_dapm_widgets = widgets;
3075 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003076
3077 return 0;
3078}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003079EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003080
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003081int snd_soc_of_get_slot_mask(struct device_node *np,
3082 const char *prop_name,
3083 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003084{
3085 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003086 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003087 int i;
3088
3089 if (!of_slot_mask)
3090 return 0;
3091 val /= sizeof(u32);
3092 for (i = 0; i < val; i++)
3093 if (be32_to_cpup(&of_slot_mask[i]))
3094 *mask |= (1 << i);
3095
3096 return val;
3097}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003098EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003099
Xiubo Li89c67852014-02-14 09:34:35 +08003100int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003101 unsigned int *tx_mask,
3102 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003103 unsigned int *slots,
3104 unsigned int *slot_width)
3105{
3106 u32 val;
3107 int ret;
3108
Jyri Sarha61310842015-09-09 21:27:43 +03003109 if (tx_mask)
3110 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3111 if (rx_mask)
3112 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3113
Xiubo Li89c67852014-02-14 09:34:35 +08003114 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3115 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3116 if (ret)
3117 return ret;
3118
3119 if (slots)
3120 *slots = val;
3121 }
3122
3123 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3124 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3125 if (ret)
3126 return ret;
3127
3128 if (slot_width)
3129 *slot_width = val;
3130 }
3131
3132 return 0;
3133}
3134EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3135
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003136void snd_soc_of_parse_node_prefix(struct device_node *np,
3137 struct snd_soc_codec_conf *codec_conf,
3138 struct device_node *of_node,
3139 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003140{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003141 const char *str;
3142 int ret;
3143
3144 ret = of_property_read_string(np, propname, &str);
3145 if (ret < 0) {
3146 /* no prefix is not error */
3147 return;
3148 }
3149
3150 codec_conf->of_node = of_node;
3151 codec_conf->name_prefix = str;
3152}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003153EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003154
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003155int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003156 const char *propname)
3157{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003158 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003159 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003160 struct snd_soc_dapm_route *routes;
3161 int i, ret;
3162
3163 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003164 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003165 dev_err(card->dev,
3166 "ASoC: Property '%s' does not exist or its length is not even\n",
3167 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003168 return -EINVAL;
3169 }
3170 num_routes /= 2;
3171 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003172 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003173 propname);
3174 return -EINVAL;
3175 }
3176
Kees Cooka86854d2018-06-12 14:07:58 -07003177 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003178 GFP_KERNEL);
3179 if (!routes) {
3180 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003181 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003182 return -EINVAL;
3183 }
3184
3185 for (i = 0; i < num_routes; i++) {
3186 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003187 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003188 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003189 dev_err(card->dev,
3190 "ASoC: Property '%s' index %d could not be read: %d\n",
3191 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003192 return -EINVAL;
3193 }
3194 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003195 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003196 if (ret) {
3197 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003198 "ASoC: Property '%s' index %d could not be read: %d\n",
3199 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003200 return -EINVAL;
3201 }
3202 }
3203
Nicolin Chenf23e8602015-02-14 17:22:49 -08003204 card->num_of_dapm_routes = num_routes;
3205 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003206
3207 return 0;
3208}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003209EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003210
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003211unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003212 const char *prefix,
3213 struct device_node **bitclkmaster,
3214 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003215{
3216 int ret, i;
3217 char prop[128];
3218 unsigned int format = 0;
3219 int bit, frame;
3220 const char *str;
3221 struct {
3222 char *name;
3223 unsigned int val;
3224 } of_fmt_table[] = {
3225 { "i2s", SND_SOC_DAIFMT_I2S },
3226 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3227 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3228 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3229 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3230 { "ac97", SND_SOC_DAIFMT_AC97 },
3231 { "pdm", SND_SOC_DAIFMT_PDM},
3232 { "msb", SND_SOC_DAIFMT_MSB },
3233 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003234 };
3235
3236 if (!prefix)
3237 prefix = "";
3238
3239 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003240 * check "dai-format = xxx"
3241 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003242 * SND_SOC_DAIFMT_FORMAT_MASK area
3243 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003244 ret = of_property_read_string(np, "dai-format", &str);
3245 if (ret < 0) {
3246 snprintf(prop, sizeof(prop), "%sformat", prefix);
3247 ret = of_property_read_string(np, prop, &str);
3248 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003249 if (ret == 0) {
3250 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3251 if (strcmp(str, of_fmt_table[i].name) == 0) {
3252 format |= of_fmt_table[i].val;
3253 break;
3254 }
3255 }
3256 }
3257
3258 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003259 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003260 * SND_SOC_DAIFMT_CLOCK_MASK area
3261 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003262 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003263 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003264 format |= SND_SOC_DAIFMT_CONT;
3265 else
3266 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003267
3268 /*
3269 * check "[prefix]bitclock-inversion"
3270 * check "[prefix]frame-inversion"
3271 * SND_SOC_DAIFMT_INV_MASK area
3272 */
3273 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3274 bit = !!of_get_property(np, prop, NULL);
3275
3276 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3277 frame = !!of_get_property(np, prop, NULL);
3278
3279 switch ((bit << 4) + frame) {
3280 case 0x11:
3281 format |= SND_SOC_DAIFMT_IB_IF;
3282 break;
3283 case 0x10:
3284 format |= SND_SOC_DAIFMT_IB_NF;
3285 break;
3286 case 0x01:
3287 format |= SND_SOC_DAIFMT_NB_IF;
3288 break;
3289 default:
3290 /* SND_SOC_DAIFMT_NB_NF is default */
3291 break;
3292 }
3293
3294 /*
3295 * check "[prefix]bitclock-master"
3296 * check "[prefix]frame-master"
3297 * SND_SOC_DAIFMT_MASTER_MASK area
3298 */
3299 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3300 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003301 if (bit && bitclkmaster)
3302 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003303
3304 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3305 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003306 if (frame && framemaster)
3307 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003308
3309 switch ((bit << 4) + frame) {
3310 case 0x11:
3311 format |= SND_SOC_DAIFMT_CBM_CFM;
3312 break;
3313 case 0x10:
3314 format |= SND_SOC_DAIFMT_CBM_CFS;
3315 break;
3316 case 0x01:
3317 format |= SND_SOC_DAIFMT_CBS_CFM;
3318 break;
3319 default:
3320 format |= SND_SOC_DAIFMT_CBS_CFS;
3321 break;
3322 }
3323
3324 return format;
3325}
3326EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3327
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003328int snd_soc_get_dai_id(struct device_node *ep)
3329{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003330 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003331 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003332 int ret;
3333
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003334 dlc.of_node = of_graph_get_port_parent(ep);
3335 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003336 /*
3337 * For example HDMI case, HDMI has video/sound port,
3338 * but ALSA SoC needs sound port number only.
3339 * Thus counting HDMI DT port/endpoint doesn't work.
3340 * Then, it should have .of_xlate_dai_id
3341 */
3342 ret = -ENOTSUPP;
3343 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003344 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003345 if (component &&
3346 component->driver->of_xlate_dai_id)
3347 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003348 mutex_unlock(&client_mutex);
3349
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003350 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003351
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003352 return ret;
3353}
3354EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3355
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003356int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003357 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003358{
3359 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003360 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003361 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003362
3363 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003364 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003365 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003366
3367 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003368 continue;
3369
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003370 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003371 ret = pos->driver->of_xlate_dai_name(pos,
3372 args,
3373 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003374 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003375 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003376 int id = -1;
3377
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003378 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003379 case 0:
3380 id = 0; /* same as dai_drv[0] */
3381 break;
3382 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003383 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003384 break;
3385 default:
3386 /* not supported */
3387 break;
3388 }
3389
3390 if (id < 0 || id >= pos->num_dai) {
3391 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003392 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003393 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003394
3395 ret = 0;
3396
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003397 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003398 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003399 if (id == 0)
3400 break;
3401 id--;
3402 }
3403
3404 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003405 if (!*dai_name)
3406 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003407 }
3408
Kuninori Morimotocb470082013-09-10 17:39:56 -07003409 break;
3410 }
3411 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003412 return ret;
3413}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003414EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003415
3416int snd_soc_of_get_dai_name(struct device_node *of_node,
3417 const char **dai_name)
3418{
3419 struct of_phandle_args args;
3420 int ret;
3421
3422 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3423 "#sound-dai-cells", 0, &args);
3424 if (ret)
3425 return ret;
3426
3427 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003428
3429 of_node_put(args.np);
3430
3431 return ret;
3432}
3433EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3434
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003435/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003436 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3437 * @dai_link: DAI link
3438 *
3439 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3440 */
3441void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3442{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003443 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003444 int index;
3445
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003446 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003447 if (!component->of_node)
3448 break;
3449 of_node_put(component->of_node);
3450 component->of_node = NULL;
3451 }
3452}
3453EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3454
3455/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003456 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3457 * @dev: Card device
3458 * @of_node: Device node
3459 * @dai_link: DAI link
3460 *
3461 * Builds an array of CODEC DAI components from the DAI link property
3462 * 'sound-dai'.
3463 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003464 * The device nodes in the array (of_node) must be dereferenced by calling
3465 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003466 *
3467 * Returns 0 for success
3468 */
3469int snd_soc_of_get_dai_link_codecs(struct device *dev,
3470 struct device_node *of_node,
3471 struct snd_soc_dai_link *dai_link)
3472{
3473 struct of_phandle_args args;
3474 struct snd_soc_dai_link_component *component;
3475 char *name;
3476 int index, num_codecs, ret;
3477
3478 /* Count the number of CODECs */
3479 name = "sound-dai";
3480 num_codecs = of_count_phandle_with_args(of_node, name,
3481 "#sound-dai-cells");
3482 if (num_codecs <= 0) {
3483 if (num_codecs == -ENOENT)
3484 dev_err(dev, "No 'sound-dai' property\n");
3485 else
3486 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3487 return num_codecs;
3488 }
Kees Cooka86854d2018-06-12 14:07:58 -07003489 component = devm_kcalloc(dev,
3490 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003491 GFP_KERNEL);
3492 if (!component)
3493 return -ENOMEM;
3494 dai_link->codecs = component;
3495 dai_link->num_codecs = num_codecs;
3496
3497 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003498 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003499 ret = of_parse_phandle_with_args(of_node, name,
3500 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003501 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003502 if (ret)
3503 goto err;
3504 component->of_node = args.np;
3505 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3506 if (ret < 0)
3507 goto err;
3508 }
3509 return 0;
3510err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003511 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003512 dai_link->codecs = NULL;
3513 dai_link->num_codecs = 0;
3514 return ret;
3515}
3516EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3517
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003518static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003519{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003520 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003521 snd_soc_util_init();
3522
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003523 return platform_driver_register(&soc_driver);
3524}
Mark Brown4abe8e12010-10-12 17:41:03 +01003525module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003526
Mark Brown7d8c16a2008-11-30 22:11:24 +00003527static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003528{
Mark Brownfb257892011-04-28 10:57:54 +01003529 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003530 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003531
Mark Brown3ff3f642008-05-19 12:32:25 +02003532 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003533}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003534module_exit(snd_soc_exit);
3535
3536/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003537MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003538MODULE_DESCRIPTION("ALSA SoC Core");
3539MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003540MODULE_ALIAS("platform:soc-audio");