blob: 42e8c11a0b26a3ff07ed0aca4cddac5ad62f1523 [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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Mark Brownc5af3a22008-11-28 13:29:45 +000049static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070050static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010051static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000052
Kuninori Morimoto368dee92018-09-21 05:23:01 +000053#define for_each_component(component) \
54 list_for_each_entry(component, &component_list, list)
55
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090057 * This is used if driver don't need to have CPU/Codec/Platform
58 * dai_link. see soc.h
59 */
60struct snd_soc_dai_link_component null_dailink_component[0];
61EXPORT_SYMBOL_GPL(null_dailink_component);
62
63/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020064 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Mark Browndbe21402010-02-12 11:37:24 +000072static ssize_t pmdown_time_show(struct device *dev,
73 struct device_attribute *attr, char *buf)
74{
Mark Brown36ae1a92012-01-06 17:12:45 -080075 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000076
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000077 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000078}
79
80static ssize_t pmdown_time_set(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
83{
Mark Brown36ae1a92012-01-06 17:12:45 -080084 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070085 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000086
Jingoo Hanb785a492013-07-19 16:24:59 +090087 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -070088 if (ret)
89 return ret;
Mark Browndbe21402010-02-12 11:37:24 +000090
91 return count;
92}
93
94static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
95
Takashi Iwaid29697d2015-01-30 20:16:37 +010096static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +010097 &dev_attr_pmdown_time.attr,
98 NULL
99};
100
101static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
102 struct attribute *attr, int idx)
103{
104 struct device *dev = kobj_to_dev(kobj);
105 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
106
Kuninori Morimotod918a372019-09-12 13:42:30 +0900107 if (!rtd)
108 return 0;
109
Takashi Iwaid29697d2015-01-30 20:16:37 +0100110 if (attr == &dev_attr_pmdown_time.attr)
111 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000112 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100113}
114
115static const struct attribute_group soc_dapm_dev_group = {
116 .attrs = soc_dapm_dev_attrs,
117 .is_visible = soc_dev_attr_is_visible,
118};
119
Mark Brownf7e73b262018-03-09 12:46:27 +0000120static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100121 .attrs = soc_dev_attrs,
122 .is_visible = soc_dev_attr_is_visible,
123};
124
125static const struct attribute_group *soc_dev_attr_groups[] = {
126 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000127 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100128 NULL
129};
130
Mark Brown2624d5f2009-11-03 21:56:13 +0000131#ifdef CONFIG_DEBUG_FS
Kuninori Morimotoa4072cd2019-12-11 13:32:20 +0900132struct dentry *snd_soc_debugfs_root;
133EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
134
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200135static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100136{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200137 if (!component->card->debugfs_card_root)
138 return;
139
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200140 if (component->debugfs_prefix) {
141 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100142
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200143 name = kasprintf(GFP_KERNEL, "%s:%s",
144 component->debugfs_prefix, component->name);
145 if (name) {
146 component->debugfs_root = debugfs_create_dir(name,
147 component->card->debugfs_card_root);
148 kfree(name);
149 }
150 } else {
151 component->debugfs_root = debugfs_create_dir(component->name,
152 component->card->debugfs_card_root);
153 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100154
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200155 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
156 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200157}
158
159static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
160{
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900161 if (!component->debugfs_root)
162 return;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200163 debugfs_remove_recursive(component->debugfs_root);
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900164 component->debugfs_root = NULL;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200165}
166
Peng Donglinc15b2a12018-02-14 22:48:07 +0800167static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100168{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100169 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100170 struct snd_soc_dai *dai;
171
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100172 mutex_lock(&client_mutex);
173
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000174 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000175 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800176 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100177
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100178 mutex_unlock(&client_mutex);
179
Donglin Peng700c17c2018-01-18 13:31:26 +0800180 return 0;
181}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800182DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100183
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000184static int component_list_show(struct seq_file *m, void *v)
185{
186 struct snd_soc_component *component;
187
188 mutex_lock(&client_mutex);
189
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000190 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000191 seq_printf(m, "%s\n", component->name);
192
193 mutex_unlock(&client_mutex);
194
195 return 0;
196}
197DEFINE_SHOW_ATTRIBUTE(component_list);
198
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200199static void soc_init_card_debugfs(struct snd_soc_card *card)
200{
201 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000202 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200203
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200204 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
205 &card->pop_time);
Kuninori Morimotod8ca7a02019-08-07 10:31:14 +0900206
207 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208}
209
210static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
211{
212 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900213 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200214}
215
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200216static void snd_soc_debugfs_init(void)
217{
218 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200219
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200220 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
221 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000222
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200223 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
224 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200225}
226
227static void snd_soc_debugfs_exit(void)
228{
229 debugfs_remove_recursive(snd_soc_debugfs_root);
230}
231
Mark Brown2624d5f2009-11-03 21:56:13 +0000232#else
233
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200234static inline void soc_init_component_debugfs(
235 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000236{
237}
238
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200239static inline void soc_cleanup_component_debugfs(
240 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000241{
242}
243
Axel Linb95fccb2010-11-09 17:06:44 +0800244static inline void soc_init_card_debugfs(struct snd_soc_card *card)
245{
246}
247
248static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
249{
250}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200251
252static inline void snd_soc_debugfs_init(void)
253{
254}
255
256static inline void snd_soc_debugfs_exit(void)
257{
258}
259
Mark Brown2624d5f2009-11-03 21:56:13 +0000260#endif
261
Kuninori Morimoto12b05232020-01-10 11:35:54 +0900262static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
263 struct snd_soc_component *component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000264{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900265 struct snd_soc_component *comp;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900266 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000267
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900268 for_each_rtd_components(rtd, i, comp) {
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000269 /* already connected */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900270 if (comp == component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000271 return 0;
272 }
273
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900274 /* see for_each_rtd_components */
275 rtd->components[rtd->num_components] = component;
276 rtd->num_components++;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000277
278 return 0;
279}
280
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000281struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
282 const char *driver_name)
283{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900284 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900285 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000286
Kuninori Morimoto971da242018-01-23 00:41:24 +0000287 if (!driver_name)
288 return NULL;
289
Kuninori Morimotoa33c0d12019-08-20 14:05:02 +0900290 /*
291 * NOTE
292 *
293 * snd_soc_rtdcom_lookup() will find component from rtd by using
294 * specified driver name.
295 * But, if many components which have same driver name are connected
296 * to 1 rtd, this function will return 1st found component.
297 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900298 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900299 const char *component_name = component->driver->name;
Kuninori Morimoto971da242018-01-23 00:41:24 +0000300
301 if (!component_name)
302 continue;
303
304 if ((component_name == driver_name) ||
305 strcmp(component_name, driver_name) == 0)
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900306 return component;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000307 }
308
309 return NULL;
310}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000311EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000312
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900313static struct snd_soc_component
314*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
Kuninori Morimotob8132652019-11-05 15:46:30 +0900315{
316 struct snd_soc_component *component;
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900317 struct snd_soc_component *found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900318
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900319 found_component = NULL;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900320 for_each_component(component) {
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900321 if ((dev == component->dev) &&
322 (!driver_name ||
323 (driver_name == component->driver->name) ||
324 (strcmp(component->driver->name, driver_name) == 0))) {
325 found_component = component;
326 break;
327 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900328 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900329
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900330 return found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900331}
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900332
333struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
334 const char *driver_name)
335{
336 struct snd_soc_component *component;
337
338 mutex_lock(&client_mutex);
339 component = snd_soc_lookup_component_nolocked(dev, driver_name);
340 mutex_unlock(&client_mutex);
341
342 return component;
343}
Kuninori Morimotob8132652019-11-05 15:46:30 +0900344EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
345
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900346struct snd_soc_pcm_runtime
347*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
Kuninori Morimoto44681892019-12-10 09:34:08 +0900348 struct snd_soc_dai_link *dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900349{
350 struct snd_soc_pcm_runtime *rtd;
351
352 for_each_card_rtds(card, rtd) {
Kuninori Morimoto44681892019-12-10 09:34:08 +0900353 if (rtd->dai_link == dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900354 return rtd;
355 }
Kuninori Morimoto44681892019-12-10 09:34:08 +0900356 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900357 return NULL;
358}
359EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
360
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900361/*
362 * Power down the audio subsystem pmdown_time msecs after close is called.
363 * This is to ensure there are no pops or clicks in between any music tracks
364 * due to DAPM power cycling.
365 */
366void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
367{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900368 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900369 int playback = SNDRV_PCM_STREAM_PLAYBACK;
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900370
371 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
372
373 dev_dbg(rtd->dev,
374 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
375 codec_dai->driver->playback.stream_name,
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900376 snd_soc_dai_stream_active(codec_dai, playback) ?
377 "active" : "inactive",
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900378 rtd->pop_wait ? "yes" : "no");
379
380 /* are we waiting on this codec DAI stream */
381 if (rtd->pop_wait == 1) {
382 rtd->pop_wait = 0;
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900383 snd_soc_dapm_stream_event(rtd, playback,
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900384 SND_SOC_DAPM_STREAM_STOP);
385 }
386
387 mutex_unlock(&rtd->card->pcm_mutex);
388}
389EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
390
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900391static void soc_release_rtd_dev(struct device *dev)
392{
393 /* "dev" means "rtd->dev" */
394 kfree(dev);
395}
396
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900397static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
398{
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900399 if (!rtd)
400 return;
401
Kuninori Morimoto753ace02019-09-12 13:38:50 +0900402 list_del(&rtd->list);
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900403
Curtis Malainey9c9b6522019-11-27 17:13:58 -0800404 if (delayed_work_pending(&rtd->delayed_work))
405 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +0900406 snd_soc_pcm_component_free(rtd);
407
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900408 /*
409 * we don't need to call kfree() for rtd->dev
410 * see
411 * soc_release_rtd_dev()
Kuninori Morimotod918a372019-09-12 13:42:30 +0900412 *
413 * We don't need rtd->dev NULL check, because
414 * it is alloced *before* rtd.
415 * see
416 * soc_new_pcm_runtime()
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900417 */
Kuninori Morimotod918a372019-09-12 13:42:30 +0900418 device_unregister(rtd->dev);
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900419}
420
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800421static void close_delayed_work(struct work_struct *work) {
422 struct snd_soc_pcm_runtime *rtd =
423 container_of(work, struct snd_soc_pcm_runtime,
424 delayed_work.work);
425
426 if (rtd->close_delayed_work_func)
427 rtd->close_delayed_work_func(rtd);
428}
429
Mengdong Lin1a497982015-11-18 02:34:11 -0500430static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
431 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
432{
433 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900434 struct snd_soc_component *component;
Kuninori Morimotod918a372019-09-12 13:42:30 +0900435 struct device *dev;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900436 int ret;
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900437 int stream;
Mengdong Lin1a497982015-11-18 02:34:11 -0500438
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900439 /*
Kuninori Morimotod918a372019-09-12 13:42:30 +0900440 * for rtd->dev
441 */
442 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
443 if (!dev)
444 return NULL;
445
446 dev->parent = card->dev;
447 dev->release = soc_release_rtd_dev;
448 dev->groups = soc_dev_attr_groups;
449
450 dev_set_name(dev, "%s", dai_link->name);
451
452 ret = device_register(dev);
453 if (ret < 0) {
454 put_device(dev); /* soc_release_rtd_dev */
455 return NULL;
456 }
457
458 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900459 * for rtd
460 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900461 rtd = devm_kzalloc(dev,
462 sizeof(*rtd) +
463 sizeof(*component) * (dai_link->num_cpus +
464 dai_link->num_codecs +
465 dai_link->num_platforms),
466 GFP_KERNEL);
Mengdong Lin1a497982015-11-18 02:34:11 -0500467 if (!rtd)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900468 goto free_rtd;
Mengdong Lin1a497982015-11-18 02:34:11 -0500469
Kuninori Morimotod918a372019-09-12 13:42:30 +0900470 rtd->dev = dev;
Takashi Iwai07d22a92019-12-04 16:14:54 +0100471 INIT_LIST_HEAD(&rtd->list);
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900472 for_each_pcm_streams(stream) {
473 INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
474 INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
475 }
Kuninori Morimotod918a372019-09-12 13:42:30 +0900476 dev_set_drvdata(dev, rtd);
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800477 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
Kuninori Morimotod918a372019-09-12 13:42:30 +0900478
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900479 /*
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900480 * for rtd->dais
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900481 */
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900482 rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
Kees Cook6396bb22018-06-12 14:03:40 -0700483 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500484 GFP_KERNEL);
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900485 if (!rtd->dais)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900486 goto free_rtd;
487
488 /*
Kuninori Morimoto22a2fc82020-03-16 15:36:58 +0900489 * dais = [][][][][][][][][][][][][][][][][][]
490 * ^cpu_dais ^codec_dais
491 * |--- num_cpus ---|--- num_codecs --|
Kuninori Morimoto17290252020-03-30 10:48:27 +0900492 * see
493 * asoc_rtd_to_cpu()
494 * asoc_rtd_to_codec()
Shreyas NC76afa642020-02-25 21:39:12 +0800495 */
Kuninori Morimoto49648d02020-03-30 10:47:54 +0900496 rtd->num_cpus = dai_link->num_cpus;
497 rtd->num_codecs = dai_link->num_codecs;
Kuninori Morimoto01faf502020-03-30 10:48:14 +0900498 rtd->card = card;
499 rtd->dai_link = dai_link;
500 rtd->num = card->num_rtd++;
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900501
Kuninori Morimoto6634e3d2019-08-07 10:30:31 +0900502 /* see for_each_card_rtds */
Mengdong Lin1a497982015-11-18 02:34:11 -0500503 list_add_tail(&rtd->list, &card->rtd_list);
Kuninori Morimotoa8481252019-09-12 13:38:34 +0900504
505 return rtd;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900506
507free_rtd:
508 soc_free_pcm_runtime(rtd);
509 return NULL;
Mengdong Lin1a497982015-11-18 02:34:11 -0500510}
511
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900512static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
513{
514 struct snd_soc_pcm_runtime *rtd;
515
516 for_each_card_rtds(card, rtd)
517 flush_delayed_work(&rtd->delayed_work);
518}
519
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000520#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000522int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000524 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000525 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500526 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900527 int playback = SNDRV_PCM_STREAM_PLAYBACK;
Mengdong Lin1a497982015-11-18 02:34:11 -0500528 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200530 /* If the card is not initialized yet there is nothing to do */
531 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200532 return 0;
533
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200534 /*
535 * Due to the resume being scheduled into a workqueue we could
536 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100537 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100539
540 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100542
Mark Browna00f90f2010-12-02 16:24:24 +0000543 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000544 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000545 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100546
Mengdong Lin1a497982015-11-18 02:34:11 -0500547 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100548 continue;
549
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900550 for_each_rtd_codec_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900551 if (snd_soc_dai_stream_active(dai, playback))
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900552 snd_soc_dai_digital_mute(dai, 1, playback);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 }
555
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100556 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000557 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500558 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100559 continue;
560
Mengdong Lin1a497982015-11-18 02:34:11 -0500561 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100562 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100563
Kuninori Morimoto130dc082020-05-28 10:48:39 +0900564 snd_soc_card_suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200566 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900567 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100568
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000569 for_each_card_rtds(card, rtd) {
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900570 int stream;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571
Mengdong Lin1a497982015-11-18 02:34:11 -0500572 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100573 continue;
574
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900575 for_each_pcm_streams(stream)
576 snd_soc_dapm_stream_event(rtd, stream,
577 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 }
579
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200580 /* Recheck all endpoints too, their state is affected by suspend */
581 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700582 snd_soc_dapm_sync(&card->dapm);
583
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000584 /* suspend all COMPONENTs */
Kuninori Morimoto12720632020-01-10 11:36:02 +0900585 for_each_card_rtds(card, rtd) {
586
587 if (rtd->dai_link->ignore_suspend)
588 continue;
589
590 for_each_rtd_components(rtd, i, component) {
591 struct snd_soc_dapm_context *dapm =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200592 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000593
Kuninori Morimoto12720632020-01-10 11:36:02 +0900594 /*
595 * ignore if component was already suspended
596 */
597 if (snd_soc_component_is_suspended(component))
598 continue;
599
600 /*
601 * If there are paths active then the COMPONENT will be
602 * held with bias _ON and should not be suspended.
603 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200604 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000606 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000607 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000608 * bias off then being in STANDBY
609 * means it's doing something,
610 * otherwise fall through.
611 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200612 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000613 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200614 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000615 break;
616 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500617 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 case SND_SOC_BIAS_OFF:
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900620 snd_soc_component_suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000621 if (component->regmap)
622 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800623 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000624 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 break;
626 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000627 dev_dbg(component->dev,
628 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 break;
630 }
631 }
632 }
633
Kuninori Morimotod17b60b2020-05-28 10:48:48 +0900634 snd_soc_card_suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635
636 return 0;
637}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000638EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200640/*
641 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100642 * setting our codec back up, which can be very slow on I2C
643 */
644static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200647 container_of(work, struct snd_soc_card,
648 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500649 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000650 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500651 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200653 /*
654 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100655 * so userspace apps are blocked from touching us
656 */
657
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000658 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100659
Mark Brown9949788b2010-05-07 20:24:05 +0100660 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100662
Kuninori Morimoto934c7522020-05-28 10:48:55 +0900663 snd_soc_card_resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000665 for_each_card_components(card, component) {
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900666 if (snd_soc_component_is_suspended(component))
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900667 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100668 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000670 for_each_card_rtds(card, rtd) {
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900671 int stream;
Mark Brown3efab7d2010-05-09 13:25:43 +0100672
Mengdong Lin1a497982015-11-18 02:34:11 -0500673 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100674 continue;
675
Kuninori Morimotod74c2a12020-02-17 17:28:25 +0900676 for_each_pcm_streams(stream)
677 snd_soc_dapm_stream_event(rtd, stream,
678 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 }
680
Mark Brown3ff3f642008-05-19 12:32:25 +0200681 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000682 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000683 struct snd_soc_dai *dai;
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900684 int playback = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown3efab7d2010-05-09 13:25:43 +0100685
Mengdong Lin1a497982015-11-18 02:34:11 -0500686 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100687 continue;
688
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900689 for_each_rtd_codec_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900690 if (snd_soc_dai_stream_active(dai, playback))
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900691 snd_soc_dai_digital_mute(dai, 0, playback);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200692 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 }
694
Kuninori Morimoto739443d2020-05-28 10:49:02 +0900695 snd_soc_card_resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000697 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100698
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200699 /* Recheck all endpoints too, their state is affected by suspend */
700 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700701 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530702
703 /* userspace can access us now we are back as we were before */
704 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100705}
706
707/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000708int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100709{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000710 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900711 struct snd_soc_component *component;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200712
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200713 /* If the card is not initialized yet there is nothing to do */
714 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800715 return 0;
716
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800717 /* activate pins from sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900718 for_each_card_components(card, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900719 if (snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900720 pinctrl_pm_select_default_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800721
Kuninori Morimoto250a15c2020-01-20 10:05:07 +0900722 dev_dbg(dev, "ASoC: Scheduling resume work\n");
723 if (!schedule_work(&card->deferred_resume_work))
724 dev_err(dev, "ASoC: resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100725
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 return 0;
727}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000728EXPORT_SYMBOL_GPL(snd_soc_resume);
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900729
730static void soc_resume_init(struct snd_soc_card *card)
731{
732 /* deferred resume work */
733 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
734}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000736#define snd_soc_suspend NULL
737#define snd_soc_resume NULL
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900738static inline void soc_resume_init(struct snd_soc_card *card)
739{
740}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741#endif
742
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900743static struct device_node
744*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100745{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900746 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100747
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900748 of_node = component->dev->of_node;
749 if (!of_node && component->dev->parent)
750 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100751
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900752 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100753}
754
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000755static int snd_soc_is_matching_component(
756 const struct snd_soc_dai_link_component *dlc,
757 struct snd_soc_component *component)
758{
759 struct device_node *component_of_node;
760
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900761 if (!dlc)
762 return 0;
763
764 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000765
766 if (dlc->of_node && component_of_node != dlc->of_node)
767 return 0;
768 if (dlc->name && strcmp(component->name, dlc->name))
769 return 0;
770
771 return 1;
772}
773
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900775 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776{
777 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200778
779 lockdep_assert_held(&client_mutex);
780
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900781 /*
782 * NOTE
783 *
784 * It returns *1st* found component, but some driver
785 * has few components by same of_node/name
786 * ex)
787 * CPU component and generic DMAEngine component
788 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900789 for_each_component(component)
790 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return component;
Barry Song02a06d32009-10-16 18:13:38 +0800792
793 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100794}
795
Mengdong Linfbb88b52016-04-22 12:25:33 +0800796/**
797 * snd_soc_find_dai - Find a registered DAI
798 *
Jeffy Chen49584712017-08-22 15:57:21 +0800799 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800800 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700801 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800802 * find the DAI of the same name. The component's of_node and name
803 * should also match if being specified.
804 *
805 * Return: pointer of DAI, or NULL if not found.
806 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800807struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200808 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100809{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200810 struct snd_soc_component *component;
811 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100812
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100813 lockdep_assert_held(&client_mutex);
814
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200815 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000816 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000817 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200818 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000819 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800820 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800821 && (!dai->driver->name
822 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100823 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100824
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200825 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100826 }
827 }
828
829 return NULL;
830}
Mengdong Lin305e9022016-04-19 13:12:25 +0800831EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100832
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900833static int soc_dai_link_sanity_check(struct snd_soc_card *card,
834 struct snd_soc_dai_link *link)
Kuninori Morimoto36794902019-11-05 15:45:41 +0900835{
836 int i;
Shreyas NC76afa642020-02-25 21:39:12 +0800837 struct snd_soc_dai_link_component *cpu, *codec, *platform;
Kuninori Morimoto36794902019-11-05 15:45:41 +0900838
839 for_each_link_codecs(link, i, codec) {
840 /*
841 * Codec must be specified by 1 of name or OF node,
842 * not both or neither.
843 */
844 if (!!codec->name == !!codec->of_node) {
845 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
846 link->name);
847 return -EINVAL;
848 }
849
850 /* Codec DAI name must be specified */
851 if (!codec->dai_name) {
852 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
853 link->name);
854 return -EINVAL;
855 }
856
857 /*
858 * Defer card registration if codec component is not added to
859 * component list.
860 */
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500861 if (!soc_find_component(codec)) {
862 dev_dbg(card->dev,
863 "ASoC: codec component %s not found for link %s\n",
864 codec->name, link->name);
Kuninori Morimoto36794902019-11-05 15:45:41 +0900865 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500866 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900867 }
868
869 for_each_link_platforms(link, i, platform) {
870 /*
871 * Platform may be specified by either name or OF node, but it
872 * can be left unspecified, then no components will be inserted
873 * in the rtdcom list
874 */
875 if (!!platform->name == !!platform->of_node) {
876 dev_err(card->dev,
877 "ASoC: Neither/both platform name/of_node are set for %s\n",
878 link->name);
879 return -EINVAL;
880 }
881
882 /*
883 * Defer card registration if platform component is not added to
884 * component list.
885 */
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500886 if (!soc_find_component(platform)) {
887 dev_dbg(card->dev,
888 "ASoC: platform component %s not found for link %s\n",
889 platform->name, link->name);
Kuninori Morimoto36794902019-11-05 15:45:41 +0900890 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500891 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900892 }
893
Shreyas NC76afa642020-02-25 21:39:12 +0800894 for_each_link_cpus(link, i, cpu) {
895 /*
896 * CPU device may be specified by either name or OF node, but
897 * can be left unspecified, and will be matched based on DAI
898 * name alone..
899 */
900 if (cpu->name && cpu->of_node) {
901 dev_err(card->dev,
902 "ASoC: Neither/both cpu name/of_node are set for %s\n",
903 link->name);
904 return -EINVAL;
905 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900906
Shreyas NC76afa642020-02-25 21:39:12 +0800907 /*
908 * Defer card registration if cpu dai component is not added to
909 * component list.
910 */
911 if ((cpu->of_node || cpu->name) &&
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500912 !soc_find_component(cpu)) {
913 dev_dbg(card->dev,
914 "ASoC: cpu component %s not found for link %s\n",
915 cpu->name, link->name);
Shreyas NC76afa642020-02-25 21:39:12 +0800916 return -EPROBE_DEFER;
Ranjani Sridharanb2acc242020-04-09 13:44:16 -0500917 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900918
Shreyas NC76afa642020-02-25 21:39:12 +0800919 /*
920 * At least one of CPU DAI name or CPU device name/node must be
921 * specified
922 */
923 if (!cpu->dai_name &&
924 !(cpu->name || cpu->of_node)) {
925 dev_err(card->dev,
926 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
927 link->name);
928 return -EINVAL;
929 }
Kuninori Morimoto36794902019-11-05 15:45:41 +0900930 }
931
932 return 0;
933}
934
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900935/**
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900936 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
937 * @card: The ASoC card to which the pcm_runtime has
938 * @rtd: The pcm_runtime to remove
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900939 *
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900940 * This function removes a pcm_runtime from the ASoC card.
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900941 */
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900942void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
943 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900944{
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900945 lockdep_assert_held(&client_mutex);
946
947 /*
948 * Notify the machine driver for extra destruction
949 */
950 if (card->remove_dai_link)
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900951 card->remove_dai_link(card, rtd->dai_link);
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900952
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900953 soc_free_pcm_runtime(rtd);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900954}
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900955EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900956
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900957/**
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900958 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
959 * @card: The ASoC card to which the pcm_runtime is added
960 * @dai_link: The DAI link to find pcm_runtime
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900961 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900962 * This function adds a pcm_runtime ASoC card by using dai_link.
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900963 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900964 * Note: Topology can use this API to add pcm_runtime when probing the
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900965 * topology component. And machine drivers can still define static
966 * DAI links in dai_link array.
967 */
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900968int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
969 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200970{
Mengdong Lin1a497982015-11-18 02:34:11 -0500971 struct snd_soc_pcm_runtime *rtd;
Shreyas NC76afa642020-02-25 21:39:12 +0800972 struct snd_soc_dai_link_component *codec, *platform, *cpu;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000973 struct snd_soc_component *component;
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900974 int i, ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200975
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900976 lockdep_assert_held(&client_mutex);
977
978 /*
979 * Notify the machine driver for extra initialization
980 */
981 if (card->add_dai_link)
982 card->add_dai_link(card, dai_link);
983
Liam Girdwooda655de82018-07-02 16:59:54 +0100984 if (dai_link->ignore)
985 return 0;
986
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500987 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000988
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900989 ret = soc_dai_link_sanity_check(card, dai_link);
990 if (ret < 0)
991 return ret;
992
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530993 rtd = soc_new_pcm_runtime(card, dai_link);
994 if (!rtd)
995 return -ENOMEM;
996
Shreyas NC76afa642020-02-25 21:39:12 +0800997 for_each_link_cpus(dai_link, i, cpu) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900998 asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
999 if (!asoc_rtd_to_cpu(rtd, i)) {
Shreyas NC76afa642020-02-25 21:39:12 +08001000 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1001 cpu->dai_name);
1002 goto _err_defer;
1003 }
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001004 snd_soc_rtd_add_component(rtd, asoc_rtd_to_cpu(rtd, i)->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001005 }
Shreyas NC76afa642020-02-25 21:39:12 +08001006
Benoit Cousson88bd8702014-07-08 23:19:34 +02001007 /* Find CODEC from registered CODECs */
Jerome Brunet34614732019-06-27 14:13:50 +02001008 for_each_link_codecs(dai_link, i, codec) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001009 asoc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1010 if (!asoc_rtd_to_codec(rtd, i)) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +01001011 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +02001012 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001013 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001014 }
Jerome Brunet34614732019-06-27 14:13:50 +02001015
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001016 snd_soc_rtd_add_component(rtd, asoc_rtd_to_codec(rtd, i)->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001017 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001018
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +09001019 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +02001020 for_each_link_platforms(dai_link, i, platform) {
1021 for_each_component(component) {
1022 if (!snd_soc_is_matching_component(platform, component))
1023 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001024
Kuninori Morimoto12b05232020-01-10 11:35:54 +09001025 snd_soc_rtd_add_component(rtd, component);
Jerome Brunet34614732019-06-27 14:13:50 +02001026 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001027 }
1028
Mark Brownb19e6e72012-03-14 21:18:39 +00001029 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001030
1031_err_defer:
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +09001032 snd_soc_remove_pcm_runtime(card, rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001033 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034}
Kuninori Morimoto0c048002019-12-10 09:34:19 +09001035EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001036
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001037static int soc_init_pcm_runtime(struct snd_soc_card *card,
1038 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001039{
1040 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001041 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001042 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001043 int ret, num, i;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001044
1045 /* set default power off timeout */
1046 rtd->pmdown_time = pmdown_time;
1047
1048 /* do machine specific initialization */
Kuninori Morimoto02e75632020-05-25 09:57:14 +09001049 ret = snd_soc_link_init(rtd);
1050 if (ret < 0)
1051 return ret;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001052
1053 if (dai_link->dai_fmt) {
1054 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1055 if (ret)
1056 return ret;
1057 }
1058
1059 /* add DPCM sysfs entries */
1060 soc_dpcm_debugfs_add(rtd);
1061
1062 num = rtd->num;
1063
1064 /*
1065 * most drivers will register their PCMs using DAI link ordering but
1066 * topology based drivers can use the DAI link id field to set PCM
1067 * device number and then use rtd + a base offset of the BEs.
1068 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001069 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001070 if (!component->driver->use_dai_pcm_id)
1071 continue;
1072
1073 if (rtd->dai_link->no_pcm)
1074 num += component->driver->be_pcm_base;
1075 else
1076 num = rtd->dai_link->id;
1077 }
1078
1079 /* create compress_device if possible */
1080 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1081 if (ret != -ENOTSUPP) {
1082 if (ret < 0)
1083 dev_err(card->dev, "ASoC: can't create compress %s\n",
1084 dai_link->stream_name);
1085 return ret;
1086 }
1087
1088 /* create the pcm */
1089 ret = soc_new_pcm(rtd, num);
1090 if (ret < 0) {
1091 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1092 dai_link->stream_name, ret);
1093 return ret;
1094 }
Kuninori Morimotod1eb6d12020-03-16 15:37:20 +09001095
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +09001096 return snd_soc_pcm_dai_new(rtd);
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001097}
1098
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001099static void soc_set_name_prefix(struct snd_soc_card *card,
1100 struct snd_soc_component *component)
1101{
Kuninori Morimotoaec3ff92019-12-10 09:34:52 +09001102 struct device_node *of_node = soc_component_to_node(component);
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001103 const char *str;
1104 int ret, i;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001105
Kuninori Morimoto4702f992019-12-10 09:34:48 +09001106 for (i = 0; i < card->num_configs; i++) {
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001107 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001108
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09001109 if (snd_soc_is_matching_component(&map->dlc, component)) {
1110 component->name_prefix = map->name_prefix;
1111 return;
1112 }
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001113 }
1114
1115 /*
1116 * If there is no configuration table or no match in the table,
1117 * check if a prefix is provided in the node
1118 */
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001119 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1120 if (ret < 0)
1121 return;
1122
1123 component->name_prefix = str;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001124}
1125
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001126static void soc_remove_component(struct snd_soc_component *component,
1127 int probed)
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001128{
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001129
1130 if (!component->card)
1131 return;
1132
1133 if (probed)
1134 snd_soc_component_remove(component);
1135
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001136 /* For framework level robustness */
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +02001137 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001138
Bard liao7a5d9812019-09-18 21:31:31 +08001139 list_del_init(&component->card_list);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001140 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1141 soc_cleanup_component_debugfs(component);
1142 component->card = NULL;
Pierre-Louis Bossart0e36f362019-08-07 21:51:31 -05001143 snd_soc_component_module_put_when_remove(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001144}
1145
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001146static int soc_probe_component(struct snd_soc_card *card,
1147 struct snd_soc_component *component)
1148{
1149 struct snd_soc_dapm_context *dapm =
1150 snd_soc_component_get_dapm(component);
1151 struct snd_soc_dai *dai;
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001152 int probed = 0;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001153 int ret;
1154
1155 if (!strcmp(component->name, "snd-soc-dummy"))
1156 return 0;
1157
1158 if (component->card) {
1159 if (component->card != card) {
1160 dev_err(component->dev,
1161 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1162 card->name, component->card->name);
1163 return -ENODEV;
1164 }
1165 return 0;
1166 }
1167
1168 ret = snd_soc_component_module_get_when_probe(component);
1169 if (ret < 0)
1170 return ret;
1171
1172 component->card = card;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001173 soc_set_name_prefix(card, component);
1174
1175 soc_init_component_debugfs(component);
1176
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001177 snd_soc_dapm_init(dapm, card, component);
Kuninori Morimotob614bea2019-08-23 09:58:47 +09001178
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001179 ret = snd_soc_dapm_new_controls(dapm,
1180 component->driver->dapm_widgets,
1181 component->driver->num_dapm_widgets);
1182
1183 if (ret != 0) {
1184 dev_err(component->dev,
1185 "Failed to create new controls %d\n", ret);
1186 goto err_probe;
1187 }
1188
1189 for_each_component_dais(component, dai) {
1190 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1191 if (ret != 0) {
1192 dev_err(component->dev,
1193 "Failed to create DAI widgets %d\n", ret);
1194 goto err_probe;
1195 }
1196 }
1197
1198 ret = snd_soc_component_probe(component);
1199 if (ret < 0) {
1200 dev_err(component->dev,
1201 "ASoC: failed to probe component %d\n", ret);
1202 goto err_probe;
1203 }
1204 WARN(dapm->idle_bias_off &&
1205 dapm->bias_level != SND_SOC_BIAS_OFF,
1206 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1207 component->name);
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001208 probed = 1;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001209
1210 /* machine specific init */
1211 if (component->init) {
1212 ret = component->init(component);
1213 if (ret < 0) {
1214 dev_err(component->dev,
1215 "Failed to do machine specific init %d\n", ret);
1216 goto err_probe;
1217 }
1218 }
1219
1220 ret = snd_soc_add_component_controls(component,
1221 component->driver->controls,
1222 component->driver->num_controls);
1223 if (ret < 0)
1224 goto err_probe;
1225
1226 ret = snd_soc_dapm_add_routes(dapm,
1227 component->driver->dapm_routes,
1228 component->driver->num_dapm_routes);
Pierre-Louis Bossarta22ae722020-03-09 14:27:43 -05001229 if (ret < 0) {
1230 if (card->disable_route_checks) {
1231 dev_info(card->dev,
1232 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1233 __func__);
1234 } else {
1235 dev_err(card->dev,
1236 "%s: snd_soc_dapm_add_routes failed: %d\n",
1237 __func__, ret);
1238 goto err_probe;
1239 }
1240 }
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001241
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001242 /* see for_each_card_components */
1243 list_add(&component->card_list, &card->component_dev_list);
1244
1245err_probe:
1246 if (ret < 0)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001247 soc_remove_component(component, probed);
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001248
1249 return ret;
1250}
1251
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001252static void soc_remove_link_dais(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001253{
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001254 struct snd_soc_pcm_runtime *rtd;
1255 int order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001257 for_each_comp_order(order) {
1258 for_each_card_rtds(card, rtd) {
Kuninori Morimoto7eaa3132020-04-24 08:15:20 +09001259 /* remove all rtd connected DAIs in good order */
1260 snd_soc_pcm_dai_remove(rtd, order);
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001261 }
1262 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263}
1264
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001265static int soc_probe_link_dais(struct snd_soc_card *card)
1266{
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001267 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto51801ae2020-04-24 08:15:15 +09001268 int order, ret;
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001269
1270 for_each_comp_order(order) {
1271 for_each_card_rtds(card, rtd) {
1272
1273 dev_dbg(card->dev,
1274 "ASoC: probe %s dai link %d late %d\n",
1275 card->name, rtd->num, order);
1276
Kuninori Morimoto51801ae2020-04-24 08:15:15 +09001277 /* probe all rtd connected DAIs in good order */
1278 ret = snd_soc_pcm_dai_probe(rtd, order);
1279 if (ret)
1280 return ret;
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001281 }
1282 }
1283
1284 return 0;
1285}
1286
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001287static void soc_remove_link_components(struct snd_soc_card *card)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001288{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001289 struct snd_soc_component *component;
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001290 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001291 int i, order;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001292
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001293 for_each_comp_order(order) {
1294 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001295 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001296 if (component->driver->remove_order != order)
1297 continue;
1298
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001299 soc_remove_component(component, 1);
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001300 }
1301 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001302 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001303}
1304
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001305static int soc_probe_link_components(struct snd_soc_card *card)
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001306{
1307 struct snd_soc_component *component;
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001308 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001309 int i, ret, order;
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001310
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001311 for_each_comp_order(order) {
1312 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001313 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001314 if (component->driver->probe_order != order)
1315 continue;
1316
1317 ret = soc_probe_component(card, component);
1318 if (ret < 0)
1319 return ret;
1320 }
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001321 }
1322 }
1323
1324 return 0;
1325}
1326
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001327static void soc_unbind_aux_dev(struct snd_soc_card *card)
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001328{
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001329 struct snd_soc_component *component, *_component;
1330
1331 for_each_card_auxs_safe(card, component, _component) {
1332 component->init = NULL;
1333 list_del(&component->card_aux_list);
1334 }
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001335}
1336
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001337static int soc_bind_aux_dev(struct snd_soc_card *card)
Mark Brownb19e6e72012-03-14 21:18:39 +00001338{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001339 struct snd_soc_component *component;
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001340 struct snd_soc_aux_dev *aux;
1341 int i;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001342
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001343 for_each_card_pre_auxs(card, i, aux) {
1344 /* codecs, usually analog devices */
1345 component = soc_find_component(&aux->dlc);
1346 if (!component)
1347 return -EPROBE_DEFER;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001348
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001349 component->init = aux->init;
1350 /* see for_each_card_auxs */
1351 list_add(&component->card_aux_list, &card->aux_comp_list);
1352 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001353 return 0;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001354}
1355
1356static int soc_probe_aux_devices(struct snd_soc_card *card)
1357{
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001358 struct snd_soc_component *component;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001359 int order;
1360 int ret;
1361
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001362 for_each_comp_order(order) {
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001363 for_each_card_auxs(card, component) {
1364 if (component->driver->probe_order != order)
1365 continue;
1366
1367 ret = soc_probe_component(card, component);
1368 if (ret < 0)
1369 return ret;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001370 }
1371 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001372
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001373 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001374}
1375
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001376static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001377{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001378 struct snd_soc_component *comp, *_comp;
1379 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001380
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001381 for_each_comp_order(order) {
Kuninori Morimotoc2b71c72019-08-08 14:54:44 +09001382 for_each_card_auxs_safe(card, comp, _comp) {
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001383 if (comp->driver->remove_order == order)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001384 soc_remove_component(comp, 1);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001385 }
1386 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001387}
1388
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001389/**
1390 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1391 * @rtd: The runtime for which the DAI link format should be changed
1392 * @dai_fmt: The new DAI link format
1393 *
1394 * This function updates the DAI link format for all DAIs connected to the DAI
1395 * link for the specified runtime.
1396 *
1397 * Note: For setups with a static format set the dai_fmt field in the
1398 * corresponding snd_dai_link struct instead of using this function.
1399 *
1400 * Returns 0 on success, otherwise a negative error code.
1401 */
1402int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1403 unsigned int dai_fmt)
1404{
Shreyas NC76afa642020-02-25 21:39:12 +08001405 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001406 struct snd_soc_dai *codec_dai;
Shreyas NC76afa642020-02-25 21:39:12 +08001407 unsigned int inv_dai_fmt;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001408 unsigned int i;
1409 int ret;
1410
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001411 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001412 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1413 if (ret != 0 && ret != -ENOTSUPP) {
1414 dev_warn(codec_dai->dev,
1415 "ASoC: Failed to set DAI format: %d\n", ret);
1416 return ret;
1417 }
1418 }
1419
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001420 /*
1421 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1422 * the component which has non_legacy_dai_naming is Codec
1423 */
Shreyas NC76afa642020-02-25 21:39:12 +08001424 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1425 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1426 case SND_SOC_DAIFMT_CBM_CFM:
1427 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1428 break;
1429 case SND_SOC_DAIFMT_CBM_CFS:
1430 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1431 break;
1432 case SND_SOC_DAIFMT_CBS_CFM:
1433 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1434 break;
1435 case SND_SOC_DAIFMT_CBS_CFS:
1436 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1437 break;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001438 }
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001439 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC76afa642020-02-25 21:39:12 +08001440 unsigned int fmt = dai_fmt;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001441
Shreyas NC76afa642020-02-25 21:39:12 +08001442 if (cpu_dai->component->driver->non_legacy_dai_naming)
1443 fmt = inv_dai_fmt;
1444
1445 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
1446 if (ret != 0 && ret != -ENOTSUPP) {
1447 dev_warn(cpu_dai->dev,
1448 "ASoC: Failed to set DAI format: %d\n", ret);
1449 return ret;
1450 }
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001451 }
1452
1453 return 0;
1454}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001455EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001456
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001457#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001458/*
Kuninori Morimoto8a6a6a32019-12-11 13:32:05 +09001459 * If a DMI filed contain strings in this blacklist (e.g.
1460 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1461 * as invalid and dropped when setting the card long name from DMI info.
1462 */
1463static const char * const dmi_blacklist[] = {
1464 "To be filled by OEM",
1465 "TBD by OEM",
1466 "Default String",
1467 "Board Manufacturer",
1468 "Board Vendor Name",
1469 "Board Product Name",
1470 NULL, /* terminator */
1471};
1472
1473/*
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001474 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001475 * separate different DMI fields in the card long name. Only number and
1476 * alphabet characters and a few separator characters are kept.
1477 */
1478static void cleanup_dmi_name(char *name)
1479{
1480 int i, j = 0;
1481
1482 for (i = 0; name[i]; i++) {
1483 if (isalnum(name[i]) || (name[i] == '.')
1484 || (name[i] == '_'))
1485 name[j++] = name[i];
1486 else if (name[i] == '-')
1487 name[j++] = '_';
1488 }
1489
1490 name[j] = '\0';
1491}
1492
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001493/*
1494 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001495 * in the black list.
1496 */
1497static int is_dmi_valid(const char *field)
1498{
1499 int i = 0;
1500
1501 while (dmi_blacklist[i]) {
1502 if (strstr(field, dmi_blacklist[i]))
1503 return 0;
1504 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001505 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001506
1507 return 1;
1508}
1509
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001510/*
1511 * Append a string to card->dmi_longname with character cleanups.
1512 */
1513static void append_dmi_string(struct snd_soc_card *card, const char *str)
1514{
1515 char *dst = card->dmi_longname;
1516 size_t dst_len = sizeof(card->dmi_longname);
1517 size_t len;
1518
1519 len = strlen(dst);
1520 snprintf(dst + len, dst_len - len, "-%s", str);
1521
1522 len++; /* skip the separator "-" */
1523 if (len < dst_len)
1524 cleanup_dmi_name(dst + len);
1525}
1526
Liam Girdwood345233d2017-01-14 16:13:02 +08001527/**
1528 * snd_soc_set_dmi_name() - Register DMI names to card
1529 * @card: The card to register DMI names
1530 * @flavour: The flavour "differentiator" for the card amongst its peers.
1531 *
1532 * An Intel machine driver may be used by many different devices but are
1533 * difficult for userspace to differentiate, since machine drivers ususally
1534 * use their own name as the card short name and leave the card long name
1535 * blank. To differentiate such devices and fix bugs due to lack of
1536 * device-specific configurations, this function allows DMI info to be used
1537 * as the sound card long name, in the format of
1538 * "vendor-product-version-board"
1539 * (Character '-' is used to separate different DMI fields here).
1540 * This will help the user space to load the device-specific Use Case Manager
1541 * (UCM) configurations for the card.
1542 *
1543 * Possible card long names may be:
1544 * DellInc.-XPS139343-01-0310JH
1545 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1546 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1547 *
1548 * This function also supports flavoring the card longname to provide
1549 * the extra differentiation, like "vendor-product-version-board-flavor".
1550 *
1551 * We only keep number and alphabet characters and a few separator characters
1552 * in the card long name since UCM in the user space uses the card long names
1553 * as card configuration directory names and AudoConf cannot support special
1554 * charactors like SPACE.
1555 *
1556 * Returns 0 on success, otherwise a negative error code.
1557 */
1558int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1559{
1560 const char *vendor, *product, *product_version, *board;
Liam Girdwood345233d2017-01-14 16:13:02 +08001561
1562 if (card->long_name)
1563 return 0; /* long name already set by driver or from DMI */
1564
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001565 /* make up dmi long name as: vendor-product-version-board */
Liam Girdwood345233d2017-01-14 16:13:02 +08001566 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001567 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001568 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1569 return 0;
1570 }
1571
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001572 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
Liam Girdwood345233d2017-01-14 16:13:02 +08001573 cleanup_dmi_name(card->dmi_longname);
1574
1575 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001576 if (product && is_dmi_valid(product)) {
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001577 append_dmi_string(card, product);
Liam Girdwood345233d2017-01-14 16:13:02 +08001578
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001579 /*
1580 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001581 * name in the product version field
1582 */
1583 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001584 if (product_version && is_dmi_valid(product_version))
1585 append_dmi_string(card, product_version);
Liam Girdwood345233d2017-01-14 16:13:02 +08001586 }
1587
1588 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001589 if (board && is_dmi_valid(board)) {
Jaroslav Kysela39870b02019-11-20 18:44:35 +01001590 if (!product || strcasecmp(board, product))
1591 append_dmi_string(card, board);
Liam Girdwood345233d2017-01-14 16:13:02 +08001592 } else if (!product) {
1593 /* fall back to using legacy name */
1594 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1595 return 0;
1596 }
1597
1598 /* Add flavour to dmi long name */
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001599 if (flavour)
1600 append_dmi_string(card, flavour);
Liam Girdwood345233d2017-01-14 16:13:02 +08001601
1602 /* set the card long name */
1603 card->long_name = card->dmi_longname;
1604
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001608#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001609
Liam Girdwooda655de82018-07-02 16:59:54 +01001610static void soc_check_tplg_fes(struct snd_soc_card *card)
1611{
1612 struct snd_soc_component *component;
1613 const struct snd_soc_component_driver *comp_drv;
1614 struct snd_soc_dai_link *dai_link;
1615 int i;
1616
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001617 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001618
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001619 /* does this component override BEs ? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001620 if (!component->driver->ignore_machine)
1621 continue;
1622
1623 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001624 if (!strcmp(component->driver->ignore_machine,
1625 card->dev->driver->name))
1626 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001627 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001628 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001629 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001630match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001631 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001632 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001633
1634 /* ignore this FE */
1635 if (dai_link->dynamic) {
1636 dai_link->ignore = true;
1637 continue;
1638 }
1639
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001640 dev_info(card->dev, "info: override BE DAI link %s\n",
Liam Girdwooda655de82018-07-02 16:59:54 +01001641 card->dai_link[i].name);
1642
1643 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001644 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001645 dev_err(card->dev, "init platform error");
1646 continue;
1647 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001648 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001649
1650 /* convert non BE into BE */
1651 dai_link->no_pcm = 1;
Daniel Baluta218fe9b2019-12-04 17:13:33 +02001652 dai_link->dpcm_playback = 1;
1653 dai_link->dpcm_capture = 1;
Liam Girdwooda655de82018-07-02 16:59:54 +01001654
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +09001655 /*
1656 * override any BE fixups
1657 * see
1658 * snd_soc_link_be_hw_params_fixup()
1659 */
Liam Girdwooda655de82018-07-02 16:59:54 +01001660 dai_link->be_hw_params_fixup =
1661 component->driver->be_hw_params_fixup;
1662
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001663 /*
1664 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001665 * dai link name if it's NULL to help bind widgets.
1666 */
1667 if (!dai_link->stream_name)
1668 dai_link->stream_name = dai_link->name;
1669 }
1670
1671 /* Inform userspace we are using alternate topology */
1672 if (component->driver->topology_name_prefix) {
1673
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001674 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001675 if (!card->topology_shortname_created) {
1676 comp_drv = component->driver;
1677
1678 snprintf(card->topology_shortname, 32, "%s-%s",
1679 comp_drv->topology_name_prefix,
1680 card->name);
1681 card->topology_shortname_created = true;
1682 }
1683
1684 /* use topology shortname */
1685 card->name = card->topology_shortname;
1686 }
1687 }
1688}
1689
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001690#define soc_setup_card_name(name, name1, name2, norm) \
1691 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1692static void __soc_setup_card_name(char *name, int len,
1693 const char *name1, const char *name2,
1694 int normalization)
1695{
1696 int i;
1697
1698 snprintf(name, len, "%s", name1 ? name1 : name2);
1699
1700 if (!normalization)
1701 return;
1702
1703 /*
1704 * Name normalization
1705 *
1706 * The driver name is somewhat special, as it's used as a key for
1707 * searches in the user-space.
1708 *
1709 * ex)
1710 * "abcd??efg" -> "abcd__efg"
1711 */
1712 for (i = 0; i < len; i++) {
1713 switch (name[i]) {
1714 case '_':
1715 case '-':
1716 case '\0':
1717 break;
1718 default:
1719 if (!isalnum(name[i]))
1720 name[i] = '_';
1721 break;
1722 }
1723 }
1724}
1725
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001726static void soc_cleanup_card_resources(struct snd_soc_card *card)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001727{
Kuninori Morimotocc733902019-12-10 09:33:40 +09001728 struct snd_soc_pcm_runtime *rtd, *n;
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001729
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001730 if (card->snd_card)
1731 snd_card_disconnect_sync(card->snd_card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001732
Kuninori Morimoto2a6f0892019-11-13 10:16:29 +09001733 snd_soc_dapm_shutdown(card);
1734
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001735 /* remove and free each DAI */
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001736 soc_remove_link_dais(card);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001737 soc_remove_link_components(card);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001738
Kuninori Morimotocc733902019-12-10 09:33:40 +09001739 for_each_card_rtds_safe(card, rtd, n)
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +09001740 snd_soc_remove_pcm_runtime(card, rtd);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001741
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001742 /* remove auxiliary devices */
1743 soc_remove_aux_devices(card);
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001744 soc_unbind_aux_dev(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001745
1746 snd_soc_dapm_free(&card->dapm);
1747 soc_cleanup_card_debugfs(card);
1748
1749 /* remove the card */
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001750 if (card->probed && card->remove)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001751 card->remove(card);
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001752 card->probed = 0;
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001753
1754 if (card->snd_card) {
1755 snd_card_free(card->snd_card);
1756 card->snd_card = NULL;
1757 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001758}
1759
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001760static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1761{
1762 if (card->instantiated) {
1763 card->instantiated = false;
1764 snd_soc_flush_all_delayed_work(card);
1765
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001766 soc_cleanup_card_resources(card);
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001767 if (!unregister)
1768 list_add(&card->list, &unbind_card_list);
1769 } else {
1770 if (unregister)
1771 list_del(&card->list);
1772 }
1773}
1774
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001775static int snd_soc_bind_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001776{
Mengdong Lin1a497982015-11-18 02:34:11 -05001777 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001778 struct snd_soc_component *component;
Mengdong Lin61b00882015-12-02 14:11:48 +08001779 struct snd_soc_dai_link *dai_link;
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001780 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001781
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001782 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001783 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001785 snd_soc_dapm_init(&card->dapm, card, NULL);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001786
Liam Girdwooda655de82018-07-02 16:59:54 +01001787 /* check whether any platform is ignore machine FE and using topology */
1788 soc_check_tplg_fes(card);
1789
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001790 /* bind aux_devs too */
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001791 ret = soc_bind_aux_dev(card);
1792 if (ret < 0)
1793 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794
Mengdong Linf8f80362015-12-02 14:11:22 +08001795 /* add predefined DAI links to the list */
Kuninori Morimotod8145982019-10-02 14:23:07 +09001796 card->num_rtd = 0;
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001797 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimoto0c048002019-12-10 09:34:19 +09001798 ret = snd_soc_add_pcm_runtime(card, dai_link);
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001799 if (ret < 0)
1800 goto probe_end;
1801 }
Mengdong Linf8f80362015-12-02 14:11:22 +08001802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001803 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001804 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001805 card->owner, 0, &card->snd_card);
1806 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001807 dev_err(card->dev,
1808 "ASoC: can't create sound card for card %s: %d\n",
1809 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001810 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001813 soc_init_card_debugfs(card);
1814
Kuninori Morimotob3da4252019-08-07 10:31:24 +09001815 soc_resume_init(card);
Andy Green6ed25972008-06-13 16:24:05 +01001816
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001817 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1818 card->num_dapm_widgets);
1819 if (ret < 0)
1820 goto probe_end;
Mark Brown9a841eb2011-04-12 17:51:37 -07001821
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001822 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1823 card->num_of_dapm_widgets);
1824 if (ret < 0)
1825 goto probe_end;
Nicolin Chenf23e8602015-02-14 17:22:49 -08001826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 /* initialise the sound card only once */
Kuninori Morimoto73de4b022020-05-28 10:49:11 +09001828 ret = snd_soc_card_probe(card);
1829 if (ret < 0)
1830 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831
Stephen Warren62ae68f2012-06-08 12:34:23 -06001832 /* probe all components used by DAI links on this card */
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001833 ret = soc_probe_link_components(card);
1834 if (ret < 0) {
1835 dev_err(card->dev,
1836 "ASoC: failed to instantiate card %d\n", ret);
1837 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001838 }
1839
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001840 /* probe auxiliary components */
1841 ret = soc_probe_aux_devices(card);
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001842 if (ret < 0) {
1843 dev_err(card->dev,
1844 "ASoC: failed to probe aux component %d\n", ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001845 goto probe_end;
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001846 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001847
Stephen Warren62ae68f2012-06-08 12:34:23 -06001848 /* probe all DAI links on this card */
Kuninori Morimotoc7e73772019-09-04 09:15:17 +09001849 ret = soc_probe_link_dais(card);
1850 if (ret < 0) {
1851 dev_err(card->dev,
1852 "ASoC: failed to instantiate card %d\n", ret);
1853 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001854 }
1855
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001856 for_each_card_rtds(card, rtd) {
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001857 ret = soc_init_pcm_runtime(card, rtd);
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001858 if (ret < 0)
1859 goto probe_end;
1860 }
Kuninori Morimotoc4b46982019-09-04 09:15:12 +09001861
Mark Brown888df392012-02-16 19:37:51 -08001862 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001863 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001864
Kuninori Morimoto9b98c7c2019-08-07 10:31:08 +09001865 ret = snd_soc_add_card_controls(card, card->controls,
1866 card->num_controls);
1867 if (ret < 0)
1868 goto probe_end;
Mark Brownb7af1da2011-04-07 19:18:44 +09001869
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001870 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1871 card->num_dapm_routes);
Pierre-Louis Bossarta22ae722020-03-09 14:27:43 -05001872 if (ret < 0) {
1873 if (card->disable_route_checks) {
1874 dev_info(card->dev,
1875 "%s: disable_route_checks set, ignoring errors on add_routes\n",
1876 __func__);
1877 } else {
1878 dev_err(card->dev,
1879 "%s: snd_soc_dapm_add_routes failed: %d\n",
1880 __func__, ret);
1881 goto probe_end;
1882 }
1883 }
Mark Brownb8ad29d2011-03-02 18:35:51 +00001884
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001885 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1886 card->num_of_dapm_routes);
1887 if (ret < 0)
1888 goto probe_end;
Mark Brown75d9ac42011-09-27 16:41:01 +01001889
Takashi Iwai861886d2017-04-24 08:54:42 +02001890 /* try to set some sane longname if DMI is available */
1891 snd_soc_set_dmi_name(card, NULL);
1892
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001893 soc_setup_card_name(card->snd_card->shortname,
1894 card->name, NULL, 0);
1895 soc_setup_card_name(card->snd_card->longname,
1896 card->long_name, card->name, 0);
1897 soc_setup_card_name(card->snd_card->driver,
1898 card->driver_name, card->name, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001899
Jaroslav Kyseladc73d732019-11-19 18:49:32 +01001900 if (card->components) {
1901 /* the current implementation of snd_component_add() accepts */
1902 /* multiple components in the string separated by space, */
1903 /* but the string collision (identical string) check might */
1904 /* not work correctly */
1905 ret = snd_component_add(card->snd_card, card->components);
1906 if (ret < 0) {
1907 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1908 card->name, ret);
1909 goto probe_end;
1910 }
1911 }
1912
Kuninori Morimoto5c0eac02020-05-28 10:49:20 +09001913 ret = snd_soc_card_late_probe(card);
1914 if (ret < 0)
1915 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00001916
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001917 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919 ret = snd_card_register(card->snd_card);
1920 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001921 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1922 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001923 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001924 }
1925
Mark Brown435c5e22008-12-04 15:32:53 +00001926 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08001927 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01001928 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00001929
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001930 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001931 for_each_card_components(card, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001932 if (!snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001933 pinctrl_pm_select_sleep_state(component->dev);
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001934
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001935probe_end:
1936 if (ret < 0)
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001937 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001938
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001939 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001940 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941
Mark Brownb19e6e72012-03-14 21:18:39 +00001942 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001943}
1944
1945/* probes a new socdev */
1946static int soc_probe(struct platform_device *pdev)
1947{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001949
Vinod Koul70a7ca32011-01-14 19:22:48 +05301950 /*
1951 * no card, so machine driver should be registering card
1952 * we should not be here in that case so ret error
1953 */
1954 if (!card)
1955 return -EINVAL;
1956
Mark Brownfe4085e2012-03-02 13:07:41 +00001957 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001958 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001959 card->name);
1960
Mark Brown435c5e22008-12-04 15:32:53 +00001961 /* Bodge while we unpick instantiation */
1962 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001963
Mark Brown28d528c2012-08-09 18:45:23 +01001964 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965}
1966
1967/* removes a socdev */
1968static int soc_remove(struct platform_device *pdev)
1969{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001970 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971
Mark Brownc5af3a22008-11-28 13:29:45 +00001972 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973 return 0;
1974}
1975
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001976int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001977{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001978 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001979 struct snd_soc_component *component;
Mark Brown51737472009-06-22 13:16:51 +01001980
1981 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001982 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001983
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001984 /*
1985 * Flush out pmdown_time work - we actually do want to run it
1986 * now, we're shutting down so no imminent restart.
1987 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09001988 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01001989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001990 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001991
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001992 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001993 for_each_card_components(card, component)
1994 pinctrl_pm_select_sleep_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001995
Mark Brown416356f2009-06-30 19:05:15 +01001996 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001997}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001998EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001999
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002000const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302001 .suspend = snd_soc_suspend,
2002 .resume = snd_soc_resume,
2003 .freeze = snd_soc_suspend,
2004 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002005 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302006 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002007};
Stephen Warrendeb26072011-04-05 19:35:30 -06002008EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002009
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010/* ASoC platform driver */
2011static struct platform_driver soc_driver = {
2012 .driver = {
2013 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002014 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 },
2016 .probe = soc_probe,
2017 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002018};
2019
Mark Brown096e49d2009-07-05 15:12:22 +01002020/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002021 * snd_soc_cnew - create new control
2022 * @_template: control template
2023 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002024 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002025 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 *
2027 * Create a new mixer control from a template control.
2028 *
2029 * Returns 0 for success, else error.
2030 */
2031struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002032 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002033 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034{
2035 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002036 struct snd_kcontrol *kcontrol;
2037 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038
2039 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002040 template.index = 0;
2041
Mark Brownefb7ac32011-03-08 17:23:24 +00002042 if (!long_name)
2043 long_name = template.name;
2044
2045 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002046 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002047 if (!name)
2048 return NULL;
2049
Mark Brownefb7ac32011-03-08 17:23:24 +00002050 template.name = name;
2051 } else {
2052 template.name = long_name;
2053 }
2054
2055 kcontrol = snd_ctl_new1(&template, data);
2056
2057 kfree(name);
2058
2059 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002060}
2061EXPORT_SYMBOL_GPL(snd_soc_cnew);
2062
Liam Girdwood022658b2012-02-03 17:43:09 +00002063static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2064 const struct snd_kcontrol_new *controls, int num_controls,
2065 const char *prefix, void *data)
2066{
2067 int err, i;
2068
2069 for (i = 0; i < num_controls; i++) {
2070 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002071
Liam Girdwood022658b2012-02-03 17:43:09 +00002072 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2073 control->name, prefix));
2074 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002075 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2076 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002077 return err;
2078 }
2079 }
2080
2081 return 0;
2082}
2083
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002085 * snd_soc_add_component_controls - Add an array of controls to a component.
2086 *
2087 * @component: Component to add controls to
2088 * @controls: Array of controls to add
2089 * @num_controls: Number of elements in the array
2090 *
2091 * Return: 0 for success, else error.
2092 */
2093int snd_soc_add_component_controls(struct snd_soc_component *component,
2094 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2095{
2096 struct snd_card *card = component->card->snd_card;
2097
2098 return snd_soc_add_controls(card, component->dev, controls,
2099 num_controls, component->name_prefix, component);
2100}
2101EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2102
2103/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002104 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2105 * Convenience function to add a list of controls.
2106 *
2107 * @soc_card: SoC card 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_card_controls(struct snd_soc_card *soc_card,
2114 const struct snd_kcontrol_new *controls, int num_controls)
2115{
2116 struct snd_card *card = soc_card->snd_card;
2117
2118 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2119 NULL, soc_card);
2120}
2121EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2122
2123/**
2124 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2125 * Convienience function to add a list of controls.
2126 *
2127 * @dai: DAI 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_dai_controls(struct snd_soc_dai *dai,
2134 const struct snd_kcontrol_new *controls, int num_controls)
2135{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002136 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002137
2138 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2139 NULL, dai);
2140}
2141EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2142
Mark Brownc5af3a22008-11-28 13:29:45 +00002143/**
2144 * snd_soc_register_card - Register a card with the ASoC core
2145 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002146 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002147 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002148 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302149int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002150{
2151 if (!card->name || !card->dev)
2152 return -EINVAL;
2153
Mark Browned77cc12011-05-03 18:25:34 +01002154 dev_set_drvdata(card->dev, card);
2155
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002156 INIT_LIST_HEAD(&card->widgets);
2157 INIT_LIST_HEAD(&card->paths);
2158 INIT_LIST_HEAD(&card->dapm_list);
2159 INIT_LIST_HEAD(&card->aux_comp_list);
2160 INIT_LIST_HEAD(&card->component_dev_list);
2161 INIT_LIST_HEAD(&card->list);
Mengdong Lin1a497982015-11-18 02:34:11 -05002162 INIT_LIST_HEAD(&card->rtd_list);
Mark Browndb432b42011-10-03 21:06:40 +01002163 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002164 INIT_LIST_HEAD(&card->dobj_list);
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002165
Mark Brownc5af3a22008-11-28 13:29:45 +00002166 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002167 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002168 mutex_init(&card->dapm_mutex);
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03002169 mutex_init(&card->pcm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002170 spin_lock_init(&card->dpcm_lock);
Mark Brownc5af3a22008-11-28 13:29:45 +00002171
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002172 return snd_soc_bind_card(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002173}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302174EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002175
2176/**
2177 * snd_soc_unregister_card - Unregister a card with the ASoC core
2178 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002179 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002180 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002181 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302182int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002183{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002184 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002185 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002186 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002187 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002188
2189 return 0;
2190}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302191EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002192
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002193/*
2194 * Simplify DAI link configuration by removing ".-1" from device names
2195 * and sanitizing names.
2196 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002197static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002198{
2199 char *found, name[NAME_SIZE];
2200 int id1, id2;
2201
2202 if (dev_name(dev) == NULL)
2203 return NULL;
2204
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002205 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002206
2207 /* are we a "%s.%d" name (platform and SPI components) */
2208 found = strstr(name, dev->driver->name);
2209 if (found) {
2210 /* get ID */
2211 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2212
2213 /* discard ID from name if ID == -1 */
2214 if (*id == -1)
2215 found[strlen(dev->driver->name)] = '\0';
2216 }
2217
2218 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002219 /* I2C component devices are named "bus-addr" */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002220 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2221 char tmp[NAME_SIZE];
2222
2223 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002224 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002225
2226 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002227 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2228 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002229 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002230 } else
2231 *id = 0;
2232 }
2233
Kuninori Morimoto50014492019-10-02 14:22:57 +09002234 return devm_kstrdup(dev, name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002235}
2236
2237/*
2238 * Simplify DAI link naming for single devices with multiple DAIs by removing
2239 * any ".-1" and using the DAI name (instead of device name).
2240 */
2241static inline char *fmt_multiple_name(struct device *dev,
2242 struct snd_soc_dai_driver *dai_drv)
2243{
2244 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002245 dev_err(dev,
2246 "ASoC: error - multiple DAI %s registered with no name\n",
2247 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002248 return NULL;
2249 }
2250
Kuninori Morimoto50014492019-10-02 14:22:57 +09002251 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002252}
2253
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002254void snd_soc_unregister_dai(struct snd_soc_dai *dai)
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002255{
2256 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2257 list_del(&dai->list);
2258}
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002259EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002260
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002261/**
2262 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2263 *
2264 * @component: The component the DAIs are registered for
2265 * @dai_drv: DAI driver to use for the DAI
Randy Dunlapbc9a6652019-12-08 20:37:04 -08002266 * @legacy_dai_naming: if %true, use legacy single-name format;
2267 * if %false, use multiple-name format;
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002268 *
2269 * Topology can use this API to register DAIs when probing a component.
2270 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2271 * will be freed in the component cleanup.
2272 */
2273struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2274 struct snd_soc_dai_driver *dai_drv,
2275 bool legacy_dai_naming)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002276{
2277 struct device *dev = component->dev;
2278 struct snd_soc_dai *dai;
2279
2280 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2281
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002282 lockdep_assert_held(&client_mutex);
2283
Kuninori Morimoto50014492019-10-02 14:22:57 +09002284 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002285 if (dai == NULL)
2286 return NULL;
2287
2288 /*
2289 * Back in the old days when we still had component-less DAIs,
2290 * instead of having a static name, component-less DAIs would
2291 * inherit the name of the parent device so it is possible to
2292 * register multiple instances of the DAI. We still need to keep
2293 * the same naming style even though those DAIs are not
2294 * component-less anymore.
2295 */
2296 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002297 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002298 dai->name = fmt_single_name(dev, &dai->id);
2299 } else {
2300 dai->name = fmt_multiple_name(dev, dai_drv);
2301 if (dai_drv->id)
2302 dai->id = dai_drv->id;
2303 else
2304 dai->id = component->num_dai;
2305 }
Kuninori Morimoto50014492019-10-02 14:22:57 +09002306 if (!dai->name)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002307 return NULL;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002308
2309 dai->component = component;
2310 dai->dev = dev;
2311 dai->driver = dai_drv;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002312
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002313 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002314 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002315 component->num_dai++;
2316
2317 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2318 return dai;
2319}
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002320
Mark Brown91151712008-11-30 23:31:24 +00002321/**
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002322 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2323 *
2324 * @component: The component for which the DAIs should be unregistered
2325 */
2326static void snd_soc_unregister_dais(struct snd_soc_component *component)
2327{
2328 struct snd_soc_dai *dai, *_dai;
2329
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002330 for_each_component_dais_safe(component, dai, _dai)
2331 snd_soc_unregister_dai(dai);
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002332}
2333
2334/**
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002335 * snd_soc_register_dais - Register a DAI with the ASoC core
2336 *
2337 * @component: The component the DAIs are registered for
2338 * @dai_drv: DAI driver to use for the DAIs
2339 * @count: Number of DAIs
2340 */
2341static int snd_soc_register_dais(struct snd_soc_component *component,
2342 struct snd_soc_dai_driver *dai_drv,
2343 size_t count)
2344{
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002345 struct snd_soc_dai *dai;
2346 unsigned int i;
2347 int ret;
2348
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002349 for (i = 0; i < count; i++) {
Kuninori Morimoto71cb85f2019-11-05 15:47:18 +09002350 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002351 !component->driver->non_legacy_dai_naming);
2352 if (dai == NULL) {
2353 ret = -ENOMEM;
2354 goto err;
2355 }
2356 }
2357
2358 return 0;
2359
2360err:
2361 snd_soc_unregister_dais(component);
2362
2363 return ret;
2364}
2365
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002366static int snd_soc_component_initialize(struct snd_soc_component *component,
2367 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002368{
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002369 INIT_LIST_HEAD(&component->dai_list);
Kuninori Morimoto495efdb2019-08-20 14:05:20 +09002370 INIT_LIST_HEAD(&component->dobj_list);
2371 INIT_LIST_HEAD(&component->card_list);
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002372 mutex_init(&component->io_mutex);
2373
Jerome Brunet6b62fa92020-02-19 11:25:26 +01002374 component->name = fmt_single_name(dev, &component->id);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002375 if (!component->name) {
2376 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002377 return -ENOMEM;
2378 }
2379
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002380 component->dev = dev;
2381 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002382
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002383 return 0;
2384}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002385
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002386static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002387{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002388 int val_bytes = regmap_get_val_bytes(component->regmap);
2389
2390 /* Errors are legitimate for non-integer byte multiples */
2391 if (val_bytes > 0)
2392 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002393}
2394
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002395#ifdef CONFIG_REGMAP
2396
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002397/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002398 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2399 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002400 * @component: The component for which to initialize the regmap instance
2401 * @regmap: The regmap instance that should be used by the component
2402 *
2403 * This function allows deferred assignment of the regmap instance that is
2404 * associated with the component. Only use this if the regmap instance is not
2405 * yet ready when the component is registered. The function must also be called
2406 * before the first IO attempt of the component.
2407 */
2408void snd_soc_component_init_regmap(struct snd_soc_component *component,
2409 struct regmap *regmap)
2410{
2411 component->regmap = regmap;
2412 snd_soc_component_setup_regmap(component);
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2415
2416/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002417 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2418 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002419 * @component: The component for which to de-initialize the regmap instance
2420 *
2421 * Calls regmap_exit() on the regmap instance associated to the component and
2422 * removes the regmap instance from the component.
2423 *
2424 * This function should only be used if snd_soc_component_init_regmap() was used
2425 * to initialize the regmap instance.
2426 */
2427void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2428{
2429 regmap_exit(component->regmap);
2430 component->regmap = NULL;
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2433
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002434#endif
2435
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002436#define ENDIANNESS_MAP(name) \
2437 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2438static u64 endianness_format_map[] = {
2439 ENDIANNESS_MAP(S16_),
2440 ENDIANNESS_MAP(U16_),
2441 ENDIANNESS_MAP(S24_),
2442 ENDIANNESS_MAP(U24_),
2443 ENDIANNESS_MAP(S32_),
2444 ENDIANNESS_MAP(U32_),
2445 ENDIANNESS_MAP(S24_3),
2446 ENDIANNESS_MAP(U24_3),
2447 ENDIANNESS_MAP(S20_3),
2448 ENDIANNESS_MAP(U20_3),
2449 ENDIANNESS_MAP(S18_3),
2450 ENDIANNESS_MAP(U18_3),
2451 ENDIANNESS_MAP(FLOAT_),
2452 ENDIANNESS_MAP(FLOAT64_),
2453 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2454};
2455
2456/*
2457 * Fix up the DAI formats for endianness: codecs don't actually see
2458 * the endianness of the data but we're using the CPU format
2459 * definitions which do need to include endianness so we ensure that
2460 * codec DAIs always have both big and little endian variants set.
2461 */
2462static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2463{
2464 int i;
2465
2466 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2467 if (stream->formats & endianness_format_map[i])
2468 stream->formats |= endianness_format_map[i];
2469}
2470
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002471static void snd_soc_try_rebind_card(void)
2472{
2473 struct snd_soc_card *card, *c;
2474
Kuninori Morimotob245d272019-08-07 10:31:19 +09002475 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2476 if (!snd_soc_bind_card(card))
2477 list_del(&card->list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002478}
2479
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002480static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2481{
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002482 struct snd_soc_card *card = component->card;
2483
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002484 snd_soc_unregister_dais(component);
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002485
2486 if (card)
2487 snd_soc_unbind_card(card, false);
2488
2489 list_del(&component->list);
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002490}
2491
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002492int snd_soc_add_component(struct device *dev,
2493 struct snd_soc_component *component,
2494 const struct snd_soc_component_driver *component_driver,
2495 struct snd_soc_dai_driver *dai_drv,
2496 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002497{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002498 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002499 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002500
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002501 mutex_lock(&client_mutex);
2502
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002503 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002504 if (ret)
2505 goto err_free;
2506
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002507 if (component_driver->endianness) {
2508 for (i = 0; i < num_dai; i++) {
2509 convert_endianness_formats(&dai_drv[i].playback);
2510 convert_endianness_formats(&dai_drv[i].capture);
2511 }
2512 }
2513
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002514 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002515 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002516 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002517 goto err_cleanup;
2518 }
2519
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002520 if (!component->driver->write && !component->driver->read) {
2521 if (!component->regmap)
2522 component->regmap = dev_get_regmap(component->dev,
2523 NULL);
2524 if (component->regmap)
2525 snd_soc_component_setup_regmap(component);
2526 }
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002527
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002528 /* see for_each_component */
2529 list_add(&component->list, &component_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002530
2531err_cleanup:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002532 if (ret < 0)
2533 snd_soc_del_component_unlocked(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002534err_free:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002535 mutex_unlock(&client_mutex);
2536
2537 if (ret == 0)
2538 snd_soc_try_rebind_card();
2539
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002540 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002541}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002542EXPORT_SYMBOL_GPL(snd_soc_add_component);
2543
2544int snd_soc_register_component(struct device *dev,
2545 const struct snd_soc_component_driver *component_driver,
2546 struct snd_soc_dai_driver *dai_drv,
2547 int num_dai)
2548{
2549 struct snd_soc_component *component;
2550
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002551 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002552 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002553 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002554
2555 return snd_soc_add_component(dev, component, component_driver,
2556 dai_drv, num_dai);
2557}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002558EXPORT_SYMBOL_GPL(snd_soc_register_component);
2559
2560/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002561 * snd_soc_unregister_component - Unregister all related component
2562 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002563 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002564 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002565 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002566void snd_soc_unregister_component(struct device *dev)
2567{
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002568 struct snd_soc_component *component;
2569
2570 mutex_lock(&client_mutex);
2571 while (1) {
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +09002572 component = snd_soc_lookup_component_nolocked(dev, NULL);
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002573 if (!component)
2574 break;
2575
2576 snd_soc_del_component_unlocked(component);
2577 }
2578 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002579}
2580EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2581
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002582/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002583int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2584 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002585{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002586 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002587 int ret;
2588
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302589 if (!card->dev) {
2590 pr_err("card->dev is not set before calling %s\n", __func__);
2591 return -EINVAL;
2592 }
2593
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002594 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302595
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002596 ret = of_property_read_string_index(np, propname, 0, &card->name);
2597 /*
2598 * EINVAL means the property does not exist. This is fine providing
2599 * card->name was previously set, which is checked later in
2600 * snd_soc_register_card.
2601 */
2602 if (ret < 0 && ret != -EINVAL) {
2603 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002604 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002605 propname, ret);
2606 return ret;
2607 }
2608
2609 return 0;
2610}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002611EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002612
Xiubo Li9a6d4862014-02-08 15:59:52 +08002613static const struct snd_soc_dapm_widget simple_widgets[] = {
2614 SND_SOC_DAPM_MIC("Microphone", NULL),
2615 SND_SOC_DAPM_LINE("Line", NULL),
2616 SND_SOC_DAPM_HP("Headphone", NULL),
2617 SND_SOC_DAPM_SPK("Speaker", NULL),
2618};
2619
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002620int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002621 const char *propname)
2622{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002623 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002624 struct snd_soc_dapm_widget *widgets;
2625 const char *template, *wname;
2626 int i, j, num_widgets, ret;
2627
2628 num_widgets = of_property_count_strings(np, propname);
2629 if (num_widgets < 0) {
2630 dev_err(card->dev,
2631 "ASoC: Property '%s' does not exist\n", propname);
2632 return -EINVAL;
2633 }
2634 if (num_widgets & 1) {
2635 dev_err(card->dev,
2636 "ASoC: Property '%s' length is not even\n", propname);
2637 return -EINVAL;
2638 }
2639
2640 num_widgets /= 2;
2641 if (!num_widgets) {
2642 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2643 propname);
2644 return -EINVAL;
2645 }
2646
2647 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2648 GFP_KERNEL);
2649 if (!widgets) {
2650 dev_err(card->dev,
2651 "ASoC: Could not allocate memory for widgets\n");
2652 return -ENOMEM;
2653 }
2654
2655 for (i = 0; i < num_widgets; i++) {
2656 ret = of_property_read_string_index(np, propname,
2657 2 * i, &template);
2658 if (ret) {
2659 dev_err(card->dev,
2660 "ASoC: Property '%s' index %d read error:%d\n",
2661 propname, 2 * i, ret);
2662 return -EINVAL;
2663 }
2664
2665 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2666 if (!strncmp(template, simple_widgets[j].name,
2667 strlen(simple_widgets[j].name))) {
2668 widgets[i] = simple_widgets[j];
2669 break;
2670 }
2671 }
2672
2673 if (j >= ARRAY_SIZE(simple_widgets)) {
2674 dev_err(card->dev,
2675 "ASoC: DAPM widget '%s' is not supported\n",
2676 template);
2677 return -EINVAL;
2678 }
2679
2680 ret = of_property_read_string_index(np, propname,
2681 (2 * i) + 1,
2682 &wname);
2683 if (ret) {
2684 dev_err(card->dev,
2685 "ASoC: Property '%s' index %d read error:%d\n",
2686 propname, (2 * i) + 1, ret);
2687 return -EINVAL;
2688 }
2689
2690 widgets[i].name = wname;
2691 }
2692
Nicolin Chenf23e8602015-02-14 17:22:49 -08002693 card->of_dapm_widgets = widgets;
2694 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002695
2696 return 0;
2697}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002698EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08002699
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002700int snd_soc_of_get_slot_mask(struct device_node *np,
2701 const char *prop_name,
2702 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03002703{
2704 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03002705 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03002706 int i;
2707
2708 if (!of_slot_mask)
2709 return 0;
2710 val /= sizeof(u32);
2711 for (i = 0; i < val; i++)
2712 if (be32_to_cpup(&of_slot_mask[i]))
2713 *mask |= (1 << i);
2714
2715 return val;
2716}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002717EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03002718
Xiubo Li89c67852014-02-14 09:34:35 +08002719int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03002720 unsigned int *tx_mask,
2721 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08002722 unsigned int *slots,
2723 unsigned int *slot_width)
2724{
2725 u32 val;
2726 int ret;
2727
Jyri Sarha61310842015-09-09 21:27:43 +03002728 if (tx_mask)
2729 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2730 if (rx_mask)
2731 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2732
Xiubo Li89c67852014-02-14 09:34:35 +08002733 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2734 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2735 if (ret)
2736 return ret;
2737
2738 if (slots)
2739 *slots = val;
2740 }
2741
2742 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2743 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2744 if (ret)
2745 return ret;
2746
2747 if (slot_width)
2748 *slot_width = val;
2749 }
2750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2754
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002755void snd_soc_of_parse_node_prefix(struct device_node *np,
2756 struct snd_soc_codec_conf *codec_conf,
2757 struct device_node *of_node,
2758 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002759{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002760 const char *str;
2761 int ret;
2762
2763 ret = of_property_read_string(np, propname, &str);
2764 if (ret < 0) {
2765 /* no prefix is not error */
2766 return;
2767 }
2768
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09002769 codec_conf->dlc.of_node = of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002770 codec_conf->name_prefix = str;
2771}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002772EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002773
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002774int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002775 const char *propname)
2776{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002777 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00002778 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002779 struct snd_soc_dapm_route *routes;
2780 int i, ret;
2781
2782 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08002783 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002784 dev_err(card->dev,
2785 "ASoC: Property '%s' does not exist or its length is not even\n",
2786 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002787 return -EINVAL;
2788 }
2789 num_routes /= 2;
2790 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002791 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002792 propname);
2793 return -EINVAL;
2794 }
2795
Kees Cooka86854d2018-06-12 14:07:58 -07002796 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002797 GFP_KERNEL);
2798 if (!routes) {
2799 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002800 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002801 return -EINVAL;
2802 }
2803
2804 for (i = 0; i < num_routes; i++) {
2805 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002806 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002807 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09002808 dev_err(card->dev,
2809 "ASoC: Property '%s' index %d could not be read: %d\n",
2810 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002811 return -EINVAL;
2812 }
2813 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002814 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002815 if (ret) {
2816 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09002817 "ASoC: Property '%s' index %d could not be read: %d\n",
2818 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002819 return -EINVAL;
2820 }
2821 }
2822
Nicolin Chenf23e8602015-02-14 17:22:49 -08002823 card->num_of_dapm_routes = num_routes;
2824 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002825
2826 return 0;
2827}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002828EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002829
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002830unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02002831 const char *prefix,
2832 struct device_node **bitclkmaster,
2833 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002834{
2835 int ret, i;
2836 char prop[128];
2837 unsigned int format = 0;
2838 int bit, frame;
2839 const char *str;
2840 struct {
2841 char *name;
2842 unsigned int val;
2843 } of_fmt_table[] = {
2844 { "i2s", SND_SOC_DAIFMT_I2S },
2845 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2846 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2847 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2848 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2849 { "ac97", SND_SOC_DAIFMT_AC97 },
2850 { "pdm", SND_SOC_DAIFMT_PDM},
2851 { "msb", SND_SOC_DAIFMT_MSB },
2852 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002853 };
2854
2855 if (!prefix)
2856 prefix = "";
2857
2858 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002859 * check "dai-format = xxx"
2860 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002861 * SND_SOC_DAIFMT_FORMAT_MASK area
2862 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002863 ret = of_property_read_string(np, "dai-format", &str);
2864 if (ret < 0) {
2865 snprintf(prop, sizeof(prop), "%sformat", prefix);
2866 ret = of_property_read_string(np, prop, &str);
2867 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002868 if (ret == 0) {
2869 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2870 if (strcmp(str, of_fmt_table[i].name) == 0) {
2871 format |= of_fmt_table[i].val;
2872 break;
2873 }
2874 }
2875 }
2876
2877 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002878 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002879 * SND_SOC_DAIFMT_CLOCK_MASK area
2880 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002881 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02002882 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002883 format |= SND_SOC_DAIFMT_CONT;
2884 else
2885 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002886
2887 /*
2888 * check "[prefix]bitclock-inversion"
2889 * check "[prefix]frame-inversion"
2890 * SND_SOC_DAIFMT_INV_MASK area
2891 */
2892 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2893 bit = !!of_get_property(np, prop, NULL);
2894
2895 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2896 frame = !!of_get_property(np, prop, NULL);
2897
2898 switch ((bit << 4) + frame) {
2899 case 0x11:
2900 format |= SND_SOC_DAIFMT_IB_IF;
2901 break;
2902 case 0x10:
2903 format |= SND_SOC_DAIFMT_IB_NF;
2904 break;
2905 case 0x01:
2906 format |= SND_SOC_DAIFMT_NB_IF;
2907 break;
2908 default:
2909 /* SND_SOC_DAIFMT_NB_NF is default */
2910 break;
2911 }
2912
2913 /*
2914 * check "[prefix]bitclock-master"
2915 * check "[prefix]frame-master"
2916 * SND_SOC_DAIFMT_MASTER_MASK area
2917 */
2918 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2919 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002920 if (bit && bitclkmaster)
2921 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002922
2923 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2924 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002925 if (frame && framemaster)
2926 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002927
2928 switch ((bit << 4) + frame) {
2929 case 0x11:
2930 format |= SND_SOC_DAIFMT_CBM_CFM;
2931 break;
2932 case 0x10:
2933 format |= SND_SOC_DAIFMT_CBM_CFS;
2934 break;
2935 case 0x01:
2936 format |= SND_SOC_DAIFMT_CBS_CFM;
2937 break;
2938 default:
2939 format |= SND_SOC_DAIFMT_CBS_CFS;
2940 break;
2941 }
2942
2943 return format;
2944}
2945EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2946
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002947int snd_soc_get_dai_id(struct device_node *ep)
2948{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09002949 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002950 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002951 int ret;
2952
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002953 dlc.of_node = of_graph_get_port_parent(ep);
2954 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002955 /*
2956 * For example HDMI case, HDMI has video/sound port,
2957 * but ALSA SoC needs sound port number only.
2958 * Thus counting HDMI DT port/endpoint doesn't work.
2959 * Then, it should have .of_xlate_dai_id
2960 */
2961 ret = -ENOTSUPP;
2962 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002963 component = soc_find_component(&dlc);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +09002964 if (component)
2965 ret = snd_soc_component_of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002966 mutex_unlock(&client_mutex);
2967
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002968 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07002969
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002970 return ret;
2971}
2972EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2973
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00002974int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002975 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07002976{
2977 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03002978 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002979 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07002980
2981 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002982 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09002983 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03002984
2985 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07002986 continue;
2987
Kuninori Morimotoa2a34172019-07-26 13:51:31 +09002988 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
2989 if (ret == -ENOTSUPP) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002990 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07002991 int id = -1;
2992
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002993 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07002994 case 0:
2995 id = 0; /* same as dai_drv[0] */
2996 break;
2997 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002998 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07002999 break;
3000 default:
3001 /* not supported */
3002 break;
3003 }
3004
3005 if (id < 0 || id >= pos->num_dai) {
3006 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003007 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003008 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003009
3010 ret = 0;
3011
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003012 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003013 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003014 if (id == 0)
3015 break;
3016 id--;
3017 }
3018
3019 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003020 if (!*dai_name)
3021 *dai_name = pos->name;
Jerome Brunet1dfa5a52020-02-13 16:51:51 +01003022 } else if (ret) {
3023 /*
3024 * if another error than ENOTSUPP is returned go on and
3025 * check if another component is provided with the same
3026 * node. This may happen if a device provides several
3027 * components
3028 */
3029 continue;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003030 }
3031
Kuninori Morimotocb470082013-09-10 17:39:56 -07003032 break;
3033 }
3034 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003035 return ret;
3036}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003037EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003038
3039int snd_soc_of_get_dai_name(struct device_node *of_node,
3040 const char **dai_name)
3041{
3042 struct of_phandle_args args;
3043 int ret;
3044
3045 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3046 "#sound-dai-cells", 0, &args);
3047 if (ret)
3048 return ret;
3049
3050 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003051
3052 of_node_put(args.np);
3053
3054 return ret;
3055}
3056EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3057
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003058/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003059 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3060 * @dai_link: DAI link
3061 *
3062 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3063 */
3064void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3065{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003066 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003067 int index;
3068
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003069 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003070 if (!component->of_node)
3071 break;
3072 of_node_put(component->of_node);
3073 component->of_node = NULL;
3074 }
3075}
3076EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3077
3078/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003079 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3080 * @dev: Card device
3081 * @of_node: Device node
3082 * @dai_link: DAI link
3083 *
3084 * Builds an array of CODEC DAI components from the DAI link property
3085 * 'sound-dai'.
3086 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003087 * The device nodes in the array (of_node) must be dereferenced by calling
3088 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003089 *
3090 * Returns 0 for success
3091 */
3092int snd_soc_of_get_dai_link_codecs(struct device *dev,
3093 struct device_node *of_node,
3094 struct snd_soc_dai_link *dai_link)
3095{
3096 struct of_phandle_args args;
3097 struct snd_soc_dai_link_component *component;
3098 char *name;
3099 int index, num_codecs, ret;
3100
3101 /* Count the number of CODECs */
3102 name = "sound-dai";
3103 num_codecs = of_count_phandle_with_args(of_node, name,
3104 "#sound-dai-cells");
3105 if (num_codecs <= 0) {
3106 if (num_codecs == -ENOENT)
3107 dev_err(dev, "No 'sound-dai' property\n");
3108 else
3109 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3110 return num_codecs;
3111 }
Kees Cooka86854d2018-06-12 14:07:58 -07003112 component = devm_kcalloc(dev,
3113 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003114 GFP_KERNEL);
3115 if (!component)
3116 return -ENOMEM;
3117 dai_link->codecs = component;
3118 dai_link->num_codecs = num_codecs;
3119
3120 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003121 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003122 ret = of_parse_phandle_with_args(of_node, name,
3123 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003124 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003125 if (ret)
3126 goto err;
3127 component->of_node = args.np;
3128 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3129 if (ret < 0)
3130 goto err;
3131 }
3132 return 0;
3133err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003134 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003135 dai_link->codecs = NULL;
3136 dai_link->num_codecs = 0;
3137 return ret;
3138}
3139EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3140
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003141static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003143 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003144 snd_soc_util_init();
3145
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003146 return platform_driver_register(&soc_driver);
3147}
Mark Brown4abe8e12010-10-12 17:41:03 +01003148module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003149
Mark Brown7d8c16a2008-11-30 22:11:24 +00003150static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003151{
Mark Brownfb257892011-04-28 10:57:54 +01003152 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003153 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003154
Mark Brown3ff3f642008-05-19 12:32:25 +02003155 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003156}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003157module_exit(snd_soc_exit);
3158
3159/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003160MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003161MODULE_DESCRIPTION("ALSA SoC Core");
3162MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003163MODULE_ALIAS("platform:soc-audio");