blob: 44f899b970c2a399bb3cf9b97ce4a93d9264f140 [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
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020076/*
77 * If a DMI filed contain strings in this blacklist (e.g.
78 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080079 * as invalid and dropped when setting the card long name from DMI info.
80 */
81static const char * const dmi_blacklist[] = {
82 "To be filled by OEM",
83 "TBD by OEM",
84 "Default String",
85 "Board Manufacturer",
86 "Board Vendor Name",
87 "Board Product Name",
88 NULL, /* terminator */
89};
90
Mark Browndbe21402010-02-12 11:37:24 +000091static ssize_t pmdown_time_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
Mark Brown36ae1a92012-01-06 17:12:45 -080094 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000097}
98
99static ssize_t pmdown_time_set(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
Mark Brown36ae1a92012-01-06 17:12:45 -0800103 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700104 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000105
Jingoo Hanb785a492013-07-19 16:24:59 +0900106 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700107 if (ret)
108 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000109
110 return count;
111}
112
113static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
114
Takashi Iwaid29697d2015-01-30 20:16:37 +0100115static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100116 &dev_attr_pmdown_time.attr,
117 NULL
118};
119
120static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
121 struct attribute *attr, int idx)
122{
123 struct device *dev = kobj_to_dev(kobj);
124 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
125
126 if (attr == &dev_attr_pmdown_time.attr)
127 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000128 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100129}
130
131static const struct attribute_group soc_dapm_dev_group = {
132 .attrs = soc_dapm_dev_attrs,
133 .is_visible = soc_dev_attr_is_visible,
134};
135
Mark Brownf7e73b262018-03-09 12:46:27 +0000136static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 .attrs = soc_dev_attrs,
138 .is_visible = soc_dev_attr_is_visible,
139};
140
141static const struct attribute_group *soc_dev_attr_groups[] = {
142 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000143 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100144 NULL
145};
146
Mark Brown2624d5f2009-11-03 21:56:13 +0000147#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200148static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100149{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200150 if (!component->card->debugfs_card_root)
151 return;
152
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200153 if (component->debugfs_prefix) {
154 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 name = kasprintf(GFP_KERNEL, "%s:%s",
157 component->debugfs_prefix, component->name);
158 if (name) {
159 component->debugfs_root = debugfs_create_dir(name,
160 component->card->debugfs_card_root);
161 kfree(name);
162 }
163 } else {
164 component->debugfs_root = debugfs_create_dir(component->name,
165 component->card->debugfs_card_root);
166 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100167
Mark Brownc2c928c2019-06-21 12:33:56 +0100168 if (IS_ERR(component->debugfs_root)) {
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169 dev_warn(component->dev,
Mark Brownc2c928c2019-06-21 12:33:56 +0100170 "ASoC: Failed to create component debugfs directory: %ld\n",
171 PTR_ERR(component->debugfs_root));
Mark Brown2624d5f2009-11-03 21:56:13 +0000172 return;
173 }
174
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200175 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
176 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200177}
178
179static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
180{
181 debugfs_remove_recursive(component->debugfs_root);
182}
183
Peng Donglinc15b2a12018-02-14 22:48:07 +0800184static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100185{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100186 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100187 struct snd_soc_dai *dai;
188
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100189 mutex_lock(&client_mutex);
190
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000191 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000192 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800193 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100194
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100195 mutex_unlock(&client_mutex);
196
Donglin Peng700c17c2018-01-18 13:31:26 +0800197 return 0;
198}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800199DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100200
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000201static int component_list_show(struct seq_file *m, void *v)
202{
203 struct snd_soc_component *component;
204
205 mutex_lock(&client_mutex);
206
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000207 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000208 seq_printf(m, "%s\n", component->name);
209
210 mutex_unlock(&client_mutex);
211
212 return 0;
213}
214DEFINE_SHOW_ATTRIBUTE(component_list);
215
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216static void soc_init_card_debugfs(struct snd_soc_card *card)
217{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200218 if (!snd_soc_debugfs_root)
219 return;
220
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200221 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000222 snd_soc_debugfs_root);
Mark Brownc2c928c2019-06-21 12:33:56 +0100223 if (IS_ERR(card->debugfs_card_root)) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200224 dev_warn(card->dev,
Mark Brownc2c928c2019-06-21 12:33:56 +0100225 "ASoC: Failed to create card debugfs directory: %ld\n",
226 PTR_ERR(card->debugfs_card_root));
227 card->debugfs_card_root = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200228 return;
229 }
230
231 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
232 card->debugfs_card_root,
233 &card->pop_time);
Mark Brownc2c928c2019-06-21 12:33:56 +0100234 if (IS_ERR(card->debugfs_pop_time))
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200235 dev_warn(card->dev,
Mark Brownc2c928c2019-06-21 12:33:56 +0100236 "ASoC: Failed to create pop time debugfs file: %ld\n",
237 PTR_ERR(card->debugfs_pop_time));
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200238}
239
240static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
241{
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900242 if (!card->debugfs_card_root)
243 return;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200244 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900245 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200246}
247
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200248static void snd_soc_debugfs_init(void)
249{
250 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300251 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200252 pr_warn("ASoC: Failed to create debugfs directory\n");
253 snd_soc_debugfs_root = NULL;
254 return;
255 }
256
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200257 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
258 &dai_list_fops))
259 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000260
261 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
262 &component_list_fops))
263 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200264}
265
266static void snd_soc_debugfs_exit(void)
267{
268 debugfs_remove_recursive(snd_soc_debugfs_root);
269}
270
Mark Brown2624d5f2009-11-03 21:56:13 +0000271#else
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static inline void soc_init_component_debugfs(
274 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000275{
276}
277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278static inline void soc_cleanup_component_debugfs(
279 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000280{
281}
282
Axel Linb95fccb2010-11-09 17:06:44 +0800283static inline void soc_init_card_debugfs(struct snd_soc_card *card)
284{
285}
286
287static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
288{
289}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200290
291static inline void snd_soc_debugfs_init(void)
292{
293}
294
295static inline void snd_soc_debugfs_exit(void)
296{
297}
298
Mark Brown2624d5f2009-11-03 21:56:13 +0000299#endif
300
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000301static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
302 struct snd_soc_component *component)
303{
304 struct snd_soc_rtdcom_list *rtdcom;
305 struct snd_soc_rtdcom_list *new_rtdcom;
306
307 for_each_rtdcom(rtd, rtdcom) {
308 /* already connected */
309 if (rtdcom->component == component)
310 return 0;
311 }
312
313 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
314 if (!new_rtdcom)
315 return -ENOMEM;
316
317 new_rtdcom->component = component;
318 INIT_LIST_HEAD(&new_rtdcom->list);
319
320 list_add_tail(&new_rtdcom->list, &rtd->component_list);
321
322 return 0;
323}
324
325static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
326{
327 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
328
329 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
330 kfree(rtdcom1);
331
332 INIT_LIST_HEAD(&rtd->component_list);
333}
334
335struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
336 const char *driver_name)
337{
338 struct snd_soc_rtdcom_list *rtdcom;
339
Kuninori Morimoto971da242018-01-23 00:41:24 +0000340 if (!driver_name)
341 return NULL;
342
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000344 const char *component_name = rtdcom->component->driver->name;
345
346 if (!component_name)
347 continue;
348
349 if ((component_name == driver_name) ||
350 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000351 return rtdcom->component;
352 }
353
354 return NULL;
355}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000356EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000357
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100358struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
359 const char *dai_link, int stream)
360{
Mengdong Lin1a497982015-11-18 02:34:11 -0500361 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100362
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000363 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500364 if (rtd->dai_link->no_pcm &&
365 !strcmp(rtd->dai_link->name, dai_link))
366 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100367 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000368 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100369 return NULL;
370}
371EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
372
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373static const struct snd_soc_ops null_snd_soc_ops;
374
Mengdong Lin1a497982015-11-18 02:34:11 -0500375static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
376 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
377{
378 struct snd_soc_pcm_runtime *rtd;
379
380 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
381 if (!rtd)
382 return NULL;
383
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000384 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500385 rtd->card = card;
386 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000387 if (!rtd->dai_link->ops)
388 rtd->dai_link->ops = &null_snd_soc_ops;
389
Kees Cook6396bb22018-06-12 14:03:40 -0700390 rtd->codec_dais = kcalloc(dai_link->num_codecs,
391 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500392 GFP_KERNEL);
393 if (!rtd->codec_dais) {
394 kfree(rtd);
395 return NULL;
396 }
397
398 return rtd;
399}
400
401static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
402{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000403 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000404 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500405 kfree(rtd);
406}
407
408static void soc_add_pcm_runtime(struct snd_soc_card *card,
409 struct snd_soc_pcm_runtime *rtd)
410{
411 list_add_tail(&rtd->list, &card->rtd_list);
412 rtd->num = card->num_rtd;
413 card->num_rtd++;
414}
415
416static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
417{
418 struct snd_soc_pcm_runtime *rtd, *_rtd;
419
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000420 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500421 list_del(&rtd->list);
422 soc_free_pcm_runtime(rtd);
423 }
424
425 card->num_rtd = 0;
426}
427
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100428struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
429 const char *dai_link)
430{
Mengdong Lin1a497982015-11-18 02:34:11 -0500431 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100432
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000433 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500434 if (!strcmp(rtd->dai_link->name, dai_link))
435 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100436 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000437 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100438 return NULL;
439}
440EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
441
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900442static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
443{
444 struct snd_soc_pcm_runtime *rtd;
445
446 for_each_card_rtds(card, rtd)
447 flush_delayed_work(&rtd->delayed_work);
448}
449
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100450static void codec2codec_close_delayed_work(struct work_struct *work)
451{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200452 /*
453 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100454 * Since c2c links are internal nodes in the DAPM graph and
455 * don't interface with the outside world or application layer
456 * we don't have to do any special handling on close.
457 */
458}
459
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000460#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000462int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200463{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000464 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000465 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500466 struct snd_soc_pcm_runtime *rtd;
467 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200469 /* If the card is not initialized yet there is nothing to do */
470 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200471 return 0;
472
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200473 /*
474 * Due to the resume being scheduled into a workqueue we could
475 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100476 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000477 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100478
479 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100481
Mark Browna00f90f2010-12-02 16:24:24 +0000482 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000483 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000484 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100485
Mengdong Lin1a497982015-11-18 02:34:11 -0500486 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100487 continue;
488
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000489 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200490 struct snd_soc_dai_driver *drv = dai->driver;
491
492 if (drv->ops->digital_mute && dai->playback_active)
493 drv->ops->digital_mute(dai, 1);
494 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 }
496
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100497 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000498 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500499 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100500 continue;
501
Mengdong Lin1a497982015-11-18 02:34:11 -0500502 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100503 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100504
Mark Brown87506542008-11-18 20:50:34 +0000505 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000506 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000508 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500509 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100510
Mengdong Lin1a497982015-11-18 02:34:11 -0500511 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100512 continue;
513
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100514 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100516 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200517
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200518 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900519 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100520
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000521 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522
Mengdong Lin1a497982015-11-18 02:34:11 -0500523 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100524 continue;
525
Mengdong Lin1a497982015-11-18 02:34:11 -0500526 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800527 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800528 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529
Mengdong Lin1a497982015-11-18 02:34:11 -0500530 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800531 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800532 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 }
534
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200535 /* Recheck all endpoints too, their state is affected by suspend */
536 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700537 snd_soc_dapm_sync(&card->dapm);
538
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000539 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000540 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200541 struct snd_soc_dapm_context *dapm =
542 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000543
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200544 /*
545 * If there are paths active then the COMPONENT will be held
546 * with bias _ON and should not be suspended.
547 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000548 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200549 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000551 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000552 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000553 * bias off then being in STANDBY
554 * means it's doing something,
555 * otherwise fall through.
556 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200557 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000558 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200559 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000560 break;
561 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500562 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000565 if (component->driver->suspend)
566 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000567 component->suspended = 1;
568 if (component->regmap)
569 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800570 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000571 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 break;
573 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000574 dev_dbg(component->dev,
575 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 break;
577 }
578 }
579 }
580
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000581 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500582 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583
Mengdong Lin1a497982015-11-18 02:34:11 -0500584 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 continue;
586
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100587 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800589
590 /* deactivate pins to sleep state */
591 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 }
593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
597 return 0;
598}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000599EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200600
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200601/*
602 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100603 * setting our codec back up, which can be very slow on I2C
604 */
605static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200606{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200608 container_of(work, struct snd_soc_card,
609 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500610 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000611 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500612 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200613
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200614 /*
615 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100616 * so userspace apps are blocked from touching us
617 */
618
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000619 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100620
Mark Brown9949788b2010-05-07 20:24:05 +0100621 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100623
Mark Brown87506542008-11-18 20:50:34 +0000624 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000625 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100627 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000628 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500629 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100630
Mengdong Lin1a497982015-11-18 02:34:11 -0500631 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100632 continue;
633
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100634 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 }
637
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000638 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000639 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000640 if (component->driver->resume)
641 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000642 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100643 }
644 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000646 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100647
Mengdong Lin1a497982015-11-18 02:34:11 -0500648 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100649 continue;
650
Mengdong Lin1a497982015-11-18 02:34:11 -0500651 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000652 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800653 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000656 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800657 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658 }
659
Mark Brown3ff3f642008-05-19 12:32:25 +0200660 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000661 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000662 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100665 continue;
666
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000667 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200668 struct snd_soc_dai_driver *drv = dai->driver;
669
670 if (drv->ops->digital_mute && dai->playback_active)
671 drv->ops->digital_mute(dai, 0);
672 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673 }
674
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000675 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500676 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100677
Mengdong Lin1a497982015-11-18 02:34:11 -0500678 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100679 continue;
680
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100681 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 }
684
Mark Brown87506542008-11-18 20:50:34 +0000685 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000686 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000688 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100689
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200690 /* Recheck all endpoints too, their state is affected by suspend */
691 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700692 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530693
694 /* userspace can access us now we are back as we were before */
695 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100696}
697
698/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000699int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100700{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000701 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100702 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500703 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900704 struct snd_soc_dai *codec_dai;
705 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200706
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200707 /* If the card is not initialized yet there is nothing to do */
708 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800709 return 0;
710
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800711 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000712 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200713 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200714
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800715 if (cpu_dai->active)
716 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200717
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900718 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200719 if (codec_dai->active)
720 pinctrl_pm_select_default_state(codec_dai->dev);
721 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800722 }
723
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100724 /*
725 * DAIs that also act as the control bus master might have other drivers
726 * hanging off them so need to resume immediately. Other drivers don't
727 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100728 * due to I/O costs and anti-pop so handle them out of line.
729 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000730 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500731 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200732
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100733 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600734 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100735 if (bus_control) {
736 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600737 soc_resume_deferred(&card->deferred_resume_work);
738 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000739 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600740 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000741 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100742 }
Andy Green6ed25972008-06-13 16:24:05 +0100743
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 return 0;
745}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000746EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000748#define snd_soc_suspend NULL
749#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750#endif
751
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100752static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800753};
754
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900755static struct device_node
756*soc_component_to_node(struct snd_soc_component *component)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100757{
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900758 struct device_node *of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100759
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900760 of_node = component->dev->of_node;
761 if (!of_node && component->dev->parent)
762 of_node = component->dev->parent->of_node;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100763
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900764 return of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100765}
766
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000767static int snd_soc_is_matching_component(
768 const struct snd_soc_dai_link_component *dlc,
769 struct snd_soc_component *component)
770{
771 struct device_node *component_of_node;
772
Kuninori Morimoto7d7db5d2019-06-20 09:49:17 +0900773 if (!dlc)
774 return 0;
775
776 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000777
778 if (dlc->of_node && component_of_node != dlc->of_node)
779 return 0;
780 if (dlc->name && strcmp(component->name, dlc->name))
781 return 0;
782
783 return 1;
784}
785
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786static struct snd_soc_component *soc_find_component(
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900787 const struct snd_soc_dai_link_component *dlc)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788{
789 struct snd_soc_component *component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790
791 lockdep_assert_held(&client_mutex);
792
Kuninori Morimotoe3303262019-06-26 10:40:59 +0900793 /*
794 * NOTE
795 *
796 * It returns *1st* found component, but some driver
797 * has few components by same of_node/name
798 * ex)
799 * CPU component and generic DMAEngine component
800 */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +0900801 for_each_component(component)
802 if (snd_soc_is_matching_component(dlc, component))
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803 return component;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200804
805 return NULL;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100806}
807
Mengdong Linfbb88b52016-04-22 12:25:33 +0800808/**
809 * snd_soc_find_dai - Find a registered DAI
810 *
Jeffy Chen49584712017-08-22 15:57:21 +0800811 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800812 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700813 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800814 * find the DAI of the same name. The component's of_node and name
815 * should also match if being specified.
816 *
817 * Return: pointer of DAI, or NULL if not found.
818 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800819struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200820 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100821{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200822 struct snd_soc_component *component;
823 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100824
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100825 lockdep_assert_held(&client_mutex);
826
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200827 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000828 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000829 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200830 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000831 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800832 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800833 && (!dai->driver->name
834 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100835 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100836
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200837 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100838 }
839 }
840
841 return NULL;
842}
Mengdong Lin305e9022016-04-19 13:12:25 +0800843EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100844
Mengdong Lin17fb17552016-11-03 01:04:12 +0800845/**
846 * snd_soc_find_dai_link - Find a DAI link
847 *
848 * @card: soc card
849 * @id: DAI link ID to match
850 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000851 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800852 *
853 * This function will search all existing DAI links of the soc card to
854 * find the link of the same ID. Since DAI links may not have their
855 * unique ID, so name and stream name should also match if being
856 * specified.
857 *
858 * Return: pointer of DAI link, or NULL if not found.
859 */
860struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
861 int id, const char *name,
862 const char *stream_name)
863{
864 struct snd_soc_dai_link *link, *_link;
865
866 lockdep_assert_held(&client_mutex);
867
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000868 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800869 if (link->id != id)
870 continue;
871
872 if (name && (!link->name || strcmp(name, link->name)))
873 continue;
874
875 if (stream_name && (!link->stream_name
876 || strcmp(stream_name, link->stream_name)))
877 continue;
878
879 return link;
880 }
881
882 return NULL;
883}
884EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
885
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800886static bool soc_is_dai_link_bound(struct snd_soc_card *card,
887 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200888{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800889 struct snd_soc_pcm_runtime *rtd;
890
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000891 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800892 if (rtd->dai_link == dai_link)
893 return true;
894 }
895
896 return false;
897}
898
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500899static int soc_bind_dai_link(struct snd_soc_card *card,
900 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200901{
Mengdong Lin1a497982015-11-18 02:34:11 -0500902 struct snd_soc_pcm_runtime *rtd;
Jerome Brunet34614732019-06-27 14:13:50 +0200903 struct snd_soc_dai_link_component *codec, *platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000904 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906
Liam Girdwooda655de82018-07-02 16:59:54 +0100907 if (dai_link->ignore)
908 return 0;
909
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500910 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000911
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800912 if (soc_is_dai_link_bound(card, dai_link)) {
913 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
914 dai_link->name);
915 return 0;
916 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200917
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530918 rtd = soc_new_pcm_runtime(card, dai_link);
919 if (!rtd)
920 return -ENOMEM;
921
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900922 /* FIXME: we need multi CPU support in the future */
923 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000924 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100925 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900926 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500927 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000928 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000929 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000930
Benoit Cousson88bd8702014-07-08 23:19:34 +0200931 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900932 rtd->num_codecs = dai_link->num_codecs;
Jerome Brunet34614732019-06-27 14:13:50 +0200933 for_each_link_codecs(dai_link, i, codec) {
934 rtd->codec_dais[i] = snd_soc_find_dai(codec);
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900935 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100936 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Jerome Brunet34614732019-06-27 14:13:50 +0200937 codec->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500938 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200939 }
Jerome Brunet34614732019-06-27 14:13:50 +0200940
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900941 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000942 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100943
Benoit Cousson88bd8702014-07-08 23:19:34 +0200944 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900945 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100946
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900947 /* Find PLATFORM from registered PLATFORMs */
Jerome Brunet34614732019-06-27 14:13:50 +0200948 for_each_link_platforms(dai_link, i, platform) {
949 for_each_component(component) {
950 if (!snd_soc_is_matching_component(platform, component))
951 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000952
Jerome Brunet34614732019-06-27 14:13:50 +0200953 snd_soc_rtdcom_add(rtd, component);
954 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000955 }
956
Mengdong Lin1a497982015-11-18 02:34:11 -0500957 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000958 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500959
960_err_defer:
961 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200962 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963}
964
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900965static void soc_cleanup_component(struct snd_soc_component *component)
966{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200967 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900968 list_del(&component->card_list);
969 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
970 soc_cleanup_component_debugfs(component);
971 component->card = NULL;
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -0700972 if (!component->driver->module_get_upon_open)
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -0600973 module_put(component->dev->driver->owner);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900974}
975
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200976static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600977{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200978 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200979 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600980
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000981 if (component->driver->remove)
982 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600983
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900984 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600985}
986
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200987static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200988{
989 int err;
990
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600991 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000992 dai->driver->remove_order != order)
993 return;
994
995 if (dai->driver->remove) {
996 err = dai->driver->remove(dai);
997 if (err < 0)
998 dev_err(dai->dev,
999 "ASoC: failed to remove %s: %d\n",
1000 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001001 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +00001002 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001003}
1004
Mengdong Lin1a497982015-11-18 02:34:11 -05001005static void soc_remove_link_dais(struct snd_soc_card *card,
1006 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001007{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001008 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001009 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010
1011 /* unregister the rtd device */
1012 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001013 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014 rtd->dev_registered = 0;
1015 }
1016
1017 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001018 for_each_rtd_codec_dai(rtd, i, codec_dai)
1019 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001021 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022}
1023
Mengdong Lin1a497982015-11-18 02:34:11 -05001024static void soc_remove_link_components(struct snd_soc_card *card,
1025 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001026{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001027 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001028 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001030 for_each_rtdcom(rtd, rtdcom) {
1031 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001032
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001033 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +02001034 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001035 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001036}
1037
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001038static void soc_remove_dai_links(struct snd_soc_card *card)
1039{
Mengdong Lin1a497982015-11-18 02:34:11 -05001040 int order;
1041 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001042 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001043
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001044 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001045 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001046 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001047 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001048
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001049 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001050 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001051 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001052 }
1053
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001054 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001055 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1056 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1057 link->name);
1058
1059 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001060 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001061}
1062
Mengdong Lin923c5e612015-11-23 11:01:49 -05001063static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001064 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001065{
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001066 int i;
Jerome Brunet34614732019-06-27 14:13:50 +02001067 struct snd_soc_dai_link_component *codec, *platform;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001068
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001069 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001070 /*
1071 * Codec must be specified by 1 of name or OF node,
1072 * not both or neither.
1073 */
Jerome Brunet34614732019-06-27 14:13:50 +02001074 if (!!codec->name == !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001075 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1076 link->name);
1077 return -EINVAL;
1078 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001079
Mengdong Lin923c5e612015-11-23 11:01:49 -05001080 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001081 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001082 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1083 link->name);
1084 return -EINVAL;
1085 }
Jerome Brunetaf18b132019-06-27 14:13:49 +02001086
1087 /*
1088 * Defer card registration if codec component is not added to
1089 * component list.
1090 */
1091 if (!soc_find_component(codec))
1092 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001093 }
1094
Jerome Brunet34614732019-06-27 14:13:50 +02001095 for_each_link_platforms(link, i, platform) {
1096 /*
1097 * Platform may be specified by either name or OF node, but it
1098 * can be left unspecified, then no components will be inserted
1099 * in the rtdcom list
1100 */
1101 if (!!platform->name == !!platform->of_node) {
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001102 dev_err(card->dev,
Jerome Brunet34614732019-06-27 14:13:50 +02001103 "ASoC: Neither/both platform name/of_node are set for %s\n",
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001104 link->name);
1105 return -EINVAL;
1106 }
1107
1108 /*
Jerome Brunet34614732019-06-27 14:13:50 +02001109 * Defer card registration if platform component is not added to
1110 * component list.
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001111 */
Jerome Brunet34614732019-06-27 14:13:50 +02001112 if (!soc_find_component(platform))
Kuninori Morimoto1d768982019-06-19 10:14:07 +09001113 return -EPROBE_DEFER;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001114 }
1115
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001116 /* FIXME */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001117 if (link->num_cpus > 1) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001118 dev_err(card->dev,
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001119 "ASoC: multi cpu is not yet supported %s\n",
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001120 link->name);
1121 return -EINVAL;
1122 }
1123
Mengdong Lin923c5e612015-11-23 11:01:49 -05001124 /*
Mengdong Lin923c5e612015-11-23 11:01:49 -05001125 * CPU device may be specified by either name or OF node, but
1126 * can be left unspecified, and will be matched based on DAI
1127 * name alone..
1128 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001129 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001130 dev_err(card->dev,
1131 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1132 link->name);
1133 return -EINVAL;
1134 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301135
1136 /*
1137 * Defer card registartion if cpu dai component is not added to
1138 * component list.
1139 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001140 if ((link->cpus->of_node || link->cpus->name) &&
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001141 !soc_find_component(link->cpus))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301142 return -EPROBE_DEFER;
1143
Mengdong Lin923c5e612015-11-23 11:01:49 -05001144 /*
1145 * At least one of CPU DAI name or CPU device name/node must be
1146 * specified
1147 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001148 if (!link->cpus->dai_name &&
1149 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001150 dev_err(card->dev,
1151 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1152 link->name);
1153 return -EINVAL;
1154 }
1155
1156 return 0;
1157}
1158
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001159void snd_soc_disconnect_sync(struct device *dev)
1160{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001161 struct snd_soc_component *component =
1162 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001163
1164 if (!component || !component->card)
1165 return;
1166
1167 snd_card_disconnect_sync(component->card->snd_card);
1168}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001169EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001170
Mengdong Linf8f80362015-12-02 14:11:22 +08001171/**
1172 * snd_soc_add_dai_link - Add a DAI link dynamically
1173 * @card: The ASoC card to which the DAI link is added
1174 * @dai_link: The new DAI link to add
1175 *
1176 * This function adds a DAI link to the ASoC card's link list.
1177 *
1178 * Note: Topology can use this API to add DAI links when probing the
1179 * topology component. And machine drivers can still define static
1180 * DAI links in dai_link array.
1181 */
1182int snd_soc_add_dai_link(struct snd_soc_card *card,
1183 struct snd_soc_dai_link *dai_link)
1184{
1185 if (dai_link->dobj.type
1186 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1187 dev_err(card->dev, "Invalid dai link type %d\n",
1188 dai_link->dobj.type);
1189 return -EINVAL;
1190 }
1191
1192 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001193 /*
1194 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001195 * on the link created by topology.
1196 */
1197 if (dai_link->dobj.type && card->add_dai_link)
1198 card->add_dai_link(card, dai_link);
1199
Mengdong Linf8f80362015-12-02 14:11:22 +08001200 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001201
1202 return 0;
1203}
1204EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1205
1206/**
1207 * snd_soc_remove_dai_link - Remove a DAI link from the list
1208 * @card: The ASoC card that owns the link
1209 * @dai_link: The DAI link to remove
1210 *
1211 * This function removes a DAI link from the ASoC card's link list.
1212 *
1213 * For DAI links previously added by topology, topology should
1214 * remove them by using the dobj embedded in the link.
1215 */
1216void snd_soc_remove_dai_link(struct snd_soc_card *card,
1217 struct snd_soc_dai_link *dai_link)
1218{
1219 struct snd_soc_dai_link *link, *_link;
1220
1221 if (dai_link->dobj.type
1222 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1223 dev_err(card->dev, "Invalid dai link type %d\n",
1224 dai_link->dobj.type);
1225 return;
1226 }
1227
1228 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001229 /*
1230 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001231 * on the link created by topology.
1232 */
1233 if (dai_link->dobj.type && card->remove_dai_link)
1234 card->remove_dai_link(card, dai_link);
1235
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001236 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001237 if (link == dai_link) {
1238 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001239 return;
1240 }
1241 }
1242}
1243EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1244
Jerome Brunetaefba452018-07-13 14:50:43 +02001245static void soc_set_of_name_prefix(struct snd_soc_component *component)
1246{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001247 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001248 const char *str;
1249 int ret;
1250
Jerome Brunetaefba452018-07-13 14:50:43 +02001251 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1252 &str);
1253 if (!ret)
1254 component->name_prefix = str;
1255}
1256
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001257static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001258 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001259{
1260 int i;
1261
Jerome Brunetaefba452018-07-13 14:50:43 +02001262 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001263 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001264 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001265
1266 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001267 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001268 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001269 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001270 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001271 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001272 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001273
1274 /*
1275 * If there is no configuration table or no match in the table,
1276 * check if a prefix is provided in the node
1277 */
1278 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001279}
1280
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001281static int soc_probe_component(struct snd_soc_card *card,
1282 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001283{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001284 struct snd_soc_dapm_context *dapm =
1285 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001286 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001287 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001289 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001290 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001291
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001292 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001293 if (component->card != card) {
1294 dev_err(component->dev,
1295 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1296 card->name, component->card->name);
1297 return -ENODEV;
1298 }
1299 return 0;
1300 }
1301
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -07001302 if (!component->driver->module_get_upon_open &&
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -06001303 !try_module_get(component->dev->driver->owner))
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001304 return -ENODEV;
1305
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001306 component->card = card;
1307 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001308 INIT_LIST_HEAD(&component->card_list);
1309 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001310 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001311
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001312 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001313
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001314 if (component->driver->dapm_widgets) {
1315 ret = snd_soc_dapm_new_controls(dapm,
1316 component->driver->dapm_widgets,
1317 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001318
1319 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001320 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001321 "Failed to create new controls %d\n", ret);
1322 goto err_probe;
1323 }
1324 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001325
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001326 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001327 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001328 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001329 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001330 "Failed to create DAI widgets %d\n", ret);
1331 goto err_probe;
1332 }
1333 }
Mark Brown888df392012-02-16 19:37:51 -08001334
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001335 if (component->driver->probe) {
1336 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001338 dev_err(component->dev,
1339 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001340 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001341 }
1342 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001343 WARN(dapm->idle_bias_off &&
1344 dapm->bias_level != SND_SOC_BIAS_OFF,
1345 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1346 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001347
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001348 /* machine specific init */
1349 if (component->init) {
1350 ret = component->init(component);
1351 if (ret < 0) {
1352 dev_err(component->dev,
1353 "Failed to do machine specific init %d\n", ret);
1354 goto err_probe;
1355 }
1356 }
1357
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001358 if (component->driver->controls)
1359 snd_soc_add_component_controls(component,
1360 component->driver->controls,
1361 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001362 if (component->driver->dapm_routes)
1363 snd_soc_dapm_add_routes(dapm,
1364 component->driver->dapm_routes,
1365 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001366
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001367 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001368 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001369 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001370
Jarkko Nikula70d293312011-01-27 16:24:22 +02001371err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001372 if (ret < 0)
1373 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001374
1375 return ret;
1376}
1377
Mark Brown36ae1a92012-01-06 17:12:45 -08001378static void rtd_release(struct device *dev)
1379{
1380 kfree(dev);
1381}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001382
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001383static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1384 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001385{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001386 int ret = 0;
1387
Jarkko Nikula589c3562010-12-06 16:27:07 +02001388 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001389 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1390 if (!rtd->dev)
1391 return -ENOMEM;
1392 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001393 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001394 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001395 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001396 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001397 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001398 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001399 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1400 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1401 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1402 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001403 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001404 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001405 /* calling put_device() here to free the rtd->dev */
1406 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001407 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001408 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001409 return ret;
1410 }
1411 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001412 return 0;
1413}
1414
Mengdong Lin1a497982015-11-18 02:34:11 -05001415static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001416 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001417{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001418 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001419 struct snd_soc_rtdcom_list *rtdcom;
1420 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001421
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001422 for_each_rtdcom(rtd, rtdcom) {
1423 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001424
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001425 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001426 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001427 if (ret < 0)
1428 return ret;
1429 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001430 }
1431
Stephen Warren62ae68f2012-06-08 12:34:23 -06001432 return 0;
1433}
1434
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001435static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001436{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001437 if (dai->probed ||
1438 dai->driver->probe_order != order)
1439 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001440
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001441 if (dai->driver->probe) {
1442 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001443
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001444 if (ret < 0) {
1445 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1446 dai->name, ret);
1447 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001448 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001449 }
1450
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001451 dai->probed = 1;
1452
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001453 return 0;
1454}
1455
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001456static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1457 struct snd_soc_pcm_runtime *rtd)
1458{
1459 int i, ret = 0;
1460
1461 for (i = 0; i < num_dais; ++i) {
1462 struct snd_soc_dai_driver *drv = dais[i]->driver;
1463
Rohit kumarde17f142018-11-01 18:08:49 +05301464 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001465 ret = drv->pcm_new(rtd, dais[i]);
1466 if (ret < 0) {
1467 dev_err(dais[i]->dev,
1468 "ASoC: Failed to bind %s with pcm device\n",
1469 dais[i]->name);
1470 return ret;
1471 }
1472 }
1473
1474 return 0;
1475}
1476
Mengdong Lin1a497982015-11-18 02:34:11 -05001477static int soc_probe_link_dais(struct snd_soc_card *card,
1478 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001479{
Mengdong Lin1a497982015-11-18 02:34:11 -05001480 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001481 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001482 struct snd_soc_rtdcom_list *rtdcom;
1483 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001484 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001485 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001487 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001488 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 /* set default power off timeout */
1491 rtd->pmdown_time = pmdown_time;
1492
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001493 ret = soc_probe_dai(cpu_dai, order);
1494 if (ret)
1495 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001497 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001498 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1499 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001500 if (ret)
1501 return ret;
1502 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503
Liam Girdwood0168bf02011-06-07 16:08:05 +01001504 /* complete DAI probe during last probe */
1505 if (order != SND_SOC_COMP_ORDER_LAST)
1506 return 0;
1507
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001508 /* do machine specific initialization */
1509 if (dai_link->init) {
1510 ret = dai_link->init(rtd);
1511 if (ret < 0) {
1512 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1513 dai_link->name, ret);
1514 return ret;
1515 }
1516 }
1517
Ricard Wanderlof40aa5382019-07-24 11:38:44 +02001518 if (dai_link->dai_fmt) {
1519 ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1520 if (ret)
1521 return ret;
1522 }
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001523
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001524 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001525 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001527
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001528#ifdef CONFIG_DEBUG_FS
1529 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001530 if (dai_link->dynamic)
1531 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001532#endif
1533
Liam Girdwooda655de82018-07-02 16:59:54 +01001534 num = rtd->num;
1535
1536 /*
1537 * most drivers will register their PCMs using DAI link ordering but
1538 * topology based drivers can use the DAI link id field to set PCM
1539 * device number and then use rtd + a base offset of the BEs.
1540 */
1541 for_each_rtdcom(rtd, rtdcom) {
1542 component = rtdcom->component;
1543
1544 if (!component->driver->use_dai_pcm_id)
1545 continue;
1546
1547 if (rtd->dai_link->no_pcm)
1548 num += component->driver->be_pcm_base;
1549 else
1550 num = rtd->dai_link->id;
1551 }
1552
Jie Yang6f0c4222015-10-13 23:41:00 +08001553 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001554 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001555 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001556 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001557 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301558 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001559 return ret;
1560 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001561 } else if (!dai_link->params) {
1562 /* create the pcm */
1563 ret = soc_new_pcm(rtd, num);
1564 if (ret < 0) {
1565 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1566 dai_link->stream_name, ret);
1567 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001568 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001569 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1570 if (ret < 0)
1571 return ret;
1572 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1573 rtd->num_codecs, rtd);
1574 if (ret < 0)
1575 return ret;
1576 } else {
1577 INIT_DELAYED_WORK(&rtd->delayed_work,
1578 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001579 }
1580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 return 0;
1582}
1583
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001584static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001585{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001586 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001587 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001588 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001589
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001590 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1591 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001592 dlc.name = aux_dev->codec_name;
1593 dlc.of_node = aux_dev->codec_of_node;
1594 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001595 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001596 if (dlc.of_node)
1597 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001598 goto err_defer;
1599 }
1600 } else if (aux_dev->name) {
1601 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001602 dlc.name = aux_dev->name;
1603 dlc.of_node = NULL;
1604 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001605 if (!component)
1606 goto err_defer;
1607 } else {
1608 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1609 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001610 }
1611
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001612 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001613 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001614
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001615 return 0;
1616
1617err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001618 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001619 return -EPROBE_DEFER;
1620}
1621
1622static int soc_probe_aux_devices(struct snd_soc_card *card)
1623{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001624 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001625 int order;
1626 int ret;
1627
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001628 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001629 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001630 if (comp->driver->probe_order == order) {
1631 ret = soc_probe_component(card, comp);
1632 if (ret < 0) {
1633 dev_err(card->dev,
1634 "ASoC: failed to probe aux component %s %d\n",
1635 comp->name, ret);
1636 return ret;
1637 }
1638 }
1639 }
1640 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001641
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001642 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001643}
1644
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001645static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001646{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001647 struct snd_soc_component *comp, *_comp;
1648 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001649
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001650 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001651 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001652 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001653
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001654 if (comp->driver->remove_order == order) {
1655 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001656 /* remove it from the card's aux_comp_list */
1657 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001658 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001659 }
1660 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001661}
1662
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001663/**
1664 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1665 * @rtd: The runtime for which the DAI link format should be changed
1666 * @dai_fmt: The new DAI link format
1667 *
1668 * This function updates the DAI link format for all DAIs connected to the DAI
1669 * link for the specified runtime.
1670 *
1671 * Note: For setups with a static format set the dai_fmt field in the
1672 * corresponding snd_dai_link struct instead of using this function.
1673 *
1674 * Returns 0 on success, otherwise a negative error code.
1675 */
1676int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1677 unsigned int dai_fmt)
1678{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001679 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001680 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001681 unsigned int i;
1682 int ret;
1683
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001684 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001685 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1686 if (ret != 0 && ret != -ENOTSUPP) {
1687 dev_warn(codec_dai->dev,
1688 "ASoC: Failed to set DAI format: %d\n", ret);
1689 return ret;
1690 }
1691 }
1692
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001693 /*
1694 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1695 * the component which has non_legacy_dai_naming is Codec
1696 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001697 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001698 unsigned int inv_dai_fmt;
1699
1700 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1701 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1702 case SND_SOC_DAIFMT_CBM_CFM:
1703 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1704 break;
1705 case SND_SOC_DAIFMT_CBM_CFS:
1706 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1707 break;
1708 case SND_SOC_DAIFMT_CBS_CFM:
1709 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1710 break;
1711 case SND_SOC_DAIFMT_CBS_CFS:
1712 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1713 break;
1714 }
1715
1716 dai_fmt = inv_dai_fmt;
1717 }
1718
1719 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1720 if (ret != 0 && ret != -ENOTSUPP) {
1721 dev_warn(cpu_dai->dev,
1722 "ASoC: Failed to set DAI format: %d\n", ret);
1723 return ret;
1724 }
1725
1726 return 0;
1727}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001728EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001729
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001730#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001731/*
1732 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001733 * separate different DMI fields in the card long name. Only number and
1734 * alphabet characters and a few separator characters are kept.
1735 */
1736static void cleanup_dmi_name(char *name)
1737{
1738 int i, j = 0;
1739
1740 for (i = 0; name[i]; i++) {
1741 if (isalnum(name[i]) || (name[i] == '.')
1742 || (name[i] == '_'))
1743 name[j++] = name[i];
1744 else if (name[i] == '-')
1745 name[j++] = '_';
1746 }
1747
1748 name[j] = '\0';
1749}
1750
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001751/*
1752 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001753 * in the black list.
1754 */
1755static int is_dmi_valid(const char *field)
1756{
1757 int i = 0;
1758
1759 while (dmi_blacklist[i]) {
1760 if (strstr(field, dmi_blacklist[i]))
1761 return 0;
1762 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001763 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001764
1765 return 1;
1766}
1767
Liam Girdwood345233d2017-01-14 16:13:02 +08001768/**
1769 * snd_soc_set_dmi_name() - Register DMI names to card
1770 * @card: The card to register DMI names
1771 * @flavour: The flavour "differentiator" for the card amongst its peers.
1772 *
1773 * An Intel machine driver may be used by many different devices but are
1774 * difficult for userspace to differentiate, since machine drivers ususally
1775 * use their own name as the card short name and leave the card long name
1776 * blank. To differentiate such devices and fix bugs due to lack of
1777 * device-specific configurations, this function allows DMI info to be used
1778 * as the sound card long name, in the format of
1779 * "vendor-product-version-board"
1780 * (Character '-' is used to separate different DMI fields here).
1781 * This will help the user space to load the device-specific Use Case Manager
1782 * (UCM) configurations for the card.
1783 *
1784 * Possible card long names may be:
1785 * DellInc.-XPS139343-01-0310JH
1786 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1787 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1788 *
1789 * This function also supports flavoring the card longname to provide
1790 * the extra differentiation, like "vendor-product-version-board-flavor".
1791 *
1792 * We only keep number and alphabet characters and a few separator characters
1793 * in the card long name since UCM in the user space uses the card long names
1794 * as card configuration directory names and AudoConf cannot support special
1795 * charactors like SPACE.
1796 *
1797 * Returns 0 on success, otherwise a negative error code.
1798 */
1799int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1800{
1801 const char *vendor, *product, *product_version, *board;
1802 size_t longname_buf_size = sizeof(card->snd_card->longname);
1803 size_t len;
1804
1805 if (card->long_name)
1806 return 0; /* long name already set by driver or from DMI */
1807
1808 /* make up dmi long name as: vendor.product.version.board */
1809 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001810 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001811 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1812 return 0;
1813 }
1814
1815 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1816 "%s", vendor);
1817 cleanup_dmi_name(card->dmi_longname);
1818
1819 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001820 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001821 len = strlen(card->dmi_longname);
1822 snprintf(card->dmi_longname + len,
1823 longname_buf_size - len,
1824 "-%s", product);
1825
1826 len++; /* skip the separator "-" */
1827 if (len < longname_buf_size)
1828 cleanup_dmi_name(card->dmi_longname + len);
1829
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001830 /*
1831 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001832 * name in the product version field
1833 */
1834 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001835 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001836 len = strlen(card->dmi_longname);
1837 snprintf(card->dmi_longname + len,
1838 longname_buf_size - len,
1839 "-%s", product_version);
1840
1841 len++;
1842 if (len < longname_buf_size)
1843 cleanup_dmi_name(card->dmi_longname + len);
1844 }
1845 }
1846
1847 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001848 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001849 len = strlen(card->dmi_longname);
1850 snprintf(card->dmi_longname + len,
1851 longname_buf_size - len,
1852 "-%s", board);
1853
1854 len++;
1855 if (len < longname_buf_size)
1856 cleanup_dmi_name(card->dmi_longname + len);
1857 } else if (!product) {
1858 /* fall back to using legacy name */
1859 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1860 return 0;
1861 }
1862
1863 /* Add flavour to dmi long name */
1864 if (flavour) {
1865 len = strlen(card->dmi_longname);
1866 snprintf(card->dmi_longname + len,
1867 longname_buf_size - len,
1868 "-%s", flavour);
1869
1870 len++;
1871 if (len < longname_buf_size)
1872 cleanup_dmi_name(card->dmi_longname + len);
1873 }
1874
1875 /* set the card long name */
1876 card->long_name = card->dmi_longname;
1877
1878 return 0;
1879}
1880EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001881#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001882
Liam Girdwooda655de82018-07-02 16:59:54 +01001883static void soc_check_tplg_fes(struct snd_soc_card *card)
1884{
1885 struct snd_soc_component *component;
1886 const struct snd_soc_component_driver *comp_drv;
1887 struct snd_soc_dai_link *dai_link;
1888 int i;
1889
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001890 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001891
1892 /* does this component override FEs ? */
1893 if (!component->driver->ignore_machine)
1894 continue;
1895
1896 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001897 if (!strcmp(component->driver->ignore_machine,
1898 card->dev->driver->name))
1899 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001900 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001901 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001902 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001903match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001904 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001905 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001906
1907 /* ignore this FE */
1908 if (dai_link->dynamic) {
1909 dai_link->ignore = true;
1910 continue;
1911 }
1912
1913 dev_info(card->dev, "info: override FE DAI link %s\n",
1914 card->dai_link[i].name);
1915
1916 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001917 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001918 dev_err(card->dev, "init platform error");
1919 continue;
1920 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001921 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001922
1923 /* convert non BE into BE */
1924 dai_link->no_pcm = 1;
1925
1926 /* override any BE fixups */
1927 dai_link->be_hw_params_fixup =
1928 component->driver->be_hw_params_fixup;
1929
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001930 /*
1931 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001932 * dai link name if it's NULL to help bind widgets.
1933 */
1934 if (!dai_link->stream_name)
1935 dai_link->stream_name = dai_link->name;
1936 }
1937
1938 /* Inform userspace we are using alternate topology */
1939 if (component->driver->topology_name_prefix) {
1940
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001941 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001942 if (!card->topology_shortname_created) {
1943 comp_drv = component->driver;
1944
1945 snprintf(card->topology_shortname, 32, "%s-%s",
1946 comp_drv->topology_name_prefix,
1947 card->name);
1948 card->topology_shortname_created = true;
1949 }
1950
1951 /* use topology shortname */
1952 card->name = card->topology_shortname;
1953 }
1954 }
1955}
1956
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001957static int soc_cleanup_card_resources(struct snd_soc_card *card)
1958{
1959 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001960 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001961 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001962 card->snd_card = NULL;
1963 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001964
1965 /* remove and free each DAI */
1966 soc_remove_dai_links(card);
1967 soc_remove_pcm_runtimes(card);
1968
1969 /* remove auxiliary devices */
1970 soc_remove_aux_devices(card);
1971
1972 snd_soc_dapm_free(&card->dapm);
1973 soc_cleanup_card_debugfs(card);
1974
1975 /* remove the card */
1976 if (card->remove)
1977 card->remove(card);
1978
1979 return 0;
1980}
1981
Mark Brownb19e6e72012-03-14 21:18:39 +00001982static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983{
Mengdong Lin1a497982015-11-18 02:34:11 -05001984 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001985 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001986 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001987
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001988 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001989 for_each_card_prelinks(card, i, dai_link) {
1990 ret = soc_init_dai_link(card, dai_link);
1991 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001992 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1993 dai_link->name, ret);
1994 mutex_unlock(&client_mutex);
1995 return ret;
1996 }
1997 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001998 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002000 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2001 card->dapm.dev = card->dev;
2002 card->dapm.card = card;
2003 list_add(&card->dapm.list, &card->dapm_list);
2004
Liam Girdwooda655de82018-07-02 16:59:54 +01002005 /* check whether any platform is ignore machine FE and using topology */
2006 soc_check_tplg_fes(card);
2007
Mark Brownb19e6e72012-03-14 21:18:39 +00002008 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002009 for_each_card_prelinks(card, i, dai_link) {
2010 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002011 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002012 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002013 }
2014
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002015 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002016 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002017 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002018 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002019 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 }
2021
Mengdong Linf8f80362015-12-02 14:11:22 +08002022 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002023 for_each_card_prelinks(card, i, dai_link)
2024 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002025
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002026 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002027 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002028 card->owner, 0, &card->snd_card);
2029 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002030 dev_err(card->dev,
2031 "ASoC: can't create sound card for card %s: %d\n",
2032 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002033 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002035
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002036 soc_init_card_debugfs(card);
2037
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002038#ifdef CONFIG_DEBUG_FS
2039 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2040#endif
2041
Mark Brown88ee1c62011-02-02 10:43:26 +00002042#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002043 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002044 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002045#endif
Andy Green6ed25972008-06-13 16:24:05 +01002046
Mark Brown9a841eb2011-04-12 17:51:37 -07002047 if (card->dapm_widgets)
2048 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2049 card->num_dapm_widgets);
2050
Nicolin Chenf23e8602015-02-14 17:22:49 -08002051 if (card->of_dapm_widgets)
2052 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2053 card->num_of_dapm_widgets);
2054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 /* initialise the sound card only once */
2056 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002057 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002058 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002059 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002060 }
2061
Stephen Warren62ae68f2012-06-08 12:34:23 -06002062 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002063 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002064 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002065 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002066 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002067 dev_err(card->dev,
2068 "ASoC: failed to instantiate card %d\n",
2069 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002070 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002071 }
2072 }
2073 }
2074
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002075 /* probe auxiliary components */
2076 ret = soc_probe_aux_devices(card);
2077 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002078 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002079
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002080 /*
2081 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002082 * Components with topology may bring new DAIs and DAI links.
2083 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002084 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002085 if (soc_is_dai_link_bound(card, dai_link))
2086 continue;
2087
2088 ret = soc_init_dai_link(card, dai_link);
2089 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002090 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002091 ret = soc_bind_dai_link(card, dai_link);
2092 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002093 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002094 }
2095
Stephen Warren62ae68f2012-06-08 12:34:23 -06002096 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002097 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002098 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002099 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002100 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002101 dev_err(card->dev,
2102 "ASoC: failed to instantiate card %d\n",
2103 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002104 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002105 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002106 }
2107 }
2108
Mark Brown888df392012-02-16 19:37:51 -08002109 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002110 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002111
Mark Brownb7af1da2011-04-07 19:18:44 +09002112 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002113 snd_soc_add_card_controls(card, card->controls,
2114 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002115
Mark Brownb8ad29d2011-03-02 18:35:51 +00002116 if (card->dapm_routes)
2117 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2118 card->num_dapm_routes);
2119
Nicolin Chenf23e8602015-02-14 17:22:49 -08002120 if (card->of_dapm_routes)
2121 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2122 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002123
Takashi Iwai861886d2017-04-24 08:54:42 +02002124 /* try to set some sane longname if DMI is available */
2125 snd_soc_set_dmi_name(card, NULL);
2126
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002128 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002129 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2130 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002131 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2132 "%s", card->driver_name ? card->driver_name : card->name);
2133 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2134 switch (card->snd_card->driver[i]) {
2135 case '_':
2136 case '-':
2137 case '\0':
2138 break;
2139 default:
2140 if (!isalnum(card->snd_card->driver[i]))
2141 card->snd_card->driver[i] = '_';
2142 break;
2143 }
2144 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002145
Mark Brown28e9ad92011-03-02 18:36:34 +00002146 if (card->late_probe) {
2147 ret = card->late_probe(card);
2148 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002149 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002150 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002151 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002152 }
2153 }
2154
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002155 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002157 ret = snd_card_register(card->snd_card);
2158 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002159 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2160 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002161 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002162 }
2163
Mark Brown435c5e22008-12-04 15:32:53 +00002164 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002165 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002166 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002167
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002168probe_end:
2169 if (ret < 0)
2170 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002172 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002173 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002174
Mark Brownb19e6e72012-03-14 21:18:39 +00002175 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002176}
2177
2178/* probes a new socdev */
2179static int soc_probe(struct platform_device *pdev)
2180{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002181 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002182
Vinod Koul70a7ca32011-01-14 19:22:48 +05302183 /*
2184 * no card, so machine driver should be registering card
2185 * we should not be here in that case so ret error
2186 */
2187 if (!card)
2188 return -EINVAL;
2189
Mark Brownfe4085e2012-03-02 13:07:41 +00002190 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002191 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002192 card->name);
2193
Mark Brown435c5e22008-12-04 15:32:53 +00002194 /* Bodge while we unpick instantiation */
2195 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002196
Mark Brown28d528c2012-08-09 18:45:23 +01002197 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198}
2199
2200/* removes a socdev */
2201static int soc_remove(struct platform_device *pdev)
2202{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002203 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002204
Mark Brownc5af3a22008-11-28 13:29:45 +00002205 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 return 0;
2207}
2208
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002209int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002210{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002211 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002212 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002213
2214 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002215 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002216
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002217 /*
2218 * Flush out pmdown_time work - we actually do want to run it
2219 * now, we're shutting down so no imminent restart.
2220 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002221 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002222
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002223 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002224
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002225 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002226 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002227 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002228 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002229 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002230
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002231 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002232 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002233 pinctrl_pm_select_sleep_state(codec_dai->dev);
2234 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002235 }
2236
Mark Brown416356f2009-06-30 19:05:15 +01002237 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002238}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002239EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002240
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002241const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302242 .suspend = snd_soc_suspend,
2243 .resume = snd_soc_resume,
2244 .freeze = snd_soc_suspend,
2245 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002246 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302247 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002248};
Stephen Warrendeb26072011-04-05 19:35:30 -06002249EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002250
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251/* ASoC platform driver */
2252static struct platform_driver soc_driver = {
2253 .driver = {
2254 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002255 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 },
2257 .probe = soc_probe,
2258 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259};
2260
Mark Brown096e49d2009-07-05 15:12:22 +01002261/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262 * snd_soc_cnew - create new control
2263 * @_template: control template
2264 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002265 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 *
2268 * Create a new mixer control from a template control.
2269 *
2270 * Returns 0 for success, else error.
2271 */
2272struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002273 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002274 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275{
2276 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002277 struct snd_kcontrol *kcontrol;
2278 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279
2280 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281 template.index = 0;
2282
Mark Brownefb7ac32011-03-08 17:23:24 +00002283 if (!long_name)
2284 long_name = template.name;
2285
2286 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002287 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002288 if (!name)
2289 return NULL;
2290
Mark Brownefb7ac32011-03-08 17:23:24 +00002291 template.name = name;
2292 } else {
2293 template.name = long_name;
2294 }
2295
2296 kcontrol = snd_ctl_new1(&template, data);
2297
2298 kfree(name);
2299
2300 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301}
2302EXPORT_SYMBOL_GPL(snd_soc_cnew);
2303
Liam Girdwood022658b2012-02-03 17:43:09 +00002304static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2305 const struct snd_kcontrol_new *controls, int num_controls,
2306 const char *prefix, void *data)
2307{
2308 int err, i;
2309
2310 for (i = 0; i < num_controls; i++) {
2311 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002312
Liam Girdwood022658b2012-02-03 17:43:09 +00002313 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2314 control->name, prefix));
2315 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002316 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2317 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002318 return err;
2319 }
2320 }
2321
2322 return 0;
2323}
2324
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002325struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2326 const char *name)
2327{
2328 struct snd_card *card = soc_card->snd_card;
2329 struct snd_kcontrol *kctl;
2330
2331 if (unlikely(!name))
2332 return NULL;
2333
2334 list_for_each_entry(kctl, &card->controls, list)
2335 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2336 return kctl;
2337 return NULL;
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2340
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002342 * snd_soc_add_component_controls - Add an array of controls to a component.
2343 *
2344 * @component: Component to add controls to
2345 * @controls: Array of controls to add
2346 * @num_controls: Number of elements in the array
2347 *
2348 * Return: 0 for success, else error.
2349 */
2350int snd_soc_add_component_controls(struct snd_soc_component *component,
2351 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2352{
2353 struct snd_card *card = component->card->snd_card;
2354
2355 return snd_soc_add_controls(card, component->dev, controls,
2356 num_controls, component->name_prefix, component);
2357}
2358EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2359
2360/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002361 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2362 * Convenience function to add a list of controls.
2363 *
2364 * @soc_card: SoC card to add controls to
2365 * @controls: array of controls to add
2366 * @num_controls: number of elements in the array
2367 *
2368 * Return 0 for success, else error.
2369 */
2370int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2371 const struct snd_kcontrol_new *controls, int num_controls)
2372{
2373 struct snd_card *card = soc_card->snd_card;
2374
2375 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2376 NULL, soc_card);
2377}
2378EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2379
2380/**
2381 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2382 * Convienience function to add a list of controls.
2383 *
2384 * @dai: DAI to add controls to
2385 * @controls: array of controls to add
2386 * @num_controls: number of elements in the array
2387 *
2388 * Return 0 for success, else error.
2389 */
2390int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2391 const struct snd_kcontrol_new *controls, int num_controls)
2392{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002393 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002394
2395 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2396 NULL, dai);
2397}
2398EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2399
2400/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002401 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2402 * @dai: DAI
2403 * @clk_id: DAI specific clock ID
2404 * @freq: new clock frequency in Hz
2405 * @dir: new clock direction - input/output.
2406 *
2407 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2408 */
2409int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2410 unsigned int freq, int dir)
2411{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002412 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002413 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002414
2415 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2416 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002417}
2418EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2419
2420/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002421 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2422 * @component: COMPONENT
2423 * @clk_id: DAI specific clock ID
2424 * @source: Source for the clock
2425 * @freq: new clock frequency in Hz
2426 * @dir: new clock direction - input/output.
2427 *
2428 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2429 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002430int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2431 int clk_id, int source, unsigned int freq,
2432 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002433{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002434 if (component->driver->set_sysclk)
2435 return component->driver->set_sysclk(component, clk_id, source,
2436 freq, dir);
2437
2438 return -ENOTSUPP;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2441
2442/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002443 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2444 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002445 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002446 * @div: new clock divisor.
2447 *
2448 * Configures the clock dividers. This is used to derive the best DAI bit and
2449 * frame clocks from the system or master clock. It's best to set the DAI bit
2450 * and frame clocks as low as possible to save system power.
2451 */
2452int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2453 int div_id, int div)
2454{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002455 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002456 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002457 else
2458 return -EINVAL;
2459}
2460EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2461
2462/**
2463 * snd_soc_dai_set_pll - configure DAI PLL.
2464 * @dai: DAI
2465 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002466 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002467 * @freq_in: PLL input clock frequency in Hz
2468 * @freq_out: requested PLL output clock frequency in Hz
2469 *
2470 * Configures and enables PLL to generate output clock based on input clock.
2471 */
Mark Brown85488032009-09-05 18:52:16 +01002472int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2473 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002474{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002475 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002476 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002477 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002478
2479 return snd_soc_component_set_pll(dai->component, pll_id, source,
2480 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002481}
2482EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2483
Mark Brownec4ee522011-03-07 20:58:11 +00002484/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002485 * snd_soc_component_set_pll - configure component PLL.
2486 * @component: COMPONENT
2487 * @pll_id: DAI specific PLL ID
2488 * @source: DAI specific source for the PLL
2489 * @freq_in: PLL input clock frequency in Hz
2490 * @freq_out: requested PLL output clock frequency in Hz
2491 *
2492 * Configures and enables PLL to generate output clock based on input clock.
2493 */
2494int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2495 int source, unsigned int freq_in,
2496 unsigned int freq_out)
2497{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002498 if (component->driver->set_pll)
2499 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002500 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002501
2502 return -EINVAL;
2503}
2504EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2505
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002506/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002507 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2508 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002509 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002510 *
2511 * Configures the DAI for a preset BCLK to sample rate ratio.
2512 */
2513int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2514{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002515 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002516 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2517 else
2518 return -EINVAL;
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2521
2522/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002523 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2524 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002525 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002526 *
2527 * Configures the DAI hardware format and clocking.
2528 */
2529int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2530{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002531 if (dai->driver->ops->set_fmt == NULL)
2532 return -ENOTSUPP;
2533 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002534}
2535EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2536
2537/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002538 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002539 * @slots: Number of slots in use.
2540 * @tx_mask: bitmask representing active TX slots.
2541 * @rx_mask: bitmask representing active RX slots.
2542 *
2543 * Generates the TDM tx and rx slot default masks for DAI.
2544 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002545static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002546 unsigned int *tx_mask,
2547 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002548{
2549 if (*tx_mask || *rx_mask)
2550 return 0;
2551
2552 if (!slots)
2553 return -EINVAL;
2554
2555 *tx_mask = (1 << slots) - 1;
2556 *rx_mask = (1 << slots) - 1;
2557
2558 return 0;
2559}
2560
2561/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002562 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2563 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002564 * @tx_mask: bitmask representing active TX slots.
2565 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002566 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002567 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002568 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002569 * This function configures the specified DAI for TDM operation. @slot contains
2570 * the total number of slots of the TDM stream and @slot_with the width of each
2571 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2572 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2573 * DAI should write to or read from. If a bit is set the corresponding slot is
2574 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2575 * the first slot, bit 1 to the second slot and so on. The first active slot
2576 * maps to the first channel of the DAI, the second active slot to the second
2577 * channel and so on.
2578 *
2579 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2580 * @rx_mask and @slot_width will be ignored.
2581 *
2582 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002583 */
2584int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002585 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002586{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002587 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002588 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002589 &tx_mask, &rx_mask);
2590 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002591 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002592
Benoit Cousson88bd8702014-07-08 23:19:34 +02002593 dai->tx_mask = tx_mask;
2594 dai->rx_mask = rx_mask;
2595
Kuninori Morimoto46471922017-09-25 01:38:34 +00002596 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002597 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002598 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002599 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002600 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002601}
2602EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2603
2604/**
Barry Song472df3c2009-09-12 01:16:29 +08002605 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2606 * @dai: DAI
2607 * @tx_num: how many TX channels
2608 * @tx_slot: pointer to an array which imply the TX slot number channel
2609 * 0~num-1 uses
2610 * @rx_num: how many RX channels
2611 * @rx_slot: pointer to an array which imply the RX slot number channel
2612 * 0~num-1 uses
2613 *
2614 * configure the relationship between channel number and TDM slot number.
2615 */
2616int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2617 unsigned int tx_num, unsigned int *tx_slot,
2618 unsigned int rx_num, unsigned int *rx_slot)
2619{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002620 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002621 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002622 rx_num, rx_slot);
2623 else
Srinivas Kandagatla74b35a72019-07-03 13:30:02 +01002624 return -ENOTSUPP;
Barry Song472df3c2009-09-12 01:16:29 +08002625}
2626EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2627
2628/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002629 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2630 * @dai: DAI
2631 * @tx_num: how many TX channels
2632 * @tx_slot: pointer to an array which imply the TX slot number channel
2633 * 0~num-1 uses
2634 * @rx_num: how many RX channels
2635 * @rx_slot: pointer to an array which imply the RX slot number channel
2636 * 0~num-1 uses
2637 */
2638int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2639 unsigned int *tx_num, unsigned int *tx_slot,
2640 unsigned int *rx_num, unsigned int *rx_slot)
2641{
2642 if (dai->driver->ops->get_channel_map)
2643 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2644 rx_num, rx_slot);
2645 else
2646 return -ENOTSUPP;
2647}
2648EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2649
2650/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002651 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2652 * @dai: DAI
2653 * @tristate: tristate enable
2654 *
2655 * Tristates the DAI so that others can use it.
2656 */
2657int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2658{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002659 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002660 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002661 else
2662 return -EINVAL;
2663}
2664EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2665
2666/**
2667 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2668 * @dai: DAI
2669 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002670 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002671 *
2672 * Mutes the DAI DAC.
2673 */
Mark Brownda183962013-02-06 15:44:07 +00002674int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2675 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002676{
Mark Brownda183962013-02-06 15:44:07 +00002677 if (dai->driver->ops->mute_stream)
2678 return dai->driver->ops->mute_stream(dai, mute, direction);
2679 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2680 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002681 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002682 else
Mark Brown04570c62012-04-13 19:16:03 +01002683 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002684}
2685EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2686
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002687static int snd_soc_bind_card(struct snd_soc_card *card)
2688{
2689 struct snd_soc_pcm_runtime *rtd;
2690 int ret;
2691
2692 ret = snd_soc_instantiate_card(card);
2693 if (ret != 0)
2694 return ret;
2695
2696 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002697 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002698 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2699 struct snd_soc_dai *codec_dai;
2700 int j;
2701
2702 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2703 if (!codec_dai->active)
2704 pinctrl_pm_select_sleep_state(codec_dai->dev);
2705 }
2706
2707 if (!cpu_dai->active)
2708 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2709 }
2710
2711 return ret;
2712}
2713
Mark Brownc5af3a22008-11-28 13:29:45 +00002714/**
2715 * snd_soc_register_card - Register a card with the ASoC core
2716 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002717 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002718 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002719 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302720int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002721{
2722 if (!card->name || !card->dev)
2723 return -EINVAL;
2724
Mark Browned77cc12011-05-03 18:25:34 +01002725 dev_set_drvdata(card->dev, card);
2726
Stephen Warren111c6412011-01-28 14:26:35 -07002727 snd_soc_initialize_card_lists(card);
2728
Mengdong Linf8f80362015-12-02 14:11:22 +08002729 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730
Mengdong Lin1a497982015-11-18 02:34:11 -05002731 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002732 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002733
Mark Browndb432b42011-10-03 21:06:40 +01002734 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002735 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736 card->instantiated = 0;
2737 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002738 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002739 spin_lock_init(&card->dpcm_lock);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002740
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002741 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002742}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302743EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002744
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002745static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2746{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002747 struct snd_soc_pcm_runtime *rtd;
2748 int order;
2749
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002750 if (card->instantiated) {
2751 card->instantiated = false;
2752 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002753 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002754
2755 /* remove all components used by DAI links on this card */
2756 for_each_comp_order(order) {
2757 for_each_card_rtds(card, rtd) {
2758 soc_remove_link_components(card, rtd, order);
2759 }
2760 }
2761
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002762 soc_cleanup_card_resources(card);
2763 if (!unregister)
2764 list_add(&card->list, &unbind_card_list);
2765 } else {
2766 if (unregister)
2767 list_del(&card->list);
2768 }
2769}
2770
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002771/**
2772 * snd_soc_unregister_card - Unregister a card with the ASoC core
2773 *
2774 * @card: Card to unregister
2775 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002776 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302777int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002778{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002779 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002780 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002781 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002782 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002783
2784 return 0;
2785}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302786EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002787
2788/*
2789 * Simplify DAI link configuration by removing ".-1" from device names
2790 * and sanitizing names.
2791 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002792static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002793{
2794 char *found, name[NAME_SIZE];
2795 int id1, id2;
2796
2797 if (dev_name(dev) == NULL)
2798 return NULL;
2799
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002800 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002801
2802 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002803 found = strstr(name, dev->driver->name);
2804 if (found) {
2805 /* get ID */
2806 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2807
2808 /* discard ID from name if ID == -1 */
2809 if (*id == -1)
2810 found[strlen(dev->driver->name)] = '\0';
2811 }
2812
2813 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002814 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002815 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2816 char tmp[NAME_SIZE];
2817
2818 /* create unique ID number from I2C addr and bus */
2819 *id = ((id1 & 0xffff) << 16) + id2;
2820
2821 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002822 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2823 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002824 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002825 } else
2826 *id = 0;
2827 }
2828
2829 return kstrdup(name, GFP_KERNEL);
2830}
2831
2832/*
2833 * Simplify DAI link naming for single devices with multiple DAIs by removing
2834 * any ".-1" and using the DAI name (instead of device name).
2835 */
2836static inline char *fmt_multiple_name(struct device *dev,
2837 struct snd_soc_dai_driver *dai_drv)
2838{
2839 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002840 dev_err(dev,
2841 "ASoC: error - multiple DAI %s registered with no name\n",
2842 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002843 return NULL;
2844 }
2845
2846 return kstrdup(dai_drv->name, GFP_KERNEL);
2847}
2848
2849/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002850 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002851 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002852 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002853 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002854static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002855{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002856 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002857
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002858 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002859 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2860 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002861 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002862 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002863 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002864 }
Mark Brown91151712008-11-30 23:31:24 +00002865}
Mark Brown91151712008-11-30 23:31:24 +00002866
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002867/* Create a DAI and add it to the component's DAI list */
2868static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2869 struct snd_soc_dai_driver *dai_drv,
2870 bool legacy_dai_naming)
2871{
2872 struct device *dev = component->dev;
2873 struct snd_soc_dai *dai;
2874
2875 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2876
2877 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2878 if (dai == NULL)
2879 return NULL;
2880
2881 /*
2882 * Back in the old days when we still had component-less DAIs,
2883 * instead of having a static name, component-less DAIs would
2884 * inherit the name of the parent device so it is possible to
2885 * register multiple instances of the DAI. We still need to keep
2886 * the same naming style even though those DAIs are not
2887 * component-less anymore.
2888 */
2889 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002890 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002891 dai->name = fmt_single_name(dev, &dai->id);
2892 } else {
2893 dai->name = fmt_multiple_name(dev, dai_drv);
2894 if (dai_drv->id)
2895 dai->id = dai_drv->id;
2896 else
2897 dai->id = component->num_dai;
2898 }
2899 if (dai->name == NULL) {
2900 kfree(dai);
2901 return NULL;
2902 }
2903
2904 dai->component = component;
2905 dai->dev = dev;
2906 dai->driver = dai_drv;
2907 if (!dai->driver->ops)
2908 dai->driver->ops = &null_dai_ops;
2909
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002910 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002911 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002912 component->num_dai++;
2913
2914 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2915 return dai;
2916}
2917
Mark Brown91151712008-11-30 23:31:24 +00002918/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002919 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002920 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002921 * @component: The component the DAIs are registered for
2922 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002923 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002924 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002925static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002926 struct snd_soc_dai_driver *dai_drv,
2927 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002928{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002929 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002930 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002931 unsigned int i;
2932 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002933
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002934 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002935
2936 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002937
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002938 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2939 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002940 if (dai == NULL) {
2941 ret = -ENOMEM;
2942 goto err;
2943 }
Mark Brown91151712008-11-30 23:31:24 +00002944 }
2945
2946 return 0;
2947
2948err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002949 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002950
2951 return ret;
2952}
Mark Brown91151712008-11-30 23:31:24 +00002953
Mengdong Lin68003e62015-12-31 16:40:43 +08002954/**
2955 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2956 *
2957 * @component: The component the DAIs are registered for
2958 * @dai_drv: DAI driver to use for the DAI
2959 *
2960 * Topology can use this API to register DAIs when probing a component.
2961 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2962 * will be freed in the component cleanup.
2963 */
2964int snd_soc_register_dai(struct snd_soc_component *component,
2965 struct snd_soc_dai_driver *dai_drv)
2966{
2967 struct snd_soc_dapm_context *dapm =
2968 snd_soc_component_get_dapm(component);
2969 struct snd_soc_dai *dai;
2970 int ret;
2971
2972 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2973 dev_err(component->dev, "Invalid dai type %d\n",
2974 dai_drv->dobj.type);
2975 return -EINVAL;
2976 }
2977
2978 lockdep_assert_held(&client_mutex);
2979 dai = soc_add_dai(component, dai_drv, false);
2980 if (!dai)
2981 return -ENOMEM;
2982
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002983 /*
2984 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002985 * also add routes that need these widgets as source or sink.
2986 */
2987 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2988 if (ret != 0) {
2989 dev_err(component->dev,
2990 "Failed to create DAI widgets %d\n", ret);
2991 }
2992
2993 return ret;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2996
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002997static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2998 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002999{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003000 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003001
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003002 component->driver->seq_notifier(component, type, subseq);
3003}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003004
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003005static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3006 int event)
3007{
3008 struct snd_soc_component *component = dapm->component;
3009
3010 return component->driver->stream_event(component, event);
3011}
3012
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003013static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3014 enum snd_soc_bias_level level)
3015{
3016 struct snd_soc_component *component = dapm->component;
3017
3018 return component->driver->set_bias_level(component, level);
3019}
3020
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003021static int snd_soc_component_initialize(struct snd_soc_component *component,
3022 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003023{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003024 struct snd_soc_dapm_context *dapm;
3025
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003026 component->name = fmt_single_name(dev, &component->id);
3027 if (!component->name) {
3028 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003029 return -ENOMEM;
3030 }
3031
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003032 component->dev = dev;
3033 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003034
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003035 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003036 dapm->dev = dev;
3037 dapm->component = component;
3038 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003039 dapm->idle_bias_off = !driver->idle_bias_on;
3040 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003041 if (driver->seq_notifier)
3042 dapm->seq_notifier = snd_soc_component_seq_notifier;
3043 if (driver->stream_event)
3044 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003045 if (driver->set_bias_level)
3046 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003048 INIT_LIST_HEAD(&component->dai_list);
3049 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003050
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003051 return 0;
3052}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003053
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003054static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003055{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003056 int val_bytes = regmap_get_val_bytes(component->regmap);
3057
3058 /* Errors are legitimate for non-integer byte multiples */
3059 if (val_bytes > 0)
3060 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003061}
3062
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003063#ifdef CONFIG_REGMAP
3064
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003065/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003066 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3067 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003068 * @component: The component for which to initialize the regmap instance
3069 * @regmap: The regmap instance that should be used by the component
3070 *
3071 * This function allows deferred assignment of the regmap instance that is
3072 * associated with the component. Only use this if the regmap instance is not
3073 * yet ready when the component is registered. The function must also be called
3074 * before the first IO attempt of the component.
3075 */
3076void snd_soc_component_init_regmap(struct snd_soc_component *component,
3077 struct regmap *regmap)
3078{
3079 component->regmap = regmap;
3080 snd_soc_component_setup_regmap(component);
3081}
3082EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3083
3084/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003085 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3086 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003087 * @component: The component for which to de-initialize the regmap instance
3088 *
3089 * Calls regmap_exit() on the regmap instance associated to the component and
3090 * removes the regmap instance from the component.
3091 *
3092 * This function should only be used if snd_soc_component_init_regmap() was used
3093 * to initialize the regmap instance.
3094 */
3095void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3096{
3097 regmap_exit(component->regmap);
3098 component->regmap = NULL;
3099}
3100EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3101
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003102#endif
3103
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003104static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003105{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003106 mutex_lock(&client_mutex);
3107
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003108 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003109 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003110 component->regmap = dev_get_regmap(component->dev,
3111 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003112 if (component->regmap)
3113 snd_soc_component_setup_regmap(component);
3114 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003115
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003116 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003117 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003118 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003119
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003120 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003121}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003122
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003123static void snd_soc_component_cleanup(struct snd_soc_component *component)
3124{
3125 snd_soc_unregister_dais(component);
3126 kfree(component->name);
3127}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003128
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003129static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3130{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003131 struct snd_soc_card *card = component->card;
3132
3133 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003134 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003135
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003136 list_del(&component->list);
3137}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003138
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003139#define ENDIANNESS_MAP(name) \
3140 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3141static u64 endianness_format_map[] = {
3142 ENDIANNESS_MAP(S16_),
3143 ENDIANNESS_MAP(U16_),
3144 ENDIANNESS_MAP(S24_),
3145 ENDIANNESS_MAP(U24_),
3146 ENDIANNESS_MAP(S32_),
3147 ENDIANNESS_MAP(U32_),
3148 ENDIANNESS_MAP(S24_3),
3149 ENDIANNESS_MAP(U24_3),
3150 ENDIANNESS_MAP(S20_3),
3151 ENDIANNESS_MAP(U20_3),
3152 ENDIANNESS_MAP(S18_3),
3153 ENDIANNESS_MAP(U18_3),
3154 ENDIANNESS_MAP(FLOAT_),
3155 ENDIANNESS_MAP(FLOAT64_),
3156 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3157};
3158
3159/*
3160 * Fix up the DAI formats for endianness: codecs don't actually see
3161 * the endianness of the data but we're using the CPU format
3162 * definitions which do need to include endianness so we ensure that
3163 * codec DAIs always have both big and little endian variants set.
3164 */
3165static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3166{
3167 int i;
3168
3169 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3170 if (stream->formats & endianness_format_map[i])
3171 stream->formats |= endianness_format_map[i];
3172}
3173
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003174static void snd_soc_try_rebind_card(void)
3175{
3176 struct snd_soc_card *card, *c;
3177
3178 if (!list_empty(&unbind_card_list)) {
3179 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3180 if (!snd_soc_bind_card(card))
3181 list_del(&card->list);
3182 }
3183 }
3184}
3185
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003186int snd_soc_add_component(struct device *dev,
3187 struct snd_soc_component *component,
3188 const struct snd_soc_component_driver *component_driver,
3189 struct snd_soc_dai_driver *dai_drv,
3190 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003191{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003192 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003193 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003194
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003195 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003196 if (ret)
3197 goto err_free;
3198
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003199 if (component_driver->endianness) {
3200 for (i = 0; i < num_dai; i++) {
3201 convert_endianness_formats(&dai_drv[i].playback);
3202 convert_endianness_formats(&dai_drv[i].capture);
3203 }
3204 }
3205
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003206 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003207 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003208 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003209 goto err_cleanup;
3210 }
3211
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003212 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003213 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003214
3215 return 0;
3216
3217err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003218 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003219err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003220 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003221}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003222EXPORT_SYMBOL_GPL(snd_soc_add_component);
3223
3224int snd_soc_register_component(struct device *dev,
3225 const struct snd_soc_component_driver *component_driver,
3226 struct snd_soc_dai_driver *dai_drv,
3227 int num_dai)
3228{
3229 struct snd_soc_component *component;
3230
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003231 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003232 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003233 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003234
3235 return snd_soc_add_component(dev, component, component_driver,
3236 dai_drv, num_dai);
3237}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003238EXPORT_SYMBOL_GPL(snd_soc_register_component);
3239
3240/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003241 * snd_soc_unregister_component - Unregister all related component
3242 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003243 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003244 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003245 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003246static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003247{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003248 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003249 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003250
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003251 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003252 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003253 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003254 continue;
3255
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003256 snd_soc_tplg_component_remove(component,
3257 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003258 snd_soc_component_del_unlocked(component);
3259 found = 1;
3260 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003261 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003262 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003263
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003264 if (found)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003265 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003266
3267 return found;
3268}
3269
3270void snd_soc_unregister_component(struct device *dev)
3271{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003272 while (__snd_soc_unregister_component(dev))
3273 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003274}
3275EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3276
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003277struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3278 const char *driver_name)
3279{
3280 struct snd_soc_component *component;
3281 struct snd_soc_component *ret;
3282
3283 ret = NULL;
3284 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003285 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003286 if (dev != component->dev)
3287 continue;
3288
3289 if (driver_name &&
3290 (driver_name != component->driver->name) &&
3291 (strcmp(component->driver->name, driver_name) != 0))
3292 continue;
3293
3294 ret = component;
3295 break;
3296 }
3297 mutex_unlock(&client_mutex);
3298
3299 return ret;
3300}
3301EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3302
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003303/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003304int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3305 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003306{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003307 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003308 int ret;
3309
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303310 if (!card->dev) {
3311 pr_err("card->dev is not set before calling %s\n", __func__);
3312 return -EINVAL;
3313 }
3314
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003315 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303316
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003317 ret = of_property_read_string_index(np, propname, 0, &card->name);
3318 /*
3319 * EINVAL means the property does not exist. This is fine providing
3320 * card->name was previously set, which is checked later in
3321 * snd_soc_register_card.
3322 */
3323 if (ret < 0 && ret != -EINVAL) {
3324 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003325 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003326 propname, ret);
3327 return ret;
3328 }
3329
3330 return 0;
3331}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003332EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003333
Xiubo Li9a6d4862014-02-08 15:59:52 +08003334static const struct snd_soc_dapm_widget simple_widgets[] = {
3335 SND_SOC_DAPM_MIC("Microphone", NULL),
3336 SND_SOC_DAPM_LINE("Line", NULL),
3337 SND_SOC_DAPM_HP("Headphone", NULL),
3338 SND_SOC_DAPM_SPK("Speaker", NULL),
3339};
3340
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003341int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003342 const char *propname)
3343{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003344 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003345 struct snd_soc_dapm_widget *widgets;
3346 const char *template, *wname;
3347 int i, j, num_widgets, ret;
3348
3349 num_widgets = of_property_count_strings(np, propname);
3350 if (num_widgets < 0) {
3351 dev_err(card->dev,
3352 "ASoC: Property '%s' does not exist\n", propname);
3353 return -EINVAL;
3354 }
3355 if (num_widgets & 1) {
3356 dev_err(card->dev,
3357 "ASoC: Property '%s' length is not even\n", propname);
3358 return -EINVAL;
3359 }
3360
3361 num_widgets /= 2;
3362 if (!num_widgets) {
3363 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3364 propname);
3365 return -EINVAL;
3366 }
3367
3368 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3369 GFP_KERNEL);
3370 if (!widgets) {
3371 dev_err(card->dev,
3372 "ASoC: Could not allocate memory for widgets\n");
3373 return -ENOMEM;
3374 }
3375
3376 for (i = 0; i < num_widgets; i++) {
3377 ret = of_property_read_string_index(np, propname,
3378 2 * i, &template);
3379 if (ret) {
3380 dev_err(card->dev,
3381 "ASoC: Property '%s' index %d read error:%d\n",
3382 propname, 2 * i, ret);
3383 return -EINVAL;
3384 }
3385
3386 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3387 if (!strncmp(template, simple_widgets[j].name,
3388 strlen(simple_widgets[j].name))) {
3389 widgets[i] = simple_widgets[j];
3390 break;
3391 }
3392 }
3393
3394 if (j >= ARRAY_SIZE(simple_widgets)) {
3395 dev_err(card->dev,
3396 "ASoC: DAPM widget '%s' is not supported\n",
3397 template);
3398 return -EINVAL;
3399 }
3400
3401 ret = of_property_read_string_index(np, propname,
3402 (2 * i) + 1,
3403 &wname);
3404 if (ret) {
3405 dev_err(card->dev,
3406 "ASoC: Property '%s' index %d read error:%d\n",
3407 propname, (2 * i) + 1, ret);
3408 return -EINVAL;
3409 }
3410
3411 widgets[i].name = wname;
3412 }
3413
Nicolin Chenf23e8602015-02-14 17:22:49 -08003414 card->of_dapm_widgets = widgets;
3415 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003416
3417 return 0;
3418}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003419EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003420
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003421int snd_soc_of_get_slot_mask(struct device_node *np,
3422 const char *prop_name,
3423 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003424{
3425 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003426 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003427 int i;
3428
3429 if (!of_slot_mask)
3430 return 0;
3431 val /= sizeof(u32);
3432 for (i = 0; i < val; i++)
3433 if (be32_to_cpup(&of_slot_mask[i]))
3434 *mask |= (1 << i);
3435
3436 return val;
3437}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003438EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003439
Xiubo Li89c67852014-02-14 09:34:35 +08003440int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003441 unsigned int *tx_mask,
3442 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003443 unsigned int *slots,
3444 unsigned int *slot_width)
3445{
3446 u32 val;
3447 int ret;
3448
Jyri Sarha61310842015-09-09 21:27:43 +03003449 if (tx_mask)
3450 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3451 if (rx_mask)
3452 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3453
Xiubo Li89c67852014-02-14 09:34:35 +08003454 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3455 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3456 if (ret)
3457 return ret;
3458
3459 if (slots)
3460 *slots = val;
3461 }
3462
3463 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3464 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3465 if (ret)
3466 return ret;
3467
3468 if (slot_width)
3469 *slot_width = val;
3470 }
3471
3472 return 0;
3473}
3474EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3475
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003476void snd_soc_of_parse_node_prefix(struct device_node *np,
3477 struct snd_soc_codec_conf *codec_conf,
3478 struct device_node *of_node,
3479 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003480{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003481 const char *str;
3482 int ret;
3483
3484 ret = of_property_read_string(np, propname, &str);
3485 if (ret < 0) {
3486 /* no prefix is not error */
3487 return;
3488 }
3489
3490 codec_conf->of_node = of_node;
3491 codec_conf->name_prefix = str;
3492}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003493EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003494
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003495int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003496 const char *propname)
3497{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003498 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003499 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003500 struct snd_soc_dapm_route *routes;
3501 int i, ret;
3502
3503 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003504 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003505 dev_err(card->dev,
3506 "ASoC: Property '%s' does not exist or its length is not even\n",
3507 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003508 return -EINVAL;
3509 }
3510 num_routes /= 2;
3511 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003512 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003513 propname);
3514 return -EINVAL;
3515 }
3516
Kees Cooka86854d2018-06-12 14:07:58 -07003517 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003518 GFP_KERNEL);
3519 if (!routes) {
3520 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003521 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003522 return -EINVAL;
3523 }
3524
3525 for (i = 0; i < num_routes; i++) {
3526 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003527 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003528 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003529 dev_err(card->dev,
3530 "ASoC: Property '%s' index %d could not be read: %d\n",
3531 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003532 return -EINVAL;
3533 }
3534 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003535 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003536 if (ret) {
3537 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003538 "ASoC: Property '%s' index %d could not be read: %d\n",
3539 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003540 return -EINVAL;
3541 }
3542 }
3543
Nicolin Chenf23e8602015-02-14 17:22:49 -08003544 card->num_of_dapm_routes = num_routes;
3545 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003546
3547 return 0;
3548}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003549EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003550
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003551unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003552 const char *prefix,
3553 struct device_node **bitclkmaster,
3554 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003555{
3556 int ret, i;
3557 char prop[128];
3558 unsigned int format = 0;
3559 int bit, frame;
3560 const char *str;
3561 struct {
3562 char *name;
3563 unsigned int val;
3564 } of_fmt_table[] = {
3565 { "i2s", SND_SOC_DAIFMT_I2S },
3566 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3567 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3568 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3569 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3570 { "ac97", SND_SOC_DAIFMT_AC97 },
3571 { "pdm", SND_SOC_DAIFMT_PDM},
3572 { "msb", SND_SOC_DAIFMT_MSB },
3573 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003574 };
3575
3576 if (!prefix)
3577 prefix = "";
3578
3579 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003580 * check "dai-format = xxx"
3581 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003582 * SND_SOC_DAIFMT_FORMAT_MASK area
3583 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003584 ret = of_property_read_string(np, "dai-format", &str);
3585 if (ret < 0) {
3586 snprintf(prop, sizeof(prop), "%sformat", prefix);
3587 ret = of_property_read_string(np, prop, &str);
3588 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003589 if (ret == 0) {
3590 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3591 if (strcmp(str, of_fmt_table[i].name) == 0) {
3592 format |= of_fmt_table[i].val;
3593 break;
3594 }
3595 }
3596 }
3597
3598 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003599 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003600 * SND_SOC_DAIFMT_CLOCK_MASK area
3601 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003602 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003603 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003604 format |= SND_SOC_DAIFMT_CONT;
3605 else
3606 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003607
3608 /*
3609 * check "[prefix]bitclock-inversion"
3610 * check "[prefix]frame-inversion"
3611 * SND_SOC_DAIFMT_INV_MASK area
3612 */
3613 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3614 bit = !!of_get_property(np, prop, NULL);
3615
3616 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3617 frame = !!of_get_property(np, prop, NULL);
3618
3619 switch ((bit << 4) + frame) {
3620 case 0x11:
3621 format |= SND_SOC_DAIFMT_IB_IF;
3622 break;
3623 case 0x10:
3624 format |= SND_SOC_DAIFMT_IB_NF;
3625 break;
3626 case 0x01:
3627 format |= SND_SOC_DAIFMT_NB_IF;
3628 break;
3629 default:
3630 /* SND_SOC_DAIFMT_NB_NF is default */
3631 break;
3632 }
3633
3634 /*
3635 * check "[prefix]bitclock-master"
3636 * check "[prefix]frame-master"
3637 * SND_SOC_DAIFMT_MASTER_MASK area
3638 */
3639 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3640 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003641 if (bit && bitclkmaster)
3642 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003643
3644 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3645 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003646 if (frame && framemaster)
3647 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003648
3649 switch ((bit << 4) + frame) {
3650 case 0x11:
3651 format |= SND_SOC_DAIFMT_CBM_CFM;
3652 break;
3653 case 0x10:
3654 format |= SND_SOC_DAIFMT_CBM_CFS;
3655 break;
3656 case 0x01:
3657 format |= SND_SOC_DAIFMT_CBS_CFM;
3658 break;
3659 default:
3660 format |= SND_SOC_DAIFMT_CBS_CFS;
3661 break;
3662 }
3663
3664 return format;
3665}
3666EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3667
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003668int snd_soc_get_dai_id(struct device_node *ep)
3669{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003670 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003671 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003672 int ret;
3673
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003674 dlc.of_node = of_graph_get_port_parent(ep);
3675 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003676 /*
3677 * For example HDMI case, HDMI has video/sound port,
3678 * but ALSA SoC needs sound port number only.
3679 * Thus counting HDMI DT port/endpoint doesn't work.
3680 * Then, it should have .of_xlate_dai_id
3681 */
3682 ret = -ENOTSUPP;
3683 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003684 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003685 if (component &&
3686 component->driver->of_xlate_dai_id)
3687 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003688 mutex_unlock(&client_mutex);
3689
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003690 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003691
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003692 return ret;
3693}
3694EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3695
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003696int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003697 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003698{
3699 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003700 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003701 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003702
3703 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003704 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003705 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003706
3707 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003708 continue;
3709
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003710 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003711 ret = pos->driver->of_xlate_dai_name(pos,
3712 args,
3713 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003714 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003715 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003716 int id = -1;
3717
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003718 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003719 case 0:
3720 id = 0; /* same as dai_drv[0] */
3721 break;
3722 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003723 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003724 break;
3725 default:
3726 /* not supported */
3727 break;
3728 }
3729
3730 if (id < 0 || id >= pos->num_dai) {
3731 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003732 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003733 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003734
3735 ret = 0;
3736
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003737 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003738 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003739 if (id == 0)
3740 break;
3741 id--;
3742 }
3743
3744 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003745 if (!*dai_name)
3746 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003747 }
3748
Kuninori Morimotocb470082013-09-10 17:39:56 -07003749 break;
3750 }
3751 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003752 return ret;
3753}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003754EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003755
3756int snd_soc_of_get_dai_name(struct device_node *of_node,
3757 const char **dai_name)
3758{
3759 struct of_phandle_args args;
3760 int ret;
3761
3762 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3763 "#sound-dai-cells", 0, &args);
3764 if (ret)
3765 return ret;
3766
3767 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003768
3769 of_node_put(args.np);
3770
3771 return ret;
3772}
3773EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3774
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003775/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003776 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3777 * @dai_link: DAI link
3778 *
3779 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3780 */
3781void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3782{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003783 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003784 int index;
3785
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003786 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003787 if (!component->of_node)
3788 break;
3789 of_node_put(component->of_node);
3790 component->of_node = NULL;
3791 }
3792}
3793EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3794
3795/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003796 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3797 * @dev: Card device
3798 * @of_node: Device node
3799 * @dai_link: DAI link
3800 *
3801 * Builds an array of CODEC DAI components from the DAI link property
3802 * 'sound-dai'.
3803 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003804 * The device nodes in the array (of_node) must be dereferenced by calling
3805 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003806 *
3807 * Returns 0 for success
3808 */
3809int snd_soc_of_get_dai_link_codecs(struct device *dev,
3810 struct device_node *of_node,
3811 struct snd_soc_dai_link *dai_link)
3812{
3813 struct of_phandle_args args;
3814 struct snd_soc_dai_link_component *component;
3815 char *name;
3816 int index, num_codecs, ret;
3817
3818 /* Count the number of CODECs */
3819 name = "sound-dai";
3820 num_codecs = of_count_phandle_with_args(of_node, name,
3821 "#sound-dai-cells");
3822 if (num_codecs <= 0) {
3823 if (num_codecs == -ENOENT)
3824 dev_err(dev, "No 'sound-dai' property\n");
3825 else
3826 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3827 return num_codecs;
3828 }
Kees Cooka86854d2018-06-12 14:07:58 -07003829 component = devm_kcalloc(dev,
3830 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003831 GFP_KERNEL);
3832 if (!component)
3833 return -ENOMEM;
3834 dai_link->codecs = component;
3835 dai_link->num_codecs = num_codecs;
3836
3837 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003838 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003839 ret = of_parse_phandle_with_args(of_node, name,
3840 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003841 index, &args);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003842 if (ret)
3843 goto err;
3844 component->of_node = args.np;
3845 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3846 if (ret < 0)
3847 goto err;
3848 }
3849 return 0;
3850err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003851 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003852 dai_link->codecs = NULL;
3853 dai_link->num_codecs = 0;
3854 return ret;
3855}
3856EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3857
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003858static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003859{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003860 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003861 snd_soc_util_init();
3862
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003863 return platform_driver_register(&soc_driver);
3864}
Mark Brown4abe8e12010-10-12 17:41:03 +01003865module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003866
Mark Brown7d8c16a2008-11-30 22:11:24 +00003867static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003868{
Mark Brownfb257892011-04-28 10:57:54 +01003869 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003870 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003871
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003872 platform_driver_unregister(&soc_driver);
3873}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003874module_exit(snd_soc_exit);
3875
3876/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003877MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003878MODULE_DESCRIPTION("ALSA SoC Core");
3879MODULE_LICENSE("GPL");
3880MODULE_ALIAS("platform:soc-audio");