blob: 068d809c349a2fc05da5db90802207232b95681f [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brownc5af3a22008-11-28 13:29:45 +000048static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070049static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010050static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000051
Kuninori Morimoto368dee92018-09-21 05:23:01 +000052#define for_each_component(component) \
53 list_for_each_entry(component, &component_list, list)
54
Frank Mandarinodb2a4162006-10-06 18:31:09 +020055/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090056 * This is used if driver don't need to have CPU/Codec/Platform
57 * dai_link. see soc.h
58 */
59struct snd_soc_dai_link_component null_dailink_component[0];
60EXPORT_SYMBOL_GPL(null_dailink_component);
61
62/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Mark Browndbe21402010-02-12 11:37:24 +000071static ssize_t pmdown_time_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
73{
Mark Brown36ae1a92012-01-06 17:12:45 -080074 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000076 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000077}
78
79static ssize_t pmdown_time_set(struct device *dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
82{
Mark Brown36ae1a92012-01-06 17:12:45 -080083 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070084 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000085
Jingoo Hanb785a492013-07-19 16:24:59 +090086 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -070087 if (ret)
88 return ret;
Mark Browndbe21402010-02-12 11:37:24 +000089
90 return count;
91}
92
93static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
94
Takashi Iwaid29697d2015-01-30 20:16:37 +010095static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +010096 &dev_attr_pmdown_time.attr,
97 NULL
98};
99
100static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
101 struct attribute *attr, int idx)
102{
103 struct device *dev = kobj_to_dev(kobj);
104 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
105
Kuninori Morimotod918a372019-09-12 13:42:30 +0900106 if (!rtd)
107 return 0;
108
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 if (attr == &dev_attr_pmdown_time.attr)
110 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000111 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100112}
113
114static const struct attribute_group soc_dapm_dev_group = {
115 .attrs = soc_dapm_dev_attrs,
116 .is_visible = soc_dev_attr_is_visible,
117};
118
Mark Brownf7e73b262018-03-09 12:46:27 +0000119static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100120 .attrs = soc_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
122};
123
124static const struct attribute_group *soc_dev_attr_groups[] = {
125 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000126 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100127 NULL
128};
129
Mark Brown2624d5f2009-11-03 21:56:13 +0000130#ifdef CONFIG_DEBUG_FS
Kuninori Morimotoa4072cd2019-12-11 13:32:20 +0900131struct dentry *snd_soc_debugfs_root;
132EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
133
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200134static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100135{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200136 if (!component->card->debugfs_card_root)
137 return;
138
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200139 if (component->debugfs_prefix) {
140 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100141
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200142 name = kasprintf(GFP_KERNEL, "%s:%s",
143 component->debugfs_prefix, component->name);
144 if (name) {
145 component->debugfs_root = debugfs_create_dir(name,
146 component->card->debugfs_card_root);
147 kfree(name);
148 }
149 } else {
150 component->debugfs_root = debugfs_create_dir(component->name,
151 component->card->debugfs_card_root);
152 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100153
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200154 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
155 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156}
157
158static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
159{
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900160 if (!component->debugfs_root)
161 return;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200162 debugfs_remove_recursive(component->debugfs_root);
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900163 component->debugfs_root = NULL;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200164}
165
Peng Donglinc15b2a12018-02-14 22:48:07 +0800166static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100167{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100168 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100169 struct snd_soc_dai *dai;
170
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100171 mutex_lock(&client_mutex);
172
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000173 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000174 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800175 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100176
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100177 mutex_unlock(&client_mutex);
178
Donglin Peng700c17c2018-01-18 13:31:26 +0800179 return 0;
180}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800181DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100182
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000183static int component_list_show(struct seq_file *m, void *v)
184{
185 struct snd_soc_component *component;
186
187 mutex_lock(&client_mutex);
188
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000189 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000190 seq_printf(m, "%s\n", component->name);
191
192 mutex_unlock(&client_mutex);
193
194 return 0;
195}
196DEFINE_SHOW_ATTRIBUTE(component_list);
197
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200198static void soc_init_card_debugfs(struct snd_soc_card *card)
199{
200 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000201 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200202
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200203 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
204 &card->pop_time);
Kuninori Morimotod8ca7a02019-08-07 10:31:14 +0900205
206 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200207}
208
209static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
210{
211 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900212 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213}
214
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200215static void snd_soc_debugfs_init(void)
216{
217 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200218
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200219 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
220 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000221
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200222 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
223 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200224}
225
226static void snd_soc_debugfs_exit(void)
227{
228 debugfs_remove_recursive(snd_soc_debugfs_root);
229}
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231#else
232
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200233static inline void soc_init_component_debugfs(
234 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000235{
236}
237
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200238static inline void soc_cleanup_component_debugfs(
239 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000240{
241}
242
Axel Linb95fccb2010-11-09 17:06:44 +0800243static inline void soc_init_card_debugfs(struct snd_soc_card *card)
244{
245}
246
247static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
248{
249}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250
251static inline void snd_soc_debugfs_init(void)
252{
253}
254
255static inline void snd_soc_debugfs_exit(void)
256{
257}
258
Mark Brown2624d5f2009-11-03 21:56:13 +0000259#endif
260
Kuninori Morimoto12b05232020-01-10 11:35:54 +0900261static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
262 struct snd_soc_component *component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000263{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900264 struct snd_soc_component *comp;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900265 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000266
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900267 for_each_rtd_components(rtd, i, comp) {
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000268 /* already connected */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900269 if (comp == component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000270 return 0;
271 }
272
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900273 /* see for_each_rtd_components */
274 rtd->components[rtd->num_components] = component;
275 rtd->num_components++;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000276
277 return 0;
278}
279
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000280struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
281 const char *driver_name)
282{
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900283 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900284 int i;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000285
Kuninori Morimoto971da242018-01-23 00:41:24 +0000286 if (!driver_name)
287 return NULL;
288
Kuninori Morimotoa33c0d12019-08-20 14:05:02 +0900289 /*
290 * NOTE
291 *
292 * snd_soc_rtdcom_lookup() will find component from rtd by using
293 * specified driver name.
294 * But, if many components which have same driver name are connected
295 * to 1 rtd, this function will return 1st found component.
296 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900297 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900298 const char *component_name = component->driver->name;
Kuninori Morimoto971da242018-01-23 00:41:24 +0000299
300 if (!component_name)
301 continue;
302
303 if ((component_name == driver_name) ||
304 strcmp(component_name, driver_name) == 0)
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900305 return component;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000306 }
307
308 return NULL;
309}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000310EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000311
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900312static struct snd_soc_component
313*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
Kuninori Morimotob8132652019-11-05 15:46:30 +0900314{
315 struct snd_soc_component *component;
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900316 struct snd_soc_component *found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900317
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900318 found_component = NULL;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900319 for_each_component(component) {
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900320 if ((dev == component->dev) &&
321 (!driver_name ||
322 (driver_name == component->driver->name) ||
323 (strcmp(component->driver->name, driver_name) == 0))) {
324 found_component = component;
325 break;
326 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900327 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900328
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900329 return found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900330}
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900331
332struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
333 const char *driver_name)
334{
335 struct snd_soc_component *component;
336
337 mutex_lock(&client_mutex);
338 component = snd_soc_lookup_component_nolocked(dev, driver_name);
339 mutex_unlock(&client_mutex);
340
341 return component;
342}
Kuninori Morimotob8132652019-11-05 15:46:30 +0900343EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
344
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900345struct snd_soc_pcm_runtime
346*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
Kuninori Morimoto44681892019-12-10 09:34:08 +0900347 struct snd_soc_dai_link *dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900348{
349 struct snd_soc_pcm_runtime *rtd;
350
351 for_each_card_rtds(card, rtd) {
Kuninori Morimoto44681892019-12-10 09:34:08 +0900352 if (rtd->dai_link == dai_link)
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900353 return rtd;
354 }
Kuninori Morimoto44681892019-12-10 09:34:08 +0900355 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
Kuninori Morimoto94def8e2019-12-10 09:34:01 +0900356 return NULL;
357}
358EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
359
Kuninori Morimoto83f94a22020-01-10 11:36:17 +0900360/*
361 * Power down the audio subsystem pmdown_time msecs after close is called.
362 * This is to ensure there are no pops or clicks in between any music tracks
363 * due to DAPM power cycling.
364 */
365void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
366{
367 struct snd_soc_dai *codec_dai = rtd->codec_dai;
368
369 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
370
371 dev_dbg(rtd->dev,
372 "ASoC: pop wq checking: %s status: %s waiting: %s\n",
373 codec_dai->driver->playback.stream_name,
374 codec_dai->playback_active ? "active" : "inactive",
375 rtd->pop_wait ? "yes" : "no");
376
377 /* are we waiting on this codec DAI stream */
378 if (rtd->pop_wait == 1) {
379 rtd->pop_wait = 0;
380 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
381 SND_SOC_DAPM_STREAM_STOP);
382 }
383
384 mutex_unlock(&rtd->card->pcm_mutex);
385}
386EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
387
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900388static void soc_release_rtd_dev(struct device *dev)
389{
390 /* "dev" means "rtd->dev" */
391 kfree(dev);
392}
393
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900394static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
395{
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900396 if (!rtd)
397 return;
398
Kuninori Morimoto753ace02019-09-12 13:38:50 +0900399 list_del(&rtd->list);
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900400
Curtis Malainey9c9b6522019-11-27 17:13:58 -0800401 if (delayed_work_pending(&rtd->delayed_work))
402 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +0900403 snd_soc_pcm_component_free(rtd);
404
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900405 /*
406 * we don't need to call kfree() for rtd->dev
407 * see
408 * soc_release_rtd_dev()
Kuninori Morimotod918a372019-09-12 13:42:30 +0900409 *
410 * We don't need rtd->dev NULL check, because
411 * it is alloced *before* rtd.
412 * see
413 * soc_new_pcm_runtime()
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900414 */
Kuninori Morimotod918a372019-09-12 13:42:30 +0900415 device_unregister(rtd->dev);
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900416}
417
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800418static void close_delayed_work(struct work_struct *work) {
419 struct snd_soc_pcm_runtime *rtd =
420 container_of(work, struct snd_soc_pcm_runtime,
421 delayed_work.work);
422
423 if (rtd->close_delayed_work_func)
424 rtd->close_delayed_work_func(rtd);
425}
426
Mengdong Lin1a497982015-11-18 02:34:11 -0500427static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
428 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
429{
430 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900431 struct snd_soc_component *component;
Kuninori Morimotod918a372019-09-12 13:42:30 +0900432 struct device *dev;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900433 int ret;
Mengdong Lin1a497982015-11-18 02:34:11 -0500434
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900435 /*
Kuninori Morimotod918a372019-09-12 13:42:30 +0900436 * for rtd->dev
437 */
438 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
439 if (!dev)
440 return NULL;
441
442 dev->parent = card->dev;
443 dev->release = soc_release_rtd_dev;
444 dev->groups = soc_dev_attr_groups;
445
446 dev_set_name(dev, "%s", dai_link->name);
447
448 ret = device_register(dev);
449 if (ret < 0) {
450 put_device(dev); /* soc_release_rtd_dev */
451 return NULL;
452 }
453
454 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900455 * for rtd
456 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900457 rtd = devm_kzalloc(dev,
458 sizeof(*rtd) +
459 sizeof(*component) * (dai_link->num_cpus +
460 dai_link->num_codecs +
461 dai_link->num_platforms),
462 GFP_KERNEL);
Mengdong Lin1a497982015-11-18 02:34:11 -0500463 if (!rtd)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900464 goto free_rtd;
Mengdong Lin1a497982015-11-18 02:34:11 -0500465
Kuninori Morimotod918a372019-09-12 13:42:30 +0900466 rtd->dev = dev;
Takashi Iwai07d22a92019-12-04 16:14:54 +0100467 INIT_LIST_HEAD(&rtd->list);
Takashi Iwai07d22a92019-12-04 16:14:54 +0100468 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
469 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
470 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
471 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Kuninori Morimotod918a372019-09-12 13:42:30 +0900472 dev_set_drvdata(dev, rtd);
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800473 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
Kuninori Morimotod918a372019-09-12 13:42:30 +0900474
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900475 /*
476 * for rtd->codec_dais
477 */
Kuninori Morimoto4dc0e7d2019-10-02 14:22:32 +0900478 rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
Kees Cook6396bb22018-06-12 14:03:40 -0700479 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500480 GFP_KERNEL);
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900481 if (!rtd->codec_dais)
482 goto free_rtd;
483
484 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900485 * rtd remaining settings
486 */
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900487 rtd->card = card;
488 rtd->dai_link = dai_link;
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900489
Kuninori Morimoto6634e3d2019-08-07 10:30:31 +0900490 /* see for_each_card_rtds */
Mengdong Lin1a497982015-11-18 02:34:11 -0500491 list_add_tail(&rtd->list, &card->rtd_list);
492 rtd->num = card->num_rtd;
493 card->num_rtd++;
Kuninori Morimotoa8481252019-09-12 13:38:34 +0900494
495 return rtd;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900496
497free_rtd:
498 soc_free_pcm_runtime(rtd);
499 return NULL;
Mengdong Lin1a497982015-11-18 02:34:11 -0500500}
501
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900502static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
503{
504 struct snd_soc_pcm_runtime *rtd;
505
506 for_each_card_rtds(card, rtd)
507 flush_delayed_work(&rtd->delayed_work);
508}
509
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000510#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200511/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000512int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000514 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000515 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500516 struct snd_soc_pcm_runtime *rtd;
517 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200519 /* If the card is not initialized yet there is nothing to do */
520 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200521 return 0;
522
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200523 /*
524 * Due to the resume being scheduled into a workqueue we could
525 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100526 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100528
529 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100531
Mark Browna00f90f2010-12-02 16:24:24 +0000532 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000533 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000534 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100535
Mengdong Lin1a497982015-11-18 02:34:11 -0500536 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100537 continue;
538
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000539 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900540 if (dai->playback_active)
541 snd_soc_dai_digital_mute(dai, 1,
542 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200543 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544 }
545
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100546 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000547 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500548 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100549 continue;
550
Mengdong Lin1a497982015-11-18 02:34:11 -0500551 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100552 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100553
Mark Brown87506542008-11-18 20:50:34 +0000554 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000555 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200556
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200557 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900558 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100559
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000560 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561
Mengdong Lin1a497982015-11-18 02:34:11 -0500562 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100563 continue;
564
Mengdong Lin1a497982015-11-18 02:34:11 -0500565 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800566 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800567 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568
Mengdong Lin1a497982015-11-18 02:34:11 -0500569 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800570 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800571 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 }
573
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200574 /* Recheck all endpoints too, their state is affected by suspend */
575 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700576 snd_soc_dapm_sync(&card->dapm);
577
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000578 /* suspend all COMPONENTs */
Kuninori Morimoto12720632020-01-10 11:36:02 +0900579 for_each_card_rtds(card, rtd) {
580
581 if (rtd->dai_link->ignore_suspend)
582 continue;
583
584 for_each_rtd_components(rtd, i, component) {
585 struct snd_soc_dapm_context *dapm =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200586 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000587
Kuninori Morimoto12720632020-01-10 11:36:02 +0900588 /*
589 * ignore if component was already suspended
590 */
591 if (snd_soc_component_is_suspended(component))
592 continue;
593
594 /*
595 * If there are paths active then the COMPONENT will be
596 * held with bias _ON and should not be suspended.
597 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200598 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000600 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000601 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000602 * bias off then being in STANDBY
603 * means it's doing something,
604 * otherwise fall through.
605 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200606 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000607 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200608 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000609 break;
610 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500611 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 case SND_SOC_BIAS_OFF:
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900614 snd_soc_component_suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000615 if (component->regmap)
616 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800617 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000618 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 break;
620 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000621 dev_dbg(component->dev,
622 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 break;
624 }
625 }
626 }
627
Mark Brown87506542008-11-18 20:50:34 +0000628 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000629 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630
631 return 0;
632}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000633EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200635/*
636 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100637 * setting our codec back up, which can be very slow on I2C
638 */
639static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200642 container_of(work, struct snd_soc_card,
643 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500644 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000645 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500646 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200648 /*
649 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100650 * so userspace apps are blocked from touching us
651 */
652
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000653 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100654
Mark Brown9949788b2010-05-07 20:24:05 +0100655 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100657
Mark Brown87506542008-11-18 20:50:34 +0000658 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000659 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000661 for_each_card_components(card, component) {
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900662 if (snd_soc_component_is_suspended(component))
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900663 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100664 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000666 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100667
Mengdong Lin1a497982015-11-18 02:34:11 -0500668 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100669 continue;
670
Mengdong Lin1a497982015-11-18 02:34:11 -0500671 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000672 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800673 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674
Mengdong Lin1a497982015-11-18 02:34:11 -0500675 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000676 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800677 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678 }
679
Mark Brown3ff3f642008-05-19 12:32:25 +0200680 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000681 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000682 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100683
Mengdong Lin1a497982015-11-18 02:34:11 -0500684 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100685 continue;
686
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000687 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900688 if (dai->playback_active)
689 snd_soc_dai_digital_mute(dai, 0,
690 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200691 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692 }
693
Mark Brown87506542008-11-18 20:50:34 +0000694 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000695 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)
719 if (component->active)
720 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
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100743static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800744};
745
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900746static struct device_node
747*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100748{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900749 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100750
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900751 of_node = component->dev->of_node;
752 if (!of_node && component->dev->parent)
753 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100754
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900755 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100756}
757
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000758static int snd_soc_is_matching_component(
759 const struct snd_soc_dai_link_component *dlc,
760 struct snd_soc_component *component)
761{
762 struct device_node *component_of_node;
763
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900764 if (!dlc)
765 return 0;
766
767 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000768
769 if (dlc->of_node && component_of_node != dlc->of_node)
770 return 0;
771 if (dlc->name && strcmp(component->name, dlc->name))
772 return 0;
773
774 return 1;
775}
776
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200777static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900778 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779{
780 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
782 lockdep_assert_held(&client_mutex);
783
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900784 /*
785 * NOTE
786 *
787 * It returns *1st* found component, but some driver
788 * has few components by same of_node/name
789 * ex)
790 * CPU component and generic DMAEngine component
791 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900792 for_each_component(component)
793 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
796 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100797}
798
Mengdong Linfbb88b52016-04-22 12:25:33 +0800799/**
800 * snd_soc_find_dai - Find a registered DAI
801 *
Jeffy Chen49584712017-08-22 15:57:21 +0800802 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800803 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700804 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800805 * find the DAI of the same name. The component's of_node and name
806 * should also match if being specified.
807 *
808 * Return: pointer of DAI, or NULL if not found.
809 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800810struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200811 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100812{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200813 struct snd_soc_component *component;
814 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100815
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100816 lockdep_assert_held(&client_mutex);
817
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200818 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000819 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000820 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200821 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000822 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800823 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800824 && (!dai->driver->name
825 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100826 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100827
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200828 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100829 }
830 }
831
832 return NULL;
833}
Mengdong Lin305e9022016-04-19 13:12:25 +0800834EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100835
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900836static int soc_dai_link_sanity_check(struct snd_soc_card *card,
837 struct snd_soc_dai_link *link)
Kuninori Morimoto36794902019-11-05 15:45:41 +0900838{
839 int i;
840 struct snd_soc_dai_link_component *codec, *platform;
841
842 for_each_link_codecs(link, i, codec) {
843 /*
844 * Codec must be specified by 1 of name or OF node,
845 * not both or neither.
846 */
847 if (!!codec->name == !!codec->of_node) {
848 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
849 link->name);
850 return -EINVAL;
851 }
852
853 /* Codec DAI name must be specified */
854 if (!codec->dai_name) {
855 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
856 link->name);
857 return -EINVAL;
858 }
859
860 /*
861 * Defer card registration if codec component is not added to
862 * component list.
863 */
864 if (!soc_find_component(codec))
865 return -EPROBE_DEFER;
866 }
867
868 for_each_link_platforms(link, i, platform) {
869 /*
870 * Platform may be specified by either name or OF node, but it
871 * can be left unspecified, then no components will be inserted
872 * in the rtdcom list
873 */
874 if (!!platform->name == !!platform->of_node) {
875 dev_err(card->dev,
876 "ASoC: Neither/both platform name/of_node are set for %s\n",
877 link->name);
878 return -EINVAL;
879 }
880
881 /*
882 * Defer card registration if platform component is not added to
883 * component list.
884 */
885 if (!soc_find_component(platform))
886 return -EPROBE_DEFER;
887 }
888
889 /* FIXME */
890 if (link->num_cpus > 1) {
891 dev_err(card->dev,
892 "ASoC: multi cpu is not yet supported %s\n",
893 link->name);
894 return -EINVAL;
895 }
896
897 /*
898 * CPU device may be specified by either name or OF node, but
899 * can be left unspecified, and will be matched based on DAI
900 * name alone..
901 */
902 if (link->cpus->name && link->cpus->of_node) {
903 dev_err(card->dev,
904 "ASoC: Neither/both cpu name/of_node are set for %s\n",
905 link->name);
906 return -EINVAL;
907 }
908
909 /*
Kuninori Morimotocd3c5ad2019-11-05 15:46:00 +0900910 * Defer card registration if cpu dai component is not added to
Kuninori Morimoto36794902019-11-05 15:45:41 +0900911 * component list.
912 */
913 if ((link->cpus->of_node || link->cpus->name) &&
914 !soc_find_component(link->cpus))
915 return -EPROBE_DEFER;
916
917 /*
918 * At least one of CPU DAI name or CPU device name/node must be
919 * specified
920 */
921 if (!link->cpus->dai_name &&
922 !(link->cpus->name || link->cpus->of_node)) {
923 dev_err(card->dev,
924 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
925 link->name);
926 return -EINVAL;
927 }
928
929 return 0;
930}
931
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900932/**
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900933 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
934 * @card: The ASoC card to which the pcm_runtime has
935 * @rtd: The pcm_runtime to remove
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900936 *
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900937 * This function removes a pcm_runtime from the ASoC card.
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900938 */
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900939void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
940 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900941{
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900942 lockdep_assert_held(&client_mutex);
943
944 /*
945 * Notify the machine driver for extra destruction
946 */
947 if (card->remove_dai_link)
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900948 card->remove_dai_link(card, rtd->dai_link);
Kuninori Morimotoda704f22019-11-06 10:07:38 +0900949
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900950 soc_free_pcm_runtime(rtd);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900951}
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900952EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +0900953
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900954/**
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900955 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
956 * @card: The ASoC card to which the pcm_runtime is added
957 * @dai_link: The DAI link to find pcm_runtime
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900958 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900959 * This function adds a pcm_runtime ASoC card by using dai_link.
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900960 *
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900961 * Note: Topology can use this API to add pcm_runtime when probing the
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900962 * topology component. And machine drivers can still define static
963 * DAI links in dai_link array.
964 */
Kuninori Morimoto0c048002019-12-10 09:34:19 +0900965int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
966 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200967{
Mengdong Lin1a497982015-11-18 02:34:11 -0500968 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +0200969 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000970 struct snd_soc_component *component;
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900971 int i, ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +0900973 lockdep_assert_held(&client_mutex);
974
975 /*
976 * Notify the machine driver for extra initialization
977 */
978 if (card->add_dai_link)
979 card->add_dai_link(card, dai_link);
980
Liam Girdwooda655de82018-07-02 16:59:54 +0100981 if (dai_link->ignore)
982 return 0;
983
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500984 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000985
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900986 ret = soc_dai_link_sanity_check(card, dai_link);
987 if (ret < 0)
988 return ret;
989
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530990 rtd = soc_new_pcm_runtime(card, dai_link);
991 if (!rtd)
992 return -ENOMEM;
993
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900994 /* FIXME: we need multi CPU support in the future */
995 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000996 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100997 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900998 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500999 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001000 }
Kuninori Morimoto12b05232020-01-10 11:35:54 +09001001 snd_soc_rtd_add_component(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001002
Benoit Cousson88bd8702014-07-08 23:19:34 +02001003 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +09001004 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +02001005 for_each_link_codecs(dai_link, i, codec) {
1006 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +09001007 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +01001008 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +02001009 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001010 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001011 }
Jerome Brunet34614732019-06-27 14:13:50 +02001012
Kuninori Morimoto12b05232020-01-10 11:35:54 +09001013 snd_soc_rtd_add_component(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001014 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001015
Benoit Cousson88bd8702014-07-08 23:19:34 +02001016 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +09001017 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +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 Morimotobfa0dd82019-12-10 09:34:32 +09001037static int soc_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1038 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto9e9c70a2019-12-10 09:34:27 +09001039{
1040 int i, ret = 0;
1041
1042 for (i = 0; i < num_dais; ++i) {
1043 struct snd_soc_dai_driver *drv = dais[i]->driver;
1044
1045 if (drv->pcm_new)
1046 ret = drv->pcm_new(rtd, dais[i]);
1047 if (ret < 0) {
1048 dev_err(dais[i]->dev,
1049 "ASoC: Failed to bind %s with pcm device\n",
1050 dais[i]->name);
1051 return ret;
1052 }
1053 }
1054
1055 return 0;
1056}
1057
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001058static int soc_init_pcm_runtime(struct snd_soc_card *card,
1059 struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001060{
1061 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1062 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001063 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001064 int ret, num, i;
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001065
1066 /* set default power off timeout */
1067 rtd->pmdown_time = pmdown_time;
1068
1069 /* do machine specific initialization */
1070 if (dai_link->init) {
1071 ret = dai_link->init(rtd);
1072 if (ret < 0) {
1073 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1074 dai_link->name, ret);
1075 return ret;
1076 }
1077 }
1078
1079 if (dai_link->dai_fmt) {
1080 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1081 if (ret)
1082 return ret;
1083 }
1084
1085 /* add DPCM sysfs entries */
1086 soc_dpcm_debugfs_add(rtd);
1087
1088 num = rtd->num;
1089
1090 /*
1091 * most drivers will register their PCMs using DAI link ordering but
1092 * topology based drivers can use the DAI link id field to set PCM
1093 * device number and then use rtd + a base offset of the BEs.
1094 */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001095 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto46496ac2019-12-10 09:34:36 +09001096 if (!component->driver->use_dai_pcm_id)
1097 continue;
1098
1099 if (rtd->dai_link->no_pcm)
1100 num += component->driver->be_pcm_base;
1101 else
1102 num = rtd->dai_link->id;
1103 }
1104
1105 /* create compress_device if possible */
1106 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1107 if (ret != -ENOTSUPP) {
1108 if (ret < 0)
1109 dev_err(card->dev, "ASoC: can't create compress %s\n",
1110 dai_link->stream_name);
1111 return ret;
1112 }
1113
1114 /* create the pcm */
1115 ret = soc_new_pcm(rtd, num);
1116 if (ret < 0) {
1117 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1118 dai_link->stream_name, ret);
1119 return ret;
1120 }
1121 ret = soc_dai_pcm_new(&cpu_dai, 1, rtd);
1122 if (ret < 0)
1123 return ret;
1124 ret = soc_dai_pcm_new(rtd->codec_dais,
1125 rtd->num_codecs, rtd);
1126 return ret;
1127}
1128
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001129static void soc_set_name_prefix(struct snd_soc_card *card,
1130 struct snd_soc_component *component)
1131{
Kuninori Morimotoaec3ff92019-12-10 09:34:52 +09001132 struct device_node *of_node = soc_component_to_node(component);
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001133 const char *str;
1134 int ret, i;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001135
Kuninori Morimoto4702f992019-12-10 09:34:48 +09001136 for (i = 0; i < card->num_configs; i++) {
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001137 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001138
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09001139 if (snd_soc_is_matching_component(&map->dlc, component)) {
1140 component->name_prefix = map->name_prefix;
1141 return;
1142 }
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001143 }
1144
1145 /*
1146 * If there is no configuration table or no match in the table,
1147 * check if a prefix is provided in the node
1148 */
Kuninori Morimoto29d9fc72019-12-11 13:31:39 +09001149 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1150 if (ret < 0)
1151 return;
1152
1153 component->name_prefix = str;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001154}
1155
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001156static void soc_remove_component(struct snd_soc_component *component,
1157 int probed)
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001158{
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001159
1160 if (!component->card)
1161 return;
1162
1163 if (probed)
1164 snd_soc_component_remove(component);
1165
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001166 /* For framework level robustness */
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +02001167 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001168
Bard liao7a5d9812019-09-18 21:31:31 +08001169 list_del_init(&component->card_list);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001170 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1171 soc_cleanup_component_debugfs(component);
1172 component->card = NULL;
Pierre-Louis Bossart0e36f362019-08-07 21:51:31 -05001173 snd_soc_component_module_put_when_remove(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001174}
1175
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001176static int soc_probe_component(struct snd_soc_card *card,
1177 struct snd_soc_component *component)
1178{
1179 struct snd_soc_dapm_context *dapm =
1180 snd_soc_component_get_dapm(component);
1181 struct snd_soc_dai *dai;
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001182 int probed = 0;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001183 int ret;
1184
1185 if (!strcmp(component->name, "snd-soc-dummy"))
1186 return 0;
1187
1188 if (component->card) {
1189 if (component->card != card) {
1190 dev_err(component->dev,
1191 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1192 card->name, component->card->name);
1193 return -ENODEV;
1194 }
1195 return 0;
1196 }
1197
1198 ret = snd_soc_component_module_get_when_probe(component);
1199 if (ret < 0)
1200 return ret;
1201
1202 component->card = card;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001203 soc_set_name_prefix(card, component);
1204
1205 soc_init_component_debugfs(component);
1206
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001207 snd_soc_dapm_init(dapm, card, component);
Kuninori Morimotob614bea2019-08-23 09:58:47 +09001208
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001209 ret = snd_soc_dapm_new_controls(dapm,
1210 component->driver->dapm_widgets,
1211 component->driver->num_dapm_widgets);
1212
1213 if (ret != 0) {
1214 dev_err(component->dev,
1215 "Failed to create new controls %d\n", ret);
1216 goto err_probe;
1217 }
1218
1219 for_each_component_dais(component, dai) {
1220 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1221 if (ret != 0) {
1222 dev_err(component->dev,
1223 "Failed to create DAI widgets %d\n", ret);
1224 goto err_probe;
1225 }
1226 }
1227
1228 ret = snd_soc_component_probe(component);
1229 if (ret < 0) {
1230 dev_err(component->dev,
1231 "ASoC: failed to probe component %d\n", ret);
1232 goto err_probe;
1233 }
1234 WARN(dapm->idle_bias_off &&
1235 dapm->bias_level != SND_SOC_BIAS_OFF,
1236 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1237 component->name);
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001238 probed = 1;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001239
1240 /* machine specific init */
1241 if (component->init) {
1242 ret = component->init(component);
1243 if (ret < 0) {
1244 dev_err(component->dev,
1245 "Failed to do machine specific init %d\n", ret);
1246 goto err_probe;
1247 }
1248 }
1249
1250 ret = snd_soc_add_component_controls(component,
1251 component->driver->controls,
1252 component->driver->num_controls);
1253 if (ret < 0)
1254 goto err_probe;
1255
1256 ret = snd_soc_dapm_add_routes(dapm,
1257 component->driver->dapm_routes,
1258 component->driver->num_dapm_routes);
1259 if (ret < 0)
1260 goto err_probe;
1261
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001262 /* see for_each_card_components */
1263 list_add(&component->card_list, &card->component_dev_list);
1264
1265err_probe:
1266 if (ret < 0)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001267 soc_remove_component(component, probed);
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001268
1269 return ret;
1270}
1271
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001272static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001273{
1274 int err;
1275
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -06001276 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +00001277 dai->driver->remove_order != order)
1278 return;
1279
Kuninori Morimotodcdab582019-07-22 10:35:05 +09001280 err = snd_soc_dai_remove(dai);
1281 if (err < 0)
1282 dev_err(dai->dev,
1283 "ASoC: failed to remove %s: %d\n",
1284 dai->name, err);
1285
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +00001286 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001287}
1288
Kuninori Morimotoa7d44f72019-09-04 09:15:06 +09001289static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1290{
1291 int ret;
1292
1293 if (dai->probed ||
1294 dai->driver->probe_order != order)
1295 return 0;
1296
1297 ret = snd_soc_dai_probe(dai);
1298 if (ret < 0) {
1299 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1300 dai->name, ret);
1301 return ret;
1302 }
1303
1304 dai->probed = 1;
1305
1306 return 0;
1307}
1308
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001309static void soc_remove_link_dais(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001311 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001312 struct snd_soc_dai *codec_dai;
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001313 struct snd_soc_pcm_runtime *rtd;
1314 int order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001316 for_each_comp_order(order) {
1317 for_each_card_rtds(card, rtd) {
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001318 /* remove the CODEC DAI */
1319 for_each_rtd_codec_dai(rtd, i, codec_dai)
1320 soc_remove_dai(codec_dai, order);
1321
1322 soc_remove_dai(rtd->cpu_dai, order);
1323 }
1324 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001325}
1326
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001327static int soc_probe_link_dais(struct snd_soc_card *card)
1328{
1329 struct snd_soc_dai *codec_dai;
1330 struct snd_soc_pcm_runtime *rtd;
1331 int i, order, ret;
1332
1333 for_each_comp_order(order) {
1334 for_each_card_rtds(card, rtd) {
1335
1336 dev_dbg(card->dev,
1337 "ASoC: probe %s dai link %d late %d\n",
1338 card->name, rtd->num, order);
1339
1340 ret = soc_probe_dai(rtd->cpu_dai, order);
1341 if (ret)
1342 return ret;
1343
1344 /* probe the CODEC DAI */
1345 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1346 ret = soc_probe_dai(codec_dai, order);
1347 if (ret)
1348 return ret;
1349 }
1350 }
1351 }
1352
1353 return 0;
1354}
1355
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001356static void soc_remove_link_components(struct snd_soc_card *card)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001357{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001358 struct snd_soc_component *component;
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001359 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001360 int i, order;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001361
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001362 for_each_comp_order(order) {
1363 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001364 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001365 if (component->driver->remove_order != order)
1366 continue;
1367
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001368 soc_remove_component(component, 1);
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001369 }
1370 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001371 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001372}
1373
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001374static int soc_probe_link_components(struct snd_soc_card *card)
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001375{
1376 struct snd_soc_component *component;
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001377 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001378 int i, ret, order;
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001379
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001380 for_each_comp_order(order) {
1381 for_each_card_rtds(card, rtd) {
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001382 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001383 if (component->driver->probe_order != order)
1384 continue;
1385
1386 ret = soc_probe_component(card, component);
1387 if (ret < 0)
1388 return ret;
1389 }
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001390 }
1391 }
1392
1393 return 0;
1394}
1395
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001396static void soc_unbind_aux_dev(struct snd_soc_card *card)
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001397{
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001398 struct snd_soc_component *component, *_component;
1399
1400 for_each_card_auxs_safe(card, component, _component) {
1401 component->init = NULL;
1402 list_del(&component->card_aux_list);
1403 }
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001404}
1405
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001406static int soc_bind_aux_dev(struct snd_soc_card *card)
Mark Brownb19e6e72012-03-14 21:18:39 +00001407{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001408 struct snd_soc_component *component;
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001409 struct snd_soc_aux_dev *aux;
1410 int i;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001411
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001412 for_each_card_pre_auxs(card, i, aux) {
1413 /* codecs, usually analog devices */
1414 component = soc_find_component(&aux->dlc);
1415 if (!component)
1416 return -EPROBE_DEFER;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001417
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001418 component->init = aux->init;
1419 /* see for_each_card_auxs */
1420 list_add(&component->card_aux_list, &card->aux_comp_list);
1421 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001422 return 0;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001423}
1424
1425static int soc_probe_aux_devices(struct snd_soc_card *card)
1426{
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001427 struct snd_soc_component *component;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001428 int order;
1429 int ret;
1430
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001431 for_each_comp_order(order) {
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001432 for_each_card_auxs(card, component) {
1433 if (component->driver->probe_order != order)
1434 continue;
1435
1436 ret = soc_probe_component(card, component);
1437 if (ret < 0)
1438 return ret;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001439 }
1440 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001441
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001442 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001443}
1444
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001445static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001446{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001447 struct snd_soc_component *comp, *_comp;
1448 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001449
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001450 for_each_comp_order(order) {
Kuninori Morimotoc2b71c72019-08-08 14:54:44 +09001451 for_each_card_auxs_safe(card, comp, _comp) {
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001452 if (comp->driver->remove_order == order)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001453 soc_remove_component(comp, 1);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001454 }
1455 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001456}
1457
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001458/**
1459 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1460 * @rtd: The runtime for which the DAI link format should be changed
1461 * @dai_fmt: The new DAI link format
1462 *
1463 * This function updates the DAI link format for all DAIs connected to the DAI
1464 * link for the specified runtime.
1465 *
1466 * Note: For setups with a static format set the dai_fmt field in the
1467 * corresponding snd_dai_link struct instead of using this function.
1468 *
1469 * Returns 0 on success, otherwise a negative error code.
1470 */
1471int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1472 unsigned int dai_fmt)
1473{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001474 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001475 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001476 unsigned int i;
1477 int ret;
1478
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001479 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001480 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1481 if (ret != 0 && ret != -ENOTSUPP) {
1482 dev_warn(codec_dai->dev,
1483 "ASoC: Failed to set DAI format: %d\n", ret);
1484 return ret;
1485 }
1486 }
1487
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001488 /*
1489 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1490 * the component which has non_legacy_dai_naming is Codec
1491 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001492 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001493 unsigned int inv_dai_fmt;
1494
1495 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1496 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1497 case SND_SOC_DAIFMT_CBM_CFM:
1498 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1499 break;
1500 case SND_SOC_DAIFMT_CBM_CFS:
1501 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1502 break;
1503 case SND_SOC_DAIFMT_CBS_CFM:
1504 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1505 break;
1506 case SND_SOC_DAIFMT_CBS_CFS:
1507 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1508 break;
1509 }
1510
1511 dai_fmt = inv_dai_fmt;
1512 }
1513
1514 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1515 if (ret != 0 && ret != -ENOTSUPP) {
1516 dev_warn(cpu_dai->dev,
1517 "ASoC: Failed to set DAI format: %d\n", ret);
1518 return ret;
1519 }
1520
1521 return 0;
1522}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001523EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001524
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001525#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001526/*
Kuninori Morimoto8a6a6a32019-12-11 13:32:05 +09001527 * If a DMI filed contain strings in this blacklist (e.g.
1528 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1529 * as invalid and dropped when setting the card long name from DMI info.
1530 */
1531static const char * const dmi_blacklist[] = {
1532 "To be filled by OEM",
1533 "TBD by OEM",
1534 "Default String",
1535 "Board Manufacturer",
1536 "Board Vendor Name",
1537 "Board Product Name",
1538 NULL, /* terminator */
1539};
1540
1541/*
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001542 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001543 * separate different DMI fields in the card long name. Only number and
1544 * alphabet characters and a few separator characters are kept.
1545 */
1546static void cleanup_dmi_name(char *name)
1547{
1548 int i, j = 0;
1549
1550 for (i = 0; name[i]; i++) {
1551 if (isalnum(name[i]) || (name[i] == '.')
1552 || (name[i] == '_'))
1553 name[j++] = name[i];
1554 else if (name[i] == '-')
1555 name[j++] = '_';
1556 }
1557
1558 name[j] = '\0';
1559}
1560
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001561/*
1562 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001563 * in the black list.
1564 */
1565static int is_dmi_valid(const char *field)
1566{
1567 int i = 0;
1568
1569 while (dmi_blacklist[i]) {
1570 if (strstr(field, dmi_blacklist[i]))
1571 return 0;
1572 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001573 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001574
1575 return 1;
1576}
1577
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001578/*
1579 * Append a string to card->dmi_longname with character cleanups.
1580 */
1581static void append_dmi_string(struct snd_soc_card *card, const char *str)
1582{
1583 char *dst = card->dmi_longname;
1584 size_t dst_len = sizeof(card->dmi_longname);
1585 size_t len;
1586
1587 len = strlen(dst);
1588 snprintf(dst + len, dst_len - len, "-%s", str);
1589
1590 len++; /* skip the separator "-" */
1591 if (len < dst_len)
1592 cleanup_dmi_name(dst + len);
1593}
1594
Liam Girdwood345233d2017-01-14 16:13:02 +08001595/**
1596 * snd_soc_set_dmi_name() - Register DMI names to card
1597 * @card: The card to register DMI names
1598 * @flavour: The flavour "differentiator" for the card amongst its peers.
1599 *
1600 * An Intel machine driver may be used by many different devices but are
1601 * difficult for userspace to differentiate, since machine drivers ususally
1602 * use their own name as the card short name and leave the card long name
1603 * blank. To differentiate such devices and fix bugs due to lack of
1604 * device-specific configurations, this function allows DMI info to be used
1605 * as the sound card long name, in the format of
1606 * "vendor-product-version-board"
1607 * (Character '-' is used to separate different DMI fields here).
1608 * This will help the user space to load the device-specific Use Case Manager
1609 * (UCM) configurations for the card.
1610 *
1611 * Possible card long names may be:
1612 * DellInc.-XPS139343-01-0310JH
1613 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1614 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1615 *
1616 * This function also supports flavoring the card longname to provide
1617 * the extra differentiation, like "vendor-product-version-board-flavor".
1618 *
1619 * We only keep number and alphabet characters and a few separator characters
1620 * in the card long name since UCM in the user space uses the card long names
1621 * as card configuration directory names and AudoConf cannot support special
1622 * charactors like SPACE.
1623 *
1624 * Returns 0 on success, otherwise a negative error code.
1625 */
1626int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1627{
1628 const char *vendor, *product, *product_version, *board;
Liam Girdwood345233d2017-01-14 16:13:02 +08001629
1630 if (card->long_name)
1631 return 0; /* long name already set by driver or from DMI */
1632
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001633 /* make up dmi long name as: vendor-product-version-board */
Liam Girdwood345233d2017-01-14 16:13:02 +08001634 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001635 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001636 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1637 return 0;
1638 }
1639
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001640 snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
Liam Girdwood345233d2017-01-14 16:13:02 +08001641 cleanup_dmi_name(card->dmi_longname);
1642
1643 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001644 if (product && is_dmi_valid(product)) {
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001645 append_dmi_string(card, product);
Liam Girdwood345233d2017-01-14 16:13:02 +08001646
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001647 /*
1648 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001649 * name in the product version field
1650 */
1651 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001652 if (product_version && is_dmi_valid(product_version))
1653 append_dmi_string(card, product_version);
Liam Girdwood345233d2017-01-14 16:13:02 +08001654 }
1655
1656 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001657 if (board && is_dmi_valid(board)) {
Jaroslav Kysela39870b02019-11-20 18:44:35 +01001658 if (!product || strcasecmp(board, product))
1659 append_dmi_string(card, board);
Liam Girdwood345233d2017-01-14 16:13:02 +08001660 } else if (!product) {
1661 /* fall back to using legacy name */
1662 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1663 return 0;
1664 }
1665
1666 /* Add flavour to dmi long name */
Jaroslav Kysela4e01e5d2019-11-20 18:44:34 +01001667 if (flavour)
1668 append_dmi_string(card, flavour);
Liam Girdwood345233d2017-01-14 16:13:02 +08001669
1670 /* set the card long name */
1671 card->long_name = card->dmi_longname;
1672
1673 return 0;
1674}
1675EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001676#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001677
Liam Girdwooda655de82018-07-02 16:59:54 +01001678static void soc_check_tplg_fes(struct snd_soc_card *card)
1679{
1680 struct snd_soc_component *component;
1681 const struct snd_soc_component_driver *comp_drv;
1682 struct snd_soc_dai_link *dai_link;
1683 int i;
1684
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001685 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001686
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001687 /* does this component override BEs ? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001688 if (!component->driver->ignore_machine)
1689 continue;
1690
1691 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001692 if (!strcmp(component->driver->ignore_machine,
1693 card->dev->driver->name))
1694 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001695 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001696 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001697 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001698match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001699 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001700 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001701
1702 /* ignore this FE */
1703 if (dai_link->dynamic) {
1704 dai_link->ignore = true;
1705 continue;
1706 }
1707
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001708 dev_info(card->dev, "info: override BE DAI link %s\n",
Liam Girdwooda655de82018-07-02 16:59:54 +01001709 card->dai_link[i].name);
1710
1711 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001712 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001713 dev_err(card->dev, "init platform error");
1714 continue;
1715 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001716 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001717
1718 /* convert non BE into BE */
1719 dai_link->no_pcm = 1;
Daniel Baluta218fe9b2019-12-04 17:13:33 +02001720 dai_link->dpcm_playback = 1;
1721 dai_link->dpcm_capture = 1;
Liam Girdwooda655de82018-07-02 16:59:54 +01001722
1723 /* override any BE fixups */
1724 dai_link->be_hw_params_fixup =
1725 component->driver->be_hw_params_fixup;
1726
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001727 /*
1728 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001729 * dai link name if it's NULL to help bind widgets.
1730 */
1731 if (!dai_link->stream_name)
1732 dai_link->stream_name = dai_link->name;
1733 }
1734
1735 /* Inform userspace we are using alternate topology */
1736 if (component->driver->topology_name_prefix) {
1737
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001738 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001739 if (!card->topology_shortname_created) {
1740 comp_drv = component->driver;
1741
1742 snprintf(card->topology_shortname, 32, "%s-%s",
1743 comp_drv->topology_name_prefix,
1744 card->name);
1745 card->topology_shortname_created = true;
1746 }
1747
1748 /* use topology shortname */
1749 card->name = card->topology_shortname;
1750 }
1751 }
1752}
1753
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001754#define soc_setup_card_name(name, name1, name2, norm) \
1755 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1756static void __soc_setup_card_name(char *name, int len,
1757 const char *name1, const char *name2,
1758 int normalization)
1759{
1760 int i;
1761
1762 snprintf(name, len, "%s", name1 ? name1 : name2);
1763
1764 if (!normalization)
1765 return;
1766
1767 /*
1768 * Name normalization
1769 *
1770 * The driver name is somewhat special, as it's used as a key for
1771 * searches in the user-space.
1772 *
1773 * ex)
1774 * "abcd??efg" -> "abcd__efg"
1775 */
1776 for (i = 0; i < len; i++) {
1777 switch (name[i]) {
1778 case '_':
1779 case '-':
1780 case '\0':
1781 break;
1782 default:
1783 if (!isalnum(name[i]))
1784 name[i] = '_';
1785 break;
1786 }
1787 }
1788}
1789
Kuninori Morimotoce214012019-11-13 10:16:48 +09001790static void soc_cleanup_card_resources(struct snd_soc_card *card,
1791 int card_probed)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001792{
Kuninori Morimotocc733902019-12-10 09:33:40 +09001793 struct snd_soc_pcm_runtime *rtd, *n;
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001794
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001795 if (card->snd_card)
1796 snd_card_disconnect_sync(card->snd_card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001797
Kuninori Morimoto2a6f0892019-11-13 10:16:29 +09001798 snd_soc_dapm_shutdown(card);
1799
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001800 /* remove and free each DAI */
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001801 soc_remove_link_dais(card);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001802 soc_remove_link_components(card);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001803
Kuninori Morimotocc733902019-12-10 09:33:40 +09001804 for_each_card_rtds_safe(card, rtd, n)
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +09001805 snd_soc_remove_pcm_runtime(card, rtd);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001806
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001807 /* remove auxiliary devices */
1808 soc_remove_aux_devices(card);
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001809 soc_unbind_aux_dev(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001810
1811 snd_soc_dapm_free(&card->dapm);
1812 soc_cleanup_card_debugfs(card);
1813
1814 /* remove the card */
Kuninori Morimotoce214012019-11-13 10:16:48 +09001815 if (card_probed && card->remove)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001816 card->remove(card);
Kuninori Morimoto0ced7b02019-11-18 10:51:11 +09001817
1818 if (card->snd_card) {
1819 snd_card_free(card->snd_card);
1820 card->snd_card = NULL;
1821 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001822}
1823
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001824static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
1825{
1826 if (card->instantiated) {
Kuninori Morimotoce214012019-11-13 10:16:48 +09001827 int card_probed = 1;
1828
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001829 card->instantiated = false;
1830 snd_soc_flush_all_delayed_work(card);
1831
Kuninori Morimotoce214012019-11-13 10:16:48 +09001832 soc_cleanup_card_resources(card, card_probed);
Kuninori Morimoto2cc1afc2019-11-13 10:16:34 +09001833 if (!unregister)
1834 list_add(&card->list, &unbind_card_list);
1835 } else {
1836 if (unregister)
1837 list_del(&card->list);
1838 }
1839}
1840
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001841static int snd_soc_bind_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842{
Mengdong Lin1a497982015-11-18 02:34:11 -05001843 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001844 struct snd_soc_component *component;
Mengdong Lin61b00882015-12-02 14:11:48 +08001845 struct snd_soc_dai_link *dai_link;
Kuninori Morimotoce214012019-11-13 10:16:48 +09001846 int ret, i, card_probed = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001847
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001848 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001849 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001851 snd_soc_dapm_init(&card->dapm, card, NULL);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001852
Liam Girdwooda655de82018-07-02 16:59:54 +01001853 /* check whether any platform is ignore machine FE and using topology */
1854 soc_check_tplg_fes(card);
1855
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001856 /* bind aux_devs too */
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001857 ret = soc_bind_aux_dev(card);
1858 if (ret < 0)
1859 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001860
Mengdong Linf8f80362015-12-02 14:11:22 +08001861 /* add predefined DAI links to the list */
Kuninori Morimotod8145982019-10-02 14:23:07 +09001862 card->num_rtd = 0;
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001863 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimoto0c048002019-12-10 09:34:19 +09001864 ret = snd_soc_add_pcm_runtime(card, dai_link);
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001865 if (ret < 0)
1866 goto probe_end;
1867 }
Mengdong Linf8f80362015-12-02 14:11:22 +08001868
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001869 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001870 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001871 card->owner, 0, &card->snd_card);
1872 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001873 dev_err(card->dev,
1874 "ASoC: can't create sound card for card %s: %d\n",
1875 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001876 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001877 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001878
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001879 soc_init_card_debugfs(card);
1880
Kuninori Morimotob3da4252019-08-07 10:31:24 +09001881 soc_resume_init(card);
Andy Green6ed25972008-06-13 16:24:05 +01001882
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001883 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1884 card->num_dapm_widgets);
1885 if (ret < 0)
1886 goto probe_end;
Mark Brown9a841eb2011-04-12 17:51:37 -07001887
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09001888 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1889 card->num_of_dapm_widgets);
1890 if (ret < 0)
1891 goto probe_end;
Nicolin Chenf23e8602015-02-14 17:22:49 -08001892
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893 /* initialise the sound card only once */
1894 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001895 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001897 goto probe_end;
Kuninori Morimotoce214012019-11-13 10:16:48 +09001898 card_probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001899 }
1900
Stephen Warren62ae68f2012-06-08 12:34:23 -06001901 /* probe all components used by DAI links on this card */
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001902 ret = soc_probe_link_components(card);
1903 if (ret < 0) {
1904 dev_err(card->dev,
1905 "ASoC: failed to instantiate card %d\n", ret);
1906 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001907 }
1908
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001909 /* probe auxiliary components */
1910 ret = soc_probe_aux_devices(card);
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001911 if (ret < 0) {
1912 dev_err(card->dev,
1913 "ASoC: failed to probe aux component %d\n", ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001914 goto probe_end;
Kuninori Morimoto74bd3f92019-11-06 10:08:06 +09001915 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001916
Stephen Warren62ae68f2012-06-08 12:34:23 -06001917 /* probe all DAI links on this card */
Kuninori Morimotoc7e73772019-09-04 09:15:17 +09001918 ret = soc_probe_link_dais(card);
1919 if (ret < 0) {
1920 dev_err(card->dev,
1921 "ASoC: failed to instantiate card %d\n", ret);
1922 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923 }
1924
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001925 for_each_card_rtds(card, rtd) {
Kuninori Morimotoeaffeef2019-12-10 09:34:44 +09001926 ret = soc_init_pcm_runtime(card, rtd);
Kuninori Morimoto626c2e52019-12-10 09:34:40 +09001927 if (ret < 0)
1928 goto probe_end;
1929 }
Kuninori Morimotoc4b46982019-09-04 09:15:12 +09001930
Mark Brown888df392012-02-16 19:37:51 -08001931 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001932 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001933
Kuninori Morimoto9b98c7c2019-08-07 10:31:08 +09001934 ret = snd_soc_add_card_controls(card, card->controls,
1935 card->num_controls);
1936 if (ret < 0)
1937 goto probe_end;
Mark Brownb7af1da2011-04-07 19:18:44 +09001938
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001939 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1940 card->num_dapm_routes);
1941 if (ret < 0)
1942 goto probe_end;
Mark Brownb8ad29d2011-03-02 18:35:51 +00001943
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09001944 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1945 card->num_of_dapm_routes);
1946 if (ret < 0)
1947 goto probe_end;
Mark Brown75d9ac42011-09-27 16:41:01 +01001948
Takashi Iwai861886d2017-04-24 08:54:42 +02001949 /* try to set some sane longname if DMI is available */
1950 snd_soc_set_dmi_name(card, NULL);
1951
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001952 soc_setup_card_name(card->snd_card->shortname,
1953 card->name, NULL, 0);
1954 soc_setup_card_name(card->snd_card->longname,
1955 card->long_name, card->name, 0);
1956 soc_setup_card_name(card->snd_card->driver,
1957 card->driver_name, card->name, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001958
Jaroslav Kyseladc73d732019-11-19 18:49:32 +01001959 if (card->components) {
1960 /* the current implementation of snd_component_add() accepts */
1961 /* multiple components in the string separated by space, */
1962 /* but the string collision (identical string) check might */
1963 /* not work correctly */
1964 ret = snd_component_add(card->snd_card, card->components);
1965 if (ret < 0) {
1966 dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
1967 card->name, ret);
1968 goto probe_end;
1969 }
1970 }
1971
Mark Brown28e9ad92011-03-02 18:36:34 +00001972 if (card->late_probe) {
1973 ret = card->late_probe(card);
1974 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001975 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001976 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001977 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00001978 }
1979 }
Kuninori Morimotoce214012019-11-13 10:16:48 +09001980 card_probed = 1;
Mark Brown28e9ad92011-03-02 18:36:34 +00001981
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001982 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 ret = snd_card_register(card->snd_card);
1985 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001986 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1987 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001988 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001989 }
1990
Mark Brown435c5e22008-12-04 15:32:53 +00001991 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08001992 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01001993 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00001994
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001995 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09001996 for_each_card_components(card, component)
1997 if (!component->active)
1998 pinctrl_pm_select_sleep_state(component->dev);
Kuninori Morimotoed90c012019-11-06 10:07:56 +09001999
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002000probe_end:
2001 if (ret < 0)
Kuninori Morimotoce214012019-11-13 10:16:48 +09002002 soc_cleanup_card_resources(card, card_probed);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002004 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002005 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002006
Mark Brownb19e6e72012-03-14 21:18:39 +00002007 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002008}
2009
2010/* probes a new socdev */
2011static int soc_probe(struct platform_device *pdev)
2012{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002013 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002014
Vinod Koul70a7ca32011-01-14 19:22:48 +05302015 /*
2016 * no card, so machine driver should be registering card
2017 * we should not be here in that case so ret error
2018 */
2019 if (!card)
2020 return -EINVAL;
2021
Mark Brownfe4085e2012-03-02 13:07:41 +00002022 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002023 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002024 card->name);
2025
Mark Brown435c5e22008-12-04 15:32:53 +00002026 /* Bodge while we unpick instantiation */
2027 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002028
Mark Brown28d528c2012-08-09 18:45:23 +01002029 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002030}
2031
2032/* removes a socdev */
2033static int soc_remove(struct platform_device *pdev)
2034{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002035 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036
Mark Brownc5af3a22008-11-28 13:29:45 +00002037 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038 return 0;
2039}
2040
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002041int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002042{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002043 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09002044 struct snd_soc_component *component;
Mark Brown51737472009-06-22 13:16:51 +01002045
2046 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002047 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002048
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002049 /*
2050 * Flush out pmdown_time work - we actually do want to run it
2051 * now, we're shutting down so no imminent restart.
2052 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002053 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002056
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002057 /* deactivate pins to sleep state */
Kuninori Morimoto76c39e82020-01-10 11:36:13 +09002058 for_each_card_components(card, component)
2059 pinctrl_pm_select_sleep_state(component->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002060
Mark Brown416356f2009-06-30 19:05:15 +01002061 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002062}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002063EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002064
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002065const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302066 .suspend = snd_soc_suspend,
2067 .resume = snd_soc_resume,
2068 .freeze = snd_soc_suspend,
2069 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002070 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302071 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002072};
Stephen Warrendeb26072011-04-05 19:35:30 -06002073EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002074
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002075/* ASoC platform driver */
2076static struct platform_driver soc_driver = {
2077 .driver = {
2078 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002079 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080 },
2081 .probe = soc_probe,
2082 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083};
2084
Mark Brown096e49d2009-07-05 15:12:22 +01002085/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002086 * snd_soc_cnew - create new control
2087 * @_template: control template
2088 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002089 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002090 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 *
2092 * Create a new mixer control from a template control.
2093 *
2094 * Returns 0 for success, else error.
2095 */
2096struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002097 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002098 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099{
2100 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002101 struct snd_kcontrol *kcontrol;
2102 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103
2104 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002105 template.index = 0;
2106
Mark Brownefb7ac32011-03-08 17:23:24 +00002107 if (!long_name)
2108 long_name = template.name;
2109
2110 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002111 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002112 if (!name)
2113 return NULL;
2114
Mark Brownefb7ac32011-03-08 17:23:24 +00002115 template.name = name;
2116 } else {
2117 template.name = long_name;
2118 }
2119
2120 kcontrol = snd_ctl_new1(&template, data);
2121
2122 kfree(name);
2123
2124 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125}
2126EXPORT_SYMBOL_GPL(snd_soc_cnew);
2127
Liam Girdwood022658b2012-02-03 17:43:09 +00002128static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2129 const struct snd_kcontrol_new *controls, int num_controls,
2130 const char *prefix, void *data)
2131{
2132 int err, i;
2133
2134 for (i = 0; i < num_controls; i++) {
2135 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002136
Liam Girdwood022658b2012-02-03 17:43:09 +00002137 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2138 control->name, prefix));
2139 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002140 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2141 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002142 return err;
2143 }
2144 }
2145
2146 return 0;
2147}
2148
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002149struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2150 const char *name)
2151{
2152 struct snd_card *card = soc_card->snd_card;
2153 struct snd_kcontrol *kctl;
2154
2155 if (unlikely(!name))
2156 return NULL;
2157
2158 list_for_each_entry(kctl, &card->controls, list)
2159 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2160 return kctl;
2161 return NULL;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2164
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002166 * snd_soc_add_component_controls - Add an array of controls to a component.
2167 *
2168 * @component: Component to add controls to
2169 * @controls: Array of controls to add
2170 * @num_controls: Number of elements in the array
2171 *
2172 * Return: 0 for success, else error.
2173 */
2174int snd_soc_add_component_controls(struct snd_soc_component *component,
2175 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2176{
2177 struct snd_card *card = component->card->snd_card;
2178
2179 return snd_soc_add_controls(card, component->dev, controls,
2180 num_controls, component->name_prefix, component);
2181}
2182EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2183
2184/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002185 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2186 * Convenience function to add a list of controls.
2187 *
2188 * @soc_card: SoC card to add controls to
2189 * @controls: array of controls to add
2190 * @num_controls: number of elements in the array
2191 *
2192 * Return 0 for success, else error.
2193 */
2194int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2195 const struct snd_kcontrol_new *controls, int num_controls)
2196{
2197 struct snd_card *card = soc_card->snd_card;
2198
2199 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2200 NULL, soc_card);
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2203
2204/**
2205 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2206 * Convienience function to add a list of controls.
2207 *
2208 * @dai: DAI to add controls to
2209 * @controls: array of controls to add
2210 * @num_controls: number of elements in the array
2211 *
2212 * Return 0 for success, else error.
2213 */
2214int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2215 const struct snd_kcontrol_new *controls, int num_controls)
2216{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002217 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002218
2219 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2220 NULL, dai);
2221}
2222EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2223
Mark Brownc5af3a22008-11-28 13:29:45 +00002224/**
2225 * snd_soc_register_card - Register a card with the ASoC core
2226 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002227 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002228 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002229 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302230int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002231{
2232 if (!card->name || !card->dev)
2233 return -EINVAL;
2234
Mark Browned77cc12011-05-03 18:25:34 +01002235 dev_set_drvdata(card->dev, card);
2236
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002237 INIT_LIST_HEAD(&card->widgets);
2238 INIT_LIST_HEAD(&card->paths);
2239 INIT_LIST_HEAD(&card->dapm_list);
2240 INIT_LIST_HEAD(&card->aux_comp_list);
2241 INIT_LIST_HEAD(&card->component_dev_list);
2242 INIT_LIST_HEAD(&card->list);
Mengdong Lin1a497982015-11-18 02:34:11 -05002243 INIT_LIST_HEAD(&card->rtd_list);
Mark Browndb432b42011-10-03 21:06:40 +01002244 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002245 INIT_LIST_HEAD(&card->dobj_list);
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002246
Mark Brownc5af3a22008-11-28 13:29:45 +00002247 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002248 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002249 mutex_init(&card->dapm_mutex);
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03002250 mutex_init(&card->pcm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002251 spin_lock_init(&card->dpcm_lock);
Mark Brownc5af3a22008-11-28 13:29:45 +00002252
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002253 return snd_soc_bind_card(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002254}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302255EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002256
2257/**
2258 * snd_soc_unregister_card - Unregister a card with the ASoC core
2259 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002260 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002261 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002262 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302263int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002264{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002265 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002266 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002267 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002268 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002269
2270 return 0;
2271}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302272EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002273
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002274/*
2275 * Simplify DAI link configuration by removing ".-1" from device names
2276 * and sanitizing names.
2277 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002278static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002279{
2280 char *found, name[NAME_SIZE];
2281 int id1, id2;
2282
2283 if (dev_name(dev) == NULL)
2284 return NULL;
2285
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002286 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002287
2288 /* are we a "%s.%d" name (platform and SPI components) */
2289 found = strstr(name, dev->driver->name);
2290 if (found) {
2291 /* get ID */
2292 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2293
2294 /* discard ID from name if ID == -1 */
2295 if (*id == -1)
2296 found[strlen(dev->driver->name)] = '\0';
2297 }
2298
2299 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002300 /* I2C component devices are named "bus-addr" */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002301 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2302 char tmp[NAME_SIZE];
2303
2304 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002305 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002306
2307 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002308 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2309 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002310 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002311 } else
2312 *id = 0;
2313 }
2314
Kuninori Morimoto50014492019-10-02 14:22:57 +09002315 return devm_kstrdup(dev, name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002316}
2317
2318/*
2319 * Simplify DAI link naming for single devices with multiple DAIs by removing
2320 * any ".-1" and using the DAI name (instead of device name).
2321 */
2322static inline char *fmt_multiple_name(struct device *dev,
2323 struct snd_soc_dai_driver *dai_drv)
2324{
2325 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002326 dev_err(dev,
2327 "ASoC: error - multiple DAI %s registered with no name\n",
2328 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002329 return NULL;
2330 }
2331
Kuninori Morimoto50014492019-10-02 14:22:57 +09002332 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002333}
2334
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002335void snd_soc_unregister_dai(struct snd_soc_dai *dai)
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002336{
2337 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2338 list_del(&dai->list);
2339}
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002340EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002341
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002342/**
2343 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2344 *
2345 * @component: The component the DAIs are registered for
2346 * @dai_drv: DAI driver to use for the DAI
Randy Dunlapbc9a6652019-12-08 20:37:04 -08002347 * @legacy_dai_naming: if %true, use legacy single-name format;
2348 * if %false, use multiple-name format;
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002349 *
2350 * Topology can use this API to register DAIs when probing a component.
2351 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2352 * will be freed in the component cleanup.
2353 */
2354struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2355 struct snd_soc_dai_driver *dai_drv,
2356 bool legacy_dai_naming)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002357{
2358 struct device *dev = component->dev;
2359 struct snd_soc_dai *dai;
2360
2361 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2362
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002363 lockdep_assert_held(&client_mutex);
2364
Kuninori Morimoto50014492019-10-02 14:22:57 +09002365 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002366 if (dai == NULL)
2367 return NULL;
2368
2369 /*
2370 * Back in the old days when we still had component-less DAIs,
2371 * instead of having a static name, component-less DAIs would
2372 * inherit the name of the parent device so it is possible to
2373 * register multiple instances of the DAI. We still need to keep
2374 * the same naming style even though those DAIs are not
2375 * component-less anymore.
2376 */
2377 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002378 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002379 dai->name = fmt_single_name(dev, &dai->id);
2380 } else {
2381 dai->name = fmt_multiple_name(dev, dai_drv);
2382 if (dai_drv->id)
2383 dai->id = dai_drv->id;
2384 else
2385 dai->id = component->num_dai;
2386 }
Kuninori Morimoto50014492019-10-02 14:22:57 +09002387 if (!dai->name)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002388 return NULL;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002389
2390 dai->component = component;
2391 dai->dev = dev;
2392 dai->driver = dai_drv;
2393 if (!dai->driver->ops)
2394 dai->driver->ops = &null_dai_ops;
2395
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002396 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002397 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002398 component->num_dai++;
2399
2400 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2401 return dai;
2402}
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002403
Mark Brown91151712008-11-30 23:31:24 +00002404/**
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002405 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2406 *
2407 * @component: The component for which the DAIs should be unregistered
2408 */
2409static void snd_soc_unregister_dais(struct snd_soc_component *component)
2410{
2411 struct snd_soc_dai *dai, *_dai;
2412
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002413 for_each_component_dais_safe(component, dai, _dai)
2414 snd_soc_unregister_dai(dai);
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002415}
2416
2417/**
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002418 * snd_soc_register_dais - Register a DAI with the ASoC core
2419 *
2420 * @component: The component the DAIs are registered for
2421 * @dai_drv: DAI driver to use for the DAIs
2422 * @count: Number of DAIs
2423 */
2424static int snd_soc_register_dais(struct snd_soc_component *component,
2425 struct snd_soc_dai_driver *dai_drv,
2426 size_t count)
2427{
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002428 struct snd_soc_dai *dai;
2429 unsigned int i;
2430 int ret;
2431
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002432 for (i = 0; i < count; i++) {
Kuninori Morimoto71cb85f2019-11-05 15:47:18 +09002433 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002434 !component->driver->non_legacy_dai_naming);
2435 if (dai == NULL) {
2436 ret = -ENOMEM;
2437 goto err;
2438 }
2439 }
2440
2441 return 0;
2442
2443err:
2444 snd_soc_unregister_dais(component);
2445
2446 return ret;
2447}
2448
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002449static int snd_soc_component_initialize(struct snd_soc_component *component,
2450 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002451{
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002452 INIT_LIST_HEAD(&component->dai_list);
Kuninori Morimoto495efdb2019-08-20 14:05:20 +09002453 INIT_LIST_HEAD(&component->dobj_list);
2454 INIT_LIST_HEAD(&component->card_list);
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002455 mutex_init(&component->io_mutex);
2456
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002457 component->name = fmt_single_name(dev, &component->id);
2458 if (!component->name) {
2459 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002460 return -ENOMEM;
2461 }
2462
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002463 component->dev = dev;
2464 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002465
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002466 return 0;
2467}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002468
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002469static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002470{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002471 int val_bytes = regmap_get_val_bytes(component->regmap);
2472
2473 /* Errors are legitimate for non-integer byte multiples */
2474 if (val_bytes > 0)
2475 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002476}
2477
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002478#ifdef CONFIG_REGMAP
2479
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002480/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002481 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2482 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002483 * @component: The component for which to initialize the regmap instance
2484 * @regmap: The regmap instance that should be used by the component
2485 *
2486 * This function allows deferred assignment of the regmap instance that is
2487 * associated with the component. Only use this if the regmap instance is not
2488 * yet ready when the component is registered. The function must also be called
2489 * before the first IO attempt of the component.
2490 */
2491void snd_soc_component_init_regmap(struct snd_soc_component *component,
2492 struct regmap *regmap)
2493{
2494 component->regmap = regmap;
2495 snd_soc_component_setup_regmap(component);
2496}
2497EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2498
2499/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002500 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2501 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002502 * @component: The component for which to de-initialize the regmap instance
2503 *
2504 * Calls regmap_exit() on the regmap instance associated to the component and
2505 * removes the regmap instance from the component.
2506 *
2507 * This function should only be used if snd_soc_component_init_regmap() was used
2508 * to initialize the regmap instance.
2509 */
2510void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2511{
2512 regmap_exit(component->regmap);
2513 component->regmap = NULL;
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2516
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002517#endif
2518
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002519#define ENDIANNESS_MAP(name) \
2520 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2521static u64 endianness_format_map[] = {
2522 ENDIANNESS_MAP(S16_),
2523 ENDIANNESS_MAP(U16_),
2524 ENDIANNESS_MAP(S24_),
2525 ENDIANNESS_MAP(U24_),
2526 ENDIANNESS_MAP(S32_),
2527 ENDIANNESS_MAP(U32_),
2528 ENDIANNESS_MAP(S24_3),
2529 ENDIANNESS_MAP(U24_3),
2530 ENDIANNESS_MAP(S20_3),
2531 ENDIANNESS_MAP(U20_3),
2532 ENDIANNESS_MAP(S18_3),
2533 ENDIANNESS_MAP(U18_3),
2534 ENDIANNESS_MAP(FLOAT_),
2535 ENDIANNESS_MAP(FLOAT64_),
2536 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2537};
2538
2539/*
2540 * Fix up the DAI formats for endianness: codecs don't actually see
2541 * the endianness of the data but we're using the CPU format
2542 * definitions which do need to include endianness so we ensure that
2543 * codec DAIs always have both big and little endian variants set.
2544 */
2545static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2546{
2547 int i;
2548
2549 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2550 if (stream->formats & endianness_format_map[i])
2551 stream->formats |= endianness_format_map[i];
2552}
2553
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002554static void snd_soc_try_rebind_card(void)
2555{
2556 struct snd_soc_card *card, *c;
2557
Kuninori Morimotob245d272019-08-07 10:31:19 +09002558 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2559 if (!snd_soc_bind_card(card))
2560 list_del(&card->list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002561}
2562
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002563static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2564{
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002565 struct snd_soc_card *card = component->card;
2566
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002567 snd_soc_unregister_dais(component);
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002568
2569 if (card)
2570 snd_soc_unbind_card(card, false);
2571
2572 list_del(&component->list);
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002573}
2574
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002575int snd_soc_add_component(struct device *dev,
2576 struct snd_soc_component *component,
2577 const struct snd_soc_component_driver *component_driver,
2578 struct snd_soc_dai_driver *dai_drv,
2579 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002580{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002581 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002582 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002583
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002584 mutex_lock(&client_mutex);
2585
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002586 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002587 if (ret)
2588 goto err_free;
2589
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002590 if (component_driver->endianness) {
2591 for (i = 0; i < num_dai; i++) {
2592 convert_endianness_formats(&dai_drv[i].playback);
2593 convert_endianness_formats(&dai_drv[i].capture);
2594 }
2595 }
2596
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002597 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002598 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002599 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002600 goto err_cleanup;
2601 }
2602
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002603 if (!component->driver->write && !component->driver->read) {
2604 if (!component->regmap)
2605 component->regmap = dev_get_regmap(component->dev,
2606 NULL);
2607 if (component->regmap)
2608 snd_soc_component_setup_regmap(component);
2609 }
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002610
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002611 /* see for_each_component */
2612 list_add(&component->list, &component_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002613
2614err_cleanup:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002615 if (ret < 0)
2616 snd_soc_del_component_unlocked(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002617err_free:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002618 mutex_unlock(&client_mutex);
2619
2620 if (ret == 0)
2621 snd_soc_try_rebind_card();
2622
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002623 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002624}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002625EXPORT_SYMBOL_GPL(snd_soc_add_component);
2626
2627int snd_soc_register_component(struct device *dev,
2628 const struct snd_soc_component_driver *component_driver,
2629 struct snd_soc_dai_driver *dai_drv,
2630 int num_dai)
2631{
2632 struct snd_soc_component *component;
2633
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002634 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002635 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002636 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002637
2638 return snd_soc_add_component(dev, component, component_driver,
2639 dai_drv, num_dai);
2640}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002641EXPORT_SYMBOL_GPL(snd_soc_register_component);
2642
2643/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002644 * snd_soc_unregister_component - Unregister all related component
2645 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002646 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002647 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002648 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002649void snd_soc_unregister_component(struct device *dev)
2650{
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002651 struct snd_soc_component *component;
2652
2653 mutex_lock(&client_mutex);
2654 while (1) {
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +09002655 component = snd_soc_lookup_component_nolocked(dev, NULL);
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002656 if (!component)
2657 break;
2658
2659 snd_soc_del_component_unlocked(component);
2660 }
2661 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002662}
2663EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2664
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002665/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002666int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2667 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002668{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002669 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002670 int ret;
2671
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302672 if (!card->dev) {
2673 pr_err("card->dev is not set before calling %s\n", __func__);
2674 return -EINVAL;
2675 }
2676
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002677 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302678
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002679 ret = of_property_read_string_index(np, propname, 0, &card->name);
2680 /*
2681 * EINVAL means the property does not exist. This is fine providing
2682 * card->name was previously set, which is checked later in
2683 * snd_soc_register_card.
2684 */
2685 if (ret < 0 && ret != -EINVAL) {
2686 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002687 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002688 propname, ret);
2689 return ret;
2690 }
2691
2692 return 0;
2693}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002694EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002695
Xiubo Li9a6d4862014-02-08 15:59:52 +08002696static const struct snd_soc_dapm_widget simple_widgets[] = {
2697 SND_SOC_DAPM_MIC("Microphone", NULL),
2698 SND_SOC_DAPM_LINE("Line", NULL),
2699 SND_SOC_DAPM_HP("Headphone", NULL),
2700 SND_SOC_DAPM_SPK("Speaker", NULL),
2701};
2702
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002703int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002704 const char *propname)
2705{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002706 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002707 struct snd_soc_dapm_widget *widgets;
2708 const char *template, *wname;
2709 int i, j, num_widgets, ret;
2710
2711 num_widgets = of_property_count_strings(np, propname);
2712 if (num_widgets < 0) {
2713 dev_err(card->dev,
2714 "ASoC: Property '%s' does not exist\n", propname);
2715 return -EINVAL;
2716 }
2717 if (num_widgets & 1) {
2718 dev_err(card->dev,
2719 "ASoC: Property '%s' length is not even\n", propname);
2720 return -EINVAL;
2721 }
2722
2723 num_widgets /= 2;
2724 if (!num_widgets) {
2725 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2726 propname);
2727 return -EINVAL;
2728 }
2729
2730 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2731 GFP_KERNEL);
2732 if (!widgets) {
2733 dev_err(card->dev,
2734 "ASoC: Could not allocate memory for widgets\n");
2735 return -ENOMEM;
2736 }
2737
2738 for (i = 0; i < num_widgets; i++) {
2739 ret = of_property_read_string_index(np, propname,
2740 2 * i, &template);
2741 if (ret) {
2742 dev_err(card->dev,
2743 "ASoC: Property '%s' index %d read error:%d\n",
2744 propname, 2 * i, ret);
2745 return -EINVAL;
2746 }
2747
2748 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2749 if (!strncmp(template, simple_widgets[j].name,
2750 strlen(simple_widgets[j].name))) {
2751 widgets[i] = simple_widgets[j];
2752 break;
2753 }
2754 }
2755
2756 if (j >= ARRAY_SIZE(simple_widgets)) {
2757 dev_err(card->dev,
2758 "ASoC: DAPM widget '%s' is not supported\n",
2759 template);
2760 return -EINVAL;
2761 }
2762
2763 ret = of_property_read_string_index(np, propname,
2764 (2 * i) + 1,
2765 &wname);
2766 if (ret) {
2767 dev_err(card->dev,
2768 "ASoC: Property '%s' index %d read error:%d\n",
2769 propname, (2 * i) + 1, ret);
2770 return -EINVAL;
2771 }
2772
2773 widgets[i].name = wname;
2774 }
2775
Nicolin Chenf23e8602015-02-14 17:22:49 -08002776 card->of_dapm_widgets = widgets;
2777 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002778
2779 return 0;
2780}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002781EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08002782
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002783int snd_soc_of_get_slot_mask(struct device_node *np,
2784 const char *prop_name,
2785 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03002786{
2787 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03002788 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03002789 int i;
2790
2791 if (!of_slot_mask)
2792 return 0;
2793 val /= sizeof(u32);
2794 for (i = 0; i < val; i++)
2795 if (be32_to_cpup(&of_slot_mask[i]))
2796 *mask |= (1 << i);
2797
2798 return val;
2799}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002800EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03002801
Xiubo Li89c67852014-02-14 09:34:35 +08002802int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03002803 unsigned int *tx_mask,
2804 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08002805 unsigned int *slots,
2806 unsigned int *slot_width)
2807{
2808 u32 val;
2809 int ret;
2810
Jyri Sarha61310842015-09-09 21:27:43 +03002811 if (tx_mask)
2812 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2813 if (rx_mask)
2814 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2815
Xiubo Li89c67852014-02-14 09:34:35 +08002816 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2817 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2818 if (ret)
2819 return ret;
2820
2821 if (slots)
2822 *slots = val;
2823 }
2824
2825 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2826 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2827 if (ret)
2828 return ret;
2829
2830 if (slot_width)
2831 *slot_width = val;
2832 }
2833
2834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2837
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002838void snd_soc_of_parse_node_prefix(struct device_node *np,
2839 struct snd_soc_codec_conf *codec_conf,
2840 struct device_node *of_node,
2841 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002842{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002843 const char *str;
2844 int ret;
2845
2846 ret = of_property_read_string(np, propname, &str);
2847 if (ret < 0) {
2848 /* no prefix is not error */
2849 return;
2850 }
2851
Kuninori Morimotoc13493a2019-12-13 09:54:36 +09002852 codec_conf->dlc.of_node = of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002853 codec_conf->name_prefix = str;
2854}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002855EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00002856
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002857int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002858 const char *propname)
2859{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002860 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00002861 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002862 struct snd_soc_dapm_route *routes;
2863 int i, ret;
2864
2865 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08002866 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002867 dev_err(card->dev,
2868 "ASoC: Property '%s' does not exist or its length is not even\n",
2869 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002870 return -EINVAL;
2871 }
2872 num_routes /= 2;
2873 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002874 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002875 propname);
2876 return -EINVAL;
2877 }
2878
Kees Cooka86854d2018-06-12 14:07:58 -07002879 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002880 GFP_KERNEL);
2881 if (!routes) {
2882 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002883 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002884 return -EINVAL;
2885 }
2886
2887 for (i = 0; i < num_routes; i++) {
2888 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002889 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002890 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09002891 dev_err(card->dev,
2892 "ASoC: Property '%s' index %d could not be read: %d\n",
2893 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002894 return -EINVAL;
2895 }
2896 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00002897 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002898 if (ret) {
2899 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09002900 "ASoC: Property '%s' index %d could not be read: %d\n",
2901 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002902 return -EINVAL;
2903 }
2904 }
2905
Nicolin Chenf23e8602015-02-14 17:22:49 -08002906 card->num_of_dapm_routes = num_routes;
2907 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002908
2909 return 0;
2910}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00002911EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07002912
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002913unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02002914 const char *prefix,
2915 struct device_node **bitclkmaster,
2916 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002917{
2918 int ret, i;
2919 char prop[128];
2920 unsigned int format = 0;
2921 int bit, frame;
2922 const char *str;
2923 struct {
2924 char *name;
2925 unsigned int val;
2926 } of_fmt_table[] = {
2927 { "i2s", SND_SOC_DAIFMT_I2S },
2928 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
2929 { "left_j", SND_SOC_DAIFMT_LEFT_J },
2930 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
2931 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
2932 { "ac97", SND_SOC_DAIFMT_AC97 },
2933 { "pdm", SND_SOC_DAIFMT_PDM},
2934 { "msb", SND_SOC_DAIFMT_MSB },
2935 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002936 };
2937
2938 if (!prefix)
2939 prefix = "";
2940
2941 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002942 * check "dai-format = xxx"
2943 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002944 * SND_SOC_DAIFMT_FORMAT_MASK area
2945 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00002946 ret = of_property_read_string(np, "dai-format", &str);
2947 if (ret < 0) {
2948 snprintf(prop, sizeof(prop), "%sformat", prefix);
2949 ret = of_property_read_string(np, prop, &str);
2950 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002951 if (ret == 0) {
2952 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
2953 if (strcmp(str, of_fmt_table[i].name) == 0) {
2954 format |= of_fmt_table[i].val;
2955 break;
2956 }
2957 }
2958 }
2959
2960 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002961 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002962 * SND_SOC_DAIFMT_CLOCK_MASK area
2963 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002964 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02002965 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08002966 format |= SND_SOC_DAIFMT_CONT;
2967 else
2968 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08002969
2970 /*
2971 * check "[prefix]bitclock-inversion"
2972 * check "[prefix]frame-inversion"
2973 * SND_SOC_DAIFMT_INV_MASK area
2974 */
2975 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
2976 bit = !!of_get_property(np, prop, NULL);
2977
2978 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
2979 frame = !!of_get_property(np, prop, NULL);
2980
2981 switch ((bit << 4) + frame) {
2982 case 0x11:
2983 format |= SND_SOC_DAIFMT_IB_IF;
2984 break;
2985 case 0x10:
2986 format |= SND_SOC_DAIFMT_IB_NF;
2987 break;
2988 case 0x01:
2989 format |= SND_SOC_DAIFMT_NB_IF;
2990 break;
2991 default:
2992 /* SND_SOC_DAIFMT_NB_NF is default */
2993 break;
2994 }
2995
2996 /*
2997 * check "[prefix]bitclock-master"
2998 * check "[prefix]frame-master"
2999 * SND_SOC_DAIFMT_MASTER_MASK area
3000 */
3001 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3002 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003003 if (bit && bitclkmaster)
3004 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003005
3006 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3007 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003008 if (frame && framemaster)
3009 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003010
3011 switch ((bit << 4) + frame) {
3012 case 0x11:
3013 format |= SND_SOC_DAIFMT_CBM_CFM;
3014 break;
3015 case 0x10:
3016 format |= SND_SOC_DAIFMT_CBM_CFS;
3017 break;
3018 case 0x01:
3019 format |= SND_SOC_DAIFMT_CBS_CFM;
3020 break;
3021 default:
3022 format |= SND_SOC_DAIFMT_CBS_CFS;
3023 break;
3024 }
3025
3026 return format;
3027}
3028EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3029
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003030int snd_soc_get_dai_id(struct device_node *ep)
3031{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003032 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003033 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003034 int ret;
3035
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003036 dlc.of_node = of_graph_get_port_parent(ep);
3037 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003038 /*
3039 * For example HDMI case, HDMI has video/sound port,
3040 * but ALSA SoC needs sound port number only.
3041 * Thus counting HDMI DT port/endpoint doesn't work.
3042 * Then, it should have .of_xlate_dai_id
3043 */
3044 ret = -ENOTSUPP;
3045 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003046 component = soc_find_component(&dlc);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +09003047 if (component)
3048 ret = snd_soc_component_of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003049 mutex_unlock(&client_mutex);
3050
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003051 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003052
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003053 return ret;
3054}
3055EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3056
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003057int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003058 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003059{
3060 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003061 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003062 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003063
3064 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003065 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003066 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003067
3068 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003069 continue;
3070
Kuninori Morimotoa2a34172019-07-26 13:51:31 +09003071 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3072 if (ret == -ENOTSUPP) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003073 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003074 int id = -1;
3075
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003076 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003077 case 0:
3078 id = 0; /* same as dai_drv[0] */
3079 break;
3080 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003081 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003082 break;
3083 default:
3084 /* not supported */
3085 break;
3086 }
3087
3088 if (id < 0 || id >= pos->num_dai) {
3089 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003090 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003091 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003092
3093 ret = 0;
3094
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003095 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003096 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003097 if (id == 0)
3098 break;
3099 id--;
3100 }
3101
3102 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003103 if (!*dai_name)
3104 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003105 }
3106
Kuninori Morimotocb470082013-09-10 17:39:56 -07003107 break;
3108 }
3109 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003110 return ret;
3111}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003112EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003113
3114int snd_soc_of_get_dai_name(struct device_node *of_node,
3115 const char **dai_name)
3116{
3117 struct of_phandle_args args;
3118 int ret;
3119
3120 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3121 "#sound-dai-cells", 0, &args);
3122 if (ret)
3123 return ret;
3124
3125 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003126
3127 of_node_put(args.np);
3128
3129 return ret;
3130}
3131EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3132
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003133/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003134 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3135 * @dai_link: DAI link
3136 *
3137 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3138 */
3139void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3140{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003141 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003142 int index;
3143
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003144 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003145 if (!component->of_node)
3146 break;
3147 of_node_put(component->of_node);
3148 component->of_node = NULL;
3149 }
3150}
3151EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3152
3153/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003154 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3155 * @dev: Card device
3156 * @of_node: Device node
3157 * @dai_link: DAI link
3158 *
3159 * Builds an array of CODEC DAI components from the DAI link property
3160 * 'sound-dai'.
3161 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003162 * The device nodes in the array (of_node) must be dereferenced by calling
3163 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003164 *
3165 * Returns 0 for success
3166 */
3167int snd_soc_of_get_dai_link_codecs(struct device *dev,
3168 struct device_node *of_node,
3169 struct snd_soc_dai_link *dai_link)
3170{
3171 struct of_phandle_args args;
3172 struct snd_soc_dai_link_component *component;
3173 char *name;
3174 int index, num_codecs, ret;
3175
3176 /* Count the number of CODECs */
3177 name = "sound-dai";
3178 num_codecs = of_count_phandle_with_args(of_node, name,
3179 "#sound-dai-cells");
3180 if (num_codecs <= 0) {
3181 if (num_codecs == -ENOENT)
3182 dev_err(dev, "No 'sound-dai' property\n");
3183 else
3184 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3185 return num_codecs;
3186 }
Kees Cooka86854d2018-06-12 14:07:58 -07003187 component = devm_kcalloc(dev,
3188 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003189 GFP_KERNEL);
3190 if (!component)
3191 return -ENOMEM;
3192 dai_link->codecs = component;
3193 dai_link->num_codecs = num_codecs;
3194
3195 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003196 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003197 ret = of_parse_phandle_with_args(of_node, name,
3198 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003199 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003200 if (ret)
3201 goto err;
3202 component->of_node = args.np;
3203 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3204 if (ret < 0)
3205 goto err;
3206 }
3207 return 0;
3208err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003209 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003210 dai_link->codecs = NULL;
3211 dai_link->num_codecs = 0;
3212 return ret;
3213}
3214EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3215
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003216static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003217{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003218 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003219 snd_soc_util_init();
3220
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003221 return platform_driver_register(&soc_driver);
3222}
Mark Brown4abe8e12010-10-12 17:41:03 +01003223module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003224
Mark Brown7d8c16a2008-11-30 22:11:24 +00003225static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003226{
Mark Brownfb257892011-04-28 10:57:54 +01003227 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003228 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003229
Mark Brown3ff3f642008-05-19 12:32:25 +02003230 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003231}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003232module_exit(snd_soc_exit);
3233
3234/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003235MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003236MODULE_DESCRIPTION("ALSA SoC Core");
3237MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003238MODULE_ALIAS("platform:soc-audio");