blob: b29d0f65611eb536ded7aaa3e41416ca600c2bde [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
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100428static void codec2codec_close_delayed_work(struct work_struct *work)
429{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200430 /*
431 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100432 * Since c2c links are internal nodes in the DAPM graph and
433 * don't interface with the outside world or application layer
434 * we don't have to do any special handling on close.
435 */
436}
437
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000438#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000440int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200441{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000442 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000443 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500444 struct snd_soc_pcm_runtime *rtd;
445 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200447 /* If the card is not initialized yet there is nothing to do */
448 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200449 return 0;
450
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200451 /*
452 * Due to the resume being scheduled into a workqueue we could
453 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100454 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100456
457 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100459
Mark Browna00f90f2010-12-02 16:24:24 +0000460 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000461 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000462 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100463
Mengdong Lin1a497982015-11-18 02:34:11 -0500464 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100465 continue;
466
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000467 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200468 struct snd_soc_dai_driver *drv = dai->driver;
469
470 if (drv->ops->digital_mute && dai->playback_active)
471 drv->ops->digital_mute(dai, 1);
472 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 }
474
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100475 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000476 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500477 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100478 continue;
479
Mengdong Lin1a497982015-11-18 02:34:11 -0500480 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100481 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100482
Mark Brown87506542008-11-18 20:50:34 +0000483 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000484 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000486 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100488
Mengdong Lin1a497982015-11-18 02:34:11 -0500489 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100490 continue;
491
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100492 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100494 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200496 /* close any waiting streams */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000497 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100499
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000500 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501
Mengdong Lin1a497982015-11-18 02:34:11 -0500502 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100503 continue;
504
Mengdong Lin1a497982015-11-18 02:34:11 -0500505 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800506 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800507 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800510 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800511 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 }
513
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200514 /* Recheck all endpoints too, their state is affected by suspend */
515 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700516 snd_soc_dapm_sync(&card->dapm);
517
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000518 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000519 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200520 struct snd_soc_dapm_context *dapm =
521 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000522
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200523 /*
524 * If there are paths active then the COMPONENT will be held
525 * with bias _ON and should not be suspended.
526 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000527 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200528 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000530 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000531 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000532 * bias off then being in STANDBY
533 * means it's doing something,
534 * otherwise fall through.
535 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200536 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000537 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200538 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000539 break;
540 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500541 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200542
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000544 if (component->driver->suspend)
545 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000546 component->suspended = 1;
547 if (component->regmap)
548 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800549 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000550 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 break;
552 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000553 dev_dbg(component->dev,
554 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 break;
556 }
557 }
558 }
559
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000560 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500561 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562
Mengdong Lin1a497982015-11-18 02:34:11 -0500563 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 continue;
565
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100566 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800568
569 /* deactivate pins to sleep state */
570 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571 }
572
Mark Brown87506542008-11-18 20:50:34 +0000573 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000574 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
576 return 0;
577}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000578EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200580/*
581 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100582 * setting our codec back up, which can be very slow on I2C
583 */
584static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200587 container_of(work, struct snd_soc_card,
588 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500589 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000590 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500591 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200593 /*
594 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100595 * so userspace apps are blocked from touching us
596 */
597
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000598 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100599
Mark Brown9949788b2010-05-07 20:24:05 +0100600 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100602
Mark Brown87506542008-11-18 20:50:34 +0000603 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000604 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100606 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000607 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500608 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100609
Mengdong Lin1a497982015-11-18 02:34:11 -0500610 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100611 continue;
612
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100613 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 }
616
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000617 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000618 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000619 if (component->driver->resume)
620 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000621 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100622 }
623 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000625 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100626
Mengdong Lin1a497982015-11-18 02:34:11 -0500627 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100628 continue;
629
Mengdong Lin1a497982015-11-18 02:34:11 -0500630 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000631 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800632 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633
Mengdong Lin1a497982015-11-18 02:34:11 -0500634 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000635 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800636 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 }
638
Mark Brown3ff3f642008-05-19 12:32:25 +0200639 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000640 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000641 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100642
Mengdong Lin1a497982015-11-18 02:34:11 -0500643 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100644 continue;
645
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000646 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200647 struct snd_soc_dai_driver *drv = dai->driver;
648
649 if (drv->ops->digital_mute && dai->playback_active)
650 drv->ops->digital_mute(dai, 0);
651 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 }
653
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000654 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100656
Mengdong Lin1a497982015-11-18 02:34:11 -0500657 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100658 continue;
659
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100660 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Mark Brown87506542008-11-18 20:50:34 +0000664 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000665 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000667 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100668
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200669 /* Recheck all endpoints too, their state is affected by suspend */
670 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700671 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530672
673 /* userspace can access us now we are back as we were before */
674 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100675}
676
677/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000678int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100679{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000680 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100681 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500682 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200683
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200684 /* If the card is not initialized yet there is nothing to do */
685 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800686 return 0;
687
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800688 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000689 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000690 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 int j;
693
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800694 if (cpu_dai->active)
695 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200696
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000697 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200698 if (codec_dai->active)
699 pinctrl_pm_select_default_state(codec_dai->dev);
700 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800701 }
702
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100703 /*
704 * DAIs that also act as the control bus master might have other drivers
705 * hanging off them so need to resume immediately. Other drivers don't
706 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100707 * due to I/O costs and anti-pop so handle them out of line.
708 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000709 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200711
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100712 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600713 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100714 if (bus_control) {
715 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600716 soc_resume_deferred(&card->deferred_resume_work);
717 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000718 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600719 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000720 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100721 }
Andy Green6ed25972008-06-13 16:24:05 +0100722
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 return 0;
724}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000725EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000727#define snd_soc_suspend NULL
728#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729#endif
730
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100731static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800732};
733
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200734static struct snd_soc_component *soc_find_component(
735 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100736{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200737 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100738
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100739 lockdep_assert_held(&client_mutex);
740
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000741 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200742 if (of_node) {
743 if (component->dev->of_node == of_node)
744 return component;
745 } else if (strcmp(component->name, name) == 0) {
746 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100747 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100748 }
749
750 return NULL;
751}
752
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000753static int snd_soc_is_matching_component(
754 const struct snd_soc_dai_link_component *dlc,
755 struct snd_soc_component *component)
756{
757 struct device_node *component_of_node;
758
759 component_of_node = component->dev->of_node;
760 if (!component_of_node && component->dev->parent)
761 component_of_node = component->dev->parent->of_node;
762
763 if (dlc->of_node && component_of_node != dlc->of_node)
764 return 0;
765 if (dlc->name && strcmp(component->name, dlc->name))
766 return 0;
767
768 return 1;
769}
770
Mengdong Linfbb88b52016-04-22 12:25:33 +0800771/**
772 * snd_soc_find_dai - Find a registered DAI
773 *
Jeffy Chen49584712017-08-22 15:57:21 +0800774 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800775 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700776 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800777 * find the DAI of the same name. The component's of_node and name
778 * should also match if being specified.
779 *
780 * Return: pointer of DAI, or NULL if not found.
781 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800782struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200783 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100784{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200785 struct snd_soc_component *component;
786 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100787
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100788 lockdep_assert_held(&client_mutex);
789
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200790 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000791 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000792 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200793 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000794 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800795 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800796 && (!dai->driver->name
797 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100798 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100799
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200800 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100801 }
802 }
803
804 return NULL;
805}
Mengdong Lin305e9022016-04-19 13:12:25 +0800806EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100807
Mengdong Lin17fb17552016-11-03 01:04:12 +0800808/**
809 * snd_soc_find_dai_link - Find a DAI link
810 *
811 * @card: soc card
812 * @id: DAI link ID to match
813 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000814 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800815 *
816 * This function will search all existing DAI links of the soc card to
817 * find the link of the same ID. Since DAI links may not have their
818 * unique ID, so name and stream name should also match if being
819 * specified.
820 *
821 * Return: pointer of DAI link, or NULL if not found.
822 */
823struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
824 int id, const char *name,
825 const char *stream_name)
826{
827 struct snd_soc_dai_link *link, *_link;
828
829 lockdep_assert_held(&client_mutex);
830
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000831 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800832 if (link->id != id)
833 continue;
834
835 if (name && (!link->name || strcmp(name, link->name)))
836 continue;
837
838 if (stream_name && (!link->stream_name
839 || strcmp(stream_name, link->stream_name)))
840 continue;
841
842 return link;
843 }
844
845 return NULL;
846}
847EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
848
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800849static bool soc_is_dai_link_bound(struct snd_soc_card *card,
850 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800852 struct snd_soc_pcm_runtime *rtd;
853
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000854 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800855 if (rtd->dai_link == dai_link)
856 return true;
857 }
858
859 return false;
860}
861
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500862static int soc_bind_dai_link(struct snd_soc_card *card,
863 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864{
Mengdong Lin1a497982015-11-18 02:34:11 -0500865 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200866 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200867 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000868 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500869 struct snd_soc_dai **codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200870 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871
Liam Girdwooda655de82018-07-02 16:59:54 +0100872 if (dai_link->ignore)
873 return 0;
874
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500875 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000876
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800877 if (soc_is_dai_link_bound(card, dai_link)) {
878 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
879 dai_link->name);
880 return 0;
881 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530883 rtd = soc_new_pcm_runtime(card, dai_link);
884 if (!rtd)
885 return -ENOMEM;
886
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200887 cpu_dai_component.name = dai_link->cpu_name;
888 cpu_dai_component.of_node = dai_link->cpu_of_node;
889 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
890 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000891 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100892 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
893 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500894 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000895 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000896 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000897
Benoit Cousson88bd8702014-07-08 23:19:34 +0200898 rtd->num_codecs = dai_link->num_codecs;
899
900 /* Find CODEC from registered CODECs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000901 /* we can use for_each_rtd_codec_dai() after this */
Mengdong Lin1a497982015-11-18 02:34:11 -0500902 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200904 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 if (!codec_dais[i]) {
906 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
907 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500908 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200909 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000910 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100912
Benoit Cousson88bd8702014-07-08 23:19:34 +0200913 /* Single codec links expect codec and codec_dai in runtime data */
914 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100915
Mark Brownb19e6e72012-03-14 21:18:39 +0000916 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000917 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000918 if (!snd_soc_is_matching_component(dai_link->platform,
919 component))
920 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000921
922 snd_soc_rtdcom_add(rtd, component);
923 }
924
Mengdong Lin1a497982015-11-18 02:34:11 -0500925 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000926 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500927
928_err_defer:
929 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200930 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931}
932
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200933static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600934{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200935 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200936 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600937
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000938 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600939
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000940 if (component->driver->remove)
941 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600942
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200943 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600944
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200945 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200946 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200947 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600948}
949
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200950static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200951{
952 int err;
953
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000954 if (!dai || !dai->probed ||
955 dai->driver->remove_order != order)
956 return;
957
958 if (dai->driver->remove) {
959 err = dai->driver->remove(dai);
960 if (err < 0)
961 dev_err(dai->dev,
962 "ASoC: failed to remove %s: %d\n",
963 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100964 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000965 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100966}
967
Mengdong Lin1a497982015-11-18 02:34:11 -0500968static void soc_remove_link_dais(struct snd_soc_card *card,
969 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000972 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
974 /* unregister the rtd device */
975 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800976 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977 rtd->dev_registered = 0;
978 }
979
980 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000981 for_each_rtd_codec_dai(rtd, i, codec_dai)
982 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200984 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985}
986
Mengdong Lin1a497982015-11-18 02:34:11 -0500987static void soc_remove_link_components(struct snd_soc_card *card,
988 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200990 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000991 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600992
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000993 for_each_rtdcom(rtd, rtdcom) {
994 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600995
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200996 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200997 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999}
1000
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001001static void soc_remove_dai_links(struct snd_soc_card *card)
1002{
Mengdong Lin1a497982015-11-18 02:34:11 -05001003 int order;
1004 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001005 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001006
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001007 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001008 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001009 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001010 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001011
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001012 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001013 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001014 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001015 }
1016
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001017 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001018 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1020 link->name);
1021
1022 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001023 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001024}
1025
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001026static int snd_soc_init_platform(struct snd_soc_card *card,
1027 struct snd_soc_dai_link *dai_link)
1028{
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001029 struct snd_soc_dai_link_component *platform = dai_link->platform;
1030
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001031 /*
1032 * FIXME
1033 *
1034 * this function should be removed in the future
1035 */
1036 /* convert Legacy platform link */
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001037 if (!platform) {
1038 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001039 sizeof(struct snd_soc_dai_link_component),
1040 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001041 if (!platform)
1042 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001043
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001044 dai_link->platform = platform;
1045 platform->name = dai_link->platform_name;
1046 platform->of_node = dai_link->platform_of_node;
1047 platform->dai_name = NULL;
1048 }
1049
1050 /* if there's no platform we match on the empty platform */
1051 if (!platform->name &&
1052 !platform->of_node)
1053 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001054
1055 return 0;
1056}
1057
Mengdong Lin923c5e612015-11-23 11:01:49 -05001058static int snd_soc_init_multicodec(struct snd_soc_card *card,
1059 struct snd_soc_dai_link *dai_link)
1060{
1061 /* Legacy codec/codec_dai link is a single entry in multicodec */
1062 if (dai_link->codec_name || dai_link->codec_of_node ||
1063 dai_link->codec_dai_name) {
1064 dai_link->num_codecs = 1;
1065
1066 dai_link->codecs = devm_kzalloc(card->dev,
1067 sizeof(struct snd_soc_dai_link_component),
1068 GFP_KERNEL);
1069 if (!dai_link->codecs)
1070 return -ENOMEM;
1071
1072 dai_link->codecs[0].name = dai_link->codec_name;
1073 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1074 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1075 }
1076
1077 if (!dai_link->codecs) {
1078 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1079 return -EINVAL;
1080 }
1081
1082 return 0;
1083}
1084
1085static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001086 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001087{
1088 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001089 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001090
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001091 ret = snd_soc_init_platform(card, link);
1092 if (ret) {
1093 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1094 return ret;
1095 }
1096
Mengdong Lin923c5e612015-11-23 11:01:49 -05001097 ret = snd_soc_init_multicodec(card, link);
1098 if (ret) {
1099 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1100 return ret;
1101 }
1102
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001103 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001104 /*
1105 * Codec must be specified by 1 of name or OF node,
1106 * not both or neither.
1107 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001108 if (!!codec->name ==
1109 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001110 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1111 link->name);
1112 return -EINVAL;
1113 }
1114 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001115 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001116 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1117 link->name);
1118 return -EINVAL;
1119 }
1120 }
1121
1122 /*
1123 * Platform may be specified by either name or OF node, but
1124 * can be left unspecified, and a dummy platform will be used.
1125 */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001126 if (link->platform->name && link->platform->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001127 dev_err(card->dev,
1128 "ASoC: Both platform name/of_node are set for %s\n",
1129 link->name);
1130 return -EINVAL;
1131 }
Mengdong Lin923c5e612015-11-23 11:01:49 -05001132 /*
1133 * CPU device may be specified by either name or OF node, but
1134 * can be left unspecified, and will be matched based on DAI
1135 * name alone..
1136 */
1137 if (link->cpu_name && link->cpu_of_node) {
1138 dev_err(card->dev,
1139 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1140 link->name);
1141 return -EINVAL;
1142 }
1143 /*
1144 * At least one of CPU DAI name or CPU device name/node must be
1145 * specified
1146 */
1147 if (!link->cpu_dai_name &&
1148 !(link->cpu_name || link->cpu_of_node)) {
1149 dev_err(card->dev,
1150 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1151 link->name);
1152 return -EINVAL;
1153 }
1154
1155 return 0;
1156}
1157
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001158void snd_soc_disconnect_sync(struct device *dev)
1159{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001160 struct snd_soc_component *component =
1161 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001162
1163 if (!component || !component->card)
1164 return;
1165
1166 snd_card_disconnect_sync(component->card->snd_card);
1167}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001168EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001169
Mengdong Linf8f80362015-12-02 14:11:22 +08001170/**
1171 * snd_soc_add_dai_link - Add a DAI link dynamically
1172 * @card: The ASoC card to which the DAI link is added
1173 * @dai_link: The new DAI link to add
1174 *
1175 * This function adds a DAI link to the ASoC card's link list.
1176 *
1177 * Note: Topology can use this API to add DAI links when probing the
1178 * topology component. And machine drivers can still define static
1179 * DAI links in dai_link array.
1180 */
1181int snd_soc_add_dai_link(struct snd_soc_card *card,
1182 struct snd_soc_dai_link *dai_link)
1183{
1184 if (dai_link->dobj.type
1185 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1186 dev_err(card->dev, "Invalid dai link type %d\n",
1187 dai_link->dobj.type);
1188 return -EINVAL;
1189 }
1190
1191 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001192 /*
1193 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001194 * on the link created by topology.
1195 */
1196 if (dai_link->dobj.type && card->add_dai_link)
1197 card->add_dai_link(card, dai_link);
1198
Mengdong Linf8f80362015-12-02 14:11:22 +08001199 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001200
1201 return 0;
1202}
1203EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1204
1205/**
1206 * snd_soc_remove_dai_link - Remove a DAI link from the list
1207 * @card: The ASoC card that owns the link
1208 * @dai_link: The DAI link to remove
1209 *
1210 * This function removes a DAI link from the ASoC card's link list.
1211 *
1212 * For DAI links previously added by topology, topology should
1213 * remove them by using the dobj embedded in the link.
1214 */
1215void snd_soc_remove_dai_link(struct snd_soc_card *card,
1216 struct snd_soc_dai_link *dai_link)
1217{
1218 struct snd_soc_dai_link *link, *_link;
1219
1220 if (dai_link->dobj.type
1221 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1222 dev_err(card->dev, "Invalid dai link type %d\n",
1223 dai_link->dobj.type);
1224 return;
1225 }
1226
1227 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001228 /*
1229 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001230 * on the link created by topology.
1231 */
1232 if (dai_link->dobj.type && card->remove_dai_link)
1233 card->remove_dai_link(card, dai_link);
1234
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001235 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001236 if (link == dai_link) {
1237 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001238 return;
1239 }
1240 }
1241}
1242EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1243
Jerome Brunetaefba452018-07-13 14:50:43 +02001244static void soc_set_of_name_prefix(struct snd_soc_component *component)
1245{
1246 struct device_node *component_of_node = component->dev->of_node;
1247 const char *str;
1248 int ret;
1249
1250 if (!component_of_node && component->dev->parent)
1251 component_of_node = component->dev->parent->of_node;
1252
1253 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1254 &str);
1255 if (!ret)
1256 component->name_prefix = str;
1257}
1258
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001259static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001260 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001261{
1262 int i;
1263
Jerome Brunetaefba452018-07-13 14:50:43 +02001264 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001265 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001266 struct device_node *component_of_node = component->dev->of_node;
1267
1268 if (!component_of_node && component->dev->parent)
1269 component_of_node = component->dev->parent->of_node;
1270
1271 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001272 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001273 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001274 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001275 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001276 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001277 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001278
1279 /*
1280 * If there is no configuration table or no match in the table,
1281 * check if a prefix is provided in the node
1282 */
1283 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001284}
1285
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001286static int soc_probe_component(struct snd_soc_card *card,
1287 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001289 struct snd_soc_dapm_context *dapm =
1290 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001291 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001292 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001294 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001295 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001296
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001297 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001298 if (component->card != card) {
1299 dev_err(component->dev,
1300 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1301 card->name, component->card->name);
1302 return -ENODEV;
1303 }
1304 return 0;
1305 }
1306
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001307 if (!try_module_get(component->dev->driver->owner))
1308 return -ENODEV;
1309
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001310 component->card = card;
1311 dapm->card = card;
1312 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001313
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001314 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001315
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001316 if (component->driver->dapm_widgets) {
1317 ret = snd_soc_dapm_new_controls(dapm,
1318 component->driver->dapm_widgets,
1319 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001320
1321 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001323 "Failed to create new controls %d\n", ret);
1324 goto err_probe;
1325 }
1326 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001327
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001328 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001329 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001330 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001331 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001332 "Failed to create DAI widgets %d\n", ret);
1333 goto err_probe;
1334 }
1335 }
Mark Brown888df392012-02-16 19:37:51 -08001336
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001337 if (component->driver->probe) {
1338 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001340 dev_err(component->dev,
1341 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001342 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001344
1345 WARN(dapm->idle_bias_off &&
1346 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001347 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001348 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349 }
1350
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001351 /* machine specific init */
1352 if (component->init) {
1353 ret = component->init(component);
1354 if (ret < 0) {
1355 dev_err(component->dev,
1356 "Failed to do machine specific init %d\n", ret);
1357 goto err_probe;
1358 }
1359 }
1360
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001361 if (component->driver->controls)
1362 snd_soc_add_component_controls(component,
1363 component->driver->controls,
1364 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001365 if (component->driver->dapm_routes)
1366 snd_soc_dapm_add_routes(dapm,
1367 component->driver->dapm_routes,
1368 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001369
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001370 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001371 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001372 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373
Jarkko Nikula70d293312011-01-27 16:24:22 +02001374 return 0;
1375
1376err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001377 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001378 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001379 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001380
1381 return ret;
1382}
1383
Mark Brown36ae1a92012-01-06 17:12:45 -08001384static void rtd_release(struct device *dev)
1385{
1386 kfree(dev);
1387}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001389static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1390 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001391{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001392 int ret = 0;
1393
Jarkko Nikula589c3562010-12-06 16:27:07 +02001394 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001395 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1396 if (!rtd->dev)
1397 return -ENOMEM;
1398 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001399 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001400 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001401 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001402 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001403 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001404 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001405 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1406 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1407 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1408 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001409 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001410 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001411 /* calling put_device() here to free the rtd->dev */
1412 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001413 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001414 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001415 return ret;
1416 }
1417 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001418 return 0;
1419}
1420
Mengdong Lin1a497982015-11-18 02:34:11 -05001421static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001422 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001423{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001424 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001425 struct snd_soc_rtdcom_list *rtdcom;
1426 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001427
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001428 for_each_rtdcom(rtd, rtdcom) {
1429 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001430
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001431 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001432 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001433 if (ret < 0)
1434 return ret;
1435 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001436 }
1437
Stephen Warren62ae68f2012-06-08 12:34:23 -06001438 return 0;
1439}
1440
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001441static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001443 if (dai->probed ||
1444 dai->driver->probe_order != order)
1445 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001446
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001447 if (dai->driver->probe) {
1448 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001449
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001450 if (ret < 0) {
1451 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1452 dai->name, ret);
1453 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001454 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001455 }
1456
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001457 dai->probed = 1;
1458
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001459 return 0;
1460}
1461
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001462static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1463 struct snd_soc_pcm_runtime *rtd)
1464{
1465 int i, ret = 0;
1466
1467 for (i = 0; i < num_dais; ++i) {
1468 struct snd_soc_dai_driver *drv = dais[i]->driver;
1469
1470 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1471 ret = drv->pcm_new(rtd, dais[i]);
1472 if (ret < 0) {
1473 dev_err(dais[i]->dev,
1474 "ASoC: Failed to bind %s with pcm device\n",
1475 dais[i]->name);
1476 return ret;
1477 }
1478 }
1479
1480 return 0;
1481}
1482
Mengdong Lin1a497982015-11-18 02:34:11 -05001483static int soc_probe_link_dais(struct snd_soc_card *card,
1484 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485{
Mengdong Lin1a497982015-11-18 02:34:11 -05001486 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001488 struct snd_soc_rtdcom_list *rtdcom;
1489 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001490 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001491 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001493 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001494 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 /* set default power off timeout */
1497 rtd->pmdown_time = pmdown_time;
1498
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001499 ret = soc_probe_dai(cpu_dai, order);
1500 if (ret)
1501 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001504 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1505 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001506 if (ret)
1507 return ret;
1508 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001509
Liam Girdwood0168bf02011-06-07 16:08:05 +01001510 /* complete DAI probe during last probe */
1511 if (order != SND_SOC_COMP_ORDER_LAST)
1512 return 0;
1513
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001514 /* do machine specific initialization */
1515 if (dai_link->init) {
1516 ret = dai_link->init(rtd);
1517 if (ret < 0) {
1518 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1519 dai_link->name, ret);
1520 return ret;
1521 }
1522 }
1523
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001524 if (dai_link->dai_fmt)
1525 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1526
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001527 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001528 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001530
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001531#ifdef CONFIG_DEBUG_FS
1532 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001533 if (dai_link->dynamic)
1534 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001535#endif
1536
Liam Girdwooda655de82018-07-02 16:59:54 +01001537 num = rtd->num;
1538
1539 /*
1540 * most drivers will register their PCMs using DAI link ordering but
1541 * topology based drivers can use the DAI link id field to set PCM
1542 * device number and then use rtd + a base offset of the BEs.
1543 */
1544 for_each_rtdcom(rtd, rtdcom) {
1545 component = rtdcom->component;
1546
1547 if (!component->driver->use_dai_pcm_id)
1548 continue;
1549
1550 if (rtd->dai_link->no_pcm)
1551 num += component->driver->be_pcm_base;
1552 else
1553 num = rtd->dai_link->id;
1554 }
1555
Jie Yang6f0c4222015-10-13 23:41:00 +08001556 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001557 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001558 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001559 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001560 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301561 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001562 return ret;
1563 }
1564 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301565
1566 if (!dai_link->params) {
1567 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001568 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301569 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001570 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001571 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001572 return ret;
1573 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001574 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1575 if (ret < 0)
1576 return ret;
1577 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1578 rtd->num_codecs, rtd);
1579 if (ret < 0)
1580 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301581 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001582 INIT_DELAYED_WORK(&rtd->delayed_work,
1583 codec2codec_close_delayed_work);
Mark Brownc74184e2012-04-04 22:12:09 +01001584 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585 }
1586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 return 0;
1588}
1589
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001590static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001591{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001592 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001593 struct snd_soc_component *component;
1594 const char *name;
1595 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001596
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001597 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1598 /* codecs, usually analog devices */
1599 name = aux_dev->codec_name;
1600 codec_of_node = aux_dev->codec_of_node;
1601 component = soc_find_component(codec_of_node, name);
1602 if (!component) {
1603 if (codec_of_node)
1604 name = of_node_full_name(codec_of_node);
1605 goto err_defer;
1606 }
1607 } else if (aux_dev->name) {
1608 /* generic components */
1609 name = aux_dev->name;
1610 component = soc_find_component(NULL, name);
1611 if (!component)
1612 goto err_defer;
1613 } else {
1614 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1615 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001616 }
1617
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001618 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001619 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001620
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001621 return 0;
1622
1623err_defer:
1624 dev_err(card->dev, "ASoC: %s not registered\n", name);
1625 return -EPROBE_DEFER;
1626}
1627
1628static int soc_probe_aux_devices(struct snd_soc_card *card)
1629{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001630 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001631 int order;
1632 int ret;
1633
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001634 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001635 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001636 if (comp->driver->probe_order == order) {
1637 ret = soc_probe_component(card, comp);
1638 if (ret < 0) {
1639 dev_err(card->dev,
1640 "ASoC: failed to probe aux component %s %d\n",
1641 comp->name, ret);
1642 return ret;
1643 }
1644 }
1645 }
1646 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001647
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001648 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001649}
1650
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001651static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001652{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001653 struct snd_soc_component *comp, *_comp;
1654 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001655
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001656 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001657 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001658 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001659
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001660 if (comp->driver->remove_order == order) {
1661 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001662 /* remove it from the card's aux_comp_list */
1663 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001664 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001665 }
1666 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001667}
1668
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001669/**
1670 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1671 * @rtd: The runtime for which the DAI link format should be changed
1672 * @dai_fmt: The new DAI link format
1673 *
1674 * This function updates the DAI link format for all DAIs connected to the DAI
1675 * link for the specified runtime.
1676 *
1677 * Note: For setups with a static format set the dai_fmt field in the
1678 * corresponding snd_dai_link struct instead of using this function.
1679 *
1680 * Returns 0 on success, otherwise a negative error code.
1681 */
1682int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1683 unsigned int dai_fmt)
1684{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001685 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001686 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001687 unsigned int i;
1688 int ret;
1689
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001690 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001691 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1692 if (ret != 0 && ret != -ENOTSUPP) {
1693 dev_warn(codec_dai->dev,
1694 "ASoC: Failed to set DAI format: %d\n", ret);
1695 return ret;
1696 }
1697 }
1698
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001699 /*
1700 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1701 * the component which has non_legacy_dai_naming is Codec
1702 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001703 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001704 unsigned int inv_dai_fmt;
1705
1706 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1707 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1708 case SND_SOC_DAIFMT_CBM_CFM:
1709 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1710 break;
1711 case SND_SOC_DAIFMT_CBM_CFS:
1712 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1713 break;
1714 case SND_SOC_DAIFMT_CBS_CFM:
1715 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1716 break;
1717 case SND_SOC_DAIFMT_CBS_CFS:
1718 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1719 break;
1720 }
1721
1722 dai_fmt = inv_dai_fmt;
1723 }
1724
1725 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1726 if (ret != 0 && ret != -ENOTSUPP) {
1727 dev_warn(cpu_dai->dev,
1728 "ASoC: Failed to set DAI format: %d\n", ret);
1729 return ret;
1730 }
1731
1732 return 0;
1733}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001734EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001735
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001736#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001737/*
1738 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001739 * separate different DMI fields in the card long name. Only number and
1740 * alphabet characters and a few separator characters are kept.
1741 */
1742static void cleanup_dmi_name(char *name)
1743{
1744 int i, j = 0;
1745
1746 for (i = 0; name[i]; i++) {
1747 if (isalnum(name[i]) || (name[i] == '.')
1748 || (name[i] == '_'))
1749 name[j++] = name[i];
1750 else if (name[i] == '-')
1751 name[j++] = '_';
1752 }
1753
1754 name[j] = '\0';
1755}
1756
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001757/*
1758 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001759 * in the black list.
1760 */
1761static int is_dmi_valid(const char *field)
1762{
1763 int i = 0;
1764
1765 while (dmi_blacklist[i]) {
1766 if (strstr(field, dmi_blacklist[i]))
1767 return 0;
1768 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001769 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001770
1771 return 1;
1772}
1773
Liam Girdwood345233d2017-01-14 16:13:02 +08001774/**
1775 * snd_soc_set_dmi_name() - Register DMI names to card
1776 * @card: The card to register DMI names
1777 * @flavour: The flavour "differentiator" for the card amongst its peers.
1778 *
1779 * An Intel machine driver may be used by many different devices but are
1780 * difficult for userspace to differentiate, since machine drivers ususally
1781 * use their own name as the card short name and leave the card long name
1782 * blank. To differentiate such devices and fix bugs due to lack of
1783 * device-specific configurations, this function allows DMI info to be used
1784 * as the sound card long name, in the format of
1785 * "vendor-product-version-board"
1786 * (Character '-' is used to separate different DMI fields here).
1787 * This will help the user space to load the device-specific Use Case Manager
1788 * (UCM) configurations for the card.
1789 *
1790 * Possible card long names may be:
1791 * DellInc.-XPS139343-01-0310JH
1792 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1793 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1794 *
1795 * This function also supports flavoring the card longname to provide
1796 * the extra differentiation, like "vendor-product-version-board-flavor".
1797 *
1798 * We only keep number and alphabet characters and a few separator characters
1799 * in the card long name since UCM in the user space uses the card long names
1800 * as card configuration directory names and AudoConf cannot support special
1801 * charactors like SPACE.
1802 *
1803 * Returns 0 on success, otherwise a negative error code.
1804 */
1805int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1806{
1807 const char *vendor, *product, *product_version, *board;
1808 size_t longname_buf_size = sizeof(card->snd_card->longname);
1809 size_t len;
1810
1811 if (card->long_name)
1812 return 0; /* long name already set by driver or from DMI */
1813
1814 /* make up dmi long name as: vendor.product.version.board */
1815 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001816 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001817 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1818 return 0;
1819 }
1820
1821 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1822 "%s", vendor);
1823 cleanup_dmi_name(card->dmi_longname);
1824
1825 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001826 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001827 len = strlen(card->dmi_longname);
1828 snprintf(card->dmi_longname + len,
1829 longname_buf_size - len,
1830 "-%s", product);
1831
1832 len++; /* skip the separator "-" */
1833 if (len < longname_buf_size)
1834 cleanup_dmi_name(card->dmi_longname + len);
1835
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001836 /*
1837 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001838 * name in the product version field
1839 */
1840 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001841 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001842 len = strlen(card->dmi_longname);
1843 snprintf(card->dmi_longname + len,
1844 longname_buf_size - len,
1845 "-%s", product_version);
1846
1847 len++;
1848 if (len < longname_buf_size)
1849 cleanup_dmi_name(card->dmi_longname + len);
1850 }
1851 }
1852
1853 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001854 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001855 len = strlen(card->dmi_longname);
1856 snprintf(card->dmi_longname + len,
1857 longname_buf_size - len,
1858 "-%s", board);
1859
1860 len++;
1861 if (len < longname_buf_size)
1862 cleanup_dmi_name(card->dmi_longname + len);
1863 } else if (!product) {
1864 /* fall back to using legacy name */
1865 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1866 return 0;
1867 }
1868
1869 /* Add flavour to dmi long name */
1870 if (flavour) {
1871 len = strlen(card->dmi_longname);
1872 snprintf(card->dmi_longname + len,
1873 longname_buf_size - len,
1874 "-%s", flavour);
1875
1876 len++;
1877 if (len < longname_buf_size)
1878 cleanup_dmi_name(card->dmi_longname + len);
1879 }
1880
1881 /* set the card long name */
1882 card->long_name = card->dmi_longname;
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001887#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001888
Liam Girdwooda655de82018-07-02 16:59:54 +01001889static void soc_check_tplg_fes(struct snd_soc_card *card)
1890{
1891 struct snd_soc_component *component;
1892 const struct snd_soc_component_driver *comp_drv;
1893 struct snd_soc_dai_link *dai_link;
1894 int i;
1895
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001896 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001897
1898 /* does this component override FEs ? */
1899 if (!component->driver->ignore_machine)
1900 continue;
1901
1902 /* for this machine ? */
1903 if (strcmp(component->driver->ignore_machine,
1904 card->dev->driver->name))
1905 continue;
1906
1907 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001908 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001909
1910 /* ignore this FE */
1911 if (dai_link->dynamic) {
1912 dai_link->ignore = true;
1913 continue;
1914 }
1915
1916 dev_info(card->dev, "info: override FE DAI link %s\n",
1917 card->dai_link[i].name);
1918
1919 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001920 if (snd_soc_init_platform(card, dai_link) < 0) {
1921 dev_err(card->dev, "init platform error");
1922 continue;
1923 }
1924 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001925
1926 /* convert non BE into BE */
1927 dai_link->no_pcm = 1;
1928
1929 /* override any BE fixups */
1930 dai_link->be_hw_params_fixup =
1931 component->driver->be_hw_params_fixup;
1932
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001933 /*
1934 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001935 * dai link name if it's NULL to help bind widgets.
1936 */
1937 if (!dai_link->stream_name)
1938 dai_link->stream_name = dai_link->name;
1939 }
1940
1941 /* Inform userspace we are using alternate topology */
1942 if (component->driver->topology_name_prefix) {
1943
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001944 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001945 if (!card->topology_shortname_created) {
1946 comp_drv = component->driver;
1947
1948 snprintf(card->topology_shortname, 32, "%s-%s",
1949 comp_drv->topology_name_prefix,
1950 card->name);
1951 card->topology_shortname_created = true;
1952 }
1953
1954 /* use topology shortname */
1955 card->name = card->topology_shortname;
1956 }
1957 }
1958}
1959
Mark Brownb19e6e72012-03-14 21:18:39 +00001960static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001961{
Mengdong Lin1a497982015-11-18 02:34:11 -05001962 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001963 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001964 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001965
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001966 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001967 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968
Liam Girdwooda655de82018-07-02 16:59:54 +01001969 /* check whether any platform is ignore machine FE and using topology */
1970 soc_check_tplg_fes(card);
1971
Mark Brownb19e6e72012-03-14 21:18:39 +00001972 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001973 for_each_card_prelinks(card, i, dai_link) {
1974 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001975 if (ret != 0)
1976 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977 }
1978
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001979 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001980 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001981 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001982 if (ret != 0)
1983 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 }
1985
Mengdong Linf8f80362015-12-02 14:11:22 +08001986 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001987 for_each_card_prelinks(card, i, dai_link)
1988 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001990 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001991 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 card->owner, 0, &card->snd_card);
1993 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001994 dev_err(card->dev,
1995 "ASoC: can't create sound card for card %s: %d\n",
1996 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001997 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002000 soc_init_card_debugfs(card);
2001
Mark Browne37a4972011-03-02 18:21:57 +00002002 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2003 card->dapm.dev = card->dev;
2004 card->dapm.card = card;
2005 list_add(&card->dapm.list, &card->dapm_list);
2006
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002007#ifdef CONFIG_DEBUG_FS
2008 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2009#endif
2010
Mark Brown88ee1c62011-02-02 10:43:26 +00002011#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002012 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002013 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002014#endif
Andy Green6ed25972008-06-13 16:24:05 +01002015
Mark Brown9a841eb2011-04-12 17:51:37 -07002016 if (card->dapm_widgets)
2017 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2018 card->num_dapm_widgets);
2019
Nicolin Chenf23e8602015-02-14 17:22:49 -08002020 if (card->of_dapm_widgets)
2021 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2022 card->num_of_dapm_widgets);
2023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002024 /* initialise the sound card only once */
2025 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002026 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 if (ret < 0)
2028 goto card_probe_error;
2029 }
2030
Stephen Warren62ae68f2012-06-08 12:34:23 -06002031 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002032 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002033 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002034 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002035 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002036 dev_err(card->dev,
2037 "ASoC: failed to instantiate card %d\n",
2038 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002039 goto probe_dai_err;
2040 }
2041 }
2042 }
2043
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002044 /* probe auxiliary components */
2045 ret = soc_probe_aux_devices(card);
2046 if (ret < 0)
2047 goto probe_dai_err;
2048
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002049 /*
2050 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002051 * Components with topology may bring new DAIs and DAI links.
2052 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002053 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002054 if (soc_is_dai_link_bound(card, dai_link))
2055 continue;
2056
2057 ret = soc_init_dai_link(card, dai_link);
2058 if (ret)
2059 goto probe_dai_err;
2060 ret = soc_bind_dai_link(card, dai_link);
2061 if (ret)
2062 goto probe_dai_err;
2063 }
2064
Stephen Warren62ae68f2012-06-08 12:34:23 -06002065 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002066 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002067 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002068 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002069 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002070 dev_err(card->dev,
2071 "ASoC: failed to instantiate card %d\n",
2072 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002073 goto probe_dai_err;
2074 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002075 }
2076 }
2077
Mark Brown888df392012-02-16 19:37:51 -08002078 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002079 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002080
Mark Brownb7af1da2011-04-07 19:18:44 +09002081 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002082 snd_soc_add_card_controls(card, card->controls,
2083 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002084
Mark Brownb8ad29d2011-03-02 18:35:51 +00002085 if (card->dapm_routes)
2086 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2087 card->num_dapm_routes);
2088
Nicolin Chenf23e8602015-02-14 17:22:49 -08002089 if (card->of_dapm_routes)
2090 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2091 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002092
Takashi Iwai861886d2017-04-24 08:54:42 +02002093 /* try to set some sane longname if DMI is available */
2094 snd_soc_set_dmi_name(card, NULL);
2095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002098 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2099 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002100 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2101 "%s", card->driver_name ? card->driver_name : card->name);
2102 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2103 switch (card->snd_card->driver[i]) {
2104 case '_':
2105 case '-':
2106 case '\0':
2107 break;
2108 default:
2109 if (!isalnum(card->snd_card->driver[i]))
2110 card->snd_card->driver[i] = '_';
2111 break;
2112 }
2113 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114
Mark Brown28e9ad92011-03-02 18:36:34 +00002115 if (card->late_probe) {
2116 ret = card->late_probe(card);
2117 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002118 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002119 card->name, ret);
2120 goto probe_aux_dev_err;
2121 }
2122 }
2123
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002124 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 ret = snd_card_register(card->snd_card);
2127 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002128 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2129 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002130 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002131 }
2132
Mark Brown435c5e22008-12-04 15:32:53 +00002133 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002134 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002135 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002136 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002137 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002138
2139 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002141probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002142 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002144probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002145 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002147card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002148 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002149 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002150
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002151 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002152 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002153 snd_card_free(card->snd_card);
2154
Mark Brownb19e6e72012-03-14 21:18:39 +00002155base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002156 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002157 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002158 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159
Mark Brownb19e6e72012-03-14 21:18:39 +00002160 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002161}
2162
2163/* probes a new socdev */
2164static int soc_probe(struct platform_device *pdev)
2165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002166 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002167
Vinod Koul70a7ca32011-01-14 19:22:48 +05302168 /*
2169 * no card, so machine driver should be registering card
2170 * we should not be here in that case so ret error
2171 */
2172 if (!card)
2173 return -EINVAL;
2174
Mark Brownfe4085e2012-03-02 13:07:41 +00002175 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002176 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002177 card->name);
2178
Mark Brown435c5e22008-12-04 15:32:53 +00002179 /* Bodge while we unpick instantiation */
2180 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002181
Mark Brown28d528c2012-08-09 18:45:23 +01002182 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183}
2184
Vinod Koulb0e26482011-01-13 22:48:02 +05302185static int soc_cleanup_card_resources(struct snd_soc_card *card)
2186{
Mengdong Lin1a497982015-11-18 02:34:11 -05002187 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302188
2189 /* make sure any delayed work runs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002190 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002191 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302192
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002193 /* free the ALSA card at first; this syncs with pending operations */
2194 snd_card_free(card->snd_card);
2195
Vinod Koulb0e26482011-01-13 22:48:02 +05302196 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002197 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002198 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302199
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002200 /* remove auxiliary devices */
2201 soc_remove_aux_devices(card);
2202
Mark Brownd1e81422016-08-18 19:32:59 +01002203 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302204 soc_cleanup_card_debugfs(card);
2205
2206 /* remove the card */
2207 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002208 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302209
Vinod Koulb0e26482011-01-13 22:48:02 +05302210 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302211}
2212
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213/* removes a socdev */
2214static int soc_remove(struct platform_device *pdev)
2215{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002216 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217
Mark Brownc5af3a22008-11-28 13:29:45 +00002218 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219 return 0;
2220}
2221
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002222int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002223{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002224 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002225 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002226
2227 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002228 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002229
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002230 /*
2231 * Flush out pmdown_time work - we actually do want to run it
2232 * now, we're shutting down so no imminent restart.
2233 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002234 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002235 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002237 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002238
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002239 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002240 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002241 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002242 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002243 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002244
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002245 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002246 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002247 pinctrl_pm_select_sleep_state(codec_dai->dev);
2248 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002249 }
2250
Mark Brown416356f2009-06-30 19:05:15 +01002251 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002252}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002253EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002254
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002255const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302256 .suspend = snd_soc_suspend,
2257 .resume = snd_soc_resume,
2258 .freeze = snd_soc_suspend,
2259 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002260 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302261 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002262};
Stephen Warrendeb26072011-04-05 19:35:30 -06002263EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002264
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265/* ASoC platform driver */
2266static struct platform_driver soc_driver = {
2267 .driver = {
2268 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002269 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002270 },
2271 .probe = soc_probe,
2272 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273};
2274
Mark Brown096e49d2009-07-05 15:12:22 +01002275/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002276 * snd_soc_cnew - create new control
2277 * @_template: control template
2278 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002279 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002280 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281 *
2282 * Create a new mixer control from a template control.
2283 *
2284 * Returns 0 for success, else error.
2285 */
2286struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002287 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002288 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289{
2290 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002291 struct snd_kcontrol *kcontrol;
2292 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293
2294 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295 template.index = 0;
2296
Mark Brownefb7ac32011-03-08 17:23:24 +00002297 if (!long_name)
2298 long_name = template.name;
2299
2300 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002301 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002302 if (!name)
2303 return NULL;
2304
Mark Brownefb7ac32011-03-08 17:23:24 +00002305 template.name = name;
2306 } else {
2307 template.name = long_name;
2308 }
2309
2310 kcontrol = snd_ctl_new1(&template, data);
2311
2312 kfree(name);
2313
2314 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315}
2316EXPORT_SYMBOL_GPL(snd_soc_cnew);
2317
Liam Girdwood022658b2012-02-03 17:43:09 +00002318static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2319 const struct snd_kcontrol_new *controls, int num_controls,
2320 const char *prefix, void *data)
2321{
2322 int err, i;
2323
2324 for (i = 0; i < num_controls; i++) {
2325 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002326
Liam Girdwood022658b2012-02-03 17:43:09 +00002327 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2328 control->name, prefix));
2329 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002330 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2331 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002332 return err;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002339struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2340 const char *name)
2341{
2342 struct snd_card *card = soc_card->snd_card;
2343 struct snd_kcontrol *kctl;
2344
2345 if (unlikely(!name))
2346 return NULL;
2347
2348 list_for_each_entry(kctl, &card->controls, list)
2349 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2350 return kctl;
2351 return NULL;
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2354
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002355/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002356 * snd_soc_add_component_controls - Add an array of controls to a component.
2357 *
2358 * @component: Component to add controls to
2359 * @controls: Array of controls to add
2360 * @num_controls: Number of elements in the array
2361 *
2362 * Return: 0 for success, else error.
2363 */
2364int snd_soc_add_component_controls(struct snd_soc_component *component,
2365 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2366{
2367 struct snd_card *card = component->card->snd_card;
2368
2369 return snd_soc_add_controls(card, component->dev, controls,
2370 num_controls, component->name_prefix, component);
2371}
2372EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2373
2374/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002375 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2376 * Convenience function to add a list of controls.
2377 *
2378 * @soc_card: SoC card to add controls to
2379 * @controls: array of controls to add
2380 * @num_controls: number of elements in the array
2381 *
2382 * Return 0 for success, else error.
2383 */
2384int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2385 const struct snd_kcontrol_new *controls, int num_controls)
2386{
2387 struct snd_card *card = soc_card->snd_card;
2388
2389 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2390 NULL, soc_card);
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2393
2394/**
2395 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2396 * Convienience function to add a list of controls.
2397 *
2398 * @dai: DAI to add controls to
2399 * @controls: array of controls to add
2400 * @num_controls: number of elements in the array
2401 *
2402 * Return 0 for success, else error.
2403 */
2404int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2405 const struct snd_kcontrol_new *controls, int num_controls)
2406{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002407 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002408
2409 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2410 NULL, dai);
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2413
2414/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002415 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2416 * @dai: DAI
2417 * @clk_id: DAI specific clock ID
2418 * @freq: new clock frequency in Hz
2419 * @dir: new clock direction - input/output.
2420 *
2421 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2422 */
2423int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2424 unsigned int freq, int dir)
2425{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002426 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002427 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002428
2429 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2430 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002431}
2432EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2433
2434/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002435 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2436 * @component: COMPONENT
2437 * @clk_id: DAI specific clock ID
2438 * @source: Source for the clock
2439 * @freq: new clock frequency in Hz
2440 * @dir: new clock direction - input/output.
2441 *
2442 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2443 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002444int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2445 int clk_id, int source, unsigned int freq,
2446 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002447{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002448 if (component->driver->set_sysclk)
2449 return component->driver->set_sysclk(component, clk_id, source,
2450 freq, dir);
2451
2452 return -ENOTSUPP;
2453}
2454EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2455
2456/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002457 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2458 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002459 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002460 * @div: new clock divisor.
2461 *
2462 * Configures the clock dividers. This is used to derive the best DAI bit and
2463 * frame clocks from the system or master clock. It's best to set the DAI bit
2464 * and frame clocks as low as possible to save system power.
2465 */
2466int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2467 int div_id, int div)
2468{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002469 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002470 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002471 else
2472 return -EINVAL;
2473}
2474EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2475
2476/**
2477 * snd_soc_dai_set_pll - configure DAI PLL.
2478 * @dai: DAI
2479 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002480 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002481 * @freq_in: PLL input clock frequency in Hz
2482 * @freq_out: requested PLL output clock frequency in Hz
2483 *
2484 * Configures and enables PLL to generate output clock based on input clock.
2485 */
Mark Brown85488032009-09-05 18:52:16 +01002486int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2487 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002488{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002489 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002490 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002491 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002492
2493 return snd_soc_component_set_pll(dai->component, pll_id, source,
2494 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002495}
2496EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2497
Mark Brownec4ee522011-03-07 20:58:11 +00002498/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002499 * snd_soc_component_set_pll - configure component PLL.
2500 * @component: COMPONENT
2501 * @pll_id: DAI specific PLL ID
2502 * @source: DAI specific source for the PLL
2503 * @freq_in: PLL input clock frequency in Hz
2504 * @freq_out: requested PLL output clock frequency in Hz
2505 *
2506 * Configures and enables PLL to generate output clock based on input clock.
2507 */
2508int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2509 int source, unsigned int freq_in,
2510 unsigned int freq_out)
2511{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002512 if (component->driver->set_pll)
2513 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002514 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002515
2516 return -EINVAL;
2517}
2518EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2519
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002520/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002521 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2522 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002523 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002524 *
2525 * Configures the DAI for a preset BCLK to sample rate ratio.
2526 */
2527int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2528{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002529 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002530 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2531 else
2532 return -EINVAL;
2533}
2534EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2535
2536/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002537 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2538 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002539 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002540 *
2541 * Configures the DAI hardware format and clocking.
2542 */
2543int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2544{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002545 if (dai->driver->ops->set_fmt == NULL)
2546 return -ENOTSUPP;
2547 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002548}
2549EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2550
2551/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002552 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002553 * @slots: Number of slots in use.
2554 * @tx_mask: bitmask representing active TX slots.
2555 * @rx_mask: bitmask representing active RX slots.
2556 *
2557 * Generates the TDM tx and rx slot default masks for DAI.
2558 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002559static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002560 unsigned int *tx_mask,
2561 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002562{
2563 if (*tx_mask || *rx_mask)
2564 return 0;
2565
2566 if (!slots)
2567 return -EINVAL;
2568
2569 *tx_mask = (1 << slots) - 1;
2570 *rx_mask = (1 << slots) - 1;
2571
2572 return 0;
2573}
2574
2575/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002576 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2577 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002578 * @tx_mask: bitmask representing active TX slots.
2579 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002580 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002581 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002582 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002583 * This function configures the specified DAI for TDM operation. @slot contains
2584 * the total number of slots of the TDM stream and @slot_with the width of each
2585 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2586 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2587 * DAI should write to or read from. If a bit is set the corresponding slot is
2588 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2589 * the first slot, bit 1 to the second slot and so on. The first active slot
2590 * maps to the first channel of the DAI, the second active slot to the second
2591 * channel and so on.
2592 *
2593 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2594 * @rx_mask and @slot_width will be ignored.
2595 *
2596 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002597 */
2598int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002599 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002600{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002601 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002602 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002603 &tx_mask, &rx_mask);
2604 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002605 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002606
Benoit Cousson88bd8702014-07-08 23:19:34 +02002607 dai->tx_mask = tx_mask;
2608 dai->rx_mask = rx_mask;
2609
Kuninori Morimoto46471922017-09-25 01:38:34 +00002610 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002611 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002612 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002613 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002614 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002615}
2616EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2617
2618/**
Barry Song472df3c2009-09-12 01:16:29 +08002619 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2620 * @dai: DAI
2621 * @tx_num: how many TX channels
2622 * @tx_slot: pointer to an array which imply the TX slot number channel
2623 * 0~num-1 uses
2624 * @rx_num: how many RX channels
2625 * @rx_slot: pointer to an array which imply the RX slot number channel
2626 * 0~num-1 uses
2627 *
2628 * configure the relationship between channel number and TDM slot number.
2629 */
2630int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2631 unsigned int tx_num, unsigned int *tx_slot,
2632 unsigned int rx_num, unsigned int *rx_slot)
2633{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002634 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002635 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002636 rx_num, rx_slot);
2637 else
2638 return -EINVAL;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2641
2642/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002643 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2644 * @dai: DAI
2645 * @tx_num: how many TX channels
2646 * @tx_slot: pointer to an array which imply the TX slot number channel
2647 * 0~num-1 uses
2648 * @rx_num: how many RX channels
2649 * @rx_slot: pointer to an array which imply the RX slot number channel
2650 * 0~num-1 uses
2651 */
2652int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2653 unsigned int *tx_num, unsigned int *tx_slot,
2654 unsigned int *rx_num, unsigned int *rx_slot)
2655{
2656 if (dai->driver->ops->get_channel_map)
2657 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2658 rx_num, rx_slot);
2659 else
2660 return -ENOTSUPP;
2661}
2662EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2663
2664/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002665 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2666 * @dai: DAI
2667 * @tristate: tristate enable
2668 *
2669 * Tristates the DAI so that others can use it.
2670 */
2671int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2672{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002673 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002674 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002675 else
2676 return -EINVAL;
2677}
2678EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2679
2680/**
2681 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2682 * @dai: DAI
2683 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002684 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002685 *
2686 * Mutes the DAI DAC.
2687 */
Mark Brownda183962013-02-06 15:44:07 +00002688int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2689 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002690{
Mark Brownda183962013-02-06 15:44:07 +00002691 if (dai->driver->ops->mute_stream)
2692 return dai->driver->ops->mute_stream(dai, mute, direction);
2693 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2694 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002695 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002696 else
Mark Brown04570c62012-04-13 19:16:03 +01002697 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002698}
2699EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2700
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002701static int snd_soc_bind_card(struct snd_soc_card *card)
2702{
2703 struct snd_soc_pcm_runtime *rtd;
2704 int ret;
2705
2706 ret = snd_soc_instantiate_card(card);
2707 if (ret != 0)
2708 return ret;
2709
2710 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002711 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002712 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2713 struct snd_soc_dai *codec_dai;
2714 int j;
2715
2716 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2717 if (!codec_dai->active)
2718 pinctrl_pm_select_sleep_state(codec_dai->dev);
2719 }
2720
2721 if (!cpu_dai->active)
2722 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2723 }
2724
2725 return ret;
2726}
2727
Mark Brownc5af3a22008-11-28 13:29:45 +00002728/**
2729 * snd_soc_register_card - Register a card with the ASoC core
2730 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002731 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002732 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002733 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302734int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002735{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002736 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002737 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002738
Mark Brownc5af3a22008-11-28 13:29:45 +00002739 if (!card->name || !card->dev)
2740 return -EINVAL;
2741
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002742 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002743
Mengdong Lin923c5e612015-11-23 11:01:49 -05002744 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002745 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002746 dev_err(card->dev, "ASoC: failed to init link %s\n",
2747 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002748 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002749 }
Stephen Warren5a504962011-12-21 10:40:59 -07002750 }
2751
Mark Browned77cc12011-05-03 18:25:34 +01002752 dev_set_drvdata(card->dev, card);
2753
Stephen Warren111c6412011-01-28 14:26:35 -07002754 snd_soc_initialize_card_lists(card);
2755
Mengdong Linf8f80362015-12-02 14:11:22 +08002756 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002757
Mengdong Lin1a497982015-11-18 02:34:11 -05002758 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002759 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002760
Mark Browndb432b42011-10-03 21:06:40 +01002761 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002762 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002763 card->instantiated = 0;
2764 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002765 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002766
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002767 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002768}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302769EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002770
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002771static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2772{
2773 if (card->instantiated) {
2774 card->instantiated = false;
2775 snd_soc_dapm_shutdown(card);
2776 soc_cleanup_card_resources(card);
2777 if (!unregister)
2778 list_add(&card->list, &unbind_card_list);
2779 } else {
2780 if (unregister)
2781 list_del(&card->list);
2782 }
2783}
2784
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002785/**
2786 * snd_soc_unregister_card - Unregister a card with the ASoC core
2787 *
2788 * @card: Card to unregister
2789 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002790 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302791int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002792{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002793 snd_soc_unbind_card(card, true);
2794 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002795
2796 return 0;
2797}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302798EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002799
2800/*
2801 * Simplify DAI link configuration by removing ".-1" from device names
2802 * and sanitizing names.
2803 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002804static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002805{
2806 char *found, name[NAME_SIZE];
2807 int id1, id2;
2808
2809 if (dev_name(dev) == NULL)
2810 return NULL;
2811
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002812 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002813
2814 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002815 found = strstr(name, dev->driver->name);
2816 if (found) {
2817 /* get ID */
2818 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2819
2820 /* discard ID from name if ID == -1 */
2821 if (*id == -1)
2822 found[strlen(dev->driver->name)] = '\0';
2823 }
2824
2825 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002826 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002827 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2828 char tmp[NAME_SIZE];
2829
2830 /* create unique ID number from I2C addr and bus */
2831 *id = ((id1 & 0xffff) << 16) + id2;
2832
2833 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002834 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2835 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002836 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002837 } else
2838 *id = 0;
2839 }
2840
2841 return kstrdup(name, GFP_KERNEL);
2842}
2843
2844/*
2845 * Simplify DAI link naming for single devices with multiple DAIs by removing
2846 * any ".-1" and using the DAI name (instead of device name).
2847 */
2848static inline char *fmt_multiple_name(struct device *dev,
2849 struct snd_soc_dai_driver *dai_drv)
2850{
2851 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002852 dev_err(dev,
2853 "ASoC: error - multiple DAI %s registered with no name\n",
2854 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002855 return NULL;
2856 }
2857
2858 return kstrdup(dai_drv->name, GFP_KERNEL);
2859}
2860
2861/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002862 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002863 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002864 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002865 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002866static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002867{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002868 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002869
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002870 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002871 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2872 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002873 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002874 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002875 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002876 }
Mark Brown91151712008-11-30 23:31:24 +00002877}
Mark Brown91151712008-11-30 23:31:24 +00002878
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002879/* Create a DAI and add it to the component's DAI list */
2880static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2881 struct snd_soc_dai_driver *dai_drv,
2882 bool legacy_dai_naming)
2883{
2884 struct device *dev = component->dev;
2885 struct snd_soc_dai *dai;
2886
2887 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2888
2889 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2890 if (dai == NULL)
2891 return NULL;
2892
2893 /*
2894 * Back in the old days when we still had component-less DAIs,
2895 * instead of having a static name, component-less DAIs would
2896 * inherit the name of the parent device so it is possible to
2897 * register multiple instances of the DAI. We still need to keep
2898 * the same naming style even though those DAIs are not
2899 * component-less anymore.
2900 */
2901 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002902 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002903 dai->name = fmt_single_name(dev, &dai->id);
2904 } else {
2905 dai->name = fmt_multiple_name(dev, dai_drv);
2906 if (dai_drv->id)
2907 dai->id = dai_drv->id;
2908 else
2909 dai->id = component->num_dai;
2910 }
2911 if (dai->name == NULL) {
2912 kfree(dai);
2913 return NULL;
2914 }
2915
2916 dai->component = component;
2917 dai->dev = dev;
2918 dai->driver = dai_drv;
2919 if (!dai->driver->ops)
2920 dai->driver->ops = &null_dai_ops;
2921
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002922 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002923 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002924 component->num_dai++;
2925
2926 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2927 return dai;
2928}
2929
Mark Brown91151712008-11-30 23:31:24 +00002930/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002931 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002932 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002933 * @component: The component the DAIs are registered for
2934 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002935 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002936 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002937static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002938 struct snd_soc_dai_driver *dai_drv,
2939 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002940{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002941 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002942 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002943 unsigned int i;
2944 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002945
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002946 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002947
2948 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002950 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2951 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002952 if (dai == NULL) {
2953 ret = -ENOMEM;
2954 goto err;
2955 }
Mark Brown91151712008-11-30 23:31:24 +00002956 }
2957
2958 return 0;
2959
2960err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002961 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002962
2963 return ret;
2964}
Mark Brown91151712008-11-30 23:31:24 +00002965
Mengdong Lin68003e62015-12-31 16:40:43 +08002966/**
2967 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2968 *
2969 * @component: The component the DAIs are registered for
2970 * @dai_drv: DAI driver to use for the DAI
2971 *
2972 * Topology can use this API to register DAIs when probing a component.
2973 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2974 * will be freed in the component cleanup.
2975 */
2976int snd_soc_register_dai(struct snd_soc_component *component,
2977 struct snd_soc_dai_driver *dai_drv)
2978{
2979 struct snd_soc_dapm_context *dapm =
2980 snd_soc_component_get_dapm(component);
2981 struct snd_soc_dai *dai;
2982 int ret;
2983
2984 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2985 dev_err(component->dev, "Invalid dai type %d\n",
2986 dai_drv->dobj.type);
2987 return -EINVAL;
2988 }
2989
2990 lockdep_assert_held(&client_mutex);
2991 dai = soc_add_dai(component, dai_drv, false);
2992 if (!dai)
2993 return -ENOMEM;
2994
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002995 /*
2996 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002997 * also add routes that need these widgets as source or sink.
2998 */
2999 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3000 if (ret != 0) {
3001 dev_err(component->dev,
3002 "Failed to create DAI widgets %d\n", ret);
3003 }
3004
3005 return ret;
3006}
3007EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3008
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003009static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3010 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003011{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003012 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003013
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003014 component->driver->seq_notifier(component, type, subseq);
3015}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003016
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003017static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3018 int event)
3019{
3020 struct snd_soc_component *component = dapm->component;
3021
3022 return component->driver->stream_event(component, event);
3023}
3024
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003025static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3026 enum snd_soc_bias_level level)
3027{
3028 struct snd_soc_component *component = dapm->component;
3029
3030 return component->driver->set_bias_level(component, level);
3031}
3032
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003033static int snd_soc_component_initialize(struct snd_soc_component *component,
3034 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003035{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003036 struct snd_soc_dapm_context *dapm;
3037
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003038 component->name = fmt_single_name(dev, &component->id);
3039 if (!component->name) {
3040 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003041 return -ENOMEM;
3042 }
3043
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003044 component->dev = dev;
3045 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003046
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003047 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003048 dapm->dev = dev;
3049 dapm->component = component;
3050 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003051 dapm->idle_bias_off = !driver->idle_bias_on;
3052 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003053 if (driver->seq_notifier)
3054 dapm->seq_notifier = snd_soc_component_seq_notifier;
3055 if (driver->stream_event)
3056 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003057 if (driver->set_bias_level)
3058 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003059
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003060 INIT_LIST_HEAD(&component->dai_list);
3061 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003062
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003063 return 0;
3064}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003065
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003066static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003067{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003068 int val_bytes = regmap_get_val_bytes(component->regmap);
3069
3070 /* Errors are legitimate for non-integer byte multiples */
3071 if (val_bytes > 0)
3072 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003073}
3074
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003075#ifdef CONFIG_REGMAP
3076
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003077/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003078 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3079 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003080 * @component: The component for which to initialize the regmap instance
3081 * @regmap: The regmap instance that should be used by the component
3082 *
3083 * This function allows deferred assignment of the regmap instance that is
3084 * associated with the component. Only use this if the regmap instance is not
3085 * yet ready when the component is registered. The function must also be called
3086 * before the first IO attempt of the component.
3087 */
3088void snd_soc_component_init_regmap(struct snd_soc_component *component,
3089 struct regmap *regmap)
3090{
3091 component->regmap = regmap;
3092 snd_soc_component_setup_regmap(component);
3093}
3094EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3095
3096/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003097 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3098 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003099 * @component: The component for which to de-initialize the regmap instance
3100 *
3101 * Calls regmap_exit() on the regmap instance associated to the component and
3102 * removes the regmap instance from the component.
3103 *
3104 * This function should only be used if snd_soc_component_init_regmap() was used
3105 * to initialize the regmap instance.
3106 */
3107void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3108{
3109 regmap_exit(component->regmap);
3110 component->regmap = NULL;
3111}
3112EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3113
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003114#endif
3115
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003116static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003117{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003118 mutex_lock(&client_mutex);
3119
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003120 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003121 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003122 component->regmap = dev_get_regmap(component->dev,
3123 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003124 if (component->regmap)
3125 snd_soc_component_setup_regmap(component);
3126 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003127
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003128 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003129 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003130 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003131
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003132 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003133}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003134
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003135static void snd_soc_component_cleanup(struct snd_soc_component *component)
3136{
3137 snd_soc_unregister_dais(component);
3138 kfree(component->name);
3139}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003140
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003141static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3142{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003143 struct snd_soc_card *card = component->card;
3144
3145 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003146 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003147
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003148 list_del(&component->list);
3149}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003150
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003151#define ENDIANNESS_MAP(name) \
3152 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3153static u64 endianness_format_map[] = {
3154 ENDIANNESS_MAP(S16_),
3155 ENDIANNESS_MAP(U16_),
3156 ENDIANNESS_MAP(S24_),
3157 ENDIANNESS_MAP(U24_),
3158 ENDIANNESS_MAP(S32_),
3159 ENDIANNESS_MAP(U32_),
3160 ENDIANNESS_MAP(S24_3),
3161 ENDIANNESS_MAP(U24_3),
3162 ENDIANNESS_MAP(S20_3),
3163 ENDIANNESS_MAP(U20_3),
3164 ENDIANNESS_MAP(S18_3),
3165 ENDIANNESS_MAP(U18_3),
3166 ENDIANNESS_MAP(FLOAT_),
3167 ENDIANNESS_MAP(FLOAT64_),
3168 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3169};
3170
3171/*
3172 * Fix up the DAI formats for endianness: codecs don't actually see
3173 * the endianness of the data but we're using the CPU format
3174 * definitions which do need to include endianness so we ensure that
3175 * codec DAIs always have both big and little endian variants set.
3176 */
3177static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3178{
3179 int i;
3180
3181 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3182 if (stream->formats & endianness_format_map[i])
3183 stream->formats |= endianness_format_map[i];
3184}
3185
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003186static void snd_soc_try_rebind_card(void)
3187{
3188 struct snd_soc_card *card, *c;
3189
3190 if (!list_empty(&unbind_card_list)) {
3191 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3192 if (!snd_soc_bind_card(card))
3193 list_del(&card->list);
3194 }
3195 }
3196}
3197
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003198int snd_soc_add_component(struct device *dev,
3199 struct snd_soc_component *component,
3200 const struct snd_soc_component_driver *component_driver,
3201 struct snd_soc_dai_driver *dai_drv,
3202 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003203{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003204 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003205 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003206
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003207 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003208 if (ret)
3209 goto err_free;
3210
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003211 if (component_driver->endianness) {
3212 for (i = 0; i < num_dai; i++) {
3213 convert_endianness_formats(&dai_drv[i].playback);
3214 convert_endianness_formats(&dai_drv[i].capture);
3215 }
3216 }
3217
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003218 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003219 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003220 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003221 goto err_cleanup;
3222 }
3223
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003224 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003225 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003226
3227 return 0;
3228
3229err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003230 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003231err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003232 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003233}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003234EXPORT_SYMBOL_GPL(snd_soc_add_component);
3235
3236int snd_soc_register_component(struct device *dev,
3237 const struct snd_soc_component_driver *component_driver,
3238 struct snd_soc_dai_driver *dai_drv,
3239 int num_dai)
3240{
3241 struct snd_soc_component *component;
3242
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003243 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003244 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003245 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003246
3247 return snd_soc_add_component(dev, component, component_driver,
3248 dai_drv, num_dai);
3249}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003250EXPORT_SYMBOL_GPL(snd_soc_register_component);
3251
3252/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003253 * snd_soc_unregister_component - Unregister all related component
3254 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003255 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003256 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003257 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003258static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003259{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003260 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003261 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003262
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003263 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003264 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003265 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003266 continue;
3267
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003268 snd_soc_tplg_component_remove(component,
3269 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003270 snd_soc_component_del_unlocked(component);
3271 found = 1;
3272 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003273 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003274 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003275
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003276 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003277 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003278
3279 return found;
3280}
3281
3282void snd_soc_unregister_component(struct device *dev)
3283{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003284 while (__snd_soc_unregister_component(dev))
3285 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003286}
3287EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3288
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003289struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3290 const char *driver_name)
3291{
3292 struct snd_soc_component *component;
3293 struct snd_soc_component *ret;
3294
3295 ret = NULL;
3296 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003297 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003298 if (dev != component->dev)
3299 continue;
3300
3301 if (driver_name &&
3302 (driver_name != component->driver->name) &&
3303 (strcmp(component->driver->name, driver_name) != 0))
3304 continue;
3305
3306 ret = component;
3307 break;
3308 }
3309 mutex_unlock(&client_mutex);
3310
3311 return ret;
3312}
3313EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3314
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003315/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003316int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3317 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003318{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003319 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003320 int ret;
3321
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303322 if (!card->dev) {
3323 pr_err("card->dev is not set before calling %s\n", __func__);
3324 return -EINVAL;
3325 }
3326
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003327 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303328
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003329 ret = of_property_read_string_index(np, propname, 0, &card->name);
3330 /*
3331 * EINVAL means the property does not exist. This is fine providing
3332 * card->name was previously set, which is checked later in
3333 * snd_soc_register_card.
3334 */
3335 if (ret < 0 && ret != -EINVAL) {
3336 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003337 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003338 propname, ret);
3339 return ret;
3340 }
3341
3342 return 0;
3343}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003344EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003345
Xiubo Li9a6d4862014-02-08 15:59:52 +08003346static const struct snd_soc_dapm_widget simple_widgets[] = {
3347 SND_SOC_DAPM_MIC("Microphone", NULL),
3348 SND_SOC_DAPM_LINE("Line", NULL),
3349 SND_SOC_DAPM_HP("Headphone", NULL),
3350 SND_SOC_DAPM_SPK("Speaker", NULL),
3351};
3352
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003353int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003354 const char *propname)
3355{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003356 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003357 struct snd_soc_dapm_widget *widgets;
3358 const char *template, *wname;
3359 int i, j, num_widgets, ret;
3360
3361 num_widgets = of_property_count_strings(np, propname);
3362 if (num_widgets < 0) {
3363 dev_err(card->dev,
3364 "ASoC: Property '%s' does not exist\n", propname);
3365 return -EINVAL;
3366 }
3367 if (num_widgets & 1) {
3368 dev_err(card->dev,
3369 "ASoC: Property '%s' length is not even\n", propname);
3370 return -EINVAL;
3371 }
3372
3373 num_widgets /= 2;
3374 if (!num_widgets) {
3375 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3376 propname);
3377 return -EINVAL;
3378 }
3379
3380 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3381 GFP_KERNEL);
3382 if (!widgets) {
3383 dev_err(card->dev,
3384 "ASoC: Could not allocate memory for widgets\n");
3385 return -ENOMEM;
3386 }
3387
3388 for (i = 0; i < num_widgets; i++) {
3389 ret = of_property_read_string_index(np, propname,
3390 2 * i, &template);
3391 if (ret) {
3392 dev_err(card->dev,
3393 "ASoC: Property '%s' index %d read error:%d\n",
3394 propname, 2 * i, ret);
3395 return -EINVAL;
3396 }
3397
3398 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3399 if (!strncmp(template, simple_widgets[j].name,
3400 strlen(simple_widgets[j].name))) {
3401 widgets[i] = simple_widgets[j];
3402 break;
3403 }
3404 }
3405
3406 if (j >= ARRAY_SIZE(simple_widgets)) {
3407 dev_err(card->dev,
3408 "ASoC: DAPM widget '%s' is not supported\n",
3409 template);
3410 return -EINVAL;
3411 }
3412
3413 ret = of_property_read_string_index(np, propname,
3414 (2 * i) + 1,
3415 &wname);
3416 if (ret) {
3417 dev_err(card->dev,
3418 "ASoC: Property '%s' index %d read error:%d\n",
3419 propname, (2 * i) + 1, ret);
3420 return -EINVAL;
3421 }
3422
3423 widgets[i].name = wname;
3424 }
3425
Nicolin Chenf23e8602015-02-14 17:22:49 -08003426 card->of_dapm_widgets = widgets;
3427 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003428
3429 return 0;
3430}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003431EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003432
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003433int snd_soc_of_get_slot_mask(struct device_node *np,
3434 const char *prop_name,
3435 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003436{
3437 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003438 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003439 int i;
3440
3441 if (!of_slot_mask)
3442 return 0;
3443 val /= sizeof(u32);
3444 for (i = 0; i < val; i++)
3445 if (be32_to_cpup(&of_slot_mask[i]))
3446 *mask |= (1 << i);
3447
3448 return val;
3449}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003450EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003451
Xiubo Li89c67852014-02-14 09:34:35 +08003452int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003453 unsigned int *tx_mask,
3454 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003455 unsigned int *slots,
3456 unsigned int *slot_width)
3457{
3458 u32 val;
3459 int ret;
3460
Jyri Sarha61310842015-09-09 21:27:43 +03003461 if (tx_mask)
3462 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3463 if (rx_mask)
3464 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3465
Xiubo Li89c67852014-02-14 09:34:35 +08003466 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3467 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3468 if (ret)
3469 return ret;
3470
3471 if (slots)
3472 *slots = val;
3473 }
3474
3475 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3476 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3477 if (ret)
3478 return ret;
3479
3480 if (slot_width)
3481 *slot_width = val;
3482 }
3483
3484 return 0;
3485}
3486EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3487
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003488void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003489 struct snd_soc_codec_conf *codec_conf,
3490 struct device_node *of_node,
3491 const char *propname)
3492{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003493 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003494 const char *str;
3495 int ret;
3496
3497 ret = of_property_read_string(np, propname, &str);
3498 if (ret < 0) {
3499 /* no prefix is not error */
3500 return;
3501 }
3502
3503 codec_conf->of_node = of_node;
3504 codec_conf->name_prefix = str;
3505}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003506EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003507
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003508int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003509 const char *propname)
3510{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003511 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003512 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003513 struct snd_soc_dapm_route *routes;
3514 int i, ret;
3515
3516 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003517 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003518 dev_err(card->dev,
3519 "ASoC: Property '%s' does not exist or its length is not even\n",
3520 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003521 return -EINVAL;
3522 }
3523 num_routes /= 2;
3524 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003525 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003526 propname);
3527 return -EINVAL;
3528 }
3529
Kees Cooka86854d2018-06-12 14:07:58 -07003530 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003531 GFP_KERNEL);
3532 if (!routes) {
3533 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003534 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003535 return -EINVAL;
3536 }
3537
3538 for (i = 0; i < num_routes; i++) {
3539 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003540 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003541 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003542 dev_err(card->dev,
3543 "ASoC: Property '%s' index %d could not be read: %d\n",
3544 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003545 return -EINVAL;
3546 }
3547 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003548 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003549 if (ret) {
3550 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003551 "ASoC: Property '%s' index %d could not be read: %d\n",
3552 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003553 return -EINVAL;
3554 }
3555 }
3556
Nicolin Chenf23e8602015-02-14 17:22:49 -08003557 card->num_of_dapm_routes = num_routes;
3558 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003559
3560 return 0;
3561}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003562EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003563
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003564unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003565 const char *prefix,
3566 struct device_node **bitclkmaster,
3567 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003568{
3569 int ret, i;
3570 char prop[128];
3571 unsigned int format = 0;
3572 int bit, frame;
3573 const char *str;
3574 struct {
3575 char *name;
3576 unsigned int val;
3577 } of_fmt_table[] = {
3578 { "i2s", SND_SOC_DAIFMT_I2S },
3579 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3580 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3581 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3582 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3583 { "ac97", SND_SOC_DAIFMT_AC97 },
3584 { "pdm", SND_SOC_DAIFMT_PDM},
3585 { "msb", SND_SOC_DAIFMT_MSB },
3586 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003587 };
3588
3589 if (!prefix)
3590 prefix = "";
3591
3592 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003593 * check "dai-format = xxx"
3594 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003595 * SND_SOC_DAIFMT_FORMAT_MASK area
3596 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003597 ret = of_property_read_string(np, "dai-format", &str);
3598 if (ret < 0) {
3599 snprintf(prop, sizeof(prop), "%sformat", prefix);
3600 ret = of_property_read_string(np, prop, &str);
3601 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003602 if (ret == 0) {
3603 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3604 if (strcmp(str, of_fmt_table[i].name) == 0) {
3605 format |= of_fmt_table[i].val;
3606 break;
3607 }
3608 }
3609 }
3610
3611 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003612 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003613 * SND_SOC_DAIFMT_CLOCK_MASK area
3614 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003615 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003616 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003617 format |= SND_SOC_DAIFMT_CONT;
3618 else
3619 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003620
3621 /*
3622 * check "[prefix]bitclock-inversion"
3623 * check "[prefix]frame-inversion"
3624 * SND_SOC_DAIFMT_INV_MASK area
3625 */
3626 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3627 bit = !!of_get_property(np, prop, NULL);
3628
3629 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3630 frame = !!of_get_property(np, prop, NULL);
3631
3632 switch ((bit << 4) + frame) {
3633 case 0x11:
3634 format |= SND_SOC_DAIFMT_IB_IF;
3635 break;
3636 case 0x10:
3637 format |= SND_SOC_DAIFMT_IB_NF;
3638 break;
3639 case 0x01:
3640 format |= SND_SOC_DAIFMT_NB_IF;
3641 break;
3642 default:
3643 /* SND_SOC_DAIFMT_NB_NF is default */
3644 break;
3645 }
3646
3647 /*
3648 * check "[prefix]bitclock-master"
3649 * check "[prefix]frame-master"
3650 * SND_SOC_DAIFMT_MASTER_MASK area
3651 */
3652 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3653 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003654 if (bit && bitclkmaster)
3655 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003656
3657 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3658 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003659 if (frame && framemaster)
3660 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003661
3662 switch ((bit << 4) + frame) {
3663 case 0x11:
3664 format |= SND_SOC_DAIFMT_CBM_CFM;
3665 break;
3666 case 0x10:
3667 format |= SND_SOC_DAIFMT_CBM_CFS;
3668 break;
3669 case 0x01:
3670 format |= SND_SOC_DAIFMT_CBS_CFM;
3671 break;
3672 default:
3673 format |= SND_SOC_DAIFMT_CBS_CFS;
3674 break;
3675 }
3676
3677 return format;
3678}
3679EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3680
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003681int snd_soc_get_dai_id(struct device_node *ep)
3682{
3683 struct snd_soc_component *pos;
3684 struct device_node *node;
3685 int ret;
3686
3687 node = of_graph_get_port_parent(ep);
3688
3689 /*
3690 * For example HDMI case, HDMI has video/sound port,
3691 * but ALSA SoC needs sound port number only.
3692 * Thus counting HDMI DT port/endpoint doesn't work.
3693 * Then, it should have .of_xlate_dai_id
3694 */
3695 ret = -ENOTSUPP;
3696 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003697 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003698 struct device_node *component_of_node = pos->dev->of_node;
3699
3700 if (!component_of_node && pos->dev->parent)
3701 component_of_node = pos->dev->parent->of_node;
3702
3703 if (component_of_node != node)
3704 continue;
3705
3706 if (pos->driver->of_xlate_dai_id)
3707 ret = pos->driver->of_xlate_dai_id(pos, ep);
3708
3709 break;
3710 }
3711 mutex_unlock(&client_mutex);
3712
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003713 of_node_put(node);
3714
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003715 return ret;
3716}
3717EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3718
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003719int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003720 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003721{
3722 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003723 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003724 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003725
3726 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003727 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003728 component_of_node = pos->dev->of_node;
3729 if (!component_of_node && pos->dev->parent)
3730 component_of_node = pos->dev->parent->of_node;
3731
3732 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003733 continue;
3734
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003735 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003736 ret = pos->driver->of_xlate_dai_name(pos,
3737 args,
3738 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003739 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003740 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003741 int id = -1;
3742
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003743 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003744 case 0:
3745 id = 0; /* same as dai_drv[0] */
3746 break;
3747 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003748 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003749 break;
3750 default:
3751 /* not supported */
3752 break;
3753 }
3754
3755 if (id < 0 || id >= pos->num_dai) {
3756 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003757 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003758 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003759
3760 ret = 0;
3761
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003762 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003763 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003764 if (id == 0)
3765 break;
3766 id--;
3767 }
3768
3769 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003770 if (!*dai_name)
3771 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003772 }
3773
Kuninori Morimotocb470082013-09-10 17:39:56 -07003774 break;
3775 }
3776 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003777 return ret;
3778}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003779EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003780
3781int snd_soc_of_get_dai_name(struct device_node *of_node,
3782 const char **dai_name)
3783{
3784 struct of_phandle_args args;
3785 int ret;
3786
3787 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3788 "#sound-dai-cells", 0, &args);
3789 if (ret)
3790 return ret;
3791
3792 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003793
3794 of_node_put(args.np);
3795
3796 return ret;
3797}
3798EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3799
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003800/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003801 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3802 * @dai_link: DAI link
3803 *
3804 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3805 */
3806void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3807{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003808 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003809 int index;
3810
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003811 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003812 if (!component->of_node)
3813 break;
3814 of_node_put(component->of_node);
3815 component->of_node = NULL;
3816 }
3817}
3818EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3819
3820/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003821 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3822 * @dev: Card device
3823 * @of_node: Device node
3824 * @dai_link: DAI link
3825 *
3826 * Builds an array of CODEC DAI components from the DAI link property
3827 * 'sound-dai'.
3828 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003829 * The device nodes in the array (of_node) must be dereferenced by calling
3830 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003831 *
3832 * Returns 0 for success
3833 */
3834int snd_soc_of_get_dai_link_codecs(struct device *dev,
3835 struct device_node *of_node,
3836 struct snd_soc_dai_link *dai_link)
3837{
3838 struct of_phandle_args args;
3839 struct snd_soc_dai_link_component *component;
3840 char *name;
3841 int index, num_codecs, ret;
3842
3843 /* Count the number of CODECs */
3844 name = "sound-dai";
3845 num_codecs = of_count_phandle_with_args(of_node, name,
3846 "#sound-dai-cells");
3847 if (num_codecs <= 0) {
3848 if (num_codecs == -ENOENT)
3849 dev_err(dev, "No 'sound-dai' property\n");
3850 else
3851 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3852 return num_codecs;
3853 }
Kees Cooka86854d2018-06-12 14:07:58 -07003854 component = devm_kcalloc(dev,
3855 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003856 GFP_KERNEL);
3857 if (!component)
3858 return -ENOMEM;
3859 dai_link->codecs = component;
3860 dai_link->num_codecs = num_codecs;
3861
3862 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003863 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003864 ret = of_parse_phandle_with_args(of_node, name,
3865 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003866 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003867 if (ret)
3868 goto err;
3869 component->of_node = args.np;
3870 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3871 if (ret < 0)
3872 goto err;
3873 }
3874 return 0;
3875err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003876 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003877 dai_link->codecs = NULL;
3878 dai_link->num_codecs = 0;
3879 return ret;
3880}
3881EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3882
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003883static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003884{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003885 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003886 snd_soc_util_init();
3887
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003888 return platform_driver_register(&soc_driver);
3889}
Mark Brown4abe8e12010-10-12 17:41:03 +01003890module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003891
Mark Brown7d8c16a2008-11-30 22:11:24 +00003892static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003893{
Mark Brownfb257892011-04-28 10:57:54 +01003894 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003895 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003896
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003897 platform_driver_unregister(&soc_driver);
3898}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003899module_exit(snd_soc_exit);
3900
3901/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003902MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003903MODULE_DESCRIPTION("ALSA SoC Core");
3904MODULE_LICENSE("GPL");
3905MODULE_ALIAS("platform:soc-audio");