blob: c03b7107cb7784e3fb043e0590a6a2fa275078ef [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>
Kuninori Morimoto02e75632020-05-25 09:57:14 +090041#include <sound/soc-link.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Mark Brownc5af3a22008-11-28 13:29:45 +000047static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070048static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010049static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000050
Kuninori Morimoto368dee92018-09-21 05:23:01 +000051#define for_each_component(component) \
52 list_for_each_entry(component, &component_list, list)
53
Frank Mandarinodb2a4162006-10-06 18:31:09 +020054/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090055 * This is used if driver don't need to have CPU/Codec/Platform
56 * dai_link. see soc.h
57 */
58struct snd_soc_dai_link_component null_dailink_component[0];
59EXPORT_SYMBOL_GPL(null_dailink_component);
60
61/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Mark Browndbe21402010-02-12 11:37:24 +000070static ssize_t pmdown_time_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
72{
Mark Brown36ae1a92012-01-06 17:12:45 -080073 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000074
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000075 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000076}
77
78static ssize_t pmdown_time_set(struct device *dev,
79 struct device_attribute *attr,
80 const char *buf, size_t count)
81{
Mark Brown36ae1a92012-01-06 17:12:45 -080082 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070083 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000084
Jingoo Hanb785a492013-07-19 16:24:59 +090085 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -070086 if (ret)
87 return ret;
Mark Browndbe21402010-02-12 11:37:24 +000088
89 return count;
90}
91
92static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
93
Takashi Iwaid29697d2015-01-30 20:16:37 +010094static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +010095 &dev_attr_pmdown_time.attr,
96 NULL
97};
98
99static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
100 struct attribute *attr, int idx)
101{
102 struct device *dev = kobj_to_dev(kobj);
103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
104
Kuninori Morimotod918a372019-09-12 13:42:30 +0900105 if (!rtd)
106 return 0;
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108 if (attr == &dev_attr_pmdown_time.attr)
109 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000110 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100111}
112
113static const struct attribute_group soc_dapm_dev_group = {
114 .attrs = soc_dapm_dev_attrs,
115 .is_visible = soc_dev_attr_is_visible,
116};
117
Mark Brownf7e73b262018-03-09 12:46:27 +0000118static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100119 .attrs = soc_dev_attrs,
120 .is_visible = soc_dev_attr_is_visible,
121};
122
123static const struct attribute_group *soc_dev_attr_groups[] = {
124 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000125 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100126 NULL
127};
128
Mark Brown2624d5f2009-11-03 21:56:13 +0000129#ifdef CONFIG_DEBUG_FS
Kuninori Morimotoa4072cd2019-12-11 13:32:20 +0900130struct dentry *snd_soc_debugfs_root;
131EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
132
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200133static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100134{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200135 if (!component->card->debugfs_card_root)
136 return;
137
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200138 if (component->debugfs_prefix) {
139 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100140
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141 name = kasprintf(GFP_KERNEL, "%s:%s",
142 component->debugfs_prefix, component->name);
143 if (name) {
144 component->debugfs_root = debugfs_create_dir(name,
145 component->card->debugfs_card_root);
146 kfree(name);
147 }
148 } else {
149 component->debugfs_root = debugfs_create_dir(component->name,
150 component->card->debugfs_card_root);
151 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100152
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
154 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200155}
156
157static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
158{
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900159 if (!component->debugfs_root)
160 return;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 debugfs_remove_recursive(component->debugfs_root);
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900162 component->debugfs_root = NULL;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200163}
164
Peng Donglinc15b2a12018-02-14 22:48:07 +0800165static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100166{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100167 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100168 struct snd_soc_dai *dai;
169
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100170 mutex_lock(&client_mutex);
171
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000172 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000173 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800174 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100175
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100176 mutex_unlock(&client_mutex);
177
Donglin Peng700c17c2018-01-18 13:31:26 +0800178 return 0;
179}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800180DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100181
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000182static int component_list_show(struct seq_file *m, void *v)
183{
184 struct snd_soc_component *component;
185
186 mutex_lock(&client_mutex);
187
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000188 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000189 seq_printf(m, "%s\n", component->name);
190
191 mutex_unlock(&client_mutex);
192
193 return 0;
194}
195DEFINE_SHOW_ATTRIBUTE(component_list);
196
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200197static void soc_init_card_debugfs(struct snd_soc_card *card)
198{
199 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000200 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200201
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200202 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
203 &card->pop_time);
Kuninori Morimotod8ca7a02019-08-07 10:31:14 +0900204
205 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200206}
207
208static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
209{
210 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900211 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200212}
213
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200214static void snd_soc_debugfs_init(void)
215{
216 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200217
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200218 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
219 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000220
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200221 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
222 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200223}
224
225static void snd_soc_debugfs_exit(void)
226{
227 debugfs_remove_recursive(snd_soc_debugfs_root);
228}
229
Mark Brown2624d5f2009-11-03 21:56:13 +0000230#else
231
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200232static inline void soc_init_component_debugfs(
233 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000234{
235}
236
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200237static inline void soc_cleanup_component_debugfs(
238 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000239{
240}
241
Axel Linb95fccb2010-11-09 17:06:44 +0800242static inline void soc_init_card_debugfs(struct snd_soc_card *card)
243{
244}
245
246static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
247{
248}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200249
250static inline void snd_soc_debugfs_init(void)
251{
252}
253
254static inline void snd_soc_debugfs_exit(void)
255{
256}
257
Mark Brown2624d5f2009-11-03 21:56:13 +0000258#endif
259
Kuninori Morimoto12b05232020-01-10 11:35:54 +0900260static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
261 struct snd_soc_component *component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000262{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900263 struct snd_soc_component *comp;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900264 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000265
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900266 for_each_rtd_components(rtd, i, comp) {
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000267 /* already connected */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900268 if (comp == component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000269 return 0;
270 }
271
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900272 /* see for_each_rtd_components */
273 rtd->components[rtd->num_components] = component;
274 rtd->num_components++;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000275
276 return 0;
277}
278
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000279struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
280 const char *driver_name)
281{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900282 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900283 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000284
Kuninori Morimoto971da242018-01-23 00:41:24 +0000285 if (!driver_name)
286 return NULL;
287
Kuninori Morimotoa33c0d12019-08-20 14:05:02 +0900288 /*
289 * NOTE
290 *
291 * snd_soc_rtdcom_lookup() will find component from rtd by using
292 * specified driver name.
293 * But, if many components which have same driver name are connected
294 * to 1 rtd, this function will return 1st found component.
295 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900296 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900297 const char *component_name = component->driver->name;
Kuninori Morimoto971da242018-01-23 00:41:24 +0000298
299 if (!component_name)
300 continue;
301
302 if ((component_name == driver_name) ||
303 strcmp(component_name, driver_name) == 0)
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900304 return component;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000305 }
306
307 return NULL;
308}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000309EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000310
Shengjiu Wang6fbea6b2020-06-12 15:37:48 +0800311struct snd_soc_component
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900312*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
Kuninori Morimotob8132652019-11-05 15:46:30 +0900313{
314 struct snd_soc_component *component;
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900315 struct snd_soc_component *found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900316
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900317 found_component = NULL;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900318 for_each_component(component) {
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900319 if ((dev == component->dev) &&
320 (!driver_name ||
321 (driver_name == component->driver->name) ||
322 (strcmp(component->driver->name, driver_name) == 0))) {
323 found_component = component;
324 break;
325 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900326 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900327
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900328 return found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900329}
Shengjiu Wang6fbea6b2020-06-12 15:37:48 +0800330EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900331
332struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
333 const char *driver_name)
334{
335 struct snd_soc_component *component;
336
337 mutex_lock(&client_mutex);
338 component = snd_soc_lookup_component_nolocked(dev, driver_name);
339 mutex_unlock(&client_mutex);
340
341 return component;
342}
Kuninori Morimotob8132652019-11-05 15:46:30 +0900343EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
344
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900345struct snd_soc_pcm_runtime
346*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
Kuninori Morimoto44681892019-12-10 09:34:08 +0900347 struct snd_soc_dai_link *dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900348{
349 struct snd_soc_pcm_runtime *rtd;
350
351 for_each_card_rtds(card, rtd) {
Kuninori Morimoto44681892019-12-10 09:34:08 +0900352 if (rtd->dai_link == dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900353 return rtd;
354 }
Kuninori Morimoto44681892019-12-10 09:34:08 +0900355 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900356 return NULL;
357}
358EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
359
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900360/*
361 * Power down the audio subsystem pmdown_time msecs after close is called.
362 * This is to ensure there are no pops or clicks in between any music tracks
363 * due to DAPM power cycling.
364 */
365void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
366{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900367 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900368 int playback = SNDRV_PCM_STREAM_PLAYBACK;
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900369
370 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
371
372 dev_dbg(rtd->dev,
373 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
374 codec_dai->driver->playback.stream_name,
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900375 snd_soc_dai_stream_active(codec_dai, playback) ?
376 "active" : "inactive",
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900377 rtd->pop_wait ? "yes" : "no");
378
379 /* are we waiting on this codec DAI stream */
380 if (rtd->pop_wait == 1) {
381 rtd->pop_wait = 0;
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900382 snd_soc_dapm_stream_event(rtd, playback,
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900383 SND_SOC_DAPM_STREAM_STOP);
384 }
385
386 mutex_unlock(&rtd->card->pcm_mutex);
387}
388EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
389
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900390static void soc_release_rtd_dev(struct device *dev)
391{
392 /* "dev" means "rtd->dev" */
393 kfree(dev);
394}
395
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900396static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
397{
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900398 if (!rtd)
399 return;
400
Kuninori Morimoto753ace02019-09-12 13:38:50 +0900401 list_del(&rtd->list);
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900402
Curtis Malainey9c9b6522019-11-27 17:13:58 -0800403 if (delayed_work_pending(&rtd->delayed_work))
404 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +0900405 snd_soc_pcm_component_free(rtd);
406
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900407 /*
408 * we don't need to call kfree() for rtd->dev
409 * see
410 * soc_release_rtd_dev()
Kuninori Morimotod918a372019-09-12 13:42:30 +0900411 *
412 * We don't need rtd->dev NULL check, because
413 * it is alloced *before* rtd.
414 * see
415 * soc_new_pcm_runtime()
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900416 */
Kuninori Morimotod918a372019-09-12 13:42:30 +0900417 device_unregister(rtd->dev);
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900418}
419
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800420static void close_delayed_work(struct work_struct *work) {
421 struct snd_soc_pcm_runtime *rtd =
422 container_of(work, struct snd_soc_pcm_runtime,
423 delayed_work.work);
424
425 if (rtd->close_delayed_work_func)
426 rtd->close_delayed_work_func(rtd);
427}
428
Mengdong Lin1a497982015-11-18 02:34:11 -0500429static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
430 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
431{
432 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900433 struct snd_soc_component *component;
Kuninori Morimotod918a372019-09-12 13:42:30 +0900434 struct device *dev;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900435 int ret;
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900436 int stream;
Mengdong Lin1a497982015-11-18 02:34:11 -0500437
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900438 /*
Kuninori Morimotod918a372019-09-12 13:42:30 +0900439 * for rtd->dev
440 */
441 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
442 if (!dev)
443 return NULL;
444
445 dev->parent = card->dev;
446 dev->release = soc_release_rtd_dev;
Kuninori Morimotod918a372019-09-12 13:42:30 +0900447
448 dev_set_name(dev, "%s", dai_link->name);
449
450 ret = device_register(dev);
451 if (ret < 0) {
452 put_device(dev); /* soc_release_rtd_dev */
453 return NULL;
454 }
455
456 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900457 * for rtd
458 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900459 rtd = devm_kzalloc(dev,
460 sizeof(*rtd) +
461 sizeof(*component) * (dai_link->num_cpus +
462 dai_link->num_codecs +
463 dai_link->num_platforms),
464 GFP_KERNEL);
Mengdong Lin1a497982015-11-18 02:34:11 -0500465 if (!rtd)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900466 goto free_rtd;
Mengdong Lin1a497982015-11-18 02:34:11 -0500467
Kuninori Morimotod918a372019-09-12 13:42:30 +0900468 rtd->dev = dev;
Takashi Iwai07d22a92019-12-04 16:14:54 +0100469 INIT_LIST_HEAD(&rtd->list);
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900470 for_each_pcm_streams(stream) {
471 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
472 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
473 }
Kuninori Morimotod918a372019-09-12 13:42:30 +0900474 dev_set_drvdata(dev, rtd);
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800475 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
Kuninori Morimotod918a372019-09-12 13:42:30 +0900476
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900477 /*
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900478 * for rtd->dais
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900479 */
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900480 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
Kees Cook6396bb22018-06-12 14:03:40 -0700481 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500482 GFP_KERNEL);
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900483 if (!rtd->dais)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900484 goto free_rtd;
485
486 /*
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900487 * dais = [][][][][][][][][][][][][][][][][][]
488 * ^cpu_dais ^codec_dais
489 * |--- num_cpus ---|--- num_codecs --|
Kuninori Morimoto17290252020-03-30 10:48:27 +0900490 * see
491 * asoc_rtd_to_cpu()
492 * asoc_rtd_to_codec()
Shreyas NC76afa642020-02-25 21:39:12 +0800493 */
Kuninori Morimoto49648d02020-03-30 10:47:54 +0900494 rtd->num_cpus = dai_link->num_cpus;
495 rtd->num_codecs = dai_link->num_codecs;
Kuninori Morimoto01faf502020-03-30 10:48:14 +0900496 rtd->card = card;
497 rtd->dai_link = dai_link;
498 rtd->num = card->num_rtd++;
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900499
Kuninori Morimoto6634e3d2019-08-07 10:30:31 +0900500 /* see for_each_card_rtds */
Mengdong Lin1a497982015-11-18 02:34:11 -0500501 list_add_tail(&rtd->list, &card->rtd_list);
Kuninori Morimotoa8481252019-09-12 13:38:34 +0900502
Charles Keepax5c74c9d2020-07-30 13:07:14 +0100503 ret = device_add_groups(dev, soc_dev_attr_groups);
504 if (ret < 0)
505 goto free_rtd;
506
Kuninori Morimotoa8481252019-09-12 13:38:34 +0900507 return rtd;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900508
509free_rtd:
510 soc_free_pcm_runtime(rtd);
511 return NULL;
Mengdong Lin1a497982015-11-18 02:34:11 -0500512}
513
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900514static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
515{
516 struct snd_soc_pcm_runtime *rtd;
517
518 for_each_card_rtds(card, rtd)
519 flush_delayed_work(&rtd->delayed_work);
520}
521
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000522#ifdef CONFIG_PM_SLEEP
Kuninori Morimotod4c1d9e2020-11-27 09:07:35 +0900523static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
524{
525 struct snd_soc_pcm_runtime *rtd;
526 struct snd_soc_dai *dai;
527 int playback = SNDRV_PCM_STREAM_PLAYBACK;
528 int i;
529
530 for_each_card_rtds(card, rtd) {
531
532 if (rtd->dai_link->ignore_suspend)
533 continue;
534
535 for_each_rtd_dais(rtd, i, dai) {
536 if (snd_soc_dai_stream_active(dai, playback))
537 snd_soc_dai_digital_mute(dai, mute, playback);
538 }
539 }
540}
541
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000543int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000545 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000546 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500547 struct snd_soc_pcm_runtime *rtd;
548 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200549
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200550 /* If the card is not initialized yet there is nothing to do */
551 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200552 return 0;
553
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200554 /*
555 * Due to the resume being scheduled into a workqueue we could
556 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100559
560 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100562
Mark Browna00f90f2010-12-02 16:24:24 +0000563 /* mute any active DACs */
Kuninori Morimotod4c1d9e2020-11-27 09:07:35 +0900564 soc_playback_digital_mute(card, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100566 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000567 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100569 continue;
570
Mengdong Lin1a497982015-11-18 02:34:11 -0500571 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100573
Kuninori Morimoto130dc082020-05-28 10:48:39 +0900574 snd_soc_card_suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200576 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900577 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100578
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000579 for_each_card_rtds(card, rtd) {
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900580 int stream;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581
Mengdong Lin1a497982015-11-18 02:34:11 -0500582 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100583 continue;
584
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900585 for_each_pcm_streams(stream)
586 snd_soc_dapm_stream_event(rtd, stream,
587 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 }
589
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200590 /* Recheck all endpoints too, their state is affected by suspend */
591 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700592 snd_soc_dapm_sync(&card->dapm);
593
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000594 /* suspend all COMPONENTs */
Kuninori Morimoto12720632020-01-10 11:36:02 +0900595 for_each_card_rtds(card, rtd) {
596
597 if (rtd->dai_link->ignore_suspend)
598 continue;
599
600 for_each_rtd_components(rtd, i, component) {
601 struct snd_soc_dapm_context *dapm =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200602 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000603
Kuninori Morimoto12720632020-01-10 11:36:02 +0900604 /*
605 * ignore if component was already suspended
606 */
607 if (snd_soc_component_is_suspended(component))
608 continue;
609
610 /*
611 * If there are paths active then the COMPONENT will be
612 * held with bias _ON and should not be suspended.
613 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200614 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000616 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000617 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000618 * bias off then being in STANDBY
619 * means it's doing something,
620 * otherwise fall through.
621 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200622 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000623 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200624 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000625 break;
626 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500627 fallthrough;
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200628
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 case SND_SOC_BIAS_OFF:
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900630 snd_soc_component_suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000631 if (component->regmap)
632 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800633 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000634 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 break;
636 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000637 dev_dbg(component->dev,
638 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 break;
640 }
641 }
642 }
643
Kuninori Morimotod17b60b2020-05-28 10:48:48 +0900644 snd_soc_card_suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645
646 return 0;
647}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000648EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200650/*
651 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100652 * setting our codec back up, which can be very slow on I2C
653 */
654static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200657 container_of(work, struct snd_soc_card,
658 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500659 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000660 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200662 /*
663 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100664 * so userspace apps are blocked from touching us
665 */
666
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000667 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100668
Mark Brown9949788b2010-05-07 20:24:05 +0100669 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100671
Kuninori Morimoto934c7522020-05-28 10:48:55 +0900672 snd_soc_card_resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000674 for_each_card_components(card, component) {
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900675 if (snd_soc_component_is_suspended(component))
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900676 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100677 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000679 for_each_card_rtds(card, rtd) {
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900680 int stream;
Mark Brown3efab7d2010-05-09 13:25:43 +0100681
Mengdong Lin1a497982015-11-18 02:34:11 -0500682 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100683 continue;
684
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900685 for_each_pcm_streams(stream)
686 snd_soc_dapm_stream_event(rtd, stream,
687 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200688 }
689
Mark Brown3ff3f642008-05-19 12:32:25 +0200690 /* unmute any active DACs */
Kuninori Morimotod4c1d9e2020-11-27 09:07:35 +0900691 soc_playback_digital_mute(card, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
Kuninori Morimoto739443d2020-05-28 10:49:02 +0900693 snd_soc_card_resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000695 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100696
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200697 /* Recheck all endpoints too, their state is affected by suspend */
698 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700699 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530700
701 /* userspace can access us now we are back as we were before */
702 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100703}
704
705/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000706int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100707{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000708 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900709 struct snd_soc_component *component;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200710
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200711 /* If the card is not initialized yet there is nothing to do */
712 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800713 return 0;
714
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800715 /* activate pins from sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900716 for_each_card_components(card, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900717 if (snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900718 pinctrl_pm_select_default_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800719
Kuninori Morimoto250a15c2020-01-20 10:05:07 +0900720 dev_dbg(dev, "ASoC: Scheduling resume work\n");
721 if (!schedule_work(&card->deferred_resume_work))
722 dev_err(dev, "ASoC: resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100723
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724 return 0;
725}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000726EXPORT_SYMBOL_GPL(snd_soc_resume);
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900727
728static void soc_resume_init(struct snd_soc_card *card)
729{
730 /* deferred resume work */
731 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
732}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734#define snd_soc_suspend NULL
735#define snd_soc_resume NULL
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900736static inline void soc_resume_init(struct snd_soc_card *card)
737{
738}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739#endif
740
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900741static struct device_node
742*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100743{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900744 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100745
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900746 of_node = component->dev->of_node;
747 if (!of_node && component->dev->parent)
748 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100749
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900750 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100751}
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
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900759 if (!dlc)
760 return 0;
761
762 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000763
764 if (dlc->of_node && component_of_node != dlc->of_node)
765 return 0;
766 if (dlc->name && strcmp(component->name, dlc->name))
767 return 0;
768
769 return 1;
770}
771
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900773 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774{
775 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776
777 lockdep_assert_held(&client_mutex);
778
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900779 /*
780 * NOTE
781 *
782 * It returns *1st* found component, but some driver
783 * has few components by same of_node/name
784 * ex)
785 * CPU component and generic DMAEngine component
786 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900787 for_each_component(component)
788 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200789 return component;
Barry Song02a06d32009-10-16 18:13:38 +0800790
791 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100792}
793
Mengdong Linfbb88b52016-04-22 12:25:33 +0800794/**
795 * snd_soc_find_dai - Find a registered DAI
796 *
Jeffy Chen49584712017-08-22 15:57:21 +0800797 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800798 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700799 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800800 * find the DAI of the same name. The component's of_node and name
801 * should also match if being specified.
802 *
803 * Return: pointer of DAI, or NULL if not found.
804 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800805struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200806 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100807{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200808 struct snd_soc_component *component;
809 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100810
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100811 lockdep_assert_held(&client_mutex);
812
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200813 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000814 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000815 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200816 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000817 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800818 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800819 && (!dai->driver->name
820 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100821 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100822
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200823 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100824 }
825 }
826
827 return NULL;
828}
Mengdong Lin305e9022016-04-19 13:12:25 +0800829EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100830
Kuninori Morimotoc1c277b2020-08-27 08:55:39 +0900831struct snd_soc_dai *snd_soc_find_dai_with_mutex(
832 const struct snd_soc_dai_link_component *dlc)
833{
834 struct snd_soc_dai *dai;
835
836 mutex_lock(&client_mutex);
837 dai = snd_soc_find_dai(dlc);
838 mutex_unlock(&client_mutex);
839
840 return dai;
841}
842EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
843
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900844static int soc_dai_link_sanity_check(struct snd_soc_card *card,
845 struct snd_soc_dai_link *link)
Kuninori Morimoto36794902019-11-05 15:45:41 +0900846{
847 int i;
Shreyas NC76afa642020-02-25 21:39:12 +0800848 struct snd_soc_dai_link_component *cpu, *codec, *platform;
Kuninori Morimoto36794902019-11-05 15:45:41 +0900849
850 for_each_link_codecs(link, i, codec) {
851 /*
852 * Codec must be specified by 1 of name or OF node,
853 * not both or neither.
854 */
855 if (!!codec->name == !!codec->of_node) {
856 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
857 link->name);
858 return -EINVAL;
859 }
860
861 /* Codec DAI name must be specified */
862 if (!codec->dai_name) {
863 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
864 link->name);
865 return -EINVAL;
866 }
867
868 /*
869 * Defer card registration if codec component is not added to
870 * component list.
871 */
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500872 if (!soc_find_component(codec)) {
873 dev_dbg(card->dev,
874 "ASoC: codec component %s not found for link %s\n",
875 codec->name, link->name);
Kuninori Morimoto36794902019-11-05 15:45:41 +0900876 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500877 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900878 }
879
880 for_each_link_platforms(link, i, platform) {
881 /*
882 * Platform may be specified by either name or OF node, but it
883 * can be left unspecified, then no components will be inserted
884 * in the rtdcom list
885 */
886 if (!!platform->name == !!platform->of_node) {
887 dev_err(card->dev,
888 "ASoC: Neither/both platform name/of_node are set for %s\n",
889 link->name);
890 return -EINVAL;
891 }
892
893 /*
894 * Defer card registration if platform component is not added to
895 * component list.
896 */
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500897 if (!soc_find_component(platform)) {
898 dev_dbg(card->dev,
899 "ASoC: platform component %s not found for link %s\n",
900 platform->name, link->name);
Kuninori Morimoto36794902019-11-05 15:45:41 +0900901 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500902 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900903 }
904
Shreyas NC76afa642020-02-25 21:39:12 +0800905 for_each_link_cpus(link, i, cpu) {
906 /*
907 * CPU device may be specified by either name or OF node, but
908 * can be left unspecified, and will be matched based on DAI
909 * name alone..
910 */
911 if (cpu->name && cpu->of_node) {
912 dev_err(card->dev,
913 "ASoC: Neither/both cpu name/of_node are set for %s\n",
914 link->name);
915 return -EINVAL;
916 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900917
Shreyas NC76afa642020-02-25 21:39:12 +0800918 /*
919 * Defer card registration if cpu dai component is not added to
920 * component list.
921 */
922 if ((cpu->of_node || cpu->name) &&
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500923 !soc_find_component(cpu)) {
924 dev_dbg(card->dev,
925 "ASoC: cpu component %s not found for link %s\n",
926 cpu->name, link->name);
Shreyas NC76afa642020-02-25 21:39:12 +0800927 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500928 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900929
Shreyas NC76afa642020-02-25 21:39:12 +0800930 /*
931 * At least one of CPU DAI name or CPU device name/node must be
932 * specified
933 */
934 if (!cpu->dai_name &&
935 !(cpu->name || cpu->of_node)) {
936 dev_err(card->dev,
937 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
938 link->name);
939 return -EINVAL;
940 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900941 }
942
943 return 0;
944}
945
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900946/**
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900947 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
948 * @card: The ASoC card to which the pcm_runtime has
949 * @rtd: The pcm_runtime to remove
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900950 *
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900951 * This function removes a pcm_runtime from the ASoC card.
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900952 */
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900953void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
954 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900955{
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900956 lockdep_assert_held(&client_mutex);
957
Pierre-Louis Bossart21a00fb2020-06-22 10:42:37 -0500958 /* release machine specific resources */
959 snd_soc_link_exit(rtd);
960
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900961 /*
962 * Notify the machine driver for extra destruction
963 */
Kuninori Morimotofcbbcc32020-05-28 10:50:46 +0900964 snd_soc_card_remove_dai_link(card, rtd->dai_link);
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900965
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900966 soc_free_pcm_runtime(rtd);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900967}
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900968EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900969
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900970/**
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900971 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
972 * @card: The ASoC card to which the pcm_runtime is added
973 * @dai_link: The DAI link to find pcm_runtime
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900974 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900975 * This function adds a pcm_runtime ASoC card by using dai_link.
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900976 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900977 * Note: Topology can use this API to add pcm_runtime when probing the
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900978 * topology component. And machine drivers can still define static
979 * DAI links in dai_link array.
980 */
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900981int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
982 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200983{
Mengdong Lin1a497982015-11-18 02:34:11 -0500984 struct snd_soc_pcm_runtime *rtd;
Shreyas NC76afa642020-02-25 21:39:12 +0800985 struct snd_soc_dai_link_component *codec, *platform, *cpu;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000986 struct snd_soc_component *component;
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900987 int i, ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200988
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900989 lockdep_assert_held(&client_mutex);
990
991 /*
992 * Notify the machine driver for extra initialization
993 */
Kuninori Morimotocbc7a6b2020-05-28 10:50:41 +0900994 ret = snd_soc_card_add_dai_link(card, dai_link);
995 if (ret < 0)
996 return ret;
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900997
Liam Girdwooda655de82018-07-02 16:59:54 +0100998 if (dai_link->ignore)
999 return 0;
1000
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001001 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001002
Kuninori Morimotobfce78a2019-11-05 15:45:50 +09001003 ret = soc_dai_link_sanity_check(card, dai_link);
1004 if (ret < 0)
1005 return ret;
1006
Sudip Mukherjee513cb3112016-02-22 14:14:31 +05301007 rtd = soc_new_pcm_runtime(card, dai_link);
1008 if (!rtd)
1009 return -ENOMEM;
1010
Shreyas NC76afa642020-02-25 21:39:12 +08001011 for_each_link_cpus(dai_link, i, cpu) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001012 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1013 if (!asoc_rtd_to_cpu(rtd, i)) {
Shreyas NC76afa642020-02-25 21:39:12 +08001014 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1015 cpu->dai_name);
1016 goto _err_defer;
1017 }
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001018 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001019 }
Shreyas NC76afa642020-02-25 21:39:12 +08001020
Benoit Cousson88bd8702014-07-08 23:19:34 +02001021 /* Find CODEC from registered CODECs */
Jerome Brunet34614732019-06-27 14:13:50 +02001022 for_each_link_codecs(dai_link, i, codec) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001023 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1024 if (!asoc_rtd_to_codec(rtd, i)) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +01001025 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +02001026 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001027 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001028 }
Jerome Brunet34614732019-06-27 14:13:50 +02001029
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001030 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001031 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001032
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +09001033 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +02001034 for_each_link_platforms(dai_link, i, platform) {
1035 for_each_component(component) {
1036 if (!snd_soc_is_matching_component(platform, component))
1037 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001038
Kuninori Morimoto12b05232020-01-10 11:35:54 +09001039 snd_soc_rtd_add_component(rtd, component);
Jerome Brunet34614732019-06-27 14:13:50 +02001040 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001041 }
1042
Mark Brownb19e6e72012-03-14 21:18:39 +00001043 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001044
1045_err_defer:
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +09001046 snd_soc_remove_pcm_runtime(card, rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001047 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048}
Kuninori Morimoto0c048002019-12-10 09:34:19 +09001049EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001050
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001051static int soc_init_pcm_runtime(struct snd_soc_card *card,
1052 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001053{
1054 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001055 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001056 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001057 int ret, num, i;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001058
1059 /* set default power off timeout */
1060 rtd->pmdown_time = pmdown_time;
1061
1062 /* do machine specific initialization */
Kuninori Morimoto02e75632020-05-25 09:57:14 +09001063 ret = snd_soc_link_init(rtd);
1064 if (ret < 0)
1065 return ret;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001066
1067 if (dai_link->dai_fmt) {
1068 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1069 if (ret)
1070 return ret;
1071 }
1072
1073 /* add DPCM sysfs entries */
1074 soc_dpcm_debugfs_add(rtd);
1075
1076 num = rtd->num;
1077
1078 /*
1079 * most drivers will register their PCMs using DAI link ordering but
1080 * topology based drivers can use the DAI link id field to set PCM
1081 * device number and then use rtd + a base offset of the BEs.
1082 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001083 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001084 if (!component->driver->use_dai_pcm_id)
1085 continue;
1086
1087 if (rtd->dai_link->no_pcm)
1088 num += component->driver->be_pcm_base;
1089 else
1090 num = rtd->dai_link->id;
1091 }
1092
1093 /* create compress_device if possible */
1094 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1095 if (ret != -ENOTSUPP) {
1096 if (ret < 0)
1097 dev_err(card->dev, "ASoC: can't create compress %s\n",
1098 dai_link->stream_name);
1099 return ret;
1100 }
1101
1102 /* create the pcm */
1103 ret = soc_new_pcm(rtd, num);
1104 if (ret < 0) {
1105 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1106 dai_link->stream_name, ret);
1107 return ret;
1108 }
Kuninori Morimotod1eb6d12020-03-16 15:37:20 +09001109
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +09001110 return snd_soc_pcm_dai_new(rtd);
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001111}
1112
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001113static void soc_set_name_prefix(struct snd_soc_card *card,
1114 struct snd_soc_component *component)
1115{
Kuninori Morimotoaec3ff92019-12-10 09:34:52 +09001116 struct device_node *of_node = soc_component_to_node(component);
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001117 const char *str;
1118 int ret, i;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001119
Kuninori Morimoto4702f992019-12-10 09:34:48 +09001120 for (i = 0; i < card->num_configs; i++) {
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001121 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001122
Sameer Pujar3256ef92020-11-02 20:40:08 +05301123 if (snd_soc_is_matching_component(&map->dlc, component) &&
1124 map->name_prefix) {
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09001125 component->name_prefix = map->name_prefix;
1126 return;
1127 }
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001128 }
1129
1130 /*
1131 * If there is no configuration table or no match in the table,
1132 * check if a prefix is provided in the node
1133 */
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001134 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1135 if (ret < 0)
1136 return;
1137
1138 component->name_prefix = str;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001139}
1140
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001141static void soc_remove_component(struct snd_soc_component *component,
1142 int probed)
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001143{
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001144
1145 if (!component->card)
1146 return;
1147
1148 if (probed)
1149 snd_soc_component_remove(component);
1150
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001151 /* For framework level robustness */
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +02001152 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001153
Bard liao7a5d9812019-09-18 21:31:31 +08001154 list_del_init(&component->card_list);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001155 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1156 soc_cleanup_component_debugfs(component);
1157 component->card = NULL;
Pierre-Louis Bossart0e36f362019-08-07 21:51:31 -05001158 snd_soc_component_module_put_when_remove(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001159}
1160
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001161static int soc_probe_component(struct snd_soc_card *card,
1162 struct snd_soc_component *component)
1163{
1164 struct snd_soc_dapm_context *dapm =
1165 snd_soc_component_get_dapm(component);
1166 struct snd_soc_dai *dai;
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001167 int probed = 0;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001168 int ret;
1169
1170 if (!strcmp(component->name, "snd-soc-dummy"))
1171 return 0;
1172
1173 if (component->card) {
1174 if (component->card != card) {
1175 dev_err(component->dev,
1176 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1177 card->name, component->card->name);
1178 return -ENODEV;
1179 }
1180 return 0;
1181 }
1182
1183 ret = snd_soc_component_module_get_when_probe(component);
1184 if (ret < 0)
1185 return ret;
1186
1187 component->card = card;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001188 soc_set_name_prefix(card, component);
1189
1190 soc_init_component_debugfs(component);
1191
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001192 snd_soc_dapm_init(dapm, card, component);
Kuninori Morimotob614bea2019-08-23 09:58:47 +09001193
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001194 ret = snd_soc_dapm_new_controls(dapm,
1195 component->driver->dapm_widgets,
1196 component->driver->num_dapm_widgets);
1197
1198 if (ret != 0) {
1199 dev_err(component->dev,
1200 "Failed to create new controls %d\n", ret);
1201 goto err_probe;
1202 }
1203
1204 for_each_component_dais(component, dai) {
1205 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1206 if (ret != 0) {
1207 dev_err(component->dev,
1208 "Failed to create DAI widgets %d\n", ret);
1209 goto err_probe;
1210 }
1211 }
1212
1213 ret = snd_soc_component_probe(component);
1214 if (ret < 0) {
1215 dev_err(component->dev,
1216 "ASoC: failed to probe component %d\n", ret);
1217 goto err_probe;
1218 }
1219 WARN(dapm->idle_bias_off &&
1220 dapm->bias_level != SND_SOC_BIAS_OFF,
1221 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1222 component->name);
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001223 probed = 1;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001224
Kuninori Morimoto257c4da2020-06-04 17:07:54 +09001225 /*
1226 * machine specific init
1227 * see
1228 * snd_soc_component_set_aux()
1229 */
1230 ret = snd_soc_component_init(component);
1231 if (ret < 0)
1232 goto err_probe;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001233
1234 ret = snd_soc_add_component_controls(component,
1235 component->driver->controls,
1236 component->driver->num_controls);
1237 if (ret < 0)
1238 goto err_probe;
1239
1240 ret = snd_soc_dapm_add_routes(dapm,
1241 component->driver->dapm_routes,
1242 component->driver->num_dapm_routes);
Pierre-Louis Bossarta22ae722020-03-09 14:27:43 -05001243 if (ret < 0) {
1244 if (card->disable_route_checks) {
1245 dev_info(card->dev,
1246 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1247 __func__);
1248 } else {
1249 dev_err(card->dev,
1250 "%s: snd_soc_dapm_add_routes failed: %d\n",
1251 __func__, ret);
1252 goto err_probe;
1253 }
1254 }
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001255
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001256 /* see for_each_card_components */
1257 list_add(&component->card_list, &card->component_dev_list);
1258
1259err_probe:
1260 if (ret < 0)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001261 soc_remove_component(component, probed);
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001262
1263 return ret;
1264}
1265
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001266static void soc_remove_link_dais(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001267{
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001268 struct snd_soc_pcm_runtime *rtd;
1269 int order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001270
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001271 for_each_comp_order(order) {
1272 for_each_card_rtds(card, rtd) {
Kuninori Morimoto7eaa3132020-04-24 08:15:20 +09001273 /* remove all rtd connected DAIs in good order */
1274 snd_soc_pcm_dai_remove(rtd, order);
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001275 }
1276 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277}
1278
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001279static int soc_probe_link_dais(struct snd_soc_card *card)
1280{
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001281 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto51801ae2020-04-24 08:15:15 +09001282 int order, ret;
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001283
1284 for_each_comp_order(order) {
1285 for_each_card_rtds(card, rtd) {
1286
1287 dev_dbg(card->dev,
1288 "ASoC: probe %s dai link %d late %d\n",
1289 card->name, rtd->num, order);
1290
Kuninori Morimoto51801ae2020-04-24 08:15:15 +09001291 /* probe all rtd connected DAIs in good order */
1292 ret = snd_soc_pcm_dai_probe(rtd, order);
1293 if (ret)
1294 return ret;
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001295 }
1296 }
1297
1298 return 0;
1299}
1300
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001301static void soc_remove_link_components(struct snd_soc_card *card)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001302{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001303 struct snd_soc_component *component;
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001304 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001305 int i, order;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001306
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001307 for_each_comp_order(order) {
1308 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001309 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001310 if (component->driver->remove_order != order)
1311 continue;
1312
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001313 soc_remove_component(component, 1);
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001314 }
1315 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001316 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001317}
1318
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001319static int soc_probe_link_components(struct snd_soc_card *card)
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001320{
1321 struct snd_soc_component *component;
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001322 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001323 int i, ret, order;
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001324
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001325 for_each_comp_order(order) {
1326 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001327 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001328 if (component->driver->probe_order != order)
1329 continue;
1330
1331 ret = soc_probe_component(card, component);
1332 if (ret < 0)
1333 return ret;
1334 }
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001335 }
1336 }
1337
1338 return 0;
1339}
1340
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001341static void soc_unbind_aux_dev(struct snd_soc_card *card)
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001342{
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001343 struct snd_soc_component *component, *_component;
1344
1345 for_each_card_auxs_safe(card, component, _component) {
Kuninori Morimoto257c4da2020-06-04 17:07:54 +09001346 /* for snd_soc_component_init() */
1347 snd_soc_component_set_aux(component, NULL);
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001348 list_del(&component->card_aux_list);
1349 }
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001350}
1351
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001352static int soc_bind_aux_dev(struct snd_soc_card *card)
Mark Brownb19e6e72012-03-14 21:18:39 +00001353{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001354 struct snd_soc_component *component;
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001355 struct snd_soc_aux_dev *aux;
1356 int i;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001357
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001358 for_each_card_pre_auxs(card, i, aux) {
1359 /* codecs, usually analog devices */
1360 component = soc_find_component(&aux->dlc);
1361 if (!component)
1362 return -EPROBE_DEFER;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001363
Kuninori Morimoto257c4da2020-06-04 17:07:54 +09001364 /* for snd_soc_component_init() */
1365 snd_soc_component_set_aux(component, aux);
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001366 /* see for_each_card_auxs */
1367 list_add(&component->card_aux_list, &card->aux_comp_list);
1368 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001369 return 0;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001370}
1371
1372static int soc_probe_aux_devices(struct snd_soc_card *card)
1373{
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001374 struct snd_soc_component *component;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001375 int order;
1376 int ret;
1377
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001378 for_each_comp_order(order) {
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001379 for_each_card_auxs(card, component) {
1380 if (component->driver->probe_order != order)
1381 continue;
1382
1383 ret = soc_probe_component(card, component);
1384 if (ret < 0)
1385 return ret;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001386 }
1387 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001388
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001389 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001390}
1391
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001392static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001393{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001394 struct snd_soc_component *comp, *_comp;
1395 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001396
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001397 for_each_comp_order(order) {
Kuninori Morimotoc2b71c72019-08-08 14:54:44 +09001398 for_each_card_auxs_safe(card, comp, _comp) {
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001399 if (comp->driver->remove_order == order)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001400 soc_remove_component(comp, 1);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001401 }
1402 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001403}
1404
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001405/**
1406 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1407 * @rtd: The runtime for which the DAI link format should be changed
1408 * @dai_fmt: The new DAI link format
1409 *
1410 * This function updates the DAI link format for all DAIs connected to the DAI
1411 * link for the specified runtime.
1412 *
1413 * Note: For setups with a static format set the dai_fmt field in the
1414 * corresponding snd_dai_link struct instead of using this function.
1415 *
1416 * Returns 0 on success, otherwise a negative error code.
1417 */
1418int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1419 unsigned int dai_fmt)
1420{
Shreyas NC76afa642020-02-25 21:39:12 +08001421 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001422 struct snd_soc_dai *codec_dai;
Shreyas NC76afa642020-02-25 21:39:12 +08001423 unsigned int inv_dai_fmt;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001424 unsigned int i;
1425 int ret;
1426
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001427 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001428 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1429 if (ret != 0 && ret != -ENOTSUPP) {
1430 dev_warn(codec_dai->dev,
1431 "ASoC: Failed to set DAI format: %d\n", ret);
1432 return ret;
1433 }
1434 }
1435
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001436 /*
1437 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1438 * the component which has non_legacy_dai_naming is Codec
1439 */
Shreyas NC76afa642020-02-25 21:39:12 +08001440 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1441 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1442 case SND_SOC_DAIFMT_CBM_CFM:
1443 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1444 break;
1445 case SND_SOC_DAIFMT_CBM_CFS:
1446 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1447 break;
1448 case SND_SOC_DAIFMT_CBS_CFM:
1449 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1450 break;
1451 case SND_SOC_DAIFMT_CBS_CFS:
1452 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1453 break;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001454 }
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001455 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC76afa642020-02-25 21:39:12 +08001456 unsigned int fmt = dai_fmt;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001457
Shreyas NC76afa642020-02-25 21:39:12 +08001458 if (cpu_dai->component->driver->non_legacy_dai_naming)
1459 fmt = inv_dai_fmt;
1460
1461 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1462 if (ret != 0 && ret != -ENOTSUPP) {
1463 dev_warn(cpu_dai->dev,
1464 "ASoC: Failed to set DAI format: %d\n", ret);
1465 return ret;
1466 }
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001467 }
1468
1469 return 0;
1470}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001471EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001472
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001473#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001474/*
Kuninori Morimoto8a6a6a32019-12-11 13:32:05 +09001475 * If a DMI filed contain strings in this blacklist (e.g.
1476 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1477 * as invalid and dropped when setting the card long name from DMI info.
1478 */
1479static const char * const dmi_blacklist[] = {
1480 "To be filled by OEM",
1481 "TBD by OEM",
1482 "Default String",
1483 "Board Manufacturer",
1484 "Board Vendor Name",
1485 "Board Product Name",
1486 NULL, /* terminator */
1487};
1488
1489/*
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001490 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001491 * separate different DMI fields in the card long name. Only number and
1492 * alphabet characters and a few separator characters are kept.
1493 */
1494static void cleanup_dmi_name(char *name)
1495{
1496 int i, j = 0;
1497
1498 for (i = 0; name[i]; i++) {
1499 if (isalnum(name[i]) || (name[i] == '.')
1500 || (name[i] == '_'))
1501 name[j++] = name[i];
1502 else if (name[i] == '-')
1503 name[j++] = '_';
1504 }
1505
1506 name[j] = '\0';
1507}
1508
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001509/*
1510 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001511 * in the black list.
1512 */
1513static int is_dmi_valid(const char *field)
1514{
1515 int i = 0;
1516
1517 while (dmi_blacklist[i]) {
1518 if (strstr(field, dmi_blacklist[i]))
1519 return 0;
1520 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001521 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001522
1523 return 1;
1524}
1525
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001526/*
1527 * Append a string to card->dmi_longname with character cleanups.
1528 */
1529static void append_dmi_string(struct snd_soc_card *card, const char *str)
1530{
1531 char *dst = card->dmi_longname;
1532 size_t dst_len = sizeof(card->dmi_longname);
1533 size_t len;
1534
1535 len = strlen(dst);
1536 snprintf(dst + len, dst_len - len, "-%s", str);
1537
1538 len++; /* skip the separator "-" */
1539 if (len < dst_len)
1540 cleanup_dmi_name(dst + len);
1541}
1542
Liam Girdwood345233d2017-01-14 16:13:02 +08001543/**
1544 * snd_soc_set_dmi_name() - Register DMI names to card
1545 * @card: The card to register DMI names
1546 * @flavour: The flavour "differentiator" for the card amongst its peers.
1547 *
1548 * An Intel machine driver may be used by many different devices but are
1549 * difficult for userspace to differentiate, since machine drivers ususally
1550 * use their own name as the card short name and leave the card long name
1551 * blank. To differentiate such devices and fix bugs due to lack of
1552 * device-specific configurations, this function allows DMI info to be used
1553 * as the sound card long name, in the format of
1554 * "vendor-product-version-board"
1555 * (Character '-' is used to separate different DMI fields here).
1556 * This will help the user space to load the device-specific Use Case Manager
1557 * (UCM) configurations for the card.
1558 *
1559 * Possible card long names may be:
1560 * DellInc.-XPS139343-01-0310JH
1561 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1562 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1563 *
1564 * This function also supports flavoring the card longname to provide
1565 * the extra differentiation, like "vendor-product-version-board-flavor".
1566 *
1567 * We only keep number and alphabet characters and a few separator characters
1568 * in the card long name since UCM in the user space uses the card long names
1569 * as card configuration directory names and AudoConf cannot support special
1570 * charactors like SPACE.
1571 *
1572 * Returns 0 on success, otherwise a negative error code.
1573 */
1574int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1575{
1576 const char *vendor, *product, *product_version, *board;
Liam Girdwood345233d2017-01-14 16:13:02 +08001577
1578 if (card->long_name)
1579 return 0; /* long name already set by driver or from DMI */
1580
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001581 /* make up dmi long name as: vendor-product-version-board */
Liam Girdwood345233d2017-01-14 16:13:02 +08001582 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001583 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001584 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1585 return 0;
1586 }
1587
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001588 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
Liam Girdwood345233d2017-01-14 16:13:02 +08001589 cleanup_dmi_name(card->dmi_longname);
1590
1591 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001592 if (product && is_dmi_valid(product)) {
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001593 append_dmi_string(card, product);
Liam Girdwood345233d2017-01-14 16:13:02 +08001594
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001595 /*
1596 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001597 * name in the product version field
1598 */
1599 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001600 if (product_version && is_dmi_valid(product_version))
1601 append_dmi_string(card, product_version);
Liam Girdwood345233d2017-01-14 16:13:02 +08001602 }
1603
1604 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001605 if (board && is_dmi_valid(board)) {
Jaroslav Kysela39870b02019-11-20 18:44:35 +01001606 if (!product || strcasecmp(board, product))
1607 append_dmi_string(card, board);
Liam Girdwood345233d2017-01-14 16:13:02 +08001608 } else if (!product) {
1609 /* fall back to using legacy name */
1610 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1611 return 0;
1612 }
1613
1614 /* Add flavour to dmi long name */
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001615 if (flavour)
1616 append_dmi_string(card, flavour);
Liam Girdwood345233d2017-01-14 16:13:02 +08001617
1618 /* set the card long name */
1619 card->long_name = card->dmi_longname;
1620
1621 return 0;
1622}
1623EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001624#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001625
Liam Girdwooda655de82018-07-02 16:59:54 +01001626static void soc_check_tplg_fes(struct snd_soc_card *card)
1627{
1628 struct snd_soc_component *component;
1629 const struct snd_soc_component_driver *comp_drv;
1630 struct snd_soc_dai_link *dai_link;
1631 int i;
1632
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001633 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001634
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001635 /* does this component override BEs ? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001636 if (!component->driver->ignore_machine)
1637 continue;
1638
1639 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001640 if (!strcmp(component->driver->ignore_machine,
1641 card->dev->driver->name))
1642 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001643 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001644 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001645 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001646match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001647 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001648 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001649
1650 /* ignore this FE */
1651 if (dai_link->dynamic) {
1652 dai_link->ignore = true;
1653 continue;
1654 }
1655
Pierre-Louis Bossart13289482020-06-12 15:40:48 -05001656 dev_dbg(card->dev, "info: override BE DAI link %s\n",
1657 card->dai_link[i].name);
Liam Girdwooda655de82018-07-02 16:59:54 +01001658
1659 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001660 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001661 dev_err(card->dev, "init platform error");
1662 continue;
1663 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001664 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001665
1666 /* convert non BE into BE */
Bard Liao607fa202020-06-08 14:44:13 -05001667 if (!dai_link->no_pcm) {
1668 dai_link->no_pcm = 1;
1669
1670 if (dai_link->dpcm_playback)
1671 dev_warn(card->dev,
1672 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_playback=1\n",
1673 dai_link->name);
1674 if (dai_link->dpcm_capture)
1675 dev_warn(card->dev,
1676 "invalid configuration, dailink %s has flags no_pcm=0 and dpcm_capture=1\n",
1677 dai_link->name);
1678
1679 /* convert normal link into DPCM one */
1680 if (!(dai_link->dpcm_playback ||
1681 dai_link->dpcm_capture)) {
1682 dai_link->dpcm_playback = !dai_link->capture_only;
1683 dai_link->dpcm_capture = !dai_link->playback_only;
1684 }
1685 }
Liam Girdwooda655de82018-07-02 16:59:54 +01001686
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +09001687 /*
1688 * override any BE fixups
1689 * see
1690 * snd_soc_link_be_hw_params_fixup()
1691 */
Liam Girdwooda655de82018-07-02 16:59:54 +01001692 dai_link->be_hw_params_fixup =
1693 component->driver->be_hw_params_fixup;
1694
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001695 /*
1696 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001697 * dai link name if it's NULL to help bind widgets.
1698 */
1699 if (!dai_link->stream_name)
1700 dai_link->stream_name = dai_link->name;
1701 }
1702
1703 /* Inform userspace we are using alternate topology */
1704 if (component->driver->topology_name_prefix) {
1705
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001706 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001707 if (!card->topology_shortname_created) {
1708 comp_drv = component->driver;
1709
1710 snprintf(card->topology_shortname, 32, "%s-%s",
1711 comp_drv->topology_name_prefix,
1712 card->name);
1713 card->topology_shortname_created = true;
1714 }
1715
1716 /* use topology shortname */
1717 card->name = card->topology_shortname;
1718 }
1719 }
1720}
1721
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001722#define soc_setup_card_name(name, name1, name2, norm) \
1723 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1724static void __soc_setup_card_name(char *name, int len,
1725 const char *name1, const char *name2,
1726 int normalization)
1727{
1728 int i;
1729
1730 snprintf(name, len, "%s", name1 ? name1 : name2);
1731
1732 if (!normalization)
1733 return;
1734
1735 /*
1736 * Name normalization
1737 *
1738 * The driver name is somewhat special, as it's used as a key for
1739 * searches in the user-space.
1740 *
1741 * ex)
1742 * "abcd??efg" -> "abcd__efg"
1743 */
1744 for (i = 0; i < len; i++) {
1745 switch (name[i]) {
1746 case '_':
1747 case '-':
1748 case '\0':
1749 break;
1750 default:
1751 if (!isalnum(name[i]))
1752 name[i] = '_';
1753 break;
1754 }
1755 }
1756}
1757
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001758static void soc_cleanup_card_resources(struct snd_soc_card *card)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001759{
Kuninori Morimotocc733902019-12-10 09:33:40 +09001760 struct snd_soc_pcm_runtime *rtd, *n;
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001761
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001762 if (card->snd_card)
1763 snd_card_disconnect_sync(card->snd_card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001764
Kuninori Morimoto2a6f0892019-11-13 10:16:29 +09001765 snd_soc_dapm_shutdown(card);
1766
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001767 /* remove and free each DAI */
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001768 soc_remove_link_dais(card);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001769 soc_remove_link_components(card);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001770
Kuninori Morimotocc733902019-12-10 09:33:40 +09001771 for_each_card_rtds_safe(card, rtd, n)
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +09001772 snd_soc_remove_pcm_runtime(card, rtd);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001773
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001774 /* remove auxiliary devices */
1775 soc_remove_aux_devices(card);
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001776 soc_unbind_aux_dev(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001777
1778 snd_soc_dapm_free(&card->dapm);
1779 soc_cleanup_card_debugfs(card);
1780
1781 /* remove the card */
Kuninori Morimotob0275d92020-05-28 10:49:26 +09001782 snd_soc_card_remove(card);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001783
1784 if (card->snd_card) {
1785 snd_card_free(card->snd_card);
1786 card->snd_card = NULL;
1787 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001788}
1789
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001790static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1791{
1792 if (card->instantiated) {
1793 card->instantiated = false;
1794 snd_soc_flush_all_delayed_work(card);
1795
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001796 soc_cleanup_card_resources(card);
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001797 if (!unregister)
1798 list_add(&card->list, &unbind_card_list);
1799 } else {
1800 if (unregister)
1801 list_del(&card->list);
1802 }
1803}
1804
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001805static int snd_soc_bind_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001806{
Mengdong Lin1a497982015-11-18 02:34:11 -05001807 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001808 struct snd_soc_component *component;
Mengdong Lin61b00882015-12-02 14:11:48 +08001809 struct snd_soc_dai_link *dai_link;
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001810 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001812 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001813 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001815 snd_soc_dapm_init(&card->dapm, card, NULL);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001816
Liam Girdwooda655de82018-07-02 16:59:54 +01001817 /* check whether any platform is ignore machine FE and using topology */
1818 soc_check_tplg_fes(card);
1819
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001820 /* bind aux_devs too */
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001821 ret = soc_bind_aux_dev(card);
1822 if (ret < 0)
1823 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824
Mengdong Linf8f80362015-12-02 14:11:22 +08001825 /* add predefined DAI links to the list */
Kuninori Morimotod8145982019-10-02 14:23:07 +09001826 card->num_rtd = 0;
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001827 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimoto0c048002019-12-10 09:34:19 +09001828 ret = snd_soc_add_pcm_runtime(card, dai_link);
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001829 if (ret < 0)
1830 goto probe_end;
1831 }
Mengdong Linf8f80362015-12-02 14:11:22 +08001832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001833 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001834 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001835 card->owner, 0, &card->snd_card);
1836 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001837 dev_err(card->dev,
1838 "ASoC: can't create sound card for card %s: %d\n",
1839 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001840 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001841 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001843 soc_init_card_debugfs(card);
1844
Kuninori Morimotob3da4252019-08-07 10:31:24 +09001845 soc_resume_init(card);
Andy Green6ed25972008-06-13 16:24:05 +01001846
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001847 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1848 card->num_dapm_widgets);
1849 if (ret < 0)
1850 goto probe_end;
Mark Brown9a841eb2011-04-12 17:51:37 -07001851
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001852 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1853 card->num_of_dapm_widgets);
1854 if (ret < 0)
1855 goto probe_end;
Nicolin Chenf23e8602015-02-14 17:22:49 -08001856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001857 /* initialise the sound card only once */
Kuninori Morimoto73de4b022020-05-28 10:49:11 +09001858 ret = snd_soc_card_probe(card);
1859 if (ret < 0)
1860 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001861
Stephen Warren62ae68f2012-06-08 12:34:23 -06001862 /* probe all components used by DAI links on this card */
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001863 ret = soc_probe_link_components(card);
1864 if (ret < 0) {
1865 dev_err(card->dev,
1866 "ASoC: failed to instantiate card %d\n", ret);
1867 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001868 }
1869
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001870 /* probe auxiliary components */
1871 ret = soc_probe_aux_devices(card);
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001872 if (ret < 0) {
1873 dev_err(card->dev,
1874 "ASoC: failed to probe aux component %d\n", ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001875 goto probe_end;
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001876 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001877
Stephen Warren62ae68f2012-06-08 12:34:23 -06001878 /* probe all DAI links on this card */
Kuninori Morimotoc7e73772019-09-04 09:15:17 +09001879 ret = soc_probe_link_dais(card);
1880 if (ret < 0) {
1881 dev_err(card->dev,
1882 "ASoC: failed to instantiate card %d\n", ret);
1883 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001884 }
1885
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001886 for_each_card_rtds(card, rtd) {
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001887 ret = soc_init_pcm_runtime(card, rtd);
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001888 if (ret < 0)
1889 goto probe_end;
1890 }
Kuninori Morimotoc4b46982019-09-04 09:15:12 +09001891
Mark Brown888df392012-02-16 19:37:51 -08001892 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001893 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001894
Kuninori Morimoto9b98c7c2019-08-07 10:31:08 +09001895 ret = snd_soc_add_card_controls(card, card->controls,
1896 card->num_controls);
1897 if (ret < 0)
1898 goto probe_end;
Mark Brownb7af1da2011-04-07 19:18:44 +09001899
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001900 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1901 card->num_dapm_routes);
Pierre-Louis Bossarta22ae722020-03-09 14:27:43 -05001902 if (ret < 0) {
1903 if (card->disable_route_checks) {
1904 dev_info(card->dev,
1905 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1906 __func__);
1907 } else {
1908 dev_err(card->dev,
1909 "%s: snd_soc_dapm_add_routes failed: %d\n",
1910 __func__, ret);
1911 goto probe_end;
1912 }
1913 }
Mark Brownb8ad29d2011-03-02 18:35:51 +00001914
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001915 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1916 card->num_of_dapm_routes);
1917 if (ret < 0)
1918 goto probe_end;
Mark Brown75d9ac42011-09-27 16:41:01 +01001919
Takashi Iwai861886d2017-04-24 08:54:42 +02001920 /* try to set some sane longname if DMI is available */
1921 snd_soc_set_dmi_name(card, NULL);
1922
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001923 soc_setup_card_name(card->snd_card->shortname,
1924 card->name, NULL, 0);
1925 soc_setup_card_name(card->snd_card->longname,
1926 card->long_name, card->name, 0);
1927 soc_setup_card_name(card->snd_card->driver,
1928 card->driver_name, card->name, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929
Jaroslav Kyseladc73d732019-11-19 18:49:32 +01001930 if (card->components) {
1931 /* the current implementation of snd_component_add() accepts */
1932 /* multiple components in the string separated by space, */
1933 /* but the string collision (identical string) check might */
1934 /* not work correctly */
1935 ret = snd_component_add(card->snd_card, card->components);
1936 if (ret < 0) {
1937 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1938 card->name, ret);
1939 goto probe_end;
1940 }
1941 }
1942
Kuninori Morimoto5c0eac02020-05-28 10:49:20 +09001943 ret = snd_soc_card_late_probe(card);
1944 if (ret < 0)
1945 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00001946
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001947 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001949 ret = snd_card_register(card->snd_card);
1950 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001951 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1952 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001953 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001954 }
1955
Mark Brown435c5e22008-12-04 15:32:53 +00001956 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08001957 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01001958 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00001959
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001960 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001961 for_each_card_components(card, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001962 if (!snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001963 pinctrl_pm_select_sleep_state(component->dev);
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001964
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001965probe_end:
1966 if (ret < 0)
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001967 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001970 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971
Mark Brownb19e6e72012-03-14 21:18:39 +00001972 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001973}
1974
1975/* probes a new socdev */
1976static int soc_probe(struct platform_device *pdev)
1977{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001979
Vinod Koul70a7ca32011-01-14 19:22:48 +05301980 /*
1981 * no card, so machine driver should be registering card
1982 * we should not be here in that case so ret error
1983 */
1984 if (!card)
1985 return -EINVAL;
1986
Mark Brownfe4085e2012-03-02 13:07:41 +00001987 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001988 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001989 card->name);
1990
Mark Brown435c5e22008-12-04 15:32:53 +00001991 /* Bodge while we unpick instantiation */
1992 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001993
Qinglang Miaoad61b782020-09-29 19:29:33 +08001994 return devm_snd_soc_register_card(&pdev->dev, card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995}
1996
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001997int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001998{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001999 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09002000 struct snd_soc_component *component;
Mark Brown51737472009-06-22 13:16:51 +01002001
2002 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002003 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002004
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002005 /*
2006 * Flush out pmdown_time work - we actually do want to run it
2007 * now, we're shutting down so no imminent restart.
2008 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002009 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002011 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002012
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002013 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09002014 for_each_card_components(card, component)
2015 pinctrl_pm_select_sleep_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002016
Mark Brown416356f2009-06-30 19:05:15 +01002017 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002018}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002019EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002020
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002021const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302022 .suspend = snd_soc_suspend,
2023 .resume = snd_soc_resume,
2024 .freeze = snd_soc_suspend,
2025 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002026 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302027 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002028};
Stephen Warrendeb26072011-04-05 19:35:30 -06002029EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002030
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031/* ASoC platform driver */
2032static struct platform_driver soc_driver = {
2033 .driver = {
2034 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002035 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 },
2037 .probe = soc_probe,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038};
2039
Mark Brown096e49d2009-07-05 15:12:22 +01002040/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 * snd_soc_cnew - create new control
2042 * @_template: control template
2043 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002044 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002045 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046 *
2047 * Create a new mixer control from a template control.
2048 *
2049 * Returns 0 for success, else error.
2050 */
2051struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002052 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002053 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054{
2055 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002056 struct snd_kcontrol *kcontrol;
2057 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002058
2059 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002060 template.index = 0;
2061
Mark Brownefb7ac32011-03-08 17:23:24 +00002062 if (!long_name)
2063 long_name = template.name;
2064
2065 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002066 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002067 if (!name)
2068 return NULL;
2069
Mark Brownefb7ac32011-03-08 17:23:24 +00002070 template.name = name;
2071 } else {
2072 template.name = long_name;
2073 }
2074
2075 kcontrol = snd_ctl_new1(&template, data);
2076
2077 kfree(name);
2078
2079 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080}
2081EXPORT_SYMBOL_GPL(snd_soc_cnew);
2082
Liam Girdwood022658b2012-02-03 17:43:09 +00002083static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2084 const struct snd_kcontrol_new *controls, int num_controls,
2085 const char *prefix, void *data)
2086{
2087 int err, i;
2088
2089 for (i = 0; i < num_controls; i++) {
2090 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002091
Liam Girdwood022658b2012-02-03 17:43:09 +00002092 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2093 control->name, prefix));
2094 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002095 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2096 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002097 return err;
2098 }
2099 }
2100
2101 return 0;
2102}
2103
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002105 * snd_soc_add_component_controls - Add an array of controls to a component.
2106 *
2107 * @component: Component to add controls to
2108 * @controls: Array of controls to add
2109 * @num_controls: Number of elements in the array
2110 *
2111 * Return: 0 for success, else error.
2112 */
2113int snd_soc_add_component_controls(struct snd_soc_component *component,
2114 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2115{
2116 struct snd_card *card = component->card->snd_card;
2117
2118 return snd_soc_add_controls(card, component->dev, controls,
2119 num_controls, component->name_prefix, component);
2120}
2121EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2122
2123/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002124 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2125 * Convenience function to add a list of controls.
2126 *
2127 * @soc_card: SoC card to add controls to
2128 * @controls: array of controls to add
2129 * @num_controls: number of elements in the array
2130 *
2131 * Return 0 for success, else error.
2132 */
2133int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2134 const struct snd_kcontrol_new *controls, int num_controls)
2135{
2136 struct snd_card *card = soc_card->snd_card;
2137
2138 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2139 NULL, soc_card);
2140}
2141EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2142
2143/**
2144 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2145 * Convienience function to add a list of controls.
2146 *
2147 * @dai: DAI to add controls to
2148 * @controls: array of controls to add
2149 * @num_controls: number of elements in the array
2150 *
2151 * Return 0 for success, else error.
2152 */
2153int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2154 const struct snd_kcontrol_new *controls, int num_controls)
2155{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002156 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002157
2158 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2159 NULL, dai);
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2162
Mark Brownc5af3a22008-11-28 13:29:45 +00002163/**
2164 * snd_soc_register_card - Register a card with the ASoC core
2165 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002166 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002167 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002168 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302169int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002170{
2171 if (!card->name || !card->dev)
2172 return -EINVAL;
2173
Mark Browned77cc12011-05-03 18:25:34 +01002174 dev_set_drvdata(card->dev, card);
2175
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002176 INIT_LIST_HEAD(&card->widgets);
2177 INIT_LIST_HEAD(&card->paths);
2178 INIT_LIST_HEAD(&card->dapm_list);
2179 INIT_LIST_HEAD(&card->aux_comp_list);
2180 INIT_LIST_HEAD(&card->component_dev_list);
2181 INIT_LIST_HEAD(&card->list);
Mengdong Lin1a497982015-11-18 02:34:11 -05002182 INIT_LIST_HEAD(&card->rtd_list);
Mark Browndb432b42011-10-03 21:06:40 +01002183 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002184 INIT_LIST_HEAD(&card->dobj_list);
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002185
Mark Brownc5af3a22008-11-28 13:29:45 +00002186 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002187 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002188 mutex_init(&card->dapm_mutex);
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03002189 mutex_init(&card->pcm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002190 spin_lock_init(&card->dpcm_lock);
Mark Brownc5af3a22008-11-28 13:29:45 +00002191
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002192 return snd_soc_bind_card(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002193}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302194EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002195
2196/**
2197 * snd_soc_unregister_card - Unregister a card with the ASoC core
2198 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002199 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002200 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002201 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302202int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002203{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002204 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002205 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002206 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002207 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002208
2209 return 0;
2210}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302211EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002212
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002213/*
2214 * Simplify DAI link configuration by removing ".-1" from device names
2215 * and sanitizing names.
2216 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002217static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002218{
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002219 const char *devname = dev_name(dev);
2220 char *found, *name;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002221 int id1, id2;
2222
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002223 if (devname == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002224 return NULL;
2225
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002226 name = devm_kstrdup(dev, devname, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002227
2228 /* are we a "%s.%d" name (platform and SPI components) */
2229 found = strstr(name, dev->driver->name);
2230 if (found) {
2231 /* get ID */
2232 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2233
2234 /* discard ID from name if ID == -1 */
2235 if (*id == -1)
2236 found[strlen(dev->driver->name)] = '\0';
2237 }
2238
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002239 /* I2C component devices are named "bus-addr" */
2240 } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2241
2242 /* create unique ID number from I2C addr and bus */
2243 *id = ((id1 & 0xffff) << 16) + id2;
2244
2245 devm_kfree(dev, name);
2246
2247 /* sanitize component name for DAI link creation */
2248 name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002249 } else {
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002250 *id = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002251 }
2252
Dmitry Baryshkov45dd9942020-08-27 23:51:00 +03002253 return name;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254}
2255
2256/*
2257 * Simplify DAI link naming for single devices with multiple DAIs by removing
2258 * any ".-1" and using the DAI name (instead of device name).
2259 */
2260static inline char *fmt_multiple_name(struct device *dev,
2261 struct snd_soc_dai_driver *dai_drv)
2262{
2263 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002264 dev_err(dev,
2265 "ASoC: error - multiple DAI %s registered with no name\n",
2266 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002267 return NULL;
2268 }
2269
Kuninori Morimoto50014492019-10-02 14:22:57 +09002270 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002271}
2272
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002273void snd_soc_unregister_dai(struct snd_soc_dai *dai)
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002274{
2275 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2276 list_del(&dai->list);
2277}
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002278EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002279
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002280/**
2281 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2282 *
2283 * @component: The component the DAIs are registered for
2284 * @dai_drv: DAI driver to use for the DAI
Randy Dunlapbc9a6652019-12-08 20:37:04 -08002285 * @legacy_dai_naming: if %true, use legacy single-name format;
2286 * if %false, use multiple-name format;
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002287 *
2288 * Topology can use this API to register DAIs when probing a component.
2289 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2290 * will be freed in the component cleanup.
2291 */
2292struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2293 struct snd_soc_dai_driver *dai_drv,
2294 bool legacy_dai_naming)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002295{
2296 struct device *dev = component->dev;
2297 struct snd_soc_dai *dai;
2298
2299 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2300
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002301 lockdep_assert_held(&client_mutex);
2302
Kuninori Morimoto50014492019-10-02 14:22:57 +09002303 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002304 if (dai == NULL)
2305 return NULL;
2306
2307 /*
2308 * Back in the old days when we still had component-less DAIs,
2309 * instead of having a static name, component-less DAIs would
2310 * inherit the name of the parent device so it is possible to
2311 * register multiple instances of the DAI. We still need to keep
2312 * the same naming style even though those DAIs are not
2313 * component-less anymore.
2314 */
2315 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002316 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002317 dai->name = fmt_single_name(dev, &dai->id);
2318 } else {
2319 dai->name = fmt_multiple_name(dev, dai_drv);
2320 if (dai_drv->id)
2321 dai->id = dai_drv->id;
2322 else
2323 dai->id = component->num_dai;
2324 }
Kuninori Morimoto50014492019-10-02 14:22:57 +09002325 if (!dai->name)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002326 return NULL;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002327
2328 dai->component = component;
2329 dai->dev = dev;
2330 dai->driver = dai_drv;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002331
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002332 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002333 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002334 component->num_dai++;
2335
2336 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2337 return dai;
2338}
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002339
Mark Brown91151712008-11-30 23:31:24 +00002340/**
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002341 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2342 *
2343 * @component: The component for which the DAIs should be unregistered
2344 */
2345static void snd_soc_unregister_dais(struct snd_soc_component *component)
2346{
2347 struct snd_soc_dai *dai, *_dai;
2348
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002349 for_each_component_dais_safe(component, dai, _dai)
2350 snd_soc_unregister_dai(dai);
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002351}
2352
2353/**
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002354 * snd_soc_register_dais - Register a DAI with the ASoC core
2355 *
2356 * @component: The component the DAIs are registered for
2357 * @dai_drv: DAI driver to use for the DAIs
2358 * @count: Number of DAIs
2359 */
2360static int snd_soc_register_dais(struct snd_soc_component *component,
2361 struct snd_soc_dai_driver *dai_drv,
2362 size_t count)
2363{
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002364 struct snd_soc_dai *dai;
2365 unsigned int i;
2366 int ret;
2367
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002368 for (i = 0; i < count; i++) {
Kuninori Morimoto71cb85f2019-11-05 15:47:18 +09002369 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002370 !component->driver->non_legacy_dai_naming);
2371 if (dai == NULL) {
2372 ret = -ENOMEM;
2373 goto err;
2374 }
2375 }
2376
2377 return 0;
2378
2379err:
2380 snd_soc_unregister_dais(component);
2381
2382 return ret;
2383}
2384
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002385#define ENDIANNESS_MAP(name) \
2386 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2387static u64 endianness_format_map[] = {
2388 ENDIANNESS_MAP(S16_),
2389 ENDIANNESS_MAP(U16_),
2390 ENDIANNESS_MAP(S24_),
2391 ENDIANNESS_MAP(U24_),
2392 ENDIANNESS_MAP(S32_),
2393 ENDIANNESS_MAP(U32_),
2394 ENDIANNESS_MAP(S24_3),
2395 ENDIANNESS_MAP(U24_3),
2396 ENDIANNESS_MAP(S20_3),
2397 ENDIANNESS_MAP(U20_3),
2398 ENDIANNESS_MAP(S18_3),
2399 ENDIANNESS_MAP(U18_3),
2400 ENDIANNESS_MAP(FLOAT_),
2401 ENDIANNESS_MAP(FLOAT64_),
2402 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2403};
2404
2405/*
2406 * Fix up the DAI formats for endianness: codecs don't actually see
2407 * the endianness of the data but we're using the CPU format
2408 * definitions which do need to include endianness so we ensure that
2409 * codec DAIs always have both big and little endian variants set.
2410 */
2411static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2412{
2413 int i;
2414
2415 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2416 if (stream->formats & endianness_format_map[i])
2417 stream->formats |= endianness_format_map[i];
2418}
2419
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002420static void snd_soc_try_rebind_card(void)
2421{
2422 struct snd_soc_card *card, *c;
2423
Kuninori Morimotob245d272019-08-07 10:31:19 +09002424 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2425 if (!snd_soc_bind_card(card))
2426 list_del(&card->list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002427}
2428
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002429static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2430{
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002431 struct snd_soc_card *card = component->card;
2432
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002433 snd_soc_unregister_dais(component);
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002434
2435 if (card)
2436 snd_soc_unbind_card(card, false);
2437
2438 list_del(&component->list);
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002439}
2440
Cezary Rojewski08ff7202020-07-31 16:41:44 +02002441int snd_soc_component_initialize(struct snd_soc_component *component,
2442 const struct snd_soc_component_driver *driver,
Cezary Rojewski7274d4c2020-07-31 16:41:45 +02002443 struct device *dev)
Cezary Rojewski08ff7202020-07-31 16:41:44 +02002444{
2445 INIT_LIST_HEAD(&component->dai_list);
2446 INIT_LIST_HEAD(&component->dobj_list);
2447 INIT_LIST_HEAD(&component->card_list);
2448 mutex_init(&component->io_mutex);
2449
Cezary Rojewski7274d4c2020-07-31 16:41:45 +02002450 component->name = fmt_single_name(dev, &component->id);
2451 if (!component->name) {
2452 dev_err(dev, "ASoC: Failed to allocate name\n");
2453 return -ENOMEM;
2454 }
2455
Cezary Rojewski08ff7202020-07-31 16:41:44 +02002456 component->dev = dev;
2457 component->driver = driver;
2458
2459 return 0;
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2462
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002463int snd_soc_add_component(struct snd_soc_component *component,
2464 struct snd_soc_dai_driver *dai_drv,
2465 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002466{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002467 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002468 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002469
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002470 mutex_lock(&client_mutex);
2471
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002472 if (component->driver->endianness) {
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002473 for (i = 0; i < num_dai; i++) {
2474 convert_endianness_formats(&dai_drv[i].playback);
2475 convert_endianness_formats(&dai_drv[i].capture);
2476 }
2477 }
2478
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002479 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002480 if (ret < 0) {
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002481 dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2482 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002483 goto err_cleanup;
2484 }
2485
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002486 if (!component->driver->write && !component->driver->read) {
2487 if (!component->regmap)
2488 component->regmap = dev_get_regmap(component->dev,
2489 NULL);
2490 if (component->regmap)
2491 snd_soc_component_setup_regmap(component);
2492 }
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002493
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002494 /* see for_each_component */
2495 list_add(&component->list, &component_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002496
2497err_cleanup:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002498 if (ret < 0)
2499 snd_soc_del_component_unlocked(component);
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002500
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002501 mutex_unlock(&client_mutex);
2502
2503 if (ret == 0)
2504 snd_soc_try_rebind_card();
2505
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002506 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002507}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002508EXPORT_SYMBOL_GPL(snd_soc_add_component);
2509
2510int snd_soc_register_component(struct device *dev,
2511 const struct snd_soc_component_driver *component_driver,
2512 struct snd_soc_dai_driver *dai_drv,
2513 int num_dai)
2514{
2515 struct snd_soc_component *component;
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002516 int ret;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002517
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002518 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002519 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002520 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002521
Cezary Rojewskiea029dd2020-07-31 16:41:46 +02002522 ret = snd_soc_component_initialize(component, component_driver, dev);
2523 if (ret < 0)
2524 return ret;
2525
2526 return snd_soc_add_component(component, dai_drv, num_dai);
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002527}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002528EXPORT_SYMBOL_GPL(snd_soc_register_component);
2529
2530/**
Maxime Ripard58f30150f2020-07-07 09:42:37 +02002531 * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2532 * from the ASoC core
2533 *
2534 * @dev: The device to unregister
2535 * @component_driver: The component driver to unregister
2536 */
2537void snd_soc_unregister_component_by_driver(struct device *dev,
2538 const struct snd_soc_component_driver *component_driver)
2539{
2540 struct snd_soc_component *component;
2541
2542 if (!component_driver)
2543 return;
2544
2545 mutex_lock(&client_mutex);
2546 component = snd_soc_lookup_component_nolocked(dev, component_driver->name);
2547 if (!component)
2548 goto out;
2549
2550 snd_soc_del_component_unlocked(component);
2551
2552out:
2553 mutex_unlock(&client_mutex);
2554}
2555EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2556
2557/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002558 * snd_soc_unregister_component - Unregister all related component
2559 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002560 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002561 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002562 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002563void snd_soc_unregister_component(struct device *dev)
2564{
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002565 struct snd_soc_component *component;
2566
2567 mutex_lock(&client_mutex);
2568 while (1) {
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +09002569 component = snd_soc_lookup_component_nolocked(dev, NULL);
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002570 if (!component)
2571 break;
2572
2573 snd_soc_del_component_unlocked(component);
2574 }
2575 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002576}
2577EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2578
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002579/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002580int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2581 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002582{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002583 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002584 int ret;
2585
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302586 if (!card->dev) {
2587 pr_err("card->dev is not set before calling %s\n", __func__);
2588 return -EINVAL;
2589 }
2590
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002591 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302592
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002593 ret = of_property_read_string_index(np, propname, 0, &card->name);
2594 /*
2595 * EINVAL means the property does not exist. This is fine providing
2596 * card->name was previously set, which is checked later in
2597 * snd_soc_register_card.
2598 */
2599 if (ret < 0 && ret != -EINVAL) {
2600 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002601 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002602 propname, ret);
2603 return ret;
2604 }
2605
2606 return 0;
2607}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002608EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002609
Xiubo Li9a6d4862014-02-08 15:59:52 +08002610static const struct snd_soc_dapm_widget simple_widgets[] = {
2611 SND_SOC_DAPM_MIC("Microphone", NULL),
2612 SND_SOC_DAPM_LINE("Line", NULL),
2613 SND_SOC_DAPM_HP("Headphone", NULL),
2614 SND_SOC_DAPM_SPK("Speaker", NULL),
2615};
2616
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002617int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002618 const char *propname)
2619{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002620 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002621 struct snd_soc_dapm_widget *widgets;
2622 const char *template, *wname;
2623 int i, j, num_widgets, ret;
2624
2625 num_widgets = of_property_count_strings(np, propname);
2626 if (num_widgets < 0) {
2627 dev_err(card->dev,
2628 "ASoC: Property '%s' does not exist\n", propname);
2629 return -EINVAL;
2630 }
2631 if (num_widgets & 1) {
2632 dev_err(card->dev,
2633 "ASoC: Property '%s' length is not even\n", propname);
2634 return -EINVAL;
2635 }
2636
2637 num_widgets /= 2;
2638 if (!num_widgets) {
2639 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2640 propname);
2641 return -EINVAL;
2642 }
2643
2644 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2645 GFP_KERNEL);
2646 if (!widgets) {
2647 dev_err(card->dev,
2648 "ASoC: Could not allocate memory for widgets\n");
2649 return -ENOMEM;
2650 }
2651
2652 for (i = 0; i < num_widgets; i++) {
2653 ret = of_property_read_string_index(np, propname,
2654 2 * i, &template);
2655 if (ret) {
2656 dev_err(card->dev,
2657 "ASoC: Property '%s' index %d read error:%d\n",
2658 propname, 2 * i, ret);
2659 return -EINVAL;
2660 }
2661
2662 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2663 if (!strncmp(template, simple_widgets[j].name,
2664 strlen(simple_widgets[j].name))) {
2665 widgets[i] = simple_widgets[j];
2666 break;
2667 }
2668 }
2669
2670 if (j >= ARRAY_SIZE(simple_widgets)) {
2671 dev_err(card->dev,
2672 "ASoC: DAPM widget '%s' is not supported\n",
2673 template);
2674 return -EINVAL;
2675 }
2676
2677 ret = of_property_read_string_index(np, propname,
2678 (2 * i) + 1,
2679 &wname);
2680 if (ret) {
2681 dev_err(card->dev,
2682 "ASoC: Property '%s' index %d read error:%d\n",
2683 propname, (2 * i) + 1, ret);
2684 return -EINVAL;
2685 }
2686
2687 widgets[i].name = wname;
2688 }
2689
Nicolin Chenf23e8602015-02-14 17:22:49 -08002690 card->of_dapm_widgets = widgets;
2691 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002692
2693 return 0;
2694}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002695EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08002696
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002697int snd_soc_of_get_slot_mask(struct device_node *np,
2698 const char *prop_name,
2699 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03002700{
2701 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03002702 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03002703 int i;
2704
2705 if (!of_slot_mask)
2706 return 0;
2707 val /= sizeof(u32);
2708 for (i = 0; i < val; i++)
2709 if (be32_to_cpup(&of_slot_mask[i]))
2710 *mask |= (1 << i);
2711
2712 return val;
2713}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002714EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03002715
Xiubo Li89c67852014-02-14 09:34:35 +08002716int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03002717 unsigned int *tx_mask,
2718 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08002719 unsigned int *slots,
2720 unsigned int *slot_width)
2721{
2722 u32 val;
2723 int ret;
2724
Jyri Sarha61310842015-09-09 21:27:43 +03002725 if (tx_mask)
2726 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2727 if (rx_mask)
2728 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2729
Xiubo Li89c67852014-02-14 09:34:35 +08002730 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2731 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2732 if (ret)
2733 return ret;
2734
2735 if (slots)
2736 *slots = val;
2737 }
2738
2739 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2740 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2741 if (ret)
2742 return ret;
2743
2744 if (slot_width)
2745 *slot_width = val;
2746 }
2747
2748 return 0;
2749}
2750EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2751
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002752void snd_soc_of_parse_node_prefix(struct device_node *np,
2753 struct snd_soc_codec_conf *codec_conf,
2754 struct device_node *of_node,
2755 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002756{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002757 const char *str;
2758 int ret;
2759
2760 ret = of_property_read_string(np, propname, &str);
2761 if (ret < 0) {
2762 /* no prefix is not error */
2763 return;
2764 }
2765
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09002766 codec_conf->dlc.of_node = of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002767 codec_conf->name_prefix = str;
2768}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002769EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002770
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002771int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002772 const char *propname)
2773{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002774 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00002775 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002776 struct snd_soc_dapm_route *routes;
2777 int i, ret;
2778
2779 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08002780 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002781 dev_err(card->dev,
2782 "ASoC: Property '%s' does not exist or its length is not even\n",
2783 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002784 return -EINVAL;
2785 }
2786 num_routes /= 2;
2787 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002788 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002789 propname);
2790 return -EINVAL;
2791 }
2792
Kees Cooka86854d2018-06-12 14:07:58 -07002793 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002794 GFP_KERNEL);
2795 if (!routes) {
2796 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002797 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002798 return -EINVAL;
2799 }
2800
2801 for (i = 0; i < num_routes; i++) {
2802 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002803 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002804 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09002805 dev_err(card->dev,
2806 "ASoC: Property '%s' index %d could not be read: %d\n",
2807 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002808 return -EINVAL;
2809 }
2810 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002811 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002812 if (ret) {
2813 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09002814 "ASoC: Property '%s' index %d could not be read: %d\n",
2815 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002816 return -EINVAL;
2817 }
2818 }
2819
Nicolin Chenf23e8602015-02-14 17:22:49 -08002820 card->num_of_dapm_routes = num_routes;
2821 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002822
2823 return 0;
2824}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002825EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002826
Stephan Gerhold1ae09652020-08-01 12:02:55 +02002827int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
2828{
2829 struct device_node *node = card->dev->of_node;
2830 struct snd_soc_aux_dev *aux;
2831 int num, i;
2832
2833 num = of_count_phandle_with_args(node, propname, NULL);
2834 if (num == -ENOENT) {
2835 return 0;
2836 } else if (num < 0) {
2837 dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
2838 propname, num);
2839 return num;
2840 }
2841
2842 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
2843 if (!aux)
2844 return -ENOMEM;
2845 card->aux_dev = aux;
2846 card->num_aux_devs = num;
2847
2848 for_each_card_pre_auxs(card, i, aux) {
2849 aux->dlc.of_node = of_parse_phandle(node, propname, i);
2850 if (!aux->dlc.of_node)
2851 return -EINVAL;
2852 }
2853
2854 return 0;
2855}
2856EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
2857
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002858unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02002859 const char *prefix,
2860 struct device_node **bitclkmaster,
2861 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002862{
2863 int ret, i;
2864 char prop[128];
2865 unsigned int format = 0;
2866 int bit, frame;
2867 const char *str;
2868 struct {
2869 char *name;
2870 unsigned int val;
2871 } of_fmt_table[] = {
2872 { "i2s", SND_SOC_DAIFMT_I2S },
2873 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2874 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2875 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2876 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2877 { "ac97", SND_SOC_DAIFMT_AC97 },
2878 { "pdm", SND_SOC_DAIFMT_PDM},
2879 { "msb", SND_SOC_DAIFMT_MSB },
2880 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002881 };
2882
2883 if (!prefix)
2884 prefix = "";
2885
2886 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002887 * check "dai-format = xxx"
2888 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002889 * SND_SOC_DAIFMT_FORMAT_MASK area
2890 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002891 ret = of_property_read_string(np, "dai-format", &str);
2892 if (ret < 0) {
2893 snprintf(prop, sizeof(prop), "%sformat", prefix);
2894 ret = of_property_read_string(np, prop, &str);
2895 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002896 if (ret == 0) {
2897 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2898 if (strcmp(str, of_fmt_table[i].name) == 0) {
2899 format |= of_fmt_table[i].val;
2900 break;
2901 }
2902 }
2903 }
2904
2905 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002906 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002907 * SND_SOC_DAIFMT_CLOCK_MASK area
2908 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002909 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02002910 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002911 format |= SND_SOC_DAIFMT_CONT;
2912 else
2913 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002914
2915 /*
2916 * check "[prefix]bitclock-inversion"
2917 * check "[prefix]frame-inversion"
2918 * SND_SOC_DAIFMT_INV_MASK area
2919 */
2920 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2921 bit = !!of_get_property(np, prop, NULL);
2922
2923 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2924 frame = !!of_get_property(np, prop, NULL);
2925
2926 switch ((bit << 4) + frame) {
2927 case 0x11:
2928 format |= SND_SOC_DAIFMT_IB_IF;
2929 break;
2930 case 0x10:
2931 format |= SND_SOC_DAIFMT_IB_NF;
2932 break;
2933 case 0x01:
2934 format |= SND_SOC_DAIFMT_NB_IF;
2935 break;
2936 default:
2937 /* SND_SOC_DAIFMT_NB_NF is default */
2938 break;
2939 }
2940
2941 /*
2942 * check "[prefix]bitclock-master"
2943 * check "[prefix]frame-master"
2944 * SND_SOC_DAIFMT_MASTER_MASK area
2945 */
2946 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2947 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002948 if (bit && bitclkmaster)
2949 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002950
2951 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2952 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002953 if (frame && framemaster)
2954 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002955
2956 switch ((bit << 4) + frame) {
2957 case 0x11:
2958 format |= SND_SOC_DAIFMT_CBM_CFM;
2959 break;
2960 case 0x10:
2961 format |= SND_SOC_DAIFMT_CBM_CFS;
2962 break;
2963 case 0x01:
2964 format |= SND_SOC_DAIFMT_CBS_CFM;
2965 break;
2966 default:
2967 format |= SND_SOC_DAIFMT_CBS_CFS;
2968 break;
2969 }
2970
2971 return format;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2974
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002975int snd_soc_get_dai_id(struct device_node *ep)
2976{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09002977 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002978 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002979 int ret;
2980
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002981 dlc.of_node = of_graph_get_port_parent(ep);
2982 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002983 /*
2984 * For example HDMI case, HDMI has video/sound port,
2985 * but ALSA SoC needs sound port number only.
2986 * Thus counting HDMI DT port/endpoint doesn't work.
2987 * Then, it should have .of_xlate_dai_id
2988 */
2989 ret = -ENOTSUPP;
2990 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002991 component = soc_find_component(&dlc);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +09002992 if (component)
2993 ret = snd_soc_component_of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002994 mutex_unlock(&client_mutex);
2995
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002996 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07002997
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002998 return ret;
2999}
3000EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3001
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003002int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003003 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003004{
3005 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003006 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003007 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003008
3009 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003010 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003011 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003012
3013 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003014 continue;
3015
Kuninori Morimotoa2a34172019-07-26 13:51:31 +09003016 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3017 if (ret == -ENOTSUPP) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003018 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003019 int id = -1;
3020
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003021 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003022 case 0:
3023 id = 0; /* same as dai_drv[0] */
3024 break;
3025 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003026 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003027 break;
3028 default:
3029 /* not supported */
3030 break;
3031 }
3032
3033 if (id < 0 || id >= pos->num_dai) {
3034 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003035 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003036 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003037
3038 ret = 0;
3039
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003040 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003041 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003042 if (id == 0)
3043 break;
3044 id--;
3045 }
3046
3047 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003048 if (!*dai_name)
3049 *dai_name = pos->name;
Jerome Brunet1dfa5a52020-02-13 16:51:51 +01003050 } else if (ret) {
3051 /*
3052 * if another error than ENOTSUPP is returned go on and
3053 * check if another component is provided with the same
3054 * node. This may happen if a device provides several
3055 * components
3056 */
3057 continue;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003058 }
3059
Kuninori Morimotocb470082013-09-10 17:39:56 -07003060 break;
3061 }
3062 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003063 return ret;
3064}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003065EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003066
3067int snd_soc_of_get_dai_name(struct device_node *of_node,
3068 const char **dai_name)
3069{
3070 struct of_phandle_args args;
3071 int ret;
3072
3073 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3074 "#sound-dai-cells", 0, &args);
3075 if (ret)
3076 return ret;
3077
3078 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003079
3080 of_node_put(args.np);
3081
3082 return ret;
3083}
3084EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3085
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003086/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003087 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3088 * @dai_link: DAI link
3089 *
3090 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3091 */
3092void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3093{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003094 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003095 int index;
3096
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003097 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003098 if (!component->of_node)
3099 break;
3100 of_node_put(component->of_node);
3101 component->of_node = NULL;
3102 }
3103}
3104EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3105
3106/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003107 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3108 * @dev: Card device
3109 * @of_node: Device node
3110 * @dai_link: DAI link
3111 *
3112 * Builds an array of CODEC DAI components from the DAI link property
3113 * 'sound-dai'.
3114 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003115 * The device nodes in the array (of_node) must be dereferenced by calling
3116 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003117 *
3118 * Returns 0 for success
3119 */
3120int snd_soc_of_get_dai_link_codecs(struct device *dev,
3121 struct device_node *of_node,
3122 struct snd_soc_dai_link *dai_link)
3123{
3124 struct of_phandle_args args;
3125 struct snd_soc_dai_link_component *component;
3126 char *name;
3127 int index, num_codecs, ret;
3128
3129 /* Count the number of CODECs */
3130 name = "sound-dai";
3131 num_codecs = of_count_phandle_with_args(of_node, name,
3132 "#sound-dai-cells");
3133 if (num_codecs <= 0) {
3134 if (num_codecs == -ENOENT)
3135 dev_err(dev, "No 'sound-dai' property\n");
3136 else
3137 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3138 return num_codecs;
3139 }
Kees Cooka86854d2018-06-12 14:07:58 -07003140 component = devm_kcalloc(dev,
3141 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003142 GFP_KERNEL);
3143 if (!component)
3144 return -ENOMEM;
3145 dai_link->codecs = component;
3146 dai_link->num_codecs = num_codecs;
3147
3148 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003149 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003150 ret = of_parse_phandle_with_args(of_node, name,
3151 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003152 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003153 if (ret)
3154 goto err;
3155 component->of_node = args.np;
3156 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3157 if (ret < 0)
3158 goto err;
3159 }
3160 return 0;
3161err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003162 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003163 dai_link->codecs = NULL;
3164 dai_link->num_codecs = 0;
3165 return ret;
3166}
3167EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3168
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003169static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003170{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003171 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003172 snd_soc_util_init();
3173
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003174 return platform_driver_register(&soc_driver);
3175}
Mark Brown4abe8e12010-10-12 17:41:03 +01003176module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003177
Mark Brown7d8c16a2008-11-30 22:11:24 +00003178static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003179{
Mark Brownfb257892011-04-28 10:57:54 +01003180 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003181 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003182
Mark Brown3ff3f642008-05-19 12:32:25 +02003183 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003184}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003185module_exit(snd_soc_exit);
3186
3187/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003188MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003189MODULE_DESCRIPTION("ALSA SoC Core");
3190MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003191MODULE_ALIAS("platform:soc-audio");