blob: 6ddcf12bc030dbb6a8a582fae810c8ba4ccf5bd3 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020069/*
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080072 * as invalid and dropped when setting the card long name from DMI info.
73 */
74static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
76 "TBD by OEM",
77 "Default String",
78 "Board Manufacturer",
79 "Board Vendor Name",
80 "Board Product Name",
81 NULL, /* terminator */
82};
83
Mark Browndbe21402010-02-12 11:37:24 +000084static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
Mark Brown36ae1a92012-01-06 17:12:45 -080087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000090}
91
92static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070097 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000098
Jingoo Hanb785a492013-07-19 16:24:59 +090099 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700100 if (ret)
101 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000102
103 return count;
104}
105
106static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 &dev_attr_pmdown_time.attr,
110 NULL
111};
112
113static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
115{
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100122}
123
124static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
Mark Brownf7e73b262018-03-09 12:46:27 +0000129static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
132};
133
134static const struct attribute_group *soc_dev_attr_groups[] = {
135 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000136 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 NULL
138};
139
Mark Brown2624d5f2009-11-03 21:56:13 +0000140#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200143 if (!component->card->debugfs_card_root)
144 return;
145
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200146 if (component->debugfs_prefix) {
147 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100148
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
151 if (name) {
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
154 kfree(name);
155 }
156 } else {
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
159 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 return;
165 }
166
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169}
170
171static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
172{
173 debugfs_remove_recursive(component->debugfs_root);
174}
175
Peng Donglinc15b2a12018-02-14 22:48:07 +0800176static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100177{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100178 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100179 struct snd_soc_dai *dai;
180
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100181 mutex_lock(&client_mutex);
182
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000183 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000184 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800185 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100186
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100187 mutex_unlock(&client_mutex);
188
Donglin Peng700c17c2018-01-18 13:31:26 +0800189 return 0;
190}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800191DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100192
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000193static int component_list_show(struct seq_file *m, void *v)
194{
195 struct snd_soc_component *component;
196
197 mutex_lock(&client_mutex);
198
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000199 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200 seq_printf(m, "%s\n", component->name);
201
202 mutex_unlock(&client_mutex);
203
204 return 0;
205}
206DEFINE_SHOW_ATTRIBUTE(component_list);
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208static void soc_init_card_debugfs(struct snd_soc_card *card)
209{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200210 if (!snd_soc_debugfs_root)
211 return;
212
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000214 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200215 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100217 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200218 return;
219 }
220
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
223 &card->pop_time);
224 if (!card->debugfs_pop_time)
225 dev_warn(card->dev,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200226 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200227}
228
229static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230{
231 debugfs_remove_recursive(card->debugfs_card_root);
232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
240 return;
241 }
242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
244 &dai_list_fops))
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000246
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250}
251
252static void snd_soc_debugfs_exit(void)
253{
254 debugfs_remove_recursive(snd_soc_debugfs_root);
255}
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257#else
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261{
262}
263
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200264static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000266{
267}
268
Axel Linb95fccb2010-11-09 17:06:44 +0800269static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270{
271}
272
273static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274{
275}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200276
277static inline void snd_soc_debugfs_init(void)
278{
279}
280
281static inline void snd_soc_debugfs_exit(void)
282{
283}
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285#endif
286
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000287static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
289{
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
292
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
296 return 0;
297 }
298
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300 if (!new_rtdcom)
301 return -ENOMEM;
302
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
305
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
307
308 return 0;
309}
310
311static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312{
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316 kfree(rtdcom1);
317
318 INIT_LIST_HEAD(&rtd->component_list);
319}
320
321struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
323{
324 struct snd_soc_rtdcom_list *rtdcom;
325
Kuninori Morimoto971da242018-01-23 00:41:24 +0000326 if (!driver_name)
327 return NULL;
328
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000329 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000330 const char *component_name = rtdcom->component->driver->name;
331
332 if (!component_name)
333 continue;
334
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000337 return rtdcom->component;
338 }
339
340 return NULL;
341}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000342EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100344struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
346{
Mengdong Lin1a497982015-11-18 02:34:11 -0500347 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000349 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100353 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100355 return NULL;
356}
357EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
358
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000359static const struct snd_soc_ops null_snd_soc_ops;
360
Mengdong Lin1a497982015-11-18 02:34:11 -0500361static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
363{
364 struct snd_soc_pcm_runtime *rtd;
365
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
367 if (!rtd)
368 return NULL;
369
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000370 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500371 rtd->card = card;
372 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
375
Kees Cook6396bb22018-06-12 14:03:40 -0700376 rtd->codec_dais = kcalloc(dai_link->num_codecs,
377 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500378 GFP_KERNEL);
379 if (!rtd->codec_dais) {
380 kfree(rtd);
381 return NULL;
382 }
383
384 return rtd;
385}
386
387static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
388{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000389 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000390 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500391 kfree(rtd);
392}
393
394static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
396{
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
399 card->num_rtd++;
400}
401
402static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
403{
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
405
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000406 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
409 }
410
411 card->num_rtd = 0;
412}
413
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100414struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
416{
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100418
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000419 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500420 if (!strcmp(rtd->dai_link->name, dai_link))
421 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100422 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424 return NULL;
425}
426EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
427
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100428static void codec2codec_close_delayed_work(struct work_struct *work)
429{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200430 /*
431 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100432 * Since c2c links are internal nodes in the DAPM graph and
433 * don't interface with the outside world or application layer
434 * we don't have to do any special handling on close.
435 */
436}
437
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000438#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000440int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200441{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000442 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000443 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500444 struct snd_soc_pcm_runtime *rtd;
445 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200447 /* If the card is not initialized yet there is nothing to do */
448 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200449 return 0;
450
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200451 /*
452 * Due to the resume being scheduled into a workqueue we could
453 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100454 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100456
457 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100459
Mark Browna00f90f2010-12-02 16:24:24 +0000460 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000461 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000462 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100463
Mengdong Lin1a497982015-11-18 02:34:11 -0500464 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100465 continue;
466
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000467 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200468 struct snd_soc_dai_driver *drv = dai->driver;
469
470 if (drv->ops->digital_mute && dai->playback_active)
471 drv->ops->digital_mute(dai, 1);
472 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 }
474
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100475 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000476 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500477 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100478 continue;
479
Mengdong Lin1a497982015-11-18 02:34:11 -0500480 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100481 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100482
Mark Brown87506542008-11-18 20:50:34 +0000483 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000484 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000486 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100488
Mengdong Lin1a497982015-11-18 02:34:11 -0500489 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100490 continue;
491
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100492 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100494 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200496 /* close any waiting streams */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000497 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100499
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000500 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501
Mengdong Lin1a497982015-11-18 02:34:11 -0500502 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100503 continue;
504
Mengdong Lin1a497982015-11-18 02:34:11 -0500505 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800506 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800507 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800510 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800511 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 }
513
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200514 /* Recheck all endpoints too, their state is affected by suspend */
515 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700516 snd_soc_dapm_sync(&card->dapm);
517
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000518 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000519 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200520 struct snd_soc_dapm_context *dapm =
521 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000522
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200523 /*
524 * If there are paths active then the COMPONENT will be held
525 * with bias _ON and should not be suspended.
526 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000527 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200528 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000530 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000531 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000532 * bias off then being in STANDBY
533 * means it's doing something,
534 * otherwise fall through.
535 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200536 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000537 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200538 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000539 break;
540 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500541 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200542
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000544 if (component->driver->suspend)
545 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000546 component->suspended = 1;
547 if (component->regmap)
548 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800549 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000550 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 break;
552 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000553 dev_dbg(component->dev,
554 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 break;
556 }
557 }
558 }
559
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000560 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500561 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562
Mengdong Lin1a497982015-11-18 02:34:11 -0500563 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 continue;
565
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100566 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800568
569 /* deactivate pins to sleep state */
570 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571 }
572
Mark Brown87506542008-11-18 20:50:34 +0000573 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000574 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
576 return 0;
577}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000578EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200580/*
581 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100582 * setting our codec back up, which can be very slow on I2C
583 */
584static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200587 container_of(work, struct snd_soc_card,
588 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500589 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000590 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500591 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200593 /*
594 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100595 * so userspace apps are blocked from touching us
596 */
597
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000598 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100599
Mark Brown9949788b2010-05-07 20:24:05 +0100600 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100602
Mark Brown87506542008-11-18 20:50:34 +0000603 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000604 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100606 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000607 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500608 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100609
Mengdong Lin1a497982015-11-18 02:34:11 -0500610 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100611 continue;
612
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100613 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 }
616
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000617 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000618 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000619 if (component->driver->resume)
620 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000621 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100622 }
623 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000625 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100626
Mengdong Lin1a497982015-11-18 02:34:11 -0500627 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100628 continue;
629
Mengdong Lin1a497982015-11-18 02:34:11 -0500630 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000631 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800632 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633
Mengdong Lin1a497982015-11-18 02:34:11 -0500634 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000635 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800636 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 }
638
Mark Brown3ff3f642008-05-19 12:32:25 +0200639 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000640 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000641 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100642
Mengdong Lin1a497982015-11-18 02:34:11 -0500643 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100644 continue;
645
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000646 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200647 struct snd_soc_dai_driver *drv = dai->driver;
648
649 if (drv->ops->digital_mute && dai->playback_active)
650 drv->ops->digital_mute(dai, 0);
651 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 }
653
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000654 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100656
Mengdong Lin1a497982015-11-18 02:34:11 -0500657 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100658 continue;
659
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100660 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Mark Brown87506542008-11-18 20:50:34 +0000664 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000665 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000667 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100668
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200669 /* Recheck all endpoints too, their state is affected by suspend */
670 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700671 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530672
673 /* userspace can access us now we are back as we were before */
674 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100675}
676
677/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000678int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100679{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000680 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100681 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500682 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200683
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200684 /* If the card is not initialized yet there is nothing to do */
685 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800686 return 0;
687
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800688 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000689 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000690 struct snd_soc_dai *codec_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 int j;
693
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800694 if (cpu_dai->active)
695 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200696
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000697 for_each_rtd_codec_dai(rtd, j, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200698 if (codec_dai->active)
699 pinctrl_pm_select_default_state(codec_dai->dev);
700 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800701 }
702
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100703 /*
704 * DAIs that also act as the control bus master might have other drivers
705 * hanging off them so need to resume immediately. Other drivers don't
706 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100707 * due to I/O costs and anti-pop so handle them out of line.
708 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000709 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200711
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100712 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600713 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100714 if (bus_control) {
715 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600716 soc_resume_deferred(&card->deferred_resume_work);
717 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000718 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600719 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000720 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100721 }
Andy Green6ed25972008-06-13 16:24:05 +0100722
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 return 0;
724}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000725EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000727#define snd_soc_suspend NULL
728#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729#endif
730
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100731static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800732};
733
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200734static struct snd_soc_component *soc_find_component(
735 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100736{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200737 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100738
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100739 lockdep_assert_held(&client_mutex);
740
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000741 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200742 if (of_node) {
743 if (component->dev->of_node == of_node)
744 return component;
745 } else if (strcmp(component->name, name) == 0) {
746 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100747 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100748 }
749
750 return NULL;
751}
752
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000753static int snd_soc_is_matching_component(
754 const struct snd_soc_dai_link_component *dlc,
755 struct snd_soc_component *component)
756{
757 struct device_node *component_of_node;
758
759 component_of_node = component->dev->of_node;
760 if (!component_of_node && component->dev->parent)
761 component_of_node = component->dev->parent->of_node;
762
763 if (dlc->of_node && component_of_node != dlc->of_node)
764 return 0;
765 if (dlc->name && strcmp(component->name, dlc->name))
766 return 0;
767
768 return 1;
769}
770
Mengdong Linfbb88b52016-04-22 12:25:33 +0800771/**
772 * snd_soc_find_dai - Find a registered DAI
773 *
Jeffy Chen49584712017-08-22 15:57:21 +0800774 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800775 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700776 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800777 * find the DAI of the same name. The component's of_node and name
778 * should also match if being specified.
779 *
780 * Return: pointer of DAI, or NULL if not found.
781 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800782struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200783 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100784{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200785 struct snd_soc_component *component;
786 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100787
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100788 lockdep_assert_held(&client_mutex);
789
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200790 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000791 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000792 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200793 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000794 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800795 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800796 && (!dai->driver->name
797 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100798 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100799
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200800 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100801 }
802 }
803
804 return NULL;
805}
Mengdong Lin305e9022016-04-19 13:12:25 +0800806EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100807
Mengdong Lin17fb17552016-11-03 01:04:12 +0800808/**
809 * snd_soc_find_dai_link - Find a DAI link
810 *
811 * @card: soc card
812 * @id: DAI link ID to match
813 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000814 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800815 *
816 * This function will search all existing DAI links of the soc card to
817 * find the link of the same ID. Since DAI links may not have their
818 * unique ID, so name and stream name should also match if being
819 * specified.
820 *
821 * Return: pointer of DAI link, or NULL if not found.
822 */
823struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
824 int id, const char *name,
825 const char *stream_name)
826{
827 struct snd_soc_dai_link *link, *_link;
828
829 lockdep_assert_held(&client_mutex);
830
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000831 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800832 if (link->id != id)
833 continue;
834
835 if (name && (!link->name || strcmp(name, link->name)))
836 continue;
837
838 if (stream_name && (!link->stream_name
839 || strcmp(stream_name, link->stream_name)))
840 continue;
841
842 return link;
843 }
844
845 return NULL;
846}
847EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
848
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800849static bool soc_is_dai_link_bound(struct snd_soc_card *card,
850 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800852 struct snd_soc_pcm_runtime *rtd;
853
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000854 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800855 if (rtd->dai_link == dai_link)
856 return true;
857 }
858
859 return false;
860}
861
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500862static int soc_bind_dai_link(struct snd_soc_card *card,
863 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864{
Mengdong Lin1a497982015-11-18 02:34:11 -0500865 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200866 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200867 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000868 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500869 struct snd_soc_dai **codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200870 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871
Liam Girdwooda655de82018-07-02 16:59:54 +0100872 if (dai_link->ignore)
873 return 0;
874
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500875 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000876
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800877 if (soc_is_dai_link_bound(card, dai_link)) {
878 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
879 dai_link->name);
880 return 0;
881 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530883 rtd = soc_new_pcm_runtime(card, dai_link);
884 if (!rtd)
885 return -ENOMEM;
886
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200887 cpu_dai_component.name = dai_link->cpu_name;
888 cpu_dai_component.of_node = dai_link->cpu_of_node;
889 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
890 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000891 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100892 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
893 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500894 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000895 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000896 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000897
Benoit Cousson88bd8702014-07-08 23:19:34 +0200898 rtd->num_codecs = dai_link->num_codecs;
899
900 /* Find CODEC from registered CODECs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000901 /* we can use for_each_rtd_codec_dai() after this */
Mengdong Lin1a497982015-11-18 02:34:11 -0500902 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200904 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 if (!codec_dais[i]) {
906 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
907 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500908 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200909 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000910 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100912
Benoit Cousson88bd8702014-07-08 23:19:34 +0200913 /* Single codec links expect codec and codec_dai in runtime data */
914 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100915
Mark Brownb19e6e72012-03-14 21:18:39 +0000916 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000917 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000918 if (!snd_soc_is_matching_component(dai_link->platform,
919 component))
920 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000921
922 snd_soc_rtdcom_add(rtd, component);
923 }
924
Mengdong Lin1a497982015-11-18 02:34:11 -0500925 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000926 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500927
928_err_defer:
929 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200930 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931}
932
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200933static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600934{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200935 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200936 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600937
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000938 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600939
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000940 if (component->driver->remove)
941 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600942
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200943 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600944
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200945 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200946 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200947 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600948}
949
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200950static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200951{
952 int err;
953
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000954 if (!dai || !dai->probed ||
955 dai->driver->remove_order != order)
956 return;
957
958 if (dai->driver->remove) {
959 err = dai->driver->remove(dai);
960 if (err < 0)
961 dev_err(dai->dev,
962 "ASoC: failed to remove %s: %d\n",
963 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100964 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000965 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100966}
967
Mengdong Lin1a497982015-11-18 02:34:11 -0500968static void soc_remove_link_dais(struct snd_soc_card *card,
969 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000972 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
974 /* unregister the rtd device */
975 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800976 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977 rtd->dev_registered = 0;
978 }
979
980 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000981 for_each_rtd_codec_dai(rtd, i, codec_dai)
982 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200984 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985}
986
Mengdong Lin1a497982015-11-18 02:34:11 -0500987static void soc_remove_link_components(struct snd_soc_card *card,
988 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200990 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000991 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600992
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000993 for_each_rtdcom(rtd, rtdcom) {
994 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600995
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200996 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200997 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999}
1000
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001001static void soc_remove_dai_links(struct snd_soc_card *card)
1002{
Mengdong Lin1a497982015-11-18 02:34:11 -05001003 int order;
1004 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001005 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001006
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001007 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001008 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001009 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001010 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001011
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001012 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001013 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001014 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001015 }
1016
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001017 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001018 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1020 link->name);
1021
1022 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001023 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001024}
1025
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001026static int snd_soc_init_platform(struct snd_soc_card *card,
1027 struct snd_soc_dai_link *dai_link)
1028{
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001029 struct snd_soc_dai_link_component *platform = dai_link->platform;
1030
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001031 /*
1032 * FIXME
1033 *
1034 * this function should be removed in the future
1035 */
1036 /* convert Legacy platform link */
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001037 if (!platform) {
1038 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001039 sizeof(struct snd_soc_dai_link_component),
1040 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001041 if (!platform)
1042 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001043
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001044 dai_link->platform = platform;
1045 platform->name = dai_link->platform_name;
1046 platform->of_node = dai_link->platform_of_node;
1047 platform->dai_name = NULL;
1048 }
1049
1050 /* if there's no platform we match on the empty platform */
1051 if (!platform->name &&
1052 !platform->of_node)
1053 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001054
1055 return 0;
1056}
1057
Mengdong Lin923c5e612015-11-23 11:01:49 -05001058static int snd_soc_init_multicodec(struct snd_soc_card *card,
1059 struct snd_soc_dai_link *dai_link)
1060{
1061 /* Legacy codec/codec_dai link is a single entry in multicodec */
1062 if (dai_link->codec_name || dai_link->codec_of_node ||
1063 dai_link->codec_dai_name) {
1064 dai_link->num_codecs = 1;
1065
1066 dai_link->codecs = devm_kzalloc(card->dev,
1067 sizeof(struct snd_soc_dai_link_component),
1068 GFP_KERNEL);
1069 if (!dai_link->codecs)
1070 return -ENOMEM;
1071
1072 dai_link->codecs[0].name = dai_link->codec_name;
1073 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1074 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1075 }
1076
1077 if (!dai_link->codecs) {
1078 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1079 return -EINVAL;
1080 }
1081
1082 return 0;
1083}
1084
1085static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001086 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001087{
1088 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001089 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001090
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001091 ret = snd_soc_init_platform(card, link);
1092 if (ret) {
1093 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1094 return ret;
1095 }
1096
Mengdong Lin923c5e612015-11-23 11:01:49 -05001097 ret = snd_soc_init_multicodec(card, link);
1098 if (ret) {
1099 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1100 return ret;
1101 }
1102
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001103 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001104 /*
1105 * Codec must be specified by 1 of name or OF node,
1106 * not both or neither.
1107 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001108 if (!!codec->name ==
1109 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001110 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1111 link->name);
1112 return -EINVAL;
1113 }
1114 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001115 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001116 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1117 link->name);
1118 return -EINVAL;
1119 }
1120 }
1121
1122 /*
1123 * Platform may be specified by either name or OF node, but
1124 * can be left unspecified, and a dummy platform will be used.
1125 */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001126 if (link->platform->name && link->platform->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001127 dev_err(card->dev,
1128 "ASoC: Both platform name/of_node are set for %s\n",
1129 link->name);
1130 return -EINVAL;
1131 }
Mengdong Lin923c5e612015-11-23 11:01:49 -05001132 /*
1133 * CPU device may be specified by either name or OF node, but
1134 * can be left unspecified, and will be matched based on DAI
1135 * name alone..
1136 */
1137 if (link->cpu_name && link->cpu_of_node) {
1138 dev_err(card->dev,
1139 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1140 link->name);
1141 return -EINVAL;
1142 }
1143 /*
1144 * At least one of CPU DAI name or CPU device name/node must be
1145 * specified
1146 */
1147 if (!link->cpu_dai_name &&
1148 !(link->cpu_name || link->cpu_of_node)) {
1149 dev_err(card->dev,
1150 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1151 link->name);
1152 return -EINVAL;
1153 }
1154
1155 return 0;
1156}
1157
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001158void snd_soc_disconnect_sync(struct device *dev)
1159{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001160 struct snd_soc_component *component =
1161 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001162
1163 if (!component || !component->card)
1164 return;
1165
1166 snd_card_disconnect_sync(component->card->snd_card);
1167}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001168EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001169
Mengdong Linf8f80362015-12-02 14:11:22 +08001170/**
1171 * snd_soc_add_dai_link - Add a DAI link dynamically
1172 * @card: The ASoC card to which the DAI link is added
1173 * @dai_link: The new DAI link to add
1174 *
1175 * This function adds a DAI link to the ASoC card's link list.
1176 *
1177 * Note: Topology can use this API to add DAI links when probing the
1178 * topology component. And machine drivers can still define static
1179 * DAI links in dai_link array.
1180 */
1181int snd_soc_add_dai_link(struct snd_soc_card *card,
1182 struct snd_soc_dai_link *dai_link)
1183{
1184 if (dai_link->dobj.type
1185 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1186 dev_err(card->dev, "Invalid dai link type %d\n",
1187 dai_link->dobj.type);
1188 return -EINVAL;
1189 }
1190
1191 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001192 /*
1193 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001194 * on the link created by topology.
1195 */
1196 if (dai_link->dobj.type && card->add_dai_link)
1197 card->add_dai_link(card, dai_link);
1198
Mengdong Linf8f80362015-12-02 14:11:22 +08001199 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001200
1201 return 0;
1202}
1203EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1204
1205/**
1206 * snd_soc_remove_dai_link - Remove a DAI link from the list
1207 * @card: The ASoC card that owns the link
1208 * @dai_link: The DAI link to remove
1209 *
1210 * This function removes a DAI link from the ASoC card's link list.
1211 *
1212 * For DAI links previously added by topology, topology should
1213 * remove them by using the dobj embedded in the link.
1214 */
1215void snd_soc_remove_dai_link(struct snd_soc_card *card,
1216 struct snd_soc_dai_link *dai_link)
1217{
1218 struct snd_soc_dai_link *link, *_link;
1219
1220 if (dai_link->dobj.type
1221 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1222 dev_err(card->dev, "Invalid dai link type %d\n",
1223 dai_link->dobj.type);
1224 return;
1225 }
1226
1227 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001228 /*
1229 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001230 * on the link created by topology.
1231 */
1232 if (dai_link->dobj.type && card->remove_dai_link)
1233 card->remove_dai_link(card, dai_link);
1234
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001235 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001236 if (link == dai_link) {
1237 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001238 return;
1239 }
1240 }
1241}
1242EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1243
Jerome Brunetaefba452018-07-13 14:50:43 +02001244static void soc_set_of_name_prefix(struct snd_soc_component *component)
1245{
1246 struct device_node *component_of_node = component->dev->of_node;
1247 const char *str;
1248 int ret;
1249
1250 if (!component_of_node && component->dev->parent)
1251 component_of_node = component->dev->parent->of_node;
1252
1253 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1254 &str);
1255 if (!ret)
1256 component->name_prefix = str;
1257}
1258
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001259static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001260 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001261{
1262 int i;
1263
Jerome Brunetaefba452018-07-13 14:50:43 +02001264 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001265 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001266 struct device_node *component_of_node = component->dev->of_node;
1267
1268 if (!component_of_node && component->dev->parent)
1269 component_of_node = component->dev->parent->of_node;
1270
1271 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001272 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001273 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001274 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001275 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001276 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001277 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001278
1279 /*
1280 * If there is no configuration table or no match in the table,
1281 * check if a prefix is provided in the node
1282 */
1283 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001284}
1285
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001286static int soc_probe_component(struct snd_soc_card *card,
1287 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001289 struct snd_soc_dapm_context *dapm =
1290 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001291 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001292 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001294 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001295 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001296
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001297 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001298 if (component->card != card) {
1299 dev_err(component->dev,
1300 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1301 card->name, component->card->name);
1302 return -ENODEV;
1303 }
1304 return 0;
1305 }
1306
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001307 if (!try_module_get(component->dev->driver->owner))
1308 return -ENODEV;
1309
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001310 component->card = card;
1311 dapm->card = card;
1312 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001313
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001314 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001315
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001316 if (component->driver->dapm_widgets) {
1317 ret = snd_soc_dapm_new_controls(dapm,
1318 component->driver->dapm_widgets,
1319 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001320
1321 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001323 "Failed to create new controls %d\n", ret);
1324 goto err_probe;
1325 }
1326 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001327
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001328 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001329 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001330 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001331 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001332 "Failed to create DAI widgets %d\n", ret);
1333 goto err_probe;
1334 }
1335 }
Mark Brown888df392012-02-16 19:37:51 -08001336
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001337 if (component->driver->probe) {
1338 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001340 dev_err(component->dev,
1341 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001342 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001344
1345 WARN(dapm->idle_bias_off &&
1346 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001347 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001348 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349 }
1350
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001351 /* machine specific init */
1352 if (component->init) {
1353 ret = component->init(component);
1354 if (ret < 0) {
1355 dev_err(component->dev,
1356 "Failed to do machine specific init %d\n", ret);
1357 goto err_probe;
1358 }
1359 }
1360
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001361 if (component->driver->controls)
1362 snd_soc_add_component_controls(component,
1363 component->driver->controls,
1364 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001365 if (component->driver->dapm_routes)
1366 snd_soc_dapm_add_routes(dapm,
1367 component->driver->dapm_routes,
1368 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001369
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001370 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001371 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001372 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373
Jarkko Nikula70d293312011-01-27 16:24:22 +02001374 return 0;
1375
1376err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001377 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001378 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001379 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001380
1381 return ret;
1382}
1383
Mark Brown36ae1a92012-01-06 17:12:45 -08001384static void rtd_release(struct device *dev)
1385{
1386 kfree(dev);
1387}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001389static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1390 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001391{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001392 int ret = 0;
1393
Jarkko Nikula589c3562010-12-06 16:27:07 +02001394 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001395 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1396 if (!rtd->dev)
1397 return -ENOMEM;
1398 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001399 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001400 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001401 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001402 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001403 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001404 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001405 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1406 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1407 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1408 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001409 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001410 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001411 /* calling put_device() here to free the rtd->dev */
1412 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001413 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001414 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001415 return ret;
1416 }
1417 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001418 return 0;
1419}
1420
Mengdong Lin1a497982015-11-18 02:34:11 -05001421static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001422 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001423{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001424 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001425 struct snd_soc_rtdcom_list *rtdcom;
1426 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001427
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001428 for_each_rtdcom(rtd, rtdcom) {
1429 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001430
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001431 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001432 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001433 if (ret < 0)
1434 return ret;
1435 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001436 }
1437
Stephen Warren62ae68f2012-06-08 12:34:23 -06001438 return 0;
1439}
1440
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001441static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001443 if (dai->probed ||
1444 dai->driver->probe_order != order)
1445 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001446
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001447 if (dai->driver->probe) {
1448 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001449
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001450 if (ret < 0) {
1451 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1452 dai->name, ret);
1453 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001454 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001455 }
1456
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001457 dai->probed = 1;
1458
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001459 return 0;
1460}
1461
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001462static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1463 struct snd_soc_pcm_runtime *rtd)
1464{
1465 int i, ret = 0;
1466
1467 for (i = 0; i < num_dais; ++i) {
1468 struct snd_soc_dai_driver *drv = dais[i]->driver;
1469
1470 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1471 ret = drv->pcm_new(rtd, dais[i]);
1472 if (ret < 0) {
1473 dev_err(dais[i]->dev,
1474 "ASoC: Failed to bind %s with pcm device\n",
1475 dais[i]->name);
1476 return ret;
1477 }
1478 }
1479
1480 return 0;
1481}
1482
Mengdong Lin1a497982015-11-18 02:34:11 -05001483static int soc_probe_link_dais(struct snd_soc_card *card,
1484 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485{
Mengdong Lin1a497982015-11-18 02:34:11 -05001486 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001487 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001488 struct snd_soc_rtdcom_list *rtdcom;
1489 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001490 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001491 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001493 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001494 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 /* set default power off timeout */
1497 rtd->pmdown_time = pmdown_time;
1498
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001499 ret = soc_probe_dai(cpu_dai, order);
1500 if (ret)
1501 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001504 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1505 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001506 if (ret)
1507 return ret;
1508 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001509
Liam Girdwood0168bf02011-06-07 16:08:05 +01001510 /* complete DAI probe during last probe */
1511 if (order != SND_SOC_COMP_ORDER_LAST)
1512 return 0;
1513
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001514 /* do machine specific initialization */
1515 if (dai_link->init) {
1516 ret = dai_link->init(rtd);
1517 if (ret < 0) {
1518 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1519 dai_link->name, ret);
1520 return ret;
1521 }
1522 }
1523
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001524 if (dai_link->dai_fmt)
1525 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1526
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001527 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001528 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001530
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001531#ifdef CONFIG_DEBUG_FS
1532 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001533 if (dai_link->dynamic)
1534 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001535#endif
1536
Liam Girdwooda655de82018-07-02 16:59:54 +01001537 num = rtd->num;
1538
1539 /*
1540 * most drivers will register their PCMs using DAI link ordering but
1541 * topology based drivers can use the DAI link id field to set PCM
1542 * device number and then use rtd + a base offset of the BEs.
1543 */
1544 for_each_rtdcom(rtd, rtdcom) {
1545 component = rtdcom->component;
1546
1547 if (!component->driver->use_dai_pcm_id)
1548 continue;
1549
1550 if (rtd->dai_link->no_pcm)
1551 num += component->driver->be_pcm_base;
1552 else
1553 num = rtd->dai_link->id;
1554 }
1555
Jie Yang6f0c4222015-10-13 23:41:00 +08001556 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001557 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001558 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001559 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001560 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301561 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001562 return ret;
1563 }
1564 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301565
1566 if (!dai_link->params) {
1567 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001568 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301569 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001570 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001571 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001572 return ret;
1573 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001574 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1575 if (ret < 0)
1576 return ret;
1577 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1578 rtd->num_codecs, rtd);
1579 if (ret < 0)
1580 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301581 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001582 INIT_DELAYED_WORK(&rtd->delayed_work,
1583 codec2codec_close_delayed_work);
Mark Brownc74184e2012-04-04 22:12:09 +01001584 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585 }
1586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 return 0;
1588}
1589
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001590static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001591{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001592 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001593 struct snd_soc_component *component;
1594 const char *name;
1595 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001596
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001597 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1598 /* codecs, usually analog devices */
1599 name = aux_dev->codec_name;
1600 codec_of_node = aux_dev->codec_of_node;
1601 component = soc_find_component(codec_of_node, name);
1602 if (!component) {
1603 if (codec_of_node)
1604 name = of_node_full_name(codec_of_node);
1605 goto err_defer;
1606 }
1607 } else if (aux_dev->name) {
1608 /* generic components */
1609 name = aux_dev->name;
1610 component = soc_find_component(NULL, name);
1611 if (!component)
1612 goto err_defer;
1613 } else {
1614 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1615 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001616 }
1617
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001618 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001619 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001620
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001621 return 0;
1622
1623err_defer:
1624 dev_err(card->dev, "ASoC: %s not registered\n", name);
1625 return -EPROBE_DEFER;
1626}
1627
1628static int soc_probe_aux_devices(struct snd_soc_card *card)
1629{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001630 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001631 int order;
1632 int ret;
1633
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001634 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001635 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001636 if (comp->driver->probe_order == order) {
1637 ret = soc_probe_component(card, comp);
1638 if (ret < 0) {
1639 dev_err(card->dev,
1640 "ASoC: failed to probe aux component %s %d\n",
1641 comp->name, ret);
1642 return ret;
1643 }
1644 }
1645 }
1646 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001647
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001648 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001649}
1650
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001651static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001652{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001653 struct snd_soc_component *comp, *_comp;
1654 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001655
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001656 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001657 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001658 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001659
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001660 if (comp->driver->remove_order == order) {
1661 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001662 /* remove it from the card's aux_comp_list */
1663 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001664 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001665 }
1666 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001667}
1668
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001669/**
1670 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1671 * @rtd: The runtime for which the DAI link format should be changed
1672 * @dai_fmt: The new DAI link format
1673 *
1674 * This function updates the DAI link format for all DAIs connected to the DAI
1675 * link for the specified runtime.
1676 *
1677 * Note: For setups with a static format set the dai_fmt field in the
1678 * corresponding snd_dai_link struct instead of using this function.
1679 *
1680 * Returns 0 on success, otherwise a negative error code.
1681 */
1682int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1683 unsigned int dai_fmt)
1684{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001685 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001686 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001687 unsigned int i;
1688 int ret;
1689
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001690 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001691 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1692 if (ret != 0 && ret != -ENOTSUPP) {
1693 dev_warn(codec_dai->dev,
1694 "ASoC: Failed to set DAI format: %d\n", ret);
1695 return ret;
1696 }
1697 }
1698
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001699 /*
1700 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1701 * the component which has non_legacy_dai_naming is Codec
1702 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001703 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001704 unsigned int inv_dai_fmt;
1705
1706 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1707 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1708 case SND_SOC_DAIFMT_CBM_CFM:
1709 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1710 break;
1711 case SND_SOC_DAIFMT_CBM_CFS:
1712 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1713 break;
1714 case SND_SOC_DAIFMT_CBS_CFM:
1715 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1716 break;
1717 case SND_SOC_DAIFMT_CBS_CFS:
1718 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1719 break;
1720 }
1721
1722 dai_fmt = inv_dai_fmt;
1723 }
1724
1725 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1726 if (ret != 0 && ret != -ENOTSUPP) {
1727 dev_warn(cpu_dai->dev,
1728 "ASoC: Failed to set DAI format: %d\n", ret);
1729 return ret;
1730 }
1731
1732 return 0;
1733}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001734EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001735
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001736#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001737/*
1738 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001739 * separate different DMI fields in the card long name. Only number and
1740 * alphabet characters and a few separator characters are kept.
1741 */
1742static void cleanup_dmi_name(char *name)
1743{
1744 int i, j = 0;
1745
1746 for (i = 0; name[i]; i++) {
1747 if (isalnum(name[i]) || (name[i] == '.')
1748 || (name[i] == '_'))
1749 name[j++] = name[i];
1750 else if (name[i] == '-')
1751 name[j++] = '_';
1752 }
1753
1754 name[j] = '\0';
1755}
1756
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001757/*
1758 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001759 * in the black list.
1760 */
1761static int is_dmi_valid(const char *field)
1762{
1763 int i = 0;
1764
1765 while (dmi_blacklist[i]) {
1766 if (strstr(field, dmi_blacklist[i]))
1767 return 0;
1768 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001769 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001770
1771 return 1;
1772}
1773
Liam Girdwood345233d2017-01-14 16:13:02 +08001774/**
1775 * snd_soc_set_dmi_name() - Register DMI names to card
1776 * @card: The card to register DMI names
1777 * @flavour: The flavour "differentiator" for the card amongst its peers.
1778 *
1779 * An Intel machine driver may be used by many different devices but are
1780 * difficult for userspace to differentiate, since machine drivers ususally
1781 * use their own name as the card short name and leave the card long name
1782 * blank. To differentiate such devices and fix bugs due to lack of
1783 * device-specific configurations, this function allows DMI info to be used
1784 * as the sound card long name, in the format of
1785 * "vendor-product-version-board"
1786 * (Character '-' is used to separate different DMI fields here).
1787 * This will help the user space to load the device-specific Use Case Manager
1788 * (UCM) configurations for the card.
1789 *
1790 * Possible card long names may be:
1791 * DellInc.-XPS139343-01-0310JH
1792 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1793 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1794 *
1795 * This function also supports flavoring the card longname to provide
1796 * the extra differentiation, like "vendor-product-version-board-flavor".
1797 *
1798 * We only keep number and alphabet characters and a few separator characters
1799 * in the card long name since UCM in the user space uses the card long names
1800 * as card configuration directory names and AudoConf cannot support special
1801 * charactors like SPACE.
1802 *
1803 * Returns 0 on success, otherwise a negative error code.
1804 */
1805int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1806{
1807 const char *vendor, *product, *product_version, *board;
1808 size_t longname_buf_size = sizeof(card->snd_card->longname);
1809 size_t len;
1810
1811 if (card->long_name)
1812 return 0; /* long name already set by driver or from DMI */
1813
1814 /* make up dmi long name as: vendor.product.version.board */
1815 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001816 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001817 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1818 return 0;
1819 }
1820
1821 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1822 "%s", vendor);
1823 cleanup_dmi_name(card->dmi_longname);
1824
1825 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001826 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001827 len = strlen(card->dmi_longname);
1828 snprintf(card->dmi_longname + len,
1829 longname_buf_size - len,
1830 "-%s", product);
1831
1832 len++; /* skip the separator "-" */
1833 if (len < longname_buf_size)
1834 cleanup_dmi_name(card->dmi_longname + len);
1835
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001836 /*
1837 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001838 * name in the product version field
1839 */
1840 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001841 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001842 len = strlen(card->dmi_longname);
1843 snprintf(card->dmi_longname + len,
1844 longname_buf_size - len,
1845 "-%s", product_version);
1846
1847 len++;
1848 if (len < longname_buf_size)
1849 cleanup_dmi_name(card->dmi_longname + len);
1850 }
1851 }
1852
1853 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001854 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001855 len = strlen(card->dmi_longname);
1856 snprintf(card->dmi_longname + len,
1857 longname_buf_size - len,
1858 "-%s", board);
1859
1860 len++;
1861 if (len < longname_buf_size)
1862 cleanup_dmi_name(card->dmi_longname + len);
1863 } else if (!product) {
1864 /* fall back to using legacy name */
1865 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1866 return 0;
1867 }
1868
1869 /* Add flavour to dmi long name */
1870 if (flavour) {
1871 len = strlen(card->dmi_longname);
1872 snprintf(card->dmi_longname + len,
1873 longname_buf_size - len,
1874 "-%s", flavour);
1875
1876 len++;
1877 if (len < longname_buf_size)
1878 cleanup_dmi_name(card->dmi_longname + len);
1879 }
1880
1881 /* set the card long name */
1882 card->long_name = card->dmi_longname;
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001887#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001888
Liam Girdwooda655de82018-07-02 16:59:54 +01001889static void soc_check_tplg_fes(struct snd_soc_card *card)
1890{
1891 struct snd_soc_component *component;
1892 const struct snd_soc_component_driver *comp_drv;
1893 struct snd_soc_dai_link *dai_link;
1894 int i;
1895
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001896 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001897
1898 /* does this component override FEs ? */
1899 if (!component->driver->ignore_machine)
1900 continue;
1901
1902 /* for this machine ? */
1903 if (strcmp(component->driver->ignore_machine,
1904 card->dev->driver->name))
1905 continue;
1906
1907 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001908 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001909
1910 /* ignore this FE */
1911 if (dai_link->dynamic) {
1912 dai_link->ignore = true;
1913 continue;
1914 }
1915
1916 dev_info(card->dev, "info: override FE DAI link %s\n",
1917 card->dai_link[i].name);
1918
1919 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001920 if (snd_soc_init_platform(card, dai_link) < 0) {
1921 dev_err(card->dev, "init platform error");
1922 continue;
1923 }
1924 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001925
1926 /* convert non BE into BE */
1927 dai_link->no_pcm = 1;
1928
1929 /* override any BE fixups */
1930 dai_link->be_hw_params_fixup =
1931 component->driver->be_hw_params_fixup;
1932
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001933 /*
1934 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001935 * dai link name if it's NULL to help bind widgets.
1936 */
1937 if (!dai_link->stream_name)
1938 dai_link->stream_name = dai_link->name;
1939 }
1940
1941 /* Inform userspace we are using alternate topology */
1942 if (component->driver->topology_name_prefix) {
1943
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001944 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001945 if (!card->topology_shortname_created) {
1946 comp_drv = component->driver;
1947
1948 snprintf(card->topology_shortname, 32, "%s-%s",
1949 comp_drv->topology_name_prefix,
1950 card->name);
1951 card->topology_shortname_created = true;
1952 }
1953
1954 /* use topology shortname */
1955 card->name = card->topology_shortname;
1956 }
1957 }
1958}
1959
Mark Brownb19e6e72012-03-14 21:18:39 +00001960static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001961{
Mengdong Lin1a497982015-11-18 02:34:11 -05001962 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001963 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001964 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001965
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001966 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001967 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968
Liam Girdwooda655de82018-07-02 16:59:54 +01001969 /* check whether any platform is ignore machine FE and using topology */
1970 soc_check_tplg_fes(card);
1971
Mark Brownb19e6e72012-03-14 21:18:39 +00001972 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001973 for_each_card_prelinks(card, i, dai_link) {
1974 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00001975 if (ret != 0)
1976 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977 }
1978
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001979 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001980 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001981 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001982 if (ret != 0)
1983 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 }
1985
Mengdong Linf8f80362015-12-02 14:11:22 +08001986 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001987 for_each_card_prelinks(card, i, dai_link)
1988 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08001989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001990 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001991 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 card->owner, 0, &card->snd_card);
1993 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001994 dev_err(card->dev,
1995 "ASoC: can't create sound card for card %s: %d\n",
1996 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001997 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002000 soc_init_card_debugfs(card);
2001
Mark Browne37a4972011-03-02 18:21:57 +00002002 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2003 card->dapm.dev = card->dev;
2004 card->dapm.card = card;
2005 list_add(&card->dapm.list, &card->dapm_list);
2006
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002007#ifdef CONFIG_DEBUG_FS
2008 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2009#endif
2010
Mark Brown88ee1c62011-02-02 10:43:26 +00002011#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002012 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002013 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002014#endif
Andy Green6ed25972008-06-13 16:24:05 +01002015
Mark Brown9a841eb2011-04-12 17:51:37 -07002016 if (card->dapm_widgets)
2017 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2018 card->num_dapm_widgets);
2019
Nicolin Chenf23e8602015-02-14 17:22:49 -08002020 if (card->of_dapm_widgets)
2021 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2022 card->num_of_dapm_widgets);
2023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002024 /* initialise the sound card only once */
2025 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002026 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 if (ret < 0)
2028 goto card_probe_error;
2029 }
2030
Stephen Warren62ae68f2012-06-08 12:34:23 -06002031 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002032 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002033 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002034 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002035 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002036 dev_err(card->dev,
2037 "ASoC: failed to instantiate card %d\n",
2038 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002039 goto probe_dai_err;
2040 }
2041 }
2042 }
2043
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002044 /* probe auxiliary components */
2045 ret = soc_probe_aux_devices(card);
2046 if (ret < 0)
2047 goto probe_dai_err;
2048
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002049 /*
2050 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002051 * Components with topology may bring new DAIs and DAI links.
2052 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002053 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002054 if (soc_is_dai_link_bound(card, dai_link))
2055 continue;
2056
2057 ret = soc_init_dai_link(card, dai_link);
2058 if (ret)
2059 goto probe_dai_err;
2060 ret = soc_bind_dai_link(card, dai_link);
2061 if (ret)
2062 goto probe_dai_err;
2063 }
2064
Stephen Warren62ae68f2012-06-08 12:34:23 -06002065 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002066 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002067 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002068 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002069 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002070 dev_err(card->dev,
2071 "ASoC: failed to instantiate card %d\n",
2072 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002073 goto probe_dai_err;
2074 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002075 }
2076 }
2077
Mark Brown888df392012-02-16 19:37:51 -08002078 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002079 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002080
Mark Brownb7af1da2011-04-07 19:18:44 +09002081 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002082 snd_soc_add_card_controls(card, card->controls,
2083 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002084
Mark Brownb8ad29d2011-03-02 18:35:51 +00002085 if (card->dapm_routes)
2086 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2087 card->num_dapm_routes);
2088
Nicolin Chenf23e8602015-02-14 17:22:49 -08002089 if (card->of_dapm_routes)
2090 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2091 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002092
Takashi Iwai861886d2017-04-24 08:54:42 +02002093 /* try to set some sane longname if DMI is available */
2094 snd_soc_set_dmi_name(card, NULL);
2095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002097 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002098 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2099 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002100 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2101 "%s", card->driver_name ? card->driver_name : card->name);
2102 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2103 switch (card->snd_card->driver[i]) {
2104 case '_':
2105 case '-':
2106 case '\0':
2107 break;
2108 default:
2109 if (!isalnum(card->snd_card->driver[i]))
2110 card->snd_card->driver[i] = '_';
2111 break;
2112 }
2113 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114
Mark Brown28e9ad92011-03-02 18:36:34 +00002115 if (card->late_probe) {
2116 ret = card->late_probe(card);
2117 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002118 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002119 card->name, ret);
2120 goto probe_aux_dev_err;
2121 }
2122 }
2123
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002124 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 ret = snd_card_register(card->snd_card);
2127 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002128 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2129 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002130 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002131 }
2132
Mark Brown435c5e22008-12-04 15:32:53 +00002133 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002134 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002135 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002136 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002137
2138 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002139
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002140probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002141 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002143probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002144 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002146card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002147 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002148 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002149
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002150 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002151 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002152 snd_card_free(card->snd_card);
2153
Mark Brownb19e6e72012-03-14 21:18:39 +00002154base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002155 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002156 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002157 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158
Mark Brownb19e6e72012-03-14 21:18:39 +00002159 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002160}
2161
2162/* probes a new socdev */
2163static int soc_probe(struct platform_device *pdev)
2164{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002165 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002166
Vinod Koul70a7ca32011-01-14 19:22:48 +05302167 /*
2168 * no card, so machine driver should be registering card
2169 * we should not be here in that case so ret error
2170 */
2171 if (!card)
2172 return -EINVAL;
2173
Mark Brownfe4085e2012-03-02 13:07:41 +00002174 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002175 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002176 card->name);
2177
Mark Brown435c5e22008-12-04 15:32:53 +00002178 /* Bodge while we unpick instantiation */
2179 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002180
Mark Brown28d528c2012-08-09 18:45:23 +01002181 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182}
2183
Vinod Koulb0e26482011-01-13 22:48:02 +05302184static int soc_cleanup_card_resources(struct snd_soc_card *card)
2185{
Mengdong Lin1a497982015-11-18 02:34:11 -05002186 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302187
2188 /* make sure any delayed work runs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002189 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002190 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302191
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002192 /* free the ALSA card at first; this syncs with pending operations */
2193 snd_card_free(card->snd_card);
2194
Vinod Koulb0e26482011-01-13 22:48:02 +05302195 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002196 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002197 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302198
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002199 /* remove auxiliary devices */
2200 soc_remove_aux_devices(card);
2201
Mark Brownd1e81422016-08-18 19:32:59 +01002202 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302203 soc_cleanup_card_debugfs(card);
2204
2205 /* remove the card */
2206 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002207 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302208
Vinod Koulb0e26482011-01-13 22:48:02 +05302209 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302210}
2211
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002212/* removes a socdev */
2213static int soc_remove(struct platform_device *pdev)
2214{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002215 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002216
Mark Brownc5af3a22008-11-28 13:29:45 +00002217 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002218 return 0;
2219}
2220
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002221int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002222{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002223 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002224 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002225
2226 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002227 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002228
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002229 /*
2230 * Flush out pmdown_time work - we actually do want to run it
2231 * now, we're shutting down so no imminent restart.
2232 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002233 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002234 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002236 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002237
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002238 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002239 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002240 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002241 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002242 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002243
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002244 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002245 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002246 pinctrl_pm_select_sleep_state(codec_dai->dev);
2247 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002248 }
2249
Mark Brown416356f2009-06-30 19:05:15 +01002250 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002251}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002252EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002253
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002254const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302255 .suspend = snd_soc_suspend,
2256 .resume = snd_soc_resume,
2257 .freeze = snd_soc_suspend,
2258 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002259 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302260 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002261};
Stephen Warrendeb26072011-04-05 19:35:30 -06002262EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002263
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264/* ASoC platform driver */
2265static struct platform_driver soc_driver = {
2266 .driver = {
2267 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002268 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002269 },
2270 .probe = soc_probe,
2271 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272};
2273
Mark Brown096e49d2009-07-05 15:12:22 +01002274/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275 * snd_soc_cnew - create new control
2276 * @_template: control template
2277 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002278 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002279 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 *
2281 * Create a new mixer control from a template control.
2282 *
2283 * Returns 0 for success, else error.
2284 */
2285struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002286 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002287 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002288{
2289 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002290 struct snd_kcontrol *kcontrol;
2291 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002292
2293 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002294 template.index = 0;
2295
Mark Brownefb7ac32011-03-08 17:23:24 +00002296 if (!long_name)
2297 long_name = template.name;
2298
2299 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002300 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002301 if (!name)
2302 return NULL;
2303
Mark Brownefb7ac32011-03-08 17:23:24 +00002304 template.name = name;
2305 } else {
2306 template.name = long_name;
2307 }
2308
2309 kcontrol = snd_ctl_new1(&template, data);
2310
2311 kfree(name);
2312
2313 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002314}
2315EXPORT_SYMBOL_GPL(snd_soc_cnew);
2316
Liam Girdwood022658b2012-02-03 17:43:09 +00002317static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2318 const struct snd_kcontrol_new *controls, int num_controls,
2319 const char *prefix, void *data)
2320{
2321 int err, i;
2322
2323 for (i = 0; i < num_controls; i++) {
2324 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002325
Liam Girdwood022658b2012-02-03 17:43:09 +00002326 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2327 control->name, prefix));
2328 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002329 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2330 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002331 return err;
2332 }
2333 }
2334
2335 return 0;
2336}
2337
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002338struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2339 const char *name)
2340{
2341 struct snd_card *card = soc_card->snd_card;
2342 struct snd_kcontrol *kctl;
2343
2344 if (unlikely(!name))
2345 return NULL;
2346
2347 list_for_each_entry(kctl, &card->controls, list)
2348 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2349 return kctl;
2350 return NULL;
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2353
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002355 * snd_soc_add_component_controls - Add an array of controls to a component.
2356 *
2357 * @component: Component to add controls to
2358 * @controls: Array of controls to add
2359 * @num_controls: Number of elements in the array
2360 *
2361 * Return: 0 for success, else error.
2362 */
2363int snd_soc_add_component_controls(struct snd_soc_component *component,
2364 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2365{
2366 struct snd_card *card = component->card->snd_card;
2367
2368 return snd_soc_add_controls(card, component->dev, controls,
2369 num_controls, component->name_prefix, component);
2370}
2371EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2372
2373/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002374 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2375 * Convenience function to add a list of controls.
2376 *
2377 * @soc_card: SoC card to add controls to
2378 * @controls: array of controls to add
2379 * @num_controls: number of elements in the array
2380 *
2381 * Return 0 for success, else error.
2382 */
2383int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2384 const struct snd_kcontrol_new *controls, int num_controls)
2385{
2386 struct snd_card *card = soc_card->snd_card;
2387
2388 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2389 NULL, soc_card);
2390}
2391EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2392
2393/**
2394 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2395 * Convienience function to add a list of controls.
2396 *
2397 * @dai: DAI to add controls to
2398 * @controls: array of controls to add
2399 * @num_controls: number of elements in the array
2400 *
2401 * Return 0 for success, else error.
2402 */
2403int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2404 const struct snd_kcontrol_new *controls, int num_controls)
2405{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002406 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002407
2408 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2409 NULL, dai);
2410}
2411EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2412
2413/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002414 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2415 * @dai: DAI
2416 * @clk_id: DAI specific clock ID
2417 * @freq: new clock frequency in Hz
2418 * @dir: new clock direction - input/output.
2419 *
2420 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2421 */
2422int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2423 unsigned int freq, int dir)
2424{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002425 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002426 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002427
2428 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2429 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002430}
2431EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2432
2433/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002434 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2435 * @component: COMPONENT
2436 * @clk_id: DAI specific clock ID
2437 * @source: Source for the clock
2438 * @freq: new clock frequency in Hz
2439 * @dir: new clock direction - input/output.
2440 *
2441 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2442 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002443int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2444 int clk_id, int source, unsigned int freq,
2445 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002446{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002447 if (component->driver->set_sysclk)
2448 return component->driver->set_sysclk(component, clk_id, source,
2449 freq, dir);
2450
2451 return -ENOTSUPP;
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2454
2455/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002456 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2457 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002458 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002459 * @div: new clock divisor.
2460 *
2461 * Configures the clock dividers. This is used to derive the best DAI bit and
2462 * frame clocks from the system or master clock. It's best to set the DAI bit
2463 * and frame clocks as low as possible to save system power.
2464 */
2465int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2466 int div_id, int div)
2467{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002468 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002469 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002470 else
2471 return -EINVAL;
2472}
2473EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2474
2475/**
2476 * snd_soc_dai_set_pll - configure DAI PLL.
2477 * @dai: DAI
2478 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002479 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002480 * @freq_in: PLL input clock frequency in Hz
2481 * @freq_out: requested PLL output clock frequency in Hz
2482 *
2483 * Configures and enables PLL to generate output clock based on input clock.
2484 */
Mark Brown85488032009-09-05 18:52:16 +01002485int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2486 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002487{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002488 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002489 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002490 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002491
2492 return snd_soc_component_set_pll(dai->component, pll_id, source,
2493 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002494}
2495EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2496
Mark Brownec4ee522011-03-07 20:58:11 +00002497/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002498 * snd_soc_component_set_pll - configure component PLL.
2499 * @component: COMPONENT
2500 * @pll_id: DAI specific PLL ID
2501 * @source: DAI specific source for the PLL
2502 * @freq_in: PLL input clock frequency in Hz
2503 * @freq_out: requested PLL output clock frequency in Hz
2504 *
2505 * Configures and enables PLL to generate output clock based on input clock.
2506 */
2507int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2508 int source, unsigned int freq_in,
2509 unsigned int freq_out)
2510{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002511 if (component->driver->set_pll)
2512 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002513 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002514
2515 return -EINVAL;
2516}
2517EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2518
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002519/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002520 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2521 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002522 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002523 *
2524 * Configures the DAI for a preset BCLK to sample rate ratio.
2525 */
2526int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2527{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002528 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002529 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2530 else
2531 return -EINVAL;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2534
2535/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002536 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2537 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002538 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002539 *
2540 * Configures the DAI hardware format and clocking.
2541 */
2542int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2543{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002544 if (dai->driver->ops->set_fmt == NULL)
2545 return -ENOTSUPP;
2546 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002547}
2548EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2549
2550/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002551 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002552 * @slots: Number of slots in use.
2553 * @tx_mask: bitmask representing active TX slots.
2554 * @rx_mask: bitmask representing active RX slots.
2555 *
2556 * Generates the TDM tx and rx slot default masks for DAI.
2557 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002558static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002559 unsigned int *tx_mask,
2560 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002561{
2562 if (*tx_mask || *rx_mask)
2563 return 0;
2564
2565 if (!slots)
2566 return -EINVAL;
2567
2568 *tx_mask = (1 << slots) - 1;
2569 *rx_mask = (1 << slots) - 1;
2570
2571 return 0;
2572}
2573
2574/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002575 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2576 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002577 * @tx_mask: bitmask representing active TX slots.
2578 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002579 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002580 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002581 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002582 * This function configures the specified DAI for TDM operation. @slot contains
2583 * the total number of slots of the TDM stream and @slot_with the width of each
2584 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2585 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2586 * DAI should write to or read from. If a bit is set the corresponding slot is
2587 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2588 * the first slot, bit 1 to the second slot and so on. The first active slot
2589 * maps to the first channel of the DAI, the second active slot to the second
2590 * channel and so on.
2591 *
2592 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2593 * @rx_mask and @slot_width will be ignored.
2594 *
2595 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002596 */
2597int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002598 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002599{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002600 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002601 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002602 &tx_mask, &rx_mask);
2603 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002604 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002605
Benoit Cousson88bd8702014-07-08 23:19:34 +02002606 dai->tx_mask = tx_mask;
2607 dai->rx_mask = rx_mask;
2608
Kuninori Morimoto46471922017-09-25 01:38:34 +00002609 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002610 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002611 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002612 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002613 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002614}
2615EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2616
2617/**
Barry Song472df3c2009-09-12 01:16:29 +08002618 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2619 * @dai: DAI
2620 * @tx_num: how many TX channels
2621 * @tx_slot: pointer to an array which imply the TX slot number channel
2622 * 0~num-1 uses
2623 * @rx_num: how many RX channels
2624 * @rx_slot: pointer to an array which imply the RX slot number channel
2625 * 0~num-1 uses
2626 *
2627 * configure the relationship between channel number and TDM slot number.
2628 */
2629int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2630 unsigned int tx_num, unsigned int *tx_slot,
2631 unsigned int rx_num, unsigned int *rx_slot)
2632{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002633 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002634 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002635 rx_num, rx_slot);
2636 else
2637 return -EINVAL;
2638}
2639EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2640
2641/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002642 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2643 * @dai: DAI
2644 * @tx_num: how many TX channels
2645 * @tx_slot: pointer to an array which imply the TX slot number channel
2646 * 0~num-1 uses
2647 * @rx_num: how many RX channels
2648 * @rx_slot: pointer to an array which imply the RX slot number channel
2649 * 0~num-1 uses
2650 */
2651int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2652 unsigned int *tx_num, unsigned int *tx_slot,
2653 unsigned int *rx_num, unsigned int *rx_slot)
2654{
2655 if (dai->driver->ops->get_channel_map)
2656 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2657 rx_num, rx_slot);
2658 else
2659 return -ENOTSUPP;
2660}
2661EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2662
2663/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002664 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2665 * @dai: DAI
2666 * @tristate: tristate enable
2667 *
2668 * Tristates the DAI so that others can use it.
2669 */
2670int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2671{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002672 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002673 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002674 else
2675 return -EINVAL;
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2678
2679/**
2680 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2681 * @dai: DAI
2682 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002683 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002684 *
2685 * Mutes the DAI DAC.
2686 */
Mark Brownda183962013-02-06 15:44:07 +00002687int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2688 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002689{
Mark Brownda183962013-02-06 15:44:07 +00002690 if (dai->driver->ops->mute_stream)
2691 return dai->driver->ops->mute_stream(dai, mute, direction);
2692 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2693 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002694 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002695 else
Mark Brown04570c62012-04-13 19:16:03 +01002696 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002697}
2698EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2699
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002700static int snd_soc_bind_card(struct snd_soc_card *card)
2701{
2702 struct snd_soc_pcm_runtime *rtd;
2703 int ret;
2704
2705 ret = snd_soc_instantiate_card(card);
2706 if (ret != 0)
2707 return ret;
2708
2709 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002710 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002711 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2712 struct snd_soc_dai *codec_dai;
2713 int j;
2714
2715 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2716 if (!codec_dai->active)
2717 pinctrl_pm_select_sleep_state(codec_dai->dev);
2718 }
2719
2720 if (!cpu_dai->active)
2721 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2722 }
2723
2724 return ret;
2725}
2726
Mark Brownc5af3a22008-11-28 13:29:45 +00002727/**
2728 * snd_soc_register_card - Register a card with the ASoC core
2729 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002730 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002731 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002732 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302733int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002734{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002735 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002736 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002737
Mark Brownc5af3a22008-11-28 13:29:45 +00002738 if (!card->name || !card->dev)
2739 return -EINVAL;
2740
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002741 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002742
Mengdong Lin923c5e612015-11-23 11:01:49 -05002743 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002744 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002745 dev_err(card->dev, "ASoC: failed to init link %s\n",
2746 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002747 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002748 }
Stephen Warren5a504962011-12-21 10:40:59 -07002749 }
2750
Mark Browned77cc12011-05-03 18:25:34 +01002751 dev_set_drvdata(card->dev, card);
2752
Stephen Warren111c6412011-01-28 14:26:35 -07002753 snd_soc_initialize_card_lists(card);
2754
Mengdong Linf8f80362015-12-02 14:11:22 +08002755 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002756
Mengdong Lin1a497982015-11-18 02:34:11 -05002757 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002758 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759
Mark Browndb432b42011-10-03 21:06:40 +01002760 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002761 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002762 card->instantiated = 0;
2763 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002764 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002766 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002767}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302768EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002769
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002770static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2771{
2772 if (card->instantiated) {
2773 card->instantiated = false;
2774 snd_soc_dapm_shutdown(card);
2775 soc_cleanup_card_resources(card);
2776 if (!unregister)
2777 list_add(&card->list, &unbind_card_list);
2778 } else {
2779 if (unregister)
2780 list_del(&card->list);
2781 }
2782}
2783
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002784/**
2785 * snd_soc_unregister_card - Unregister a card with the ASoC core
2786 *
2787 * @card: Card to unregister
2788 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002789 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302790int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002791{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002792 snd_soc_unbind_card(card, true);
2793 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002794
2795 return 0;
2796}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302797EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002798
2799/*
2800 * Simplify DAI link configuration by removing ".-1" from device names
2801 * and sanitizing names.
2802 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002803static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002804{
2805 char *found, name[NAME_SIZE];
2806 int id1, id2;
2807
2808 if (dev_name(dev) == NULL)
2809 return NULL;
2810
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002811 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002812
2813 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002814 found = strstr(name, dev->driver->name);
2815 if (found) {
2816 /* get ID */
2817 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2818
2819 /* discard ID from name if ID == -1 */
2820 if (*id == -1)
2821 found[strlen(dev->driver->name)] = '\0';
2822 }
2823
2824 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002825 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002826 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2827 char tmp[NAME_SIZE];
2828
2829 /* create unique ID number from I2C addr and bus */
2830 *id = ((id1 & 0xffff) << 16) + id2;
2831
2832 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002833 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2834 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002835 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002836 } else
2837 *id = 0;
2838 }
2839
2840 return kstrdup(name, GFP_KERNEL);
2841}
2842
2843/*
2844 * Simplify DAI link naming for single devices with multiple DAIs by removing
2845 * any ".-1" and using the DAI name (instead of device name).
2846 */
2847static inline char *fmt_multiple_name(struct device *dev,
2848 struct snd_soc_dai_driver *dai_drv)
2849{
2850 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002851 dev_err(dev,
2852 "ASoC: error - multiple DAI %s registered with no name\n",
2853 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002854 return NULL;
2855 }
2856
2857 return kstrdup(dai_drv->name, GFP_KERNEL);
2858}
2859
2860/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002861 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002862 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002863 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002864 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002865static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002866{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002867 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002868
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002869 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002870 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2871 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002872 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002873 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002874 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002875 }
Mark Brown91151712008-11-30 23:31:24 +00002876}
Mark Brown91151712008-11-30 23:31:24 +00002877
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002878/* Create a DAI and add it to the component's DAI list */
2879static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2880 struct snd_soc_dai_driver *dai_drv,
2881 bool legacy_dai_naming)
2882{
2883 struct device *dev = component->dev;
2884 struct snd_soc_dai *dai;
2885
2886 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2887
2888 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2889 if (dai == NULL)
2890 return NULL;
2891
2892 /*
2893 * Back in the old days when we still had component-less DAIs,
2894 * instead of having a static name, component-less DAIs would
2895 * inherit the name of the parent device so it is possible to
2896 * register multiple instances of the DAI. We still need to keep
2897 * the same naming style even though those DAIs are not
2898 * component-less anymore.
2899 */
2900 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002901 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002902 dai->name = fmt_single_name(dev, &dai->id);
2903 } else {
2904 dai->name = fmt_multiple_name(dev, dai_drv);
2905 if (dai_drv->id)
2906 dai->id = dai_drv->id;
2907 else
2908 dai->id = component->num_dai;
2909 }
2910 if (dai->name == NULL) {
2911 kfree(dai);
2912 return NULL;
2913 }
2914
2915 dai->component = component;
2916 dai->dev = dev;
2917 dai->driver = dai_drv;
2918 if (!dai->driver->ops)
2919 dai->driver->ops = &null_dai_ops;
2920
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002921 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002922 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002923 component->num_dai++;
2924
2925 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2926 return dai;
2927}
2928
Mark Brown91151712008-11-30 23:31:24 +00002929/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002930 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002931 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002932 * @component: The component the DAIs are registered for
2933 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002934 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002935 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002936static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002937 struct snd_soc_dai_driver *dai_drv,
2938 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002939{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002940 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002941 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002942 unsigned int i;
2943 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002944
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002945 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002946
2947 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002948
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002949 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2950 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002951 if (dai == NULL) {
2952 ret = -ENOMEM;
2953 goto err;
2954 }
Mark Brown91151712008-11-30 23:31:24 +00002955 }
2956
2957 return 0;
2958
2959err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002960 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002961
2962 return ret;
2963}
Mark Brown91151712008-11-30 23:31:24 +00002964
Mengdong Lin68003e62015-12-31 16:40:43 +08002965/**
2966 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2967 *
2968 * @component: The component the DAIs are registered for
2969 * @dai_drv: DAI driver to use for the DAI
2970 *
2971 * Topology can use this API to register DAIs when probing a component.
2972 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2973 * will be freed in the component cleanup.
2974 */
2975int snd_soc_register_dai(struct snd_soc_component *component,
2976 struct snd_soc_dai_driver *dai_drv)
2977{
2978 struct snd_soc_dapm_context *dapm =
2979 snd_soc_component_get_dapm(component);
2980 struct snd_soc_dai *dai;
2981 int ret;
2982
2983 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2984 dev_err(component->dev, "Invalid dai type %d\n",
2985 dai_drv->dobj.type);
2986 return -EINVAL;
2987 }
2988
2989 lockdep_assert_held(&client_mutex);
2990 dai = soc_add_dai(component, dai_drv, false);
2991 if (!dai)
2992 return -ENOMEM;
2993
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002994 /*
2995 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002996 * also add routes that need these widgets as source or sink.
2997 */
2998 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2999 if (ret != 0) {
3000 dev_err(component->dev,
3001 "Failed to create DAI widgets %d\n", ret);
3002 }
3003
3004 return ret;
3005}
3006EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3007
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003008static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3009 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003010{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003011 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003012
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003013 component->driver->seq_notifier(component, type, subseq);
3014}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003015
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003016static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3017 int event)
3018{
3019 struct snd_soc_component *component = dapm->component;
3020
3021 return component->driver->stream_event(component, event);
3022}
3023
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003024static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3025 enum snd_soc_bias_level level)
3026{
3027 struct snd_soc_component *component = dapm->component;
3028
3029 return component->driver->set_bias_level(component, level);
3030}
3031
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003032static int snd_soc_component_initialize(struct snd_soc_component *component,
3033 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003034{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003035 struct snd_soc_dapm_context *dapm;
3036
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003037 component->name = fmt_single_name(dev, &component->id);
3038 if (!component->name) {
3039 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003040 return -ENOMEM;
3041 }
3042
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003043 component->dev = dev;
3044 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003045
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003046 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003047 dapm->dev = dev;
3048 dapm->component = component;
3049 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003050 dapm->idle_bias_off = !driver->idle_bias_on;
3051 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003052 if (driver->seq_notifier)
3053 dapm->seq_notifier = snd_soc_component_seq_notifier;
3054 if (driver->stream_event)
3055 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003056 if (driver->set_bias_level)
3057 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003058
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003059 INIT_LIST_HEAD(&component->dai_list);
3060 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003061
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003062 return 0;
3063}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003064
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003065static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003066{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003067 int val_bytes = regmap_get_val_bytes(component->regmap);
3068
3069 /* Errors are legitimate for non-integer byte multiples */
3070 if (val_bytes > 0)
3071 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003072}
3073
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003074#ifdef CONFIG_REGMAP
3075
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003076/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003077 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3078 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003079 * @component: The component for which to initialize the regmap instance
3080 * @regmap: The regmap instance that should be used by the component
3081 *
3082 * This function allows deferred assignment of the regmap instance that is
3083 * associated with the component. Only use this if the regmap instance is not
3084 * yet ready when the component is registered. The function must also be called
3085 * before the first IO attempt of the component.
3086 */
3087void snd_soc_component_init_regmap(struct snd_soc_component *component,
3088 struct regmap *regmap)
3089{
3090 component->regmap = regmap;
3091 snd_soc_component_setup_regmap(component);
3092}
3093EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3094
3095/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003096 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3097 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003098 * @component: The component for which to de-initialize the regmap instance
3099 *
3100 * Calls regmap_exit() on the regmap instance associated to the component and
3101 * removes the regmap instance from the component.
3102 *
3103 * This function should only be used if snd_soc_component_init_regmap() was used
3104 * to initialize the regmap instance.
3105 */
3106void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3107{
3108 regmap_exit(component->regmap);
3109 component->regmap = NULL;
3110}
3111EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3112
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003113#endif
3114
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003115static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003116{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003117 mutex_lock(&client_mutex);
3118
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003119 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003120 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003121 component->regmap = dev_get_regmap(component->dev,
3122 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003123 if (component->regmap)
3124 snd_soc_component_setup_regmap(component);
3125 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003126
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003127 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003128 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003129 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003130
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003131 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003132}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003133
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003134static void snd_soc_component_cleanup(struct snd_soc_component *component)
3135{
3136 snd_soc_unregister_dais(component);
3137 kfree(component->name);
3138}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003139
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003140static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3141{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003142 struct snd_soc_card *card = component->card;
3143
3144 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003145 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003146
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003147 list_del(&component->list);
3148}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003149
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003150#define ENDIANNESS_MAP(name) \
3151 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3152static u64 endianness_format_map[] = {
3153 ENDIANNESS_MAP(S16_),
3154 ENDIANNESS_MAP(U16_),
3155 ENDIANNESS_MAP(S24_),
3156 ENDIANNESS_MAP(U24_),
3157 ENDIANNESS_MAP(S32_),
3158 ENDIANNESS_MAP(U32_),
3159 ENDIANNESS_MAP(S24_3),
3160 ENDIANNESS_MAP(U24_3),
3161 ENDIANNESS_MAP(S20_3),
3162 ENDIANNESS_MAP(U20_3),
3163 ENDIANNESS_MAP(S18_3),
3164 ENDIANNESS_MAP(U18_3),
3165 ENDIANNESS_MAP(FLOAT_),
3166 ENDIANNESS_MAP(FLOAT64_),
3167 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3168};
3169
3170/*
3171 * Fix up the DAI formats for endianness: codecs don't actually see
3172 * the endianness of the data but we're using the CPU format
3173 * definitions which do need to include endianness so we ensure that
3174 * codec DAIs always have both big and little endian variants set.
3175 */
3176static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3177{
3178 int i;
3179
3180 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3181 if (stream->formats & endianness_format_map[i])
3182 stream->formats |= endianness_format_map[i];
3183}
3184
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003185static void snd_soc_try_rebind_card(void)
3186{
3187 struct snd_soc_card *card, *c;
3188
3189 if (!list_empty(&unbind_card_list)) {
3190 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3191 if (!snd_soc_bind_card(card))
3192 list_del(&card->list);
3193 }
3194 }
3195}
3196
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003197int snd_soc_add_component(struct device *dev,
3198 struct snd_soc_component *component,
3199 const struct snd_soc_component_driver *component_driver,
3200 struct snd_soc_dai_driver *dai_drv,
3201 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003202{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003203 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003204 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003205
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003206 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003207 if (ret)
3208 goto err_free;
3209
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003210 if (component_driver->endianness) {
3211 for (i = 0; i < num_dai; i++) {
3212 convert_endianness_formats(&dai_drv[i].playback);
3213 convert_endianness_formats(&dai_drv[i].capture);
3214 }
3215 }
3216
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003217 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003218 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003219 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003220 goto err_cleanup;
3221 }
3222
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003223 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003224 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003225
3226 return 0;
3227
3228err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003229 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003230err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003231 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003232}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003233EXPORT_SYMBOL_GPL(snd_soc_add_component);
3234
3235int snd_soc_register_component(struct device *dev,
3236 const struct snd_soc_component_driver *component_driver,
3237 struct snd_soc_dai_driver *dai_drv,
3238 int num_dai)
3239{
3240 struct snd_soc_component *component;
3241
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003242 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003243 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003244 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003245
3246 return snd_soc_add_component(dev, component, component_driver,
3247 dai_drv, num_dai);
3248}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003249EXPORT_SYMBOL_GPL(snd_soc_register_component);
3250
3251/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003252 * snd_soc_unregister_component - Unregister all related component
3253 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003254 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003255 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003256 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003257static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003258{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003259 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003260 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003261
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003262 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003263 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003264 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003265 continue;
3266
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003267 snd_soc_tplg_component_remove(component,
3268 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003269 snd_soc_component_del_unlocked(component);
3270 found = 1;
3271 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003272 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003273 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003274
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003275 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003276 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003277
3278 return found;
3279}
3280
3281void snd_soc_unregister_component(struct device *dev)
3282{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003283 while (__snd_soc_unregister_component(dev))
3284 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003285}
3286EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3287
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003288struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3289 const char *driver_name)
3290{
3291 struct snd_soc_component *component;
3292 struct snd_soc_component *ret;
3293
3294 ret = NULL;
3295 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003296 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003297 if (dev != component->dev)
3298 continue;
3299
3300 if (driver_name &&
3301 (driver_name != component->driver->name) &&
3302 (strcmp(component->driver->name, driver_name) != 0))
3303 continue;
3304
3305 ret = component;
3306 break;
3307 }
3308 mutex_unlock(&client_mutex);
3309
3310 return ret;
3311}
3312EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3313
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003314/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003315int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3316 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003317{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003318 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003319 int ret;
3320
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303321 if (!card->dev) {
3322 pr_err("card->dev is not set before calling %s\n", __func__);
3323 return -EINVAL;
3324 }
3325
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003326 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303327
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003328 ret = of_property_read_string_index(np, propname, 0, &card->name);
3329 /*
3330 * EINVAL means the property does not exist. This is fine providing
3331 * card->name was previously set, which is checked later in
3332 * snd_soc_register_card.
3333 */
3334 if (ret < 0 && ret != -EINVAL) {
3335 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003336 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003337 propname, ret);
3338 return ret;
3339 }
3340
3341 return 0;
3342}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003343EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003344
Xiubo Li9a6d4862014-02-08 15:59:52 +08003345static const struct snd_soc_dapm_widget simple_widgets[] = {
3346 SND_SOC_DAPM_MIC("Microphone", NULL),
3347 SND_SOC_DAPM_LINE("Line", NULL),
3348 SND_SOC_DAPM_HP("Headphone", NULL),
3349 SND_SOC_DAPM_SPK("Speaker", NULL),
3350};
3351
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003352int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003353 const char *propname)
3354{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003355 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003356 struct snd_soc_dapm_widget *widgets;
3357 const char *template, *wname;
3358 int i, j, num_widgets, ret;
3359
3360 num_widgets = of_property_count_strings(np, propname);
3361 if (num_widgets < 0) {
3362 dev_err(card->dev,
3363 "ASoC: Property '%s' does not exist\n", propname);
3364 return -EINVAL;
3365 }
3366 if (num_widgets & 1) {
3367 dev_err(card->dev,
3368 "ASoC: Property '%s' length is not even\n", propname);
3369 return -EINVAL;
3370 }
3371
3372 num_widgets /= 2;
3373 if (!num_widgets) {
3374 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3375 propname);
3376 return -EINVAL;
3377 }
3378
3379 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3380 GFP_KERNEL);
3381 if (!widgets) {
3382 dev_err(card->dev,
3383 "ASoC: Could not allocate memory for widgets\n");
3384 return -ENOMEM;
3385 }
3386
3387 for (i = 0; i < num_widgets; i++) {
3388 ret = of_property_read_string_index(np, propname,
3389 2 * i, &template);
3390 if (ret) {
3391 dev_err(card->dev,
3392 "ASoC: Property '%s' index %d read error:%d\n",
3393 propname, 2 * i, ret);
3394 return -EINVAL;
3395 }
3396
3397 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3398 if (!strncmp(template, simple_widgets[j].name,
3399 strlen(simple_widgets[j].name))) {
3400 widgets[i] = simple_widgets[j];
3401 break;
3402 }
3403 }
3404
3405 if (j >= ARRAY_SIZE(simple_widgets)) {
3406 dev_err(card->dev,
3407 "ASoC: DAPM widget '%s' is not supported\n",
3408 template);
3409 return -EINVAL;
3410 }
3411
3412 ret = of_property_read_string_index(np, propname,
3413 (2 * i) + 1,
3414 &wname);
3415 if (ret) {
3416 dev_err(card->dev,
3417 "ASoC: Property '%s' index %d read error:%d\n",
3418 propname, (2 * i) + 1, ret);
3419 return -EINVAL;
3420 }
3421
3422 widgets[i].name = wname;
3423 }
3424
Nicolin Chenf23e8602015-02-14 17:22:49 -08003425 card->of_dapm_widgets = widgets;
3426 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003427
3428 return 0;
3429}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003430EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003431
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003432int snd_soc_of_get_slot_mask(struct device_node *np,
3433 const char *prop_name,
3434 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003435{
3436 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003437 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003438 int i;
3439
3440 if (!of_slot_mask)
3441 return 0;
3442 val /= sizeof(u32);
3443 for (i = 0; i < val; i++)
3444 if (be32_to_cpup(&of_slot_mask[i]))
3445 *mask |= (1 << i);
3446
3447 return val;
3448}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003449EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003450
Xiubo Li89c67852014-02-14 09:34:35 +08003451int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003452 unsigned int *tx_mask,
3453 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003454 unsigned int *slots,
3455 unsigned int *slot_width)
3456{
3457 u32 val;
3458 int ret;
3459
Jyri Sarha61310842015-09-09 21:27:43 +03003460 if (tx_mask)
3461 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3462 if (rx_mask)
3463 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3464
Xiubo Li89c67852014-02-14 09:34:35 +08003465 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3466 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3467 if (ret)
3468 return ret;
3469
3470 if (slots)
3471 *slots = val;
3472 }
3473
3474 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3475 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3476 if (ret)
3477 return ret;
3478
3479 if (slot_width)
3480 *slot_width = val;
3481 }
3482
3483 return 0;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3486
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003487void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003488 struct snd_soc_codec_conf *codec_conf,
3489 struct device_node *of_node,
3490 const char *propname)
3491{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003492 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003493 const char *str;
3494 int ret;
3495
3496 ret = of_property_read_string(np, propname, &str);
3497 if (ret < 0) {
3498 /* no prefix is not error */
3499 return;
3500 }
3501
3502 codec_conf->of_node = of_node;
3503 codec_conf->name_prefix = str;
3504}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003505EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003506
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003507int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003508 const char *propname)
3509{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003510 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003511 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003512 struct snd_soc_dapm_route *routes;
3513 int i, ret;
3514
3515 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003516 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003517 dev_err(card->dev,
3518 "ASoC: Property '%s' does not exist or its length is not even\n",
3519 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003520 return -EINVAL;
3521 }
3522 num_routes /= 2;
3523 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003524 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003525 propname);
3526 return -EINVAL;
3527 }
3528
Kees Cooka86854d2018-06-12 14:07:58 -07003529 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003530 GFP_KERNEL);
3531 if (!routes) {
3532 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003533 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003534 return -EINVAL;
3535 }
3536
3537 for (i = 0; i < num_routes; i++) {
3538 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003539 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003540 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003541 dev_err(card->dev,
3542 "ASoC: Property '%s' index %d could not be read: %d\n",
3543 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003544 return -EINVAL;
3545 }
3546 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003547 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003548 if (ret) {
3549 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003550 "ASoC: Property '%s' index %d could not be read: %d\n",
3551 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003552 return -EINVAL;
3553 }
3554 }
3555
Nicolin Chenf23e8602015-02-14 17:22:49 -08003556 card->num_of_dapm_routes = num_routes;
3557 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003558
3559 return 0;
3560}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003561EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003562
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003563unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003564 const char *prefix,
3565 struct device_node **bitclkmaster,
3566 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003567{
3568 int ret, i;
3569 char prop[128];
3570 unsigned int format = 0;
3571 int bit, frame;
3572 const char *str;
3573 struct {
3574 char *name;
3575 unsigned int val;
3576 } of_fmt_table[] = {
3577 { "i2s", SND_SOC_DAIFMT_I2S },
3578 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3579 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3580 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3581 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3582 { "ac97", SND_SOC_DAIFMT_AC97 },
3583 { "pdm", SND_SOC_DAIFMT_PDM},
3584 { "msb", SND_SOC_DAIFMT_MSB },
3585 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003586 };
3587
3588 if (!prefix)
3589 prefix = "";
3590
3591 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003592 * check "dai-format = xxx"
3593 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003594 * SND_SOC_DAIFMT_FORMAT_MASK area
3595 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003596 ret = of_property_read_string(np, "dai-format", &str);
3597 if (ret < 0) {
3598 snprintf(prop, sizeof(prop), "%sformat", prefix);
3599 ret = of_property_read_string(np, prop, &str);
3600 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003601 if (ret == 0) {
3602 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3603 if (strcmp(str, of_fmt_table[i].name) == 0) {
3604 format |= of_fmt_table[i].val;
3605 break;
3606 }
3607 }
3608 }
3609
3610 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003611 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003612 * SND_SOC_DAIFMT_CLOCK_MASK area
3613 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003614 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003615 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003616 format |= SND_SOC_DAIFMT_CONT;
3617 else
3618 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003619
3620 /*
3621 * check "[prefix]bitclock-inversion"
3622 * check "[prefix]frame-inversion"
3623 * SND_SOC_DAIFMT_INV_MASK area
3624 */
3625 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3626 bit = !!of_get_property(np, prop, NULL);
3627
3628 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3629 frame = !!of_get_property(np, prop, NULL);
3630
3631 switch ((bit << 4) + frame) {
3632 case 0x11:
3633 format |= SND_SOC_DAIFMT_IB_IF;
3634 break;
3635 case 0x10:
3636 format |= SND_SOC_DAIFMT_IB_NF;
3637 break;
3638 case 0x01:
3639 format |= SND_SOC_DAIFMT_NB_IF;
3640 break;
3641 default:
3642 /* SND_SOC_DAIFMT_NB_NF is default */
3643 break;
3644 }
3645
3646 /*
3647 * check "[prefix]bitclock-master"
3648 * check "[prefix]frame-master"
3649 * SND_SOC_DAIFMT_MASTER_MASK area
3650 */
3651 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3652 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003653 if (bit && bitclkmaster)
3654 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003655
3656 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3657 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003658 if (frame && framemaster)
3659 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003660
3661 switch ((bit << 4) + frame) {
3662 case 0x11:
3663 format |= SND_SOC_DAIFMT_CBM_CFM;
3664 break;
3665 case 0x10:
3666 format |= SND_SOC_DAIFMT_CBM_CFS;
3667 break;
3668 case 0x01:
3669 format |= SND_SOC_DAIFMT_CBS_CFM;
3670 break;
3671 default:
3672 format |= SND_SOC_DAIFMT_CBS_CFS;
3673 break;
3674 }
3675
3676 return format;
3677}
3678EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3679
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003680int snd_soc_get_dai_id(struct device_node *ep)
3681{
3682 struct snd_soc_component *pos;
3683 struct device_node *node;
3684 int ret;
3685
3686 node = of_graph_get_port_parent(ep);
3687
3688 /*
3689 * For example HDMI case, HDMI has video/sound port,
3690 * but ALSA SoC needs sound port number only.
3691 * Thus counting HDMI DT port/endpoint doesn't work.
3692 * Then, it should have .of_xlate_dai_id
3693 */
3694 ret = -ENOTSUPP;
3695 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003696 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003697 struct device_node *component_of_node = pos->dev->of_node;
3698
3699 if (!component_of_node && pos->dev->parent)
3700 component_of_node = pos->dev->parent->of_node;
3701
3702 if (component_of_node != node)
3703 continue;
3704
3705 if (pos->driver->of_xlate_dai_id)
3706 ret = pos->driver->of_xlate_dai_id(pos, ep);
3707
3708 break;
3709 }
3710 mutex_unlock(&client_mutex);
3711
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003712 of_node_put(node);
3713
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003714 return ret;
3715}
3716EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3717
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003718int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003719 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003720{
3721 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003722 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003723 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003724
3725 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003726 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003727 component_of_node = pos->dev->of_node;
3728 if (!component_of_node && pos->dev->parent)
3729 component_of_node = pos->dev->parent->of_node;
3730
3731 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003732 continue;
3733
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003734 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003735 ret = pos->driver->of_xlate_dai_name(pos,
3736 args,
3737 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003738 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003739 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003740 int id = -1;
3741
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003742 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003743 case 0:
3744 id = 0; /* same as dai_drv[0] */
3745 break;
3746 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003747 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003748 break;
3749 default:
3750 /* not supported */
3751 break;
3752 }
3753
3754 if (id < 0 || id >= pos->num_dai) {
3755 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003756 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003757 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003758
3759 ret = 0;
3760
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003761 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003762 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003763 if (id == 0)
3764 break;
3765 id--;
3766 }
3767
3768 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003769 if (!*dai_name)
3770 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003771 }
3772
Kuninori Morimotocb470082013-09-10 17:39:56 -07003773 break;
3774 }
3775 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003776 return ret;
3777}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003778EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003779
3780int snd_soc_of_get_dai_name(struct device_node *of_node,
3781 const char **dai_name)
3782{
3783 struct of_phandle_args args;
3784 int ret;
3785
3786 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3787 "#sound-dai-cells", 0, &args);
3788 if (ret)
3789 return ret;
3790
3791 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003792
3793 of_node_put(args.np);
3794
3795 return ret;
3796}
3797EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3798
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003799/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003800 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3801 * @dai_link: DAI link
3802 *
3803 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3804 */
3805void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3806{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003807 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003808 int index;
3809
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003810 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003811 if (!component->of_node)
3812 break;
3813 of_node_put(component->of_node);
3814 component->of_node = NULL;
3815 }
3816}
3817EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3818
3819/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003820 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3821 * @dev: Card device
3822 * @of_node: Device node
3823 * @dai_link: DAI link
3824 *
3825 * Builds an array of CODEC DAI components from the DAI link property
3826 * 'sound-dai'.
3827 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003828 * The device nodes in the array (of_node) must be dereferenced by calling
3829 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003830 *
3831 * Returns 0 for success
3832 */
3833int snd_soc_of_get_dai_link_codecs(struct device *dev,
3834 struct device_node *of_node,
3835 struct snd_soc_dai_link *dai_link)
3836{
3837 struct of_phandle_args args;
3838 struct snd_soc_dai_link_component *component;
3839 char *name;
3840 int index, num_codecs, ret;
3841
3842 /* Count the number of CODECs */
3843 name = "sound-dai";
3844 num_codecs = of_count_phandle_with_args(of_node, name,
3845 "#sound-dai-cells");
3846 if (num_codecs <= 0) {
3847 if (num_codecs == -ENOENT)
3848 dev_err(dev, "No 'sound-dai' property\n");
3849 else
3850 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3851 return num_codecs;
3852 }
Kees Cooka86854d2018-06-12 14:07:58 -07003853 component = devm_kcalloc(dev,
3854 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003855 GFP_KERNEL);
3856 if (!component)
3857 return -ENOMEM;
3858 dai_link->codecs = component;
3859 dai_link->num_codecs = num_codecs;
3860
3861 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003862 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003863 ret = of_parse_phandle_with_args(of_node, name,
3864 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003865 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003866 if (ret)
3867 goto err;
3868 component->of_node = args.np;
3869 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3870 if (ret < 0)
3871 goto err;
3872 }
3873 return 0;
3874err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003875 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003876 dai_link->codecs = NULL;
3877 dai_link->num_codecs = 0;
3878 return ret;
3879}
3880EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3881
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003882static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003883{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003884 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003885 snd_soc_util_init();
3886
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003887 return platform_driver_register(&soc_driver);
3888}
Mark Brown4abe8e12010-10-12 17:41:03 +01003889module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003890
Mark Brown7d8c16a2008-11-30 22:11:24 +00003891static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003892{
Mark Brownfb257892011-04-28 10:57:54 +01003893 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003894 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003895
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003896 platform_driver_unregister(&soc_driver);
3897}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003898module_exit(snd_soc_exit);
3899
3900/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003901MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003902MODULE_DESCRIPTION("ALSA SoC Core");
3903MODULE_LICENSE("GPL");
3904MODULE_ALIAS("platform:soc-audio");