blob: 8c63d32ab2fe593eadfb1e47988272cd6a534ad9 [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/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020069/*
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080072 * as invalid and dropped when setting the card long name from DMI info.
73 */
74static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
76 "TBD by OEM",
77 "Default String",
78 "Board Manufacturer",
79 "Board Vendor Name",
80 "Board Product Name",
81 NULL, /* terminator */
82};
83
Mark Browndbe21402010-02-12 11:37:24 +000084static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
Mark Brown36ae1a92012-01-06 17:12:45 -080087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000090}
91
92static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070097 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000098
Jingoo Hanb785a492013-07-19 16:24:59 +090099 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700100 if (ret)
101 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000102
103 return count;
104}
105
106static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 &dev_attr_pmdown_time.attr,
110 NULL
111};
112
113static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
115{
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100122}
123
124static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
Mark Brownf7e73b262018-03-09 12:46:27 +0000129static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
132};
133
134static const struct attribute_group *soc_dev_attr_groups[] = {
135 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000136 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 NULL
138};
139
Mark Brown2624d5f2009-11-03 21:56:13 +0000140#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200143 if (!component->card->debugfs_card_root)
144 return;
145
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200146 if (component->debugfs_prefix) {
147 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100148
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
151 if (name) {
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
154 kfree(name);
155 }
156 } else {
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
159 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 return;
165 }
166
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169}
170
171static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
172{
173 debugfs_remove_recursive(component->debugfs_root);
174}
175
Peng Donglinc15b2a12018-02-14 22:48:07 +0800176static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100177{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100178 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100179 struct snd_soc_dai *dai;
180
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100181 mutex_lock(&client_mutex);
182
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000183 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000184 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800185 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100186
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100187 mutex_unlock(&client_mutex);
188
Donglin Peng700c17c2018-01-18 13:31:26 +0800189 return 0;
190}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800191DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100192
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000193static int component_list_show(struct seq_file *m, void *v)
194{
195 struct snd_soc_component *component;
196
197 mutex_lock(&client_mutex);
198
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000199 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200 seq_printf(m, "%s\n", component->name);
201
202 mutex_unlock(&client_mutex);
203
204 return 0;
205}
206DEFINE_SHOW_ATTRIBUTE(component_list);
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208static void soc_init_card_debugfs(struct snd_soc_card *card)
209{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200210 if (!snd_soc_debugfs_root)
211 return;
212
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000214 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200215 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100217 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200218 return;
219 }
220
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
223 &card->pop_time);
224 if (!card->debugfs_pop_time)
225 dev_warn(card->dev,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200226 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200227}
228
229static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230{
231 debugfs_remove_recursive(card->debugfs_card_root);
232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
240 return;
241 }
242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
244 &dai_list_fops))
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000246
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250}
251
252static void snd_soc_debugfs_exit(void)
253{
254 debugfs_remove_recursive(snd_soc_debugfs_root);
255}
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257#else
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261{
262}
263
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200264static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000266{
267}
268
Axel Linb95fccb2010-11-09 17:06:44 +0800269static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270{
271}
272
273static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274{
275}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200276
277static inline void snd_soc_debugfs_init(void)
278{
279}
280
281static inline void snd_soc_debugfs_exit(void)
282{
283}
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285#endif
286
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000287static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
289{
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
292
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
296 return 0;
297 }
298
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300 if (!new_rtdcom)
301 return -ENOMEM;
302
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
305
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
307
308 return 0;
309}
310
311static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312{
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316 kfree(rtdcom1);
317
318 INIT_LIST_HEAD(&rtd->component_list);
319}
320
321struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
323{
324 struct snd_soc_rtdcom_list *rtdcom;
325
Kuninori Morimoto971da242018-01-23 00:41:24 +0000326 if (!driver_name)
327 return NULL;
328
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000329 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000330 const char *component_name = rtdcom->component->driver->name;
331
332 if (!component_name)
333 continue;
334
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000337 return rtdcom->component;
338 }
339
340 return NULL;
341}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000342EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100344struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
346{
Mengdong Lin1a497982015-11-18 02:34:11 -0500347 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000349 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100353 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100355 return NULL;
356}
357EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
358
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000359static const struct snd_soc_ops null_snd_soc_ops;
360
Mengdong Lin1a497982015-11-18 02:34:11 -0500361static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
363{
364 struct snd_soc_pcm_runtime *rtd;
365
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
367 if (!rtd)
368 return NULL;
369
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000370 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500371 rtd->card = card;
372 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
375
Kees Cook6396bb22018-06-12 14:03:40 -0700376 rtd->codec_dais = kcalloc(dai_link->num_codecs,
377 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500378 GFP_KERNEL);
379 if (!rtd->codec_dais) {
380 kfree(rtd);
381 return NULL;
382 }
383
384 return rtd;
385}
386
387static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
388{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000389 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000390 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500391 kfree(rtd);
392}
393
394static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
396{
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
399 card->num_rtd++;
400}
401
402static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
403{
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
405
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000406 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
409 }
410
411 card->num_rtd = 0;
412}
413
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100414struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
416{
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100418
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000419 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500420 if (!strcmp(rtd->dai_link->name, dai_link))
421 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100422 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424 return NULL;
425}
426EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
427
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900428static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
429{
430 struct snd_soc_pcm_runtime *rtd;
431
432 for_each_card_rtds(card, rtd)
433 flush_delayed_work(&rtd->delayed_work);
434}
435
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100436static void codec2codec_close_delayed_work(struct work_struct *work)
437{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200438 /*
439 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100440 * Since c2c links are internal nodes in the DAPM graph and
441 * don't interface with the outside world or application layer
442 * we don't have to do any special handling on close.
443 */
444}
445
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000446#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000448int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000450 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000451 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500452 struct snd_soc_pcm_runtime *rtd;
453 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200455 /* If the card is not initialized yet there is nothing to do */
456 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200457 return 0;
458
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200459 /*
460 * Due to the resume being scheduled into a workqueue we could
461 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100462 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100464
465 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100467
Mark Browna00f90f2010-12-02 16:24:24 +0000468 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000469 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000470 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100471
Mengdong Lin1a497982015-11-18 02:34:11 -0500472 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100473 continue;
474
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000475 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200476 struct snd_soc_dai_driver *drv = dai->driver;
477
478 if (drv->ops->digital_mute && dai->playback_active)
479 drv->ops->digital_mute(dai, 1);
480 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 }
482
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100483 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000484 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500485 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100486 continue;
487
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100489 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100490
Mark Brown87506542008-11-18 20:50:34 +0000491 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000492 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000494 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100496
Mengdong Lin1a497982015-11-18 02:34:11 -0500497 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100498 continue;
499
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100500 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100502 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200504 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900505 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100506
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000507 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100510 continue;
511
Mengdong Lin1a497982015-11-18 02:34:11 -0500512 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800513 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800514 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515
Mengdong Lin1a497982015-11-18 02:34:11 -0500516 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800518 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 }
520
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200521 /* Recheck all endpoints too, their state is affected by suspend */
522 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700523 snd_soc_dapm_sync(&card->dapm);
524
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000525 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000526 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200527 struct snd_soc_dapm_context *dapm =
528 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000529
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200530 /*
531 * If there are paths active then the COMPONENT will be held
532 * with bias _ON and should not be suspended.
533 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000534 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200535 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000537 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000538 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000539 * bias off then being in STANDBY
540 * means it's doing something,
541 * otherwise fall through.
542 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200543 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000544 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200545 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000546 break;
547 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500548 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000551 if (component->driver->suspend)
552 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000553 component->suspended = 1;
554 if (component->regmap)
555 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800556 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000557 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 break;
559 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000560 dev_dbg(component->dev,
561 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 break;
563 }
564 }
565 }
566
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000567 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569
Mengdong Lin1a497982015-11-18 02:34:11 -0500570 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 continue;
572
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100573 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800575
576 /* deactivate pins to sleep state */
577 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578 }
579
Mark Brown87506542008-11-18 20:50:34 +0000580 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000581 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
583 return 0;
584}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000585EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200587/*
588 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100589 * setting our codec back up, which can be very slow on I2C
590 */
591static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000593 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200594 container_of(work, struct snd_soc_card,
595 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500596 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000597 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500598 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200600 /*
601 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100602 * so userspace apps are blocked from touching us
603 */
604
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000605 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100606
Mark Brown9949788b2010-05-07 20:24:05 +0100607 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100609
Mark Brown87506542008-11-18 20:50:34 +0000610 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000611 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100613 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000614 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500615 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100616
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100618 continue;
619
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100620 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622 }
623
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000624 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000625 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000626 if (component->driver->resume)
627 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000628 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100629 }
630 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000632 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100633
Mengdong Lin1a497982015-11-18 02:34:11 -0500634 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100635 continue;
636
Mengdong Lin1a497982015-11-18 02:34:11 -0500637 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000638 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800639 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640
Mengdong Lin1a497982015-11-18 02:34:11 -0500641 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000642 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800643 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 }
645
Mark Brown3ff3f642008-05-19 12:32:25 +0200646 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000647 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000648 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100649
Mengdong Lin1a497982015-11-18 02:34:11 -0500650 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100651 continue;
652
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000653 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200654 struct snd_soc_dai_driver *drv = dai->driver;
655
656 if (drv->ops->digital_mute && dai->playback_active)
657 drv->ops->digital_mute(dai, 0);
658 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 }
660
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000661 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100665 continue;
666
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100667 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 }
670
Mark Brown87506542008-11-18 20:50:34 +0000671 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000672 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000674 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100675
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200676 /* Recheck all endpoints too, their state is affected by suspend */
677 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700678 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530679
680 /* userspace can access us now we are back as we were before */
681 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100682}
683
684/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000685int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100686{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000687 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100688 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500689 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200690
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200691 /* If the card is not initialized yet there is nothing to do */
692 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800693 return 0;
694
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800695 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000696 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000697 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
699 int j;
700
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800701 if (cpu_dai->active)
702 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000704 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200705 if (codec_dai->active)
706 pinctrl_pm_select_default_state(codec_dai->dev);
707 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800708 }
709
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100710 /*
711 * DAIs that also act as the control bus master might have other drivers
712 * hanging off them so need to resume immediately. Other drivers don't
713 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100714 * due to I/O costs and anti-pop so handle them out of line.
715 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000716 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200718
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100719 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600720 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100721 if (bus_control) {
722 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600723 soc_resume_deferred(&card->deferred_resume_work);
724 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000725 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000727 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100728 }
Andy Green6ed25972008-06-13 16:24:05 +0100729
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 return 0;
731}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000732EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734#define snd_soc_suspend NULL
735#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#endif
737
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100738static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800739};
740
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200741static struct snd_soc_component *soc_find_component(
742 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100743{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200744 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100745
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100746 lockdep_assert_held(&client_mutex);
747
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000748 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200749 if (of_node) {
750 if (component->dev->of_node == of_node)
751 return component;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000752 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200753 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100754 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100755 }
756
757 return NULL;
758}
759
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000760static int snd_soc_is_matching_component(
761 const struct snd_soc_dai_link_component *dlc,
762 struct snd_soc_component *component)
763{
764 struct device_node *component_of_node;
765
766 component_of_node = component->dev->of_node;
767 if (!component_of_node && component->dev->parent)
768 component_of_node = component->dev->parent->of_node;
769
770 if (dlc->of_node && component_of_node != dlc->of_node)
771 return 0;
772 if (dlc->name && strcmp(component->name, dlc->name))
773 return 0;
774
775 return 1;
776}
777
Mengdong Linfbb88b52016-04-22 12:25:33 +0800778/**
779 * snd_soc_find_dai - Find a registered DAI
780 *
Jeffy Chen49584712017-08-22 15:57:21 +0800781 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800782 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700783 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800784 * find the DAI of the same name. The component's of_node and name
785 * should also match if being specified.
786 *
787 * Return: pointer of DAI, or NULL if not found.
788 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800789struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200790 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100791{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200792 struct snd_soc_component *component;
793 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100794
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100795 lockdep_assert_held(&client_mutex);
796
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200797 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000798 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000799 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200800 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000801 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800802 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800803 && (!dai->driver->name
804 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100805 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100806
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200807 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100808 }
809 }
810
811 return NULL;
812}
Mengdong Lin305e9022016-04-19 13:12:25 +0800813EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100814
Mengdong Lin17fb17552016-11-03 01:04:12 +0800815/**
816 * snd_soc_find_dai_link - Find a DAI link
817 *
818 * @card: soc card
819 * @id: DAI link ID to match
820 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000821 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800822 *
823 * This function will search all existing DAI links of the soc card to
824 * find the link of the same ID. Since DAI links may not have their
825 * unique ID, so name and stream name should also match if being
826 * specified.
827 *
828 * Return: pointer of DAI link, or NULL if not found.
829 */
830struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
831 int id, const char *name,
832 const char *stream_name)
833{
834 struct snd_soc_dai_link *link, *_link;
835
836 lockdep_assert_held(&client_mutex);
837
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000838 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800839 if (link->id != id)
840 continue;
841
842 if (name && (!link->name || strcmp(name, link->name)))
843 continue;
844
845 if (stream_name && (!link->stream_name
846 || strcmp(stream_name, link->stream_name)))
847 continue;
848
849 return link;
850 }
851
852 return NULL;
853}
854EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
855
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800856static bool soc_is_dai_link_bound(struct snd_soc_card *card,
857 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800859 struct snd_soc_pcm_runtime *rtd;
860
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000861 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800862 if (rtd->dai_link == dai_link)
863 return true;
864 }
865
866 return false;
867}
868
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500869static int soc_bind_dai_link(struct snd_soc_card *card,
870 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871{
Mengdong Lin1a497982015-11-18 02:34:11 -0500872 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900873 struct snd_soc_dai_link_component *codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200874 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000875 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500876 struct snd_soc_dai **codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200877 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878
Liam Girdwooda655de82018-07-02 16:59:54 +0100879 if (dai_link->ignore)
880 return 0;
881
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500882 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000883
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800884 if (soc_is_dai_link_bound(card, dai_link)) {
885 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
886 dai_link->name);
887 return 0;
888 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530890 rtd = soc_new_pcm_runtime(card, dai_link);
891 if (!rtd)
892 return -ENOMEM;
893
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200894 cpu_dai_component.name = dai_link->cpu_name;
895 cpu_dai_component.of_node = dai_link->cpu_of_node;
896 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
897 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000898 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100899 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
900 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500901 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000902 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000903 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000904
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 rtd->num_codecs = dai_link->num_codecs;
906
907 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500908 codec_dais = rtd->codec_dais;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900909 for_each_link_codecs(dai_link, i, codecs) {
910 codec_dais[i] = snd_soc_find_dai(codecs);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200911 if (!codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100912 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900913 codecs->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500914 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200915 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000916 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100918
Benoit Cousson88bd8702014-07-08 23:19:34 +0200919 /* Single codec links expect codec and codec_dai in runtime data */
920 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100921
Mark Brownb19e6e72012-03-14 21:18:39 +0000922 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000923 for_each_component(component) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900924 if (!snd_soc_is_matching_component(dai_link->platforms,
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000925 component))
926 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000927
928 snd_soc_rtdcom_add(rtd, component);
929 }
930
Mengdong Lin1a497982015-11-18 02:34:11 -0500931 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000932 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500933
934_err_defer:
935 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200936 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000937}
938
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900939static void soc_cleanup_component(struct snd_soc_component *component)
940{
941 list_del(&component->card_list);
942 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
943 soc_cleanup_component_debugfs(component);
944 component->card = NULL;
945 module_put(component->dev->driver->owner);
946}
947
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200948static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600949{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200950 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200951 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600952
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000953 if (component->driver->remove)
954 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600955
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900956 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600957}
958
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200959static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200960{
961 int err;
962
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000963 if (!dai || !dai->probed ||
964 dai->driver->remove_order != order)
965 return;
966
967 if (dai->driver->remove) {
968 err = dai->driver->remove(dai);
969 if (err < 0)
970 dev_err(dai->dev,
971 "ASoC: failed to remove %s: %d\n",
972 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100973 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000974 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100975}
976
Mengdong Lin1a497982015-11-18 02:34:11 -0500977static void soc_remove_link_dais(struct snd_soc_card *card,
978 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200980 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000981 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982
983 /* unregister the rtd device */
984 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800985 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 rtd->dev_registered = 0;
987 }
988
989 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000990 for_each_rtd_codec_dai(rtd, i, codec_dai)
991 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200993 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994}
995
Mengdong Lin1a497982015-11-18 02:34:11 -0500996static void soc_remove_link_components(struct snd_soc_card *card,
997 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200999 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001000 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001002 for_each_rtdcom(rtd, rtdcom) {
1003 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001004
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001005 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001006 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001008}
1009
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001010static void soc_remove_dai_links(struct snd_soc_card *card)
1011{
Mengdong Lin1a497982015-11-18 02:34:11 -05001012 int order;
1013 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001014 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001015
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001016 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001017 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001018 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001019 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001020
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001021 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001022 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001023 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001024 }
1025
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001026 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001027 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1028 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1029 link->name);
1030
1031 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001032 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001033}
1034
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001035static int snd_soc_init_platform(struct snd_soc_card *card,
1036 struct snd_soc_dai_link *dai_link)
1037{
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001038 struct snd_soc_dai_link_component *platform = dai_link->platforms;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001039
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001040 /*
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001041 * REMOVE ME
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001042 *
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001043 * This is glue code for Legacy vs Modern dai_link.
1044 * This function will be removed if all derivers are switched to
1045 * modern style dai_link.
1046 * Driver shouldn't use both legacy and modern style in the same time.
1047 * see
1048 * soc.h :: struct snd_soc_dai_link
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001049 */
1050 /* convert Legacy platform link */
Curtis Malainey78a24e12019-01-29 13:47:09 -08001051 if (!platform) {
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001052 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001053 sizeof(struct snd_soc_dai_link_component),
1054 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001055 if (!platform)
1056 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001057
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001058 dai_link->platforms = platform;
1059 dai_link->num_platforms = 1;
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001060 dai_link->legacy_platform = 1;
1061 platform->name = dai_link->platform_name;
1062 platform->of_node = dai_link->platform_of_node;
1063 platform->dai_name = NULL;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001064 }
1065
1066 /* if there's no platform we match on the empty platform */
1067 if (!platform->name &&
1068 !platform->of_node)
1069 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001070
1071 return 0;
1072}
1073
Curtis Malainey78a24e12019-01-29 13:47:09 -08001074static void soc_cleanup_platform(struct snd_soc_card *card)
1075{
1076 struct snd_soc_dai_link *link;
1077 int i;
1078 /*
1079 * FIXME
1080 *
1081 * this function should be removed with snd_soc_init_platform
1082 */
1083
1084 for_each_card_prelinks(card, i, link) {
1085 if (link->legacy_platform) {
1086 link->legacy_platform = 0;
1087 link->platforms = NULL;
1088 }
1089 }
1090}
1091
Mengdong Lin923c5e612015-11-23 11:01:49 -05001092static int snd_soc_init_multicodec(struct snd_soc_card *card,
1093 struct snd_soc_dai_link *dai_link)
1094{
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001095 /*
1096 * REMOVE ME
1097 *
1098 * This is glue code for Legacy vs Modern dai_link.
1099 * This function will be removed if all derivers are switched to
1100 * modern style dai_link.
1101 * Driver shouldn't use both legacy and modern style in the same time.
1102 * see
1103 * soc.h :: struct snd_soc_dai_link
1104 */
1105
Mengdong Lin923c5e612015-11-23 11:01:49 -05001106 /* Legacy codec/codec_dai link is a single entry in multicodec */
1107 if (dai_link->codec_name || dai_link->codec_of_node ||
1108 dai_link->codec_dai_name) {
1109 dai_link->num_codecs = 1;
1110
1111 dai_link->codecs = devm_kzalloc(card->dev,
1112 sizeof(struct snd_soc_dai_link_component),
1113 GFP_KERNEL);
1114 if (!dai_link->codecs)
1115 return -ENOMEM;
1116
1117 dai_link->codecs[0].name = dai_link->codec_name;
1118 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1119 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1120 }
1121
1122 if (!dai_link->codecs) {
1123 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1124 return -EINVAL;
1125 }
1126
1127 return 0;
1128}
1129
1130static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001131 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001132{
1133 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001134 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001135
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001136 ret = snd_soc_init_platform(card, link);
1137 if (ret) {
1138 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1139 return ret;
1140 }
1141
Mengdong Lin923c5e612015-11-23 11:01:49 -05001142 ret = snd_soc_init_multicodec(card, link);
1143 if (ret) {
1144 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1145 return ret;
1146 }
1147
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001148 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001149 /*
1150 * Codec must be specified by 1 of name or OF node,
1151 * not both or neither.
1152 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001153 if (!!codec->name ==
1154 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001155 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1156 link->name);
1157 return -EINVAL;
1158 }
1159 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001160 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001161 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1162 link->name);
1163 return -EINVAL;
1164 }
1165 }
1166
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001167 /* FIXME */
1168 if (link->num_platforms > 1) {
1169 dev_err(card->dev,
1170 "ASoC: multi platform is not yet supported %s\n",
1171 link->name);
1172 return -EINVAL;
1173 }
1174
Mengdong Lin923c5e612015-11-23 11:01:49 -05001175 /*
1176 * Platform may be specified by either name or OF node, but
1177 * can be left unspecified, and a dummy platform will be used.
1178 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001179 if (link->platforms->name && link->platforms->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001180 dev_err(card->dev,
1181 "ASoC: Both platform name/of_node are set for %s\n",
1182 link->name);
1183 return -EINVAL;
1184 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301185
1186 /*
1187 * Defer card registartion if platform dai component is not added to
1188 * component list.
1189 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001190 if ((link->platforms->of_node || link->platforms->name) &&
1191 !soc_find_component(link->platforms->of_node, link->platforms->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301192 return -EPROBE_DEFER;
1193
Mengdong Lin923c5e612015-11-23 11:01:49 -05001194 /*
1195 * CPU device may be specified by either name or OF node, but
1196 * can be left unspecified, and will be matched based on DAI
1197 * name alone..
1198 */
1199 if (link->cpu_name && link->cpu_of_node) {
1200 dev_err(card->dev,
1201 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1202 link->name);
1203 return -EINVAL;
1204 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301205
1206 /*
1207 * Defer card registartion if cpu dai component is not added to
1208 * component list.
1209 */
Matthias Reichl28335482019-01-15 17:51:07 +01001210 if ((link->cpu_of_node || link->cpu_name) &&
1211 !soc_find_component(link->cpu_of_node, link->cpu_name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301212 return -EPROBE_DEFER;
1213
Mengdong Lin923c5e612015-11-23 11:01:49 -05001214 /*
1215 * At least one of CPU DAI name or CPU device name/node must be
1216 * specified
1217 */
1218 if (!link->cpu_dai_name &&
1219 !(link->cpu_name || link->cpu_of_node)) {
1220 dev_err(card->dev,
1221 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1222 link->name);
1223 return -EINVAL;
1224 }
1225
1226 return 0;
1227}
1228
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001229void snd_soc_disconnect_sync(struct device *dev)
1230{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001231 struct snd_soc_component *component =
1232 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001233
1234 if (!component || !component->card)
1235 return;
1236
1237 snd_card_disconnect_sync(component->card->snd_card);
1238}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001239EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001240
Mengdong Linf8f80362015-12-02 14:11:22 +08001241/**
1242 * snd_soc_add_dai_link - Add a DAI link dynamically
1243 * @card: The ASoC card to which the DAI link is added
1244 * @dai_link: The new DAI link to add
1245 *
1246 * This function adds a DAI link to the ASoC card's link list.
1247 *
1248 * Note: Topology can use this API to add DAI links when probing the
1249 * topology component. And machine drivers can still define static
1250 * DAI links in dai_link array.
1251 */
1252int snd_soc_add_dai_link(struct snd_soc_card *card,
1253 struct snd_soc_dai_link *dai_link)
1254{
1255 if (dai_link->dobj.type
1256 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1257 dev_err(card->dev, "Invalid dai link type %d\n",
1258 dai_link->dobj.type);
1259 return -EINVAL;
1260 }
1261
1262 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001263 /*
1264 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001265 * on the link created by topology.
1266 */
1267 if (dai_link->dobj.type && card->add_dai_link)
1268 card->add_dai_link(card, dai_link);
1269
Mengdong Linf8f80362015-12-02 14:11:22 +08001270 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001271
1272 return 0;
1273}
1274EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1275
1276/**
1277 * snd_soc_remove_dai_link - Remove a DAI link from the list
1278 * @card: The ASoC card that owns the link
1279 * @dai_link: The DAI link to remove
1280 *
1281 * This function removes a DAI link from the ASoC card's link list.
1282 *
1283 * For DAI links previously added by topology, topology should
1284 * remove them by using the dobj embedded in the link.
1285 */
1286void snd_soc_remove_dai_link(struct snd_soc_card *card,
1287 struct snd_soc_dai_link *dai_link)
1288{
1289 struct snd_soc_dai_link *link, *_link;
1290
1291 if (dai_link->dobj.type
1292 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1293 dev_err(card->dev, "Invalid dai link type %d\n",
1294 dai_link->dobj.type);
1295 return;
1296 }
1297
1298 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001299 /*
1300 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001301 * on the link created by topology.
1302 */
1303 if (dai_link->dobj.type && card->remove_dai_link)
1304 card->remove_dai_link(card, dai_link);
1305
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001306 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001307 if (link == dai_link) {
1308 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001309 return;
1310 }
1311 }
1312}
1313EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1314
Jerome Brunetaefba452018-07-13 14:50:43 +02001315static void soc_set_of_name_prefix(struct snd_soc_component *component)
1316{
1317 struct device_node *component_of_node = component->dev->of_node;
1318 const char *str;
1319 int ret;
1320
1321 if (!component_of_node && component->dev->parent)
1322 component_of_node = component->dev->parent->of_node;
1323
1324 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1325 &str);
1326 if (!ret)
1327 component->name_prefix = str;
1328}
1329
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001330static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001331 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001332{
1333 int i;
1334
Jerome Brunetaefba452018-07-13 14:50:43 +02001335 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001336 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001337 struct device_node *component_of_node = component->dev->of_node;
1338
1339 if (!component_of_node && component->dev->parent)
1340 component_of_node = component->dev->parent->of_node;
1341
1342 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001343 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001344 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001345 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001346 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001347 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001348 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001349
1350 /*
1351 * If there is no configuration table or no match in the table,
1352 * check if a prefix is provided in the node
1353 */
1354 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001355}
1356
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001357static int soc_probe_component(struct snd_soc_card *card,
1358 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001359{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001360 struct snd_soc_dapm_context *dapm =
1361 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001362 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001363 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001364
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001365 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001366 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001367
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001368 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001369 if (component->card != card) {
1370 dev_err(component->dev,
1371 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1372 card->name, component->card->name);
1373 return -ENODEV;
1374 }
1375 return 0;
1376 }
1377
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001378 if (!try_module_get(component->dev->driver->owner))
1379 return -ENODEV;
1380
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001381 component->card = card;
1382 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001383 INIT_LIST_HEAD(&component->card_list);
1384 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001385 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001386
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001387 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001388
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001389 if (component->driver->dapm_widgets) {
1390 ret = snd_soc_dapm_new_controls(dapm,
1391 component->driver->dapm_widgets,
1392 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001393
1394 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001395 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001396 "Failed to create new controls %d\n", ret);
1397 goto err_probe;
1398 }
1399 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001400
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001401 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001402 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001403 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001404 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001405 "Failed to create DAI widgets %d\n", ret);
1406 goto err_probe;
1407 }
1408 }
Mark Brown888df392012-02-16 19:37:51 -08001409
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001410 if (component->driver->probe) {
1411 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001412 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001413 dev_err(component->dev,
1414 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001415 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001416 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001417
1418 WARN(dapm->idle_bias_off &&
1419 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001420 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001421 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001422 }
1423
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001424 /* machine specific init */
1425 if (component->init) {
1426 ret = component->init(component);
1427 if (ret < 0) {
1428 dev_err(component->dev,
1429 "Failed to do machine specific init %d\n", ret);
1430 goto err_probe;
1431 }
1432 }
1433
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001434 if (component->driver->controls)
1435 snd_soc_add_component_controls(component,
1436 component->driver->controls,
1437 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001438 if (component->driver->dapm_routes)
1439 snd_soc_dapm_add_routes(dapm,
1440 component->driver->dapm_routes,
1441 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001442
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001443 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001444 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001445 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001446
Jarkko Nikula70d293312011-01-27 16:24:22 +02001447err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001448 if (ret < 0)
1449 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001450
1451 return ret;
1452}
1453
Mark Brown36ae1a92012-01-06 17:12:45 -08001454static void rtd_release(struct device *dev)
1455{
1456 kfree(dev);
1457}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001458
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001459static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1460 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001461{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001462 int ret = 0;
1463
Jarkko Nikula589c3562010-12-06 16:27:07 +02001464 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001465 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1466 if (!rtd->dev)
1467 return -ENOMEM;
1468 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001469 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001470 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001471 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001472 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001473 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001474 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001475 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1476 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1477 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1478 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001479 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001480 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001481 /* calling put_device() here to free the rtd->dev */
1482 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001483 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001484 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001485 return ret;
1486 }
1487 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001488 return 0;
1489}
1490
Mengdong Lin1a497982015-11-18 02:34:11 -05001491static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001492 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001493{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001494 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001495 struct snd_soc_rtdcom_list *rtdcom;
1496 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001497
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001498 for_each_rtdcom(rtd, rtdcom) {
1499 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001500
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001501 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001502 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001503 if (ret < 0)
1504 return ret;
1505 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001506 }
1507
Stephen Warren62ae68f2012-06-08 12:34:23 -06001508 return 0;
1509}
1510
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001511static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001512{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001513 if (dai->probed ||
1514 dai->driver->probe_order != order)
1515 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001516
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001517 if (dai->driver->probe) {
1518 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001519
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001520 if (ret < 0) {
1521 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1522 dai->name, ret);
1523 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001524 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001525 }
1526
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001527 dai->probed = 1;
1528
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001529 return 0;
1530}
1531
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001532static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1533 struct snd_soc_pcm_runtime *rtd)
1534{
1535 int i, ret = 0;
1536
1537 for (i = 0; i < num_dais; ++i) {
1538 struct snd_soc_dai_driver *drv = dais[i]->driver;
1539
Rohit kumarde17f142018-11-01 18:08:49 +05301540 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001541 ret = drv->pcm_new(rtd, dais[i]);
1542 if (ret < 0) {
1543 dev_err(dais[i]->dev,
1544 "ASoC: Failed to bind %s with pcm device\n",
1545 dais[i]->name);
1546 return ret;
1547 }
1548 }
1549
1550 return 0;
1551}
1552
Mengdong Lin1a497982015-11-18 02:34:11 -05001553static int soc_probe_link_dais(struct snd_soc_card *card,
1554 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001555{
Mengdong Lin1a497982015-11-18 02:34:11 -05001556 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001557 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001558 struct snd_soc_rtdcom_list *rtdcom;
1559 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001560 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001561 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001563 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001564 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001565
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566 /* set default power off timeout */
1567 rtd->pmdown_time = pmdown_time;
1568
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001569 ret = soc_probe_dai(cpu_dai, order);
1570 if (ret)
1571 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001574 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1575 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001576 if (ret)
1577 return ret;
1578 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001579
Liam Girdwood0168bf02011-06-07 16:08:05 +01001580 /* complete DAI probe during last probe */
1581 if (order != SND_SOC_COMP_ORDER_LAST)
1582 return 0;
1583
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001584 /* do machine specific initialization */
1585 if (dai_link->init) {
1586 ret = dai_link->init(rtd);
1587 if (ret < 0) {
1588 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1589 dai_link->name, ret);
1590 return ret;
1591 }
1592 }
1593
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001594 if (dai_link->dai_fmt)
1595 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1596
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001597 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001598 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001600
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001601#ifdef CONFIG_DEBUG_FS
1602 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001603 if (dai_link->dynamic)
1604 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001605#endif
1606
Liam Girdwooda655de82018-07-02 16:59:54 +01001607 num = rtd->num;
1608
1609 /*
1610 * most drivers will register their PCMs using DAI link ordering but
1611 * topology based drivers can use the DAI link id field to set PCM
1612 * device number and then use rtd + a base offset of the BEs.
1613 */
1614 for_each_rtdcom(rtd, rtdcom) {
1615 component = rtdcom->component;
1616
1617 if (!component->driver->use_dai_pcm_id)
1618 continue;
1619
1620 if (rtd->dai_link->no_pcm)
1621 num += component->driver->be_pcm_base;
1622 else
1623 num = rtd->dai_link->id;
1624 }
1625
Jie Yang6f0c4222015-10-13 23:41:00 +08001626 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001627 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001628 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001629 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001630 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301631 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001632 return ret;
1633 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001634 } else if (!dai_link->params) {
1635 /* create the pcm */
1636 ret = soc_new_pcm(rtd, num);
1637 if (ret < 0) {
1638 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1639 dai_link->stream_name, ret);
1640 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001641 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001642 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1643 if (ret < 0)
1644 return ret;
1645 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1646 rtd->num_codecs, rtd);
1647 if (ret < 0)
1648 return ret;
1649 } else {
1650 INIT_DELAYED_WORK(&rtd->delayed_work,
1651 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 }
1653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 return 0;
1655}
1656
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001657static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001658{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001659 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001660 struct snd_soc_component *component;
1661 const char *name;
1662 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001663
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001664 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1665 /* codecs, usually analog devices */
1666 name = aux_dev->codec_name;
1667 codec_of_node = aux_dev->codec_of_node;
1668 component = soc_find_component(codec_of_node, name);
1669 if (!component) {
1670 if (codec_of_node)
1671 name = of_node_full_name(codec_of_node);
1672 goto err_defer;
1673 }
1674 } else if (aux_dev->name) {
1675 /* generic components */
1676 name = aux_dev->name;
1677 component = soc_find_component(NULL, name);
1678 if (!component)
1679 goto err_defer;
1680 } else {
1681 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1682 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001683 }
1684
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001685 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001686 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001687
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001688 return 0;
1689
1690err_defer:
1691 dev_err(card->dev, "ASoC: %s not registered\n", name);
1692 return -EPROBE_DEFER;
1693}
1694
1695static int soc_probe_aux_devices(struct snd_soc_card *card)
1696{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001697 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001698 int order;
1699 int ret;
1700
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001701 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001702 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001703 if (comp->driver->probe_order == order) {
1704 ret = soc_probe_component(card, comp);
1705 if (ret < 0) {
1706 dev_err(card->dev,
1707 "ASoC: failed to probe aux component %s %d\n",
1708 comp->name, ret);
1709 return ret;
1710 }
1711 }
1712 }
1713 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001714
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001715 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001716}
1717
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001718static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001719{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001720 struct snd_soc_component *comp, *_comp;
1721 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001722
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001723 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001724 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001725 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001726
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001727 if (comp->driver->remove_order == order) {
1728 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001729 /* remove it from the card's aux_comp_list */
1730 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001731 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001732 }
1733 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001734}
1735
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001736/**
1737 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1738 * @rtd: The runtime for which the DAI link format should be changed
1739 * @dai_fmt: The new DAI link format
1740 *
1741 * This function updates the DAI link format for all DAIs connected to the DAI
1742 * link for the specified runtime.
1743 *
1744 * Note: For setups with a static format set the dai_fmt field in the
1745 * corresponding snd_dai_link struct instead of using this function.
1746 *
1747 * Returns 0 on success, otherwise a negative error code.
1748 */
1749int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1750 unsigned int dai_fmt)
1751{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001752 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001753 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001754 unsigned int i;
1755 int ret;
1756
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001757 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001758 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1759 if (ret != 0 && ret != -ENOTSUPP) {
1760 dev_warn(codec_dai->dev,
1761 "ASoC: Failed to set DAI format: %d\n", ret);
1762 return ret;
1763 }
1764 }
1765
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001766 /*
1767 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1768 * the component which has non_legacy_dai_naming is Codec
1769 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001770 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001771 unsigned int inv_dai_fmt;
1772
1773 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1774 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1775 case SND_SOC_DAIFMT_CBM_CFM:
1776 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1777 break;
1778 case SND_SOC_DAIFMT_CBM_CFS:
1779 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1780 break;
1781 case SND_SOC_DAIFMT_CBS_CFM:
1782 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1783 break;
1784 case SND_SOC_DAIFMT_CBS_CFS:
1785 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1786 break;
1787 }
1788
1789 dai_fmt = inv_dai_fmt;
1790 }
1791
1792 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1793 if (ret != 0 && ret != -ENOTSUPP) {
1794 dev_warn(cpu_dai->dev,
1795 "ASoC: Failed to set DAI format: %d\n", ret);
1796 return ret;
1797 }
1798
1799 return 0;
1800}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001801EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001802
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001803#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001804/*
1805 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001806 * separate different DMI fields in the card long name. Only number and
1807 * alphabet characters and a few separator characters are kept.
1808 */
1809static void cleanup_dmi_name(char *name)
1810{
1811 int i, j = 0;
1812
1813 for (i = 0; name[i]; i++) {
1814 if (isalnum(name[i]) || (name[i] == '.')
1815 || (name[i] == '_'))
1816 name[j++] = name[i];
1817 else if (name[i] == '-')
1818 name[j++] = '_';
1819 }
1820
1821 name[j] = '\0';
1822}
1823
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001824/*
1825 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001826 * in the black list.
1827 */
1828static int is_dmi_valid(const char *field)
1829{
1830 int i = 0;
1831
1832 while (dmi_blacklist[i]) {
1833 if (strstr(field, dmi_blacklist[i]))
1834 return 0;
1835 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001836 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001837
1838 return 1;
1839}
1840
Liam Girdwood345233d2017-01-14 16:13:02 +08001841/**
1842 * snd_soc_set_dmi_name() - Register DMI names to card
1843 * @card: The card to register DMI names
1844 * @flavour: The flavour "differentiator" for the card amongst its peers.
1845 *
1846 * An Intel machine driver may be used by many different devices but are
1847 * difficult for userspace to differentiate, since machine drivers ususally
1848 * use their own name as the card short name and leave the card long name
1849 * blank. To differentiate such devices and fix bugs due to lack of
1850 * device-specific configurations, this function allows DMI info to be used
1851 * as the sound card long name, in the format of
1852 * "vendor-product-version-board"
1853 * (Character '-' is used to separate different DMI fields here).
1854 * This will help the user space to load the device-specific Use Case Manager
1855 * (UCM) configurations for the card.
1856 *
1857 * Possible card long names may be:
1858 * DellInc.-XPS139343-01-0310JH
1859 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1860 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1861 *
1862 * This function also supports flavoring the card longname to provide
1863 * the extra differentiation, like "vendor-product-version-board-flavor".
1864 *
1865 * We only keep number and alphabet characters and a few separator characters
1866 * in the card long name since UCM in the user space uses the card long names
1867 * as card configuration directory names and AudoConf cannot support special
1868 * charactors like SPACE.
1869 *
1870 * Returns 0 on success, otherwise a negative error code.
1871 */
1872int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1873{
1874 const char *vendor, *product, *product_version, *board;
1875 size_t longname_buf_size = sizeof(card->snd_card->longname);
1876 size_t len;
1877
1878 if (card->long_name)
1879 return 0; /* long name already set by driver or from DMI */
1880
1881 /* make up dmi long name as: vendor.product.version.board */
1882 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001883 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001884 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1885 return 0;
1886 }
1887
1888 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1889 "%s", vendor);
1890 cleanup_dmi_name(card->dmi_longname);
1891
1892 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001893 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001894 len = strlen(card->dmi_longname);
1895 snprintf(card->dmi_longname + len,
1896 longname_buf_size - len,
1897 "-%s", product);
1898
1899 len++; /* skip the separator "-" */
1900 if (len < longname_buf_size)
1901 cleanup_dmi_name(card->dmi_longname + len);
1902
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001903 /*
1904 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001905 * name in the product version field
1906 */
1907 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001908 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001909 len = strlen(card->dmi_longname);
1910 snprintf(card->dmi_longname + len,
1911 longname_buf_size - len,
1912 "-%s", product_version);
1913
1914 len++;
1915 if (len < longname_buf_size)
1916 cleanup_dmi_name(card->dmi_longname + len);
1917 }
1918 }
1919
1920 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001921 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001922 len = strlen(card->dmi_longname);
1923 snprintf(card->dmi_longname + len,
1924 longname_buf_size - len,
1925 "-%s", board);
1926
1927 len++;
1928 if (len < longname_buf_size)
1929 cleanup_dmi_name(card->dmi_longname + len);
1930 } else if (!product) {
1931 /* fall back to using legacy name */
1932 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1933 return 0;
1934 }
1935
1936 /* Add flavour to dmi long name */
1937 if (flavour) {
1938 len = strlen(card->dmi_longname);
1939 snprintf(card->dmi_longname + len,
1940 longname_buf_size - len,
1941 "-%s", flavour);
1942
1943 len++;
1944 if (len < longname_buf_size)
1945 cleanup_dmi_name(card->dmi_longname + len);
1946 }
1947
1948 /* set the card long name */
1949 card->long_name = card->dmi_longname;
1950
1951 return 0;
1952}
1953EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001954#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001955
Liam Girdwooda655de82018-07-02 16:59:54 +01001956static void soc_check_tplg_fes(struct snd_soc_card *card)
1957{
1958 struct snd_soc_component *component;
1959 const struct snd_soc_component_driver *comp_drv;
1960 struct snd_soc_dai_link *dai_link;
1961 int i;
1962
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001963 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001964
1965 /* does this component override FEs ? */
1966 if (!component->driver->ignore_machine)
1967 continue;
1968
1969 /* for this machine ? */
1970 if (strcmp(component->driver->ignore_machine,
1971 card->dev->driver->name))
1972 continue;
1973
1974 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001975 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001976
1977 /* ignore this FE */
1978 if (dai_link->dynamic) {
1979 dai_link->ignore = true;
1980 continue;
1981 }
1982
1983 dev_info(card->dev, "info: override FE DAI link %s\n",
1984 card->dai_link[i].name);
1985
1986 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001987 if (snd_soc_init_platform(card, dai_link) < 0) {
1988 dev_err(card->dev, "init platform error");
1989 continue;
1990 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001991 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001992
1993 /* convert non BE into BE */
1994 dai_link->no_pcm = 1;
1995
1996 /* override any BE fixups */
1997 dai_link->be_hw_params_fixup =
1998 component->driver->be_hw_params_fixup;
1999
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002000 /*
2001 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01002002 * dai link name if it's NULL to help bind widgets.
2003 */
2004 if (!dai_link->stream_name)
2005 dai_link->stream_name = dai_link->name;
2006 }
2007
2008 /* Inform userspace we are using alternate topology */
2009 if (component->driver->topology_name_prefix) {
2010
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002011 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01002012 if (!card->topology_shortname_created) {
2013 comp_drv = component->driver;
2014
2015 snprintf(card->topology_shortname, 32, "%s-%s",
2016 comp_drv->topology_name_prefix,
2017 card->name);
2018 card->topology_shortname_created = true;
2019 }
2020
2021 /* use topology shortname */
2022 card->name = card->topology_shortname;
2023 }
2024 }
2025}
2026
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002027static int soc_cleanup_card_resources(struct snd_soc_card *card)
2028{
2029 /* free the ALSA card at first; this syncs with pending operations */
2030 if (card->snd_card)
2031 snd_card_free(card->snd_card);
2032
2033 /* remove and free each DAI */
2034 soc_remove_dai_links(card);
2035 soc_remove_pcm_runtimes(card);
Curtis Malainey78a24e12019-01-29 13:47:09 -08002036 soc_cleanup_platform(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002037
2038 /* remove auxiliary devices */
2039 soc_remove_aux_devices(card);
2040
2041 snd_soc_dapm_free(&card->dapm);
2042 soc_cleanup_card_debugfs(card);
2043
2044 /* remove the card */
2045 if (card->remove)
2046 card->remove(card);
2047
2048 return 0;
2049}
2050
Mark Brownb19e6e72012-03-14 21:18:39 +00002051static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002052{
Mengdong Lin1a497982015-11-18 02:34:11 -05002053 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002054 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002055 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002057 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002058 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002060 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2061 card->dapm.dev = card->dev;
2062 card->dapm.card = card;
2063 list_add(&card->dapm.list, &card->dapm_list);
2064
Liam Girdwooda655de82018-07-02 16:59:54 +01002065 /* check whether any platform is ignore machine FE and using topology */
2066 soc_check_tplg_fes(card);
2067
Mark Brownb19e6e72012-03-14 21:18:39 +00002068 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002069 for_each_card_prelinks(card, i, dai_link) {
2070 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002071 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002072 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002073 }
2074
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002075 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002076 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002077 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002078 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002079 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002080 }
2081
Mengdong Linf8f80362015-12-02 14:11:22 +08002082 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002083 for_each_card_prelinks(card, i, dai_link)
2084 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002085
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002086 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002087 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088 card->owner, 0, &card->snd_card);
2089 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002090 dev_err(card->dev,
2091 "ASoC: can't create sound card for card %s: %d\n",
2092 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002093 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002094 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002095
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002096 soc_init_card_debugfs(card);
2097
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002098#ifdef CONFIG_DEBUG_FS
2099 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2100#endif
2101
Mark Brown88ee1c62011-02-02 10:43:26 +00002102#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002103 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002104 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002105#endif
Andy Green6ed25972008-06-13 16:24:05 +01002106
Mark Brown9a841eb2011-04-12 17:51:37 -07002107 if (card->dapm_widgets)
2108 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2109 card->num_dapm_widgets);
2110
Nicolin Chenf23e8602015-02-14 17:22:49 -08002111 if (card->of_dapm_widgets)
2112 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2113 card->num_of_dapm_widgets);
2114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002115 /* initialise the sound card only once */
2116 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002117 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002118 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002119 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002120 }
2121
Stephen Warren62ae68f2012-06-08 12:34:23 -06002122 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002123 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002124 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002125 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002126 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002127 dev_err(card->dev,
2128 "ASoC: failed to instantiate card %d\n",
2129 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002130 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002131 }
2132 }
2133 }
2134
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002135 /* probe auxiliary components */
2136 ret = soc_probe_aux_devices(card);
2137 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002138 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002139
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002140 /*
2141 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002142 * Components with topology may bring new DAIs and DAI links.
2143 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002144 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002145 if (soc_is_dai_link_bound(card, dai_link))
2146 continue;
2147
2148 ret = soc_init_dai_link(card, dai_link);
2149 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002150 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002151 ret = soc_bind_dai_link(card, dai_link);
2152 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002153 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002154 }
2155
Stephen Warren62ae68f2012-06-08 12:34:23 -06002156 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002157 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002158 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002159 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002160 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002161 dev_err(card->dev,
2162 "ASoC: failed to instantiate card %d\n",
2163 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002164 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002165 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002166 }
2167 }
2168
Mark Brown888df392012-02-16 19:37:51 -08002169 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002170 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002171
Mark Brownb7af1da2011-04-07 19:18:44 +09002172 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002173 snd_soc_add_card_controls(card, card->controls,
2174 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002175
Mark Brownb8ad29d2011-03-02 18:35:51 +00002176 if (card->dapm_routes)
2177 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2178 card->num_dapm_routes);
2179
Nicolin Chenf23e8602015-02-14 17:22:49 -08002180 if (card->of_dapm_routes)
2181 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2182 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002183
Takashi Iwai861886d2017-04-24 08:54:42 +02002184 /* try to set some sane longname if DMI is available */
2185 snd_soc_set_dmi_name(card, NULL);
2186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002187 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002188 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002189 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2190 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002191 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2192 "%s", card->driver_name ? card->driver_name : card->name);
2193 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2194 switch (card->snd_card->driver[i]) {
2195 case '_':
2196 case '-':
2197 case '\0':
2198 break;
2199 default:
2200 if (!isalnum(card->snd_card->driver[i]))
2201 card->snd_card->driver[i] = '_';
2202 break;
2203 }
2204 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002205
Mark Brown28e9ad92011-03-02 18:36:34 +00002206 if (card->late_probe) {
2207 ret = card->late_probe(card);
2208 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002209 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002210 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002211 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002212 }
2213 }
2214
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002215 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002216
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002217 ret = snd_card_register(card->snd_card);
2218 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002219 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2220 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002221 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002222 }
2223
Mark Brown435c5e22008-12-04 15:32:53 +00002224 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002225 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002226 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002227
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002228probe_end:
2229 if (ret < 0)
2230 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002232 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002233 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002234
Mark Brownb19e6e72012-03-14 21:18:39 +00002235 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002236}
2237
2238/* probes a new socdev */
2239static int soc_probe(struct platform_device *pdev)
2240{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002241 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002242
Vinod Koul70a7ca32011-01-14 19:22:48 +05302243 /*
2244 * no card, so machine driver should be registering card
2245 * we should not be here in that case so ret error
2246 */
2247 if (!card)
2248 return -EINVAL;
2249
Mark Brownfe4085e2012-03-02 13:07:41 +00002250 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002251 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002252 card->name);
2253
Mark Brown435c5e22008-12-04 15:32:53 +00002254 /* Bodge while we unpick instantiation */
2255 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002256
Mark Brown28d528c2012-08-09 18:45:23 +01002257 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258}
2259
2260/* removes a socdev */
2261static int soc_remove(struct platform_device *pdev)
2262{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002263 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264
Mark Brownc5af3a22008-11-28 13:29:45 +00002265 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002266 return 0;
2267}
2268
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002269int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002270{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002271 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002272 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002273
2274 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002275 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002276
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002277 /*
2278 * Flush out pmdown_time work - we actually do want to run it
2279 * now, we're shutting down so no imminent restart.
2280 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002281 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002282
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002283 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002284
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002285 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002286 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002287 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002288 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002289 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002290
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002291 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002292 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002293 pinctrl_pm_select_sleep_state(codec_dai->dev);
2294 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002295 }
2296
Mark Brown416356f2009-06-30 19:05:15 +01002297 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002298}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002299EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002300
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002301const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302302 .suspend = snd_soc_suspend,
2303 .resume = snd_soc_resume,
2304 .freeze = snd_soc_suspend,
2305 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002306 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302307 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002308};
Stephen Warrendeb26072011-04-05 19:35:30 -06002309EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002310
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311/* ASoC platform driver */
2312static struct platform_driver soc_driver = {
2313 .driver = {
2314 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002315 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 },
2317 .probe = soc_probe,
2318 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319};
2320
Mark Brown096e49d2009-07-05 15:12:22 +01002321/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 * snd_soc_cnew - create new control
2323 * @_template: control template
2324 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002325 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002326 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002327 *
2328 * Create a new mixer control from a template control.
2329 *
2330 * Returns 0 for success, else error.
2331 */
2332struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002333 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002334 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335{
2336 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002337 struct snd_kcontrol *kcontrol;
2338 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339
2340 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341 template.index = 0;
2342
Mark Brownefb7ac32011-03-08 17:23:24 +00002343 if (!long_name)
2344 long_name = template.name;
2345
2346 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002347 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002348 if (!name)
2349 return NULL;
2350
Mark Brownefb7ac32011-03-08 17:23:24 +00002351 template.name = name;
2352 } else {
2353 template.name = long_name;
2354 }
2355
2356 kcontrol = snd_ctl_new1(&template, data);
2357
2358 kfree(name);
2359
2360 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002361}
2362EXPORT_SYMBOL_GPL(snd_soc_cnew);
2363
Liam Girdwood022658b2012-02-03 17:43:09 +00002364static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2365 const struct snd_kcontrol_new *controls, int num_controls,
2366 const char *prefix, void *data)
2367{
2368 int err, i;
2369
2370 for (i = 0; i < num_controls; i++) {
2371 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002372
Liam Girdwood022658b2012-02-03 17:43:09 +00002373 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2374 control->name, prefix));
2375 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002376 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2377 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002378 return err;
2379 }
2380 }
2381
2382 return 0;
2383}
2384
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002385struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2386 const char *name)
2387{
2388 struct snd_card *card = soc_card->snd_card;
2389 struct snd_kcontrol *kctl;
2390
2391 if (unlikely(!name))
2392 return NULL;
2393
2394 list_for_each_entry(kctl, &card->controls, list)
2395 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2396 return kctl;
2397 return NULL;
2398}
2399EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2400
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002402 * snd_soc_add_component_controls - Add an array of controls to a component.
2403 *
2404 * @component: Component to add controls to
2405 * @controls: Array of controls to add
2406 * @num_controls: Number of elements in the array
2407 *
2408 * Return: 0 for success, else error.
2409 */
2410int snd_soc_add_component_controls(struct snd_soc_component *component,
2411 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2412{
2413 struct snd_card *card = component->card->snd_card;
2414
2415 return snd_soc_add_controls(card, component->dev, controls,
2416 num_controls, component->name_prefix, component);
2417}
2418EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2419
2420/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002421 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2422 * Convenience function to add a list of controls.
2423 *
2424 * @soc_card: SoC card to add controls to
2425 * @controls: array of controls to add
2426 * @num_controls: number of elements in the array
2427 *
2428 * Return 0 for success, else error.
2429 */
2430int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2431 const struct snd_kcontrol_new *controls, int num_controls)
2432{
2433 struct snd_card *card = soc_card->snd_card;
2434
2435 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2436 NULL, soc_card);
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2439
2440/**
2441 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2442 * Convienience function to add a list of controls.
2443 *
2444 * @dai: DAI to add controls to
2445 * @controls: array of controls to add
2446 * @num_controls: number of elements in the array
2447 *
2448 * Return 0 for success, else error.
2449 */
2450int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2451 const struct snd_kcontrol_new *controls, int num_controls)
2452{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002453 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002454
2455 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2456 NULL, dai);
2457}
2458EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2459
2460/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002461 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2462 * @dai: DAI
2463 * @clk_id: DAI specific clock ID
2464 * @freq: new clock frequency in Hz
2465 * @dir: new clock direction - input/output.
2466 *
2467 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2468 */
2469int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2470 unsigned int freq, int dir)
2471{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002472 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002473 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002474
2475 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2476 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002477}
2478EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2479
2480/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002481 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2482 * @component: COMPONENT
2483 * @clk_id: DAI specific clock ID
2484 * @source: Source for the clock
2485 * @freq: new clock frequency in Hz
2486 * @dir: new clock direction - input/output.
2487 *
2488 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2489 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002490int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2491 int clk_id, int source, unsigned int freq,
2492 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002493{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002494 if (component->driver->set_sysclk)
2495 return component->driver->set_sysclk(component, clk_id, source,
2496 freq, dir);
2497
2498 return -ENOTSUPP;
2499}
2500EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2501
2502/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002503 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2504 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002505 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002506 * @div: new clock divisor.
2507 *
2508 * Configures the clock dividers. This is used to derive the best DAI bit and
2509 * frame clocks from the system or master clock. It's best to set the DAI bit
2510 * and frame clocks as low as possible to save system power.
2511 */
2512int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2513 int div_id, int div)
2514{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002515 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002516 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002517 else
2518 return -EINVAL;
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2521
2522/**
2523 * snd_soc_dai_set_pll - configure DAI PLL.
2524 * @dai: DAI
2525 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002526 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002527 * @freq_in: PLL input clock frequency in Hz
2528 * @freq_out: requested PLL output clock frequency in Hz
2529 *
2530 * Configures and enables PLL to generate output clock based on input clock.
2531 */
Mark Brown85488032009-09-05 18:52:16 +01002532int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2533 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002534{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002535 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002536 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002537 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002538
2539 return snd_soc_component_set_pll(dai->component, pll_id, source,
2540 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002541}
2542EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2543
Mark Brownec4ee522011-03-07 20:58:11 +00002544/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002545 * snd_soc_component_set_pll - configure component PLL.
2546 * @component: COMPONENT
2547 * @pll_id: DAI specific PLL ID
2548 * @source: DAI specific source for the PLL
2549 * @freq_in: PLL input clock frequency in Hz
2550 * @freq_out: requested PLL output clock frequency in Hz
2551 *
2552 * Configures and enables PLL to generate output clock based on input clock.
2553 */
2554int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2555 int source, unsigned int freq_in,
2556 unsigned int freq_out)
2557{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002558 if (component->driver->set_pll)
2559 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002560 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002561
2562 return -EINVAL;
2563}
2564EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2565
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002566/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002567 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2568 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002569 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002570 *
2571 * Configures the DAI for a preset BCLK to sample rate ratio.
2572 */
2573int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2574{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002575 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002576 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2577 else
2578 return -EINVAL;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2581
2582/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002583 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2584 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002585 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002586 *
2587 * Configures the DAI hardware format and clocking.
2588 */
2589int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2590{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002591 if (dai->driver->ops->set_fmt == NULL)
2592 return -ENOTSUPP;
2593 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002594}
2595EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2596
2597/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002598 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002599 * @slots: Number of slots in use.
2600 * @tx_mask: bitmask representing active TX slots.
2601 * @rx_mask: bitmask representing active RX slots.
2602 *
2603 * Generates the TDM tx and rx slot default masks for DAI.
2604 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002605static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002606 unsigned int *tx_mask,
2607 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002608{
2609 if (*tx_mask || *rx_mask)
2610 return 0;
2611
2612 if (!slots)
2613 return -EINVAL;
2614
2615 *tx_mask = (1 << slots) - 1;
2616 *rx_mask = (1 << slots) - 1;
2617
2618 return 0;
2619}
2620
2621/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002622 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2623 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002624 * @tx_mask: bitmask representing active TX slots.
2625 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002626 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002627 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002628 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002629 * This function configures the specified DAI for TDM operation. @slot contains
2630 * the total number of slots of the TDM stream and @slot_with the width of each
2631 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2632 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2633 * DAI should write to or read from. If a bit is set the corresponding slot is
2634 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2635 * the first slot, bit 1 to the second slot and so on. The first active slot
2636 * maps to the first channel of the DAI, the second active slot to the second
2637 * channel and so on.
2638 *
2639 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2640 * @rx_mask and @slot_width will be ignored.
2641 *
2642 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002643 */
2644int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002645 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002646{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002647 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002648 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002649 &tx_mask, &rx_mask);
2650 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002651 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002652
Benoit Cousson88bd8702014-07-08 23:19:34 +02002653 dai->tx_mask = tx_mask;
2654 dai->rx_mask = rx_mask;
2655
Kuninori Morimoto46471922017-09-25 01:38:34 +00002656 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002657 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002658 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002659 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002660 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002661}
2662EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2663
2664/**
Barry Song472df3c2009-09-12 01:16:29 +08002665 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2666 * @dai: DAI
2667 * @tx_num: how many TX channels
2668 * @tx_slot: pointer to an array which imply the TX slot number channel
2669 * 0~num-1 uses
2670 * @rx_num: how many RX channels
2671 * @rx_slot: pointer to an array which imply the RX slot number channel
2672 * 0~num-1 uses
2673 *
2674 * configure the relationship between channel number and TDM slot number.
2675 */
2676int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2677 unsigned int tx_num, unsigned int *tx_slot,
2678 unsigned int rx_num, unsigned int *rx_slot)
2679{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002680 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002681 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002682 rx_num, rx_slot);
2683 else
2684 return -EINVAL;
2685}
2686EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2687
2688/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002689 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2690 * @dai: DAI
2691 * @tx_num: how many TX channels
2692 * @tx_slot: pointer to an array which imply the TX slot number channel
2693 * 0~num-1 uses
2694 * @rx_num: how many RX channels
2695 * @rx_slot: pointer to an array which imply the RX slot number channel
2696 * 0~num-1 uses
2697 */
2698int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2699 unsigned int *tx_num, unsigned int *tx_slot,
2700 unsigned int *rx_num, unsigned int *rx_slot)
2701{
2702 if (dai->driver->ops->get_channel_map)
2703 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2704 rx_num, rx_slot);
2705 else
2706 return -ENOTSUPP;
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2709
2710/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002711 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2712 * @dai: DAI
2713 * @tristate: tristate enable
2714 *
2715 * Tristates the DAI so that others can use it.
2716 */
2717int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2718{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002719 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002720 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002721 else
2722 return -EINVAL;
2723}
2724EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2725
2726/**
2727 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2728 * @dai: DAI
2729 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002730 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002731 *
2732 * Mutes the DAI DAC.
2733 */
Mark Brownda183962013-02-06 15:44:07 +00002734int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2735 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002736{
Mark Brownda183962013-02-06 15:44:07 +00002737 if (dai->driver->ops->mute_stream)
2738 return dai->driver->ops->mute_stream(dai, mute, direction);
2739 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2740 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002741 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002742 else
Mark Brown04570c62012-04-13 19:16:03 +01002743 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002744}
2745EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2746
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002747static int snd_soc_bind_card(struct snd_soc_card *card)
2748{
2749 struct snd_soc_pcm_runtime *rtd;
2750 int ret;
2751
2752 ret = snd_soc_instantiate_card(card);
2753 if (ret != 0)
2754 return ret;
2755
2756 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002757 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2759 struct snd_soc_dai *codec_dai;
2760 int j;
2761
2762 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2763 if (!codec_dai->active)
2764 pinctrl_pm_select_sleep_state(codec_dai->dev);
2765 }
2766
2767 if (!cpu_dai->active)
2768 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2769 }
2770
2771 return ret;
2772}
2773
Mark Brownc5af3a22008-11-28 13:29:45 +00002774/**
2775 * snd_soc_register_card - Register a card with the ASoC core
2776 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002777 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002778 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002779 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302780int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002781{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002782 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002783 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002784
Mark Brownc5af3a22008-11-28 13:29:45 +00002785 if (!card->name || !card->dev)
2786 return -EINVAL;
2787
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302788 mutex_lock(&client_mutex);
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002789 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002790
Mengdong Lin923c5e612015-11-23 11:01:49 -05002791 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002792 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002793 dev_err(card->dev, "ASoC: failed to init link %s\n",
2794 link->name);
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302795 mutex_unlock(&client_mutex);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002796 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002797 }
Stephen Warren5a504962011-12-21 10:40:59 -07002798 }
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302799 mutex_unlock(&client_mutex);
Stephen Warren5a504962011-12-21 10:40:59 -07002800
Mark Browned77cc12011-05-03 18:25:34 +01002801 dev_set_drvdata(card->dev, card);
2802
Stephen Warren111c6412011-01-28 14:26:35 -07002803 snd_soc_initialize_card_lists(card);
2804
Mengdong Linf8f80362015-12-02 14:11:22 +08002805 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002806
Mengdong Lin1a497982015-11-18 02:34:11 -05002807 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002808 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002809
Mark Browndb432b42011-10-03 21:06:40 +01002810 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002811 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002812 card->instantiated = 0;
2813 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002814 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002815
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002816 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002817}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302818EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002819
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002820static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2821{
2822 if (card->instantiated) {
2823 card->instantiated = false;
2824 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002825 snd_soc_flush_all_delayed_work(card);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002826 soc_cleanup_card_resources(card);
2827 if (!unregister)
2828 list_add(&card->list, &unbind_card_list);
2829 } else {
2830 if (unregister)
2831 list_del(&card->list);
2832 }
2833}
2834
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002835/**
2836 * snd_soc_unregister_card - Unregister a card with the ASoC core
2837 *
2838 * @card: Card to unregister
2839 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002840 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302841int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002842{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002843 snd_soc_unbind_card(card, true);
2844 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002845
2846 return 0;
2847}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302848EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002849
2850/*
2851 * Simplify DAI link configuration by removing ".-1" from device names
2852 * and sanitizing names.
2853 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002854static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002855{
2856 char *found, name[NAME_SIZE];
2857 int id1, id2;
2858
2859 if (dev_name(dev) == NULL)
2860 return NULL;
2861
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002862 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002863
2864 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002865 found = strstr(name, dev->driver->name);
2866 if (found) {
2867 /* get ID */
2868 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2869
2870 /* discard ID from name if ID == -1 */
2871 if (*id == -1)
2872 found[strlen(dev->driver->name)] = '\0';
2873 }
2874
2875 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002876 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002877 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2878 char tmp[NAME_SIZE];
2879
2880 /* create unique ID number from I2C addr and bus */
2881 *id = ((id1 & 0xffff) << 16) + id2;
2882
2883 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002884 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2885 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002886 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002887 } else
2888 *id = 0;
2889 }
2890
2891 return kstrdup(name, GFP_KERNEL);
2892}
2893
2894/*
2895 * Simplify DAI link naming for single devices with multiple DAIs by removing
2896 * any ".-1" and using the DAI name (instead of device name).
2897 */
2898static inline char *fmt_multiple_name(struct device *dev,
2899 struct snd_soc_dai_driver *dai_drv)
2900{
2901 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002902 dev_err(dev,
2903 "ASoC: error - multiple DAI %s registered with no name\n",
2904 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002905 return NULL;
2906 }
2907
2908 return kstrdup(dai_drv->name, GFP_KERNEL);
2909}
2910
2911/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002912 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002913 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002914 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002915 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002916static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002917{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002918 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002919
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002920 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002921 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2922 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002923 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002924 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002925 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002926 }
Mark Brown91151712008-11-30 23:31:24 +00002927}
Mark Brown91151712008-11-30 23:31:24 +00002928
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002929/* Create a DAI and add it to the component's DAI list */
2930static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2931 struct snd_soc_dai_driver *dai_drv,
2932 bool legacy_dai_naming)
2933{
2934 struct device *dev = component->dev;
2935 struct snd_soc_dai *dai;
2936
2937 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2938
2939 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2940 if (dai == NULL)
2941 return NULL;
2942
2943 /*
2944 * Back in the old days when we still had component-less DAIs,
2945 * instead of having a static name, component-less DAIs would
2946 * inherit the name of the parent device so it is possible to
2947 * register multiple instances of the DAI. We still need to keep
2948 * the same naming style even though those DAIs are not
2949 * component-less anymore.
2950 */
2951 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002952 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002953 dai->name = fmt_single_name(dev, &dai->id);
2954 } else {
2955 dai->name = fmt_multiple_name(dev, dai_drv);
2956 if (dai_drv->id)
2957 dai->id = dai_drv->id;
2958 else
2959 dai->id = component->num_dai;
2960 }
2961 if (dai->name == NULL) {
2962 kfree(dai);
2963 return NULL;
2964 }
2965
2966 dai->component = component;
2967 dai->dev = dev;
2968 dai->driver = dai_drv;
2969 if (!dai->driver->ops)
2970 dai->driver->ops = &null_dai_ops;
2971
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002972 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002973 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002974 component->num_dai++;
2975
2976 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2977 return dai;
2978}
2979
Mark Brown91151712008-11-30 23:31:24 +00002980/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002981 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002982 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002983 * @component: The component the DAIs are registered for
2984 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002985 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002986 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002987static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002988 struct snd_soc_dai_driver *dai_drv,
2989 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002990{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002991 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002992 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002993 unsigned int i;
2994 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002995
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002996 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002997
2998 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002999
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003000 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
3001 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003002 if (dai == NULL) {
3003 ret = -ENOMEM;
3004 goto err;
3005 }
Mark Brown91151712008-11-30 23:31:24 +00003006 }
3007
3008 return 0;
3009
3010err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003011 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003012
3013 return ret;
3014}
Mark Brown91151712008-11-30 23:31:24 +00003015
Mengdong Lin68003e62015-12-31 16:40:43 +08003016/**
3017 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3018 *
3019 * @component: The component the DAIs are registered for
3020 * @dai_drv: DAI driver to use for the DAI
3021 *
3022 * Topology can use this API to register DAIs when probing a component.
3023 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3024 * will be freed in the component cleanup.
3025 */
3026int snd_soc_register_dai(struct snd_soc_component *component,
3027 struct snd_soc_dai_driver *dai_drv)
3028{
3029 struct snd_soc_dapm_context *dapm =
3030 snd_soc_component_get_dapm(component);
3031 struct snd_soc_dai *dai;
3032 int ret;
3033
3034 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3035 dev_err(component->dev, "Invalid dai type %d\n",
3036 dai_drv->dobj.type);
3037 return -EINVAL;
3038 }
3039
3040 lockdep_assert_held(&client_mutex);
3041 dai = soc_add_dai(component, dai_drv, false);
3042 if (!dai)
3043 return -ENOMEM;
3044
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003045 /*
3046 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08003047 * also add routes that need these widgets as source or sink.
3048 */
3049 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3050 if (ret != 0) {
3051 dev_err(component->dev,
3052 "Failed to create DAI widgets %d\n", ret);
3053 }
3054
3055 return ret;
3056}
3057EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3058
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003059static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3060 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003061{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003062 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003063
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003064 component->driver->seq_notifier(component, type, subseq);
3065}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003066
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003067static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3068 int event)
3069{
3070 struct snd_soc_component *component = dapm->component;
3071
3072 return component->driver->stream_event(component, event);
3073}
3074
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003075static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3076 enum snd_soc_bias_level level)
3077{
3078 struct snd_soc_component *component = dapm->component;
3079
3080 return component->driver->set_bias_level(component, level);
3081}
3082
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003083static int snd_soc_component_initialize(struct snd_soc_component *component,
3084 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003085{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003086 struct snd_soc_dapm_context *dapm;
3087
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003088 component->name = fmt_single_name(dev, &component->id);
3089 if (!component->name) {
3090 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003091 return -ENOMEM;
3092 }
3093
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003094 component->dev = dev;
3095 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003096
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003097 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003098 dapm->dev = dev;
3099 dapm->component = component;
3100 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003101 dapm->idle_bias_off = !driver->idle_bias_on;
3102 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003103 if (driver->seq_notifier)
3104 dapm->seq_notifier = snd_soc_component_seq_notifier;
3105 if (driver->stream_event)
3106 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003107 if (driver->set_bias_level)
3108 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003109
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003110 INIT_LIST_HEAD(&component->dai_list);
3111 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003112
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003113 return 0;
3114}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003115
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003116static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003117{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003118 int val_bytes = regmap_get_val_bytes(component->regmap);
3119
3120 /* Errors are legitimate for non-integer byte multiples */
3121 if (val_bytes > 0)
3122 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003123}
3124
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003125#ifdef CONFIG_REGMAP
3126
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003127/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003128 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3129 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003130 * @component: The component for which to initialize the regmap instance
3131 * @regmap: The regmap instance that should be used by the component
3132 *
3133 * This function allows deferred assignment of the regmap instance that is
3134 * associated with the component. Only use this if the regmap instance is not
3135 * yet ready when the component is registered. The function must also be called
3136 * before the first IO attempt of the component.
3137 */
3138void snd_soc_component_init_regmap(struct snd_soc_component *component,
3139 struct regmap *regmap)
3140{
3141 component->regmap = regmap;
3142 snd_soc_component_setup_regmap(component);
3143}
3144EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3145
3146/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003147 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3148 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003149 * @component: The component for which to de-initialize the regmap instance
3150 *
3151 * Calls regmap_exit() on the regmap instance associated to the component and
3152 * removes the regmap instance from the component.
3153 *
3154 * This function should only be used if snd_soc_component_init_regmap() was used
3155 * to initialize the regmap instance.
3156 */
3157void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3158{
3159 regmap_exit(component->regmap);
3160 component->regmap = NULL;
3161}
3162EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3163
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003164#endif
3165
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003166static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003167{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003168 mutex_lock(&client_mutex);
3169
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003170 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003171 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003172 component->regmap = dev_get_regmap(component->dev,
3173 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003174 if (component->regmap)
3175 snd_soc_component_setup_regmap(component);
3176 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003177
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003178 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003179 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003180 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003181
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003182 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003183}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003184
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003185static void snd_soc_component_cleanup(struct snd_soc_component *component)
3186{
3187 snd_soc_unregister_dais(component);
3188 kfree(component->name);
3189}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003190
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003191static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3192{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003193 struct snd_soc_card *card = component->card;
3194
3195 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003196 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003197
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003198 list_del(&component->list);
3199}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003200
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003201#define ENDIANNESS_MAP(name) \
3202 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3203static u64 endianness_format_map[] = {
3204 ENDIANNESS_MAP(S16_),
3205 ENDIANNESS_MAP(U16_),
3206 ENDIANNESS_MAP(S24_),
3207 ENDIANNESS_MAP(U24_),
3208 ENDIANNESS_MAP(S32_),
3209 ENDIANNESS_MAP(U32_),
3210 ENDIANNESS_MAP(S24_3),
3211 ENDIANNESS_MAP(U24_3),
3212 ENDIANNESS_MAP(S20_3),
3213 ENDIANNESS_MAP(U20_3),
3214 ENDIANNESS_MAP(S18_3),
3215 ENDIANNESS_MAP(U18_3),
3216 ENDIANNESS_MAP(FLOAT_),
3217 ENDIANNESS_MAP(FLOAT64_),
3218 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3219};
3220
3221/*
3222 * Fix up the DAI formats for endianness: codecs don't actually see
3223 * the endianness of the data but we're using the CPU format
3224 * definitions which do need to include endianness so we ensure that
3225 * codec DAIs always have both big and little endian variants set.
3226 */
3227static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3228{
3229 int i;
3230
3231 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3232 if (stream->formats & endianness_format_map[i])
3233 stream->formats |= endianness_format_map[i];
3234}
3235
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003236static void snd_soc_try_rebind_card(void)
3237{
3238 struct snd_soc_card *card, *c;
3239
3240 if (!list_empty(&unbind_card_list)) {
3241 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3242 if (!snd_soc_bind_card(card))
3243 list_del(&card->list);
3244 }
3245 }
3246}
3247
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003248int snd_soc_add_component(struct device *dev,
3249 struct snd_soc_component *component,
3250 const struct snd_soc_component_driver *component_driver,
3251 struct snd_soc_dai_driver *dai_drv,
3252 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003253{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003254 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003255 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003256
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003257 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003258 if (ret)
3259 goto err_free;
3260
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003261 if (component_driver->endianness) {
3262 for (i = 0; i < num_dai; i++) {
3263 convert_endianness_formats(&dai_drv[i].playback);
3264 convert_endianness_formats(&dai_drv[i].capture);
3265 }
3266 }
3267
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003268 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003269 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003270 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003271 goto err_cleanup;
3272 }
3273
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003274 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003275 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003276
3277 return 0;
3278
3279err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003280 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003281err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003282 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003283}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003284EXPORT_SYMBOL_GPL(snd_soc_add_component);
3285
3286int snd_soc_register_component(struct device *dev,
3287 const struct snd_soc_component_driver *component_driver,
3288 struct snd_soc_dai_driver *dai_drv,
3289 int num_dai)
3290{
3291 struct snd_soc_component *component;
3292
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003293 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003294 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003295 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003296
3297 return snd_soc_add_component(dev, component, component_driver,
3298 dai_drv, num_dai);
3299}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003300EXPORT_SYMBOL_GPL(snd_soc_register_component);
3301
3302/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003303 * snd_soc_unregister_component - Unregister all related component
3304 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003305 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003306 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003307 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003308static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003309{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003310 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003311 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003312
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003313 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003314 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003315 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003316 continue;
3317
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003318 snd_soc_tplg_component_remove(component,
3319 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003320 snd_soc_component_del_unlocked(component);
3321 found = 1;
3322 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003323 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003324 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003325
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003326 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003327 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003328
3329 return found;
3330}
3331
3332void snd_soc_unregister_component(struct device *dev)
3333{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003334 while (__snd_soc_unregister_component(dev))
3335 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003336}
3337EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3338
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003339struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3340 const char *driver_name)
3341{
3342 struct snd_soc_component *component;
3343 struct snd_soc_component *ret;
3344
3345 ret = NULL;
3346 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003347 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003348 if (dev != component->dev)
3349 continue;
3350
3351 if (driver_name &&
3352 (driver_name != component->driver->name) &&
3353 (strcmp(component->driver->name, driver_name) != 0))
3354 continue;
3355
3356 ret = component;
3357 break;
3358 }
3359 mutex_unlock(&client_mutex);
3360
3361 return ret;
3362}
3363EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3364
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003365/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003366int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3367 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003368{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003369 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003370 int ret;
3371
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303372 if (!card->dev) {
3373 pr_err("card->dev is not set before calling %s\n", __func__);
3374 return -EINVAL;
3375 }
3376
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003377 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303378
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003379 ret = of_property_read_string_index(np, propname, 0, &card->name);
3380 /*
3381 * EINVAL means the property does not exist. This is fine providing
3382 * card->name was previously set, which is checked later in
3383 * snd_soc_register_card.
3384 */
3385 if (ret < 0 && ret != -EINVAL) {
3386 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003387 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003388 propname, ret);
3389 return ret;
3390 }
3391
3392 return 0;
3393}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003394EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003395
Xiubo Li9a6d4862014-02-08 15:59:52 +08003396static const struct snd_soc_dapm_widget simple_widgets[] = {
3397 SND_SOC_DAPM_MIC("Microphone", NULL),
3398 SND_SOC_DAPM_LINE("Line", NULL),
3399 SND_SOC_DAPM_HP("Headphone", NULL),
3400 SND_SOC_DAPM_SPK("Speaker", NULL),
3401};
3402
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003403int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003404 const char *propname)
3405{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003406 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003407 struct snd_soc_dapm_widget *widgets;
3408 const char *template, *wname;
3409 int i, j, num_widgets, ret;
3410
3411 num_widgets = of_property_count_strings(np, propname);
3412 if (num_widgets < 0) {
3413 dev_err(card->dev,
3414 "ASoC: Property '%s' does not exist\n", propname);
3415 return -EINVAL;
3416 }
3417 if (num_widgets & 1) {
3418 dev_err(card->dev,
3419 "ASoC: Property '%s' length is not even\n", propname);
3420 return -EINVAL;
3421 }
3422
3423 num_widgets /= 2;
3424 if (!num_widgets) {
3425 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3426 propname);
3427 return -EINVAL;
3428 }
3429
3430 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3431 GFP_KERNEL);
3432 if (!widgets) {
3433 dev_err(card->dev,
3434 "ASoC: Could not allocate memory for widgets\n");
3435 return -ENOMEM;
3436 }
3437
3438 for (i = 0; i < num_widgets; i++) {
3439 ret = of_property_read_string_index(np, propname,
3440 2 * i, &template);
3441 if (ret) {
3442 dev_err(card->dev,
3443 "ASoC: Property '%s' index %d read error:%d\n",
3444 propname, 2 * i, ret);
3445 return -EINVAL;
3446 }
3447
3448 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3449 if (!strncmp(template, simple_widgets[j].name,
3450 strlen(simple_widgets[j].name))) {
3451 widgets[i] = simple_widgets[j];
3452 break;
3453 }
3454 }
3455
3456 if (j >= ARRAY_SIZE(simple_widgets)) {
3457 dev_err(card->dev,
3458 "ASoC: DAPM widget '%s' is not supported\n",
3459 template);
3460 return -EINVAL;
3461 }
3462
3463 ret = of_property_read_string_index(np, propname,
3464 (2 * i) + 1,
3465 &wname);
3466 if (ret) {
3467 dev_err(card->dev,
3468 "ASoC: Property '%s' index %d read error:%d\n",
3469 propname, (2 * i) + 1, ret);
3470 return -EINVAL;
3471 }
3472
3473 widgets[i].name = wname;
3474 }
3475
Nicolin Chenf23e8602015-02-14 17:22:49 -08003476 card->of_dapm_widgets = widgets;
3477 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003478
3479 return 0;
3480}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003481EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003482
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003483int snd_soc_of_get_slot_mask(struct device_node *np,
3484 const char *prop_name,
3485 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003486{
3487 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003488 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003489 int i;
3490
3491 if (!of_slot_mask)
3492 return 0;
3493 val /= sizeof(u32);
3494 for (i = 0; i < val; i++)
3495 if (be32_to_cpup(&of_slot_mask[i]))
3496 *mask |= (1 << i);
3497
3498 return val;
3499}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003500EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003501
Xiubo Li89c67852014-02-14 09:34:35 +08003502int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003503 unsigned int *tx_mask,
3504 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003505 unsigned int *slots,
3506 unsigned int *slot_width)
3507{
3508 u32 val;
3509 int ret;
3510
Jyri Sarha61310842015-09-09 21:27:43 +03003511 if (tx_mask)
3512 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3513 if (rx_mask)
3514 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3515
Xiubo Li89c67852014-02-14 09:34:35 +08003516 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3517 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3518 if (ret)
3519 return ret;
3520
3521 if (slots)
3522 *slots = val;
3523 }
3524
3525 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3526 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3527 if (ret)
3528 return ret;
3529
3530 if (slot_width)
3531 *slot_width = val;
3532 }
3533
3534 return 0;
3535}
3536EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3537
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003538void snd_soc_of_parse_node_prefix(struct device_node *np,
3539 struct snd_soc_codec_conf *codec_conf,
3540 struct device_node *of_node,
3541 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003542{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003543 const char *str;
3544 int ret;
3545
3546 ret = of_property_read_string(np, propname, &str);
3547 if (ret < 0) {
3548 /* no prefix is not error */
3549 return;
3550 }
3551
3552 codec_conf->of_node = of_node;
3553 codec_conf->name_prefix = str;
3554}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003555EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003556
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003557int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003558 const char *propname)
3559{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003560 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003561 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003562 struct snd_soc_dapm_route *routes;
3563 int i, ret;
3564
3565 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003566 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003567 dev_err(card->dev,
3568 "ASoC: Property '%s' does not exist or its length is not even\n",
3569 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003570 return -EINVAL;
3571 }
3572 num_routes /= 2;
3573 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003574 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003575 propname);
3576 return -EINVAL;
3577 }
3578
Kees Cooka86854d2018-06-12 14:07:58 -07003579 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003580 GFP_KERNEL);
3581 if (!routes) {
3582 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003583 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003584 return -EINVAL;
3585 }
3586
3587 for (i = 0; i < num_routes; i++) {
3588 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003589 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003590 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003591 dev_err(card->dev,
3592 "ASoC: Property '%s' index %d could not be read: %d\n",
3593 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003594 return -EINVAL;
3595 }
3596 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003597 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003598 if (ret) {
3599 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003600 "ASoC: Property '%s' index %d could not be read: %d\n",
3601 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003602 return -EINVAL;
3603 }
3604 }
3605
Nicolin Chenf23e8602015-02-14 17:22:49 -08003606 card->num_of_dapm_routes = num_routes;
3607 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003608
3609 return 0;
3610}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003611EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003612
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003613unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003614 const char *prefix,
3615 struct device_node **bitclkmaster,
3616 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003617{
3618 int ret, i;
3619 char prop[128];
3620 unsigned int format = 0;
3621 int bit, frame;
3622 const char *str;
3623 struct {
3624 char *name;
3625 unsigned int val;
3626 } of_fmt_table[] = {
3627 { "i2s", SND_SOC_DAIFMT_I2S },
3628 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3629 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3630 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3631 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3632 { "ac97", SND_SOC_DAIFMT_AC97 },
3633 { "pdm", SND_SOC_DAIFMT_PDM},
3634 { "msb", SND_SOC_DAIFMT_MSB },
3635 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003636 };
3637
3638 if (!prefix)
3639 prefix = "";
3640
3641 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003642 * check "dai-format = xxx"
3643 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003644 * SND_SOC_DAIFMT_FORMAT_MASK area
3645 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003646 ret = of_property_read_string(np, "dai-format", &str);
3647 if (ret < 0) {
3648 snprintf(prop, sizeof(prop), "%sformat", prefix);
3649 ret = of_property_read_string(np, prop, &str);
3650 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003651 if (ret == 0) {
3652 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3653 if (strcmp(str, of_fmt_table[i].name) == 0) {
3654 format |= of_fmt_table[i].val;
3655 break;
3656 }
3657 }
3658 }
3659
3660 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003661 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003662 * SND_SOC_DAIFMT_CLOCK_MASK area
3663 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003664 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003665 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003666 format |= SND_SOC_DAIFMT_CONT;
3667 else
3668 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003669
3670 /*
3671 * check "[prefix]bitclock-inversion"
3672 * check "[prefix]frame-inversion"
3673 * SND_SOC_DAIFMT_INV_MASK area
3674 */
3675 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3676 bit = !!of_get_property(np, prop, NULL);
3677
3678 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3679 frame = !!of_get_property(np, prop, NULL);
3680
3681 switch ((bit << 4) + frame) {
3682 case 0x11:
3683 format |= SND_SOC_DAIFMT_IB_IF;
3684 break;
3685 case 0x10:
3686 format |= SND_SOC_DAIFMT_IB_NF;
3687 break;
3688 case 0x01:
3689 format |= SND_SOC_DAIFMT_NB_IF;
3690 break;
3691 default:
3692 /* SND_SOC_DAIFMT_NB_NF is default */
3693 break;
3694 }
3695
3696 /*
3697 * check "[prefix]bitclock-master"
3698 * check "[prefix]frame-master"
3699 * SND_SOC_DAIFMT_MASTER_MASK area
3700 */
3701 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3702 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003703 if (bit && bitclkmaster)
3704 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003705
3706 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3707 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003708 if (frame && framemaster)
3709 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003710
3711 switch ((bit << 4) + frame) {
3712 case 0x11:
3713 format |= SND_SOC_DAIFMT_CBM_CFM;
3714 break;
3715 case 0x10:
3716 format |= SND_SOC_DAIFMT_CBM_CFS;
3717 break;
3718 case 0x01:
3719 format |= SND_SOC_DAIFMT_CBS_CFM;
3720 break;
3721 default:
3722 format |= SND_SOC_DAIFMT_CBS_CFS;
3723 break;
3724 }
3725
3726 return format;
3727}
3728EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3729
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003730int snd_soc_get_dai_id(struct device_node *ep)
3731{
3732 struct snd_soc_component *pos;
3733 struct device_node *node;
3734 int ret;
3735
3736 node = of_graph_get_port_parent(ep);
3737
3738 /*
3739 * For example HDMI case, HDMI has video/sound port,
3740 * but ALSA SoC needs sound port number only.
3741 * Thus counting HDMI DT port/endpoint doesn't work.
3742 * Then, it should have .of_xlate_dai_id
3743 */
3744 ret = -ENOTSUPP;
3745 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003746 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003747 struct device_node *component_of_node = pos->dev->of_node;
3748
3749 if (!component_of_node && pos->dev->parent)
3750 component_of_node = pos->dev->parent->of_node;
3751
3752 if (component_of_node != node)
3753 continue;
3754
3755 if (pos->driver->of_xlate_dai_id)
3756 ret = pos->driver->of_xlate_dai_id(pos, ep);
3757
3758 break;
3759 }
3760 mutex_unlock(&client_mutex);
3761
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003762 of_node_put(node);
3763
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003764 return ret;
3765}
3766EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3767
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003768int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003769 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003770{
3771 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003772 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003773 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003774
3775 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003776 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003777 component_of_node = pos->dev->of_node;
3778 if (!component_of_node && pos->dev->parent)
3779 component_of_node = pos->dev->parent->of_node;
3780
3781 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003782 continue;
3783
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003784 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003785 ret = pos->driver->of_xlate_dai_name(pos,
3786 args,
3787 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003788 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003789 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003790 int id = -1;
3791
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003792 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003793 case 0:
3794 id = 0; /* same as dai_drv[0] */
3795 break;
3796 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003797 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003798 break;
3799 default:
3800 /* not supported */
3801 break;
3802 }
3803
3804 if (id < 0 || id >= pos->num_dai) {
3805 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003806 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003807 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003808
3809 ret = 0;
3810
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003811 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003812 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003813 if (id == 0)
3814 break;
3815 id--;
3816 }
3817
3818 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003819 if (!*dai_name)
3820 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003821 }
3822
Kuninori Morimotocb470082013-09-10 17:39:56 -07003823 break;
3824 }
3825 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003826 return ret;
3827}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003828EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003829
3830int snd_soc_of_get_dai_name(struct device_node *of_node,
3831 const char **dai_name)
3832{
3833 struct of_phandle_args args;
3834 int ret;
3835
3836 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3837 "#sound-dai-cells", 0, &args);
3838 if (ret)
3839 return ret;
3840
3841 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003842
3843 of_node_put(args.np);
3844
3845 return ret;
3846}
3847EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3848
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003849/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003850 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3851 * @dai_link: DAI link
3852 *
3853 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3854 */
3855void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3856{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003857 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003858 int index;
3859
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003860 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003861 if (!component->of_node)
3862 break;
3863 of_node_put(component->of_node);
3864 component->of_node = NULL;
3865 }
3866}
3867EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3868
3869/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003870 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3871 * @dev: Card device
3872 * @of_node: Device node
3873 * @dai_link: DAI link
3874 *
3875 * Builds an array of CODEC DAI components from the DAI link property
3876 * 'sound-dai'.
3877 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003878 * The device nodes in the array (of_node) must be dereferenced by calling
3879 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003880 *
3881 * Returns 0 for success
3882 */
3883int snd_soc_of_get_dai_link_codecs(struct device *dev,
3884 struct device_node *of_node,
3885 struct snd_soc_dai_link *dai_link)
3886{
3887 struct of_phandle_args args;
3888 struct snd_soc_dai_link_component *component;
3889 char *name;
3890 int index, num_codecs, ret;
3891
3892 /* Count the number of CODECs */
3893 name = "sound-dai";
3894 num_codecs = of_count_phandle_with_args(of_node, name,
3895 "#sound-dai-cells");
3896 if (num_codecs <= 0) {
3897 if (num_codecs == -ENOENT)
3898 dev_err(dev, "No 'sound-dai' property\n");
3899 else
3900 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3901 return num_codecs;
3902 }
Kees Cooka86854d2018-06-12 14:07:58 -07003903 component = devm_kcalloc(dev,
3904 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003905 GFP_KERNEL);
3906 if (!component)
3907 return -ENOMEM;
3908 dai_link->codecs = component;
3909 dai_link->num_codecs = num_codecs;
3910
3911 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003912 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003913 ret = of_parse_phandle_with_args(of_node, name,
3914 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003915 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003916 if (ret)
3917 goto err;
3918 component->of_node = args.np;
3919 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3920 if (ret < 0)
3921 goto err;
3922 }
3923 return 0;
3924err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003925 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003926 dai_link->codecs = NULL;
3927 dai_link->num_codecs = 0;
3928 return ret;
3929}
3930EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3931
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003932static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003933{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003934 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003935 snd_soc_util_init();
3936
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003937 return platform_driver_register(&soc_driver);
3938}
Mark Brown4abe8e12010-10-12 17:41:03 +01003939module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003940
Mark Brown7d8c16a2008-11-30 22:11:24 +00003941static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003942{
Mark Brownfb257892011-04-28 10:57:54 +01003943 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003944 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003945
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003946 platform_driver_unregister(&soc_driver);
3947}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003948module_exit(snd_soc_exit);
3949
3950/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003951MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003952MODULE_DESCRIPTION("ALSA SoC Core");
3953MODULE_LICENSE("GPL");
3954MODULE_ALIAS("platform:soc-audio");