blob: 75f6a8085a766f29f73d045dbe611bbe9c7b1ba1 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020069/*
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080072 * as invalid and dropped when setting the card long name from DMI info.
73 */
74static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
76 "TBD by OEM",
77 "Default String",
78 "Board Manufacturer",
79 "Board Vendor Name",
80 "Board Product Name",
81 NULL, /* terminator */
82};
83
Mark Browndbe21402010-02-12 11:37:24 +000084static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
Mark Brown36ae1a92012-01-06 17:12:45 -080087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000090}
91
92static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070097 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000098
Jingoo Hanb785a492013-07-19 16:24:59 +090099 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700100 if (ret)
101 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000102
103 return count;
104}
105
106static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 &dev_attr_pmdown_time.attr,
110 NULL
111};
112
113static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
115{
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100122}
123
124static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
Mark Brownf7e73b262018-03-09 12:46:27 +0000129static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
132};
133
134static const struct attribute_group *soc_dev_attr_groups[] = {
135 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000136 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 NULL
138};
139
Mark Brown2624d5f2009-11-03 21:56:13 +0000140#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200143 if (!component->card->debugfs_card_root)
144 return;
145
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200146 if (component->debugfs_prefix) {
147 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100148
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
151 if (name) {
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
154 kfree(name);
155 }
156 } else {
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
159 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 return;
165 }
166
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169}
170
171static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
172{
173 debugfs_remove_recursive(component->debugfs_root);
174}
175
Peng Donglinc15b2a12018-02-14 22:48:07 +0800176static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100177{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100178 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100179 struct snd_soc_dai *dai;
180
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100181 mutex_lock(&client_mutex);
182
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000183 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000184 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800185 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100186
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100187 mutex_unlock(&client_mutex);
188
Donglin Peng700c17c2018-01-18 13:31:26 +0800189 return 0;
190}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800191DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100192
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000193static int component_list_show(struct seq_file *m, void *v)
194{
195 struct snd_soc_component *component;
196
197 mutex_lock(&client_mutex);
198
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000199 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200 seq_printf(m, "%s\n", component->name);
201
202 mutex_unlock(&client_mutex);
203
204 return 0;
205}
206DEFINE_SHOW_ATTRIBUTE(component_list);
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208static void soc_init_card_debugfs(struct snd_soc_card *card)
209{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200210 if (!snd_soc_debugfs_root)
211 return;
212
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000214 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200215 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100217 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200218 return;
219 }
220
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
223 &card->pop_time);
224 if (!card->debugfs_pop_time)
225 dev_warn(card->dev,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200226 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200227}
228
229static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230{
231 debugfs_remove_recursive(card->debugfs_card_root);
232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
240 return;
241 }
242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
244 &dai_list_fops))
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000246
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250}
251
252static void snd_soc_debugfs_exit(void)
253{
254 debugfs_remove_recursive(snd_soc_debugfs_root);
255}
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257#else
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261{
262}
263
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200264static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000266{
267}
268
Axel Linb95fccb2010-11-09 17:06:44 +0800269static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270{
271}
272
273static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274{
275}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200276
277static inline void snd_soc_debugfs_init(void)
278{
279}
280
281static inline void snd_soc_debugfs_exit(void)
282{
283}
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285#endif
286
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000287static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
289{
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
292
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
296 return 0;
297 }
298
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300 if (!new_rtdcom)
301 return -ENOMEM;
302
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
305
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
307
308 return 0;
309}
310
311static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312{
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316 kfree(rtdcom1);
317
318 INIT_LIST_HEAD(&rtd->component_list);
319}
320
321struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
323{
324 struct snd_soc_rtdcom_list *rtdcom;
325
Kuninori Morimoto971da242018-01-23 00:41:24 +0000326 if (!driver_name)
327 return NULL;
328
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000329 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000330 const char *component_name = rtdcom->component->driver->name;
331
332 if (!component_name)
333 continue;
334
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000337 return rtdcom->component;
338 }
339
340 return NULL;
341}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000342EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100344struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
346{
Mengdong Lin1a497982015-11-18 02:34:11 -0500347 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000349 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100353 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100355 return NULL;
356}
357EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
358
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000359static const struct snd_soc_ops null_snd_soc_ops;
360
Mengdong Lin1a497982015-11-18 02:34:11 -0500361static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
363{
364 struct snd_soc_pcm_runtime *rtd;
365
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
367 if (!rtd)
368 return NULL;
369
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000370 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500371 rtd->card = card;
372 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
375
Kees Cook6396bb22018-06-12 14:03:40 -0700376 rtd->codec_dais = kcalloc(dai_link->num_codecs,
377 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500378 GFP_KERNEL);
379 if (!rtd->codec_dais) {
380 kfree(rtd);
381 return NULL;
382 }
383
384 return rtd;
385}
386
387static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
388{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000389 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000390 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500391 kfree(rtd);
392}
393
394static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
396{
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
399 card->num_rtd++;
400}
401
402static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
403{
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
405
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000406 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
409 }
410
411 card->num_rtd = 0;
412}
413
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100414struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
416{
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100418
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000419 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500420 if (!strcmp(rtd->dai_link->name, dai_link))
421 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100422 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424 return NULL;
425}
426EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
427
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900428static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
429{
430 struct snd_soc_pcm_runtime *rtd;
431
432 for_each_card_rtds(card, rtd)
433 flush_delayed_work(&rtd->delayed_work);
434}
435
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100436static void codec2codec_close_delayed_work(struct work_struct *work)
437{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200438 /*
439 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100440 * Since c2c links are internal nodes in the DAPM graph and
441 * don't interface with the outside world or application layer
442 * we don't have to do any special handling on close.
443 */
444}
445
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000446#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000448int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000450 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000451 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500452 struct snd_soc_pcm_runtime *rtd;
453 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200455 /* If the card is not initialized yet there is nothing to do */
456 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200457 return 0;
458
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200459 /*
460 * Due to the resume being scheduled into a workqueue we could
461 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100462 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100464
465 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100467
Mark Browna00f90f2010-12-02 16:24:24 +0000468 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000469 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000470 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100471
Mengdong Lin1a497982015-11-18 02:34:11 -0500472 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100473 continue;
474
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000475 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200476 struct snd_soc_dai_driver *drv = dai->driver;
477
478 if (drv->ops->digital_mute && dai->playback_active)
479 drv->ops->digital_mute(dai, 1);
480 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 }
482
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100483 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000484 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500485 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100486 continue;
487
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100489 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100490
Mark Brown87506542008-11-18 20:50:34 +0000491 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000492 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000494 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100496
Mengdong Lin1a497982015-11-18 02:34:11 -0500497 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100498 continue;
499
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100500 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100502 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200504 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900505 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100506
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000507 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100510 continue;
511
Mengdong Lin1a497982015-11-18 02:34:11 -0500512 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800513 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800514 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515
Mengdong Lin1a497982015-11-18 02:34:11 -0500516 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800518 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 }
520
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200521 /* Recheck all endpoints too, their state is affected by suspend */
522 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700523 snd_soc_dapm_sync(&card->dapm);
524
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000525 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000526 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200527 struct snd_soc_dapm_context *dapm =
528 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000529
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200530 /*
531 * If there are paths active then the COMPONENT will be held
532 * with bias _ON and should not be suspended.
533 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000534 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200535 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000537 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000538 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000539 * bias off then being in STANDBY
540 * means it's doing something,
541 * otherwise fall through.
542 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200543 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000544 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200545 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000546 break;
547 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500548 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000551 if (component->driver->suspend)
552 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000553 component->suspended = 1;
554 if (component->regmap)
555 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800556 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000557 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 break;
559 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000560 dev_dbg(component->dev,
561 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 break;
563 }
564 }
565 }
566
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000567 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569
Mengdong Lin1a497982015-11-18 02:34:11 -0500570 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 continue;
572
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100573 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800575
576 /* deactivate pins to sleep state */
577 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578 }
579
Mark Brown87506542008-11-18 20:50:34 +0000580 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000581 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
583 return 0;
584}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000585EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200587/*
588 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100589 * setting our codec back up, which can be very slow on I2C
590 */
591static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000593 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200594 container_of(work, struct snd_soc_card,
595 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500596 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000597 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500598 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200600 /*
601 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100602 * so userspace apps are blocked from touching us
603 */
604
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000605 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100606
Mark Brown9949788b2010-05-07 20:24:05 +0100607 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100609
Mark Brown87506542008-11-18 20:50:34 +0000610 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000611 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100613 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000614 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500615 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100616
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100618 continue;
619
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100620 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622 }
623
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000624 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000625 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000626 if (component->driver->resume)
627 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000628 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100629 }
630 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000632 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100633
Mengdong Lin1a497982015-11-18 02:34:11 -0500634 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100635 continue;
636
Mengdong Lin1a497982015-11-18 02:34:11 -0500637 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000638 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800639 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640
Mengdong Lin1a497982015-11-18 02:34:11 -0500641 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000642 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800643 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 }
645
Mark Brown3ff3f642008-05-19 12:32:25 +0200646 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000647 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000648 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100649
Mengdong Lin1a497982015-11-18 02:34:11 -0500650 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100651 continue;
652
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000653 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200654 struct snd_soc_dai_driver *drv = dai->driver;
655
656 if (drv->ops->digital_mute && dai->playback_active)
657 drv->ops->digital_mute(dai, 0);
658 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 }
660
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000661 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100665 continue;
666
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100667 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 }
670
Mark Brown87506542008-11-18 20:50:34 +0000671 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000672 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000674 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100675
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200676 /* Recheck all endpoints too, their state is affected by suspend */
677 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700678 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530679
680 /* userspace can access us now we are back as we were before */
681 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100682}
683
684/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000685int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100686{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000687 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100688 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500689 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200690
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200691 /* If the card is not initialized yet there is nothing to do */
692 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800693 return 0;
694
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800695 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000696 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000697 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
699 int j;
700
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800701 if (cpu_dai->active)
702 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000704 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200705 if (codec_dai->active)
706 pinctrl_pm_select_default_state(codec_dai->dev);
707 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800708 }
709
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100710 /*
711 * DAIs that also act as the control bus master might have other drivers
712 * hanging off them so need to resume immediately. Other drivers don't
713 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100714 * due to I/O costs and anti-pop so handle them out of line.
715 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000716 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200718
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100719 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600720 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100721 if (bus_control) {
722 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600723 soc_resume_deferred(&card->deferred_resume_work);
724 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000725 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000727 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100728 }
Andy Green6ed25972008-06-13 16:24:05 +0100729
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 return 0;
731}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000732EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734#define snd_soc_suspend NULL
735#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#endif
737
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100738static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800739};
740
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200741static struct snd_soc_component *soc_find_component(
742 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100743{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200744 struct snd_soc_component *component;
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000745 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100746
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100747 lockdep_assert_held(&client_mutex);
748
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000749 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200750 if (of_node) {
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000751 component_of_node = component->dev->of_node;
752 if (!component_of_node && component->dev->parent)
753 component_of_node = component->dev->parent->of_node;
754
755 if (component_of_node == of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200756 return component;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000757 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200758 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100759 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100760 }
761
762 return NULL;
763}
764
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000765static int snd_soc_is_matching_component(
766 const struct snd_soc_dai_link_component *dlc,
767 struct snd_soc_component *component)
768{
769 struct device_node *component_of_node;
770
771 component_of_node = component->dev->of_node;
772 if (!component_of_node && component->dev->parent)
773 component_of_node = component->dev->parent->of_node;
774
775 if (dlc->of_node && component_of_node != dlc->of_node)
776 return 0;
777 if (dlc->name && strcmp(component->name, dlc->name))
778 return 0;
779
780 return 1;
781}
782
Mengdong Linfbb88b52016-04-22 12:25:33 +0800783/**
784 * snd_soc_find_dai - Find a registered DAI
785 *
Jeffy Chen49584712017-08-22 15:57:21 +0800786 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800787 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700788 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800789 * find the DAI of the same name. The component's of_node and name
790 * should also match if being specified.
791 *
792 * Return: pointer of DAI, or NULL if not found.
793 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800794struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200795 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100796{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200797 struct snd_soc_component *component;
798 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100799
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100800 lockdep_assert_held(&client_mutex);
801
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200802 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000803 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000804 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200805 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000806 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800807 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800808 && (!dai->driver->name
809 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100810 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100811
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200812 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100813 }
814 }
815
816 return NULL;
817}
Mengdong Lin305e9022016-04-19 13:12:25 +0800818EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100819
Mengdong Lin17fb17552016-11-03 01:04:12 +0800820/**
821 * snd_soc_find_dai_link - Find a DAI link
822 *
823 * @card: soc card
824 * @id: DAI link ID to match
825 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000826 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800827 *
828 * This function will search all existing DAI links of the soc card to
829 * find the link of the same ID. Since DAI links may not have their
830 * unique ID, so name and stream name should also match if being
831 * specified.
832 *
833 * Return: pointer of DAI link, or NULL if not found.
834 */
835struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
836 int id, const char *name,
837 const char *stream_name)
838{
839 struct snd_soc_dai_link *link, *_link;
840
841 lockdep_assert_held(&client_mutex);
842
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000843 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800844 if (link->id != id)
845 continue;
846
847 if (name && (!link->name || strcmp(name, link->name)))
848 continue;
849
850 if (stream_name && (!link->stream_name
851 || strcmp(stream_name, link->stream_name)))
852 continue;
853
854 return link;
855 }
856
857 return NULL;
858}
859EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
860
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800861static bool soc_is_dai_link_bound(struct snd_soc_card *card,
862 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800864 struct snd_soc_pcm_runtime *rtd;
865
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000866 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800867 if (rtd->dai_link == dai_link)
868 return true;
869 }
870
871 return false;
872}
873
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500874static int soc_bind_dai_link(struct snd_soc_card *card,
875 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876{
Mengdong Lin1a497982015-11-18 02:34:11 -0500877 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900878 struct snd_soc_dai_link_component *codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200879 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000880 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500881 struct snd_soc_dai **codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200882 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200883
Liam Girdwooda655de82018-07-02 16:59:54 +0100884 if (dai_link->ignore)
885 return 0;
886
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500887 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000888
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800889 if (soc_is_dai_link_bound(card, dai_link)) {
890 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
891 dai_link->name);
892 return 0;
893 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530895 rtd = soc_new_pcm_runtime(card, dai_link);
896 if (!rtd)
897 return -ENOMEM;
898
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200899 cpu_dai_component.name = dai_link->cpu_name;
900 cpu_dai_component.of_node = dai_link->cpu_of_node;
901 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
902 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000903 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100904 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
905 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500906 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000907 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000908 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000909
Benoit Cousson88bd8702014-07-08 23:19:34 +0200910 rtd->num_codecs = dai_link->num_codecs;
911
912 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500913 codec_dais = rtd->codec_dais;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900914 for_each_link_codecs(dai_link, i, codecs) {
915 codec_dais[i] = snd_soc_find_dai(codecs);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200916 if (!codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100917 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900918 codecs->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500919 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000921 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000922 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100923
Benoit Cousson88bd8702014-07-08 23:19:34 +0200924 /* Single codec links expect codec and codec_dai in runtime data */
925 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100926
Mark Brownb19e6e72012-03-14 21:18:39 +0000927 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000928 for_each_component(component) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900929 if (!snd_soc_is_matching_component(dai_link->platforms,
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000930 component))
931 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000932
933 snd_soc_rtdcom_add(rtd, component);
934 }
935
Mengdong Lin1a497982015-11-18 02:34:11 -0500936 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000937 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500938
939_err_defer:
940 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200941 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000942}
943
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900944static void soc_cleanup_component(struct snd_soc_component *component)
945{
946 list_del(&component->card_list);
947 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
948 soc_cleanup_component_debugfs(component);
949 component->card = NULL;
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -0600950 if (!component->driver->ignore_module_refcount)
951 module_put(component->dev->driver->owner);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900952}
953
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200954static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600955{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200956 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200957 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600958
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000959 if (component->driver->remove)
960 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600961
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900962 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600963}
964
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200965static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200966{
967 int err;
968
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600969 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000970 dai->driver->remove_order != order)
971 return;
972
973 if (dai->driver->remove) {
974 err = dai->driver->remove(dai);
975 if (err < 0)
976 dev_err(dai->dev,
977 "ASoC: failed to remove %s: %d\n",
978 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100979 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000980 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100981}
982
Mengdong Lin1a497982015-11-18 02:34:11 -0500983static void soc_remove_link_dais(struct snd_soc_card *card,
984 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200986 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000987 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988
989 /* unregister the rtd device */
990 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800991 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 rtd->dev_registered = 0;
993 }
994
995 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000996 for_each_rtd_codec_dai(rtd, i, codec_dai)
997 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200999 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000}
1001
Mengdong Lin1a497982015-11-18 02:34:11 -05001002static void soc_remove_link_components(struct snd_soc_card *card,
1003 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001004{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001005 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001006 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001008 for_each_rtdcom(rtd, rtdcom) {
1009 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001010
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001011 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001012 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001013 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001014}
1015
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001016static void soc_remove_dai_links(struct snd_soc_card *card)
1017{
Mengdong Lin1a497982015-11-18 02:34:11 -05001018 int order;
1019 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001020 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001021
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001022 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001023 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001024 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001025 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001026
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001027 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001028 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001029 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001030 }
1031
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001032 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001033 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1034 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1035 link->name);
1036
1037 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001038 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001039}
1040
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001041static int snd_soc_init_platform(struct snd_soc_card *card,
1042 struct snd_soc_dai_link *dai_link)
1043{
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001044 struct snd_soc_dai_link_component *platform = dai_link->platforms;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001045
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001046 /*
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001047 * REMOVE ME
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001048 *
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001049 * This is glue code for Legacy vs Modern dai_link.
1050 * This function will be removed if all derivers are switched to
1051 * modern style dai_link.
1052 * Driver shouldn't use both legacy and modern style in the same time.
1053 * see
1054 * soc.h :: struct snd_soc_dai_link
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001055 */
1056 /* convert Legacy platform link */
Curtis Malainey78a24e12019-01-29 13:47:09 -08001057 if (!platform) {
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001058 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001059 sizeof(struct snd_soc_dai_link_component),
1060 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001061 if (!platform)
1062 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001063
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001064 dai_link->platforms = platform;
1065 dai_link->num_platforms = 1;
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001066 dai_link->legacy_platform = 1;
1067 platform->name = dai_link->platform_name;
1068 platform->of_node = dai_link->platform_of_node;
1069 platform->dai_name = NULL;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001070 }
1071
1072 /* if there's no platform we match on the empty platform */
1073 if (!platform->name &&
1074 !platform->of_node)
1075 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001076
1077 return 0;
1078}
1079
Curtis Malainey78a24e12019-01-29 13:47:09 -08001080static void soc_cleanup_platform(struct snd_soc_card *card)
1081{
1082 struct snd_soc_dai_link *link;
1083 int i;
1084 /*
1085 * FIXME
1086 *
1087 * this function should be removed with snd_soc_init_platform
1088 */
1089
1090 for_each_card_prelinks(card, i, link) {
1091 if (link->legacy_platform) {
1092 link->legacy_platform = 0;
1093 link->platforms = NULL;
1094 }
1095 }
1096}
1097
Mengdong Lin923c5e612015-11-23 11:01:49 -05001098static int snd_soc_init_multicodec(struct snd_soc_card *card,
1099 struct snd_soc_dai_link *dai_link)
1100{
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001101 /*
1102 * REMOVE ME
1103 *
1104 * This is glue code for Legacy vs Modern dai_link.
1105 * This function will be removed if all derivers are switched to
1106 * modern style dai_link.
1107 * Driver shouldn't use both legacy and modern style in the same time.
1108 * see
1109 * soc.h :: struct snd_soc_dai_link
1110 */
1111
Mengdong Lin923c5e612015-11-23 11:01:49 -05001112 /* Legacy codec/codec_dai link is a single entry in multicodec */
1113 if (dai_link->codec_name || dai_link->codec_of_node ||
1114 dai_link->codec_dai_name) {
1115 dai_link->num_codecs = 1;
1116
1117 dai_link->codecs = devm_kzalloc(card->dev,
1118 sizeof(struct snd_soc_dai_link_component),
1119 GFP_KERNEL);
1120 if (!dai_link->codecs)
1121 return -ENOMEM;
1122
1123 dai_link->codecs[0].name = dai_link->codec_name;
1124 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1125 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1126 }
1127
1128 if (!dai_link->codecs) {
1129 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1130 return -EINVAL;
1131 }
1132
1133 return 0;
1134}
1135
1136static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001137 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001138{
1139 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001140 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001141
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001142 ret = snd_soc_init_platform(card, link);
1143 if (ret) {
1144 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1145 return ret;
1146 }
1147
Mengdong Lin923c5e612015-11-23 11:01:49 -05001148 ret = snd_soc_init_multicodec(card, link);
1149 if (ret) {
1150 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1151 return ret;
1152 }
1153
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001154 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001155 /*
1156 * Codec must be specified by 1 of name or OF node,
1157 * not both or neither.
1158 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001159 if (!!codec->name ==
1160 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001161 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1162 link->name);
1163 return -EINVAL;
1164 }
1165 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001166 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001167 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1168 link->name);
1169 return -EINVAL;
1170 }
1171 }
1172
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001173 /* FIXME */
1174 if (link->num_platforms > 1) {
1175 dev_err(card->dev,
1176 "ASoC: multi platform is not yet supported %s\n",
1177 link->name);
1178 return -EINVAL;
1179 }
1180
Mengdong Lin923c5e612015-11-23 11:01:49 -05001181 /*
1182 * Platform may be specified by either name or OF node, but
1183 * can be left unspecified, and a dummy platform will be used.
1184 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001185 if (link->platforms->name && link->platforms->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001186 dev_err(card->dev,
1187 "ASoC: Both platform name/of_node are set for %s\n",
1188 link->name);
1189 return -EINVAL;
1190 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301191
1192 /*
1193 * Defer card registartion if platform dai component is not added to
1194 * component list.
1195 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001196 if ((link->platforms->of_node || link->platforms->name) &&
1197 !soc_find_component(link->platforms->of_node, link->platforms->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301198 return -EPROBE_DEFER;
1199
Mengdong Lin923c5e612015-11-23 11:01:49 -05001200 /*
1201 * CPU device may be specified by either name or OF node, but
1202 * can be left unspecified, and will be matched based on DAI
1203 * name alone..
1204 */
1205 if (link->cpu_name && link->cpu_of_node) {
1206 dev_err(card->dev,
1207 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1208 link->name);
1209 return -EINVAL;
1210 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301211
1212 /*
1213 * Defer card registartion if cpu dai component is not added to
1214 * component list.
1215 */
Matthias Reichl28335482019-01-15 17:51:07 +01001216 if ((link->cpu_of_node || link->cpu_name) &&
1217 !soc_find_component(link->cpu_of_node, link->cpu_name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301218 return -EPROBE_DEFER;
1219
Mengdong Lin923c5e612015-11-23 11:01:49 -05001220 /*
1221 * At least one of CPU DAI name or CPU device name/node must be
1222 * specified
1223 */
1224 if (!link->cpu_dai_name &&
1225 !(link->cpu_name || link->cpu_of_node)) {
1226 dev_err(card->dev,
1227 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1228 link->name);
1229 return -EINVAL;
1230 }
1231
1232 return 0;
1233}
1234
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001235void snd_soc_disconnect_sync(struct device *dev)
1236{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001237 struct snd_soc_component *component =
1238 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001239
1240 if (!component || !component->card)
1241 return;
1242
1243 snd_card_disconnect_sync(component->card->snd_card);
1244}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001245EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001246
Mengdong Linf8f80362015-12-02 14:11:22 +08001247/**
1248 * snd_soc_add_dai_link - Add a DAI link dynamically
1249 * @card: The ASoC card to which the DAI link is added
1250 * @dai_link: The new DAI link to add
1251 *
1252 * This function adds a DAI link to the ASoC card's link list.
1253 *
1254 * Note: Topology can use this API to add DAI links when probing the
1255 * topology component. And machine drivers can still define static
1256 * DAI links in dai_link array.
1257 */
1258int snd_soc_add_dai_link(struct snd_soc_card *card,
1259 struct snd_soc_dai_link *dai_link)
1260{
1261 if (dai_link->dobj.type
1262 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1263 dev_err(card->dev, "Invalid dai link type %d\n",
1264 dai_link->dobj.type);
1265 return -EINVAL;
1266 }
1267
1268 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001269 /*
1270 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001271 * on the link created by topology.
1272 */
1273 if (dai_link->dobj.type && card->add_dai_link)
1274 card->add_dai_link(card, dai_link);
1275
Mengdong Linf8f80362015-12-02 14:11:22 +08001276 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001277
1278 return 0;
1279}
1280EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1281
1282/**
1283 * snd_soc_remove_dai_link - Remove a DAI link from the list
1284 * @card: The ASoC card that owns the link
1285 * @dai_link: The DAI link to remove
1286 *
1287 * This function removes a DAI link from the ASoC card's link list.
1288 *
1289 * For DAI links previously added by topology, topology should
1290 * remove them by using the dobj embedded in the link.
1291 */
1292void snd_soc_remove_dai_link(struct snd_soc_card *card,
1293 struct snd_soc_dai_link *dai_link)
1294{
1295 struct snd_soc_dai_link *link, *_link;
1296
1297 if (dai_link->dobj.type
1298 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1299 dev_err(card->dev, "Invalid dai link type %d\n",
1300 dai_link->dobj.type);
1301 return;
1302 }
1303
1304 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001305 /*
1306 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001307 * on the link created by topology.
1308 */
1309 if (dai_link->dobj.type && card->remove_dai_link)
1310 card->remove_dai_link(card, dai_link);
1311
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001312 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001313 if (link == dai_link) {
1314 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001315 return;
1316 }
1317 }
1318}
1319EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1320
Jerome Brunetaefba452018-07-13 14:50:43 +02001321static void soc_set_of_name_prefix(struct snd_soc_component *component)
1322{
1323 struct device_node *component_of_node = component->dev->of_node;
1324 const char *str;
1325 int ret;
1326
1327 if (!component_of_node && component->dev->parent)
1328 component_of_node = component->dev->parent->of_node;
1329
1330 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1331 &str);
1332 if (!ret)
1333 component->name_prefix = str;
1334}
1335
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001336static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001337 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001338{
1339 int i;
1340
Jerome Brunetaefba452018-07-13 14:50:43 +02001341 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001342 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001343 struct device_node *component_of_node = component->dev->of_node;
1344
1345 if (!component_of_node && component->dev->parent)
1346 component_of_node = component->dev->parent->of_node;
1347
1348 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001349 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001350 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001351 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001352 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001353 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001354 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001355
1356 /*
1357 * If there is no configuration table or no match in the table,
1358 * check if a prefix is provided in the node
1359 */
1360 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001361}
1362
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001363static int soc_probe_component(struct snd_soc_card *card,
1364 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001365{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001366 struct snd_soc_dapm_context *dapm =
1367 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001368 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001369 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001370
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001371 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001372 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001374 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001375 if (component->card != card) {
1376 dev_err(component->dev,
1377 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1378 card->name, component->card->name);
1379 return -ENODEV;
1380 }
1381 return 0;
1382 }
1383
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -06001384 if (!component->driver->ignore_module_refcount &&
1385 !try_module_get(component->dev->driver->owner))
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001386 return -ENODEV;
1387
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001388 component->card = card;
1389 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001390 INIT_LIST_HEAD(&component->card_list);
1391 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001392 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001393
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001394 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001395
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001396 if (component->driver->dapm_widgets) {
1397 ret = snd_soc_dapm_new_controls(dapm,
1398 component->driver->dapm_widgets,
1399 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001400
1401 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001402 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001403 "Failed to create new controls %d\n", ret);
1404 goto err_probe;
1405 }
1406 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001407
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001408 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001409 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001410 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001411 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001412 "Failed to create DAI widgets %d\n", ret);
1413 goto err_probe;
1414 }
1415 }
Mark Brown888df392012-02-16 19:37:51 -08001416
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001417 if (component->driver->probe) {
1418 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001419 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001420 dev_err(component->dev,
1421 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001422 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001423 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001424
1425 WARN(dapm->idle_bias_off &&
1426 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001427 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001428 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001429 }
1430
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001431 /* machine specific init */
1432 if (component->init) {
1433 ret = component->init(component);
1434 if (ret < 0) {
1435 dev_err(component->dev,
1436 "Failed to do machine specific init %d\n", ret);
1437 goto err_probe;
1438 }
1439 }
1440
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001441 if (component->driver->controls)
1442 snd_soc_add_component_controls(component,
1443 component->driver->controls,
1444 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001445 if (component->driver->dapm_routes)
1446 snd_soc_dapm_add_routes(dapm,
1447 component->driver->dapm_routes,
1448 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001449
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001450 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001451 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001452 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001453
Jarkko Nikula70d293312011-01-27 16:24:22 +02001454err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001455 if (ret < 0)
1456 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001457
1458 return ret;
1459}
1460
Mark Brown36ae1a92012-01-06 17:12:45 -08001461static void rtd_release(struct device *dev)
1462{
1463 kfree(dev);
1464}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001465
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001466static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1467 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001468{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001469 int ret = 0;
1470
Jarkko Nikula589c3562010-12-06 16:27:07 +02001471 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001472 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1473 if (!rtd->dev)
1474 return -ENOMEM;
1475 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001476 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001477 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001478 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001479 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001480 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001481 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001482 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1483 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1484 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1485 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001486 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001487 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001488 /* calling put_device() here to free the rtd->dev */
1489 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001490 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001491 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001492 return ret;
1493 }
1494 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001495 return 0;
1496}
1497
Mengdong Lin1a497982015-11-18 02:34:11 -05001498static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001499 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001500{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001501 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001502 struct snd_soc_rtdcom_list *rtdcom;
1503 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001504
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001505 for_each_rtdcom(rtd, rtdcom) {
1506 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001507
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001508 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001509 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001510 if (ret < 0)
1511 return ret;
1512 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001513 }
1514
Stephen Warren62ae68f2012-06-08 12:34:23 -06001515 return 0;
1516}
1517
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001518static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001519{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001520 if (dai->probed ||
1521 dai->driver->probe_order != order)
1522 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001523
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001524 if (dai->driver->probe) {
1525 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001526
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001527 if (ret < 0) {
1528 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1529 dai->name, ret);
1530 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001531 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001532 }
1533
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001534 dai->probed = 1;
1535
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001536 return 0;
1537}
1538
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001539static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1540 struct snd_soc_pcm_runtime *rtd)
1541{
1542 int i, ret = 0;
1543
1544 for (i = 0; i < num_dais; ++i) {
1545 struct snd_soc_dai_driver *drv = dais[i]->driver;
1546
Rohit kumarde17f142018-11-01 18:08:49 +05301547 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001548 ret = drv->pcm_new(rtd, dais[i]);
1549 if (ret < 0) {
1550 dev_err(dais[i]->dev,
1551 "ASoC: Failed to bind %s with pcm device\n",
1552 dais[i]->name);
1553 return ret;
1554 }
1555 }
1556
1557 return 0;
1558}
1559
Mengdong Lin1a497982015-11-18 02:34:11 -05001560static int soc_probe_link_dais(struct snd_soc_card *card,
1561 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562{
Mengdong Lin1a497982015-11-18 02:34:11 -05001563 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001564 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001565 struct snd_soc_rtdcom_list *rtdcom;
1566 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001567 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001568 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001569
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001570 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001571 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 /* set default power off timeout */
1574 rtd->pmdown_time = pmdown_time;
1575
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001576 ret = soc_probe_dai(cpu_dai, order);
1577 if (ret)
1578 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001581 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1582 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001583 if (ret)
1584 return ret;
1585 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001586
Liam Girdwood0168bf02011-06-07 16:08:05 +01001587 /* complete DAI probe during last probe */
1588 if (order != SND_SOC_COMP_ORDER_LAST)
1589 return 0;
1590
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001591 /* do machine specific initialization */
1592 if (dai_link->init) {
1593 ret = dai_link->init(rtd);
1594 if (ret < 0) {
1595 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1596 dai_link->name, ret);
1597 return ret;
1598 }
1599 }
1600
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001601 if (dai_link->dai_fmt)
1602 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1603
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001604 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001605 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001607
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001608#ifdef CONFIG_DEBUG_FS
1609 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001610 if (dai_link->dynamic)
1611 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001612#endif
1613
Liam Girdwooda655de82018-07-02 16:59:54 +01001614 num = rtd->num;
1615
1616 /*
1617 * most drivers will register their PCMs using DAI link ordering but
1618 * topology based drivers can use the DAI link id field to set PCM
1619 * device number and then use rtd + a base offset of the BEs.
1620 */
1621 for_each_rtdcom(rtd, rtdcom) {
1622 component = rtdcom->component;
1623
1624 if (!component->driver->use_dai_pcm_id)
1625 continue;
1626
1627 if (rtd->dai_link->no_pcm)
1628 num += component->driver->be_pcm_base;
1629 else
1630 num = rtd->dai_link->id;
1631 }
1632
Jie Yang6f0c4222015-10-13 23:41:00 +08001633 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001634 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001635 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001636 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001637 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301638 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001639 return ret;
1640 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001641 } else if (!dai_link->params) {
1642 /* create the pcm */
1643 ret = soc_new_pcm(rtd, num);
1644 if (ret < 0) {
1645 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1646 dai_link->stream_name, ret);
1647 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001648 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001649 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1650 if (ret < 0)
1651 return ret;
1652 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1653 rtd->num_codecs, rtd);
1654 if (ret < 0)
1655 return ret;
1656 } else {
1657 INIT_DELAYED_WORK(&rtd->delayed_work,
1658 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001659 }
1660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661 return 0;
1662}
1663
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001664static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001665{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001666 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001667 struct snd_soc_component *component;
1668 const char *name;
1669 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001670
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001671 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1672 /* codecs, usually analog devices */
1673 name = aux_dev->codec_name;
1674 codec_of_node = aux_dev->codec_of_node;
1675 component = soc_find_component(codec_of_node, name);
1676 if (!component) {
1677 if (codec_of_node)
1678 name = of_node_full_name(codec_of_node);
1679 goto err_defer;
1680 }
1681 } else if (aux_dev->name) {
1682 /* generic components */
1683 name = aux_dev->name;
1684 component = soc_find_component(NULL, name);
1685 if (!component)
1686 goto err_defer;
1687 } else {
1688 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1689 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001690 }
1691
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001692 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001693 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001694
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001695 return 0;
1696
1697err_defer:
1698 dev_err(card->dev, "ASoC: %s not registered\n", name);
1699 return -EPROBE_DEFER;
1700}
1701
1702static int soc_probe_aux_devices(struct snd_soc_card *card)
1703{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001704 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001705 int order;
1706 int ret;
1707
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001708 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001709 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001710 if (comp->driver->probe_order == order) {
1711 ret = soc_probe_component(card, comp);
1712 if (ret < 0) {
1713 dev_err(card->dev,
1714 "ASoC: failed to probe aux component %s %d\n",
1715 comp->name, ret);
1716 return ret;
1717 }
1718 }
1719 }
1720 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001721
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001722 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001723}
1724
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001725static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001726{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001727 struct snd_soc_component *comp, *_comp;
1728 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001729
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001730 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001731 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001732 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001733
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001734 if (comp->driver->remove_order == order) {
1735 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001736 /* remove it from the card's aux_comp_list */
1737 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001738 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001739 }
1740 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001741}
1742
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001743/**
1744 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1745 * @rtd: The runtime for which the DAI link format should be changed
1746 * @dai_fmt: The new DAI link format
1747 *
1748 * This function updates the DAI link format for all DAIs connected to the DAI
1749 * link for the specified runtime.
1750 *
1751 * Note: For setups with a static format set the dai_fmt field in the
1752 * corresponding snd_dai_link struct instead of using this function.
1753 *
1754 * Returns 0 on success, otherwise a negative error code.
1755 */
1756int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1757 unsigned int dai_fmt)
1758{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001759 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001760 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001761 unsigned int i;
1762 int ret;
1763
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001764 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001765 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1766 if (ret != 0 && ret != -ENOTSUPP) {
1767 dev_warn(codec_dai->dev,
1768 "ASoC: Failed to set DAI format: %d\n", ret);
1769 return ret;
1770 }
1771 }
1772
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001773 /*
1774 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1775 * the component which has non_legacy_dai_naming is Codec
1776 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001777 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001778 unsigned int inv_dai_fmt;
1779
1780 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1781 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1782 case SND_SOC_DAIFMT_CBM_CFM:
1783 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1784 break;
1785 case SND_SOC_DAIFMT_CBM_CFS:
1786 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1787 break;
1788 case SND_SOC_DAIFMT_CBS_CFM:
1789 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1790 break;
1791 case SND_SOC_DAIFMT_CBS_CFS:
1792 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1793 break;
1794 }
1795
1796 dai_fmt = inv_dai_fmt;
1797 }
1798
1799 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1800 if (ret != 0 && ret != -ENOTSUPP) {
1801 dev_warn(cpu_dai->dev,
1802 "ASoC: Failed to set DAI format: %d\n", ret);
1803 return ret;
1804 }
1805
1806 return 0;
1807}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001808EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001809
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001810#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001811/*
1812 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001813 * separate different DMI fields in the card long name. Only number and
1814 * alphabet characters and a few separator characters are kept.
1815 */
1816static void cleanup_dmi_name(char *name)
1817{
1818 int i, j = 0;
1819
1820 for (i = 0; name[i]; i++) {
1821 if (isalnum(name[i]) || (name[i] == '.')
1822 || (name[i] == '_'))
1823 name[j++] = name[i];
1824 else if (name[i] == '-')
1825 name[j++] = '_';
1826 }
1827
1828 name[j] = '\0';
1829}
1830
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001831/*
1832 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001833 * in the black list.
1834 */
1835static int is_dmi_valid(const char *field)
1836{
1837 int i = 0;
1838
1839 while (dmi_blacklist[i]) {
1840 if (strstr(field, dmi_blacklist[i]))
1841 return 0;
1842 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001843 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001844
1845 return 1;
1846}
1847
Liam Girdwood345233d2017-01-14 16:13:02 +08001848/**
1849 * snd_soc_set_dmi_name() - Register DMI names to card
1850 * @card: The card to register DMI names
1851 * @flavour: The flavour "differentiator" for the card amongst its peers.
1852 *
1853 * An Intel machine driver may be used by many different devices but are
1854 * difficult for userspace to differentiate, since machine drivers ususally
1855 * use their own name as the card short name and leave the card long name
1856 * blank. To differentiate such devices and fix bugs due to lack of
1857 * device-specific configurations, this function allows DMI info to be used
1858 * as the sound card long name, in the format of
1859 * "vendor-product-version-board"
1860 * (Character '-' is used to separate different DMI fields here).
1861 * This will help the user space to load the device-specific Use Case Manager
1862 * (UCM) configurations for the card.
1863 *
1864 * Possible card long names may be:
1865 * DellInc.-XPS139343-01-0310JH
1866 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1867 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1868 *
1869 * This function also supports flavoring the card longname to provide
1870 * the extra differentiation, like "vendor-product-version-board-flavor".
1871 *
1872 * We only keep number and alphabet characters and a few separator characters
1873 * in the card long name since UCM in the user space uses the card long names
1874 * as card configuration directory names and AudoConf cannot support special
1875 * charactors like SPACE.
1876 *
1877 * Returns 0 on success, otherwise a negative error code.
1878 */
1879int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1880{
1881 const char *vendor, *product, *product_version, *board;
1882 size_t longname_buf_size = sizeof(card->snd_card->longname);
1883 size_t len;
1884
1885 if (card->long_name)
1886 return 0; /* long name already set by driver or from DMI */
1887
1888 /* make up dmi long name as: vendor.product.version.board */
1889 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001890 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001891 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1892 return 0;
1893 }
1894
1895 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1896 "%s", vendor);
1897 cleanup_dmi_name(card->dmi_longname);
1898
1899 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001900 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001901 len = strlen(card->dmi_longname);
1902 snprintf(card->dmi_longname + len,
1903 longname_buf_size - len,
1904 "-%s", product);
1905
1906 len++; /* skip the separator "-" */
1907 if (len < longname_buf_size)
1908 cleanup_dmi_name(card->dmi_longname + len);
1909
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001910 /*
1911 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001912 * name in the product version field
1913 */
1914 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001915 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001916 len = strlen(card->dmi_longname);
1917 snprintf(card->dmi_longname + len,
1918 longname_buf_size - len,
1919 "-%s", product_version);
1920
1921 len++;
1922 if (len < longname_buf_size)
1923 cleanup_dmi_name(card->dmi_longname + len);
1924 }
1925 }
1926
1927 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001928 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001929 len = strlen(card->dmi_longname);
1930 snprintf(card->dmi_longname + len,
1931 longname_buf_size - len,
1932 "-%s", board);
1933
1934 len++;
1935 if (len < longname_buf_size)
1936 cleanup_dmi_name(card->dmi_longname + len);
1937 } else if (!product) {
1938 /* fall back to using legacy name */
1939 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1940 return 0;
1941 }
1942
1943 /* Add flavour to dmi long name */
1944 if (flavour) {
1945 len = strlen(card->dmi_longname);
1946 snprintf(card->dmi_longname + len,
1947 longname_buf_size - len,
1948 "-%s", flavour);
1949
1950 len++;
1951 if (len < longname_buf_size)
1952 cleanup_dmi_name(card->dmi_longname + len);
1953 }
1954
1955 /* set the card long name */
1956 card->long_name = card->dmi_longname;
1957
1958 return 0;
1959}
1960EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001961#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001962
Liam Girdwooda655de82018-07-02 16:59:54 +01001963static void soc_check_tplg_fes(struct snd_soc_card *card)
1964{
1965 struct snd_soc_component *component;
1966 const struct snd_soc_component_driver *comp_drv;
1967 struct snd_soc_dai_link *dai_link;
1968 int i;
1969
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001970 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001971
1972 /* does this component override FEs ? */
1973 if (!component->driver->ignore_machine)
1974 continue;
1975
1976 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001977 if (!strcmp(component->driver->ignore_machine,
1978 card->dev->driver->name))
1979 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001980 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001981 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001982 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001983match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001984 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001985 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001986
1987 /* ignore this FE */
1988 if (dai_link->dynamic) {
1989 dai_link->ignore = true;
1990 continue;
1991 }
1992
1993 dev_info(card->dev, "info: override FE DAI link %s\n",
1994 card->dai_link[i].name);
1995
1996 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001997 if (snd_soc_init_platform(card, dai_link) < 0) {
1998 dev_err(card->dev, "init platform error");
1999 continue;
2000 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09002001 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01002002
2003 /* convert non BE into BE */
2004 dai_link->no_pcm = 1;
2005
2006 /* override any BE fixups */
2007 dai_link->be_hw_params_fixup =
2008 component->driver->be_hw_params_fixup;
2009
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002010 /*
2011 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01002012 * dai link name if it's NULL to help bind widgets.
2013 */
2014 if (!dai_link->stream_name)
2015 dai_link->stream_name = dai_link->name;
2016 }
2017
2018 /* Inform userspace we are using alternate topology */
2019 if (component->driver->topology_name_prefix) {
2020
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002021 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01002022 if (!card->topology_shortname_created) {
2023 comp_drv = component->driver;
2024
2025 snprintf(card->topology_shortname, 32, "%s-%s",
2026 comp_drv->topology_name_prefix,
2027 card->name);
2028 card->topology_shortname_created = true;
2029 }
2030
2031 /* use topology shortname */
2032 card->name = card->topology_shortname;
2033 }
2034 }
2035}
2036
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002037static int soc_cleanup_card_resources(struct snd_soc_card *card)
2038{
2039 /* free the ALSA card at first; this syncs with pending operations */
2040 if (card->snd_card)
2041 snd_card_free(card->snd_card);
2042
2043 /* remove and free each DAI */
2044 soc_remove_dai_links(card);
2045 soc_remove_pcm_runtimes(card);
Curtis Malainey78a24e12019-01-29 13:47:09 -08002046 soc_cleanup_platform(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002047
2048 /* remove auxiliary devices */
2049 soc_remove_aux_devices(card);
2050
2051 snd_soc_dapm_free(&card->dapm);
2052 soc_cleanup_card_debugfs(card);
2053
2054 /* remove the card */
2055 if (card->remove)
2056 card->remove(card);
2057
2058 return 0;
2059}
2060
Mark Brownb19e6e72012-03-14 21:18:39 +00002061static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002062{
Mengdong Lin1a497982015-11-18 02:34:11 -05002063 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002064 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002065 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002066
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002067 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002068 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002069
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002070 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2071 card->dapm.dev = card->dev;
2072 card->dapm.card = card;
2073 list_add(&card->dapm.list, &card->dapm_list);
2074
Liam Girdwooda655de82018-07-02 16:59:54 +01002075 /* check whether any platform is ignore machine FE and using topology */
2076 soc_check_tplg_fes(card);
2077
Mark Brownb19e6e72012-03-14 21:18:39 +00002078 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002079 for_each_card_prelinks(card, i, dai_link) {
2080 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002081 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002082 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002083 }
2084
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002085 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002086 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002087 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002088 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002089 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002090 }
2091
Mengdong Linf8f80362015-12-02 14:11:22 +08002092 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002093 for_each_card_prelinks(card, i, dai_link)
2094 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002097 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002098 card->owner, 0, &card->snd_card);
2099 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002100 dev_err(card->dev,
2101 "ASoC: can't create sound card for card %s: %d\n",
2102 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002103 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002104 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002105
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002106 soc_init_card_debugfs(card);
2107
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002108#ifdef CONFIG_DEBUG_FS
2109 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2110#endif
2111
Mark Brown88ee1c62011-02-02 10:43:26 +00002112#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002113 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002114 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002115#endif
Andy Green6ed25972008-06-13 16:24:05 +01002116
Mark Brown9a841eb2011-04-12 17:51:37 -07002117 if (card->dapm_widgets)
2118 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2119 card->num_dapm_widgets);
2120
Nicolin Chenf23e8602015-02-14 17:22:49 -08002121 if (card->of_dapm_widgets)
2122 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2123 card->num_of_dapm_widgets);
2124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002125 /* initialise the sound card only once */
2126 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002127 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002128 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002129 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002130 }
2131
Stephen Warren62ae68f2012-06-08 12:34:23 -06002132 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002133 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002134 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002135 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002136 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002137 dev_err(card->dev,
2138 "ASoC: failed to instantiate card %d\n",
2139 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002140 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002141 }
2142 }
2143 }
2144
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002145 /* probe auxiliary components */
2146 ret = soc_probe_aux_devices(card);
2147 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002148 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002149
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002150 /*
2151 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002152 * Components with topology may bring new DAIs and DAI links.
2153 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002154 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002155 if (soc_is_dai_link_bound(card, dai_link))
2156 continue;
2157
2158 ret = soc_init_dai_link(card, dai_link);
2159 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002160 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002161 ret = soc_bind_dai_link(card, dai_link);
2162 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002163 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002164 }
2165
Stephen Warren62ae68f2012-06-08 12:34:23 -06002166 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002167 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002168 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002169 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002170 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002171 dev_err(card->dev,
2172 "ASoC: failed to instantiate card %d\n",
2173 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002174 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002175 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002176 }
2177 }
2178
Mark Brown888df392012-02-16 19:37:51 -08002179 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002180 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002181
Mark Brownb7af1da2011-04-07 19:18:44 +09002182 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002183 snd_soc_add_card_controls(card, card->controls,
2184 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002185
Mark Brownb8ad29d2011-03-02 18:35:51 +00002186 if (card->dapm_routes)
2187 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2188 card->num_dapm_routes);
2189
Nicolin Chenf23e8602015-02-14 17:22:49 -08002190 if (card->of_dapm_routes)
2191 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2192 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002193
Takashi Iwai861886d2017-04-24 08:54:42 +02002194 /* try to set some sane longname if DMI is available */
2195 snd_soc_set_dmi_name(card, NULL);
2196
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002197 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002198 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002199 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2200 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002201 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2202 "%s", card->driver_name ? card->driver_name : card->name);
2203 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2204 switch (card->snd_card->driver[i]) {
2205 case '_':
2206 case '-':
2207 case '\0':
2208 break;
2209 default:
2210 if (!isalnum(card->snd_card->driver[i]))
2211 card->snd_card->driver[i] = '_';
2212 break;
2213 }
2214 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002215
Mark Brown28e9ad92011-03-02 18:36:34 +00002216 if (card->late_probe) {
2217 ret = card->late_probe(card);
2218 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002219 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002220 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002221 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002222 }
2223 }
2224
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002225 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002226
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002227 ret = snd_card_register(card->snd_card);
2228 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002229 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2230 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002231 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002232 }
2233
Mark Brown435c5e22008-12-04 15:32:53 +00002234 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002235 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002236 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002237
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002238probe_end:
2239 if (ret < 0)
2240 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002242 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002243 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244
Mark Brownb19e6e72012-03-14 21:18:39 +00002245 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002246}
2247
2248/* probes a new socdev */
2249static int soc_probe(struct platform_device *pdev)
2250{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002251 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002252
Vinod Koul70a7ca32011-01-14 19:22:48 +05302253 /*
2254 * no card, so machine driver should be registering card
2255 * we should not be here in that case so ret error
2256 */
2257 if (!card)
2258 return -EINVAL;
2259
Mark Brownfe4085e2012-03-02 13:07:41 +00002260 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002261 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002262 card->name);
2263
Mark Brown435c5e22008-12-04 15:32:53 +00002264 /* Bodge while we unpick instantiation */
2265 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002266
Mark Brown28d528c2012-08-09 18:45:23 +01002267 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268}
2269
2270/* removes a socdev */
2271static int soc_remove(struct platform_device *pdev)
2272{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002273 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274
Mark Brownc5af3a22008-11-28 13:29:45 +00002275 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002276 return 0;
2277}
2278
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002279int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002280{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002281 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002282 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002283
2284 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002285 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002286
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002287 /*
2288 * Flush out pmdown_time work - we actually do want to run it
2289 * now, we're shutting down so no imminent restart.
2290 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002291 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002292
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002293 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002294
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002295 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002296 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002297 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002298 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002299 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002300
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002301 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002302 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002303 pinctrl_pm_select_sleep_state(codec_dai->dev);
2304 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002305 }
2306
Mark Brown416356f2009-06-30 19:05:15 +01002307 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002308}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002309EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002310
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002311const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302312 .suspend = snd_soc_suspend,
2313 .resume = snd_soc_resume,
2314 .freeze = snd_soc_suspend,
2315 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002316 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302317 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002318};
Stephen Warrendeb26072011-04-05 19:35:30 -06002319EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002320
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321/* ASoC platform driver */
2322static struct platform_driver soc_driver = {
2323 .driver = {
2324 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002325 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 },
2327 .probe = soc_probe,
2328 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329};
2330
Mark Brown096e49d2009-07-05 15:12:22 +01002331/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332 * snd_soc_cnew - create new control
2333 * @_template: control template
2334 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002335 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002336 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337 *
2338 * Create a new mixer control from a template control.
2339 *
2340 * Returns 0 for success, else error.
2341 */
2342struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002343 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002344 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345{
2346 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002347 struct snd_kcontrol *kcontrol;
2348 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002349
2350 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351 template.index = 0;
2352
Mark Brownefb7ac32011-03-08 17:23:24 +00002353 if (!long_name)
2354 long_name = template.name;
2355
2356 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002357 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002358 if (!name)
2359 return NULL;
2360
Mark Brownefb7ac32011-03-08 17:23:24 +00002361 template.name = name;
2362 } else {
2363 template.name = long_name;
2364 }
2365
2366 kcontrol = snd_ctl_new1(&template, data);
2367
2368 kfree(name);
2369
2370 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371}
2372EXPORT_SYMBOL_GPL(snd_soc_cnew);
2373
Liam Girdwood022658b2012-02-03 17:43:09 +00002374static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2375 const struct snd_kcontrol_new *controls, int num_controls,
2376 const char *prefix, void *data)
2377{
2378 int err, i;
2379
2380 for (i = 0; i < num_controls; i++) {
2381 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002382
Liam Girdwood022658b2012-02-03 17:43:09 +00002383 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2384 control->name, prefix));
2385 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002386 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2387 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002388 return err;
2389 }
2390 }
2391
2392 return 0;
2393}
2394
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002395struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2396 const char *name)
2397{
2398 struct snd_card *card = soc_card->snd_card;
2399 struct snd_kcontrol *kctl;
2400
2401 if (unlikely(!name))
2402 return NULL;
2403
2404 list_for_each_entry(kctl, &card->controls, list)
2405 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2406 return kctl;
2407 return NULL;
2408}
2409EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2410
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002412 * snd_soc_add_component_controls - Add an array of controls to a component.
2413 *
2414 * @component: Component to add controls to
2415 * @controls: Array of controls to add
2416 * @num_controls: Number of elements in the array
2417 *
2418 * Return: 0 for success, else error.
2419 */
2420int snd_soc_add_component_controls(struct snd_soc_component *component,
2421 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2422{
2423 struct snd_card *card = component->card->snd_card;
2424
2425 return snd_soc_add_controls(card, component->dev, controls,
2426 num_controls, component->name_prefix, component);
2427}
2428EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2429
2430/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002431 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2432 * Convenience function to add a list of controls.
2433 *
2434 * @soc_card: SoC card to add controls to
2435 * @controls: array of controls to add
2436 * @num_controls: number of elements in the array
2437 *
2438 * Return 0 for success, else error.
2439 */
2440int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2441 const struct snd_kcontrol_new *controls, int num_controls)
2442{
2443 struct snd_card *card = soc_card->snd_card;
2444
2445 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2446 NULL, soc_card);
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2449
2450/**
2451 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2452 * Convienience function to add a list of controls.
2453 *
2454 * @dai: DAI to add controls to
2455 * @controls: array of controls to add
2456 * @num_controls: number of elements in the array
2457 *
2458 * Return 0 for success, else error.
2459 */
2460int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2461 const struct snd_kcontrol_new *controls, int num_controls)
2462{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002463 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002464
2465 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2466 NULL, dai);
2467}
2468EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2469
2470/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002471 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2472 * @dai: DAI
2473 * @clk_id: DAI specific clock ID
2474 * @freq: new clock frequency in Hz
2475 * @dir: new clock direction - input/output.
2476 *
2477 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2478 */
2479int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2480 unsigned int freq, int dir)
2481{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002482 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002483 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002484
2485 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2486 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002487}
2488EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2489
2490/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002491 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2492 * @component: COMPONENT
2493 * @clk_id: DAI specific clock ID
2494 * @source: Source for the clock
2495 * @freq: new clock frequency in Hz
2496 * @dir: new clock direction - input/output.
2497 *
2498 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2499 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002500int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2501 int clk_id, int source, unsigned int freq,
2502 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002503{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002504 if (component->driver->set_sysclk)
2505 return component->driver->set_sysclk(component, clk_id, source,
2506 freq, dir);
2507
2508 return -ENOTSUPP;
2509}
2510EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2511
2512/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002513 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2514 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002515 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002516 * @div: new clock divisor.
2517 *
2518 * Configures the clock dividers. This is used to derive the best DAI bit and
2519 * frame clocks from the system or master clock. It's best to set the DAI bit
2520 * and frame clocks as low as possible to save system power.
2521 */
2522int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2523 int div_id, int div)
2524{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002525 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002526 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002527 else
2528 return -EINVAL;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2531
2532/**
2533 * snd_soc_dai_set_pll - configure DAI PLL.
2534 * @dai: DAI
2535 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002536 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002537 * @freq_in: PLL input clock frequency in Hz
2538 * @freq_out: requested PLL output clock frequency in Hz
2539 *
2540 * Configures and enables PLL to generate output clock based on input clock.
2541 */
Mark Brown85488032009-09-05 18:52:16 +01002542int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2543 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002544{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002545 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002546 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002547 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002548
2549 return snd_soc_component_set_pll(dai->component, pll_id, source,
2550 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002551}
2552EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2553
Mark Brownec4ee522011-03-07 20:58:11 +00002554/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002555 * snd_soc_component_set_pll - configure component PLL.
2556 * @component: COMPONENT
2557 * @pll_id: DAI specific PLL ID
2558 * @source: DAI specific source for the PLL
2559 * @freq_in: PLL input clock frequency in Hz
2560 * @freq_out: requested PLL output clock frequency in Hz
2561 *
2562 * Configures and enables PLL to generate output clock based on input clock.
2563 */
2564int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2565 int source, unsigned int freq_in,
2566 unsigned int freq_out)
2567{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002568 if (component->driver->set_pll)
2569 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002570 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002571
2572 return -EINVAL;
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2575
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002576/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002577 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2578 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002579 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002580 *
2581 * Configures the DAI for a preset BCLK to sample rate ratio.
2582 */
2583int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2584{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002585 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002586 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2587 else
2588 return -EINVAL;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2591
2592/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002593 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2594 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002595 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002596 *
2597 * Configures the DAI hardware format and clocking.
2598 */
2599int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2600{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002601 if (dai->driver->ops->set_fmt == NULL)
2602 return -ENOTSUPP;
2603 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002604}
2605EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2606
2607/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002608 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002609 * @slots: Number of slots in use.
2610 * @tx_mask: bitmask representing active TX slots.
2611 * @rx_mask: bitmask representing active RX slots.
2612 *
2613 * Generates the TDM tx and rx slot default masks for DAI.
2614 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002615static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002616 unsigned int *tx_mask,
2617 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002618{
2619 if (*tx_mask || *rx_mask)
2620 return 0;
2621
2622 if (!slots)
2623 return -EINVAL;
2624
2625 *tx_mask = (1 << slots) - 1;
2626 *rx_mask = (1 << slots) - 1;
2627
2628 return 0;
2629}
2630
2631/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002632 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2633 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002634 * @tx_mask: bitmask representing active TX slots.
2635 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002636 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002637 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002638 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002639 * This function configures the specified DAI for TDM operation. @slot contains
2640 * the total number of slots of the TDM stream and @slot_with the width of each
2641 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2642 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2643 * DAI should write to or read from. If a bit is set the corresponding slot is
2644 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2645 * the first slot, bit 1 to the second slot and so on. The first active slot
2646 * maps to the first channel of the DAI, the second active slot to the second
2647 * channel and so on.
2648 *
2649 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2650 * @rx_mask and @slot_width will be ignored.
2651 *
2652 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002653 */
2654int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002655 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002656{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002657 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002658 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002659 &tx_mask, &rx_mask);
2660 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002661 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002662
Benoit Cousson88bd8702014-07-08 23:19:34 +02002663 dai->tx_mask = tx_mask;
2664 dai->rx_mask = rx_mask;
2665
Kuninori Morimoto46471922017-09-25 01:38:34 +00002666 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002667 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002668 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002669 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002670 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002671}
2672EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2673
2674/**
Barry Song472df3c2009-09-12 01:16:29 +08002675 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2676 * @dai: DAI
2677 * @tx_num: how many TX channels
2678 * @tx_slot: pointer to an array which imply the TX slot number channel
2679 * 0~num-1 uses
2680 * @rx_num: how many RX channels
2681 * @rx_slot: pointer to an array which imply the RX slot number channel
2682 * 0~num-1 uses
2683 *
2684 * configure the relationship between channel number and TDM slot number.
2685 */
2686int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2687 unsigned int tx_num, unsigned int *tx_slot,
2688 unsigned int rx_num, unsigned int *rx_slot)
2689{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002690 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002692 rx_num, rx_slot);
2693 else
2694 return -EINVAL;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2697
2698/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002699 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2700 * @dai: DAI
2701 * @tx_num: how many TX channels
2702 * @tx_slot: pointer to an array which imply the TX slot number channel
2703 * 0~num-1 uses
2704 * @rx_num: how many RX channels
2705 * @rx_slot: pointer to an array which imply the RX slot number channel
2706 * 0~num-1 uses
2707 */
2708int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2709 unsigned int *tx_num, unsigned int *tx_slot,
2710 unsigned int *rx_num, unsigned int *rx_slot)
2711{
2712 if (dai->driver->ops->get_channel_map)
2713 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2714 rx_num, rx_slot);
2715 else
2716 return -ENOTSUPP;
2717}
2718EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2719
2720/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002721 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2722 * @dai: DAI
2723 * @tristate: tristate enable
2724 *
2725 * Tristates the DAI so that others can use it.
2726 */
2727int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2728{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002729 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002731 else
2732 return -EINVAL;
2733}
2734EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2735
2736/**
2737 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2738 * @dai: DAI
2739 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002740 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002741 *
2742 * Mutes the DAI DAC.
2743 */
Mark Brownda183962013-02-06 15:44:07 +00002744int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2745 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002746{
Mark Brownda183962013-02-06 15:44:07 +00002747 if (dai->driver->ops->mute_stream)
2748 return dai->driver->ops->mute_stream(dai, mute, direction);
2749 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2750 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002751 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002752 else
Mark Brown04570c62012-04-13 19:16:03 +01002753 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002754}
2755EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2756
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002757static int snd_soc_bind_card(struct snd_soc_card *card)
2758{
2759 struct snd_soc_pcm_runtime *rtd;
2760 int ret;
2761
2762 ret = snd_soc_instantiate_card(card);
2763 if (ret != 0)
2764 return ret;
2765
2766 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002767 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002768 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2769 struct snd_soc_dai *codec_dai;
2770 int j;
2771
2772 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2773 if (!codec_dai->active)
2774 pinctrl_pm_select_sleep_state(codec_dai->dev);
2775 }
2776
2777 if (!cpu_dai->active)
2778 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2779 }
2780
2781 return ret;
2782}
2783
Mark Brownc5af3a22008-11-28 13:29:45 +00002784/**
2785 * snd_soc_register_card - Register a card with the ASoC core
2786 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002787 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002788 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002789 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302790int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002791{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002792 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002793 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002794
Mark Brownc5af3a22008-11-28 13:29:45 +00002795 if (!card->name || !card->dev)
2796 return -EINVAL;
2797
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302798 mutex_lock(&client_mutex);
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002799 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002800
Mengdong Lin923c5e612015-11-23 11:01:49 -05002801 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002802 if (ret) {
Jonathan Hunterc342feb2019-03-04 13:31:14 +00002803 soc_cleanup_platform(card);
Mengdong Lin923c5e612015-11-23 11:01:49 -05002804 dev_err(card->dev, "ASoC: failed to init link %s\n",
2805 link->name);
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302806 mutex_unlock(&client_mutex);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002807 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002808 }
Stephen Warren5a504962011-12-21 10:40:59 -07002809 }
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302810 mutex_unlock(&client_mutex);
Stephen Warren5a504962011-12-21 10:40:59 -07002811
Mark Browned77cc12011-05-03 18:25:34 +01002812 dev_set_drvdata(card->dev, card);
2813
Stephen Warren111c6412011-01-28 14:26:35 -07002814 snd_soc_initialize_card_lists(card);
2815
Mengdong Linf8f80362015-12-02 14:11:22 +08002816 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002817
Mengdong Lin1a497982015-11-18 02:34:11 -05002818 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002819 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002820
Mark Browndb432b42011-10-03 21:06:40 +01002821 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002822 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002823 card->instantiated = 0;
2824 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002825 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002826 spin_lock_init(&card->dpcm_lock);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002827
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002828 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002829}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302830EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002831
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002832static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2833{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002834 struct snd_soc_pcm_runtime *rtd;
2835 int order;
2836
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002837 if (card->instantiated) {
2838 card->instantiated = false;
2839 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002840 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002841
2842 /* remove all components used by DAI links on this card */
2843 for_each_comp_order(order) {
2844 for_each_card_rtds(card, rtd) {
2845 soc_remove_link_components(card, rtd, order);
2846 }
2847 }
2848
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002849 soc_cleanup_card_resources(card);
2850 if (!unregister)
2851 list_add(&card->list, &unbind_card_list);
2852 } else {
2853 if (unregister)
2854 list_del(&card->list);
2855 }
2856}
2857
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002858/**
2859 * snd_soc_unregister_card - Unregister a card with the ASoC core
2860 *
2861 * @card: Card to unregister
2862 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002863 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302864int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002865{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002866 snd_soc_unbind_card(card, true);
2867 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002868
2869 return 0;
2870}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302871EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002872
2873/*
2874 * Simplify DAI link configuration by removing ".-1" from device names
2875 * and sanitizing names.
2876 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002877static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002878{
2879 char *found, name[NAME_SIZE];
2880 int id1, id2;
2881
2882 if (dev_name(dev) == NULL)
2883 return NULL;
2884
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002885 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002886
2887 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002888 found = strstr(name, dev->driver->name);
2889 if (found) {
2890 /* get ID */
2891 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2892
2893 /* discard ID from name if ID == -1 */
2894 if (*id == -1)
2895 found[strlen(dev->driver->name)] = '\0';
2896 }
2897
2898 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002899 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002900 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2901 char tmp[NAME_SIZE];
2902
2903 /* create unique ID number from I2C addr and bus */
2904 *id = ((id1 & 0xffff) << 16) + id2;
2905
2906 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002907 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2908 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002909 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002910 } else
2911 *id = 0;
2912 }
2913
2914 return kstrdup(name, GFP_KERNEL);
2915}
2916
2917/*
2918 * Simplify DAI link naming for single devices with multiple DAIs by removing
2919 * any ".-1" and using the DAI name (instead of device name).
2920 */
2921static inline char *fmt_multiple_name(struct device *dev,
2922 struct snd_soc_dai_driver *dai_drv)
2923{
2924 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002925 dev_err(dev,
2926 "ASoC: error - multiple DAI %s registered with no name\n",
2927 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002928 return NULL;
2929 }
2930
2931 return kstrdup(dai_drv->name, GFP_KERNEL);
2932}
2933
2934/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002935 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002936 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002937 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002938 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002939static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002940{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002941 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002942
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002943 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002944 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2945 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002946 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002947 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002948 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002949 }
Mark Brown91151712008-11-30 23:31:24 +00002950}
Mark Brown91151712008-11-30 23:31:24 +00002951
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002952/* Create a DAI and add it to the component's DAI list */
2953static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2954 struct snd_soc_dai_driver *dai_drv,
2955 bool legacy_dai_naming)
2956{
2957 struct device *dev = component->dev;
2958 struct snd_soc_dai *dai;
2959
2960 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2961
2962 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2963 if (dai == NULL)
2964 return NULL;
2965
2966 /*
2967 * Back in the old days when we still had component-less DAIs,
2968 * instead of having a static name, component-less DAIs would
2969 * inherit the name of the parent device so it is possible to
2970 * register multiple instances of the DAI. We still need to keep
2971 * the same naming style even though those DAIs are not
2972 * component-less anymore.
2973 */
2974 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002975 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002976 dai->name = fmt_single_name(dev, &dai->id);
2977 } else {
2978 dai->name = fmt_multiple_name(dev, dai_drv);
2979 if (dai_drv->id)
2980 dai->id = dai_drv->id;
2981 else
2982 dai->id = component->num_dai;
2983 }
2984 if (dai->name == NULL) {
2985 kfree(dai);
2986 return NULL;
2987 }
2988
2989 dai->component = component;
2990 dai->dev = dev;
2991 dai->driver = dai_drv;
2992 if (!dai->driver->ops)
2993 dai->driver->ops = &null_dai_ops;
2994
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002995 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002996 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002997 component->num_dai++;
2998
2999 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3000 return dai;
3001}
3002
Mark Brown91151712008-11-30 23:31:24 +00003003/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003004 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003005 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003006 * @component: The component the DAIs are registered for
3007 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003008 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003009 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003010static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003011 struct snd_soc_dai_driver *dai_drv,
3012 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003013{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003014 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003016 unsigned int i;
3017 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003019 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003020
3021 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003022
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003023 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
3024 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003025 if (dai == NULL) {
3026 ret = -ENOMEM;
3027 goto err;
3028 }
Mark Brown91151712008-11-30 23:31:24 +00003029 }
3030
3031 return 0;
3032
3033err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003034 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003035
3036 return ret;
3037}
Mark Brown91151712008-11-30 23:31:24 +00003038
Mengdong Lin68003e62015-12-31 16:40:43 +08003039/**
3040 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3041 *
3042 * @component: The component the DAIs are registered for
3043 * @dai_drv: DAI driver to use for the DAI
3044 *
3045 * Topology can use this API to register DAIs when probing a component.
3046 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3047 * will be freed in the component cleanup.
3048 */
3049int snd_soc_register_dai(struct snd_soc_component *component,
3050 struct snd_soc_dai_driver *dai_drv)
3051{
3052 struct snd_soc_dapm_context *dapm =
3053 snd_soc_component_get_dapm(component);
3054 struct snd_soc_dai *dai;
3055 int ret;
3056
3057 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3058 dev_err(component->dev, "Invalid dai type %d\n",
3059 dai_drv->dobj.type);
3060 return -EINVAL;
3061 }
3062
3063 lockdep_assert_held(&client_mutex);
3064 dai = soc_add_dai(component, dai_drv, false);
3065 if (!dai)
3066 return -ENOMEM;
3067
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003068 /*
3069 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08003070 * also add routes that need these widgets as source or sink.
3071 */
3072 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3073 if (ret != 0) {
3074 dev_err(component->dev,
3075 "Failed to create DAI widgets %d\n", ret);
3076 }
3077
3078 return ret;
3079}
3080EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3081
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003082static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3083 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003084{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003085 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003086
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003087 component->driver->seq_notifier(component, type, subseq);
3088}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003089
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003090static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3091 int event)
3092{
3093 struct snd_soc_component *component = dapm->component;
3094
3095 return component->driver->stream_event(component, event);
3096}
3097
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003098static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3099 enum snd_soc_bias_level level)
3100{
3101 struct snd_soc_component *component = dapm->component;
3102
3103 return component->driver->set_bias_level(component, level);
3104}
3105
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003106static int snd_soc_component_initialize(struct snd_soc_component *component,
3107 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003108{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003109 struct snd_soc_dapm_context *dapm;
3110
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003111 component->name = fmt_single_name(dev, &component->id);
3112 if (!component->name) {
3113 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003114 return -ENOMEM;
3115 }
3116
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003117 component->dev = dev;
3118 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003119
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003120 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003121 dapm->dev = dev;
3122 dapm->component = component;
3123 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003124 dapm->idle_bias_off = !driver->idle_bias_on;
3125 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003126 if (driver->seq_notifier)
3127 dapm->seq_notifier = snd_soc_component_seq_notifier;
3128 if (driver->stream_event)
3129 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003130 if (driver->set_bias_level)
3131 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003132
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003133 INIT_LIST_HEAD(&component->dai_list);
3134 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003135
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003136 return 0;
3137}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003138
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003139static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003140{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003141 int val_bytes = regmap_get_val_bytes(component->regmap);
3142
3143 /* Errors are legitimate for non-integer byte multiples */
3144 if (val_bytes > 0)
3145 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003146}
3147
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003148#ifdef CONFIG_REGMAP
3149
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003150/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003151 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3152 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003153 * @component: The component for which to initialize the regmap instance
3154 * @regmap: The regmap instance that should be used by the component
3155 *
3156 * This function allows deferred assignment of the regmap instance that is
3157 * associated with the component. Only use this if the regmap instance is not
3158 * yet ready when the component is registered. The function must also be called
3159 * before the first IO attempt of the component.
3160 */
3161void snd_soc_component_init_regmap(struct snd_soc_component *component,
3162 struct regmap *regmap)
3163{
3164 component->regmap = regmap;
3165 snd_soc_component_setup_regmap(component);
3166}
3167EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3168
3169/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003170 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3171 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003172 * @component: The component for which to de-initialize the regmap instance
3173 *
3174 * Calls regmap_exit() on the regmap instance associated to the component and
3175 * removes the regmap instance from the component.
3176 *
3177 * This function should only be used if snd_soc_component_init_regmap() was used
3178 * to initialize the regmap instance.
3179 */
3180void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3181{
3182 regmap_exit(component->regmap);
3183 component->regmap = NULL;
3184}
3185EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3186
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003187#endif
3188
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003189static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003190{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003191 mutex_lock(&client_mutex);
3192
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003193 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003194 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003195 component->regmap = dev_get_regmap(component->dev,
3196 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003197 if (component->regmap)
3198 snd_soc_component_setup_regmap(component);
3199 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003200
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003201 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003202 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003203 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003204
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003205 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003206}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003207
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003208static void snd_soc_component_cleanup(struct snd_soc_component *component)
3209{
3210 snd_soc_unregister_dais(component);
3211 kfree(component->name);
3212}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003213
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003214static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3215{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003216 struct snd_soc_card *card = component->card;
3217
3218 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003219 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003220
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003221 list_del(&component->list);
3222}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003223
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003224#define ENDIANNESS_MAP(name) \
3225 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3226static u64 endianness_format_map[] = {
3227 ENDIANNESS_MAP(S16_),
3228 ENDIANNESS_MAP(U16_),
3229 ENDIANNESS_MAP(S24_),
3230 ENDIANNESS_MAP(U24_),
3231 ENDIANNESS_MAP(S32_),
3232 ENDIANNESS_MAP(U32_),
3233 ENDIANNESS_MAP(S24_3),
3234 ENDIANNESS_MAP(U24_3),
3235 ENDIANNESS_MAP(S20_3),
3236 ENDIANNESS_MAP(U20_3),
3237 ENDIANNESS_MAP(S18_3),
3238 ENDIANNESS_MAP(U18_3),
3239 ENDIANNESS_MAP(FLOAT_),
3240 ENDIANNESS_MAP(FLOAT64_),
3241 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3242};
3243
3244/*
3245 * Fix up the DAI formats for endianness: codecs don't actually see
3246 * the endianness of the data but we're using the CPU format
3247 * definitions which do need to include endianness so we ensure that
3248 * codec DAIs always have both big and little endian variants set.
3249 */
3250static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3251{
3252 int i;
3253
3254 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3255 if (stream->formats & endianness_format_map[i])
3256 stream->formats |= endianness_format_map[i];
3257}
3258
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003259static void snd_soc_try_rebind_card(void)
3260{
3261 struct snd_soc_card *card, *c;
3262
3263 if (!list_empty(&unbind_card_list)) {
3264 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3265 if (!snd_soc_bind_card(card))
3266 list_del(&card->list);
3267 }
3268 }
3269}
3270
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003271int snd_soc_add_component(struct device *dev,
3272 struct snd_soc_component *component,
3273 const struct snd_soc_component_driver *component_driver,
3274 struct snd_soc_dai_driver *dai_drv,
3275 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003276{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003277 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003278 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003279
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003280 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003281 if (ret)
3282 goto err_free;
3283
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003284 if (component_driver->endianness) {
3285 for (i = 0; i < num_dai; i++) {
3286 convert_endianness_formats(&dai_drv[i].playback);
3287 convert_endianness_formats(&dai_drv[i].capture);
3288 }
3289 }
3290
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003291 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003292 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003293 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003294 goto err_cleanup;
3295 }
3296
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003297 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003298 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003299
3300 return 0;
3301
3302err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003303 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003304err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003305 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003306}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003307EXPORT_SYMBOL_GPL(snd_soc_add_component);
3308
3309int snd_soc_register_component(struct device *dev,
3310 const struct snd_soc_component_driver *component_driver,
3311 struct snd_soc_dai_driver *dai_drv,
3312 int num_dai)
3313{
3314 struct snd_soc_component *component;
3315
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003316 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003317 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003318 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003319
3320 return snd_soc_add_component(dev, component, component_driver,
3321 dai_drv, num_dai);
3322}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003323EXPORT_SYMBOL_GPL(snd_soc_register_component);
3324
3325/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003326 * snd_soc_unregister_component - Unregister all related component
3327 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003328 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003329 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003330 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003331static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003332{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003333 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003334 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003335
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003336 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003337 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003338 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003339 continue;
3340
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003341 snd_soc_tplg_component_remove(component,
3342 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003343 snd_soc_component_del_unlocked(component);
3344 found = 1;
3345 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003346 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003347 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003348
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003349 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003350 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003351
3352 return found;
3353}
3354
3355void snd_soc_unregister_component(struct device *dev)
3356{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003357 while (__snd_soc_unregister_component(dev))
3358 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003359}
3360EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3361
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003362struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3363 const char *driver_name)
3364{
3365 struct snd_soc_component *component;
3366 struct snd_soc_component *ret;
3367
3368 ret = NULL;
3369 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003370 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003371 if (dev != component->dev)
3372 continue;
3373
3374 if (driver_name &&
3375 (driver_name != component->driver->name) &&
3376 (strcmp(component->driver->name, driver_name) != 0))
3377 continue;
3378
3379 ret = component;
3380 break;
3381 }
3382 mutex_unlock(&client_mutex);
3383
3384 return ret;
3385}
3386EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3387
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003388/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003389int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3390 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003391{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003392 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003393 int ret;
3394
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303395 if (!card->dev) {
3396 pr_err("card->dev is not set before calling %s\n", __func__);
3397 return -EINVAL;
3398 }
3399
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003400 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303401
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003402 ret = of_property_read_string_index(np, propname, 0, &card->name);
3403 /*
3404 * EINVAL means the property does not exist. This is fine providing
3405 * card->name was previously set, which is checked later in
3406 * snd_soc_register_card.
3407 */
3408 if (ret < 0 && ret != -EINVAL) {
3409 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003410 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003411 propname, ret);
3412 return ret;
3413 }
3414
3415 return 0;
3416}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003417EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003418
Xiubo Li9a6d4862014-02-08 15:59:52 +08003419static const struct snd_soc_dapm_widget simple_widgets[] = {
3420 SND_SOC_DAPM_MIC("Microphone", NULL),
3421 SND_SOC_DAPM_LINE("Line", NULL),
3422 SND_SOC_DAPM_HP("Headphone", NULL),
3423 SND_SOC_DAPM_SPK("Speaker", NULL),
3424};
3425
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003426int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003427 const char *propname)
3428{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003429 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003430 struct snd_soc_dapm_widget *widgets;
3431 const char *template, *wname;
3432 int i, j, num_widgets, ret;
3433
3434 num_widgets = of_property_count_strings(np, propname);
3435 if (num_widgets < 0) {
3436 dev_err(card->dev,
3437 "ASoC: Property '%s' does not exist\n", propname);
3438 return -EINVAL;
3439 }
3440 if (num_widgets & 1) {
3441 dev_err(card->dev,
3442 "ASoC: Property '%s' length is not even\n", propname);
3443 return -EINVAL;
3444 }
3445
3446 num_widgets /= 2;
3447 if (!num_widgets) {
3448 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3449 propname);
3450 return -EINVAL;
3451 }
3452
3453 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3454 GFP_KERNEL);
3455 if (!widgets) {
3456 dev_err(card->dev,
3457 "ASoC: Could not allocate memory for widgets\n");
3458 return -ENOMEM;
3459 }
3460
3461 for (i = 0; i < num_widgets; i++) {
3462 ret = of_property_read_string_index(np, propname,
3463 2 * i, &template);
3464 if (ret) {
3465 dev_err(card->dev,
3466 "ASoC: Property '%s' index %d read error:%d\n",
3467 propname, 2 * i, ret);
3468 return -EINVAL;
3469 }
3470
3471 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3472 if (!strncmp(template, simple_widgets[j].name,
3473 strlen(simple_widgets[j].name))) {
3474 widgets[i] = simple_widgets[j];
3475 break;
3476 }
3477 }
3478
3479 if (j >= ARRAY_SIZE(simple_widgets)) {
3480 dev_err(card->dev,
3481 "ASoC: DAPM widget '%s' is not supported\n",
3482 template);
3483 return -EINVAL;
3484 }
3485
3486 ret = of_property_read_string_index(np, propname,
3487 (2 * i) + 1,
3488 &wname);
3489 if (ret) {
3490 dev_err(card->dev,
3491 "ASoC: Property '%s' index %d read error:%d\n",
3492 propname, (2 * i) + 1, ret);
3493 return -EINVAL;
3494 }
3495
3496 widgets[i].name = wname;
3497 }
3498
Nicolin Chenf23e8602015-02-14 17:22:49 -08003499 card->of_dapm_widgets = widgets;
3500 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003501
3502 return 0;
3503}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003504EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003505
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003506int snd_soc_of_get_slot_mask(struct device_node *np,
3507 const char *prop_name,
3508 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003509{
3510 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003511 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003512 int i;
3513
3514 if (!of_slot_mask)
3515 return 0;
3516 val /= sizeof(u32);
3517 for (i = 0; i < val; i++)
3518 if (be32_to_cpup(&of_slot_mask[i]))
3519 *mask |= (1 << i);
3520
3521 return val;
3522}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003523EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003524
Xiubo Li89c67852014-02-14 09:34:35 +08003525int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003526 unsigned int *tx_mask,
3527 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003528 unsigned int *slots,
3529 unsigned int *slot_width)
3530{
3531 u32 val;
3532 int ret;
3533
Jyri Sarha61310842015-09-09 21:27:43 +03003534 if (tx_mask)
3535 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3536 if (rx_mask)
3537 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3538
Xiubo Li89c67852014-02-14 09:34:35 +08003539 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3540 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3541 if (ret)
3542 return ret;
3543
3544 if (slots)
3545 *slots = val;
3546 }
3547
3548 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3549 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3550 if (ret)
3551 return ret;
3552
3553 if (slot_width)
3554 *slot_width = val;
3555 }
3556
3557 return 0;
3558}
3559EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3560
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003561void snd_soc_of_parse_node_prefix(struct device_node *np,
3562 struct snd_soc_codec_conf *codec_conf,
3563 struct device_node *of_node,
3564 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003565{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003566 const char *str;
3567 int ret;
3568
3569 ret = of_property_read_string(np, propname, &str);
3570 if (ret < 0) {
3571 /* no prefix is not error */
3572 return;
3573 }
3574
3575 codec_conf->of_node = of_node;
3576 codec_conf->name_prefix = str;
3577}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003578EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003579
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003580int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003581 const char *propname)
3582{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003583 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003584 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003585 struct snd_soc_dapm_route *routes;
3586 int i, ret;
3587
3588 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003589 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003590 dev_err(card->dev,
3591 "ASoC: Property '%s' does not exist or its length is not even\n",
3592 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003593 return -EINVAL;
3594 }
3595 num_routes /= 2;
3596 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003597 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003598 propname);
3599 return -EINVAL;
3600 }
3601
Kees Cooka86854d2018-06-12 14:07:58 -07003602 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003603 GFP_KERNEL);
3604 if (!routes) {
3605 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003606 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003607 return -EINVAL;
3608 }
3609
3610 for (i = 0; i < num_routes; i++) {
3611 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003612 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003613 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003614 dev_err(card->dev,
3615 "ASoC: Property '%s' index %d could not be read: %d\n",
3616 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003617 return -EINVAL;
3618 }
3619 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003620 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003621 if (ret) {
3622 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003623 "ASoC: Property '%s' index %d could not be read: %d\n",
3624 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003625 return -EINVAL;
3626 }
3627 }
3628
Nicolin Chenf23e8602015-02-14 17:22:49 -08003629 card->num_of_dapm_routes = num_routes;
3630 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003631
3632 return 0;
3633}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003634EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003635
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003636unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003637 const char *prefix,
3638 struct device_node **bitclkmaster,
3639 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003640{
3641 int ret, i;
3642 char prop[128];
3643 unsigned int format = 0;
3644 int bit, frame;
3645 const char *str;
3646 struct {
3647 char *name;
3648 unsigned int val;
3649 } of_fmt_table[] = {
3650 { "i2s", SND_SOC_DAIFMT_I2S },
3651 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3652 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3653 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3654 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3655 { "ac97", SND_SOC_DAIFMT_AC97 },
3656 { "pdm", SND_SOC_DAIFMT_PDM},
3657 { "msb", SND_SOC_DAIFMT_MSB },
3658 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003659 };
3660
3661 if (!prefix)
3662 prefix = "";
3663
3664 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003665 * check "dai-format = xxx"
3666 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003667 * SND_SOC_DAIFMT_FORMAT_MASK area
3668 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003669 ret = of_property_read_string(np, "dai-format", &str);
3670 if (ret < 0) {
3671 snprintf(prop, sizeof(prop), "%sformat", prefix);
3672 ret = of_property_read_string(np, prop, &str);
3673 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003674 if (ret == 0) {
3675 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3676 if (strcmp(str, of_fmt_table[i].name) == 0) {
3677 format |= of_fmt_table[i].val;
3678 break;
3679 }
3680 }
3681 }
3682
3683 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003684 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003685 * SND_SOC_DAIFMT_CLOCK_MASK area
3686 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003687 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003688 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003689 format |= SND_SOC_DAIFMT_CONT;
3690 else
3691 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003692
3693 /*
3694 * check "[prefix]bitclock-inversion"
3695 * check "[prefix]frame-inversion"
3696 * SND_SOC_DAIFMT_INV_MASK area
3697 */
3698 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3699 bit = !!of_get_property(np, prop, NULL);
3700
3701 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3702 frame = !!of_get_property(np, prop, NULL);
3703
3704 switch ((bit << 4) + frame) {
3705 case 0x11:
3706 format |= SND_SOC_DAIFMT_IB_IF;
3707 break;
3708 case 0x10:
3709 format |= SND_SOC_DAIFMT_IB_NF;
3710 break;
3711 case 0x01:
3712 format |= SND_SOC_DAIFMT_NB_IF;
3713 break;
3714 default:
3715 /* SND_SOC_DAIFMT_NB_NF is default */
3716 break;
3717 }
3718
3719 /*
3720 * check "[prefix]bitclock-master"
3721 * check "[prefix]frame-master"
3722 * SND_SOC_DAIFMT_MASTER_MASK area
3723 */
3724 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3725 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003726 if (bit && bitclkmaster)
3727 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003728
3729 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3730 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003731 if (frame && framemaster)
3732 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003733
3734 switch ((bit << 4) + frame) {
3735 case 0x11:
3736 format |= SND_SOC_DAIFMT_CBM_CFM;
3737 break;
3738 case 0x10:
3739 format |= SND_SOC_DAIFMT_CBM_CFS;
3740 break;
3741 case 0x01:
3742 format |= SND_SOC_DAIFMT_CBS_CFM;
3743 break;
3744 default:
3745 format |= SND_SOC_DAIFMT_CBS_CFS;
3746 break;
3747 }
3748
3749 return format;
3750}
3751EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3752
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003753int snd_soc_get_dai_id(struct device_node *ep)
3754{
3755 struct snd_soc_component *pos;
3756 struct device_node *node;
3757 int ret;
3758
3759 node = of_graph_get_port_parent(ep);
3760
3761 /*
3762 * For example HDMI case, HDMI has video/sound port,
3763 * but ALSA SoC needs sound port number only.
3764 * Thus counting HDMI DT port/endpoint doesn't work.
3765 * Then, it should have .of_xlate_dai_id
3766 */
3767 ret = -ENOTSUPP;
3768 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003769 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003770 struct device_node *component_of_node = pos->dev->of_node;
3771
3772 if (!component_of_node && pos->dev->parent)
3773 component_of_node = pos->dev->parent->of_node;
3774
3775 if (component_of_node != node)
3776 continue;
3777
3778 if (pos->driver->of_xlate_dai_id)
3779 ret = pos->driver->of_xlate_dai_id(pos, ep);
3780
3781 break;
3782 }
3783 mutex_unlock(&client_mutex);
3784
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003785 of_node_put(node);
3786
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003787 return ret;
3788}
3789EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3790
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003791int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003792 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003793{
3794 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003795 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003796 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003797
3798 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003799 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003800 component_of_node = pos->dev->of_node;
3801 if (!component_of_node && pos->dev->parent)
3802 component_of_node = pos->dev->parent->of_node;
3803
3804 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003805 continue;
3806
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003807 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003808 ret = pos->driver->of_xlate_dai_name(pos,
3809 args,
3810 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003811 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003812 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003813 int id = -1;
3814
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003815 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003816 case 0:
3817 id = 0; /* same as dai_drv[0] */
3818 break;
3819 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003820 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003821 break;
3822 default:
3823 /* not supported */
3824 break;
3825 }
3826
3827 if (id < 0 || id >= pos->num_dai) {
3828 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003829 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003830 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003831
3832 ret = 0;
3833
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003834 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003835 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003836 if (id == 0)
3837 break;
3838 id--;
3839 }
3840
3841 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003842 if (!*dai_name)
3843 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003844 }
3845
Kuninori Morimotocb470082013-09-10 17:39:56 -07003846 break;
3847 }
3848 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003849 return ret;
3850}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003851EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003852
3853int snd_soc_of_get_dai_name(struct device_node *of_node,
3854 const char **dai_name)
3855{
3856 struct of_phandle_args args;
3857 int ret;
3858
3859 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3860 "#sound-dai-cells", 0, &args);
3861 if (ret)
3862 return ret;
3863
3864 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003865
3866 of_node_put(args.np);
3867
3868 return ret;
3869}
3870EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3871
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003872/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003873 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3874 * @dai_link: DAI link
3875 *
3876 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3877 */
3878void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3879{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003880 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003881 int index;
3882
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003883 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003884 if (!component->of_node)
3885 break;
3886 of_node_put(component->of_node);
3887 component->of_node = NULL;
3888 }
3889}
3890EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3891
3892/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003893 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3894 * @dev: Card device
3895 * @of_node: Device node
3896 * @dai_link: DAI link
3897 *
3898 * Builds an array of CODEC DAI components from the DAI link property
3899 * 'sound-dai'.
3900 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003901 * The device nodes in the array (of_node) must be dereferenced by calling
3902 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003903 *
3904 * Returns 0 for success
3905 */
3906int snd_soc_of_get_dai_link_codecs(struct device *dev,
3907 struct device_node *of_node,
3908 struct snd_soc_dai_link *dai_link)
3909{
3910 struct of_phandle_args args;
3911 struct snd_soc_dai_link_component *component;
3912 char *name;
3913 int index, num_codecs, ret;
3914
3915 /* Count the number of CODECs */
3916 name = "sound-dai";
3917 num_codecs = of_count_phandle_with_args(of_node, name,
3918 "#sound-dai-cells");
3919 if (num_codecs <= 0) {
3920 if (num_codecs == -ENOENT)
3921 dev_err(dev, "No 'sound-dai' property\n");
3922 else
3923 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3924 return num_codecs;
3925 }
Kees Cooka86854d2018-06-12 14:07:58 -07003926 component = devm_kcalloc(dev,
3927 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003928 GFP_KERNEL);
3929 if (!component)
3930 return -ENOMEM;
3931 dai_link->codecs = component;
3932 dai_link->num_codecs = num_codecs;
3933
3934 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003935 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003936 ret = of_parse_phandle_with_args(of_node, name,
3937 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003938 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003939 if (ret)
3940 goto err;
3941 component->of_node = args.np;
3942 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3943 if (ret < 0)
3944 goto err;
3945 }
3946 return 0;
3947err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003948 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003949 dai_link->codecs = NULL;
3950 dai_link->num_codecs = 0;
3951 return ret;
3952}
3953EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3954
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003955static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003956{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003957 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003958 snd_soc_util_init();
3959
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003960 return platform_driver_register(&soc_driver);
3961}
Mark Brown4abe8e12010-10-12 17:41:03 +01003962module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003963
Mark Brown7d8c16a2008-11-30 22:11:24 +00003964static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003965{
Mark Brownfb257892011-04-28 10:57:54 +01003966 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003967 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003968
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003969 platform_driver_unregister(&soc_driver);
3970}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003971module_exit(snd_soc_exit);
3972
3973/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003974MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003975MODULE_DESCRIPTION("ALSA SoC Core");
3976MODULE_LICENSE("GPL");
3977MODULE_ALIAS("platform:soc-audio");