blob: 13a7d515853485c86441f194f96770bdc42cc871 [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
Mark Brown28e9ad92011-03-02 18:36:34 +00001913 if (card->late_probe) {
1914 ret = card->late_probe(card);
1915 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001916 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001917 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001918 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00001919 }
1920 }
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001921 card->probed = 1;
Mark Brown28e9ad92011-03-02 18:36:34 +00001922
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001923 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925 ret = snd_card_register(card->snd_card);
1926 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001927 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1928 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001929 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001930 }
1931
Mark Brown435c5e22008-12-04 15:32:53 +00001932 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08001933 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01001934 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00001935
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001936 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001937 for_each_card_components(card, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001938 if (!snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001939 pinctrl_pm_select_sleep_state(component->dev);
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001940
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001941probe_end:
1942 if (ret < 0)
Kuninori Morimoto27f07ca2020-05-28 10:50:21 +09001943 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001945 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001946 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947
Mark Brownb19e6e72012-03-14 21:18:39 +00001948 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001949}
1950
1951/* probes a new socdev */
1952static int soc_probe(struct platform_device *pdev)
1953{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001954 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001955
Vinod Koul70a7ca32011-01-14 19:22:48 +05301956 /*
1957 * no card, so machine driver should be registering card
1958 * we should not be here in that case so ret error
1959 */
1960 if (!card)
1961 return -EINVAL;
1962
Mark Brownfe4085e2012-03-02 13:07:41 +00001963 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001964 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001965 card->name);
1966
Mark Brown435c5e22008-12-04 15:32:53 +00001967 /* Bodge while we unpick instantiation */
1968 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969
Mark Brown28d528c2012-08-09 18:45:23 +01001970 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971}
1972
1973/* removes a socdev */
1974static int soc_remove(struct platform_device *pdev)
1975{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001977
Mark Brownc5af3a22008-11-28 13:29:45 +00001978 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979 return 0;
1980}
1981
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001982int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001983{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001984 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001985 struct snd_soc_component *component;
Mark Brown51737472009-06-22 13:16:51 +01001986
1987 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001988 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001989
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001990 /*
1991 * Flush out pmdown_time work - we actually do want to run it
1992 * now, we're shutting down so no imminent restart.
1993 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09001994 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01001995
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001997
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001998 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001999 for_each_card_components(card, component)
2000 pinctrl_pm_select_sleep_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002001
Mark Brown416356f2009-06-30 19:05:15 +01002002 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002003}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002004EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002005
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002006const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302007 .suspend = snd_soc_suspend,
2008 .resume = snd_soc_resume,
2009 .freeze = snd_soc_suspend,
2010 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002011 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302012 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002013};
Stephen Warrendeb26072011-04-05 19:35:30 -06002014EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002015
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016/* ASoC platform driver */
2017static struct platform_driver soc_driver = {
2018 .driver = {
2019 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002020 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002021 },
2022 .probe = soc_probe,
2023 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002024};
2025
Mark Brown096e49d2009-07-05 15:12:22 +01002026/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002027 * snd_soc_cnew - create new control
2028 * @_template: control template
2029 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002030 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002031 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002032 *
2033 * Create a new mixer control from a template control.
2034 *
2035 * Returns 0 for success, else error.
2036 */
2037struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002038 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002039 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002040{
2041 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002042 struct snd_kcontrol *kcontrol;
2043 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002044
2045 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046 template.index = 0;
2047
Mark Brownefb7ac32011-03-08 17:23:24 +00002048 if (!long_name)
2049 long_name = template.name;
2050
2051 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002052 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002053 if (!name)
2054 return NULL;
2055
Mark Brownefb7ac32011-03-08 17:23:24 +00002056 template.name = name;
2057 } else {
2058 template.name = long_name;
2059 }
2060
2061 kcontrol = snd_ctl_new1(&template, data);
2062
2063 kfree(name);
2064
2065 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002066}
2067EXPORT_SYMBOL_GPL(snd_soc_cnew);
2068
Liam Girdwood022658b2012-02-03 17:43:09 +00002069static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2070 const struct snd_kcontrol_new *controls, int num_controls,
2071 const char *prefix, void *data)
2072{
2073 int err, i;
2074
2075 for (i = 0; i < num_controls; i++) {
2076 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002077
Liam Girdwood022658b2012-02-03 17:43:09 +00002078 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2079 control->name, prefix));
2080 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002081 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2082 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002083 return err;
2084 }
2085 }
2086
2087 return 0;
2088}
2089
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002091 * snd_soc_add_component_controls - Add an array of controls to a component.
2092 *
2093 * @component: Component to add controls to
2094 * @controls: Array of controls to add
2095 * @num_controls: Number of elements in the array
2096 *
2097 * Return: 0 for success, else error.
2098 */
2099int snd_soc_add_component_controls(struct snd_soc_component *component,
2100 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2101{
2102 struct snd_card *card = component->card->snd_card;
2103
2104 return snd_soc_add_controls(card, component->dev, controls,
2105 num_controls, component->name_prefix, component);
2106}
2107EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2108
2109/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002110 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2111 * Convenience function to add a list of controls.
2112 *
2113 * @soc_card: SoC card to add controls to
2114 * @controls: array of controls to add
2115 * @num_controls: number of elements in the array
2116 *
2117 * Return 0 for success, else error.
2118 */
2119int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2120 const struct snd_kcontrol_new *controls, int num_controls)
2121{
2122 struct snd_card *card = soc_card->snd_card;
2123
2124 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2125 NULL, soc_card);
2126}
2127EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2128
2129/**
2130 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2131 * Convienience function to add a list of controls.
2132 *
2133 * @dai: DAI to add controls to
2134 * @controls: array of controls to add
2135 * @num_controls: number of elements in the array
2136 *
2137 * Return 0 for success, else error.
2138 */
2139int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2140 const struct snd_kcontrol_new *controls, int num_controls)
2141{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002142 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002143
2144 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2145 NULL, dai);
2146}
2147EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2148
Mark Brownc5af3a22008-11-28 13:29:45 +00002149/**
2150 * snd_soc_register_card - Register a card with the ASoC core
2151 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002152 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002153 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002154 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302155int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002156{
2157 if (!card->name || !card->dev)
2158 return -EINVAL;
2159
Mark Browned77cc12011-05-03 18:25:34 +01002160 dev_set_drvdata(card->dev, card);
2161
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002162 INIT_LIST_HEAD(&card->widgets);
2163 INIT_LIST_HEAD(&card->paths);
2164 INIT_LIST_HEAD(&card->dapm_list);
2165 INIT_LIST_HEAD(&card->aux_comp_list);
2166 INIT_LIST_HEAD(&card->component_dev_list);
2167 INIT_LIST_HEAD(&card->list);
Mengdong Lin1a497982015-11-18 02:34:11 -05002168 INIT_LIST_HEAD(&card->rtd_list);
Mark Browndb432b42011-10-03 21:06:40 +01002169 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002170 INIT_LIST_HEAD(&card->dobj_list);
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002171
Mark Brownc5af3a22008-11-28 13:29:45 +00002172 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002173 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002174 mutex_init(&card->dapm_mutex);
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03002175 mutex_init(&card->pcm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002176 spin_lock_init(&card->dpcm_lock);
Mark Brownc5af3a22008-11-28 13:29:45 +00002177
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002178 return snd_soc_bind_card(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002179}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302180EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002181
2182/**
2183 * snd_soc_unregister_card - Unregister a card with the ASoC core
2184 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002185 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002186 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002187 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302188int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002189{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002190 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002191 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002192 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002193 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002194
2195 return 0;
2196}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302197EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199/*
2200 * Simplify DAI link configuration by removing ".-1" from device names
2201 * and sanitizing names.
2202 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002203static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002204{
2205 char *found, name[NAME_SIZE];
2206 int id1, id2;
2207
2208 if (dev_name(dev) == NULL)
2209 return NULL;
2210
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002211 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002212
2213 /* are we a "%s.%d" name (platform and SPI components) */
2214 found = strstr(name, dev->driver->name);
2215 if (found) {
2216 /* get ID */
2217 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2218
2219 /* discard ID from name if ID == -1 */
2220 if (*id == -1)
2221 found[strlen(dev->driver->name)] = '\0';
2222 }
2223
2224 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002225 /* I2C component devices are named "bus-addr" */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002226 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2227 char tmp[NAME_SIZE];
2228
2229 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002230 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002231
2232 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002233 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2234 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002235 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002236 } else
2237 *id = 0;
2238 }
2239
Kuninori Morimoto50014492019-10-02 14:22:57 +09002240 return devm_kstrdup(dev, name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002241}
2242
2243/*
2244 * Simplify DAI link naming for single devices with multiple DAIs by removing
2245 * any ".-1" and using the DAI name (instead of device name).
2246 */
2247static inline char *fmt_multiple_name(struct device *dev,
2248 struct snd_soc_dai_driver *dai_drv)
2249{
2250 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002251 dev_err(dev,
2252 "ASoC: error - multiple DAI %s registered with no name\n",
2253 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254 return NULL;
2255 }
2256
Kuninori Morimoto50014492019-10-02 14:22:57 +09002257 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258}
2259
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002260void snd_soc_unregister_dai(struct snd_soc_dai *dai)
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002261{
2262 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2263 list_del(&dai->list);
2264}
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002265EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002266
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002267/**
2268 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2269 *
2270 * @component: The component the DAIs are registered for
2271 * @dai_drv: DAI driver to use for the DAI
Randy Dunlapbc9a6652019-12-08 20:37:04 -08002272 * @legacy_dai_naming: if %true, use legacy single-name format;
2273 * if %false, use multiple-name format;
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002274 *
2275 * Topology can use this API to register DAIs when probing a component.
2276 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2277 * will be freed in the component cleanup.
2278 */
2279struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2280 struct snd_soc_dai_driver *dai_drv,
2281 bool legacy_dai_naming)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002282{
2283 struct device *dev = component->dev;
2284 struct snd_soc_dai *dai;
2285
2286 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2287
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002288 lockdep_assert_held(&client_mutex);
2289
Kuninori Morimoto50014492019-10-02 14:22:57 +09002290 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002291 if (dai == NULL)
2292 return NULL;
2293
2294 /*
2295 * Back in the old days when we still had component-less DAIs,
2296 * instead of having a static name, component-less DAIs would
2297 * inherit the name of the parent device so it is possible to
2298 * register multiple instances of the DAI. We still need to keep
2299 * the same naming style even though those DAIs are not
2300 * component-less anymore.
2301 */
2302 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002303 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002304 dai->name = fmt_single_name(dev, &dai->id);
2305 } else {
2306 dai->name = fmt_multiple_name(dev, dai_drv);
2307 if (dai_drv->id)
2308 dai->id = dai_drv->id;
2309 else
2310 dai->id = component->num_dai;
2311 }
Kuninori Morimoto50014492019-10-02 14:22:57 +09002312 if (!dai->name)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002313 return NULL;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002314
2315 dai->component = component;
2316 dai->dev = dev;
2317 dai->driver = dai_drv;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002318
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002319 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002320 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002321 component->num_dai++;
2322
2323 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2324 return dai;
2325}
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002326
Mark Brown91151712008-11-30 23:31:24 +00002327/**
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002328 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2329 *
2330 * @component: The component for which the DAIs should be unregistered
2331 */
2332static void snd_soc_unregister_dais(struct snd_soc_component *component)
2333{
2334 struct snd_soc_dai *dai, *_dai;
2335
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002336 for_each_component_dais_safe(component, dai, _dai)
2337 snd_soc_unregister_dai(dai);
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002338}
2339
2340/**
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002341 * snd_soc_register_dais - Register a DAI with the ASoC core
2342 *
2343 * @component: The component the DAIs are registered for
2344 * @dai_drv: DAI driver to use for the DAIs
2345 * @count: Number of DAIs
2346 */
2347static int snd_soc_register_dais(struct snd_soc_component *component,
2348 struct snd_soc_dai_driver *dai_drv,
2349 size_t count)
2350{
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002351 struct snd_soc_dai *dai;
2352 unsigned int i;
2353 int ret;
2354
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002355 for (i = 0; i < count; i++) {
Kuninori Morimoto71cb85f2019-11-05 15:47:18 +09002356 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002357 !component->driver->non_legacy_dai_naming);
2358 if (dai == NULL) {
2359 ret = -ENOMEM;
2360 goto err;
2361 }
2362 }
2363
2364 return 0;
2365
2366err:
2367 snd_soc_unregister_dais(component);
2368
2369 return ret;
2370}
2371
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002372static int snd_soc_component_initialize(struct snd_soc_component *component,
2373 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002374{
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002375 INIT_LIST_HEAD(&component->dai_list);
Kuninori Morimoto495efdb2019-08-20 14:05:20 +09002376 INIT_LIST_HEAD(&component->dobj_list);
2377 INIT_LIST_HEAD(&component->card_list);
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002378 mutex_init(&component->io_mutex);
2379
Jerome Brunet6b62fa92020-02-19 11:25:26 +01002380 component->name = fmt_single_name(dev, &component->id);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002381 if (!component->name) {
2382 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002383 return -ENOMEM;
2384 }
2385
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002386 component->dev = dev;
2387 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002388
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002389 return 0;
2390}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002391
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002392static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002393{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002394 int val_bytes = regmap_get_val_bytes(component->regmap);
2395
2396 /* Errors are legitimate for non-integer byte multiples */
2397 if (val_bytes > 0)
2398 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002399}
2400
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002401#ifdef CONFIG_REGMAP
2402
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002403/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002404 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2405 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002406 * @component: The component for which to initialize the regmap instance
2407 * @regmap: The regmap instance that should be used by the component
2408 *
2409 * This function allows deferred assignment of the regmap instance that is
2410 * associated with the component. Only use this if the regmap instance is not
2411 * yet ready when the component is registered. The function must also be called
2412 * before the first IO attempt of the component.
2413 */
2414void snd_soc_component_init_regmap(struct snd_soc_component *component,
2415 struct regmap *regmap)
2416{
2417 component->regmap = regmap;
2418 snd_soc_component_setup_regmap(component);
2419}
2420EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2421
2422/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002423 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2424 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002425 * @component: The component for which to de-initialize the regmap instance
2426 *
2427 * Calls regmap_exit() on the regmap instance associated to the component and
2428 * removes the regmap instance from the component.
2429 *
2430 * This function should only be used if snd_soc_component_init_regmap() was used
2431 * to initialize the regmap instance.
2432 */
2433void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2434{
2435 regmap_exit(component->regmap);
2436 component->regmap = NULL;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2439
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002440#endif
2441
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002442#define ENDIANNESS_MAP(name) \
2443 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2444static u64 endianness_format_map[] = {
2445 ENDIANNESS_MAP(S16_),
2446 ENDIANNESS_MAP(U16_),
2447 ENDIANNESS_MAP(S24_),
2448 ENDIANNESS_MAP(U24_),
2449 ENDIANNESS_MAP(S32_),
2450 ENDIANNESS_MAP(U32_),
2451 ENDIANNESS_MAP(S24_3),
2452 ENDIANNESS_MAP(U24_3),
2453 ENDIANNESS_MAP(S20_3),
2454 ENDIANNESS_MAP(U20_3),
2455 ENDIANNESS_MAP(S18_3),
2456 ENDIANNESS_MAP(U18_3),
2457 ENDIANNESS_MAP(FLOAT_),
2458 ENDIANNESS_MAP(FLOAT64_),
2459 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2460};
2461
2462/*
2463 * Fix up the DAI formats for endianness: codecs don't actually see
2464 * the endianness of the data but we're using the CPU format
2465 * definitions which do need to include endianness so we ensure that
2466 * codec DAIs always have both big and little endian variants set.
2467 */
2468static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2469{
2470 int i;
2471
2472 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2473 if (stream->formats & endianness_format_map[i])
2474 stream->formats |= endianness_format_map[i];
2475}
2476
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002477static void snd_soc_try_rebind_card(void)
2478{
2479 struct snd_soc_card *card, *c;
2480
Kuninori Morimotob245d272019-08-07 10:31:19 +09002481 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2482 if (!snd_soc_bind_card(card))
2483 list_del(&card->list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002484}
2485
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002486static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2487{
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002488 struct snd_soc_card *card = component->card;
2489
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002490 snd_soc_unregister_dais(component);
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002491
2492 if (card)
2493 snd_soc_unbind_card(card, false);
2494
2495 list_del(&component->list);
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002496}
2497
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002498int snd_soc_add_component(struct device *dev,
2499 struct snd_soc_component *component,
2500 const struct snd_soc_component_driver *component_driver,
2501 struct snd_soc_dai_driver *dai_drv,
2502 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002503{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002504 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002505 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002506
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002507 mutex_lock(&client_mutex);
2508
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002509 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002510 if (ret)
2511 goto err_free;
2512
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002513 if (component_driver->endianness) {
2514 for (i = 0; i < num_dai; i++) {
2515 convert_endianness_formats(&dai_drv[i].playback);
2516 convert_endianness_formats(&dai_drv[i].capture);
2517 }
2518 }
2519
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002520 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002521 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002522 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002523 goto err_cleanup;
2524 }
2525
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002526 if (!component->driver->write && !component->driver->read) {
2527 if (!component->regmap)
2528 component->regmap = dev_get_regmap(component->dev,
2529 NULL);
2530 if (component->regmap)
2531 snd_soc_component_setup_regmap(component);
2532 }
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002533
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002534 /* see for_each_component */
2535 list_add(&component->list, &component_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002536
2537err_cleanup:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002538 if (ret < 0)
2539 snd_soc_del_component_unlocked(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002540err_free:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002541 mutex_unlock(&client_mutex);
2542
2543 if (ret == 0)
2544 snd_soc_try_rebind_card();
2545
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002546 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002547}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002548EXPORT_SYMBOL_GPL(snd_soc_add_component);
2549
2550int snd_soc_register_component(struct device *dev,
2551 const struct snd_soc_component_driver *component_driver,
2552 struct snd_soc_dai_driver *dai_drv,
2553 int num_dai)
2554{
2555 struct snd_soc_component *component;
2556
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002557 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002558 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002559 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002560
2561 return snd_soc_add_component(dev, component, component_driver,
2562 dai_drv, num_dai);
2563}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002564EXPORT_SYMBOL_GPL(snd_soc_register_component);
2565
2566/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002567 * snd_soc_unregister_component - Unregister all related component
2568 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002569 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002570 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002571 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002572void snd_soc_unregister_component(struct device *dev)
2573{
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002574 struct snd_soc_component *component;
2575
2576 mutex_lock(&client_mutex);
2577 while (1) {
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +09002578 component = snd_soc_lookup_component_nolocked(dev, NULL);
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002579 if (!component)
2580 break;
2581
2582 snd_soc_del_component_unlocked(component);
2583 }
2584 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002585}
2586EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2587
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002588/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002589int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2590 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002591{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002592 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002593 int ret;
2594
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302595 if (!card->dev) {
2596 pr_err("card->dev is not set before calling %s\n", __func__);
2597 return -EINVAL;
2598 }
2599
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002600 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302601
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002602 ret = of_property_read_string_index(np, propname, 0, &card->name);
2603 /*
2604 * EINVAL means the property does not exist. This is fine providing
2605 * card->name was previously set, which is checked later in
2606 * snd_soc_register_card.
2607 */
2608 if (ret < 0 && ret != -EINVAL) {
2609 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002610 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002611 propname, ret);
2612 return ret;
2613 }
2614
2615 return 0;
2616}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002617EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002618
Xiubo Li9a6d4862014-02-08 15:59:52 +08002619static const struct snd_soc_dapm_widget simple_widgets[] = {
2620 SND_SOC_DAPM_MIC("Microphone", NULL),
2621 SND_SOC_DAPM_LINE("Line", NULL),
2622 SND_SOC_DAPM_HP("Headphone", NULL),
2623 SND_SOC_DAPM_SPK("Speaker", NULL),
2624};
2625
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002626int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002627 const char *propname)
2628{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002629 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002630 struct snd_soc_dapm_widget *widgets;
2631 const char *template, *wname;
2632 int i, j, num_widgets, ret;
2633
2634 num_widgets = of_property_count_strings(np, propname);
2635 if (num_widgets < 0) {
2636 dev_err(card->dev,
2637 "ASoC: Property '%s' does not exist\n", propname);
2638 return -EINVAL;
2639 }
2640 if (num_widgets & 1) {
2641 dev_err(card->dev,
2642 "ASoC: Property '%s' length is not even\n", propname);
2643 return -EINVAL;
2644 }
2645
2646 num_widgets /= 2;
2647 if (!num_widgets) {
2648 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2649 propname);
2650 return -EINVAL;
2651 }
2652
2653 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2654 GFP_KERNEL);
2655 if (!widgets) {
2656 dev_err(card->dev,
2657 "ASoC: Could not allocate memory for widgets\n");
2658 return -ENOMEM;
2659 }
2660
2661 for (i = 0; i < num_widgets; i++) {
2662 ret = of_property_read_string_index(np, propname,
2663 2 * i, &template);
2664 if (ret) {
2665 dev_err(card->dev,
2666 "ASoC: Property '%s' index %d read error:%d\n",
2667 propname, 2 * i, ret);
2668 return -EINVAL;
2669 }
2670
2671 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2672 if (!strncmp(template, simple_widgets[j].name,
2673 strlen(simple_widgets[j].name))) {
2674 widgets[i] = simple_widgets[j];
2675 break;
2676 }
2677 }
2678
2679 if (j >= ARRAY_SIZE(simple_widgets)) {
2680 dev_err(card->dev,
2681 "ASoC: DAPM widget '%s' is not supported\n",
2682 template);
2683 return -EINVAL;
2684 }
2685
2686 ret = of_property_read_string_index(np, propname,
2687 (2 * i) + 1,
2688 &wname);
2689 if (ret) {
2690 dev_err(card->dev,
2691 "ASoC: Property '%s' index %d read error:%d\n",
2692 propname, (2 * i) + 1, ret);
2693 return -EINVAL;
2694 }
2695
2696 widgets[i].name = wname;
2697 }
2698
Nicolin Chenf23e8602015-02-14 17:22:49 -08002699 card->of_dapm_widgets = widgets;
2700 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002701
2702 return 0;
2703}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002704EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08002705
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002706int snd_soc_of_get_slot_mask(struct device_node *np,
2707 const char *prop_name,
2708 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03002709{
2710 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03002711 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03002712 int i;
2713
2714 if (!of_slot_mask)
2715 return 0;
2716 val /= sizeof(u32);
2717 for (i = 0; i < val; i++)
2718 if (be32_to_cpup(&of_slot_mask[i]))
2719 *mask |= (1 << i);
2720
2721 return val;
2722}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002723EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03002724
Xiubo Li89c67852014-02-14 09:34:35 +08002725int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03002726 unsigned int *tx_mask,
2727 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08002728 unsigned int *slots,
2729 unsigned int *slot_width)
2730{
2731 u32 val;
2732 int ret;
2733
Jyri Sarha61310842015-09-09 21:27:43 +03002734 if (tx_mask)
2735 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2736 if (rx_mask)
2737 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2738
Xiubo Li89c67852014-02-14 09:34:35 +08002739 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2740 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2741 if (ret)
2742 return ret;
2743
2744 if (slots)
2745 *slots = val;
2746 }
2747
2748 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2749 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2750 if (ret)
2751 return ret;
2752
2753 if (slot_width)
2754 *slot_width = val;
2755 }
2756
2757 return 0;
2758}
2759EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2760
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002761void snd_soc_of_parse_node_prefix(struct device_node *np,
2762 struct snd_soc_codec_conf *codec_conf,
2763 struct device_node *of_node,
2764 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002765{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002766 const char *str;
2767 int ret;
2768
2769 ret = of_property_read_string(np, propname, &str);
2770 if (ret < 0) {
2771 /* no prefix is not error */
2772 return;
2773 }
2774
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09002775 codec_conf->dlc.of_node = of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002776 codec_conf->name_prefix = str;
2777}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002778EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002779
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002780int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002781 const char *propname)
2782{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002783 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00002784 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002785 struct snd_soc_dapm_route *routes;
2786 int i, ret;
2787
2788 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08002789 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002790 dev_err(card->dev,
2791 "ASoC: Property '%s' does not exist or its length is not even\n",
2792 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002793 return -EINVAL;
2794 }
2795 num_routes /= 2;
2796 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002797 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002798 propname);
2799 return -EINVAL;
2800 }
2801
Kees Cooka86854d2018-06-12 14:07:58 -07002802 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002803 GFP_KERNEL);
2804 if (!routes) {
2805 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002806 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002807 return -EINVAL;
2808 }
2809
2810 for (i = 0; i < num_routes; i++) {
2811 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002812 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002813 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09002814 dev_err(card->dev,
2815 "ASoC: Property '%s' index %d could not be read: %d\n",
2816 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002817 return -EINVAL;
2818 }
2819 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002820 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002821 if (ret) {
2822 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09002823 "ASoC: Property '%s' index %d could not be read: %d\n",
2824 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002825 return -EINVAL;
2826 }
2827 }
2828
Nicolin Chenf23e8602015-02-14 17:22:49 -08002829 card->num_of_dapm_routes = num_routes;
2830 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002831
2832 return 0;
2833}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002834EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002835
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002836unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02002837 const char *prefix,
2838 struct device_node **bitclkmaster,
2839 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002840{
2841 int ret, i;
2842 char prop[128];
2843 unsigned int format = 0;
2844 int bit, frame;
2845 const char *str;
2846 struct {
2847 char *name;
2848 unsigned int val;
2849 } of_fmt_table[] = {
2850 { "i2s", SND_SOC_DAIFMT_I2S },
2851 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2852 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2853 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2854 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2855 { "ac97", SND_SOC_DAIFMT_AC97 },
2856 { "pdm", SND_SOC_DAIFMT_PDM},
2857 { "msb", SND_SOC_DAIFMT_MSB },
2858 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002859 };
2860
2861 if (!prefix)
2862 prefix = "";
2863
2864 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002865 * check "dai-format = xxx"
2866 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002867 * SND_SOC_DAIFMT_FORMAT_MASK area
2868 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002869 ret = of_property_read_string(np, "dai-format", &str);
2870 if (ret < 0) {
2871 snprintf(prop, sizeof(prop), "%sformat", prefix);
2872 ret = of_property_read_string(np, prop, &str);
2873 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002874 if (ret == 0) {
2875 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2876 if (strcmp(str, of_fmt_table[i].name) == 0) {
2877 format |= of_fmt_table[i].val;
2878 break;
2879 }
2880 }
2881 }
2882
2883 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002884 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002885 * SND_SOC_DAIFMT_CLOCK_MASK area
2886 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002887 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02002888 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002889 format |= SND_SOC_DAIFMT_CONT;
2890 else
2891 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002892
2893 /*
2894 * check "[prefix]bitclock-inversion"
2895 * check "[prefix]frame-inversion"
2896 * SND_SOC_DAIFMT_INV_MASK area
2897 */
2898 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2899 bit = !!of_get_property(np, prop, NULL);
2900
2901 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2902 frame = !!of_get_property(np, prop, NULL);
2903
2904 switch ((bit << 4) + frame) {
2905 case 0x11:
2906 format |= SND_SOC_DAIFMT_IB_IF;
2907 break;
2908 case 0x10:
2909 format |= SND_SOC_DAIFMT_IB_NF;
2910 break;
2911 case 0x01:
2912 format |= SND_SOC_DAIFMT_NB_IF;
2913 break;
2914 default:
2915 /* SND_SOC_DAIFMT_NB_NF is default */
2916 break;
2917 }
2918
2919 /*
2920 * check "[prefix]bitclock-master"
2921 * check "[prefix]frame-master"
2922 * SND_SOC_DAIFMT_MASTER_MASK area
2923 */
2924 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
2925 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002926 if (bit && bitclkmaster)
2927 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002928
2929 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
2930 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02002931 if (frame && framemaster)
2932 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002933
2934 switch ((bit << 4) + frame) {
2935 case 0x11:
2936 format |= SND_SOC_DAIFMT_CBM_CFM;
2937 break;
2938 case 0x10:
2939 format |= SND_SOC_DAIFMT_CBM_CFS;
2940 break;
2941 case 0x01:
2942 format |= SND_SOC_DAIFMT_CBS_CFM;
2943 break;
2944 default:
2945 format |= SND_SOC_DAIFMT_CBS_CFS;
2946 break;
2947 }
2948
2949 return format;
2950}
2951EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
2952
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002953int snd_soc_get_dai_id(struct device_node *ep)
2954{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09002955 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002956 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002957 int ret;
2958
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002959 dlc.of_node = of_graph_get_port_parent(ep);
2960 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002961 /*
2962 * For example HDMI case, HDMI has video/sound port,
2963 * but ALSA SoC needs sound port number only.
2964 * Thus counting HDMI DT port/endpoint doesn't work.
2965 * Then, it should have .of_xlate_dai_id
2966 */
2967 ret = -ENOTSUPP;
2968 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002969 component = soc_find_component(&dlc);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +09002970 if (component)
2971 ret = snd_soc_component_of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002972 mutex_unlock(&client_mutex);
2973
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09002974 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07002975
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00002976 return ret;
2977}
2978EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
2979
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00002980int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002981 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07002982{
2983 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03002984 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002985 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07002986
2987 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002988 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09002989 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03002990
2991 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07002992 continue;
2993
Kuninori Morimotoa2a34172019-07-26 13:51:31 +09002994 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
2995 if (ret == -ENOTSUPP) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002996 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07002997 int id = -1;
2998
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01002999 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003000 case 0:
3001 id = 0; /* same as dai_drv[0] */
3002 break;
3003 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003004 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003005 break;
3006 default:
3007 /* not supported */
3008 break;
3009 }
3010
3011 if (id < 0 || id >= pos->num_dai) {
3012 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003013 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003014 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003015
3016 ret = 0;
3017
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003018 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003019 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003020 if (id == 0)
3021 break;
3022 id--;
3023 }
3024
3025 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003026 if (!*dai_name)
3027 *dai_name = pos->name;
Jerome Brunet1dfa5a52020-02-13 16:51:51 +01003028 } else if (ret) {
3029 /*
3030 * if another error than ENOTSUPP is returned go on and
3031 * check if another component is provided with the same
3032 * node. This may happen if a device provides several
3033 * components
3034 */
3035 continue;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003036 }
3037
Kuninori Morimotocb470082013-09-10 17:39:56 -07003038 break;
3039 }
3040 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003041 return ret;
3042}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003043EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003044
3045int snd_soc_of_get_dai_name(struct device_node *of_node,
3046 const char **dai_name)
3047{
3048 struct of_phandle_args args;
3049 int ret;
3050
3051 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3052 "#sound-dai-cells", 0, &args);
3053 if (ret)
3054 return ret;
3055
3056 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003057
3058 of_node_put(args.np);
3059
3060 return ret;
3061}
3062EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3063
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003064/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003065 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3066 * @dai_link: DAI link
3067 *
3068 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3069 */
3070void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3071{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003072 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003073 int index;
3074
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003075 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003076 if (!component->of_node)
3077 break;
3078 of_node_put(component->of_node);
3079 component->of_node = NULL;
3080 }
3081}
3082EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3083
3084/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003085 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3086 * @dev: Card device
3087 * @of_node: Device node
3088 * @dai_link: DAI link
3089 *
3090 * Builds an array of CODEC DAI components from the DAI link property
3091 * 'sound-dai'.
3092 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003093 * The device nodes in the array (of_node) must be dereferenced by calling
3094 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003095 *
3096 * Returns 0 for success
3097 */
3098int snd_soc_of_get_dai_link_codecs(struct device *dev,
3099 struct device_node *of_node,
3100 struct snd_soc_dai_link *dai_link)
3101{
3102 struct of_phandle_args args;
3103 struct snd_soc_dai_link_component *component;
3104 char *name;
3105 int index, num_codecs, ret;
3106
3107 /* Count the number of CODECs */
3108 name = "sound-dai";
3109 num_codecs = of_count_phandle_with_args(of_node, name,
3110 "#sound-dai-cells");
3111 if (num_codecs <= 0) {
3112 if (num_codecs == -ENOENT)
3113 dev_err(dev, "No 'sound-dai' property\n");
3114 else
3115 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3116 return num_codecs;
3117 }
Kees Cooka86854d2018-06-12 14:07:58 -07003118 component = devm_kcalloc(dev,
3119 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003120 GFP_KERNEL);
3121 if (!component)
3122 return -ENOMEM;
3123 dai_link->codecs = component;
3124 dai_link->num_codecs = num_codecs;
3125
3126 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003127 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003128 ret = of_parse_phandle_with_args(of_node, name,
3129 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003130 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003131 if (ret)
3132 goto err;
3133 component->of_node = args.np;
3134 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3135 if (ret < 0)
3136 goto err;
3137 }
3138 return 0;
3139err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003140 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003141 dai_link->codecs = NULL;
3142 dai_link->num_codecs = 0;
3143 return ret;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3146
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003147static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003148{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003149 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003150 snd_soc_util_init();
3151
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003152 return platform_driver_register(&soc_driver);
3153}
Mark Brown4abe8e12010-10-12 17:41:03 +01003154module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003155
Mark Brown7d8c16a2008-11-30 22:11:24 +00003156static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003157{
Mark Brownfb257892011-04-28 10:57:54 +01003158 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003159 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003160
Mark Brown3ff3f642008-05-19 12:32:25 +02003161 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003162}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003163module_exit(snd_soc_exit);
3164
3165/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003166MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003167MODULE_DESCRIPTION("ALSA SoC Core");
3168MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003169MODULE_ALIAS("platform:soc-audio");