blob: de2851f1b3df3a996770c2ac25578902f0bdef55 [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;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000745 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200746 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]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100906 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
907 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500908 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200909 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000910 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100912
Benoit Cousson88bd8702014-07-08 23:19:34 +0200913 /* Single codec links expect codec and codec_dai in runtime data */
914 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100915
Mark Brownb19e6e72012-03-14 21:18:39 +0000916 /* find one from the set of registered platforms */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000917 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000918 if (!snd_soc_is_matching_component(dai_link->platform,
919 component))
920 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000921
922 snd_soc_rtdcom_add(rtd, component);
923 }
924
Mengdong Lin1a497982015-11-18 02:34:11 -0500925 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000926 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500927
928_err_defer:
929 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200930 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931}
932
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200933static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600934{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200935 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200936 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600937
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000938 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600939
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000940 if (component->driver->remove)
941 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600942
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200943 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600944
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200945 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200946 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200947 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600948}
949
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200950static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200951{
952 int err;
953
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000954 if (!dai || !dai->probed ||
955 dai->driver->remove_order != order)
956 return;
957
958 if (dai->driver->remove) {
959 err = dai->driver->remove(dai);
960 if (err < 0)
961 dev_err(dai->dev,
962 "ASoC: failed to remove %s: %d\n",
963 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100964 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000965 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100966}
967
Mengdong Lin1a497982015-11-18 02:34:11 -0500968static void soc_remove_link_dais(struct snd_soc_card *card,
969 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000972 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
974 /* unregister the rtd device */
975 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800976 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977 rtd->dev_registered = 0;
978 }
979
980 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000981 for_each_rtd_codec_dai(rtd, i, codec_dai)
982 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200984 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985}
986
Mengdong Lin1a497982015-11-18 02:34:11 -0500987static void soc_remove_link_components(struct snd_soc_card *card,
988 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200990 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000991 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600992
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000993 for_each_rtdcom(rtd, rtdcom) {
994 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600995
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200996 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200997 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600999}
1000
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001001static void soc_remove_dai_links(struct snd_soc_card *card)
1002{
Mengdong Lin1a497982015-11-18 02:34:11 -05001003 int order;
1004 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001005 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001006
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001007 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001008 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001009 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001010 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001011
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001012 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001013 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001014 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001015 }
1016
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001017 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001018 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1019 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1020 link->name);
1021
1022 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001023 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001024}
1025
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001026static int snd_soc_init_platform(struct snd_soc_card *card,
1027 struct snd_soc_dai_link *dai_link)
1028{
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001029 struct snd_soc_dai_link_component *platform = dai_link->platform;
1030
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001031 /*
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001032 * REMOVE ME
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001033 *
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001034 * This is glue code for Legacy vs Modern dai_link.
1035 * This function will be removed if all derivers are switched to
1036 * modern style dai_link.
1037 * Driver shouldn't use both legacy and modern style in the same time.
1038 * see
1039 * soc.h :: struct snd_soc_dai_link
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001040 */
1041 /* convert Legacy platform link */
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001042 if (!platform || dai_link->legacy_platform) {
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001043 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001044 sizeof(struct snd_soc_dai_link_component),
1045 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001046 if (!platform)
1047 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001048
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001049 dai_link->platform = platform;
1050 dai_link->legacy_platform = 1;
1051 platform->name = dai_link->platform_name;
1052 platform->of_node = dai_link->platform_of_node;
1053 platform->dai_name = NULL;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001054 }
1055
1056 /* if there's no platform we match on the empty platform */
1057 if (!platform->name &&
1058 !platform->of_node)
1059 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001060
1061 return 0;
1062}
1063
Mengdong Lin923c5e612015-11-23 11:01:49 -05001064static int snd_soc_init_multicodec(struct snd_soc_card *card,
1065 struct snd_soc_dai_link *dai_link)
1066{
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001067 /*
1068 * REMOVE ME
1069 *
1070 * This is glue code for Legacy vs Modern dai_link.
1071 * This function will be removed if all derivers are switched to
1072 * modern style dai_link.
1073 * Driver shouldn't use both legacy and modern style in the same time.
1074 * see
1075 * soc.h :: struct snd_soc_dai_link
1076 */
1077
Mengdong Lin923c5e612015-11-23 11:01:49 -05001078 /* Legacy codec/codec_dai link is a single entry in multicodec */
1079 if (dai_link->codec_name || dai_link->codec_of_node ||
1080 dai_link->codec_dai_name) {
1081 dai_link->num_codecs = 1;
1082
1083 dai_link->codecs = devm_kzalloc(card->dev,
1084 sizeof(struct snd_soc_dai_link_component),
1085 GFP_KERNEL);
1086 if (!dai_link->codecs)
1087 return -ENOMEM;
1088
1089 dai_link->codecs[0].name = dai_link->codec_name;
1090 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1091 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1092 }
1093
1094 if (!dai_link->codecs) {
1095 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1096 return -EINVAL;
1097 }
1098
1099 return 0;
1100}
1101
1102static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001103 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001104{
1105 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001106 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001107
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001108 ret = snd_soc_init_platform(card, link);
1109 if (ret) {
1110 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1111 return ret;
1112 }
1113
Mengdong Lin923c5e612015-11-23 11:01:49 -05001114 ret = snd_soc_init_multicodec(card, link);
1115 if (ret) {
1116 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1117 return ret;
1118 }
1119
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001120 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001121 /*
1122 * Codec must be specified by 1 of name or OF node,
1123 * not both or neither.
1124 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001125 if (!!codec->name ==
1126 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001127 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1128 link->name);
1129 return -EINVAL;
1130 }
1131 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001132 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001133 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1134 link->name);
1135 return -EINVAL;
1136 }
1137 }
1138
1139 /*
1140 * Platform may be specified by either name or OF node, but
1141 * can be left unspecified, and a dummy platform will be used.
1142 */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001143 if (link->platform->name && link->platform->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001144 dev_err(card->dev,
1145 "ASoC: Both platform name/of_node are set for %s\n",
1146 link->name);
1147 return -EINVAL;
1148 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301149
1150 /*
1151 * Defer card registartion if platform dai component is not added to
1152 * component list.
1153 */
Matthias Reichl28335482019-01-15 17:51:07 +01001154 if ((link->platform->of_node || link->platform->name) &&
1155 !soc_find_component(link->platform->of_node, link->platform->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301156 return -EPROBE_DEFER;
1157
Mengdong Lin923c5e612015-11-23 11:01:49 -05001158 /*
1159 * CPU device may be specified by either name or OF node, but
1160 * can be left unspecified, and will be matched based on DAI
1161 * name alone..
1162 */
1163 if (link->cpu_name && link->cpu_of_node) {
1164 dev_err(card->dev,
1165 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1166 link->name);
1167 return -EINVAL;
1168 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301169
1170 /*
1171 * Defer card registartion if cpu dai component is not added to
1172 * component list.
1173 */
Matthias Reichl28335482019-01-15 17:51:07 +01001174 if ((link->cpu_of_node || link->cpu_name) &&
1175 !soc_find_component(link->cpu_of_node, link->cpu_name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301176 return -EPROBE_DEFER;
1177
Mengdong Lin923c5e612015-11-23 11:01:49 -05001178 /*
1179 * At least one of CPU DAI name or CPU device name/node must be
1180 * specified
1181 */
1182 if (!link->cpu_dai_name &&
1183 !(link->cpu_name || link->cpu_of_node)) {
1184 dev_err(card->dev,
1185 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1186 link->name);
1187 return -EINVAL;
1188 }
1189
1190 return 0;
1191}
1192
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001193void snd_soc_disconnect_sync(struct device *dev)
1194{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001195 struct snd_soc_component *component =
1196 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001197
1198 if (!component || !component->card)
1199 return;
1200
1201 snd_card_disconnect_sync(component->card->snd_card);
1202}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001203EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001204
Mengdong Linf8f80362015-12-02 14:11:22 +08001205/**
1206 * snd_soc_add_dai_link - Add a DAI link dynamically
1207 * @card: The ASoC card to which the DAI link is added
1208 * @dai_link: The new DAI link to add
1209 *
1210 * This function adds a DAI link to the ASoC card's link list.
1211 *
1212 * Note: Topology can use this API to add DAI links when probing the
1213 * topology component. And machine drivers can still define static
1214 * DAI links in dai_link array.
1215 */
1216int snd_soc_add_dai_link(struct snd_soc_card *card,
1217 struct snd_soc_dai_link *dai_link)
1218{
1219 if (dai_link->dobj.type
1220 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1221 dev_err(card->dev, "Invalid dai link type %d\n",
1222 dai_link->dobj.type);
1223 return -EINVAL;
1224 }
1225
1226 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001227 /*
1228 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001229 * on the link created by topology.
1230 */
1231 if (dai_link->dobj.type && card->add_dai_link)
1232 card->add_dai_link(card, dai_link);
1233
Mengdong Linf8f80362015-12-02 14:11:22 +08001234 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001235
1236 return 0;
1237}
1238EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1239
1240/**
1241 * snd_soc_remove_dai_link - Remove a DAI link from the list
1242 * @card: The ASoC card that owns the link
1243 * @dai_link: The DAI link to remove
1244 *
1245 * This function removes a DAI link from the ASoC card's link list.
1246 *
1247 * For DAI links previously added by topology, topology should
1248 * remove them by using the dobj embedded in the link.
1249 */
1250void snd_soc_remove_dai_link(struct snd_soc_card *card,
1251 struct snd_soc_dai_link *dai_link)
1252{
1253 struct snd_soc_dai_link *link, *_link;
1254
1255 if (dai_link->dobj.type
1256 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1257 dev_err(card->dev, "Invalid dai link type %d\n",
1258 dai_link->dobj.type);
1259 return;
1260 }
1261
1262 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001263 /*
1264 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001265 * on the link created by topology.
1266 */
1267 if (dai_link->dobj.type && card->remove_dai_link)
1268 card->remove_dai_link(card, dai_link);
1269
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001270 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001271 if (link == dai_link) {
1272 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001273 return;
1274 }
1275 }
1276}
1277EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1278
Jerome Brunetaefba452018-07-13 14:50:43 +02001279static void soc_set_of_name_prefix(struct snd_soc_component *component)
1280{
1281 struct device_node *component_of_node = component->dev->of_node;
1282 const char *str;
1283 int ret;
1284
1285 if (!component_of_node && component->dev->parent)
1286 component_of_node = component->dev->parent->of_node;
1287
1288 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1289 &str);
1290 if (!ret)
1291 component->name_prefix = str;
1292}
1293
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001294static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001295 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001296{
1297 int i;
1298
Jerome Brunetaefba452018-07-13 14:50:43 +02001299 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001300 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001301 struct device_node *component_of_node = component->dev->of_node;
1302
1303 if (!component_of_node && component->dev->parent)
1304 component_of_node = component->dev->parent->of_node;
1305
1306 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001307 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001308 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001309 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001310 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001311 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001312 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001313
1314 /*
1315 * If there is no configuration table or no match in the table,
1316 * check if a prefix is provided in the node
1317 */
1318 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001319}
1320
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001321static int soc_probe_component(struct snd_soc_card *card,
1322 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001323{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001324 struct snd_soc_dapm_context *dapm =
1325 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001326 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001327 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001329 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001330 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001331
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001332 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001333 if (component->card != card) {
1334 dev_err(component->dev,
1335 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1336 card->name, component->card->name);
1337 return -ENODEV;
1338 }
1339 return 0;
1340 }
1341
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001342 if (!try_module_get(component->dev->driver->owner))
1343 return -ENODEV;
1344
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001345 component->card = card;
1346 dapm->card = card;
1347 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001348
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001349 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001350
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001351 if (component->driver->dapm_widgets) {
1352 ret = snd_soc_dapm_new_controls(dapm,
1353 component->driver->dapm_widgets,
1354 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001355
1356 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001357 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001358 "Failed to create new controls %d\n", ret);
1359 goto err_probe;
1360 }
1361 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001362
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001363 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001364 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001365 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001366 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001367 "Failed to create DAI widgets %d\n", ret);
1368 goto err_probe;
1369 }
1370 }
Mark Brown888df392012-02-16 19:37:51 -08001371
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001372 if (component->driver->probe) {
1373 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001374 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001375 dev_err(component->dev,
1376 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001377 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001378 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001379
1380 WARN(dapm->idle_bias_off &&
1381 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001382 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001383 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001384 }
1385
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001386 /* machine specific init */
1387 if (component->init) {
1388 ret = component->init(component);
1389 if (ret < 0) {
1390 dev_err(component->dev,
1391 "Failed to do machine specific init %d\n", ret);
1392 goto err_probe;
1393 }
1394 }
1395
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001396 if (component->driver->controls)
1397 snd_soc_add_component_controls(component,
1398 component->driver->controls,
1399 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001400 if (component->driver->dapm_routes)
1401 snd_soc_dapm_add_routes(dapm,
1402 component->driver->dapm_routes,
1403 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001404
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001405 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001406 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001407 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001408
Jarkko Nikula70d293312011-01-27 16:24:22 +02001409 return 0;
1410
1411err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001412 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001413 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001414 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001415
1416 return ret;
1417}
1418
Mark Brown36ae1a92012-01-06 17:12:45 -08001419static void rtd_release(struct device *dev)
1420{
1421 kfree(dev);
1422}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001424static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1425 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001426{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001427 int ret = 0;
1428
Jarkko Nikula589c3562010-12-06 16:27:07 +02001429 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001430 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1431 if (!rtd->dev)
1432 return -ENOMEM;
1433 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001434 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001435 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001436 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001437 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001438 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001439 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001440 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1441 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1442 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1443 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001444 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001445 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001446 /* calling put_device() here to free the rtd->dev */
1447 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001448 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001449 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001450 return ret;
1451 }
1452 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001453 return 0;
1454}
1455
Mengdong Lin1a497982015-11-18 02:34:11 -05001456static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001457 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001458{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001459 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001460 struct snd_soc_rtdcom_list *rtdcom;
1461 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001462
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001463 for_each_rtdcom(rtd, rtdcom) {
1464 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001465
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001466 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001467 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001468 if (ret < 0)
1469 return ret;
1470 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001471 }
1472
Stephen Warren62ae68f2012-06-08 12:34:23 -06001473 return 0;
1474}
1475
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001476static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001477{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001478 if (dai->probed ||
1479 dai->driver->probe_order != order)
1480 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001481
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001482 if (dai->driver->probe) {
1483 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001484
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001485 if (ret < 0) {
1486 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1487 dai->name, ret);
1488 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001489 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001490 }
1491
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001492 dai->probed = 1;
1493
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001494 return 0;
1495}
1496
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001497static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1498 struct snd_soc_pcm_runtime *rtd)
1499{
1500 int i, ret = 0;
1501
1502 for (i = 0; i < num_dais; ++i) {
1503 struct snd_soc_dai_driver *drv = dais[i]->driver;
1504
Rohit kumarde17f142018-11-01 18:08:49 +05301505 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001506 ret = drv->pcm_new(rtd, dais[i]);
1507 if (ret < 0) {
1508 dev_err(dais[i]->dev,
1509 "ASoC: Failed to bind %s with pcm device\n",
1510 dais[i]->name);
1511 return ret;
1512 }
1513 }
1514
1515 return 0;
1516}
1517
Mengdong Lin1a497982015-11-18 02:34:11 -05001518static int soc_probe_link_dais(struct snd_soc_card *card,
1519 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520{
Mengdong Lin1a497982015-11-18 02:34:11 -05001521 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001523 struct snd_soc_rtdcom_list *rtdcom;
1524 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001525 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001526 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001528 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001529 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001531 /* set default power off timeout */
1532 rtd->pmdown_time = pmdown_time;
1533
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001534 ret = soc_probe_dai(cpu_dai, order);
1535 if (ret)
1536 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001539 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1540 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001541 if (ret)
1542 return ret;
1543 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001544
Liam Girdwood0168bf02011-06-07 16:08:05 +01001545 /* complete DAI probe during last probe */
1546 if (order != SND_SOC_COMP_ORDER_LAST)
1547 return 0;
1548
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001549 /* do machine specific initialization */
1550 if (dai_link->init) {
1551 ret = dai_link->init(rtd);
1552 if (ret < 0) {
1553 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1554 dai_link->name, ret);
1555 return ret;
1556 }
1557 }
1558
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001559 if (dai_link->dai_fmt)
1560 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1561
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001562 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001563 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001564 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001565
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001566#ifdef CONFIG_DEBUG_FS
1567 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001568 if (dai_link->dynamic)
1569 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001570#endif
1571
Liam Girdwooda655de82018-07-02 16:59:54 +01001572 num = rtd->num;
1573
1574 /*
1575 * most drivers will register their PCMs using DAI link ordering but
1576 * topology based drivers can use the DAI link id field to set PCM
1577 * device number and then use rtd + a base offset of the BEs.
1578 */
1579 for_each_rtdcom(rtd, rtdcom) {
1580 component = rtdcom->component;
1581
1582 if (!component->driver->use_dai_pcm_id)
1583 continue;
1584
1585 if (rtd->dai_link->no_pcm)
1586 num += component->driver->be_pcm_base;
1587 else
1588 num = rtd->dai_link->id;
1589 }
1590
Jie Yang6f0c4222015-10-13 23:41:00 +08001591 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001592 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001593 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001594 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001595 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301596 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001597 return ret;
1598 }
1599 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301600
1601 if (!dai_link->params) {
1602 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001603 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301604 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001605 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001606 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001607 return ret;
1608 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001609 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1610 if (ret < 0)
1611 return ret;
1612 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1613 rtd->num_codecs, rtd);
1614 if (ret < 0)
1615 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301616 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001617 INIT_DELAYED_WORK(&rtd->delayed_work,
1618 codec2codec_close_delayed_work);
Mark Brownc74184e2012-04-04 22:12:09 +01001619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620 }
1621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622 return 0;
1623}
1624
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001625static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001626{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001627 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001628 struct snd_soc_component *component;
1629 const char *name;
1630 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001631
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001632 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1633 /* codecs, usually analog devices */
1634 name = aux_dev->codec_name;
1635 codec_of_node = aux_dev->codec_of_node;
1636 component = soc_find_component(codec_of_node, name);
1637 if (!component) {
1638 if (codec_of_node)
1639 name = of_node_full_name(codec_of_node);
1640 goto err_defer;
1641 }
1642 } else if (aux_dev->name) {
1643 /* generic components */
1644 name = aux_dev->name;
1645 component = soc_find_component(NULL, name);
1646 if (!component)
1647 goto err_defer;
1648 } else {
1649 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1650 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001651 }
1652
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001653 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001654 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001655
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001656 return 0;
1657
1658err_defer:
1659 dev_err(card->dev, "ASoC: %s not registered\n", name);
1660 return -EPROBE_DEFER;
1661}
1662
1663static int soc_probe_aux_devices(struct snd_soc_card *card)
1664{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001665 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001666 int order;
1667 int ret;
1668
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001669 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001670 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001671 if (comp->driver->probe_order == order) {
1672 ret = soc_probe_component(card, comp);
1673 if (ret < 0) {
1674 dev_err(card->dev,
1675 "ASoC: failed to probe aux component %s %d\n",
1676 comp->name, ret);
1677 return ret;
1678 }
1679 }
1680 }
1681 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001682
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001683 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001684}
1685
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001686static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001687{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001688 struct snd_soc_component *comp, *_comp;
1689 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001690
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001691 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001692 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001693 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001694
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001695 if (comp->driver->remove_order == order) {
1696 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001697 /* remove it from the card's aux_comp_list */
1698 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001699 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001700 }
1701 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702}
1703
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001704/**
1705 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1706 * @rtd: The runtime for which the DAI link format should be changed
1707 * @dai_fmt: The new DAI link format
1708 *
1709 * This function updates the DAI link format for all DAIs connected to the DAI
1710 * link for the specified runtime.
1711 *
1712 * Note: For setups with a static format set the dai_fmt field in the
1713 * corresponding snd_dai_link struct instead of using this function.
1714 *
1715 * Returns 0 on success, otherwise a negative error code.
1716 */
1717int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1718 unsigned int dai_fmt)
1719{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001721 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001722 unsigned int i;
1723 int ret;
1724
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001725 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001726 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1727 if (ret != 0 && ret != -ENOTSUPP) {
1728 dev_warn(codec_dai->dev,
1729 "ASoC: Failed to set DAI format: %d\n", ret);
1730 return ret;
1731 }
1732 }
1733
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001734 /*
1735 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1736 * the component which has non_legacy_dai_naming is Codec
1737 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001738 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001739 unsigned int inv_dai_fmt;
1740
1741 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1742 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1743 case SND_SOC_DAIFMT_CBM_CFM:
1744 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1745 break;
1746 case SND_SOC_DAIFMT_CBM_CFS:
1747 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1748 break;
1749 case SND_SOC_DAIFMT_CBS_CFM:
1750 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1751 break;
1752 case SND_SOC_DAIFMT_CBS_CFS:
1753 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1754 break;
1755 }
1756
1757 dai_fmt = inv_dai_fmt;
1758 }
1759
1760 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1761 if (ret != 0 && ret != -ENOTSUPP) {
1762 dev_warn(cpu_dai->dev,
1763 "ASoC: Failed to set DAI format: %d\n", ret);
1764 return ret;
1765 }
1766
1767 return 0;
1768}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001769EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001770
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001771#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001772/*
1773 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001774 * separate different DMI fields in the card long name. Only number and
1775 * alphabet characters and a few separator characters are kept.
1776 */
1777static void cleanup_dmi_name(char *name)
1778{
1779 int i, j = 0;
1780
1781 for (i = 0; name[i]; i++) {
1782 if (isalnum(name[i]) || (name[i] == '.')
1783 || (name[i] == '_'))
1784 name[j++] = name[i];
1785 else if (name[i] == '-')
1786 name[j++] = '_';
1787 }
1788
1789 name[j] = '\0';
1790}
1791
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001792/*
1793 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001794 * in the black list.
1795 */
1796static int is_dmi_valid(const char *field)
1797{
1798 int i = 0;
1799
1800 while (dmi_blacklist[i]) {
1801 if (strstr(field, dmi_blacklist[i]))
1802 return 0;
1803 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001804 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001805
1806 return 1;
1807}
1808
Liam Girdwood345233d2017-01-14 16:13:02 +08001809/**
1810 * snd_soc_set_dmi_name() - Register DMI names to card
1811 * @card: The card to register DMI names
1812 * @flavour: The flavour "differentiator" for the card amongst its peers.
1813 *
1814 * An Intel machine driver may be used by many different devices but are
1815 * difficult for userspace to differentiate, since machine drivers ususally
1816 * use their own name as the card short name and leave the card long name
1817 * blank. To differentiate such devices and fix bugs due to lack of
1818 * device-specific configurations, this function allows DMI info to be used
1819 * as the sound card long name, in the format of
1820 * "vendor-product-version-board"
1821 * (Character '-' is used to separate different DMI fields here).
1822 * This will help the user space to load the device-specific Use Case Manager
1823 * (UCM) configurations for the card.
1824 *
1825 * Possible card long names may be:
1826 * DellInc.-XPS139343-01-0310JH
1827 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1828 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1829 *
1830 * This function also supports flavoring the card longname to provide
1831 * the extra differentiation, like "vendor-product-version-board-flavor".
1832 *
1833 * We only keep number and alphabet characters and a few separator characters
1834 * in the card long name since UCM in the user space uses the card long names
1835 * as card configuration directory names and AudoConf cannot support special
1836 * charactors like SPACE.
1837 *
1838 * Returns 0 on success, otherwise a negative error code.
1839 */
1840int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1841{
1842 const char *vendor, *product, *product_version, *board;
1843 size_t longname_buf_size = sizeof(card->snd_card->longname);
1844 size_t len;
1845
1846 if (card->long_name)
1847 return 0; /* long name already set by driver or from DMI */
1848
1849 /* make up dmi long name as: vendor.product.version.board */
1850 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001851 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001852 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1853 return 0;
1854 }
1855
1856 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1857 "%s", vendor);
1858 cleanup_dmi_name(card->dmi_longname);
1859
1860 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001861 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001862 len = strlen(card->dmi_longname);
1863 snprintf(card->dmi_longname + len,
1864 longname_buf_size - len,
1865 "-%s", product);
1866
1867 len++; /* skip the separator "-" */
1868 if (len < longname_buf_size)
1869 cleanup_dmi_name(card->dmi_longname + len);
1870
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001871 /*
1872 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001873 * name in the product version field
1874 */
1875 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001876 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001877 len = strlen(card->dmi_longname);
1878 snprintf(card->dmi_longname + len,
1879 longname_buf_size - len,
1880 "-%s", product_version);
1881
1882 len++;
1883 if (len < longname_buf_size)
1884 cleanup_dmi_name(card->dmi_longname + len);
1885 }
1886 }
1887
1888 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001889 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001890 len = strlen(card->dmi_longname);
1891 snprintf(card->dmi_longname + len,
1892 longname_buf_size - len,
1893 "-%s", board);
1894
1895 len++;
1896 if (len < longname_buf_size)
1897 cleanup_dmi_name(card->dmi_longname + len);
1898 } else if (!product) {
1899 /* fall back to using legacy name */
1900 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1901 return 0;
1902 }
1903
1904 /* Add flavour to dmi long name */
1905 if (flavour) {
1906 len = strlen(card->dmi_longname);
1907 snprintf(card->dmi_longname + len,
1908 longname_buf_size - len,
1909 "-%s", flavour);
1910
1911 len++;
1912 if (len < longname_buf_size)
1913 cleanup_dmi_name(card->dmi_longname + len);
1914 }
1915
1916 /* set the card long name */
1917 card->long_name = card->dmi_longname;
1918
1919 return 0;
1920}
1921EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001922#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001923
Liam Girdwooda655de82018-07-02 16:59:54 +01001924static void soc_check_tplg_fes(struct snd_soc_card *card)
1925{
1926 struct snd_soc_component *component;
1927 const struct snd_soc_component_driver *comp_drv;
1928 struct snd_soc_dai_link *dai_link;
1929 int i;
1930
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001931 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001932
1933 /* does this component override FEs ? */
1934 if (!component->driver->ignore_machine)
1935 continue;
1936
1937 /* for this machine ? */
1938 if (strcmp(component->driver->ignore_machine,
1939 card->dev->driver->name))
1940 continue;
1941
1942 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001943 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001944
1945 /* ignore this FE */
1946 if (dai_link->dynamic) {
1947 dai_link->ignore = true;
1948 continue;
1949 }
1950
1951 dev_info(card->dev, "info: override FE DAI link %s\n",
1952 card->dai_link[i].name);
1953
1954 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001955 if (snd_soc_init_platform(card, dai_link) < 0) {
1956 dev_err(card->dev, "init platform error");
1957 continue;
1958 }
1959 dai_link->platform->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001960
1961 /* convert non BE into BE */
1962 dai_link->no_pcm = 1;
1963
1964 /* override any BE fixups */
1965 dai_link->be_hw_params_fixup =
1966 component->driver->be_hw_params_fixup;
1967
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001968 /*
1969 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001970 * dai link name if it's NULL to help bind widgets.
1971 */
1972 if (!dai_link->stream_name)
1973 dai_link->stream_name = dai_link->name;
1974 }
1975
1976 /* Inform userspace we are using alternate topology */
1977 if (component->driver->topology_name_prefix) {
1978
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001979 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001980 if (!card->topology_shortname_created) {
1981 comp_drv = component->driver;
1982
1983 snprintf(card->topology_shortname, 32, "%s-%s",
1984 comp_drv->topology_name_prefix,
1985 card->name);
1986 card->topology_shortname_created = true;
1987 }
1988
1989 /* use topology shortname */
1990 card->name = card->topology_shortname;
1991 }
1992 }
1993}
1994
Mark Brownb19e6e72012-03-14 21:18:39 +00001995static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996{
Mengdong Lin1a497982015-11-18 02:34:11 -05001997 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001998 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001999 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002001 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002002 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002003
Liam Girdwooda655de82018-07-02 16:59:54 +01002004 /* check whether any platform is ignore machine FE and using topology */
2005 soc_check_tplg_fes(card);
2006
Mark Brownb19e6e72012-03-14 21:18:39 +00002007 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002008 for_each_card_prelinks(card, i, dai_link) {
2009 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002010 if (ret != 0)
2011 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 }
2013
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002014 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002015 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002016 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002017 if (ret != 0)
2018 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002019 }
2020
Mengdong Linf8f80362015-12-02 14:11:22 +08002021 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002022 for_each_card_prelinks(card, i, dai_link)
2023 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002025 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002026 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 card->owner, 0, &card->snd_card);
2028 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002029 dev_err(card->dev,
2030 "ASoC: can't create sound card for card %s: %d\n",
2031 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002032 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002033 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002035 soc_init_card_debugfs(card);
2036
Mark Browne37a4972011-03-02 18:21:57 +00002037 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2038 card->dapm.dev = card->dev;
2039 card->dapm.card = card;
2040 list_add(&card->dapm.list, &card->dapm_list);
2041
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002042#ifdef CONFIG_DEBUG_FS
2043 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2044#endif
2045
Mark Brown88ee1c62011-02-02 10:43:26 +00002046#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002047 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002048 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002049#endif
Andy Green6ed25972008-06-13 16:24:05 +01002050
Mark Brown9a841eb2011-04-12 17:51:37 -07002051 if (card->dapm_widgets)
2052 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2053 card->num_dapm_widgets);
2054
Nicolin Chenf23e8602015-02-14 17:22:49 -08002055 if (card->of_dapm_widgets)
2056 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2057 card->num_of_dapm_widgets);
2058
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059 /* initialise the sound card only once */
2060 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002061 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002062 if (ret < 0)
2063 goto card_probe_error;
2064 }
2065
Stephen Warren62ae68f2012-06-08 12:34:23 -06002066 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002067 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002068 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002069 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002070 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002071 dev_err(card->dev,
2072 "ASoC: failed to instantiate card %d\n",
2073 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002074 goto probe_dai_err;
2075 }
2076 }
2077 }
2078
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002079 /* probe auxiliary components */
2080 ret = soc_probe_aux_devices(card);
2081 if (ret < 0)
2082 goto probe_dai_err;
2083
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002084 /*
2085 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002086 * Components with topology may bring new DAIs and DAI links.
2087 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002088 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002089 if (soc_is_dai_link_bound(card, dai_link))
2090 continue;
2091
2092 ret = soc_init_dai_link(card, dai_link);
2093 if (ret)
2094 goto probe_dai_err;
2095 ret = soc_bind_dai_link(card, dai_link);
2096 if (ret)
2097 goto probe_dai_err;
2098 }
2099
Stephen Warren62ae68f2012-06-08 12:34:23 -06002100 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002101 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002102 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002103 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002104 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002105 dev_err(card->dev,
2106 "ASoC: failed to instantiate card %d\n",
2107 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002108 goto probe_dai_err;
2109 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002110 }
2111 }
2112
Mark Brown888df392012-02-16 19:37:51 -08002113 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002114 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002115
Mark Brownb7af1da2011-04-07 19:18:44 +09002116 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002117 snd_soc_add_card_controls(card, card->controls,
2118 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002119
Mark Brownb8ad29d2011-03-02 18:35:51 +00002120 if (card->dapm_routes)
2121 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2122 card->num_dapm_routes);
2123
Nicolin Chenf23e8602015-02-14 17:22:49 -08002124 if (card->of_dapm_routes)
2125 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2126 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002127
Takashi Iwai861886d2017-04-24 08:54:42 +02002128 /* try to set some sane longname if DMI is available */
2129 snd_soc_set_dmi_name(card, NULL);
2130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002131 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002132 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002133 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2134 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002135 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2136 "%s", card->driver_name ? card->driver_name : card->name);
2137 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2138 switch (card->snd_card->driver[i]) {
2139 case '_':
2140 case '-':
2141 case '\0':
2142 break;
2143 default:
2144 if (!isalnum(card->snd_card->driver[i]))
2145 card->snd_card->driver[i] = '_';
2146 break;
2147 }
2148 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002149
Mark Brown28e9ad92011-03-02 18:36:34 +00002150 if (card->late_probe) {
2151 ret = card->late_probe(card);
2152 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002153 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002154 card->name, ret);
2155 goto probe_aux_dev_err;
2156 }
2157 }
2158
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002159 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002160
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002161 ret = snd_card_register(card->snd_card);
2162 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002163 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2164 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002165 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002166 }
2167
Mark Brown435c5e22008-12-04 15:32:53 +00002168 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002169 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002170 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002171 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002172 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002173
2174 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002175
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002176probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002177 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002178
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002179probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002180 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002181
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002182card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002183 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002184 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002185
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002186 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002187 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002188 snd_card_free(card->snd_card);
2189
Mark Brownb19e6e72012-03-14 21:18:39 +00002190base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002191 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002192 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002193 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194
Mark Brownb19e6e72012-03-14 21:18:39 +00002195 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002196}
2197
2198/* probes a new socdev */
2199static int soc_probe(struct platform_device *pdev)
2200{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002201 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002202
Vinod Koul70a7ca32011-01-14 19:22:48 +05302203 /*
2204 * no card, so machine driver should be registering card
2205 * we should not be here in that case so ret error
2206 */
2207 if (!card)
2208 return -EINVAL;
2209
Mark Brownfe4085e2012-03-02 13:07:41 +00002210 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002211 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002212 card->name);
2213
Mark Brown435c5e22008-12-04 15:32:53 +00002214 /* Bodge while we unpick instantiation */
2215 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002216
Mark Brown28d528c2012-08-09 18:45:23 +01002217 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002218}
2219
Vinod Koulb0e26482011-01-13 22:48:02 +05302220static int soc_cleanup_card_resources(struct snd_soc_card *card)
2221{
Mengdong Lin1a497982015-11-18 02:34:11 -05002222 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302223
2224 /* make sure any delayed work runs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002225 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002226 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302227
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002228 /* free the ALSA card at first; this syncs with pending operations */
2229 snd_card_free(card->snd_card);
2230
Vinod Koulb0e26482011-01-13 22:48:02 +05302231 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002232 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002233 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302234
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002235 /* remove auxiliary devices */
2236 soc_remove_aux_devices(card);
2237
Mark Brownd1e81422016-08-18 19:32:59 +01002238 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302239 soc_cleanup_card_debugfs(card);
2240
2241 /* remove the card */
2242 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002243 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302244
Vinod Koulb0e26482011-01-13 22:48:02 +05302245 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302246}
2247
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248/* removes a socdev */
2249static int soc_remove(struct platform_device *pdev)
2250{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002251 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002252
Mark Brownc5af3a22008-11-28 13:29:45 +00002253 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 return 0;
2255}
2256
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002257int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002258{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002259 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002260 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002261
2262 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002263 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002264
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002265 /*
2266 * Flush out pmdown_time work - we actually do want to run it
2267 * now, we're shutting down so no imminent restart.
2268 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002269 for_each_card_rtds(card, rtd)
Tejun Heo43829732012-08-20 14:51:24 -07002270 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002271
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002272 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002273
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002274 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002275 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002276 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002277 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002278 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002279
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002280 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002281 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002282 pinctrl_pm_select_sleep_state(codec_dai->dev);
2283 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002284 }
2285
Mark Brown416356f2009-06-30 19:05:15 +01002286 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002287}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002288EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002289
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002290const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302291 .suspend = snd_soc_suspend,
2292 .resume = snd_soc_resume,
2293 .freeze = snd_soc_suspend,
2294 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002295 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302296 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002297};
Stephen Warrendeb26072011-04-05 19:35:30 -06002298EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002299
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002300/* ASoC platform driver */
2301static struct platform_driver soc_driver = {
2302 .driver = {
2303 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002304 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305 },
2306 .probe = soc_probe,
2307 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308};
2309
Mark Brown096e49d2009-07-05 15:12:22 +01002310/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 * snd_soc_cnew - create new control
2312 * @_template: control template
2313 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002314 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002315 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 *
2317 * Create a new mixer control from a template control.
2318 *
2319 * Returns 0 for success, else error.
2320 */
2321struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002322 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002323 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324{
2325 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002326 struct snd_kcontrol *kcontrol;
2327 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328
2329 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 template.index = 0;
2331
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 if (!long_name)
2333 long_name = template.name;
2334
2335 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002336 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002337 if (!name)
2338 return NULL;
2339
Mark Brownefb7ac32011-03-08 17:23:24 +00002340 template.name = name;
2341 } else {
2342 template.name = long_name;
2343 }
2344
2345 kcontrol = snd_ctl_new1(&template, data);
2346
2347 kfree(name);
2348
2349 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350}
2351EXPORT_SYMBOL_GPL(snd_soc_cnew);
2352
Liam Girdwood022658b2012-02-03 17:43:09 +00002353static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2354 const struct snd_kcontrol_new *controls, int num_controls,
2355 const char *prefix, void *data)
2356{
2357 int err, i;
2358
2359 for (i = 0; i < num_controls; i++) {
2360 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002361
Liam Girdwood022658b2012-02-03 17:43:09 +00002362 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2363 control->name, prefix));
2364 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002365 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2366 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002367 return err;
2368 }
2369 }
2370
2371 return 0;
2372}
2373
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002374struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2375 const char *name)
2376{
2377 struct snd_card *card = soc_card->snd_card;
2378 struct snd_kcontrol *kctl;
2379
2380 if (unlikely(!name))
2381 return NULL;
2382
2383 list_for_each_entry(kctl, &card->controls, list)
2384 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2385 return kctl;
2386 return NULL;
2387}
2388EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2389
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002391 * snd_soc_add_component_controls - Add an array of controls to a component.
2392 *
2393 * @component: Component to add controls to
2394 * @controls: Array of controls to add
2395 * @num_controls: Number of elements in the array
2396 *
2397 * Return: 0 for success, else error.
2398 */
2399int snd_soc_add_component_controls(struct snd_soc_component *component,
2400 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2401{
2402 struct snd_card *card = component->card->snd_card;
2403
2404 return snd_soc_add_controls(card, component->dev, controls,
2405 num_controls, component->name_prefix, component);
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2408
2409/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002410 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2411 * Convenience function to add a list of controls.
2412 *
2413 * @soc_card: SoC card to add controls to
2414 * @controls: array of controls to add
2415 * @num_controls: number of elements in the array
2416 *
2417 * Return 0 for success, else error.
2418 */
2419int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2420 const struct snd_kcontrol_new *controls, int num_controls)
2421{
2422 struct snd_card *card = soc_card->snd_card;
2423
2424 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2425 NULL, soc_card);
2426}
2427EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2428
2429/**
2430 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2431 * Convienience function to add a list of controls.
2432 *
2433 * @dai: DAI to add controls to
2434 * @controls: array of controls to add
2435 * @num_controls: number of elements in the array
2436 *
2437 * Return 0 for success, else error.
2438 */
2439int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2440 const struct snd_kcontrol_new *controls, int num_controls)
2441{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002442 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002443
2444 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2445 NULL, dai);
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2448
2449/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002450 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2451 * @dai: DAI
2452 * @clk_id: DAI specific clock ID
2453 * @freq: new clock frequency in Hz
2454 * @dir: new clock direction - input/output.
2455 *
2456 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2457 */
2458int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2459 unsigned int freq, int dir)
2460{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002461 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002462 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002463
2464 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2465 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002466}
2467EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2468
2469/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002470 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2471 * @component: COMPONENT
2472 * @clk_id: DAI specific clock ID
2473 * @source: Source for the clock
2474 * @freq: new clock frequency in Hz
2475 * @dir: new clock direction - input/output.
2476 *
2477 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2478 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002479int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2480 int clk_id, int source, unsigned int freq,
2481 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002482{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002483 if (component->driver->set_sysclk)
2484 return component->driver->set_sysclk(component, clk_id, source,
2485 freq, dir);
2486
2487 return -ENOTSUPP;
2488}
2489EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2490
2491/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002492 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2493 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002494 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002495 * @div: new clock divisor.
2496 *
2497 * Configures the clock dividers. This is used to derive the best DAI bit and
2498 * frame clocks from the system or master clock. It's best to set the DAI bit
2499 * and frame clocks as low as possible to save system power.
2500 */
2501int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2502 int div_id, int div)
2503{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002504 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002505 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002506 else
2507 return -EINVAL;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2510
2511/**
2512 * snd_soc_dai_set_pll - configure DAI PLL.
2513 * @dai: DAI
2514 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002515 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002516 * @freq_in: PLL input clock frequency in Hz
2517 * @freq_out: requested PLL output clock frequency in Hz
2518 *
2519 * Configures and enables PLL to generate output clock based on input clock.
2520 */
Mark Brown85488032009-09-05 18:52:16 +01002521int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2522 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002523{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002524 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002525 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002526 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002527
2528 return snd_soc_component_set_pll(dai->component, pll_id, source,
2529 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002530}
2531EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2532
Mark Brownec4ee522011-03-07 20:58:11 +00002533/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002534 * snd_soc_component_set_pll - configure component PLL.
2535 * @component: COMPONENT
2536 * @pll_id: DAI specific PLL ID
2537 * @source: DAI specific source for the PLL
2538 * @freq_in: PLL input clock frequency in Hz
2539 * @freq_out: requested PLL output clock frequency in Hz
2540 *
2541 * Configures and enables PLL to generate output clock based on input clock.
2542 */
2543int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2544 int source, unsigned int freq_in,
2545 unsigned int freq_out)
2546{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002547 if (component->driver->set_pll)
2548 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002549 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002550
2551 return -EINVAL;
2552}
2553EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2554
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002555/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002556 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2557 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002558 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002559 *
2560 * Configures the DAI for a preset BCLK to sample rate ratio.
2561 */
2562int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2563{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002564 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002565 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2566 else
2567 return -EINVAL;
2568}
2569EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2570
2571/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002572 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2573 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002574 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002575 *
2576 * Configures the DAI hardware format and clocking.
2577 */
2578int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2579{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002580 if (dai->driver->ops->set_fmt == NULL)
2581 return -ENOTSUPP;
2582 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002583}
2584EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2585
2586/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002587 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002588 * @slots: Number of slots in use.
2589 * @tx_mask: bitmask representing active TX slots.
2590 * @rx_mask: bitmask representing active RX slots.
2591 *
2592 * Generates the TDM tx and rx slot default masks for DAI.
2593 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002594static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002595 unsigned int *tx_mask,
2596 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002597{
2598 if (*tx_mask || *rx_mask)
2599 return 0;
2600
2601 if (!slots)
2602 return -EINVAL;
2603
2604 *tx_mask = (1 << slots) - 1;
2605 *rx_mask = (1 << slots) - 1;
2606
2607 return 0;
2608}
2609
2610/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002611 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2612 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002613 * @tx_mask: bitmask representing active TX slots.
2614 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002615 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002616 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002617 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002618 * This function configures the specified DAI for TDM operation. @slot contains
2619 * the total number of slots of the TDM stream and @slot_with the width of each
2620 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2621 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2622 * DAI should write to or read from. If a bit is set the corresponding slot is
2623 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2624 * the first slot, bit 1 to the second slot and so on. The first active slot
2625 * maps to the first channel of the DAI, the second active slot to the second
2626 * channel and so on.
2627 *
2628 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2629 * @rx_mask and @slot_width will be ignored.
2630 *
2631 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002632 */
2633int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002634 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002635{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002636 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002637 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002638 &tx_mask, &rx_mask);
2639 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002640 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002641
Benoit Cousson88bd8702014-07-08 23:19:34 +02002642 dai->tx_mask = tx_mask;
2643 dai->rx_mask = rx_mask;
2644
Kuninori Morimoto46471922017-09-25 01:38:34 +00002645 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002646 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002647 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002648 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002649 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002650}
2651EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2652
2653/**
Barry Song472df3c2009-09-12 01:16:29 +08002654 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2655 * @dai: DAI
2656 * @tx_num: how many TX channels
2657 * @tx_slot: pointer to an array which imply the TX slot number channel
2658 * 0~num-1 uses
2659 * @rx_num: how many RX channels
2660 * @rx_slot: pointer to an array which imply the RX slot number channel
2661 * 0~num-1 uses
2662 *
2663 * configure the relationship between channel number and TDM slot number.
2664 */
2665int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2666 unsigned int tx_num, unsigned int *tx_slot,
2667 unsigned int rx_num, unsigned int *rx_slot)
2668{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002669 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002670 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002671 rx_num, rx_slot);
2672 else
2673 return -EINVAL;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2676
2677/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002678 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2679 * @dai: DAI
2680 * @tx_num: how many TX channels
2681 * @tx_slot: pointer to an array which imply the TX slot number channel
2682 * 0~num-1 uses
2683 * @rx_num: how many RX channels
2684 * @rx_slot: pointer to an array which imply the RX slot number channel
2685 * 0~num-1 uses
2686 */
2687int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2688 unsigned int *tx_num, unsigned int *tx_slot,
2689 unsigned int *rx_num, unsigned int *rx_slot)
2690{
2691 if (dai->driver->ops->get_channel_map)
2692 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2693 rx_num, rx_slot);
2694 else
2695 return -ENOTSUPP;
2696}
2697EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2698
2699/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002700 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2701 * @dai: DAI
2702 * @tristate: tristate enable
2703 *
2704 * Tristates the DAI so that others can use it.
2705 */
2706int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2707{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002708 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002709 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002710 else
2711 return -EINVAL;
2712}
2713EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2714
2715/**
2716 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2717 * @dai: DAI
2718 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002719 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002720 *
2721 * Mutes the DAI DAC.
2722 */
Mark Brownda183962013-02-06 15:44:07 +00002723int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2724 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002725{
Mark Brownda183962013-02-06 15:44:07 +00002726 if (dai->driver->ops->mute_stream)
2727 return dai->driver->ops->mute_stream(dai, mute, direction);
2728 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2729 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002731 else
Mark Brown04570c62012-04-13 19:16:03 +01002732 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002733}
2734EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2735
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002736static int snd_soc_bind_card(struct snd_soc_card *card)
2737{
2738 struct snd_soc_pcm_runtime *rtd;
2739 int ret;
2740
2741 ret = snd_soc_instantiate_card(card);
2742 if (ret != 0)
2743 return ret;
2744
2745 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002746 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002747 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2748 struct snd_soc_dai *codec_dai;
2749 int j;
2750
2751 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2752 if (!codec_dai->active)
2753 pinctrl_pm_select_sleep_state(codec_dai->dev);
2754 }
2755
2756 if (!cpu_dai->active)
2757 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2758 }
2759
2760 return ret;
2761}
2762
Mark Brownc5af3a22008-11-28 13:29:45 +00002763/**
2764 * snd_soc_register_card - Register a card with the ASoC core
2765 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002766 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002767 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002768 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302769int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002770{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002771 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002772 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002773
Mark Brownc5af3a22008-11-28 13:29:45 +00002774 if (!card->name || !card->dev)
2775 return -EINVAL;
2776
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302777 mutex_lock(&client_mutex);
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002778 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002779
Mengdong Lin923c5e612015-11-23 11:01:49 -05002780 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002781 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002782 dev_err(card->dev, "ASoC: failed to init link %s\n",
2783 link->name);
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302784 mutex_unlock(&client_mutex);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002785 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002786 }
Stephen Warren5a504962011-12-21 10:40:59 -07002787 }
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302788 mutex_unlock(&client_mutex);
Stephen Warren5a504962011-12-21 10:40:59 -07002789
Mark Browned77cc12011-05-03 18:25:34 +01002790 dev_set_drvdata(card->dev, card);
2791
Stephen Warren111c6412011-01-28 14:26:35 -07002792 snd_soc_initialize_card_lists(card);
2793
Mengdong Linf8f80362015-12-02 14:11:22 +08002794 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002795
Mengdong Lin1a497982015-11-18 02:34:11 -05002796 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002797 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002798
Mark Browndb432b42011-10-03 21:06:40 +01002799 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002800 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002801 card->instantiated = 0;
2802 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002803 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002804
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002805 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002806}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302807EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002808
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002809static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2810{
2811 if (card->instantiated) {
2812 card->instantiated = false;
2813 snd_soc_dapm_shutdown(card);
2814 soc_cleanup_card_resources(card);
2815 if (!unregister)
2816 list_add(&card->list, &unbind_card_list);
2817 } else {
2818 if (unregister)
2819 list_del(&card->list);
2820 }
2821}
2822
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002823/**
2824 * snd_soc_unregister_card - Unregister a card with the ASoC core
2825 *
2826 * @card: Card to unregister
2827 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002828 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302829int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002830{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002831 snd_soc_unbind_card(card, true);
2832 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002833
2834 return 0;
2835}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302836EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002837
2838/*
2839 * Simplify DAI link configuration by removing ".-1" from device names
2840 * and sanitizing names.
2841 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002842static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002843{
2844 char *found, name[NAME_SIZE];
2845 int id1, id2;
2846
2847 if (dev_name(dev) == NULL)
2848 return NULL;
2849
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002850 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002851
2852 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002853 found = strstr(name, dev->driver->name);
2854 if (found) {
2855 /* get ID */
2856 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2857
2858 /* discard ID from name if ID == -1 */
2859 if (*id == -1)
2860 found[strlen(dev->driver->name)] = '\0';
2861 }
2862
2863 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002864 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002865 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2866 char tmp[NAME_SIZE];
2867
2868 /* create unique ID number from I2C addr and bus */
2869 *id = ((id1 & 0xffff) << 16) + id2;
2870
2871 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002872 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2873 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002874 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002875 } else
2876 *id = 0;
2877 }
2878
2879 return kstrdup(name, GFP_KERNEL);
2880}
2881
2882/*
2883 * Simplify DAI link naming for single devices with multiple DAIs by removing
2884 * any ".-1" and using the DAI name (instead of device name).
2885 */
2886static inline char *fmt_multiple_name(struct device *dev,
2887 struct snd_soc_dai_driver *dai_drv)
2888{
2889 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002890 dev_err(dev,
2891 "ASoC: error - multiple DAI %s registered with no name\n",
2892 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002893 return NULL;
2894 }
2895
2896 return kstrdup(dai_drv->name, GFP_KERNEL);
2897}
2898
2899/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002900 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002901 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002902 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002903 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002904static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002905{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002906 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002907
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002908 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002909 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2910 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002911 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002912 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002913 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002914 }
Mark Brown91151712008-11-30 23:31:24 +00002915}
Mark Brown91151712008-11-30 23:31:24 +00002916
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002917/* Create a DAI and add it to the component's DAI list */
2918static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2919 struct snd_soc_dai_driver *dai_drv,
2920 bool legacy_dai_naming)
2921{
2922 struct device *dev = component->dev;
2923 struct snd_soc_dai *dai;
2924
2925 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2926
2927 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2928 if (dai == NULL)
2929 return NULL;
2930
2931 /*
2932 * Back in the old days when we still had component-less DAIs,
2933 * instead of having a static name, component-less DAIs would
2934 * inherit the name of the parent device so it is possible to
2935 * register multiple instances of the DAI. We still need to keep
2936 * the same naming style even though those DAIs are not
2937 * component-less anymore.
2938 */
2939 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002940 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002941 dai->name = fmt_single_name(dev, &dai->id);
2942 } else {
2943 dai->name = fmt_multiple_name(dev, dai_drv);
2944 if (dai_drv->id)
2945 dai->id = dai_drv->id;
2946 else
2947 dai->id = component->num_dai;
2948 }
2949 if (dai->name == NULL) {
2950 kfree(dai);
2951 return NULL;
2952 }
2953
2954 dai->component = component;
2955 dai->dev = dev;
2956 dai->driver = dai_drv;
2957 if (!dai->driver->ops)
2958 dai->driver->ops = &null_dai_ops;
2959
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002960 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002961 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002962 component->num_dai++;
2963
2964 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2965 return dai;
2966}
2967
Mark Brown91151712008-11-30 23:31:24 +00002968/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002969 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002970 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002971 * @component: The component the DAIs are registered for
2972 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002973 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002974 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002975static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002976 struct snd_soc_dai_driver *dai_drv,
2977 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002978{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002979 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002981 unsigned int i;
2982 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002983
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002984 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002985
2986 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002987
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002988 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2989 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002990 if (dai == NULL) {
2991 ret = -ENOMEM;
2992 goto err;
2993 }
Mark Brown91151712008-11-30 23:31:24 +00002994 }
2995
2996 return 0;
2997
2998err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002999 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003000
3001 return ret;
3002}
Mark Brown91151712008-11-30 23:31:24 +00003003
Mengdong Lin68003e62015-12-31 16:40:43 +08003004/**
3005 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3006 *
3007 * @component: The component the DAIs are registered for
3008 * @dai_drv: DAI driver to use for the DAI
3009 *
3010 * Topology can use this API to register DAIs when probing a component.
3011 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3012 * will be freed in the component cleanup.
3013 */
3014int snd_soc_register_dai(struct snd_soc_component *component,
3015 struct snd_soc_dai_driver *dai_drv)
3016{
3017 struct snd_soc_dapm_context *dapm =
3018 snd_soc_component_get_dapm(component);
3019 struct snd_soc_dai *dai;
3020 int ret;
3021
3022 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3023 dev_err(component->dev, "Invalid dai type %d\n",
3024 dai_drv->dobj.type);
3025 return -EINVAL;
3026 }
3027
3028 lockdep_assert_held(&client_mutex);
3029 dai = soc_add_dai(component, dai_drv, false);
3030 if (!dai)
3031 return -ENOMEM;
3032
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003033 /*
3034 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08003035 * also add routes that need these widgets as source or sink.
3036 */
3037 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3038 if (ret != 0) {
3039 dev_err(component->dev,
3040 "Failed to create DAI widgets %d\n", ret);
3041 }
3042
3043 return ret;
3044}
3045EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3046
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003047static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3048 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003049{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003050 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003051
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003052 component->driver->seq_notifier(component, type, subseq);
3053}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003054
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003055static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3056 int event)
3057{
3058 struct snd_soc_component *component = dapm->component;
3059
3060 return component->driver->stream_event(component, event);
3061}
3062
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003063static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3064 enum snd_soc_bias_level level)
3065{
3066 struct snd_soc_component *component = dapm->component;
3067
3068 return component->driver->set_bias_level(component, level);
3069}
3070
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003071static int snd_soc_component_initialize(struct snd_soc_component *component,
3072 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003073{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003074 struct snd_soc_dapm_context *dapm;
3075
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003076 component->name = fmt_single_name(dev, &component->id);
3077 if (!component->name) {
3078 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003079 return -ENOMEM;
3080 }
3081
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003082 component->dev = dev;
3083 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003084
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003085 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003086 dapm->dev = dev;
3087 dapm->component = component;
3088 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003089 dapm->idle_bias_off = !driver->idle_bias_on;
3090 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003091 if (driver->seq_notifier)
3092 dapm->seq_notifier = snd_soc_component_seq_notifier;
3093 if (driver->stream_event)
3094 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003095 if (driver->set_bias_level)
3096 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003098 INIT_LIST_HEAD(&component->dai_list);
3099 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003100
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003101 return 0;
3102}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003103
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003104static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003105{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003106 int val_bytes = regmap_get_val_bytes(component->regmap);
3107
3108 /* Errors are legitimate for non-integer byte multiples */
3109 if (val_bytes > 0)
3110 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003111}
3112
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003113#ifdef CONFIG_REGMAP
3114
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003115/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003116 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3117 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003118 * @component: The component for which to initialize the regmap instance
3119 * @regmap: The regmap instance that should be used by the component
3120 *
3121 * This function allows deferred assignment of the regmap instance that is
3122 * associated with the component. Only use this if the regmap instance is not
3123 * yet ready when the component is registered. The function must also be called
3124 * before the first IO attempt of the component.
3125 */
3126void snd_soc_component_init_regmap(struct snd_soc_component *component,
3127 struct regmap *regmap)
3128{
3129 component->regmap = regmap;
3130 snd_soc_component_setup_regmap(component);
3131}
3132EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3133
3134/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003135 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3136 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003137 * @component: The component for which to de-initialize the regmap instance
3138 *
3139 * Calls regmap_exit() on the regmap instance associated to the component and
3140 * removes the regmap instance from the component.
3141 *
3142 * This function should only be used if snd_soc_component_init_regmap() was used
3143 * to initialize the regmap instance.
3144 */
3145void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3146{
3147 regmap_exit(component->regmap);
3148 component->regmap = NULL;
3149}
3150EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3151
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003152#endif
3153
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003154static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003155{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003156 mutex_lock(&client_mutex);
3157
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003158 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003159 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003160 component->regmap = dev_get_regmap(component->dev,
3161 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003162 if (component->regmap)
3163 snd_soc_component_setup_regmap(component);
3164 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003165
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003166 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003167 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003168 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003169
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003170 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003171}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003172
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003173static void snd_soc_component_cleanup(struct snd_soc_component *component)
3174{
3175 snd_soc_unregister_dais(component);
3176 kfree(component->name);
3177}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003178
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003179static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3180{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003181 struct snd_soc_card *card = component->card;
3182
3183 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003184 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003185
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003186 list_del(&component->list);
3187}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003188
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003189#define ENDIANNESS_MAP(name) \
3190 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3191static u64 endianness_format_map[] = {
3192 ENDIANNESS_MAP(S16_),
3193 ENDIANNESS_MAP(U16_),
3194 ENDIANNESS_MAP(S24_),
3195 ENDIANNESS_MAP(U24_),
3196 ENDIANNESS_MAP(S32_),
3197 ENDIANNESS_MAP(U32_),
3198 ENDIANNESS_MAP(S24_3),
3199 ENDIANNESS_MAP(U24_3),
3200 ENDIANNESS_MAP(S20_3),
3201 ENDIANNESS_MAP(U20_3),
3202 ENDIANNESS_MAP(S18_3),
3203 ENDIANNESS_MAP(U18_3),
3204 ENDIANNESS_MAP(FLOAT_),
3205 ENDIANNESS_MAP(FLOAT64_),
3206 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3207};
3208
3209/*
3210 * Fix up the DAI formats for endianness: codecs don't actually see
3211 * the endianness of the data but we're using the CPU format
3212 * definitions which do need to include endianness so we ensure that
3213 * codec DAIs always have both big and little endian variants set.
3214 */
3215static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3216{
3217 int i;
3218
3219 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3220 if (stream->formats & endianness_format_map[i])
3221 stream->formats |= endianness_format_map[i];
3222}
3223
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003224static void snd_soc_try_rebind_card(void)
3225{
3226 struct snd_soc_card *card, *c;
3227
3228 if (!list_empty(&unbind_card_list)) {
3229 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3230 if (!snd_soc_bind_card(card))
3231 list_del(&card->list);
3232 }
3233 }
3234}
3235
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003236int snd_soc_add_component(struct device *dev,
3237 struct snd_soc_component *component,
3238 const struct snd_soc_component_driver *component_driver,
3239 struct snd_soc_dai_driver *dai_drv,
3240 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003241{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003242 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003243 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003244
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003245 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003246 if (ret)
3247 goto err_free;
3248
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003249 if (component_driver->endianness) {
3250 for (i = 0; i < num_dai; i++) {
3251 convert_endianness_formats(&dai_drv[i].playback);
3252 convert_endianness_formats(&dai_drv[i].capture);
3253 }
3254 }
3255
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003256 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003257 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003258 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003259 goto err_cleanup;
3260 }
3261
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003262 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003263 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003264
3265 return 0;
3266
3267err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003268 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003269err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003270 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003271}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003272EXPORT_SYMBOL_GPL(snd_soc_add_component);
3273
3274int snd_soc_register_component(struct device *dev,
3275 const struct snd_soc_component_driver *component_driver,
3276 struct snd_soc_dai_driver *dai_drv,
3277 int num_dai)
3278{
3279 struct snd_soc_component *component;
3280
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003281 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003282 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003283 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003284
3285 return snd_soc_add_component(dev, component, component_driver,
3286 dai_drv, num_dai);
3287}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003288EXPORT_SYMBOL_GPL(snd_soc_register_component);
3289
3290/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003291 * snd_soc_unregister_component - Unregister all related component
3292 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003293 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003294 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003295 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003296static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003297{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003298 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003299 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003300
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003301 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003302 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003303 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003304 continue;
3305
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003306 snd_soc_tplg_component_remove(component,
3307 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003308 snd_soc_component_del_unlocked(component);
3309 found = 1;
3310 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003311 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003312 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003313
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003314 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003315 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003316
3317 return found;
3318}
3319
3320void snd_soc_unregister_component(struct device *dev)
3321{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003322 while (__snd_soc_unregister_component(dev))
3323 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003324}
3325EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3326
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003327struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3328 const char *driver_name)
3329{
3330 struct snd_soc_component *component;
3331 struct snd_soc_component *ret;
3332
3333 ret = NULL;
3334 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003335 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003336 if (dev != component->dev)
3337 continue;
3338
3339 if (driver_name &&
3340 (driver_name != component->driver->name) &&
3341 (strcmp(component->driver->name, driver_name) != 0))
3342 continue;
3343
3344 ret = component;
3345 break;
3346 }
3347 mutex_unlock(&client_mutex);
3348
3349 return ret;
3350}
3351EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3352
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003353/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003354int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3355 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003356{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003357 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003358 int ret;
3359
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303360 if (!card->dev) {
3361 pr_err("card->dev is not set before calling %s\n", __func__);
3362 return -EINVAL;
3363 }
3364
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003365 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303366
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003367 ret = of_property_read_string_index(np, propname, 0, &card->name);
3368 /*
3369 * EINVAL means the property does not exist. This is fine providing
3370 * card->name was previously set, which is checked later in
3371 * snd_soc_register_card.
3372 */
3373 if (ret < 0 && ret != -EINVAL) {
3374 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003375 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003376 propname, ret);
3377 return ret;
3378 }
3379
3380 return 0;
3381}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003382EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003383
Xiubo Li9a6d4862014-02-08 15:59:52 +08003384static const struct snd_soc_dapm_widget simple_widgets[] = {
3385 SND_SOC_DAPM_MIC("Microphone", NULL),
3386 SND_SOC_DAPM_LINE("Line", NULL),
3387 SND_SOC_DAPM_HP("Headphone", NULL),
3388 SND_SOC_DAPM_SPK("Speaker", NULL),
3389};
3390
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003391int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003392 const char *propname)
3393{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003394 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003395 struct snd_soc_dapm_widget *widgets;
3396 const char *template, *wname;
3397 int i, j, num_widgets, ret;
3398
3399 num_widgets = of_property_count_strings(np, propname);
3400 if (num_widgets < 0) {
3401 dev_err(card->dev,
3402 "ASoC: Property '%s' does not exist\n", propname);
3403 return -EINVAL;
3404 }
3405 if (num_widgets & 1) {
3406 dev_err(card->dev,
3407 "ASoC: Property '%s' length is not even\n", propname);
3408 return -EINVAL;
3409 }
3410
3411 num_widgets /= 2;
3412 if (!num_widgets) {
3413 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3414 propname);
3415 return -EINVAL;
3416 }
3417
3418 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3419 GFP_KERNEL);
3420 if (!widgets) {
3421 dev_err(card->dev,
3422 "ASoC: Could not allocate memory for widgets\n");
3423 return -ENOMEM;
3424 }
3425
3426 for (i = 0; i < num_widgets; i++) {
3427 ret = of_property_read_string_index(np, propname,
3428 2 * i, &template);
3429 if (ret) {
3430 dev_err(card->dev,
3431 "ASoC: Property '%s' index %d read error:%d\n",
3432 propname, 2 * i, ret);
3433 return -EINVAL;
3434 }
3435
3436 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3437 if (!strncmp(template, simple_widgets[j].name,
3438 strlen(simple_widgets[j].name))) {
3439 widgets[i] = simple_widgets[j];
3440 break;
3441 }
3442 }
3443
3444 if (j >= ARRAY_SIZE(simple_widgets)) {
3445 dev_err(card->dev,
3446 "ASoC: DAPM widget '%s' is not supported\n",
3447 template);
3448 return -EINVAL;
3449 }
3450
3451 ret = of_property_read_string_index(np, propname,
3452 (2 * i) + 1,
3453 &wname);
3454 if (ret) {
3455 dev_err(card->dev,
3456 "ASoC: Property '%s' index %d read error:%d\n",
3457 propname, (2 * i) + 1, ret);
3458 return -EINVAL;
3459 }
3460
3461 widgets[i].name = wname;
3462 }
3463
Nicolin Chenf23e8602015-02-14 17:22:49 -08003464 card->of_dapm_widgets = widgets;
3465 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003466
3467 return 0;
3468}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003469EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003470
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003471int snd_soc_of_get_slot_mask(struct device_node *np,
3472 const char *prop_name,
3473 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003474{
3475 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003476 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003477 int i;
3478
3479 if (!of_slot_mask)
3480 return 0;
3481 val /= sizeof(u32);
3482 for (i = 0; i < val; i++)
3483 if (be32_to_cpup(&of_slot_mask[i]))
3484 *mask |= (1 << i);
3485
3486 return val;
3487}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003488EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003489
Xiubo Li89c67852014-02-14 09:34:35 +08003490int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003491 unsigned int *tx_mask,
3492 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003493 unsigned int *slots,
3494 unsigned int *slot_width)
3495{
3496 u32 val;
3497 int ret;
3498
Jyri Sarha61310842015-09-09 21:27:43 +03003499 if (tx_mask)
3500 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3501 if (rx_mask)
3502 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3503
Xiubo Li89c67852014-02-14 09:34:35 +08003504 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3505 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3506 if (ret)
3507 return ret;
3508
3509 if (slots)
3510 *slots = val;
3511 }
3512
3513 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3514 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3515 if (ret)
3516 return ret;
3517
3518 if (slot_width)
3519 *slot_width = val;
3520 }
3521
3522 return 0;
3523}
3524EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3525
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003526void snd_soc_of_parse_node_prefix(struct device_node *np,
3527 struct snd_soc_codec_conf *codec_conf,
3528 struct device_node *of_node,
3529 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003530{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003531 const char *str;
3532 int ret;
3533
3534 ret = of_property_read_string(np, propname, &str);
3535 if (ret < 0) {
3536 /* no prefix is not error */
3537 return;
3538 }
3539
3540 codec_conf->of_node = of_node;
3541 codec_conf->name_prefix = str;
3542}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003543EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003544
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003545int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003546 const char *propname)
3547{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003548 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003549 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003550 struct snd_soc_dapm_route *routes;
3551 int i, ret;
3552
3553 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003554 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003555 dev_err(card->dev,
3556 "ASoC: Property '%s' does not exist or its length is not even\n",
3557 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003558 return -EINVAL;
3559 }
3560 num_routes /= 2;
3561 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003562 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003563 propname);
3564 return -EINVAL;
3565 }
3566
Kees Cooka86854d2018-06-12 14:07:58 -07003567 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003568 GFP_KERNEL);
3569 if (!routes) {
3570 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003571 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003572 return -EINVAL;
3573 }
3574
3575 for (i = 0; i < num_routes; i++) {
3576 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003577 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003578 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003579 dev_err(card->dev,
3580 "ASoC: Property '%s' index %d could not be read: %d\n",
3581 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003582 return -EINVAL;
3583 }
3584 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003585 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003586 if (ret) {
3587 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003588 "ASoC: Property '%s' index %d could not be read: %d\n",
3589 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003590 return -EINVAL;
3591 }
3592 }
3593
Nicolin Chenf23e8602015-02-14 17:22:49 -08003594 card->num_of_dapm_routes = num_routes;
3595 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003596
3597 return 0;
3598}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003599EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003600
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003601unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003602 const char *prefix,
3603 struct device_node **bitclkmaster,
3604 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003605{
3606 int ret, i;
3607 char prop[128];
3608 unsigned int format = 0;
3609 int bit, frame;
3610 const char *str;
3611 struct {
3612 char *name;
3613 unsigned int val;
3614 } of_fmt_table[] = {
3615 { "i2s", SND_SOC_DAIFMT_I2S },
3616 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3617 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3618 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3619 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3620 { "ac97", SND_SOC_DAIFMT_AC97 },
3621 { "pdm", SND_SOC_DAIFMT_PDM},
3622 { "msb", SND_SOC_DAIFMT_MSB },
3623 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003624 };
3625
3626 if (!prefix)
3627 prefix = "";
3628
3629 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003630 * check "dai-format = xxx"
3631 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003632 * SND_SOC_DAIFMT_FORMAT_MASK area
3633 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003634 ret = of_property_read_string(np, "dai-format", &str);
3635 if (ret < 0) {
3636 snprintf(prop, sizeof(prop), "%sformat", prefix);
3637 ret = of_property_read_string(np, prop, &str);
3638 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003639 if (ret == 0) {
3640 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3641 if (strcmp(str, of_fmt_table[i].name) == 0) {
3642 format |= of_fmt_table[i].val;
3643 break;
3644 }
3645 }
3646 }
3647
3648 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003649 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003650 * SND_SOC_DAIFMT_CLOCK_MASK area
3651 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003652 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003653 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003654 format |= SND_SOC_DAIFMT_CONT;
3655 else
3656 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003657
3658 /*
3659 * check "[prefix]bitclock-inversion"
3660 * check "[prefix]frame-inversion"
3661 * SND_SOC_DAIFMT_INV_MASK area
3662 */
3663 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3664 bit = !!of_get_property(np, prop, NULL);
3665
3666 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3667 frame = !!of_get_property(np, prop, NULL);
3668
3669 switch ((bit << 4) + frame) {
3670 case 0x11:
3671 format |= SND_SOC_DAIFMT_IB_IF;
3672 break;
3673 case 0x10:
3674 format |= SND_SOC_DAIFMT_IB_NF;
3675 break;
3676 case 0x01:
3677 format |= SND_SOC_DAIFMT_NB_IF;
3678 break;
3679 default:
3680 /* SND_SOC_DAIFMT_NB_NF is default */
3681 break;
3682 }
3683
3684 /*
3685 * check "[prefix]bitclock-master"
3686 * check "[prefix]frame-master"
3687 * SND_SOC_DAIFMT_MASTER_MASK area
3688 */
3689 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3690 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003691 if (bit && bitclkmaster)
3692 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003693
3694 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3695 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003696 if (frame && framemaster)
3697 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003698
3699 switch ((bit << 4) + frame) {
3700 case 0x11:
3701 format |= SND_SOC_DAIFMT_CBM_CFM;
3702 break;
3703 case 0x10:
3704 format |= SND_SOC_DAIFMT_CBM_CFS;
3705 break;
3706 case 0x01:
3707 format |= SND_SOC_DAIFMT_CBS_CFM;
3708 break;
3709 default:
3710 format |= SND_SOC_DAIFMT_CBS_CFS;
3711 break;
3712 }
3713
3714 return format;
3715}
3716EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3717
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003718int snd_soc_get_dai_id(struct device_node *ep)
3719{
3720 struct snd_soc_component *pos;
3721 struct device_node *node;
3722 int ret;
3723
3724 node = of_graph_get_port_parent(ep);
3725
3726 /*
3727 * For example HDMI case, HDMI has video/sound port,
3728 * but ALSA SoC needs sound port number only.
3729 * Thus counting HDMI DT port/endpoint doesn't work.
3730 * Then, it should have .of_xlate_dai_id
3731 */
3732 ret = -ENOTSUPP;
3733 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003734 for_each_component(pos) {
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003735 struct device_node *component_of_node = pos->dev->of_node;
3736
3737 if (!component_of_node && pos->dev->parent)
3738 component_of_node = pos->dev->parent->of_node;
3739
3740 if (component_of_node != node)
3741 continue;
3742
3743 if (pos->driver->of_xlate_dai_id)
3744 ret = pos->driver->of_xlate_dai_id(pos, ep);
3745
3746 break;
3747 }
3748 mutex_unlock(&client_mutex);
3749
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003750 of_node_put(node);
3751
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003752 return ret;
3753}
3754EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3755
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003756int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003757 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003758{
3759 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003760 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003761 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003762
3763 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003764 for_each_component(pos) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003765 component_of_node = pos->dev->of_node;
3766 if (!component_of_node && pos->dev->parent)
3767 component_of_node = pos->dev->parent->of_node;
3768
3769 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003770 continue;
3771
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003772 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003773 ret = pos->driver->of_xlate_dai_name(pos,
3774 args,
3775 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003776 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003777 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003778 int id = -1;
3779
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003780 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003781 case 0:
3782 id = 0; /* same as dai_drv[0] */
3783 break;
3784 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003785 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003786 break;
3787 default:
3788 /* not supported */
3789 break;
3790 }
3791
3792 if (id < 0 || id >= pos->num_dai) {
3793 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003794 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003795 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003796
3797 ret = 0;
3798
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003799 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003800 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003801 if (id == 0)
3802 break;
3803 id--;
3804 }
3805
3806 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003807 if (!*dai_name)
3808 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003809 }
3810
Kuninori Morimotocb470082013-09-10 17:39:56 -07003811 break;
3812 }
3813 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003814 return ret;
3815}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003816EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003817
3818int snd_soc_of_get_dai_name(struct device_node *of_node,
3819 const char **dai_name)
3820{
3821 struct of_phandle_args args;
3822 int ret;
3823
3824 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3825 "#sound-dai-cells", 0, &args);
3826 if (ret)
3827 return ret;
3828
3829 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003830
3831 of_node_put(args.np);
3832
3833 return ret;
3834}
3835EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3836
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003837/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003838 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3839 * @dai_link: DAI link
3840 *
3841 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3842 */
3843void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3844{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003845 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003846 int index;
3847
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003848 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003849 if (!component->of_node)
3850 break;
3851 of_node_put(component->of_node);
3852 component->of_node = NULL;
3853 }
3854}
3855EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3856
3857/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003858 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3859 * @dev: Card device
3860 * @of_node: Device node
3861 * @dai_link: DAI link
3862 *
3863 * Builds an array of CODEC DAI components from the DAI link property
3864 * 'sound-dai'.
3865 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003866 * The device nodes in the array (of_node) must be dereferenced by calling
3867 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003868 *
3869 * Returns 0 for success
3870 */
3871int snd_soc_of_get_dai_link_codecs(struct device *dev,
3872 struct device_node *of_node,
3873 struct snd_soc_dai_link *dai_link)
3874{
3875 struct of_phandle_args args;
3876 struct snd_soc_dai_link_component *component;
3877 char *name;
3878 int index, num_codecs, ret;
3879
3880 /* Count the number of CODECs */
3881 name = "sound-dai";
3882 num_codecs = of_count_phandle_with_args(of_node, name,
3883 "#sound-dai-cells");
3884 if (num_codecs <= 0) {
3885 if (num_codecs == -ENOENT)
3886 dev_err(dev, "No 'sound-dai' property\n");
3887 else
3888 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3889 return num_codecs;
3890 }
Kees Cooka86854d2018-06-12 14:07:58 -07003891 component = devm_kcalloc(dev,
3892 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003893 GFP_KERNEL);
3894 if (!component)
3895 return -ENOMEM;
3896 dai_link->codecs = component;
3897 dai_link->num_codecs = num_codecs;
3898
3899 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003900 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003901 ret = of_parse_phandle_with_args(of_node, name,
3902 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003903 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003904 if (ret)
3905 goto err;
3906 component->of_node = args.np;
3907 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3908 if (ret < 0)
3909 goto err;
3910 }
3911 return 0;
3912err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003913 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003914 dai_link->codecs = NULL;
3915 dai_link->num_codecs = 0;
3916 return ret;
3917}
3918EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3919
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003920static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003921{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003922 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003923 snd_soc_util_init();
3924
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003925 return platform_driver_register(&soc_driver);
3926}
Mark Brown4abe8e12010-10-12 17:41:03 +01003927module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003928
Mark Brown7d8c16a2008-11-30 22:11:24 +00003929static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003930{
Mark Brownfb257892011-04-28 10:57:54 +01003931 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003932 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003933
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003934 platform_driver_unregister(&soc_driver);
3935}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003936module_exit(snd_soc_exit);
3937
3938/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003939MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003940MODULE_DESCRIPTION("ALSA SoC Core");
3941MODULE_LICENSE("GPL");
3942MODULE_ALIAS("platform:soc-audio");