blob: 11cbd7915154d5b4d39ab5919db8486308b4feb8 [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
Kuninori Morimoto587c9842019-06-06 13:07:42 +090061 * This is used if driver don't need to have CPU/Codec/Platform
62 * dai_link. see soc.h
63 */
64struct snd_soc_dai_link_component null_dailink_component[0];
65EXPORT_SYMBOL_GPL(null_dailink_component);
66
67/*
Frank Mandarinodb2a4162006-10-06 18:31:09 +020068 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
71 */
72static int pmdown_time = 5000;
73module_param(pmdown_time, int, 0);
74MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
75
YueHaibing0faf1232019-08-13 22:25:01 +080076#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020077/*
78 * If a DMI filed contain strings in this blacklist (e.g.
79 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080080 * as invalid and dropped when setting the card long name from DMI info.
81 */
82static const char * const dmi_blacklist[] = {
83 "To be filled by OEM",
84 "TBD by OEM",
85 "Default String",
86 "Board Manufacturer",
87 "Board Vendor Name",
88 "Board Product Name",
89 NULL, /* terminator */
90};
YueHaibing0faf1232019-08-13 22:25:01 +080091#endif
Mengdong Lin98faf432017-06-28 15:01:39 +080092
Mark Browndbe21402010-02-12 11:37:24 +000093static ssize_t pmdown_time_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000097
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000098 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000099}
100
101static ssize_t pmdown_time_set(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
104{
Mark Brown36ae1a92012-01-06 17:12:45 -0800105 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700106 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000107
Jingoo Hanb785a492013-07-19 16:24:59 +0900108 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700109 if (ret)
110 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000111
112 return count;
113}
114
115static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
116
Takashi Iwaid29697d2015-01-30 20:16:37 +0100117static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100118 &dev_attr_pmdown_time.attr,
119 NULL
120};
121
122static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
123 struct attribute *attr, int idx)
124{
125 struct device *dev = kobj_to_dev(kobj);
126 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
127
Kuninori Morimotod918a372019-09-12 13:42:30 +0900128 if (!rtd)
129 return 0;
130
Takashi Iwaid29697d2015-01-30 20:16:37 +0100131 if (attr == &dev_attr_pmdown_time.attr)
132 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000133 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100134}
135
136static const struct attribute_group soc_dapm_dev_group = {
137 .attrs = soc_dapm_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
Mark Brownf7e73b262018-03-09 12:46:27 +0000141static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100142 .attrs = soc_dev_attrs,
143 .is_visible = soc_dev_attr_is_visible,
144};
145
146static const struct attribute_group *soc_dev_attr_groups[] = {
147 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000148 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100149 NULL
150};
151
Mark Brown2624d5f2009-11-03 21:56:13 +0000152#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100154{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200155 if (!component->card->debugfs_card_root)
156 return;
157
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200158 if (component->debugfs_prefix) {
159 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 name = kasprintf(GFP_KERNEL, "%s:%s",
162 component->debugfs_prefix, component->name);
163 if (name) {
164 component->debugfs_root = debugfs_create_dir(name,
165 component->card->debugfs_card_root);
166 kfree(name);
167 }
168 } else {
169 component->debugfs_root = debugfs_create_dir(component->name,
170 component->card->debugfs_card_root);
171 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100172
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200173 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
174 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200175}
176
177static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
178{
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900179 if (!component->debugfs_root)
180 return;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200181 debugfs_remove_recursive(component->debugfs_root);
Kuninori Morimotoad64bfb2019-08-07 10:30:13 +0900182 component->debugfs_root = NULL;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200183}
184
Peng Donglinc15b2a12018-02-14 22:48:07 +0800185static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100186{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100187 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100188 struct snd_soc_dai *dai;
189
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100190 mutex_lock(&client_mutex);
191
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000192 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000193 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800194 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100195
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100196 mutex_unlock(&client_mutex);
197
Donglin Peng700c17c2018-01-18 13:31:26 +0800198 return 0;
199}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800200DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100201
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000202static int component_list_show(struct seq_file *m, void *v)
203{
204 struct snd_soc_component *component;
205
206 mutex_lock(&client_mutex);
207
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000208 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000209 seq_printf(m, "%s\n", component->name);
210
211 mutex_unlock(&client_mutex);
212
213 return 0;
214}
215DEFINE_SHOW_ATTRIBUTE(component_list);
216
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200217static void soc_init_card_debugfs(struct snd_soc_card *card)
218{
219 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000220 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200221
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200222 debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
223 &card->pop_time);
Kuninori Morimotod8ca7a02019-08-07 10:31:14 +0900224
225 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200226}
227
228static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
229{
230 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900231 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200237
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200238 debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239 &dai_list_fops);
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000240
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +0200241 debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
242 &component_list_fops);
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243}
244
245static void snd_soc_debugfs_exit(void)
246{
247 debugfs_remove_recursive(snd_soc_debugfs_root);
248}
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250#else
251
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200252static inline void soc_init_component_debugfs(
253 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000254{
255}
256
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200257static inline void soc_cleanup_component_debugfs(
258 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000259{
260}
261
Axel Linb95fccb2010-11-09 17:06:44 +0800262static inline void soc_init_card_debugfs(struct snd_soc_card *card)
263{
264}
265
266static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
267{
268}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200269
270static inline void snd_soc_debugfs_init(void)
271{
272}
273
274static inline void snd_soc_debugfs_exit(void)
275{
276}
277
Mark Brown2624d5f2009-11-03 21:56:13 +0000278#endif
279
Kuninori Morimoto8ec241c2019-10-02 14:31:06 +0900280/*
281 * This is glue code between snd_pcm_lib_ioctl() and
282 * snd_soc_component_driver :: ioctl
283 */
284int snd_soc_pcm_lib_ioctl(struct snd_soc_component *component,
285 struct snd_pcm_substream *substream,
286 unsigned int cmd, void *arg)
287{
288 return snd_pcm_lib_ioctl(substream, cmd, arg);
289}
290EXPORT_SYMBOL_GPL(snd_soc_pcm_lib_ioctl);
291
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000292static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
293 struct snd_soc_component *component)
294{
295 struct snd_soc_rtdcom_list *rtdcom;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900296 struct snd_soc_component *comp;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000297
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900298 for_each_rtd_components(rtd, rtdcom, comp) {
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000299 /* already connected */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900300 if (comp == component)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000301 return 0;
302 }
303
Kuninori Morimoto353e16bf2019-09-12 13:42:44 +0900304 /*
305 * created rtdcom here will be freed when rtd->dev was freed.
306 * see
307 * soc_free_pcm_runtime() :: device_unregister(rtd->dev)
308 */
309 rtdcom = devm_kzalloc(rtd->dev, sizeof(*rtdcom), GFP_KERNEL);
Kuninori Morimoto32d2c172019-08-07 10:30:47 +0900310 if (!rtdcom)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000311 return -ENOMEM;
312
Kuninori Morimoto32d2c172019-08-07 10:30:47 +0900313 rtdcom->component = component;
314 INIT_LIST_HEAD(&rtdcom->list);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000315
Kuninori Morimoto353e16bf2019-09-12 13:42:44 +0900316 /*
317 * When rtd was freed, created rtdcom here will be
318 * also freed.
319 * And we don't need to call list_del(&rtdcom->list)
320 * when freed, because rtd is also freed.
321 */
Kuninori Morimoto32d2c172019-08-07 10:30:47 +0900322 list_add_tail(&rtdcom->list, &rtd->component_list);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000323
324 return 0;
325}
326
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000327struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
328 const char *driver_name)
329{
330 struct snd_soc_rtdcom_list *rtdcom;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900331 struct snd_soc_component *component;
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000332
Kuninori Morimoto971da242018-01-23 00:41:24 +0000333 if (!driver_name)
334 return NULL;
335
Kuninori Morimotoa33c0d12019-08-20 14:05:02 +0900336 /*
337 * NOTE
338 *
339 * snd_soc_rtdcom_lookup() will find component from rtd by using
340 * specified driver name.
341 * But, if many components which have same driver name are connected
342 * to 1 rtd, this function will return 1st found component.
343 */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900344 for_each_rtd_components(rtd, rtdcom, component) {
345 const char *component_name = component->driver->name;
Kuninori Morimoto971da242018-01-23 00:41:24 +0000346
347 if (!component_name)
348 continue;
349
350 if ((component_name == driver_name) ||
351 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000352 return rtdcom->component;
353 }
354
355 return NULL;
356}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000357EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000358
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900359static struct snd_soc_component
360*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
Kuninori Morimotob8132652019-11-05 15:46:30 +0900361{
362 struct snd_soc_component *component;
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900363 struct snd_soc_component *found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900364
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900365 found_component = NULL;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900366 for_each_component(component) {
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900367 if ((dev == component->dev) &&
368 (!driver_name ||
369 (driver_name == component->driver->name) ||
370 (strcmp(component->driver->name, driver_name) == 0))) {
371 found_component = component;
372 break;
373 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900374 }
Kuninori Morimotob8132652019-11-05 15:46:30 +0900375
Kuninori Morimoto5bd7e082019-11-05 15:46:35 +0900376 return found_component;
Kuninori Morimotob8132652019-11-05 15:46:30 +0900377}
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +0900378
379struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
380 const char *driver_name)
381{
382 struct snd_soc_component *component;
383
384 mutex_lock(&client_mutex);
385 component = snd_soc_lookup_component_nolocked(dev, driver_name);
386 mutex_unlock(&client_mutex);
387
388 return component;
389}
Kuninori Morimotob8132652019-11-05 15:46:30 +0900390EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
391
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100392struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
393 const char *dai_link, int stream)
394{
Mengdong Lin1a497982015-11-18 02:34:11 -0500395 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100396
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000397 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500398 if (rtd->dai_link->no_pcm &&
399 !strcmp(rtd->dai_link->name, dai_link))
400 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100401 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000402 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100403 return NULL;
404}
405EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
406
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000407static const struct snd_soc_ops null_snd_soc_ops;
408
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900409static void soc_release_rtd_dev(struct device *dev)
410{
411 /* "dev" means "rtd->dev" */
412 kfree(dev);
413}
414
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900415static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
416{
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900417 if (!rtd)
418 return;
419
Kuninori Morimoto753ace02019-09-12 13:38:50 +0900420 list_del(&rtd->list);
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900421
422 /*
423 * we don't need to call kfree() for rtd->dev
424 * see
425 * soc_release_rtd_dev()
Kuninori Morimotod918a372019-09-12 13:42:30 +0900426 *
427 * We don't need rtd->dev NULL check, because
428 * it is alloced *before* rtd.
429 * see
430 * soc_new_pcm_runtime()
Kuninori Morimotob7c5bc42019-09-12 13:41:01 +0900431 */
Kuninori Morimotod918a372019-09-12 13:42:30 +0900432 device_unregister(rtd->dev);
Kuninori Morimoto1c93a9e2019-09-12 13:38:22 +0900433}
434
Mengdong Lin1a497982015-11-18 02:34:11 -0500435static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
436 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
437{
438 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod918a372019-09-12 13:42:30 +0900439 struct device *dev;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900440 int ret;
Mengdong Lin1a497982015-11-18 02:34:11 -0500441
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900442 /*
Kuninori Morimotod918a372019-09-12 13:42:30 +0900443 * for rtd->dev
444 */
445 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
446 if (!dev)
447 return NULL;
448
449 dev->parent = card->dev;
450 dev->release = soc_release_rtd_dev;
451 dev->groups = soc_dev_attr_groups;
452
453 dev_set_name(dev, "%s", dai_link->name);
454
455 ret = device_register(dev);
456 if (ret < 0) {
457 put_device(dev); /* soc_release_rtd_dev */
458 return NULL;
459 }
460
461 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900462 * for rtd
463 */
Kuninori Morimoto4dc0e7d2019-10-02 14:22:32 +0900464 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
Mengdong Lin1a497982015-11-18 02:34:11 -0500465 if (!rtd)
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900466 goto free_rtd;
Mengdong Lin1a497982015-11-18 02:34:11 -0500467
Kuninori Morimotod918a372019-09-12 13:42:30 +0900468 rtd->dev = dev;
469 dev_set_drvdata(dev, rtd);
470
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900471 /*
472 * for rtd->codec_dais
473 */
Kuninori Morimoto4dc0e7d2019-10-02 14:22:32 +0900474 rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs,
Kees Cook6396bb22018-06-12 14:03:40 -0700475 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500476 GFP_KERNEL);
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900477 if (!rtd->codec_dais)
478 goto free_rtd;
479
480 /*
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900481 * rtd remaining settings
482 */
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900483 INIT_LIST_HEAD(&rtd->component_list);
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900484 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
485 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
486 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
487 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
488
Kuninori Morimoto929deb82019-09-12 13:39:32 +0900489 rtd->card = card;
490 rtd->dai_link = dai_link;
491 if (!rtd->dai_link->ops)
492 rtd->dai_link->ops = &null_snd_soc_ops;
493
Kuninori Morimoto6634e3d2019-08-07 10:30:31 +0900494 /* see for_each_card_rtds */
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 list_add_tail(&rtd->list, &card->rtd_list);
496 rtd->num = card->num_rtd;
497 card->num_rtd++;
Kuninori Morimotoa8481252019-09-12 13:38:34 +0900498
499 return rtd;
Kuninori Morimoto6e864342019-09-12 13:40:08 +0900500
501free_rtd:
502 soc_free_pcm_runtime(rtd);
503 return NULL;
Mengdong Lin1a497982015-11-18 02:34:11 -0500504}
505
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100506struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
507 const char *dai_link)
508{
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100510
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000511 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500512 if (!strcmp(rtd->dai_link->name, dai_link))
513 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100514 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000515 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100516 return NULL;
517}
518EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
519
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900520static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
521{
522 struct snd_soc_pcm_runtime *rtd;
523
524 for_each_card_rtds(card, rtd)
525 flush_delayed_work(&rtd->delayed_work);
526}
527
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000528#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000530int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000533 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500534 struct snd_soc_pcm_runtime *rtd;
535 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200537 /* If the card is not initialized yet there is nothing to do */
538 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200539 return 0;
540
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200541 /*
542 * Due to the resume being scheduled into a workqueue we could
543 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100544 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100546
547 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100549
Mark Browna00f90f2010-12-02 16:24:24 +0000550 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000551 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000552 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100553
Mengdong Lin1a497982015-11-18 02:34:11 -0500554 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100555 continue;
556
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000557 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900558 if (dai->playback_active)
559 snd_soc_dai_digital_mute(dai, 1,
560 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200561 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562 }
563
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100564 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000565 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500566 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100567 continue;
568
Mengdong Lin1a497982015-11-18 02:34:11 -0500569 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100570 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100571
Mark Brown87506542008-11-18 20:50:34 +0000572 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000573 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000575 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500576 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100577
Mengdong Lin1a497982015-11-18 02:34:11 -0500578 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100579 continue;
580
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900581 if (!cpu_dai->driver->bus_control)
582 snd_soc_dai_suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200585 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900586 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100587
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000588 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589
Mengdong Lin1a497982015-11-18 02:34:11 -0500590 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100591 continue;
592
Mengdong Lin1a497982015-11-18 02:34:11 -0500593 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800594 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800595 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596
Mengdong Lin1a497982015-11-18 02:34:11 -0500597 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800598 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800599 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 }
601
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200602 /* Recheck all endpoints too, their state is affected by suspend */
603 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700604 snd_soc_dapm_sync(&card->dapm);
605
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000606 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000607 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200608 struct snd_soc_dapm_context *dapm =
609 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000610
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200611 /*
612 * If there are paths active then the COMPONENT will be held
613 * with bias _ON and should not be suspended.
614 */
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900615 if (!snd_soc_component_is_suspended(component)) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200616 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000618 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000619 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000620 * bias off then being in STANDBY
621 * means it's doing something,
622 * otherwise fall through.
623 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200624 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000625 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200626 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000627 break;
628 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500629 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 case SND_SOC_BIAS_OFF:
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900632 snd_soc_component_suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000633 if (component->regmap)
634 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800635 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000636 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 break;
638 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000639 dev_dbg(component->dev,
640 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 break;
642 }
643 }
644 }
645
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000646 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500647 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648
Mengdong Lin1a497982015-11-18 02:34:11 -0500649 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 continue;
651
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900652 if (cpu_dai->driver->bus_control)
653 snd_soc_dai_suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800654
655 /* deactivate pins to sleep state */
656 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657 }
658
Mark Brown87506542008-11-18 20:50:34 +0000659 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000660 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661
662 return 0;
663}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000664EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200666/*
667 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100668 * setting our codec back up, which can be very slow on I2C
669 */
670static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200673 container_of(work, struct snd_soc_card,
674 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500675 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000676 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500677 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200679 /*
680 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100681 * so userspace apps are blocked from touching us
682 */
683
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000684 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100685
Mark Brown9949788b2010-05-07 20:24:05 +0100686 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100688
Mark Brown87506542008-11-18 20:50:34 +0000689 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000690 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100692 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000693 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500694 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100695
Mengdong Lin1a497982015-11-18 02:34:11 -0500696 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100697 continue;
698
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900699 if (cpu_dai->driver->bus_control)
700 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 }
702
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000703 for_each_card_components(card, component) {
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900704 if (snd_soc_component_is_suspended(component))
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900705 snd_soc_component_resume(component);
Mark Brown1547aba2010-05-07 21:11:40 +0100706 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000708 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100709
Mengdong Lin1a497982015-11-18 02:34:11 -0500710 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100711 continue;
712
Mengdong Lin1a497982015-11-18 02:34:11 -0500713 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000714 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800715 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716
Mengdong Lin1a497982015-11-18 02:34:11 -0500717 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000718 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800719 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720 }
721
Mark Brown3ff3f642008-05-19 12:32:25 +0200722 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000723 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000724 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100725
Mengdong Lin1a497982015-11-18 02:34:11 -0500726 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100727 continue;
728
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000729 for_each_rtd_codec_dai(rtd, i, dai) {
Kuninori Morimoto88fdffa2019-07-22 10:36:27 +0900730 if (dai->playback_active)
731 snd_soc_dai_digital_mute(dai, 0,
732 SNDRV_PCM_STREAM_PLAYBACK);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200733 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 }
735
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000736 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500737 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100738
Mengdong Lin1a497982015-11-18 02:34:11 -0500739 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100740 continue;
741
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900742 if (!cpu_dai->driver->bus_control)
743 snd_soc_dai_resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 }
745
Mark Brown87506542008-11-18 20:50:34 +0000746 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000747 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000749 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100750
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200751 /* Recheck all endpoints too, their state is affected by suspend */
752 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700753 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530754
755 /* userspace can access us now we are back as we were before */
756 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100757}
758
759/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000760int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100761{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100763 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500764 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900765 struct snd_soc_dai *codec_dai;
766 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200767
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200768 /* If the card is not initialized yet there is nothing to do */
769 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800770 return 0;
771
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800772 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000773 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200775
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800776 if (cpu_dai->active)
777 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200778
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900779 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200780 if (codec_dai->active)
781 pinctrl_pm_select_default_state(codec_dai->dev);
782 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800783 }
784
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100785 /*
786 * DAIs that also act as the control bus master might have other drivers
787 * hanging off them so need to resume immediately. Other drivers don't
788 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 * due to I/O costs and anti-pop so handle them out of line.
790 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000791 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500792 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200793
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100794 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600795 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100796 if (bus_control) {
797 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600798 soc_resume_deferred(&card->deferred_resume_work);
799 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000800 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600801 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000802 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100803 }
Andy Green6ed25972008-06-13 16:24:05 +0100804
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 return 0;
806}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000807EXPORT_SYMBOL_GPL(snd_soc_resume);
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900808
809static void soc_resume_init(struct snd_soc_card *card)
810{
811 /* deferred resume work */
812 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
813}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000815#define snd_soc_suspend NULL
816#define snd_soc_resume NULL
Kuninori Morimotob3da4252019-08-07 10:31:24 +0900817static inline void soc_resume_init(struct snd_soc_card *card)
818{
819}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820#endif
821
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100822static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800823};
824
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900825static struct device_node
826*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100827{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900828 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100829
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900830 of_node = component->dev->of_node;
831 if (!of_node && component->dev->parent)
832 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100833
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900834 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100835}
836
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000837static int snd_soc_is_matching_component(
838 const struct snd_soc_dai_link_component *dlc,
839 struct snd_soc_component *component)
840{
841 struct device_node *component_of_node;
842
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900843 if (!dlc)
844 return 0;
845
846 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000847
848 if (dlc->of_node && component_of_node != dlc->of_node)
849 return 0;
850 if (dlc->name && strcmp(component->name, dlc->name))
851 return 0;
852
853 return 1;
854}
855
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900857 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858{
859 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860
861 lockdep_assert_held(&client_mutex);
862
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900863 /*
864 * NOTE
865 *
866 * It returns *1st* found component, but some driver
867 * has few components by same of_node/name
868 * ex)
869 * CPU component and generic DMAEngine component
870 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900871 for_each_component(component)
872 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200873 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874
875 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100876}
877
Mengdong Linfbb88b52016-04-22 12:25:33 +0800878/**
879 * snd_soc_find_dai - Find a registered DAI
880 *
Jeffy Chen49584712017-08-22 15:57:21 +0800881 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800882 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700883 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800884 * find the DAI of the same name. The component's of_node and name
885 * should also match if being specified.
886 *
887 * Return: pointer of DAI, or NULL if not found.
888 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800889struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200890 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200892 struct snd_soc_component *component;
893 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100894
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100895 lockdep_assert_held(&client_mutex);
896
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200897 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000898 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000899 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200900 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000901 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800902 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800903 && (!dai->driver->name
904 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100905 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200907 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100908 }
909 }
910
911 return NULL;
912}
Mengdong Lin305e9022016-04-19 13:12:25 +0800913EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100914
Mengdong Lin17fb17552016-11-03 01:04:12 +0800915/**
916 * snd_soc_find_dai_link - Find a DAI link
917 *
918 * @card: soc card
919 * @id: DAI link ID to match
920 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000921 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800922 *
923 * This function will search all existing DAI links of the soc card to
924 * find the link of the same ID. Since DAI links may not have their
925 * unique ID, so name and stream name should also match if being
926 * specified.
927 *
928 * Return: pointer of DAI link, or NULL if not found.
929 */
930struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
931 int id, const char *name,
932 const char *stream_name)
933{
Kuninori Morimoto42849062019-08-07 10:30:41 +0900934 struct snd_soc_dai_link *link;
Mengdong Lin17fb17552016-11-03 01:04:12 +0800935
936 lockdep_assert_held(&client_mutex);
937
Kuninori Morimoto42849062019-08-07 10:30:41 +0900938 for_each_card_links(card, link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800939 if (link->id != id)
940 continue;
941
942 if (name && (!link->name || strcmp(name, link->name)))
943 continue;
944
945 if (stream_name && (!link->stream_name
946 || strcmp(stream_name, link->stream_name)))
947 continue;
948
949 return link;
950 }
951
952 return NULL;
953}
954EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
955
Kuninori Morimotobfce78a2019-11-05 15:45:50 +0900956static int soc_dai_link_sanity_check(struct snd_soc_card *card,
957 struct snd_soc_dai_link *link)
Kuninori Morimoto36794902019-11-05 15:45:41 +0900958{
959 int i;
960 struct snd_soc_dai_link_component *codec, *platform;
961
962 for_each_link_codecs(link, i, codec) {
963 /*
964 * Codec must be specified by 1 of name or OF node,
965 * not both or neither.
966 */
967 if (!!codec->name == !!codec->of_node) {
968 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
969 link->name);
970 return -EINVAL;
971 }
972
973 /* Codec DAI name must be specified */
974 if (!codec->dai_name) {
975 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
976 link->name);
977 return -EINVAL;
978 }
979
980 /*
981 * Defer card registration if codec component is not added to
982 * component list.
983 */
984 if (!soc_find_component(codec))
985 return -EPROBE_DEFER;
986 }
987
988 for_each_link_platforms(link, i, platform) {
989 /*
990 * Platform may be specified by either name or OF node, but it
991 * can be left unspecified, then no components will be inserted
992 * in the rtdcom list
993 */
994 if (!!platform->name == !!platform->of_node) {
995 dev_err(card->dev,
996 "ASoC: Neither/both platform name/of_node are set for %s\n",
997 link->name);
998 return -EINVAL;
999 }
1000
1001 /*
1002 * Defer card registration if platform component is not added to
1003 * component list.
1004 */
1005 if (!soc_find_component(platform))
1006 return -EPROBE_DEFER;
1007 }
1008
1009 /* FIXME */
1010 if (link->num_cpus > 1) {
1011 dev_err(card->dev,
1012 "ASoC: multi cpu is not yet supported %s\n",
1013 link->name);
1014 return -EINVAL;
1015 }
1016
1017 /*
1018 * CPU device may be specified by either name or OF node, but
1019 * can be left unspecified, and will be matched based on DAI
1020 * name alone..
1021 */
1022 if (link->cpus->name && link->cpus->of_node) {
1023 dev_err(card->dev,
1024 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1025 link->name);
1026 return -EINVAL;
1027 }
1028
1029 /*
Kuninori Morimotocd3c5ad2019-11-05 15:46:00 +09001030 * Defer card registration if cpu dai component is not added to
Kuninori Morimoto36794902019-11-05 15:45:41 +09001031 * component list.
1032 */
1033 if ((link->cpus->of_node || link->cpus->name) &&
1034 !soc_find_component(link->cpus))
1035 return -EPROBE_DEFER;
1036
1037 /*
1038 * At least one of CPU DAI name or CPU device name/node must be
1039 * specified
1040 */
1041 if (!link->cpus->dai_name &&
1042 !(link->cpus->name || link->cpus->of_node)) {
1043 dev_err(card->dev,
1044 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1045 link->name);
1046 return -EINVAL;
1047 }
1048
1049 return 0;
1050}
1051
Kuninori Morimotoda704f22019-11-06 10:07:38 +09001052/**
1053 * snd_soc_remove_dai_link - Remove a DAI link from the list
1054 * @card: The ASoC card that owns the link
1055 * @dai_link: The DAI link to remove
1056 *
1057 * This function removes a DAI link from the ASoC card's link list.
1058 *
1059 * For DAI links previously added by topology, topology should
1060 * remove them by using the dobj embedded in the link.
1061 */
1062void snd_soc_remove_dai_link(struct snd_soc_card *card,
1063 struct snd_soc_dai_link *dai_link)
Kuninori Morimotobc7a9092019-11-05 15:46:25 +09001064{
1065 struct snd_soc_pcm_runtime *rtd;
1066
Kuninori Morimotoda704f22019-11-06 10:07:38 +09001067 lockdep_assert_held(&client_mutex);
1068
1069 /*
1070 * Notify the machine driver for extra destruction
1071 */
1072 if (card->remove_dai_link)
1073 card->remove_dai_link(card, dai_link);
1074
1075 list_del(&dai_link->list);
1076
Kuninori Morimotobc7a9092019-11-05 15:46:25 +09001077 rtd = snd_soc_get_pcm_runtime(card, dai_link->name);
1078 if (rtd)
1079 soc_free_pcm_runtime(rtd);
1080}
Kuninori Morimotoda704f22019-11-06 10:07:38 +09001081EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
Kuninori Morimotobc7a9092019-11-05 15:46:25 +09001082
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +09001083/**
1084 * snd_soc_add_dai_link - Add a DAI link dynamically
1085 * @card: The ASoC card to which the DAI link is added
1086 * @dai_link: The new DAI link to add
1087 *
1088 * This function adds a DAI link to the ASoC card's link list.
1089 *
1090 * Note: Topology can use this API to add DAI links when probing the
1091 * topology component. And machine drivers can still define static
1092 * DAI links in dai_link array.
1093 */
1094int snd_soc_add_dai_link(struct snd_soc_card *card,
1095 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001096{
Mengdong Lin1a497982015-11-18 02:34:11 -05001097 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +02001098 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001099 struct snd_soc_component *component;
Kuninori Morimotobfce78a2019-11-05 15:45:50 +09001100 int i, ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001101
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +09001102 lockdep_assert_held(&client_mutex);
1103
1104 /*
1105 * Notify the machine driver for extra initialization
1106 */
1107 if (card->add_dai_link)
1108 card->add_dai_link(card, dai_link);
1109
Liam Girdwooda655de82018-07-02 16:59:54 +01001110 if (dai_link->ignore)
1111 return 0;
1112
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001113 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001114
Kuninori Morimotobfce78a2019-11-05 15:45:50 +09001115 ret = soc_dai_link_sanity_check(card, dai_link);
1116 if (ret < 0)
1117 return ret;
1118
Sudip Mukherjee513cb3112016-02-22 14:14:31 +05301119 rtd = soc_new_pcm_runtime(card, dai_link);
1120 if (!rtd)
1121 return -ENOMEM;
1122
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001123 /* FIXME: we need multi CPU support in the future */
1124 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +00001125 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +01001126 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001127 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001128 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001129 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001130 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001131
Benoit Cousson88bd8702014-07-08 23:19:34 +02001132 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +09001133 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +02001134 for_each_link_codecs(dai_link, i, codec) {
1135 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +09001136 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +01001137 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +02001138 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001139 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001140 }
Jerome Brunet34614732019-06-27 14:13:50 +02001141
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +09001142 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001143 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001144
Benoit Cousson88bd8702014-07-08 23:19:34 +02001145 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +09001146 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001147
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +09001148 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +02001149 for_each_link_platforms(dai_link, i, platform) {
1150 for_each_component(component) {
1151 if (!snd_soc_is_matching_component(platform, component))
1152 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001153
Jerome Brunet34614732019-06-27 14:13:50 +02001154 snd_soc_rtdcom_add(rtd, component);
1155 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001156 }
1157
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +09001158 /* see for_each_card_links */
1159 list_add_tail(&dai_link->list, &card->dai_link_list);
1160
Mark Brownb19e6e72012-03-14 21:18:39 +00001161 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001162
1163_err_defer:
1164 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001165 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166}
Kuninori Morimoto63dc47d2019-11-06 10:07:31 +09001167EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001168
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001169static void soc_set_of_name_prefix(struct snd_soc_component *component)
1170{
1171 struct device_node *of_node = soc_component_to_node(component);
1172 const char *str;
1173 int ret;
1174
1175 ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1176 if (!ret)
1177 component->name_prefix = str;
1178}
1179
1180static void soc_set_name_prefix(struct snd_soc_card *card,
1181 struct snd_soc_component *component)
1182{
1183 int i;
1184
1185 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
1186 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1187 struct device_node *of_node = soc_component_to_node(component);
1188
1189 if (map->of_node && of_node != map->of_node)
1190 continue;
1191 if (map->dev_name && strcmp(component->name, map->dev_name))
1192 continue;
1193 component->name_prefix = map->name_prefix;
1194 return;
1195 }
1196
1197 /*
1198 * If there is no configuration table or no match in the table,
1199 * check if a prefix is provided in the node
1200 */
1201 soc_set_of_name_prefix(component);
1202}
1203
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001204static void soc_remove_component(struct snd_soc_component *component,
1205 int probed)
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001206{
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001207
1208 if (!component->card)
1209 return;
1210
1211 if (probed)
1212 snd_soc_component_remove(component);
1213
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001214 /* For framework level robustness */
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +02001215 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto04f770d2019-09-04 09:14:35 +09001216
Bard liao7a5d9812019-09-18 21:31:31 +08001217 list_del_init(&component->card_list);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001218 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1219 soc_cleanup_component_debugfs(component);
1220 component->card = NULL;
Pierre-Louis Bossart0e36f362019-08-07 21:51:31 -05001221 snd_soc_component_module_put_when_remove(component);
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001222}
1223
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001224static int soc_probe_component(struct snd_soc_card *card,
1225 struct snd_soc_component *component)
1226{
1227 struct snd_soc_dapm_context *dapm =
1228 snd_soc_component_get_dapm(component);
1229 struct snd_soc_dai *dai;
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001230 int probed = 0;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001231 int ret;
1232
1233 if (!strcmp(component->name, "snd-soc-dummy"))
1234 return 0;
1235
1236 if (component->card) {
1237 if (component->card != card) {
1238 dev_err(component->dev,
1239 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1240 card->name, component->card->name);
1241 return -ENODEV;
1242 }
1243 return 0;
1244 }
1245
1246 ret = snd_soc_component_module_get_when_probe(component);
1247 if (ret < 0)
1248 return ret;
1249
1250 component->card = card;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001251 soc_set_name_prefix(card, component);
1252
1253 soc_init_component_debugfs(component);
1254
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001255 snd_soc_dapm_init(dapm, card, component);
Kuninori Morimotob614bea2019-08-23 09:58:47 +09001256
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001257 ret = snd_soc_dapm_new_controls(dapm,
1258 component->driver->dapm_widgets,
1259 component->driver->num_dapm_widgets);
1260
1261 if (ret != 0) {
1262 dev_err(component->dev,
1263 "Failed to create new controls %d\n", ret);
1264 goto err_probe;
1265 }
1266
1267 for_each_component_dais(component, dai) {
1268 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1269 if (ret != 0) {
1270 dev_err(component->dev,
1271 "Failed to create DAI widgets %d\n", ret);
1272 goto err_probe;
1273 }
1274 }
1275
1276 ret = snd_soc_component_probe(component);
1277 if (ret < 0) {
1278 dev_err(component->dev,
1279 "ASoC: failed to probe component %d\n", ret);
1280 goto err_probe;
1281 }
1282 WARN(dapm->idle_bias_off &&
1283 dapm->bias_level != SND_SOC_BIAS_OFF,
1284 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1285 component->name);
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001286 probed = 1;
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001287
1288 /* machine specific init */
1289 if (component->init) {
1290 ret = component->init(component);
1291 if (ret < 0) {
1292 dev_err(component->dev,
1293 "Failed to do machine specific init %d\n", ret);
1294 goto err_probe;
1295 }
1296 }
1297
1298 ret = snd_soc_add_component_controls(component,
1299 component->driver->controls,
1300 component->driver->num_controls);
1301 if (ret < 0)
1302 goto err_probe;
1303
1304 ret = snd_soc_dapm_add_routes(dapm,
1305 component->driver->dapm_routes,
1306 component->driver->num_dapm_routes);
1307 if (ret < 0)
1308 goto err_probe;
1309
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001310 /* see for_each_card_components */
1311 list_add(&component->card_list, &card->component_dev_list);
1312
1313err_probe:
1314 if (ret < 0)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001315 soc_remove_component(component, probed);
Kuninori Morimotoffd60fb2019-08-23 09:58:42 +09001316
1317 return ret;
1318}
1319
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001320static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321{
1322 int err;
1323
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -06001324 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +00001325 dai->driver->remove_order != order)
1326 return;
1327
Kuninori Morimotodcdab582019-07-22 10:35:05 +09001328 err = snd_soc_dai_remove(dai);
1329 if (err < 0)
1330 dev_err(dai->dev,
1331 "ASoC: failed to remove %s: %d\n",
1332 dai->name, err);
1333
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +00001334 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001335}
1336
Kuninori Morimotoa7d44f72019-09-04 09:15:06 +09001337static int soc_probe_dai(struct snd_soc_dai *dai, int order)
1338{
1339 int ret;
1340
1341 if (dai->probed ||
1342 dai->driver->probe_order != order)
1343 return 0;
1344
1345 ret = snd_soc_dai_probe(dai);
1346 if (ret < 0) {
1347 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1348 dai->name, ret);
1349 return ret;
1350 }
1351
1352 dai->probed = 1;
1353
1354 return 0;
1355}
1356
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001357static void soc_remove_link_dais(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001358{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001359 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001360 struct snd_soc_dai *codec_dai;
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001361 struct snd_soc_pcm_runtime *rtd;
1362 int order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001364 for_each_comp_order(order) {
1365 for_each_card_rtds(card, rtd) {
Kuninori Morimoto4ca47d22019-09-04 09:14:57 +09001366 /* remove the CODEC DAI */
1367 for_each_rtd_codec_dai(rtd, i, codec_dai)
1368 soc_remove_dai(codec_dai, order);
1369
1370 soc_remove_dai(rtd->cpu_dai, order);
1371 }
1372 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373}
1374
Kuninori Morimotobc7c16c2019-09-04 09:15:23 +09001375static int soc_probe_link_dais(struct snd_soc_card *card)
1376{
1377 struct snd_soc_dai *codec_dai;
1378 struct snd_soc_pcm_runtime *rtd;
1379 int i, order, ret;
1380
1381 for_each_comp_order(order) {
1382 for_each_card_rtds(card, rtd) {
1383
1384 dev_dbg(card->dev,
1385 "ASoC: probe %s dai link %d late %d\n",
1386 card->name, rtd->num, order);
1387
1388 ret = soc_probe_dai(rtd->cpu_dai, order);
1389 if (ret)
1390 return ret;
1391
1392 /* probe the CODEC DAI */
1393 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1394 ret = soc_probe_dai(codec_dai, order);
1395 if (ret)
1396 return ret;
1397 }
1398 }
1399 }
1400
1401 return 0;
1402}
1403
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001404static void soc_remove_link_components(struct snd_soc_card *card)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001405{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001406 struct snd_soc_component *component;
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001407 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001408 struct snd_soc_rtdcom_list *rtdcom;
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001409 int order;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001410
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001411 for_each_comp_order(order) {
1412 for_each_card_rtds(card, rtd) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001413 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001414 if (component->driver->remove_order != order)
1415 continue;
1416
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001417 soc_remove_component(component, 1);
Kuninori Morimotob006c0c2019-09-04 09:14:51 +09001418 }
1419 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001420 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001421}
1422
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001423static int soc_probe_link_components(struct snd_soc_card *card)
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001424{
1425 struct snd_soc_component *component;
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001426 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001427 struct snd_soc_rtdcom_list *rtdcom;
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001428 int ret, order;
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001429
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001430 for_each_comp_order(order) {
1431 for_each_card_rtds(card, rtd) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001432 for_each_rtd_components(rtd, rtdcom, component) {
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09001433 if (component->driver->probe_order != order)
1434 continue;
1435
1436 ret = soc_probe_component(card, component);
1437 if (ret < 0)
1438 return ret;
1439 }
Kuninori Morimoto6fb03552019-08-23 09:58:58 +09001440 }
1441 }
1442
1443 return 0;
1444}
1445
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001446void snd_soc_disconnect_sync(struct device *dev)
1447{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001448 struct snd_soc_component *component =
1449 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001450
1451 if (!component || !component->card)
1452 return;
1453
1454 snd_card_disconnect_sync(component->card->snd_card);
1455}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001456EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001457
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001458static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1459 struct snd_soc_pcm_runtime *rtd)
1460{
1461 int i, ret = 0;
1462
1463 for (i = 0; i < num_dais; ++i) {
1464 struct snd_soc_dai_driver *drv = dais[i]->driver;
1465
Rohit kumarde17f142018-11-01 18:08:49 +05301466 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001467 ret = drv->pcm_new(rtd, dais[i]);
1468 if (ret < 0) {
1469 dev_err(dais[i]->dev,
1470 "ASoC: Failed to bind %s with pcm device\n",
1471 dais[i]->name);
1472 return ret;
1473 }
1474 }
1475
1476 return 0;
1477}
1478
Kuninori Morimotoc4b46982019-09-04 09:15:12 +09001479static int soc_link_init(struct snd_soc_card *card,
1480 struct snd_soc_pcm_runtime *rtd)
1481{
1482 struct snd_soc_dai_link *dai_link = rtd->dai_link;
1483 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1484 struct snd_soc_rtdcom_list *rtdcom;
1485 struct snd_soc_component *component;
1486 int ret, num;
1487
1488 /* set default power off timeout */
1489 rtd->pmdown_time = pmdown_time;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001490
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001491 /* do machine specific initialization */
1492 if (dai_link->init) {
1493 ret = dai_link->init(rtd);
1494 if (ret < 0) {
1495 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1496 dai_link->name, ret);
1497 return ret;
1498 }
1499 }
1500
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001501 if (dai_link->dai_fmt) {
1502 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1503 if (ret)
1504 return ret;
1505 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001506
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001507 /* add DPCM sysfs entries */
Kuninori Morimoto596becd2019-08-07 10:31:36 +09001508 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001509
Liam Girdwooda655de82018-07-02 16:59:54 +01001510 num = rtd->num;
1511
1512 /*
1513 * most drivers will register their PCMs using DAI link ordering but
1514 * topology based drivers can use the DAI link id field to set PCM
1515 * device number and then use rtd + a base offset of the BEs.
1516 */
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001517 for_each_rtd_components(rtd, rtdcom, component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001518 if (!component->driver->use_dai_pcm_id)
1519 continue;
1520
1521 if (rtd->dai_link->no_pcm)
1522 num += component->driver->be_pcm_base;
1523 else
1524 num = rtd->dai_link->id;
1525 }
1526
Kuninori Morimotob423c422019-07-22 10:35:29 +09001527 /* create compress_device if possible */
1528 ret = snd_soc_dai_compress_new(cpu_dai, rtd, num);
1529 if (ret != -ENOTSUPP) {
1530 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001531 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301532 dai_link->stream_name);
Kuninori Morimotob423c422019-07-22 10:35:29 +09001533 return ret;
1534 }
1535
Jerome Bruneta3420312019-07-25 18:59:47 +02001536 /* create the pcm */
1537 ret = soc_new_pcm(rtd, num);
1538 if (ret < 0) {
1539 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1540 dai_link->stream_name, ret);
1541 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 }
Jerome Bruneta3420312019-07-25 18:59:47 +02001543 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1544 if (ret < 0)
1545 return ret;
1546 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1547 rtd->num_codecs, rtd);
1548 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549}
1550
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001551static void soc_unbind_aux_dev(struct snd_soc_card *card)
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001552{
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001553 struct snd_soc_component *component, *_component;
1554
1555 for_each_card_auxs_safe(card, component, _component) {
1556 component->init = NULL;
1557 list_del(&component->card_aux_list);
1558 }
Kuninori Morimoto4893a2e2019-09-04 09:15:35 +09001559}
1560
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001561static int soc_bind_aux_dev(struct snd_soc_card *card)
Mark Brownb19e6e72012-03-14 21:18:39 +00001562{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001563 struct snd_soc_component *component;
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001564 struct snd_soc_aux_dev *aux;
1565 int i;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001566
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001567 for_each_card_pre_auxs(card, i, aux) {
1568 /* codecs, usually analog devices */
1569 component = soc_find_component(&aux->dlc);
1570 if (!component)
1571 return -EPROBE_DEFER;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001572
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001573 component->init = aux->init;
1574 /* see for_each_card_auxs */
1575 list_add(&component->card_aux_list, &card->aux_comp_list);
1576 }
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001577 return 0;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001578}
1579
1580static int soc_probe_aux_devices(struct snd_soc_card *card)
1581{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001582 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001583 int order;
1584 int ret;
1585
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001586 for_each_comp_order(order) {
Kuninori Morimotoc2b71c72019-08-08 14:54:44 +09001587 for_each_card_auxs(card, comp) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001588 if (comp->driver->probe_order == order) {
1589 ret = soc_probe_component(card, comp);
1590 if (ret < 0) {
1591 dev_err(card->dev,
1592 "ASoC: failed to probe aux component %s %d\n",
1593 comp->name, ret);
1594 return ret;
1595 }
1596 }
1597 }
1598 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001599
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001600 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001601}
1602
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001603static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001604{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001605 struct snd_soc_component *comp, *_comp;
1606 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001607
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001608 for_each_comp_order(order) {
Kuninori Morimotoc2b71c72019-08-08 14:54:44 +09001609 for_each_card_auxs_safe(card, comp, _comp) {
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001610 if (comp->driver->remove_order == order)
Kuninori Morimotoc6619b72019-11-06 10:07:46 +09001611 soc_remove_component(comp, 1);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001612 }
1613 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001614}
1615
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001616/**
1617 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1618 * @rtd: The runtime for which the DAI link format should be changed
1619 * @dai_fmt: The new DAI link format
1620 *
1621 * This function updates the DAI link format for all DAIs connected to the DAI
1622 * link for the specified runtime.
1623 *
1624 * Note: For setups with a static format set the dai_fmt field in the
1625 * corresponding snd_dai_link struct instead of using this function.
1626 *
1627 * Returns 0 on success, otherwise a negative error code.
1628 */
1629int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1630 unsigned int dai_fmt)
1631{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001632 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001633 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001634 unsigned int i;
1635 int ret;
1636
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001637 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001638 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1639 if (ret != 0 && ret != -ENOTSUPP) {
1640 dev_warn(codec_dai->dev,
1641 "ASoC: Failed to set DAI format: %d\n", ret);
1642 return ret;
1643 }
1644 }
1645
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001646 /*
1647 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1648 * the component which has non_legacy_dai_naming is Codec
1649 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001650 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001651 unsigned int inv_dai_fmt;
1652
1653 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1654 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1655 case SND_SOC_DAIFMT_CBM_CFM:
1656 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1657 break;
1658 case SND_SOC_DAIFMT_CBM_CFS:
1659 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1660 break;
1661 case SND_SOC_DAIFMT_CBS_CFM:
1662 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1663 break;
1664 case SND_SOC_DAIFMT_CBS_CFS:
1665 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1666 break;
1667 }
1668
1669 dai_fmt = inv_dai_fmt;
1670 }
1671
1672 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1673 if (ret != 0 && ret != -ENOTSUPP) {
1674 dev_warn(cpu_dai->dev,
1675 "ASoC: Failed to set DAI format: %d\n", ret);
1676 return ret;
1677 }
1678
1679 return 0;
1680}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001681EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001682
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001683#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001684/*
1685 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001686 * separate different DMI fields in the card long name. Only number and
1687 * alphabet characters and a few separator characters are kept.
1688 */
1689static void cleanup_dmi_name(char *name)
1690{
1691 int i, j = 0;
1692
1693 for (i = 0; name[i]; i++) {
1694 if (isalnum(name[i]) || (name[i] == '.')
1695 || (name[i] == '_'))
1696 name[j++] = name[i];
1697 else if (name[i] == '-')
1698 name[j++] = '_';
1699 }
1700
1701 name[j] = '\0';
1702}
1703
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001704/*
1705 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001706 * in the black list.
1707 */
1708static int is_dmi_valid(const char *field)
1709{
1710 int i = 0;
1711
1712 while (dmi_blacklist[i]) {
1713 if (strstr(field, dmi_blacklist[i]))
1714 return 0;
1715 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001716 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001717
1718 return 1;
1719}
1720
Liam Girdwood345233d2017-01-14 16:13:02 +08001721/**
1722 * snd_soc_set_dmi_name() - Register DMI names to card
1723 * @card: The card to register DMI names
1724 * @flavour: The flavour "differentiator" for the card amongst its peers.
1725 *
1726 * An Intel machine driver may be used by many different devices but are
1727 * difficult for userspace to differentiate, since machine drivers ususally
1728 * use their own name as the card short name and leave the card long name
1729 * blank. To differentiate such devices and fix bugs due to lack of
1730 * device-specific configurations, this function allows DMI info to be used
1731 * as the sound card long name, in the format of
1732 * "vendor-product-version-board"
1733 * (Character '-' is used to separate different DMI fields here).
1734 * This will help the user space to load the device-specific Use Case Manager
1735 * (UCM) configurations for the card.
1736 *
1737 * Possible card long names may be:
1738 * DellInc.-XPS139343-01-0310JH
1739 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1740 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1741 *
1742 * This function also supports flavoring the card longname to provide
1743 * the extra differentiation, like "vendor-product-version-board-flavor".
1744 *
1745 * We only keep number and alphabet characters and a few separator characters
1746 * in the card long name since UCM in the user space uses the card long names
1747 * as card configuration directory names and AudoConf cannot support special
1748 * charactors like SPACE.
1749 *
1750 * Returns 0 on success, otherwise a negative error code.
1751 */
1752int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1753{
1754 const char *vendor, *product, *product_version, *board;
1755 size_t longname_buf_size = sizeof(card->snd_card->longname);
1756 size_t len;
1757
1758 if (card->long_name)
1759 return 0; /* long name already set by driver or from DMI */
1760
1761 /* make up dmi long name as: vendor.product.version.board */
1762 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001763 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001764 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1765 return 0;
1766 }
1767
1768 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1769 "%s", vendor);
1770 cleanup_dmi_name(card->dmi_longname);
1771
1772 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001773 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001774 len = strlen(card->dmi_longname);
1775 snprintf(card->dmi_longname + len,
1776 longname_buf_size - len,
1777 "-%s", product);
1778
1779 len++; /* skip the separator "-" */
1780 if (len < longname_buf_size)
1781 cleanup_dmi_name(card->dmi_longname + len);
1782
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001783 /*
1784 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001785 * name in the product version field
1786 */
1787 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001788 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001789 len = strlen(card->dmi_longname);
1790 snprintf(card->dmi_longname + len,
1791 longname_buf_size - len,
1792 "-%s", product_version);
1793
1794 len++;
1795 if (len < longname_buf_size)
1796 cleanup_dmi_name(card->dmi_longname + len);
1797 }
1798 }
1799
1800 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001801 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001802 len = strlen(card->dmi_longname);
1803 snprintf(card->dmi_longname + len,
1804 longname_buf_size - len,
1805 "-%s", board);
1806
1807 len++;
1808 if (len < longname_buf_size)
1809 cleanup_dmi_name(card->dmi_longname + len);
1810 } else if (!product) {
1811 /* fall back to using legacy name */
1812 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1813 return 0;
1814 }
1815
1816 /* Add flavour to dmi long name */
1817 if (flavour) {
1818 len = strlen(card->dmi_longname);
1819 snprintf(card->dmi_longname + len,
1820 longname_buf_size - len,
1821 "-%s", flavour);
1822
1823 len++;
1824 if (len < longname_buf_size)
1825 cleanup_dmi_name(card->dmi_longname + len);
1826 }
1827
1828 /* set the card long name */
1829 card->long_name = card->dmi_longname;
1830
1831 return 0;
1832}
1833EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001834#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001835
Liam Girdwooda655de82018-07-02 16:59:54 +01001836static void soc_check_tplg_fes(struct snd_soc_card *card)
1837{
1838 struct snd_soc_component *component;
1839 const struct snd_soc_component_driver *comp_drv;
1840 struct snd_soc_dai_link *dai_link;
1841 int i;
1842
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001843 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001844
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001845 /* does this component override BEs ? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001846 if (!component->driver->ignore_machine)
1847 continue;
1848
1849 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001850 if (!strcmp(component->driver->ignore_machine,
1851 card->dev->driver->name))
1852 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001853 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001854 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001855 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001856match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001857 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001858 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001859
1860 /* ignore this FE */
1861 if (dai_link->dynamic) {
1862 dai_link->ignore = true;
1863 continue;
1864 }
1865
Daniel Baluta49f9c4f2019-09-25 21:33:58 +03001866 dev_info(card->dev, "info: override BE DAI link %s\n",
Liam Girdwooda655de82018-07-02 16:59:54 +01001867 card->dai_link[i].name);
1868
1869 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001870 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001871 dev_err(card->dev, "init platform error");
1872 continue;
1873 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001874 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001875
1876 /* convert non BE into BE */
1877 dai_link->no_pcm = 1;
1878
1879 /* override any BE fixups */
1880 dai_link->be_hw_params_fixup =
1881 component->driver->be_hw_params_fixup;
1882
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001883 /*
1884 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001885 * dai link name if it's NULL to help bind widgets.
1886 */
1887 if (!dai_link->stream_name)
1888 dai_link->stream_name = dai_link->name;
1889 }
1890
1891 /* Inform userspace we are using alternate topology */
1892 if (component->driver->topology_name_prefix) {
1893
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001894 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001895 if (!card->topology_shortname_created) {
1896 comp_drv = component->driver;
1897
1898 snprintf(card->topology_shortname, 32, "%s-%s",
1899 comp_drv->topology_name_prefix,
1900 card->name);
1901 card->topology_shortname_created = true;
1902 }
1903
1904 /* use topology shortname */
1905 card->name = card->topology_shortname;
1906 }
1907 }
1908}
1909
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09001910#define soc_setup_card_name(name, name1, name2, norm) \
1911 __soc_setup_card_name(name, sizeof(name), name1, name2, norm)
1912static void __soc_setup_card_name(char *name, int len,
1913 const char *name1, const char *name2,
1914 int normalization)
1915{
1916 int i;
1917
1918 snprintf(name, len, "%s", name1 ? name1 : name2);
1919
1920 if (!normalization)
1921 return;
1922
1923 /*
1924 * Name normalization
1925 *
1926 * The driver name is somewhat special, as it's used as a key for
1927 * searches in the user-space.
1928 *
1929 * ex)
1930 * "abcd??efg" -> "abcd__efg"
1931 */
1932 for (i = 0; i < len; i++) {
1933 switch (name[i]) {
1934 case '_':
1935 case '-':
1936 case '\0':
1937 break;
1938 default:
1939 if (!isalnum(name[i]))
1940 name[i] = '_';
1941 break;
1942 }
1943 }
1944}
1945
Kuninori Morimotoa4de83a2019-08-20 14:05:10 +09001946static void soc_cleanup_card_resources(struct snd_soc_card *card)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001947{
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001948 struct snd_soc_dai_link *link, *_link;
1949
Pierre-Louis Bossartdf95a162019-11-06 08:58:01 -06001950 /* This should be called before snd_card_free() */
1951 soc_remove_link_components(card);
1952
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001953 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001954 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001955 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001956 card->snd_card = NULL;
1957 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001958
1959 /* remove and free each DAI */
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001960 soc_remove_link_dais(card);
Kuninori Morimoto7ce60882019-10-02 14:22:40 +09001961
1962 for_each_card_links_safe(card, link, _link)
1963 snd_soc_remove_dai_link(card, link);
1964
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001965 /* remove auxiliary devices */
1966 soc_remove_aux_devices(card);
Kuninori Morimotoe8fbd252019-09-04 09:15:40 +09001967 soc_unbind_aux_dev(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001968
1969 snd_soc_dapm_free(&card->dapm);
1970 soc_cleanup_card_debugfs(card);
1971
1972 /* remove the card */
1973 if (card->remove)
1974 card->remove(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001975}
1976
Mark Brownb19e6e72012-03-14 21:18:39 +00001977static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978{
Mengdong Lin1a497982015-11-18 02:34:11 -05001979 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001980 struct snd_soc_dai_link *dai_link;
Kuninori Morimotoc7e73772019-09-04 09:15:17 +09001981 int ret, i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001983 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001984 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985
Kuninori Morimoto95c267d2019-08-23 09:58:52 +09001986 snd_soc_dapm_init(&card->dapm, card, NULL);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001987
Liam Girdwooda655de82018-07-02 16:59:54 +01001988 /* check whether any platform is ignore machine FE and using topology */
1989 soc_check_tplg_fes(card);
1990
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001991 /* bind aux_devs too */
Kuninori Morimotobee886f2019-09-04 09:15:28 +09001992 ret = soc_bind_aux_dev(card);
1993 if (ret < 0)
1994 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995
Mengdong Linf8f80362015-12-02 14:11:22 +08001996 /* add predefined DAI links to the list */
Kuninori Morimotod8145982019-10-02 14:23:07 +09001997 card->num_rtd = 0;
Kuninori Morimoto5b99a0a2019-08-07 10:30:36 +09001998 for_each_card_prelinks(card, i, dai_link) {
1999 ret = snd_soc_add_dai_link(card, dai_link);
2000 if (ret < 0)
2001 goto probe_end;
2002 }
Mengdong Linf8f80362015-12-02 14:11:22 +08002003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002004 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002005 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002006 card->owner, 0, &card->snd_card);
2007 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002008 dev_err(card->dev,
2009 "ASoC: can't create sound card for card %s: %d\n",
2010 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002011 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002013
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002014 soc_init_card_debugfs(card);
2015
Kuninori Morimotob3da4252019-08-07 10:31:24 +09002016 soc_resume_init(card);
Andy Green6ed25972008-06-13 16:24:05 +01002017
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09002018 ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2019 card->num_dapm_widgets);
2020 if (ret < 0)
2021 goto probe_end;
Mark Brown9a841eb2011-04-12 17:51:37 -07002022
Kuninori Morimotob8ba3b52019-08-07 10:30:53 +09002023 ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2024 card->num_of_dapm_widgets);
2025 if (ret < 0)
2026 goto probe_end;
Nicolin Chenf23e8602015-02-14 17:22:49 -08002027
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002028 /* initialise the sound card only once */
2029 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002030 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002031 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002032 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002033 }
2034
Stephen Warren62ae68f2012-06-08 12:34:23 -06002035 /* probe all components used by DAI links on this card */
Kuninori Morimoto62f07a62019-09-04 09:14:46 +09002036 ret = soc_probe_link_components(card);
2037 if (ret < 0) {
2038 dev_err(card->dev,
2039 "ASoC: failed to instantiate card %d\n", ret);
2040 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002041 }
2042
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002043 /* probe auxiliary components */
2044 ret = soc_probe_aux_devices(card);
2045 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002046 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002047
Stephen Warren62ae68f2012-06-08 12:34:23 -06002048 /* probe all DAI links on this card */
Kuninori Morimotoc7e73772019-09-04 09:15:17 +09002049 ret = soc_probe_link_dais(card);
2050 if (ret < 0) {
2051 dev_err(card->dev,
2052 "ASoC: failed to instantiate card %d\n", ret);
2053 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002054 }
2055
Kuninori Morimotoc4b46982019-09-04 09:15:12 +09002056 for_each_card_rtds(card, rtd)
2057 soc_link_init(card, rtd);
2058
Mark Brown888df392012-02-16 19:37:51 -08002059 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002060 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002061
Kuninori Morimoto9b98c7c2019-08-07 10:31:08 +09002062 ret = snd_soc_add_card_controls(card, card->controls,
2063 card->num_controls);
2064 if (ret < 0)
2065 goto probe_end;
Mark Brownb7af1da2011-04-07 19:18:44 +09002066
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09002067 ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2068 card->num_dapm_routes);
2069 if (ret < 0)
2070 goto probe_end;
Mark Brownb8ad29d2011-03-02 18:35:51 +00002071
Kuninori Morimotodaa480b2019-08-07 10:31:03 +09002072 ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2073 card->num_of_dapm_routes);
2074 if (ret < 0)
2075 goto probe_end;
Mark Brown75d9ac42011-09-27 16:41:01 +01002076
Takashi Iwai861886d2017-04-24 08:54:42 +02002077 /* try to set some sane longname if DMI is available */
2078 snd_soc_set_dmi_name(card, NULL);
2079
Kuninori Morimoto0f23f712019-10-02 14:22:49 +09002080 soc_setup_card_name(card->snd_card->shortname,
2081 card->name, NULL, 0);
2082 soc_setup_card_name(card->snd_card->longname,
2083 card->long_name, card->name, 0);
2084 soc_setup_card_name(card->snd_card->driver,
2085 card->driver_name, card->name, 1);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002086
Mark Brown28e9ad92011-03-02 18:36:34 +00002087 if (card->late_probe) {
2088 ret = card->late_probe(card);
2089 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002090 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002091 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002092 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002093 }
2094 }
2095
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002096 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002097
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002098 ret = snd_card_register(card->snd_card);
2099 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002100 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2101 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002102 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002103 }
2104
Mark Brown435c5e22008-12-04 15:32:53 +00002105 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002106 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002107 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002108
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002109probe_end:
2110 if (ret < 0)
2111 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002113 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002114 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115
Mark Brownb19e6e72012-03-14 21:18:39 +00002116 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002117}
2118
2119/* probes a new socdev */
2120static int soc_probe(struct platform_device *pdev)
2121{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002122 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002123
Vinod Koul70a7ca32011-01-14 19:22:48 +05302124 /*
2125 * no card, so machine driver should be registering card
2126 * we should not be here in that case so ret error
2127 */
2128 if (!card)
2129 return -EINVAL;
2130
Mark Brownfe4085e2012-03-02 13:07:41 +00002131 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002132 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002133 card->name);
2134
Mark Brown435c5e22008-12-04 15:32:53 +00002135 /* Bodge while we unpick instantiation */
2136 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002137
Mark Brown28d528c2012-08-09 18:45:23 +01002138 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002139}
2140
2141/* removes a socdev */
2142static int soc_remove(struct platform_device *pdev)
2143{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002144 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145
Mark Brownc5af3a22008-11-28 13:29:45 +00002146 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147 return 0;
2148}
2149
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002150int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002151{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002152 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002153 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002154
2155 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002156 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002157
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002158 /*
2159 * Flush out pmdown_time work - we actually do want to run it
2160 * now, we're shutting down so no imminent restart.
2161 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002162 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002163
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002164 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002165
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002166 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002167 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002168 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002169 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002170 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002171
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002172 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002173 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002174 pinctrl_pm_select_sleep_state(codec_dai->dev);
2175 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002176 }
2177
Mark Brown416356f2009-06-30 19:05:15 +01002178 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002179}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002180EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002181
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002182const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302183 .suspend = snd_soc_suspend,
2184 .resume = snd_soc_resume,
2185 .freeze = snd_soc_suspend,
2186 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002187 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302188 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002189};
Stephen Warrendeb26072011-04-05 19:35:30 -06002190EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002191
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002192/* ASoC platform driver */
2193static struct platform_driver soc_driver = {
2194 .driver = {
2195 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002196 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 },
2198 .probe = soc_probe,
2199 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200};
2201
Mark Brown096e49d2009-07-05 15:12:22 +01002202/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203 * snd_soc_cnew - create new control
2204 * @_template: control template
2205 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002206 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002207 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208 *
2209 * Create a new mixer control from a template control.
2210 *
2211 * Returns 0 for success, else error.
2212 */
2213struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002214 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002215 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002216{
2217 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002218 struct snd_kcontrol *kcontrol;
2219 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220
2221 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 template.index = 0;
2223
Mark Brownefb7ac32011-03-08 17:23:24 +00002224 if (!long_name)
2225 long_name = template.name;
2226
2227 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002228 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002229 if (!name)
2230 return NULL;
2231
Mark Brownefb7ac32011-03-08 17:23:24 +00002232 template.name = name;
2233 } else {
2234 template.name = long_name;
2235 }
2236
2237 kcontrol = snd_ctl_new1(&template, data);
2238
2239 kfree(name);
2240
2241 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242}
2243EXPORT_SYMBOL_GPL(snd_soc_cnew);
2244
Liam Girdwood022658b2012-02-03 17:43:09 +00002245static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2246 const struct snd_kcontrol_new *controls, int num_controls,
2247 const char *prefix, void *data)
2248{
2249 int err, i;
2250
2251 for (i = 0; i < num_controls; i++) {
2252 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002253
Liam Girdwood022658b2012-02-03 17:43:09 +00002254 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2255 control->name, prefix));
2256 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002257 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2258 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002259 return err;
2260 }
2261 }
2262
2263 return 0;
2264}
2265
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002266struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2267 const char *name)
2268{
2269 struct snd_card *card = soc_card->snd_card;
2270 struct snd_kcontrol *kctl;
2271
2272 if (unlikely(!name))
2273 return NULL;
2274
2275 list_for_each_entry(kctl, &card->controls, list)
2276 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2277 return kctl;
2278 return NULL;
2279}
2280EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2281
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002283 * snd_soc_add_component_controls - Add an array of controls to a component.
2284 *
2285 * @component: Component to add controls to
2286 * @controls: Array of controls to add
2287 * @num_controls: Number of elements in the array
2288 *
2289 * Return: 0 for success, else error.
2290 */
2291int snd_soc_add_component_controls(struct snd_soc_component *component,
2292 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2293{
2294 struct snd_card *card = component->card->snd_card;
2295
2296 return snd_soc_add_controls(card, component->dev, controls,
2297 num_controls, component->name_prefix, component);
2298}
2299EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2300
2301/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002302 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2303 * Convenience function to add a list of controls.
2304 *
2305 * @soc_card: SoC card to add controls to
2306 * @controls: array of controls to add
2307 * @num_controls: number of elements in the array
2308 *
2309 * Return 0 for success, else error.
2310 */
2311int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2312 const struct snd_kcontrol_new *controls, int num_controls)
2313{
2314 struct snd_card *card = soc_card->snd_card;
2315
2316 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2317 NULL, soc_card);
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2320
2321/**
2322 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2323 * Convienience function to add a list of controls.
2324 *
2325 * @dai: DAI to add controls to
2326 * @controls: array of controls to add
2327 * @num_controls: number of elements in the array
2328 *
2329 * Return 0 for success, else error.
2330 */
2331int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2332 const struct snd_kcontrol_new *controls, int num_controls)
2333{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002334 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002335
2336 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2337 NULL, dai);
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2340
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002341static int snd_soc_bind_card(struct snd_soc_card *card)
2342{
2343 struct snd_soc_pcm_runtime *rtd;
2344 int ret;
2345
2346 ret = snd_soc_instantiate_card(card);
2347 if (ret != 0)
2348 return ret;
2349
2350 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002351 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002352 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2353 struct snd_soc_dai *codec_dai;
2354 int j;
2355
2356 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2357 if (!codec_dai->active)
2358 pinctrl_pm_select_sleep_state(codec_dai->dev);
2359 }
2360
2361 if (!cpu_dai->active)
2362 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2363 }
2364
2365 return ret;
2366}
2367
Mark Brownc5af3a22008-11-28 13:29:45 +00002368/**
2369 * snd_soc_register_card - Register a card with the ASoC core
2370 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002371 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002372 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002373 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302374int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002375{
2376 if (!card->name || !card->dev)
2377 return -EINVAL;
2378
Mark Browned77cc12011-05-03 18:25:34 +01002379 dev_set_drvdata(card->dev, card);
2380
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002381 INIT_LIST_HEAD(&card->widgets);
2382 INIT_LIST_HEAD(&card->paths);
2383 INIT_LIST_HEAD(&card->dapm_list);
2384 INIT_LIST_HEAD(&card->aux_comp_list);
2385 INIT_LIST_HEAD(&card->component_dev_list);
2386 INIT_LIST_HEAD(&card->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08002387 INIT_LIST_HEAD(&card->dai_link_list);
Mengdong Lin1a497982015-11-18 02:34:11 -05002388 INIT_LIST_HEAD(&card->rtd_list);
Mark Browndb432b42011-10-03 21:06:40 +01002389 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002390 INIT_LIST_HEAD(&card->dobj_list);
Kuninori Morimotob03bfae2019-08-20 14:04:54 +09002391
Mark Brownc5af3a22008-11-28 13:29:45 +00002392 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002393 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002394 mutex_init(&card->dapm_mutex);
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03002395 mutex_init(&card->pcm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002396 spin_lock_init(&card->dpcm_lock);
Mark Brownc5af3a22008-11-28 13:29:45 +00002397
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002398 return snd_soc_bind_card(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002399}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302400EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002401
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002402static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2403{
2404 if (card->instantiated) {
2405 card->instantiated = false;
2406 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002407 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002408
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002409 soc_cleanup_card_resources(card);
2410 if (!unregister)
2411 list_add(&card->list, &unbind_card_list);
2412 } else {
2413 if (unregister)
2414 list_del(&card->list);
2415 }
2416}
2417
Mark Brownc5af3a22008-11-28 13:29:45 +00002418/**
2419 * snd_soc_unregister_card - Unregister a card with the ASoC core
2420 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002421 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002422 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002423 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302424int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002425{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002426 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002427 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002428 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002429 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00002430
2431 return 0;
2432}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302433EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002434
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002435/*
2436 * Simplify DAI link configuration by removing ".-1" from device names
2437 * and sanitizing names.
2438 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002439static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002440{
2441 char *found, name[NAME_SIZE];
2442 int id1, id2;
2443
2444 if (dev_name(dev) == NULL)
2445 return NULL;
2446
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002447 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002448
2449 /* are we a "%s.%d" name (platform and SPI components) */
2450 found = strstr(name, dev->driver->name);
2451 if (found) {
2452 /* get ID */
2453 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2454
2455 /* discard ID from name if ID == -1 */
2456 if (*id == -1)
2457 found[strlen(dev->driver->name)] = '\0';
2458 }
2459
2460 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002461 /* I2C component devices are named "bus-addr" */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002462 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2463 char tmp[NAME_SIZE];
2464
2465 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002466 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002467
2468 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002469 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2470 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002471 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002472 } else
2473 *id = 0;
2474 }
2475
Kuninori Morimoto50014492019-10-02 14:22:57 +09002476 return devm_kstrdup(dev, name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002477}
2478
2479/*
2480 * Simplify DAI link naming for single devices with multiple DAIs by removing
2481 * any ".-1" and using the DAI name (instead of device name).
2482 */
2483static inline char *fmt_multiple_name(struct device *dev,
2484 struct snd_soc_dai_driver *dai_drv)
2485{
2486 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002487 dev_err(dev,
2488 "ASoC: error - multiple DAI %s registered with no name\n",
2489 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002490 return NULL;
2491 }
2492
Kuninori Morimoto50014492019-10-02 14:22:57 +09002493 return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002494}
2495
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002496void snd_soc_unregister_dai(struct snd_soc_dai *dai)
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002497{
2498 dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2499 list_del(&dai->list);
2500}
Kuninori Morimotoffdbca02019-11-06 10:07:23 +09002501EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002502
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002503/**
2504 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2505 *
2506 * @component: The component the DAIs are registered for
2507 * @dai_drv: DAI driver to use for the DAI
2508 *
2509 * Topology can use this API to register DAIs when probing a component.
2510 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2511 * will be freed in the component cleanup.
2512 */
2513struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2514 struct snd_soc_dai_driver *dai_drv,
2515 bool legacy_dai_naming)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002516{
2517 struct device *dev = component->dev;
2518 struct snd_soc_dai *dai;
2519
2520 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2521
Kuninori Morimoto7ca24382019-11-06 10:07:17 +09002522 lockdep_assert_held(&client_mutex);
2523
Kuninori Morimoto50014492019-10-02 14:22:57 +09002524 dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002525 if (dai == NULL)
2526 return NULL;
2527
2528 /*
2529 * Back in the old days when we still had component-less DAIs,
2530 * instead of having a static name, component-less DAIs would
2531 * inherit the name of the parent device so it is possible to
2532 * register multiple instances of the DAI. We still need to keep
2533 * the same naming style even though those DAIs are not
2534 * component-less anymore.
2535 */
2536 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002537 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002538 dai->name = fmt_single_name(dev, &dai->id);
2539 } else {
2540 dai->name = fmt_multiple_name(dev, dai_drv);
2541 if (dai_drv->id)
2542 dai->id = dai_drv->id;
2543 else
2544 dai->id = component->num_dai;
2545 }
Kuninori Morimoto50014492019-10-02 14:22:57 +09002546 if (!dai->name)
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002547 return NULL;
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002548
2549 dai->component = component;
2550 dai->dev = dev;
2551 dai->driver = dai_drv;
2552 if (!dai->driver->ops)
2553 dai->driver->ops = &null_dai_ops;
2554
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002555 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002556 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002557 component->num_dai++;
2558
2559 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2560 return dai;
2561}
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002562
Mark Brown91151712008-11-30 23:31:24 +00002563/**
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002564 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2565 *
2566 * @component: The component for which the DAIs should be unregistered
2567 */
2568static void snd_soc_unregister_dais(struct snd_soc_component *component)
2569{
2570 struct snd_soc_dai *dai, *_dai;
2571
Kuninori Morimotoe11381f2019-11-05 15:47:04 +09002572 for_each_component_dais_safe(component, dai, _dai)
2573 snd_soc_unregister_dai(dai);
Kuninori Morimoto3f6674a2019-11-05 15:47:00 +09002574}
2575
2576/**
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002577 * snd_soc_register_dais - Register a DAI with the ASoC core
2578 *
2579 * @component: The component the DAIs are registered for
2580 * @dai_drv: DAI driver to use for the DAIs
2581 * @count: Number of DAIs
2582 */
2583static int snd_soc_register_dais(struct snd_soc_component *component,
2584 struct snd_soc_dai_driver *dai_drv,
2585 size_t count)
2586{
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002587 struct snd_soc_dai *dai;
2588 unsigned int i;
2589 int ret;
2590
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002591 for (i = 0; i < count; i++) {
Kuninori Morimoto71cb85f2019-11-05 15:47:18 +09002592 dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
Kuninori Morimotodaf77372019-11-05 15:46:55 +09002593 !component->driver->non_legacy_dai_naming);
2594 if (dai == NULL) {
2595 ret = -ENOMEM;
2596 goto err;
2597 }
2598 }
2599
2600 return 0;
2601
2602err:
2603 snd_soc_unregister_dais(component);
2604
2605 return ret;
2606}
2607
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002608static int snd_soc_component_initialize(struct snd_soc_component *component,
2609 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002610{
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002611 INIT_LIST_HEAD(&component->dai_list);
Kuninori Morimoto495efdb2019-08-20 14:05:20 +09002612 INIT_LIST_HEAD(&component->dobj_list);
2613 INIT_LIST_HEAD(&component->card_list);
Kuninori Morimoto8d92bb52019-08-20 14:05:16 +09002614 mutex_init(&component->io_mutex);
2615
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002616 component->name = fmt_single_name(dev, &component->id);
2617 if (!component->name) {
2618 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002619 return -ENOMEM;
2620 }
2621
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002622 component->dev = dev;
2623 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002624
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002625 return 0;
2626}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002627
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002628static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002629{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002630 int val_bytes = regmap_get_val_bytes(component->regmap);
2631
2632 /* Errors are legitimate for non-integer byte multiples */
2633 if (val_bytes > 0)
2634 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002635}
2636
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002637#ifdef CONFIG_REGMAP
2638
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002639/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002640 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2641 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002642 * @component: The component for which to initialize the regmap instance
2643 * @regmap: The regmap instance that should be used by the component
2644 *
2645 * This function allows deferred assignment of the regmap instance that is
2646 * associated with the component. Only use this if the regmap instance is not
2647 * yet ready when the component is registered. The function must also be called
2648 * before the first IO attempt of the component.
2649 */
2650void snd_soc_component_init_regmap(struct snd_soc_component *component,
2651 struct regmap *regmap)
2652{
2653 component->regmap = regmap;
2654 snd_soc_component_setup_regmap(component);
2655}
2656EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2657
2658/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002659 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2660 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002661 * @component: The component for which to de-initialize the regmap instance
2662 *
2663 * Calls regmap_exit() on the regmap instance associated to the component and
2664 * removes the regmap instance from the component.
2665 *
2666 * This function should only be used if snd_soc_component_init_regmap() was used
2667 * to initialize the regmap instance.
2668 */
2669void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2670{
2671 regmap_exit(component->regmap);
2672 component->regmap = NULL;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2675
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002676#endif
2677
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002678#define ENDIANNESS_MAP(name) \
2679 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2680static u64 endianness_format_map[] = {
2681 ENDIANNESS_MAP(S16_),
2682 ENDIANNESS_MAP(U16_),
2683 ENDIANNESS_MAP(S24_),
2684 ENDIANNESS_MAP(U24_),
2685 ENDIANNESS_MAP(S32_),
2686 ENDIANNESS_MAP(U32_),
2687 ENDIANNESS_MAP(S24_3),
2688 ENDIANNESS_MAP(U24_3),
2689 ENDIANNESS_MAP(S20_3),
2690 ENDIANNESS_MAP(U20_3),
2691 ENDIANNESS_MAP(S18_3),
2692 ENDIANNESS_MAP(U18_3),
2693 ENDIANNESS_MAP(FLOAT_),
2694 ENDIANNESS_MAP(FLOAT64_),
2695 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2696};
2697
2698/*
2699 * Fix up the DAI formats for endianness: codecs don't actually see
2700 * the endianness of the data but we're using the CPU format
2701 * definitions which do need to include endianness so we ensure that
2702 * codec DAIs always have both big and little endian variants set.
2703 */
2704static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2705{
2706 int i;
2707
2708 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2709 if (stream->formats & endianness_format_map[i])
2710 stream->formats |= endianness_format_map[i];
2711}
2712
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002713static void snd_soc_try_rebind_card(void)
2714{
2715 struct snd_soc_card *card, *c;
2716
Kuninori Morimotob245d272019-08-07 10:31:19 +09002717 list_for_each_entry_safe(card, c, &unbind_card_list, list)
2718 if (!snd_soc_bind_card(card))
2719 list_del(&card->list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002720}
2721
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002722static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2723{
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002724 struct snd_soc_card *card = component->card;
2725
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002726 snd_soc_unregister_dais(component);
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002727
2728 if (card)
2729 snd_soc_unbind_card(card, false);
2730
2731 list_del(&component->list);
Kuninori Morimoto486c7972019-11-05 15:46:39 +09002732}
2733
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002734int snd_soc_add_component(struct device *dev,
2735 struct snd_soc_component *component,
2736 const struct snd_soc_component_driver *component_driver,
2737 struct snd_soc_dai_driver *dai_drv,
2738 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002739{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002740 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002741 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002742
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002743 mutex_lock(&client_mutex);
2744
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002745 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002746 if (ret)
2747 goto err_free;
2748
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002749 if (component_driver->endianness) {
2750 for (i = 0; i < num_dai; i++) {
2751 convert_endianness_formats(&dai_drv[i].playback);
2752 convert_endianness_formats(&dai_drv[i].capture);
2753 }
2754 }
2755
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002756 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002757 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002758 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002759 goto err_cleanup;
2760 }
2761
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002762 if (!component->driver->write && !component->driver->read) {
2763 if (!component->regmap)
2764 component->regmap = dev_get_regmap(component->dev,
2765 NULL);
2766 if (component->regmap)
2767 snd_soc_component_setup_regmap(component);
2768 }
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002769
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002770 /* see for_each_component */
2771 list_add(&component->list, &component_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002772
2773err_cleanup:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002774 if (ret < 0)
2775 snd_soc_del_component_unlocked(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002776err_free:
Kuninori Morimotob18768f2019-11-05 15:46:45 +09002777 mutex_unlock(&client_mutex);
2778
2779 if (ret == 0)
2780 snd_soc_try_rebind_card();
2781
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002782 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002783}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002784EXPORT_SYMBOL_GPL(snd_soc_add_component);
2785
2786int snd_soc_register_component(struct device *dev,
2787 const struct snd_soc_component_driver *component_driver,
2788 struct snd_soc_dai_driver *dai_drv,
2789 int num_dai)
2790{
2791 struct snd_soc_component *component;
2792
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002793 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002794 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002795 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002796
2797 return snd_soc_add_component(dev, component, component_driver,
2798 dai_drv, num_dai);
2799}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002800EXPORT_SYMBOL_GPL(snd_soc_register_component);
2801
2802/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002803 * snd_soc_unregister_component - Unregister all related component
2804 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002805 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002806 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002807 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002808void snd_soc_unregister_component(struct device *dev)
2809{
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002810 struct snd_soc_component *component;
2811
2812 mutex_lock(&client_mutex);
2813 while (1) {
Kuninori Morimoto18dd66e2019-11-06 16:05:05 +09002814 component = snd_soc_lookup_component_nolocked(dev, NULL);
Kuninori Morimotoac6a4dd2019-11-05 15:46:51 +09002815 if (!component)
2816 break;
2817
2818 snd_soc_del_component_unlocked(component);
2819 }
2820 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002821}
2822EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2823
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002824/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002825int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2826 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002827{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002828 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002829 int ret;
2830
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302831 if (!card->dev) {
2832 pr_err("card->dev is not set before calling %s\n", __func__);
2833 return -EINVAL;
2834 }
2835
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002836 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05302837
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002838 ret = of_property_read_string_index(np, propname, 0, &card->name);
2839 /*
2840 * EINVAL means the property does not exist. This is fine providing
2841 * card->name was previously set, which is checked later in
2842 * snd_soc_register_card.
2843 */
2844 if (ret < 0 && ret != -EINVAL) {
2845 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002846 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002847 propname, ret);
2848 return ret;
2849 }
2850
2851 return 0;
2852}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00002853EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07002854
Xiubo Li9a6d4862014-02-08 15:59:52 +08002855static const struct snd_soc_dapm_widget simple_widgets[] = {
2856 SND_SOC_DAPM_MIC("Microphone", NULL),
2857 SND_SOC_DAPM_LINE("Line", NULL),
2858 SND_SOC_DAPM_HP("Headphone", NULL),
2859 SND_SOC_DAPM_SPK("Speaker", NULL),
2860};
2861
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002862int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08002863 const char *propname)
2864{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002865 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002866 struct snd_soc_dapm_widget *widgets;
2867 const char *template, *wname;
2868 int i, j, num_widgets, ret;
2869
2870 num_widgets = of_property_count_strings(np, propname);
2871 if (num_widgets < 0) {
2872 dev_err(card->dev,
2873 "ASoC: Property '%s' does not exist\n", propname);
2874 return -EINVAL;
2875 }
2876 if (num_widgets & 1) {
2877 dev_err(card->dev,
2878 "ASoC: Property '%s' length is not even\n", propname);
2879 return -EINVAL;
2880 }
2881
2882 num_widgets /= 2;
2883 if (!num_widgets) {
2884 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
2885 propname);
2886 return -EINVAL;
2887 }
2888
2889 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
2890 GFP_KERNEL);
2891 if (!widgets) {
2892 dev_err(card->dev,
2893 "ASoC: Could not allocate memory for widgets\n");
2894 return -ENOMEM;
2895 }
2896
2897 for (i = 0; i < num_widgets; i++) {
2898 ret = of_property_read_string_index(np, propname,
2899 2 * i, &template);
2900 if (ret) {
2901 dev_err(card->dev,
2902 "ASoC: Property '%s' index %d read error:%d\n",
2903 propname, 2 * i, ret);
2904 return -EINVAL;
2905 }
2906
2907 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
2908 if (!strncmp(template, simple_widgets[j].name,
2909 strlen(simple_widgets[j].name))) {
2910 widgets[i] = simple_widgets[j];
2911 break;
2912 }
2913 }
2914
2915 if (j >= ARRAY_SIZE(simple_widgets)) {
2916 dev_err(card->dev,
2917 "ASoC: DAPM widget '%s' is not supported\n",
2918 template);
2919 return -EINVAL;
2920 }
2921
2922 ret = of_property_read_string_index(np, propname,
2923 (2 * i) + 1,
2924 &wname);
2925 if (ret) {
2926 dev_err(card->dev,
2927 "ASoC: Property '%s' index %d read error:%d\n",
2928 propname, (2 * i) + 1, ret);
2929 return -EINVAL;
2930 }
2931
2932 widgets[i].name = wname;
2933 }
2934
Nicolin Chenf23e8602015-02-14 17:22:49 -08002935 card->of_dapm_widgets = widgets;
2936 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08002937
2938 return 0;
2939}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00002940EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08002941
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002942int snd_soc_of_get_slot_mask(struct device_node *np,
2943 const char *prop_name,
2944 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03002945{
2946 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03002947 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03002948 int i;
2949
2950 if (!of_slot_mask)
2951 return 0;
2952 val /= sizeof(u32);
2953 for (i = 0; i < val; i++)
2954 if (be32_to_cpup(&of_slot_mask[i]))
2955 *mask |= (1 << i);
2956
2957 return val;
2958}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02002959EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03002960
Xiubo Li89c67852014-02-14 09:34:35 +08002961int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03002962 unsigned int *tx_mask,
2963 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08002964 unsigned int *slots,
2965 unsigned int *slot_width)
2966{
2967 u32 val;
2968 int ret;
2969
Jyri Sarha61310842015-09-09 21:27:43 +03002970 if (tx_mask)
2971 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
2972 if (rx_mask)
2973 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
2974
Xiubo Li89c67852014-02-14 09:34:35 +08002975 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
2976 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
2977 if (ret)
2978 return ret;
2979
2980 if (slots)
2981 *slots = val;
2982 }
2983
2984 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
2985 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
2986 if (ret)
2987 return ret;
2988
2989 if (slot_width)
2990 *slot_width = val;
2991 }
2992
2993 return 0;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
2996
Kuninori Morimoto3b710352018-11-22 00:55:09 +00002997void snd_soc_of_parse_node_prefix(struct device_node *np,
2998 struct snd_soc_codec_conf *codec_conf,
2999 struct device_node *of_node,
3000 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003001{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003002 const char *str;
3003 int ret;
3004
3005 ret = of_property_read_string(np, propname, &str);
3006 if (ret < 0) {
3007 /* no prefix is not error */
3008 return;
3009 }
3010
3011 codec_conf->of_node = of_node;
3012 codec_conf->name_prefix = str;
3013}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003014EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003015
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003016int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003017 const char *propname)
3018{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003019 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003020 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003021 struct snd_soc_dapm_route *routes;
3022 int i, ret;
3023
3024 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003025 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003026 dev_err(card->dev,
3027 "ASoC: Property '%s' does not exist or its length is not even\n",
3028 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003029 return -EINVAL;
3030 }
3031 num_routes /= 2;
3032 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003033 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003034 propname);
3035 return -EINVAL;
3036 }
3037
Kees Cooka86854d2018-06-12 14:07:58 -07003038 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003039 GFP_KERNEL);
3040 if (!routes) {
3041 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003042 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003043 return -EINVAL;
3044 }
3045
3046 for (i = 0; i < num_routes; i++) {
3047 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003048 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003049 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003050 dev_err(card->dev,
3051 "ASoC: Property '%s' index %d could not be read: %d\n",
3052 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003053 return -EINVAL;
3054 }
3055 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003056 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003057 if (ret) {
3058 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003059 "ASoC: Property '%s' index %d could not be read: %d\n",
3060 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003061 return -EINVAL;
3062 }
3063 }
3064
Nicolin Chenf23e8602015-02-14 17:22:49 -08003065 card->num_of_dapm_routes = num_routes;
3066 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003067
3068 return 0;
3069}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003070EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003071
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003072unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003073 const char *prefix,
3074 struct device_node **bitclkmaster,
3075 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003076{
3077 int ret, i;
3078 char prop[128];
3079 unsigned int format = 0;
3080 int bit, frame;
3081 const char *str;
3082 struct {
3083 char *name;
3084 unsigned int val;
3085 } of_fmt_table[] = {
3086 { "i2s", SND_SOC_DAIFMT_I2S },
3087 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3088 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3089 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3090 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3091 { "ac97", SND_SOC_DAIFMT_AC97 },
3092 { "pdm", SND_SOC_DAIFMT_PDM},
3093 { "msb", SND_SOC_DAIFMT_MSB },
3094 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003095 };
3096
3097 if (!prefix)
3098 prefix = "";
3099
3100 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003101 * check "dai-format = xxx"
3102 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003103 * SND_SOC_DAIFMT_FORMAT_MASK area
3104 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003105 ret = of_property_read_string(np, "dai-format", &str);
3106 if (ret < 0) {
3107 snprintf(prop, sizeof(prop), "%sformat", prefix);
3108 ret = of_property_read_string(np, prop, &str);
3109 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003110 if (ret == 0) {
3111 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3112 if (strcmp(str, of_fmt_table[i].name) == 0) {
3113 format |= of_fmt_table[i].val;
3114 break;
3115 }
3116 }
3117 }
3118
3119 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003120 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003121 * SND_SOC_DAIFMT_CLOCK_MASK area
3122 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003123 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003124 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003125 format |= SND_SOC_DAIFMT_CONT;
3126 else
3127 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003128
3129 /*
3130 * check "[prefix]bitclock-inversion"
3131 * check "[prefix]frame-inversion"
3132 * SND_SOC_DAIFMT_INV_MASK area
3133 */
3134 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3135 bit = !!of_get_property(np, prop, NULL);
3136
3137 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3138 frame = !!of_get_property(np, prop, NULL);
3139
3140 switch ((bit << 4) + frame) {
3141 case 0x11:
3142 format |= SND_SOC_DAIFMT_IB_IF;
3143 break;
3144 case 0x10:
3145 format |= SND_SOC_DAIFMT_IB_NF;
3146 break;
3147 case 0x01:
3148 format |= SND_SOC_DAIFMT_NB_IF;
3149 break;
3150 default:
3151 /* SND_SOC_DAIFMT_NB_NF is default */
3152 break;
3153 }
3154
3155 /*
3156 * check "[prefix]bitclock-master"
3157 * check "[prefix]frame-master"
3158 * SND_SOC_DAIFMT_MASTER_MASK area
3159 */
3160 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3161 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003162 if (bit && bitclkmaster)
3163 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003164
3165 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3166 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003167 if (frame && framemaster)
3168 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003169
3170 switch ((bit << 4) + frame) {
3171 case 0x11:
3172 format |= SND_SOC_DAIFMT_CBM_CFM;
3173 break;
3174 case 0x10:
3175 format |= SND_SOC_DAIFMT_CBM_CFS;
3176 break;
3177 case 0x01:
3178 format |= SND_SOC_DAIFMT_CBS_CFM;
3179 break;
3180 default:
3181 format |= SND_SOC_DAIFMT_CBS_CFS;
3182 break;
3183 }
3184
3185 return format;
3186}
3187EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3188
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003189int snd_soc_get_dai_id(struct device_node *ep)
3190{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003191 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003192 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003193 int ret;
3194
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003195 dlc.of_node = of_graph_get_port_parent(ep);
3196 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003197 /*
3198 * For example HDMI case, HDMI has video/sound port,
3199 * but ALSA SoC needs sound port number only.
3200 * Thus counting HDMI DT port/endpoint doesn't work.
3201 * Then, it should have .of_xlate_dai_id
3202 */
3203 ret = -ENOTSUPP;
3204 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003205 component = soc_find_component(&dlc);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +09003206 if (component)
3207 ret = snd_soc_component_of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003208 mutex_unlock(&client_mutex);
3209
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003210 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003211
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003212 return ret;
3213}
3214EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3215
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003216int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003217 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003218{
3219 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003220 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003221 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003222
3223 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003224 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003225 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003226
3227 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003228 continue;
3229
Kuninori Morimotoa2a34172019-07-26 13:51:31 +09003230 ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name);
3231 if (ret == -ENOTSUPP) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003232 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003233 int id = -1;
3234
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003235 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003236 case 0:
3237 id = 0; /* same as dai_drv[0] */
3238 break;
3239 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003240 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003241 break;
3242 default:
3243 /* not supported */
3244 break;
3245 }
3246
3247 if (id < 0 || id >= pos->num_dai) {
3248 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003249 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003250 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003251
3252 ret = 0;
3253
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003254 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003255 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003256 if (id == 0)
3257 break;
3258 id--;
3259 }
3260
3261 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003262 if (!*dai_name)
3263 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003264 }
3265
Kuninori Morimotocb470082013-09-10 17:39:56 -07003266 break;
3267 }
3268 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003269 return ret;
3270}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003271EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003272
3273int snd_soc_of_get_dai_name(struct device_node *of_node,
3274 const char **dai_name)
3275{
3276 struct of_phandle_args args;
3277 int ret;
3278
3279 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3280 "#sound-dai-cells", 0, &args);
3281 if (ret)
3282 return ret;
3283
3284 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003285
3286 of_node_put(args.np);
3287
3288 return ret;
3289}
3290EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3291
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003292/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003293 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3294 * @dai_link: DAI link
3295 *
3296 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3297 */
3298void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3299{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003300 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003301 int index;
3302
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003303 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003304 if (!component->of_node)
3305 break;
3306 of_node_put(component->of_node);
3307 component->of_node = NULL;
3308 }
3309}
3310EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3311
3312/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003313 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3314 * @dev: Card device
3315 * @of_node: Device node
3316 * @dai_link: DAI link
3317 *
3318 * Builds an array of CODEC DAI components from the DAI link property
3319 * 'sound-dai'.
3320 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003321 * The device nodes in the array (of_node) must be dereferenced by calling
3322 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003323 *
3324 * Returns 0 for success
3325 */
3326int snd_soc_of_get_dai_link_codecs(struct device *dev,
3327 struct device_node *of_node,
3328 struct snd_soc_dai_link *dai_link)
3329{
3330 struct of_phandle_args args;
3331 struct snd_soc_dai_link_component *component;
3332 char *name;
3333 int index, num_codecs, ret;
3334
3335 /* Count the number of CODECs */
3336 name = "sound-dai";
3337 num_codecs = of_count_phandle_with_args(of_node, name,
3338 "#sound-dai-cells");
3339 if (num_codecs <= 0) {
3340 if (num_codecs == -ENOENT)
3341 dev_err(dev, "No 'sound-dai' property\n");
3342 else
3343 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3344 return num_codecs;
3345 }
Kees Cooka86854d2018-06-12 14:07:58 -07003346 component = devm_kcalloc(dev,
3347 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003348 GFP_KERNEL);
3349 if (!component)
3350 return -ENOMEM;
3351 dai_link->codecs = component;
3352 dai_link->num_codecs = num_codecs;
3353
3354 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003355 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003356 ret = of_parse_phandle_with_args(of_node, name,
3357 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003358 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003359 if (ret)
3360 goto err;
3361 component->of_node = args.np;
3362 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3363 if (ret < 0)
3364 goto err;
3365 }
3366 return 0;
3367err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003368 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003369 dai_link->codecs = NULL;
3370 dai_link->num_codecs = 0;
3371 return ret;
3372}
3373EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3374
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003375static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003376{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003377 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003378 snd_soc_util_init();
3379
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003380 return platform_driver_register(&soc_driver);
3381}
Mark Brown4abe8e12010-10-12 17:41:03 +01003382module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003383
Mark Brown7d8c16a2008-11-30 22:11:24 +00003384static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003385{
Mark Brownfb257892011-04-28 10:57:54 +01003386 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003387 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003388
Mark Brown3ff3f642008-05-19 12:32:25 +02003389 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003390}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003391module_exit(snd_soc_exit);
3392
3393/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003394MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003395MODULE_DESCRIPTION("ALSA SoC Core");
3396MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003397MODULE_ALIAS("platform:soc-audio");