blob: 8a58fa86675ac57a29d4a7354d6c0e78e4fb5766 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020069/*
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080072 * as invalid and dropped when setting the card long name from DMI info.
73 */
74static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
76 "TBD by OEM",
77 "Default String",
78 "Board Manufacturer",
79 "Board Vendor Name",
80 "Board Product Name",
81 NULL, /* terminator */
82};
83
Mark Browndbe21402010-02-12 11:37:24 +000084static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
Mark Brown36ae1a92012-01-06 17:12:45 -080087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000090}
91
92static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070097 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000098
Jingoo Hanb785a492013-07-19 16:24:59 +090099 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700100 if (ret)
101 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000102
103 return count;
104}
105
106static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 &dev_attr_pmdown_time.attr,
110 NULL
111};
112
113static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
115{
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100122}
123
124static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
Mark Brownf7e73b262018-03-09 12:46:27 +0000129static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
132};
133
134static const struct attribute_group *soc_dev_attr_groups[] = {
135 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000136 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 NULL
138};
139
Mark Brown2624d5f2009-11-03 21:56:13 +0000140#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200143 if (!component->card->debugfs_card_root)
144 return;
145
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200146 if (component->debugfs_prefix) {
147 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100148
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
151 if (name) {
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
154 kfree(name);
155 }
156 } else {
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
159 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 return;
165 }
166
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169}
170
171static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
172{
173 debugfs_remove_recursive(component->debugfs_root);
174}
175
Peng Donglinc15b2a12018-02-14 22:48:07 +0800176static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100177{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100178 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100179 struct snd_soc_dai *dai;
180
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100181 mutex_lock(&client_mutex);
182
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000183 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000184 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800185 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100186
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100187 mutex_unlock(&client_mutex);
188
Donglin Peng700c17c2018-01-18 13:31:26 +0800189 return 0;
190}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800191DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100192
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000193static int component_list_show(struct seq_file *m, void *v)
194{
195 struct snd_soc_component *component;
196
197 mutex_lock(&client_mutex);
198
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000199 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200 seq_printf(m, "%s\n", component->name);
201
202 mutex_unlock(&client_mutex);
203
204 return 0;
205}
206DEFINE_SHOW_ATTRIBUTE(component_list);
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208static void soc_init_card_debugfs(struct snd_soc_card *card)
209{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200210 if (!snd_soc_debugfs_root)
211 return;
212
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000214 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200215 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100217 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200218 return;
219 }
220
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
223 &card->pop_time);
224 if (!card->debugfs_pop_time)
225 dev_warn(card->dev,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200226 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200227}
228
229static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230{
231 debugfs_remove_recursive(card->debugfs_card_root);
232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
240 return;
241 }
242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
244 &dai_list_fops))
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000246
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250}
251
252static void snd_soc_debugfs_exit(void)
253{
254 debugfs_remove_recursive(snd_soc_debugfs_root);
255}
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257#else
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261{
262}
263
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200264static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000266{
267}
268
Axel Linb95fccb2010-11-09 17:06:44 +0800269static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270{
271}
272
273static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274{
275}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200276
277static inline void snd_soc_debugfs_init(void)
278{
279}
280
281static inline void snd_soc_debugfs_exit(void)
282{
283}
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285#endif
286
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000287static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
289{
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
292
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
296 return 0;
297 }
298
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300 if (!new_rtdcom)
301 return -ENOMEM;
302
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
305
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
307
308 return 0;
309}
310
311static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312{
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316 kfree(rtdcom1);
317
318 INIT_LIST_HEAD(&rtd->component_list);
319}
320
321struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
323{
324 struct snd_soc_rtdcom_list *rtdcom;
325
Kuninori Morimoto971da242018-01-23 00:41:24 +0000326 if (!driver_name)
327 return NULL;
328
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000329 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000330 const char *component_name = rtdcom->component->driver->name;
331
332 if (!component_name)
333 continue;
334
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000337 return rtdcom->component;
338 }
339
340 return NULL;
341}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000342EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100344struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
346{
Mengdong Lin1a497982015-11-18 02:34:11 -0500347 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000349 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100353 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100355 return NULL;
356}
357EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
358
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000359static const struct snd_soc_ops null_snd_soc_ops;
360
Mengdong Lin1a497982015-11-18 02:34:11 -0500361static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
363{
364 struct snd_soc_pcm_runtime *rtd;
365
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
367 if (!rtd)
368 return NULL;
369
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000370 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500371 rtd->card = card;
372 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
375
Kees Cook6396bb22018-06-12 14:03:40 -0700376 rtd->codec_dais = kcalloc(dai_link->num_codecs,
377 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500378 GFP_KERNEL);
379 if (!rtd->codec_dais) {
380 kfree(rtd);
381 return NULL;
382 }
383
384 return rtd;
385}
386
387static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
388{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000389 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000390 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500391 kfree(rtd);
392}
393
394static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
396{
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
399 card->num_rtd++;
400}
401
402static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
403{
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
405
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000406 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
409 }
410
411 card->num_rtd = 0;
412}
413
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100414struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
416{
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100418
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000419 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500420 if (!strcmp(rtd->dai_link->name, dai_link))
421 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100422 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424 return NULL;
425}
426EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
427
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900428static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
429{
430 struct snd_soc_pcm_runtime *rtd;
431
432 for_each_card_rtds(card, rtd)
433 flush_delayed_work(&rtd->delayed_work);
434}
435
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100436static void codec2codec_close_delayed_work(struct work_struct *work)
437{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200438 /*
439 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100440 * Since c2c links are internal nodes in the DAPM graph and
441 * don't interface with the outside world or application layer
442 * we don't have to do any special handling on close.
443 */
444}
445
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000446#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000448int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000450 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000451 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500452 struct snd_soc_pcm_runtime *rtd;
453 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200455 /* If the card is not initialized yet there is nothing to do */
456 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200457 return 0;
458
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200459 /*
460 * Due to the resume being scheduled into a workqueue we could
461 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100462 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100464
465 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100467
Mark Browna00f90f2010-12-02 16:24:24 +0000468 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000469 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000470 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100471
Mengdong Lin1a497982015-11-18 02:34:11 -0500472 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100473 continue;
474
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000475 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200476 struct snd_soc_dai_driver *drv = dai->driver;
477
478 if (drv->ops->digital_mute && dai->playback_active)
479 drv->ops->digital_mute(dai, 1);
480 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 }
482
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100483 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000484 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500485 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100486 continue;
487
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100489 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100490
Mark Brown87506542008-11-18 20:50:34 +0000491 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000492 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000494 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100496
Mengdong Lin1a497982015-11-18 02:34:11 -0500497 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100498 continue;
499
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100500 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100502 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200504 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900505 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100506
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000507 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100510 continue;
511
Mengdong Lin1a497982015-11-18 02:34:11 -0500512 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800513 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800514 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515
Mengdong Lin1a497982015-11-18 02:34:11 -0500516 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800518 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 }
520
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200521 /* Recheck all endpoints too, their state is affected by suspend */
522 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700523 snd_soc_dapm_sync(&card->dapm);
524
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000525 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000526 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200527 struct snd_soc_dapm_context *dapm =
528 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000529
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200530 /*
531 * If there are paths active then the COMPONENT will be held
532 * with bias _ON and should not be suspended.
533 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000534 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200535 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000537 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000538 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000539 * bias off then being in STANDBY
540 * means it's doing something,
541 * otherwise fall through.
542 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200543 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000544 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200545 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000546 break;
547 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500548 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000551 if (component->driver->suspend)
552 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000553 component->suspended = 1;
554 if (component->regmap)
555 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800556 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000557 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 break;
559 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000560 dev_dbg(component->dev,
561 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 break;
563 }
564 }
565 }
566
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000567 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569
Mengdong Lin1a497982015-11-18 02:34:11 -0500570 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 continue;
572
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100573 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800575
576 /* deactivate pins to sleep state */
577 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578 }
579
Mark Brown87506542008-11-18 20:50:34 +0000580 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000581 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
583 return 0;
584}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000585EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200587/*
588 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100589 * setting our codec back up, which can be very slow on I2C
590 */
591static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000593 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200594 container_of(work, struct snd_soc_card,
595 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500596 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000597 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500598 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200600 /*
601 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100602 * so userspace apps are blocked from touching us
603 */
604
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000605 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100606
Mark Brown9949788b2010-05-07 20:24:05 +0100607 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100609
Mark Brown87506542008-11-18 20:50:34 +0000610 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000611 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100613 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000614 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500615 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100616
Mengdong Lin1a497982015-11-18 02:34:11 -0500617 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100618 continue;
619
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100620 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622 }
623
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000624 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000625 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000626 if (component->driver->resume)
627 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000628 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100629 }
630 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000632 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100633
Mengdong Lin1a497982015-11-18 02:34:11 -0500634 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100635 continue;
636
Mengdong Lin1a497982015-11-18 02:34:11 -0500637 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000638 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800639 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640
Mengdong Lin1a497982015-11-18 02:34:11 -0500641 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000642 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800643 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 }
645
Mark Brown3ff3f642008-05-19 12:32:25 +0200646 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000647 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000648 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100649
Mengdong Lin1a497982015-11-18 02:34:11 -0500650 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100651 continue;
652
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000653 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200654 struct snd_soc_dai_driver *drv = dai->driver;
655
656 if (drv->ops->digital_mute && dai->playback_active)
657 drv->ops->digital_mute(dai, 0);
658 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 }
660
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000661 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100665 continue;
666
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100667 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 }
670
Mark Brown87506542008-11-18 20:50:34 +0000671 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000672 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000674 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100675
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200676 /* Recheck all endpoints too, their state is affected by suspend */
677 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700678 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530679
680 /* userspace can access us now we are back as we were before */
681 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100682}
683
684/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000685int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100686{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000687 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100688 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500689 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200690
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200691 /* If the card is not initialized yet there is nothing to do */
692 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800693 return 0;
694
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800695 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000696 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000697 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
699 int j;
700
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800701 if (cpu_dai->active)
702 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000704 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200705 if (codec_dai->active)
706 pinctrl_pm_select_default_state(codec_dai->dev);
707 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800708 }
709
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100710 /*
711 * DAIs that also act as the control bus master might have other drivers
712 * hanging off them so need to resume immediately. Other drivers don't
713 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100714 * due to I/O costs and anti-pop so handle them out of line.
715 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000716 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200718
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100719 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600720 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100721 if (bus_control) {
722 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600723 soc_resume_deferred(&card->deferred_resume_work);
724 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000725 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000727 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100728 }
Andy Green6ed25972008-06-13 16:24:05 +0100729
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 return 0;
731}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000732EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734#define snd_soc_suspend NULL
735#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#endif
737
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100738static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800739};
740
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200741static struct snd_soc_component *soc_find_component(
742 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100743{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200744 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100745
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100746 lockdep_assert_held(&client_mutex);
747
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000748 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200749 if (of_node) {
750 if (component->dev->of_node == of_node)
751 return component;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000752 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200753 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100754 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100755 }
756
757 return NULL;
758}
759
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000760static int snd_soc_is_matching_component(
761 const struct snd_soc_dai_link_component *dlc,
762 struct snd_soc_component *component)
763{
764 struct device_node *component_of_node;
765
766 component_of_node = component->dev->of_node;
767 if (!component_of_node && component->dev->parent)
768 component_of_node = component->dev->parent->of_node;
769
770 if (dlc->of_node && component_of_node != dlc->of_node)
771 return 0;
772 if (dlc->name && strcmp(component->name, dlc->name))
773 return 0;
774
775 return 1;
776}
777
Mengdong Linfbb88b52016-04-22 12:25:33 +0800778/**
779 * snd_soc_find_dai - Find a registered DAI
780 *
Jeffy Chen49584712017-08-22 15:57:21 +0800781 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800782 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700783 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800784 * find the DAI of the same name. The component's of_node and name
785 * should also match if being specified.
786 *
787 * Return: pointer of DAI, or NULL if not found.
788 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800789struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200790 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100791{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200792 struct snd_soc_component *component;
793 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100794
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100795 lockdep_assert_held(&client_mutex);
796
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200797 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000798 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000799 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200800 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000801 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800802 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800803 && (!dai->driver->name
804 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100805 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100806
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200807 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100808 }
809 }
810
811 return NULL;
812}
Mengdong Lin305e9022016-04-19 13:12:25 +0800813EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100814
Mengdong Lin17fb17552016-11-03 01:04:12 +0800815/**
816 * snd_soc_find_dai_link - Find a DAI link
817 *
818 * @card: soc card
819 * @id: DAI link ID to match
820 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000821 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800822 *
823 * This function will search all existing DAI links of the soc card to
824 * find the link of the same ID. Since DAI links may not have their
825 * unique ID, so name and stream name should also match if being
826 * specified.
827 *
828 * Return: pointer of DAI link, or NULL if not found.
829 */
830struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
831 int id, const char *name,
832 const char *stream_name)
833{
834 struct snd_soc_dai_link *link, *_link;
835
836 lockdep_assert_held(&client_mutex);
837
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000838 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800839 if (link->id != id)
840 continue;
841
842 if (name && (!link->name || strcmp(name, link->name)))
843 continue;
844
845 if (stream_name && (!link->stream_name
846 || strcmp(stream_name, link->stream_name)))
847 continue;
848
849 return link;
850 }
851
852 return NULL;
853}
854EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
855
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800856static bool soc_is_dai_link_bound(struct snd_soc_card *card,
857 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800859 struct snd_soc_pcm_runtime *rtd;
860
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000861 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800862 if (rtd->dai_link == dai_link)
863 return true;
864 }
865
866 return false;
867}
868
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500869static int soc_bind_dai_link(struct snd_soc_card *card,
870 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871{
Mengdong Lin1a497982015-11-18 02:34:11 -0500872 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200873 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200874 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000875 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500876 struct snd_soc_dai **codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200877 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878
Liam Girdwooda655de82018-07-02 16:59:54 +0100879 if (dai_link->ignore)
880 return 0;
881
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500882 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000883
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800884 if (soc_is_dai_link_bound(card, dai_link)) {
885 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
886 dai_link->name);
887 return 0;
888 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530890 rtd = soc_new_pcm_runtime(card, dai_link);
891 if (!rtd)
892 return -ENOMEM;
893
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200894 cpu_dai_component.name = dai_link->cpu_name;
895 cpu_dai_component.of_node = dai_link->cpu_of_node;
896 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
897 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000898 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100899 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
900 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500901 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000902 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000903 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000904
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 rtd->num_codecs = dai_link->num_codecs;
906
907 /* Find CODEC from registered CODECs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000908 /* we can use for_each_rtd_codec_dai() after this */
Mengdong Lin1a497982015-11-18 02:34:11 -0500909 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200910 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200911 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200912 if (!codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100913 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
914 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500915 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200916 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000917 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000918 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100919
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 /* Single codec links expect codec and codec_dai in runtime data */
921 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100922
Mark Brownb19e6e72012-03-14 21:18:39 +0000923 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000924 for_each_component(component) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900925 if (!snd_soc_is_matching_component(dai_link->platforms,
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000926 component))
927 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000928
929 snd_soc_rtdcom_add(rtd, component);
930 }
931
Mengdong Lin1a497982015-11-18 02:34:11 -0500932 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000933 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500934
935_err_defer:
936 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200937 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938}
939
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900940static void soc_cleanup_component(struct snd_soc_component *component)
941{
942 list_del(&component->card_list);
943 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
944 soc_cleanup_component_debugfs(component);
945 component->card = NULL;
946 module_put(component->dev->driver->owner);
947}
948
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200949static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600950{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200951 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200952 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600953
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000954 if (component->driver->remove)
955 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600956
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900957 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600958}
959
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200960static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200961{
962 int err;
963
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000964 if (!dai || !dai->probed ||
965 dai->driver->remove_order != order)
966 return;
967
968 if (dai->driver->remove) {
969 err = dai->driver->remove(dai);
970 if (err < 0)
971 dev_err(dai->dev,
972 "ASoC: failed to remove %s: %d\n",
973 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100974 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000975 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100976}
977
Mengdong Lin1a497982015-11-18 02:34:11 -0500978static void soc_remove_link_dais(struct snd_soc_card *card,
979 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200981 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000982 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983
984 /* unregister the rtd device */
985 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800986 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 rtd->dev_registered = 0;
988 }
989
990 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000991 for_each_rtd_codec_dai(rtd, i, codec_dai)
992 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000993
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200994 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995}
996
Mengdong Lin1a497982015-11-18 02:34:11 -0500997static void soc_remove_link_components(struct snd_soc_card *card,
998 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001000 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001001 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001002
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001003 for_each_rtdcom(rtd, rtdcom) {
1004 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001005
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001006 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001007 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001008 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001009}
1010
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001011static void soc_remove_dai_links(struct snd_soc_card *card)
1012{
Mengdong Lin1a497982015-11-18 02:34:11 -05001013 int order;
1014 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001015 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001016
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001017 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001018 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001019 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001020 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001021
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_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001025 }
1026
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001027 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001028 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1029 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1030 link->name);
1031
1032 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001033 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001034}
1035
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001036static int snd_soc_init_platform(struct snd_soc_card *card,
1037 struct snd_soc_dai_link *dai_link)
1038{
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001039 struct snd_soc_dai_link_component *platform = dai_link->platforms;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001040
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001041 /*
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001042 * REMOVE ME
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001043 *
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001044 * This is glue code for Legacy vs Modern dai_link.
1045 * This function will be removed if all derivers are switched to
1046 * modern style dai_link.
1047 * Driver shouldn't use both legacy and modern style in the same time.
1048 * see
1049 * soc.h :: struct snd_soc_dai_link
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001050 */
1051 /* convert Legacy platform link */
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001052 if (!platform || dai_link->legacy_platform) {
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001053 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001054 sizeof(struct snd_soc_dai_link_component),
1055 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001056 if (!platform)
1057 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001058
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001059 dai_link->platforms = platform;
1060 dai_link->num_platforms = 1;
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001061 dai_link->legacy_platform = 1;
1062 platform->name = dai_link->platform_name;
1063 platform->of_node = dai_link->platform_of_node;
1064 platform->dai_name = NULL;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001065 }
1066
1067 /* if there's no platform we match on the empty platform */
1068 if (!platform->name &&
1069 !platform->of_node)
1070 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001071
1072 return 0;
1073}
1074
Mengdong Lin923c5e612015-11-23 11:01:49 -05001075static int snd_soc_init_multicodec(struct snd_soc_card *card,
1076 struct snd_soc_dai_link *dai_link)
1077{
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001078 /*
1079 * REMOVE ME
1080 *
1081 * This is glue code for Legacy vs Modern dai_link.
1082 * This function will be removed if all derivers are switched to
1083 * modern style dai_link.
1084 * Driver shouldn't use both legacy and modern style in the same time.
1085 * see
1086 * soc.h :: struct snd_soc_dai_link
1087 */
1088
Mengdong Lin923c5e612015-11-23 11:01:49 -05001089 /* Legacy codec/codec_dai link is a single entry in multicodec */
1090 if (dai_link->codec_name || dai_link->codec_of_node ||
1091 dai_link->codec_dai_name) {
1092 dai_link->num_codecs = 1;
1093
1094 dai_link->codecs = devm_kzalloc(card->dev,
1095 sizeof(struct snd_soc_dai_link_component),
1096 GFP_KERNEL);
1097 if (!dai_link->codecs)
1098 return -ENOMEM;
1099
1100 dai_link->codecs[0].name = dai_link->codec_name;
1101 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1102 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1103 }
1104
1105 if (!dai_link->codecs) {
1106 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1107 return -EINVAL;
1108 }
1109
1110 return 0;
1111}
1112
1113static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001114 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001115{
1116 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001117 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001118
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001119 ret = snd_soc_init_platform(card, link);
1120 if (ret) {
1121 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1122 return ret;
1123 }
1124
Mengdong Lin923c5e612015-11-23 11:01:49 -05001125 ret = snd_soc_init_multicodec(card, link);
1126 if (ret) {
1127 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1128 return ret;
1129 }
1130
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001131 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001132 /*
1133 * Codec must be specified by 1 of name or OF node,
1134 * not both or neither.
1135 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001136 if (!!codec->name ==
1137 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001138 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1139 link->name);
1140 return -EINVAL;
1141 }
1142 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001143 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001144 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1145 link->name);
1146 return -EINVAL;
1147 }
1148 }
1149
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001150 /* FIXME */
1151 if (link->num_platforms > 1) {
1152 dev_err(card->dev,
1153 "ASoC: multi platform is not yet supported %s\n",
1154 link->name);
1155 return -EINVAL;
1156 }
1157
Mengdong Lin923c5e612015-11-23 11:01:49 -05001158 /*
1159 * Platform may be specified by either name or OF node, but
1160 * can be left unspecified, and a dummy platform will be used.
1161 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001162 if (link->platforms->name && link->platforms->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001163 dev_err(card->dev,
1164 "ASoC: Both platform name/of_node are set for %s\n",
1165 link->name);
1166 return -EINVAL;
1167 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301168
1169 /*
1170 * Defer card registartion if platform dai component is not added to
1171 * component list.
1172 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001173 if ((link->platforms->of_node || link->platforms->name) &&
1174 !soc_find_component(link->platforms->of_node, link->platforms->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301175 return -EPROBE_DEFER;
1176
Mengdong Lin923c5e612015-11-23 11:01:49 -05001177 /*
1178 * CPU device may be specified by either name or OF node, but
1179 * can be left unspecified, and will be matched based on DAI
1180 * name alone..
1181 */
1182 if (link->cpu_name && link->cpu_of_node) {
1183 dev_err(card->dev,
1184 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1185 link->name);
1186 return -EINVAL;
1187 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301188
1189 /*
1190 * Defer card registartion if cpu dai component is not added to
1191 * component list.
1192 */
Matthias Reichl28335482019-01-15 17:51:07 +01001193 if ((link->cpu_of_node || link->cpu_name) &&
1194 !soc_find_component(link->cpu_of_node, link->cpu_name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301195 return -EPROBE_DEFER;
1196
Mengdong Lin923c5e612015-11-23 11:01:49 -05001197 /*
1198 * At least one of CPU DAI name or CPU device name/node must be
1199 * specified
1200 */
1201 if (!link->cpu_dai_name &&
1202 !(link->cpu_name || link->cpu_of_node)) {
1203 dev_err(card->dev,
1204 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1205 link->name);
1206 return -EINVAL;
1207 }
1208
1209 return 0;
1210}
1211
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001212void snd_soc_disconnect_sync(struct device *dev)
1213{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001214 struct snd_soc_component *component =
1215 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001216
1217 if (!component || !component->card)
1218 return;
1219
1220 snd_card_disconnect_sync(component->card->snd_card);
1221}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001222EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001223
Mengdong Linf8f80362015-12-02 14:11:22 +08001224/**
1225 * snd_soc_add_dai_link - Add a DAI link dynamically
1226 * @card: The ASoC card to which the DAI link is added
1227 * @dai_link: The new DAI link to add
1228 *
1229 * This function adds a DAI link to the ASoC card's link list.
1230 *
1231 * Note: Topology can use this API to add DAI links when probing the
1232 * topology component. And machine drivers can still define static
1233 * DAI links in dai_link array.
1234 */
1235int snd_soc_add_dai_link(struct snd_soc_card *card,
1236 struct snd_soc_dai_link *dai_link)
1237{
1238 if (dai_link->dobj.type
1239 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1240 dev_err(card->dev, "Invalid dai link type %d\n",
1241 dai_link->dobj.type);
1242 return -EINVAL;
1243 }
1244
1245 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001246 /*
1247 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001248 * on the link created by topology.
1249 */
1250 if (dai_link->dobj.type && card->add_dai_link)
1251 card->add_dai_link(card, dai_link);
1252
Mengdong Linf8f80362015-12-02 14:11:22 +08001253 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001254
1255 return 0;
1256}
1257EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1258
1259/**
1260 * snd_soc_remove_dai_link - Remove a DAI link from the list
1261 * @card: The ASoC card that owns the link
1262 * @dai_link: The DAI link to remove
1263 *
1264 * This function removes a DAI link from the ASoC card's link list.
1265 *
1266 * For DAI links previously added by topology, topology should
1267 * remove them by using the dobj embedded in the link.
1268 */
1269void snd_soc_remove_dai_link(struct snd_soc_card *card,
1270 struct snd_soc_dai_link *dai_link)
1271{
1272 struct snd_soc_dai_link *link, *_link;
1273
1274 if (dai_link->dobj.type
1275 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1276 dev_err(card->dev, "Invalid dai link type %d\n",
1277 dai_link->dobj.type);
1278 return;
1279 }
1280
1281 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001282 /*
1283 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001284 * on the link created by topology.
1285 */
1286 if (dai_link->dobj.type && card->remove_dai_link)
1287 card->remove_dai_link(card, dai_link);
1288
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001289 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001290 if (link == dai_link) {
1291 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001292 return;
1293 }
1294 }
1295}
1296EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1297
Jerome Brunetaefba452018-07-13 14:50:43 +02001298static void soc_set_of_name_prefix(struct snd_soc_component *component)
1299{
1300 struct device_node *component_of_node = component->dev->of_node;
1301 const char *str;
1302 int ret;
1303
1304 if (!component_of_node && component->dev->parent)
1305 component_of_node = component->dev->parent->of_node;
1306
1307 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1308 &str);
1309 if (!ret)
1310 component->name_prefix = str;
1311}
1312
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001313static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001314 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001315{
1316 int i;
1317
Jerome Brunetaefba452018-07-13 14:50:43 +02001318 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001319 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001320 struct device_node *component_of_node = component->dev->of_node;
1321
1322 if (!component_of_node && component->dev->parent)
1323 component_of_node = component->dev->parent->of_node;
1324
1325 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001326 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001327 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001328 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001329 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001330 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001331 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001332
1333 /*
1334 * If there is no configuration table or no match in the table,
1335 * check if a prefix is provided in the node
1336 */
1337 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001338}
1339
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001340static int soc_probe_component(struct snd_soc_card *card,
1341 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001342{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001343 struct snd_soc_dapm_context *dapm =
1344 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001345 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001346 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001347
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001348 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001349 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001350
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001351 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001352 if (component->card != card) {
1353 dev_err(component->dev,
1354 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1355 card->name, component->card->name);
1356 return -ENODEV;
1357 }
1358 return 0;
1359 }
1360
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001361 if (!try_module_get(component->dev->driver->owner))
1362 return -ENODEV;
1363
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001364 component->card = card;
1365 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001366 INIT_LIST_HEAD(&component->card_list);
1367 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001368 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001369
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001370 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001371
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001372 if (component->driver->dapm_widgets) {
1373 ret = snd_soc_dapm_new_controls(dapm,
1374 component->driver->dapm_widgets,
1375 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001376
1377 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001378 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001379 "Failed to create new controls %d\n", ret);
1380 goto err_probe;
1381 }
1382 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001383
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001384 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001385 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001386 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001387 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001388 "Failed to create DAI widgets %d\n", ret);
1389 goto err_probe;
1390 }
1391 }
Mark Brown888df392012-02-16 19:37:51 -08001392
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001393 if (component->driver->probe) {
1394 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001395 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001396 dev_err(component->dev,
1397 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001398 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001399 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001400
1401 WARN(dapm->idle_bias_off &&
1402 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001403 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001404 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001405 }
1406
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001407 /* machine specific init */
1408 if (component->init) {
1409 ret = component->init(component);
1410 if (ret < 0) {
1411 dev_err(component->dev,
1412 "Failed to do machine specific init %d\n", ret);
1413 goto err_probe;
1414 }
1415 }
1416
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001417 if (component->driver->controls)
1418 snd_soc_add_component_controls(component,
1419 component->driver->controls,
1420 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001421 if (component->driver->dapm_routes)
1422 snd_soc_dapm_add_routes(dapm,
1423 component->driver->dapm_routes,
1424 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001425
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001426 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001427 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001428 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001429
Jarkko Nikula70d293312011-01-27 16:24:22 +02001430err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001431 if (ret < 0)
1432 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001433
1434 return ret;
1435}
1436
Mark Brown36ae1a92012-01-06 17:12:45 -08001437static void rtd_release(struct device *dev)
1438{
1439 kfree(dev);
1440}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001441
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001442static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1443 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001444{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001445 int ret = 0;
1446
Jarkko Nikula589c3562010-12-06 16:27:07 +02001447 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001448 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1449 if (!rtd->dev)
1450 return -ENOMEM;
1451 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001452 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001453 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001454 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001455 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001456 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001457 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001458 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1459 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1460 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1461 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001462 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001463 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001464 /* calling put_device() here to free the rtd->dev */
1465 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001466 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001467 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001468 return ret;
1469 }
1470 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001471 return 0;
1472}
1473
Mengdong Lin1a497982015-11-18 02:34:11 -05001474static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001475 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001476{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001477 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001478 struct snd_soc_rtdcom_list *rtdcom;
1479 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001480
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001481 for_each_rtdcom(rtd, rtdcom) {
1482 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001483
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001484 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001485 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001486 if (ret < 0)
1487 return ret;
1488 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001489 }
1490
Stephen Warren62ae68f2012-06-08 12:34:23 -06001491 return 0;
1492}
1493
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001494static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001495{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001496 if (dai->probed ||
1497 dai->driver->probe_order != order)
1498 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001499
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001500 if (dai->driver->probe) {
1501 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001502
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001503 if (ret < 0) {
1504 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1505 dai->name, ret);
1506 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001507 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001508 }
1509
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001510 dai->probed = 1;
1511
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001512 return 0;
1513}
1514
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001515static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1516 struct snd_soc_pcm_runtime *rtd)
1517{
1518 int i, ret = 0;
1519
1520 for (i = 0; i < num_dais; ++i) {
1521 struct snd_soc_dai_driver *drv = dais[i]->driver;
1522
Rohit kumarde17f142018-11-01 18:08:49 +05301523 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001524 ret = drv->pcm_new(rtd, dais[i]);
1525 if (ret < 0) {
1526 dev_err(dais[i]->dev,
1527 "ASoC: Failed to bind %s with pcm device\n",
1528 dais[i]->name);
1529 return ret;
1530 }
1531 }
1532
1533 return 0;
1534}
1535
Mengdong Lin1a497982015-11-18 02:34:11 -05001536static int soc_probe_link_dais(struct snd_soc_card *card,
1537 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538{
Mengdong Lin1a497982015-11-18 02:34:11 -05001539 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001540 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001541 struct snd_soc_rtdcom_list *rtdcom;
1542 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001543 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001544 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001546 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001547 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549 /* set default power off timeout */
1550 rtd->pmdown_time = pmdown_time;
1551
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001552 ret = soc_probe_dai(cpu_dai, order);
1553 if (ret)
1554 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001555
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001556 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001557 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1558 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001559 if (ret)
1560 return ret;
1561 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562
Liam Girdwood0168bf02011-06-07 16:08:05 +01001563 /* complete DAI probe during last probe */
1564 if (order != SND_SOC_COMP_ORDER_LAST)
1565 return 0;
1566
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001567 /* do machine specific initialization */
1568 if (dai_link->init) {
1569 ret = dai_link->init(rtd);
1570 if (ret < 0) {
1571 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1572 dai_link->name, ret);
1573 return ret;
1574 }
1575 }
1576
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001577 if (dai_link->dai_fmt)
1578 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1579
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001580 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001581 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001584#ifdef CONFIG_DEBUG_FS
1585 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001586 if (dai_link->dynamic)
1587 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001588#endif
1589
Liam Girdwooda655de82018-07-02 16:59:54 +01001590 num = rtd->num;
1591
1592 /*
1593 * most drivers will register their PCMs using DAI link ordering but
1594 * topology based drivers can use the DAI link id field to set PCM
1595 * device number and then use rtd + a base offset of the BEs.
1596 */
1597 for_each_rtdcom(rtd, rtdcom) {
1598 component = rtdcom->component;
1599
1600 if (!component->driver->use_dai_pcm_id)
1601 continue;
1602
1603 if (rtd->dai_link->no_pcm)
1604 num += component->driver->be_pcm_base;
1605 else
1606 num = rtd->dai_link->id;
1607 }
1608
Jie Yang6f0c4222015-10-13 23:41:00 +08001609 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001610 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001611 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001612 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001613 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301614 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001615 return ret;
1616 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001617 } else if (!dai_link->params) {
1618 /* create the pcm */
1619 ret = soc_new_pcm(rtd, num);
1620 if (ret < 0) {
1621 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1622 dai_link->stream_name, ret);
1623 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001624 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001625 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1626 if (ret < 0)
1627 return ret;
1628 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1629 rtd->num_codecs, rtd);
1630 if (ret < 0)
1631 return ret;
1632 } else {
1633 INIT_DELAYED_WORK(&rtd->delayed_work,
1634 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 }
1636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 return 0;
1638}
1639
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001640static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001641{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001642 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001643 struct snd_soc_component *component;
1644 const char *name;
1645 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001646
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001647 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1648 /* codecs, usually analog devices */
1649 name = aux_dev->codec_name;
1650 codec_of_node = aux_dev->codec_of_node;
1651 component = soc_find_component(codec_of_node, name);
1652 if (!component) {
1653 if (codec_of_node)
1654 name = of_node_full_name(codec_of_node);
1655 goto err_defer;
1656 }
1657 } else if (aux_dev->name) {
1658 /* generic components */
1659 name = aux_dev->name;
1660 component = soc_find_component(NULL, name);
1661 if (!component)
1662 goto err_defer;
1663 } else {
1664 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1665 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001666 }
1667
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001668 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001669 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001670
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001671 return 0;
1672
1673err_defer:
1674 dev_err(card->dev, "ASoC: %s not registered\n", name);
1675 return -EPROBE_DEFER;
1676}
1677
1678static int soc_probe_aux_devices(struct snd_soc_card *card)
1679{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001680 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001681 int order;
1682 int ret;
1683
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001684 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001685 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001686 if (comp->driver->probe_order == order) {
1687 ret = soc_probe_component(card, comp);
1688 if (ret < 0) {
1689 dev_err(card->dev,
1690 "ASoC: failed to probe aux component %s %d\n",
1691 comp->name, ret);
1692 return ret;
1693 }
1694 }
1695 }
1696 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001697
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001698 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001699}
1700
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001701static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001703 struct snd_soc_component *comp, *_comp;
1704 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001705
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001706 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001707 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001708 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001709
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001710 if (comp->driver->remove_order == order) {
1711 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001712 /* remove it from the card's aux_comp_list */
1713 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001714 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001715 }
1716 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001717}
1718
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001719/**
1720 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1721 * @rtd: The runtime for which the DAI link format should be changed
1722 * @dai_fmt: The new DAI link format
1723 *
1724 * This function updates the DAI link format for all DAIs connected to the DAI
1725 * link for the specified runtime.
1726 *
1727 * Note: For setups with a static format set the dai_fmt field in the
1728 * corresponding snd_dai_link struct instead of using this function.
1729 *
1730 * Returns 0 on success, otherwise a negative error code.
1731 */
1732int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1733 unsigned int dai_fmt)
1734{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001735 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001736 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001737 unsigned int i;
1738 int ret;
1739
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001740 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001741 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1742 if (ret != 0 && ret != -ENOTSUPP) {
1743 dev_warn(codec_dai->dev,
1744 "ASoC: Failed to set DAI format: %d\n", ret);
1745 return ret;
1746 }
1747 }
1748
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001749 /*
1750 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1751 * the component which has non_legacy_dai_naming is Codec
1752 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001753 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001754 unsigned int inv_dai_fmt;
1755
1756 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1757 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1758 case SND_SOC_DAIFMT_CBM_CFM:
1759 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1760 break;
1761 case SND_SOC_DAIFMT_CBM_CFS:
1762 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1763 break;
1764 case SND_SOC_DAIFMT_CBS_CFM:
1765 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1766 break;
1767 case SND_SOC_DAIFMT_CBS_CFS:
1768 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1769 break;
1770 }
1771
1772 dai_fmt = inv_dai_fmt;
1773 }
1774
1775 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1776 if (ret != 0 && ret != -ENOTSUPP) {
1777 dev_warn(cpu_dai->dev,
1778 "ASoC: Failed to set DAI format: %d\n", ret);
1779 return ret;
1780 }
1781
1782 return 0;
1783}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001784EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001785
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001786#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001787/*
1788 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001789 * separate different DMI fields in the card long name. Only number and
1790 * alphabet characters and a few separator characters are kept.
1791 */
1792static void cleanup_dmi_name(char *name)
1793{
1794 int i, j = 0;
1795
1796 for (i = 0; name[i]; i++) {
1797 if (isalnum(name[i]) || (name[i] == '.')
1798 || (name[i] == '_'))
1799 name[j++] = name[i];
1800 else if (name[i] == '-')
1801 name[j++] = '_';
1802 }
1803
1804 name[j] = '\0';
1805}
1806
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001807/*
1808 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001809 * in the black list.
1810 */
1811static int is_dmi_valid(const char *field)
1812{
1813 int i = 0;
1814
1815 while (dmi_blacklist[i]) {
1816 if (strstr(field, dmi_blacklist[i]))
1817 return 0;
1818 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001819 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001820
1821 return 1;
1822}
1823
Liam Girdwood345233d2017-01-14 16:13:02 +08001824/**
1825 * snd_soc_set_dmi_name() - Register DMI names to card
1826 * @card: The card to register DMI names
1827 * @flavour: The flavour "differentiator" for the card amongst its peers.
1828 *
1829 * An Intel machine driver may be used by many different devices but are
1830 * difficult for userspace to differentiate, since machine drivers ususally
1831 * use their own name as the card short name and leave the card long name
1832 * blank. To differentiate such devices and fix bugs due to lack of
1833 * device-specific configurations, this function allows DMI info to be used
1834 * as the sound card long name, in the format of
1835 * "vendor-product-version-board"
1836 * (Character '-' is used to separate different DMI fields here).
1837 * This will help the user space to load the device-specific Use Case Manager
1838 * (UCM) configurations for the card.
1839 *
1840 * Possible card long names may be:
1841 * DellInc.-XPS139343-01-0310JH
1842 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1843 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1844 *
1845 * This function also supports flavoring the card longname to provide
1846 * the extra differentiation, like "vendor-product-version-board-flavor".
1847 *
1848 * We only keep number and alphabet characters and a few separator characters
1849 * in the card long name since UCM in the user space uses the card long names
1850 * as card configuration directory names and AudoConf cannot support special
1851 * charactors like SPACE.
1852 *
1853 * Returns 0 on success, otherwise a negative error code.
1854 */
1855int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1856{
1857 const char *vendor, *product, *product_version, *board;
1858 size_t longname_buf_size = sizeof(card->snd_card->longname);
1859 size_t len;
1860
1861 if (card->long_name)
1862 return 0; /* long name already set by driver or from DMI */
1863
1864 /* make up dmi long name as: vendor.product.version.board */
1865 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001866 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001867 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1868 return 0;
1869 }
1870
1871 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1872 "%s", vendor);
1873 cleanup_dmi_name(card->dmi_longname);
1874
1875 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001876 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001877 len = strlen(card->dmi_longname);
1878 snprintf(card->dmi_longname + len,
1879 longname_buf_size - len,
1880 "-%s", product);
1881
1882 len++; /* skip the separator "-" */
1883 if (len < longname_buf_size)
1884 cleanup_dmi_name(card->dmi_longname + len);
1885
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001886 /*
1887 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001888 * name in the product version field
1889 */
1890 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001891 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001892 len = strlen(card->dmi_longname);
1893 snprintf(card->dmi_longname + len,
1894 longname_buf_size - len,
1895 "-%s", product_version);
1896
1897 len++;
1898 if (len < longname_buf_size)
1899 cleanup_dmi_name(card->dmi_longname + len);
1900 }
1901 }
1902
1903 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001904 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001905 len = strlen(card->dmi_longname);
1906 snprintf(card->dmi_longname + len,
1907 longname_buf_size - len,
1908 "-%s", board);
1909
1910 len++;
1911 if (len < longname_buf_size)
1912 cleanup_dmi_name(card->dmi_longname + len);
1913 } else if (!product) {
1914 /* fall back to using legacy name */
1915 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1916 return 0;
1917 }
1918
1919 /* Add flavour to dmi long name */
1920 if (flavour) {
1921 len = strlen(card->dmi_longname);
1922 snprintf(card->dmi_longname + len,
1923 longname_buf_size - len,
1924 "-%s", flavour);
1925
1926 len++;
1927 if (len < longname_buf_size)
1928 cleanup_dmi_name(card->dmi_longname + len);
1929 }
1930
1931 /* set the card long name */
1932 card->long_name = card->dmi_longname;
1933
1934 return 0;
1935}
1936EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001937#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001938
Liam Girdwooda655de82018-07-02 16:59:54 +01001939static void soc_check_tplg_fes(struct snd_soc_card *card)
1940{
1941 struct snd_soc_component *component;
1942 const struct snd_soc_component_driver *comp_drv;
1943 struct snd_soc_dai_link *dai_link;
1944 int i;
1945
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001946 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001947
1948 /* does this component override FEs ? */
1949 if (!component->driver->ignore_machine)
1950 continue;
1951
1952 /* for this machine ? */
1953 if (strcmp(component->driver->ignore_machine,
1954 card->dev->driver->name))
1955 continue;
1956
1957 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001958 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001959
1960 /* ignore this FE */
1961 if (dai_link->dynamic) {
1962 dai_link->ignore = true;
1963 continue;
1964 }
1965
1966 dev_info(card->dev, "info: override FE DAI link %s\n",
1967 card->dai_link[i].name);
1968
1969 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001970 if (snd_soc_init_platform(card, dai_link) < 0) {
1971 dev_err(card->dev, "init platform error");
1972 continue;
1973 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001974 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001975
1976 /* convert non BE into BE */
1977 dai_link->no_pcm = 1;
1978
1979 /* override any BE fixups */
1980 dai_link->be_hw_params_fixup =
1981 component->driver->be_hw_params_fixup;
1982
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001983 /*
1984 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001985 * dai link name if it's NULL to help bind widgets.
1986 */
1987 if (!dai_link->stream_name)
1988 dai_link->stream_name = dai_link->name;
1989 }
1990
1991 /* Inform userspace we are using alternate topology */
1992 if (component->driver->topology_name_prefix) {
1993
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001994 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001995 if (!card->topology_shortname_created) {
1996 comp_drv = component->driver;
1997
1998 snprintf(card->topology_shortname, 32, "%s-%s",
1999 comp_drv->topology_name_prefix,
2000 card->name);
2001 card->topology_shortname_created = true;
2002 }
2003
2004 /* use topology shortname */
2005 card->name = card->topology_shortname;
2006 }
2007 }
2008}
2009
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002010static int soc_cleanup_card_resources(struct snd_soc_card *card)
2011{
2012 /* free the ALSA card at first; this syncs with pending operations */
2013 if (card->snd_card)
2014 snd_card_free(card->snd_card);
2015
2016 /* remove and free each DAI */
2017 soc_remove_dai_links(card);
2018 soc_remove_pcm_runtimes(card);
2019
2020 /* remove auxiliary devices */
2021 soc_remove_aux_devices(card);
2022
2023 snd_soc_dapm_free(&card->dapm);
2024 soc_cleanup_card_debugfs(card);
2025
2026 /* remove the card */
2027 if (card->remove)
2028 card->remove(card);
2029
2030 return 0;
2031}
2032
Mark Brownb19e6e72012-03-14 21:18:39 +00002033static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034{
Mengdong Lin1a497982015-11-18 02:34:11 -05002035 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002036 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002037 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002038
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002039 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002040 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002041
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002042 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2043 card->dapm.dev = card->dev;
2044 card->dapm.card = card;
2045 list_add(&card->dapm.list, &card->dapm_list);
2046
Liam Girdwooda655de82018-07-02 16:59:54 +01002047 /* check whether any platform is ignore machine FE and using topology */
2048 soc_check_tplg_fes(card);
2049
Mark Brownb19e6e72012-03-14 21:18:39 +00002050 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002051 for_each_card_prelinks(card, i, dai_link) {
2052 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002053 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002054 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 }
2056
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002057 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002058 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002059 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002060 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002061 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002062 }
2063
Mengdong Linf8f80362015-12-02 14:11:22 +08002064 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002065 for_each_card_prelinks(card, i, dai_link)
2066 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002067
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002068 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002069 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 card->owner, 0, &card->snd_card);
2071 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002072 dev_err(card->dev,
2073 "ASoC: can't create sound card for card %s: %d\n",
2074 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002075 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002076 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002077
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002078 soc_init_card_debugfs(card);
2079
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002080#ifdef CONFIG_DEBUG_FS
2081 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2082#endif
2083
Mark Brown88ee1c62011-02-02 10:43:26 +00002084#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002085 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002086 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002087#endif
Andy Green6ed25972008-06-13 16:24:05 +01002088
Mark Brown9a841eb2011-04-12 17:51:37 -07002089 if (card->dapm_widgets)
2090 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2091 card->num_dapm_widgets);
2092
Nicolin Chenf23e8602015-02-14 17:22:49 -08002093 if (card->of_dapm_widgets)
2094 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2095 card->num_of_dapm_widgets);
2096
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097 /* initialise the sound card only once */
2098 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002099 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002100 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002101 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002102 }
2103
Stephen Warren62ae68f2012-06-08 12:34:23 -06002104 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002105 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002106 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002107 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002108 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002109 dev_err(card->dev,
2110 "ASoC: failed to instantiate card %d\n",
2111 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002112 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002113 }
2114 }
2115 }
2116
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002117 /* probe auxiliary components */
2118 ret = soc_probe_aux_devices(card);
2119 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002120 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002121
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002122 /*
2123 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002124 * Components with topology may bring new DAIs and DAI links.
2125 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002126 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002127 if (soc_is_dai_link_bound(card, dai_link))
2128 continue;
2129
2130 ret = soc_init_dai_link(card, dai_link);
2131 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002132 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002133 ret = soc_bind_dai_link(card, dai_link);
2134 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002135 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002136 }
2137
Stephen Warren62ae68f2012-06-08 12:34:23 -06002138 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002139 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002140 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002141 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002142 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002143 dev_err(card->dev,
2144 "ASoC: failed to instantiate card %d\n",
2145 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002146 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002147 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002148 }
2149 }
2150
Mark Brown888df392012-02-16 19:37:51 -08002151 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002152 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002153
Mark Brownb7af1da2011-04-07 19:18:44 +09002154 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002155 snd_soc_add_card_controls(card, card->controls,
2156 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002157
Mark Brownb8ad29d2011-03-02 18:35:51 +00002158 if (card->dapm_routes)
2159 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2160 card->num_dapm_routes);
2161
Nicolin Chenf23e8602015-02-14 17:22:49 -08002162 if (card->of_dapm_routes)
2163 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2164 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002165
Takashi Iwai861886d2017-04-24 08:54:42 +02002166 /* try to set some sane longname if DMI is available */
2167 snd_soc_set_dmi_name(card, NULL);
2168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002169 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002170 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002171 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2172 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002173 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2174 "%s", card->driver_name ? card->driver_name : card->name);
2175 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2176 switch (card->snd_card->driver[i]) {
2177 case '_':
2178 case '-':
2179 case '\0':
2180 break;
2181 default:
2182 if (!isalnum(card->snd_card->driver[i]))
2183 card->snd_card->driver[i] = '_';
2184 break;
2185 }
2186 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002187
Mark Brown28e9ad92011-03-02 18:36:34 +00002188 if (card->late_probe) {
2189 ret = card->late_probe(card);
2190 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002191 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002192 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002193 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002194 }
2195 }
2196
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002197 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199 ret = snd_card_register(card->snd_card);
2200 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002201 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2202 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002203 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002204 }
2205
Mark Brown435c5e22008-12-04 15:32:53 +00002206 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002207 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002208 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002209
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002210probe_end:
2211 if (ret < 0)
2212 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002214 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002215 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002216
Mark Brownb19e6e72012-03-14 21:18:39 +00002217 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002218}
2219
2220/* probes a new socdev */
2221static int soc_probe(struct platform_device *pdev)
2222{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002223 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002224
Vinod Koul70a7ca32011-01-14 19:22:48 +05302225 /*
2226 * no card, so machine driver should be registering card
2227 * we should not be here in that case so ret error
2228 */
2229 if (!card)
2230 return -EINVAL;
2231
Mark Brownfe4085e2012-03-02 13:07:41 +00002232 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002233 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002234 card->name);
2235
Mark Brown435c5e22008-12-04 15:32:53 +00002236 /* Bodge while we unpick instantiation */
2237 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002238
Mark Brown28d528c2012-08-09 18:45:23 +01002239 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240}
2241
2242/* removes a socdev */
2243static int soc_remove(struct platform_device *pdev)
2244{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002245 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002246
Mark Brownc5af3a22008-11-28 13:29:45 +00002247 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 return 0;
2249}
2250
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002251int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002252{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002253 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002254 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002255
2256 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002257 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002258
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002259 /*
2260 * Flush out pmdown_time work - we actually do want to run it
2261 * now, we're shutting down so no imminent restart.
2262 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002263 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002265 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002266
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002267 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002268 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002269 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002270 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002271 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002272
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002273 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002274 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002275 pinctrl_pm_select_sleep_state(codec_dai->dev);
2276 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002277 }
2278
Mark Brown416356f2009-06-30 19:05:15 +01002279 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002280}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002281EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002282
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002283const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302284 .suspend = snd_soc_suspend,
2285 .resume = snd_soc_resume,
2286 .freeze = snd_soc_suspend,
2287 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002288 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302289 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002290};
Stephen Warrendeb26072011-04-05 19:35:30 -06002291EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002292
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293/* ASoC platform driver */
2294static struct platform_driver soc_driver = {
2295 .driver = {
2296 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002297 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002298 },
2299 .probe = soc_probe,
2300 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301};
2302
Mark Brown096e49d2009-07-05 15:12:22 +01002303/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304 * snd_soc_cnew - create new control
2305 * @_template: control template
2306 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002307 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002308 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309 *
2310 * Create a new mixer control from a template control.
2311 *
2312 * Returns 0 for success, else error.
2313 */
2314struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002315 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002316 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317{
2318 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002319 struct snd_kcontrol *kcontrol;
2320 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321
2322 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 template.index = 0;
2324
Mark Brownefb7ac32011-03-08 17:23:24 +00002325 if (!long_name)
2326 long_name = template.name;
2327
2328 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002329 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002330 if (!name)
2331 return NULL;
2332
Mark Brownefb7ac32011-03-08 17:23:24 +00002333 template.name = name;
2334 } else {
2335 template.name = long_name;
2336 }
2337
2338 kcontrol = snd_ctl_new1(&template, data);
2339
2340 kfree(name);
2341
2342 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343}
2344EXPORT_SYMBOL_GPL(snd_soc_cnew);
2345
Liam Girdwood022658b2012-02-03 17:43:09 +00002346static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2347 const struct snd_kcontrol_new *controls, int num_controls,
2348 const char *prefix, void *data)
2349{
2350 int err, i;
2351
2352 for (i = 0; i < num_controls; i++) {
2353 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002354
Liam Girdwood022658b2012-02-03 17:43:09 +00002355 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2356 control->name, prefix));
2357 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002358 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2359 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002360 return err;
2361 }
2362 }
2363
2364 return 0;
2365}
2366
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002367struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2368 const char *name)
2369{
2370 struct snd_card *card = soc_card->snd_card;
2371 struct snd_kcontrol *kctl;
2372
2373 if (unlikely(!name))
2374 return NULL;
2375
2376 list_for_each_entry(kctl, &card->controls, list)
2377 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2378 return kctl;
2379 return NULL;
2380}
2381EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2382
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002384 * snd_soc_add_component_controls - Add an array of controls to a component.
2385 *
2386 * @component: Component to add controls to
2387 * @controls: Array of controls to add
2388 * @num_controls: Number of elements in the array
2389 *
2390 * Return: 0 for success, else error.
2391 */
2392int snd_soc_add_component_controls(struct snd_soc_component *component,
2393 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2394{
2395 struct snd_card *card = component->card->snd_card;
2396
2397 return snd_soc_add_controls(card, component->dev, controls,
2398 num_controls, component->name_prefix, component);
2399}
2400EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2401
2402/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002403 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2404 * Convenience function to add a list of controls.
2405 *
2406 * @soc_card: SoC card to add controls to
2407 * @controls: array of controls to add
2408 * @num_controls: number of elements in the array
2409 *
2410 * Return 0 for success, else error.
2411 */
2412int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2413 const struct snd_kcontrol_new *controls, int num_controls)
2414{
2415 struct snd_card *card = soc_card->snd_card;
2416
2417 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2418 NULL, soc_card);
2419}
2420EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2421
2422/**
2423 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2424 * Convienience function to add a list of controls.
2425 *
2426 * @dai: DAI to add controls to
2427 * @controls: array of controls to add
2428 * @num_controls: number of elements in the array
2429 *
2430 * Return 0 for success, else error.
2431 */
2432int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2433 const struct snd_kcontrol_new *controls, int num_controls)
2434{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002435 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002436
2437 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2438 NULL, dai);
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2441
2442/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002443 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2444 * @dai: DAI
2445 * @clk_id: DAI specific clock ID
2446 * @freq: new clock frequency in Hz
2447 * @dir: new clock direction - input/output.
2448 *
2449 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2450 */
2451int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2452 unsigned int freq, int dir)
2453{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002454 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002455 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002456
2457 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2458 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002459}
2460EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2461
2462/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002463 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2464 * @component: COMPONENT
2465 * @clk_id: DAI specific clock ID
2466 * @source: Source for the clock
2467 * @freq: new clock frequency in Hz
2468 * @dir: new clock direction - input/output.
2469 *
2470 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2471 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002472int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2473 int clk_id, int source, unsigned int freq,
2474 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002475{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002476 if (component->driver->set_sysclk)
2477 return component->driver->set_sysclk(component, clk_id, source,
2478 freq, dir);
2479
2480 return -ENOTSUPP;
2481}
2482EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2483
2484/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002485 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2486 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002487 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002488 * @div: new clock divisor.
2489 *
2490 * Configures the clock dividers. This is used to derive the best DAI bit and
2491 * frame clocks from the system or master clock. It's best to set the DAI bit
2492 * and frame clocks as low as possible to save system power.
2493 */
2494int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2495 int div_id, int div)
2496{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002497 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002498 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002499 else
2500 return -EINVAL;
2501}
2502EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2503
2504/**
2505 * snd_soc_dai_set_pll - configure DAI PLL.
2506 * @dai: DAI
2507 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002508 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002509 * @freq_in: PLL input clock frequency in Hz
2510 * @freq_out: requested PLL output clock frequency in Hz
2511 *
2512 * Configures and enables PLL to generate output clock based on input clock.
2513 */
Mark Brown85488032009-09-05 18:52:16 +01002514int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2515 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002516{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002517 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002518 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002519 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002520
2521 return snd_soc_component_set_pll(dai->component, pll_id, source,
2522 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002523}
2524EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2525
Mark Brownec4ee522011-03-07 20:58:11 +00002526/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002527 * snd_soc_component_set_pll - configure component PLL.
2528 * @component: COMPONENT
2529 * @pll_id: DAI specific PLL ID
2530 * @source: DAI specific source for the PLL
2531 * @freq_in: PLL input clock frequency in Hz
2532 * @freq_out: requested PLL output clock frequency in Hz
2533 *
2534 * Configures and enables PLL to generate output clock based on input clock.
2535 */
2536int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2537 int source, unsigned int freq_in,
2538 unsigned int freq_out)
2539{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002540 if (component->driver->set_pll)
2541 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002542 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002543
2544 return -EINVAL;
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2547
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002548/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002549 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2550 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002551 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002552 *
2553 * Configures the DAI for a preset BCLK to sample rate ratio.
2554 */
2555int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2556{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002557 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002558 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2559 else
2560 return -EINVAL;
2561}
2562EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2563
2564/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002565 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2566 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002567 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002568 *
2569 * Configures the DAI hardware format and clocking.
2570 */
2571int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2572{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002573 if (dai->driver->ops->set_fmt == NULL)
2574 return -ENOTSUPP;
2575 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002576}
2577EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2578
2579/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002580 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002581 * @slots: Number of slots in use.
2582 * @tx_mask: bitmask representing active TX slots.
2583 * @rx_mask: bitmask representing active RX slots.
2584 *
2585 * Generates the TDM tx and rx slot default masks for DAI.
2586 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002587static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002588 unsigned int *tx_mask,
2589 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002590{
2591 if (*tx_mask || *rx_mask)
2592 return 0;
2593
2594 if (!slots)
2595 return -EINVAL;
2596
2597 *tx_mask = (1 << slots) - 1;
2598 *rx_mask = (1 << slots) - 1;
2599
2600 return 0;
2601}
2602
2603/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002604 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2605 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002606 * @tx_mask: bitmask representing active TX slots.
2607 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002608 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002609 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002610 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002611 * This function configures the specified DAI for TDM operation. @slot contains
2612 * the total number of slots of the TDM stream and @slot_with the width of each
2613 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2614 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2615 * DAI should write to or read from. If a bit is set the corresponding slot is
2616 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2617 * the first slot, bit 1 to the second slot and so on. The first active slot
2618 * maps to the first channel of the DAI, the second active slot to the second
2619 * channel and so on.
2620 *
2621 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2622 * @rx_mask and @slot_width will be ignored.
2623 *
2624 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002625 */
2626int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002627 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002628{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002629 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002630 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002631 &tx_mask, &rx_mask);
2632 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002633 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002634
Benoit Cousson88bd8702014-07-08 23:19:34 +02002635 dai->tx_mask = tx_mask;
2636 dai->rx_mask = rx_mask;
2637
Kuninori Morimoto46471922017-09-25 01:38:34 +00002638 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002639 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002640 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002641 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002642 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002643}
2644EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2645
2646/**
Barry Song472df3c2009-09-12 01:16:29 +08002647 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2648 * @dai: DAI
2649 * @tx_num: how many TX channels
2650 * @tx_slot: pointer to an array which imply the TX slot number channel
2651 * 0~num-1 uses
2652 * @rx_num: how many RX channels
2653 * @rx_slot: pointer to an array which imply the RX slot number channel
2654 * 0~num-1 uses
2655 *
2656 * configure the relationship between channel number and TDM slot number.
2657 */
2658int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2659 unsigned int tx_num, unsigned int *tx_slot,
2660 unsigned int rx_num, unsigned int *rx_slot)
2661{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002662 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002663 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002664 rx_num, rx_slot);
2665 else
2666 return -EINVAL;
2667}
2668EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2669
2670/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002671 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2672 * @dai: DAI
2673 * @tx_num: how many TX channels
2674 * @tx_slot: pointer to an array which imply the TX slot number channel
2675 * 0~num-1 uses
2676 * @rx_num: how many RX channels
2677 * @rx_slot: pointer to an array which imply the RX slot number channel
2678 * 0~num-1 uses
2679 */
2680int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2681 unsigned int *tx_num, unsigned int *tx_slot,
2682 unsigned int *rx_num, unsigned int *rx_slot)
2683{
2684 if (dai->driver->ops->get_channel_map)
2685 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2686 rx_num, rx_slot);
2687 else
2688 return -ENOTSUPP;
2689}
2690EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2691
2692/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002693 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2694 * @dai: DAI
2695 * @tristate: tristate enable
2696 *
2697 * Tristates the DAI so that others can use it.
2698 */
2699int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2700{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002701 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002702 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002703 else
2704 return -EINVAL;
2705}
2706EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2707
2708/**
2709 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2710 * @dai: DAI
2711 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002712 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002713 *
2714 * Mutes the DAI DAC.
2715 */
Mark Brownda183962013-02-06 15:44:07 +00002716int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2717 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002718{
Mark Brownda183962013-02-06 15:44:07 +00002719 if (dai->driver->ops->mute_stream)
2720 return dai->driver->ops->mute_stream(dai, mute, direction);
2721 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2722 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002723 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002724 else
Mark Brown04570c62012-04-13 19:16:03 +01002725 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726}
2727EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2728
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002729static int snd_soc_bind_card(struct snd_soc_card *card)
2730{
2731 struct snd_soc_pcm_runtime *rtd;
2732 int ret;
2733
2734 ret = snd_soc_instantiate_card(card);
2735 if (ret != 0)
2736 return ret;
2737
2738 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002739 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002740 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2741 struct snd_soc_dai *codec_dai;
2742 int j;
2743
2744 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2745 if (!codec_dai->active)
2746 pinctrl_pm_select_sleep_state(codec_dai->dev);
2747 }
2748
2749 if (!cpu_dai->active)
2750 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2751 }
2752
2753 return ret;
2754}
2755
Mark Brownc5af3a22008-11-28 13:29:45 +00002756/**
2757 * snd_soc_register_card - Register a card with the ASoC core
2758 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002759 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002760 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002761 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302762int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002763{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002764 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002765 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002766
Mark Brownc5af3a22008-11-28 13:29:45 +00002767 if (!card->name || !card->dev)
2768 return -EINVAL;
2769
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302770 mutex_lock(&client_mutex);
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002771 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002772
Mengdong Lin923c5e612015-11-23 11:01:49 -05002773 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002774 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002775 dev_err(card->dev, "ASoC: failed to init link %s\n",
2776 link->name);
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302777 mutex_unlock(&client_mutex);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002778 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002779 }
Stephen Warren5a504962011-12-21 10:40:59 -07002780 }
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302781 mutex_unlock(&client_mutex);
Stephen Warren5a504962011-12-21 10:40:59 -07002782
Mark Browned77cc12011-05-03 18:25:34 +01002783 dev_set_drvdata(card->dev, card);
2784
Stephen Warren111c6412011-01-28 14:26:35 -07002785 snd_soc_initialize_card_lists(card);
2786
Mengdong Linf8f80362015-12-02 14:11:22 +08002787 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002788
Mengdong Lin1a497982015-11-18 02:34:11 -05002789 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002790 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002791
Mark Browndb432b42011-10-03 21:06:40 +01002792 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002793 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002794 card->instantiated = 0;
2795 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002796 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002797
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002798 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002799}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302800EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002801
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002802static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2803{
2804 if (card->instantiated) {
2805 card->instantiated = false;
2806 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002807 snd_soc_flush_all_delayed_work(card);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002808 soc_cleanup_card_resources(card);
2809 if (!unregister)
2810 list_add(&card->list, &unbind_card_list);
2811 } else {
2812 if (unregister)
2813 list_del(&card->list);
2814 }
2815}
2816
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002817/**
2818 * snd_soc_unregister_card - Unregister a card with the ASoC core
2819 *
2820 * @card: Card to unregister
2821 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002822 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302823int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002824{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002825 snd_soc_unbind_card(card, true);
2826 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002827
2828 return 0;
2829}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302830EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002831
2832/*
2833 * Simplify DAI link configuration by removing ".-1" from device names
2834 * and sanitizing names.
2835 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002836static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002837{
2838 char *found, name[NAME_SIZE];
2839 int id1, id2;
2840
2841 if (dev_name(dev) == NULL)
2842 return NULL;
2843
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002844 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002845
2846 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002847 found = strstr(name, dev->driver->name);
2848 if (found) {
2849 /* get ID */
2850 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2851
2852 /* discard ID from name if ID == -1 */
2853 if (*id == -1)
2854 found[strlen(dev->driver->name)] = '\0';
2855 }
2856
2857 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002858 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002859 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2860 char tmp[NAME_SIZE];
2861
2862 /* create unique ID number from I2C addr and bus */
2863 *id = ((id1 & 0xffff) << 16) + id2;
2864
2865 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002866 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2867 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002868 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002869 } else
2870 *id = 0;
2871 }
2872
2873 return kstrdup(name, GFP_KERNEL);
2874}
2875
2876/*
2877 * Simplify DAI link naming for single devices with multiple DAIs by removing
2878 * any ".-1" and using the DAI name (instead of device name).
2879 */
2880static inline char *fmt_multiple_name(struct device *dev,
2881 struct snd_soc_dai_driver *dai_drv)
2882{
2883 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002884 dev_err(dev,
2885 "ASoC: error - multiple DAI %s registered with no name\n",
2886 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002887 return NULL;
2888 }
2889
2890 return kstrdup(dai_drv->name, GFP_KERNEL);
2891}
2892
2893/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002894 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002895 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002896 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002897 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002898static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002899{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002900 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002901
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002902 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002903 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2904 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002905 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002906 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002907 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002908 }
Mark Brown91151712008-11-30 23:31:24 +00002909}
Mark Brown91151712008-11-30 23:31:24 +00002910
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002911/* Create a DAI and add it to the component's DAI list */
2912static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2913 struct snd_soc_dai_driver *dai_drv,
2914 bool legacy_dai_naming)
2915{
2916 struct device *dev = component->dev;
2917 struct snd_soc_dai *dai;
2918
2919 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2920
2921 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2922 if (dai == NULL)
2923 return NULL;
2924
2925 /*
2926 * Back in the old days when we still had component-less DAIs,
2927 * instead of having a static name, component-less DAIs would
2928 * inherit the name of the parent device so it is possible to
2929 * register multiple instances of the DAI. We still need to keep
2930 * the same naming style even though those DAIs are not
2931 * component-less anymore.
2932 */
2933 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002934 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002935 dai->name = fmt_single_name(dev, &dai->id);
2936 } else {
2937 dai->name = fmt_multiple_name(dev, dai_drv);
2938 if (dai_drv->id)
2939 dai->id = dai_drv->id;
2940 else
2941 dai->id = component->num_dai;
2942 }
2943 if (dai->name == NULL) {
2944 kfree(dai);
2945 return NULL;
2946 }
2947
2948 dai->component = component;
2949 dai->dev = dev;
2950 dai->driver = dai_drv;
2951 if (!dai->driver->ops)
2952 dai->driver->ops = &null_dai_ops;
2953
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002954 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002955 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002956 component->num_dai++;
2957
2958 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2959 return dai;
2960}
2961
Mark Brown91151712008-11-30 23:31:24 +00002962/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002963 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002964 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002965 * @component: The component the DAIs are registered for
2966 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002967 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002968 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002969static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002970 struct snd_soc_dai_driver *dai_drv,
2971 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002972{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002973 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002974 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002975 unsigned int i;
2976 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002977
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002978 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002979
2980 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002981
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002982 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2983 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002984 if (dai == NULL) {
2985 ret = -ENOMEM;
2986 goto err;
2987 }
Mark Brown91151712008-11-30 23:31:24 +00002988 }
2989
2990 return 0;
2991
2992err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002993 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002994
2995 return ret;
2996}
Mark Brown91151712008-11-30 23:31:24 +00002997
Mengdong Lin68003e62015-12-31 16:40:43 +08002998/**
2999 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3000 *
3001 * @component: The component the DAIs are registered for
3002 * @dai_drv: DAI driver to use for the DAI
3003 *
3004 * Topology can use this API to register DAIs when probing a component.
3005 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3006 * will be freed in the component cleanup.
3007 */
3008int snd_soc_register_dai(struct snd_soc_component *component,
3009 struct snd_soc_dai_driver *dai_drv)
3010{
3011 struct snd_soc_dapm_context *dapm =
3012 snd_soc_component_get_dapm(component);
3013 struct snd_soc_dai *dai;
3014 int ret;
3015
3016 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3017 dev_err(component->dev, "Invalid dai type %d\n",
3018 dai_drv->dobj.type);
3019 return -EINVAL;
3020 }
3021
3022 lockdep_assert_held(&client_mutex);
3023 dai = soc_add_dai(component, dai_drv, false);
3024 if (!dai)
3025 return -ENOMEM;
3026
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003027 /*
3028 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08003029 * also add routes that need these widgets as source or sink.
3030 */
3031 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3032 if (ret != 0) {
3033 dev_err(component->dev,
3034 "Failed to create DAI widgets %d\n", ret);
3035 }
3036
3037 return ret;
3038}
3039EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3040
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003041static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3042 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003043{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003044 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003045
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003046 component->driver->seq_notifier(component, type, subseq);
3047}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003048
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003049static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3050 int event)
3051{
3052 struct snd_soc_component *component = dapm->component;
3053
3054 return component->driver->stream_event(component, event);
3055}
3056
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003057static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3058 enum snd_soc_bias_level level)
3059{
3060 struct snd_soc_component *component = dapm->component;
3061
3062 return component->driver->set_bias_level(component, level);
3063}
3064
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003065static int snd_soc_component_initialize(struct snd_soc_component *component,
3066 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003067{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003068 struct snd_soc_dapm_context *dapm;
3069
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003070 component->name = fmt_single_name(dev, &component->id);
3071 if (!component->name) {
3072 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003073 return -ENOMEM;
3074 }
3075
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003076 component->dev = dev;
3077 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003078
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003079 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003080 dapm->dev = dev;
3081 dapm->component = component;
3082 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003083 dapm->idle_bias_off = !driver->idle_bias_on;
3084 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003085 if (driver->seq_notifier)
3086 dapm->seq_notifier = snd_soc_component_seq_notifier;
3087 if (driver->stream_event)
3088 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003089 if (driver->set_bias_level)
3090 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003091
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003092 INIT_LIST_HEAD(&component->dai_list);
3093 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003094
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003095 return 0;
3096}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003097
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003098static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003099{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003100 int val_bytes = regmap_get_val_bytes(component->regmap);
3101
3102 /* Errors are legitimate for non-integer byte multiples */
3103 if (val_bytes > 0)
3104 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003105}
3106
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003107#ifdef CONFIG_REGMAP
3108
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003109/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003110 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3111 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003112 * @component: The component for which to initialize the regmap instance
3113 * @regmap: The regmap instance that should be used by the component
3114 *
3115 * This function allows deferred assignment of the regmap instance that is
3116 * associated with the component. Only use this if the regmap instance is not
3117 * yet ready when the component is registered. The function must also be called
3118 * before the first IO attempt of the component.
3119 */
3120void snd_soc_component_init_regmap(struct snd_soc_component *component,
3121 struct regmap *regmap)
3122{
3123 component->regmap = regmap;
3124 snd_soc_component_setup_regmap(component);
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3127
3128/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003129 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3130 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003131 * @component: The component for which to de-initialize the regmap instance
3132 *
3133 * Calls regmap_exit() on the regmap instance associated to the component and
3134 * removes the regmap instance from the component.
3135 *
3136 * This function should only be used if snd_soc_component_init_regmap() was used
3137 * to initialize the regmap instance.
3138 */
3139void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3140{
3141 regmap_exit(component->regmap);
3142 component->regmap = NULL;
3143}
3144EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3145
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003146#endif
3147
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003148static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003149{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003150 mutex_lock(&client_mutex);
3151
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003152 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003153 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003154 component->regmap = dev_get_regmap(component->dev,
3155 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003156 if (component->regmap)
3157 snd_soc_component_setup_regmap(component);
3158 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003159
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003160 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003161 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003162 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003163
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003164 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003165}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003166
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003167static void snd_soc_component_cleanup(struct snd_soc_component *component)
3168{
3169 snd_soc_unregister_dais(component);
3170 kfree(component->name);
3171}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003172
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003173static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3174{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003175 struct snd_soc_card *card = component->card;
3176
3177 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003178 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003179
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003180 list_del(&component->list);
3181}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003182
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003183#define ENDIANNESS_MAP(name) \
3184 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3185static u64 endianness_format_map[] = {
3186 ENDIANNESS_MAP(S16_),
3187 ENDIANNESS_MAP(U16_),
3188 ENDIANNESS_MAP(S24_),
3189 ENDIANNESS_MAP(U24_),
3190 ENDIANNESS_MAP(S32_),
3191 ENDIANNESS_MAP(U32_),
3192 ENDIANNESS_MAP(S24_3),
3193 ENDIANNESS_MAP(U24_3),
3194 ENDIANNESS_MAP(S20_3),
3195 ENDIANNESS_MAP(U20_3),
3196 ENDIANNESS_MAP(S18_3),
3197 ENDIANNESS_MAP(U18_3),
3198 ENDIANNESS_MAP(FLOAT_),
3199 ENDIANNESS_MAP(FLOAT64_),
3200 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3201};
3202
3203/*
3204 * Fix up the DAI formats for endianness: codecs don't actually see
3205 * the endianness of the data but we're using the CPU format
3206 * definitions which do need to include endianness so we ensure that
3207 * codec DAIs always have both big and little endian variants set.
3208 */
3209static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3210{
3211 int i;
3212
3213 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3214 if (stream->formats & endianness_format_map[i])
3215 stream->formats |= endianness_format_map[i];
3216}
3217
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003218static void snd_soc_try_rebind_card(void)
3219{
3220 struct snd_soc_card *card, *c;
3221
3222 if (!list_empty(&unbind_card_list)) {
3223 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3224 if (!snd_soc_bind_card(card))
3225 list_del(&card->list);
3226 }
3227 }
3228}
3229
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003230int snd_soc_add_component(struct device *dev,
3231 struct snd_soc_component *component,
3232 const struct snd_soc_component_driver *component_driver,
3233 struct snd_soc_dai_driver *dai_drv,
3234 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003235{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003236 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003237 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003238
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003239 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003240 if (ret)
3241 goto err_free;
3242
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003243 if (component_driver->endianness) {
3244 for (i = 0; i < num_dai; i++) {
3245 convert_endianness_formats(&dai_drv[i].playback);
3246 convert_endianness_formats(&dai_drv[i].capture);
3247 }
3248 }
3249
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003250 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003251 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003252 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003253 goto err_cleanup;
3254 }
3255
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003256 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003257 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003258
3259 return 0;
3260
3261err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003262 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003263err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003264 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003265}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003266EXPORT_SYMBOL_GPL(snd_soc_add_component);
3267
3268int snd_soc_register_component(struct device *dev,
3269 const struct snd_soc_component_driver *component_driver,
3270 struct snd_soc_dai_driver *dai_drv,
3271 int num_dai)
3272{
3273 struct snd_soc_component *component;
3274
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003275 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003276 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003277 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003278
3279 return snd_soc_add_component(dev, component, component_driver,
3280 dai_drv, num_dai);
3281}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003282EXPORT_SYMBOL_GPL(snd_soc_register_component);
3283
3284/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003285 * snd_soc_unregister_component - Unregister all related component
3286 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003287 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003288 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003289 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003290static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003291{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003292 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003293 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003294
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003295 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003296 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003297 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003298 continue;
3299
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003300 snd_soc_tplg_component_remove(component,
3301 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003302 snd_soc_component_del_unlocked(component);
3303 found = 1;
3304 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003305 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003306 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003307
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003308 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003309 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003310
3311 return found;
3312}
3313
3314void snd_soc_unregister_component(struct device *dev)
3315{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003316 while (__snd_soc_unregister_component(dev))
3317 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003318}
3319EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3320
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003321struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3322 const char *driver_name)
3323{
3324 struct snd_soc_component *component;
3325 struct snd_soc_component *ret;
3326
3327 ret = NULL;
3328 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003329 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003330 if (dev != component->dev)
3331 continue;
3332
3333 if (driver_name &&
3334 (driver_name != component->driver->name) &&
3335 (strcmp(component->driver->name, driver_name) != 0))
3336 continue;
3337
3338 ret = component;
3339 break;
3340 }
3341 mutex_unlock(&client_mutex);
3342
3343 return ret;
3344}
3345EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3346
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003347/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003348int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3349 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003350{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003351 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003352 int ret;
3353
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303354 if (!card->dev) {
3355 pr_err("card->dev is not set before calling %s\n", __func__);
3356 return -EINVAL;
3357 }
3358
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003359 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303360
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003361 ret = of_property_read_string_index(np, propname, 0, &card->name);
3362 /*
3363 * EINVAL means the property does not exist. This is fine providing
3364 * card->name was previously set, which is checked later in
3365 * snd_soc_register_card.
3366 */
3367 if (ret < 0 && ret != -EINVAL) {
3368 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003369 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003370 propname, ret);
3371 return ret;
3372 }
3373
3374 return 0;
3375}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003376EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003377
Xiubo Li9a6d4862014-02-08 15:59:52 +08003378static const struct snd_soc_dapm_widget simple_widgets[] = {
3379 SND_SOC_DAPM_MIC("Microphone", NULL),
3380 SND_SOC_DAPM_LINE("Line", NULL),
3381 SND_SOC_DAPM_HP("Headphone", NULL),
3382 SND_SOC_DAPM_SPK("Speaker", NULL),
3383};
3384
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003385int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003386 const char *propname)
3387{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003388 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003389 struct snd_soc_dapm_widget *widgets;
3390 const char *template, *wname;
3391 int i, j, num_widgets, ret;
3392
3393 num_widgets = of_property_count_strings(np, propname);
3394 if (num_widgets < 0) {
3395 dev_err(card->dev,
3396 "ASoC: Property '%s' does not exist\n", propname);
3397 return -EINVAL;
3398 }
3399 if (num_widgets & 1) {
3400 dev_err(card->dev,
3401 "ASoC: Property '%s' length is not even\n", propname);
3402 return -EINVAL;
3403 }
3404
3405 num_widgets /= 2;
3406 if (!num_widgets) {
3407 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3408 propname);
3409 return -EINVAL;
3410 }
3411
3412 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3413 GFP_KERNEL);
3414 if (!widgets) {
3415 dev_err(card->dev,
3416 "ASoC: Could not allocate memory for widgets\n");
3417 return -ENOMEM;
3418 }
3419
3420 for (i = 0; i < num_widgets; i++) {
3421 ret = of_property_read_string_index(np, propname,
3422 2 * i, &template);
3423 if (ret) {
3424 dev_err(card->dev,
3425 "ASoC: Property '%s' index %d read error:%d\n",
3426 propname, 2 * i, ret);
3427 return -EINVAL;
3428 }
3429
3430 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3431 if (!strncmp(template, simple_widgets[j].name,
3432 strlen(simple_widgets[j].name))) {
3433 widgets[i] = simple_widgets[j];
3434 break;
3435 }
3436 }
3437
3438 if (j >= ARRAY_SIZE(simple_widgets)) {
3439 dev_err(card->dev,
3440 "ASoC: DAPM widget '%s' is not supported\n",
3441 template);
3442 return -EINVAL;
3443 }
3444
3445 ret = of_property_read_string_index(np, propname,
3446 (2 * i) + 1,
3447 &wname);
3448 if (ret) {
3449 dev_err(card->dev,
3450 "ASoC: Property '%s' index %d read error:%d\n",
3451 propname, (2 * i) + 1, ret);
3452 return -EINVAL;
3453 }
3454
3455 widgets[i].name = wname;
3456 }
3457
Nicolin Chenf23e8602015-02-14 17:22:49 -08003458 card->of_dapm_widgets = widgets;
3459 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003460
3461 return 0;
3462}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003463EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003464
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003465int snd_soc_of_get_slot_mask(struct device_node *np,
3466 const char *prop_name,
3467 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003468{
3469 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003470 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003471 int i;
3472
3473 if (!of_slot_mask)
3474 return 0;
3475 val /= sizeof(u32);
3476 for (i = 0; i < val; i++)
3477 if (be32_to_cpup(&of_slot_mask[i]))
3478 *mask |= (1 << i);
3479
3480 return val;
3481}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003482EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003483
Xiubo Li89c67852014-02-14 09:34:35 +08003484int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003485 unsigned int *tx_mask,
3486 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003487 unsigned int *slots,
3488 unsigned int *slot_width)
3489{
3490 u32 val;
3491 int ret;
3492
Jyri Sarha61310842015-09-09 21:27:43 +03003493 if (tx_mask)
3494 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3495 if (rx_mask)
3496 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3497
Xiubo Li89c67852014-02-14 09:34:35 +08003498 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3499 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3500 if (ret)
3501 return ret;
3502
3503 if (slots)
3504 *slots = val;
3505 }
3506
3507 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3508 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3509 if (ret)
3510 return ret;
3511
3512 if (slot_width)
3513 *slot_width = val;
3514 }
3515
3516 return 0;
3517}
3518EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3519
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003520void snd_soc_of_parse_node_prefix(struct device_node *np,
3521 struct snd_soc_codec_conf *codec_conf,
3522 struct device_node *of_node,
3523 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003524{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003525 const char *str;
3526 int ret;
3527
3528 ret = of_property_read_string(np, propname, &str);
3529 if (ret < 0) {
3530 /* no prefix is not error */
3531 return;
3532 }
3533
3534 codec_conf->of_node = of_node;
3535 codec_conf->name_prefix = str;
3536}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003537EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003538
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003539int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003540 const char *propname)
3541{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003542 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003543 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003544 struct snd_soc_dapm_route *routes;
3545 int i, ret;
3546
3547 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003548 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003549 dev_err(card->dev,
3550 "ASoC: Property '%s' does not exist or its length is not even\n",
3551 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003552 return -EINVAL;
3553 }
3554 num_routes /= 2;
3555 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003556 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003557 propname);
3558 return -EINVAL;
3559 }
3560
Kees Cooka86854d2018-06-12 14:07:58 -07003561 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003562 GFP_KERNEL);
3563 if (!routes) {
3564 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003565 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003566 return -EINVAL;
3567 }
3568
3569 for (i = 0; i < num_routes; i++) {
3570 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003571 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003572 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003573 dev_err(card->dev,
3574 "ASoC: Property '%s' index %d could not be read: %d\n",
3575 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003576 return -EINVAL;
3577 }
3578 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003579 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003580 if (ret) {
3581 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003582 "ASoC: Property '%s' index %d could not be read: %d\n",
3583 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003584 return -EINVAL;
3585 }
3586 }
3587
Nicolin Chenf23e8602015-02-14 17:22:49 -08003588 card->num_of_dapm_routes = num_routes;
3589 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003590
3591 return 0;
3592}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003593EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003594
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003595unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003596 const char *prefix,
3597 struct device_node **bitclkmaster,
3598 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003599{
3600 int ret, i;
3601 char prop[128];
3602 unsigned int format = 0;
3603 int bit, frame;
3604 const char *str;
3605 struct {
3606 char *name;
3607 unsigned int val;
3608 } of_fmt_table[] = {
3609 { "i2s", SND_SOC_DAIFMT_I2S },
3610 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3611 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3612 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3613 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3614 { "ac97", SND_SOC_DAIFMT_AC97 },
3615 { "pdm", SND_SOC_DAIFMT_PDM},
3616 { "msb", SND_SOC_DAIFMT_MSB },
3617 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003618 };
3619
3620 if (!prefix)
3621 prefix = "";
3622
3623 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003624 * check "dai-format = xxx"
3625 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003626 * SND_SOC_DAIFMT_FORMAT_MASK area
3627 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003628 ret = of_property_read_string(np, "dai-format", &str);
3629 if (ret < 0) {
3630 snprintf(prop, sizeof(prop), "%sformat", prefix);
3631 ret = of_property_read_string(np, prop, &str);
3632 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003633 if (ret == 0) {
3634 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3635 if (strcmp(str, of_fmt_table[i].name) == 0) {
3636 format |= of_fmt_table[i].val;
3637 break;
3638 }
3639 }
3640 }
3641
3642 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003643 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003644 * SND_SOC_DAIFMT_CLOCK_MASK area
3645 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003646 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003647 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003648 format |= SND_SOC_DAIFMT_CONT;
3649 else
3650 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003651
3652 /*
3653 * check "[prefix]bitclock-inversion"
3654 * check "[prefix]frame-inversion"
3655 * SND_SOC_DAIFMT_INV_MASK area
3656 */
3657 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3658 bit = !!of_get_property(np, prop, NULL);
3659
3660 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3661 frame = !!of_get_property(np, prop, NULL);
3662
3663 switch ((bit << 4) + frame) {
3664 case 0x11:
3665 format |= SND_SOC_DAIFMT_IB_IF;
3666 break;
3667 case 0x10:
3668 format |= SND_SOC_DAIFMT_IB_NF;
3669 break;
3670 case 0x01:
3671 format |= SND_SOC_DAIFMT_NB_IF;
3672 break;
3673 default:
3674 /* SND_SOC_DAIFMT_NB_NF is default */
3675 break;
3676 }
3677
3678 /*
3679 * check "[prefix]bitclock-master"
3680 * check "[prefix]frame-master"
3681 * SND_SOC_DAIFMT_MASTER_MASK area
3682 */
3683 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3684 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003685 if (bit && bitclkmaster)
3686 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003687
3688 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3689 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003690 if (frame && framemaster)
3691 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003692
3693 switch ((bit << 4) + frame) {
3694 case 0x11:
3695 format |= SND_SOC_DAIFMT_CBM_CFM;
3696 break;
3697 case 0x10:
3698 format |= SND_SOC_DAIFMT_CBM_CFS;
3699 break;
3700 case 0x01:
3701 format |= SND_SOC_DAIFMT_CBS_CFM;
3702 break;
3703 default:
3704 format |= SND_SOC_DAIFMT_CBS_CFS;
3705 break;
3706 }
3707
3708 return format;
3709}
3710EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3711
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003712int snd_soc_get_dai_id(struct device_node *ep)
3713{
3714 struct snd_soc_component *pos;
3715 struct device_node *node;
3716 int ret;
3717
3718 node = of_graph_get_port_parent(ep);
3719
3720 /*
3721 * For example HDMI case, HDMI has video/sound port,
3722 * but ALSA SoC needs sound port number only.
3723 * Thus counting HDMI DT port/endpoint doesn't work.
3724 * Then, it should have .of_xlate_dai_id
3725 */
3726 ret = -ENOTSUPP;
3727 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003728 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003729 struct device_node *component_of_node = pos->dev->of_node;
3730
3731 if (!component_of_node && pos->dev->parent)
3732 component_of_node = pos->dev->parent->of_node;
3733
3734 if (component_of_node != node)
3735 continue;
3736
3737 if (pos->driver->of_xlate_dai_id)
3738 ret = pos->driver->of_xlate_dai_id(pos, ep);
3739
3740 break;
3741 }
3742 mutex_unlock(&client_mutex);
3743
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003744 of_node_put(node);
3745
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003746 return ret;
3747}
3748EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3749
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003750int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003751 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003752{
3753 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003754 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003755 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003756
3757 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003758 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003759 component_of_node = pos->dev->of_node;
3760 if (!component_of_node && pos->dev->parent)
3761 component_of_node = pos->dev->parent->of_node;
3762
3763 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003764 continue;
3765
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003766 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003767 ret = pos->driver->of_xlate_dai_name(pos,
3768 args,
3769 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003770 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003771 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003772 int id = -1;
3773
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003774 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003775 case 0:
3776 id = 0; /* same as dai_drv[0] */
3777 break;
3778 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003779 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003780 break;
3781 default:
3782 /* not supported */
3783 break;
3784 }
3785
3786 if (id < 0 || id >= pos->num_dai) {
3787 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003788 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003789 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003790
3791 ret = 0;
3792
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003793 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003794 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003795 if (id == 0)
3796 break;
3797 id--;
3798 }
3799
3800 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003801 if (!*dai_name)
3802 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003803 }
3804
Kuninori Morimotocb470082013-09-10 17:39:56 -07003805 break;
3806 }
3807 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003808 return ret;
3809}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003810EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003811
3812int snd_soc_of_get_dai_name(struct device_node *of_node,
3813 const char **dai_name)
3814{
3815 struct of_phandle_args args;
3816 int ret;
3817
3818 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3819 "#sound-dai-cells", 0, &args);
3820 if (ret)
3821 return ret;
3822
3823 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003824
3825 of_node_put(args.np);
3826
3827 return ret;
3828}
3829EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3830
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003831/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003832 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3833 * @dai_link: DAI link
3834 *
3835 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3836 */
3837void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3838{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003839 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003840 int index;
3841
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003842 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003843 if (!component->of_node)
3844 break;
3845 of_node_put(component->of_node);
3846 component->of_node = NULL;
3847 }
3848}
3849EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3850
3851/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003852 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3853 * @dev: Card device
3854 * @of_node: Device node
3855 * @dai_link: DAI link
3856 *
3857 * Builds an array of CODEC DAI components from the DAI link property
3858 * 'sound-dai'.
3859 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003860 * The device nodes in the array (of_node) must be dereferenced by calling
3861 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003862 *
3863 * Returns 0 for success
3864 */
3865int snd_soc_of_get_dai_link_codecs(struct device *dev,
3866 struct device_node *of_node,
3867 struct snd_soc_dai_link *dai_link)
3868{
3869 struct of_phandle_args args;
3870 struct snd_soc_dai_link_component *component;
3871 char *name;
3872 int index, num_codecs, ret;
3873
3874 /* Count the number of CODECs */
3875 name = "sound-dai";
3876 num_codecs = of_count_phandle_with_args(of_node, name,
3877 "#sound-dai-cells");
3878 if (num_codecs <= 0) {
3879 if (num_codecs == -ENOENT)
3880 dev_err(dev, "No 'sound-dai' property\n");
3881 else
3882 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3883 return num_codecs;
3884 }
Kees Cooka86854d2018-06-12 14:07:58 -07003885 component = devm_kcalloc(dev,
3886 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003887 GFP_KERNEL);
3888 if (!component)
3889 return -ENOMEM;
3890 dai_link->codecs = component;
3891 dai_link->num_codecs = num_codecs;
3892
3893 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003894 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003895 ret = of_parse_phandle_with_args(of_node, name,
3896 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003897 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003898 if (ret)
3899 goto err;
3900 component->of_node = args.np;
3901 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3902 if (ret < 0)
3903 goto err;
3904 }
3905 return 0;
3906err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003907 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003908 dai_link->codecs = NULL;
3909 dai_link->num_codecs = 0;
3910 return ret;
3911}
3912EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3913
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003914static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003915{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003916 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003917 snd_soc_util_init();
3918
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003919 return platform_driver_register(&soc_driver);
3920}
Mark Brown4abe8e12010-10-12 17:41:03 +01003921module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003922
Mark Brown7d8c16a2008-11-30 22:11:24 +00003923static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003924{
Mark Brownfb257892011-04-28 10:57:54 +01003925 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003926 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003927
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003928 platform_driver_unregister(&soc_driver);
3929}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003930module_exit(snd_soc_exit);
3931
3932/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003933MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003934MODULE_DESCRIPTION("ALSA SoC Core");
3935MODULE_LICENSE("GPL");
3936MODULE_ALIAS("platform:soc-audio");