blob: 3e73468225f9898967e7ea1d21547f381120a3f1 [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
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900514 if (!cpu_dai->driver->bus_control)
515 snd_soc_dai_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
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900587 if (cpu_dai->driver->bus_control)
588 snd_soc_dai_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
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900634 if (cpu_dai->driver->bus_control)
635 snd_soc_dai_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
Kuninori Morimoto24b09d02019-07-22 10:34:43 +0900681 if (!cpu_dai->driver->bus_control)
682 snd_soc_dai_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 Clausen61aca562014-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 Clausen61aca562014-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 Morimotocfd9b5f2019-07-22 10:34:56 +09001437 int ret;
1438
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001439 if (dai->probed ||
1440 dai->driver->probe_order != order)
1441 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442
Kuninori Morimotocfd9b5f2019-07-22 10:34:56 +09001443 ret = snd_soc_dai_probe(dai);
1444 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 }
1449
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001450 dai->probed = 1;
1451
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001452 return 0;
1453}
1454
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001455static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1456 struct snd_soc_pcm_runtime *rtd)
1457{
1458 int i, ret = 0;
1459
1460 for (i = 0; i < num_dais; ++i) {
1461 struct snd_soc_dai_driver *drv = dais[i]->driver;
1462
Rohit kumarde17f142018-11-01 18:08:49 +05301463 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001464 ret = drv->pcm_new(rtd, dais[i]);
1465 if (ret < 0) {
1466 dev_err(dais[i]->dev,
1467 "ASoC: Failed to bind %s with pcm device\n",
1468 dais[i]->name);
1469 return ret;
1470 }
1471 }
1472
1473 return 0;
1474}
1475
Mengdong Lin1a497982015-11-18 02:34:11 -05001476static int soc_probe_link_dais(struct snd_soc_card *card,
1477 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478{
Mengdong Lin1a497982015-11-18 02:34:11 -05001479 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001481 struct snd_soc_rtdcom_list *rtdcom;
1482 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001483 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001484 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001486 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001487 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 /* set default power off timeout */
1490 rtd->pmdown_time = pmdown_time;
1491
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001492 ret = soc_probe_dai(cpu_dai, order);
1493 if (ret)
1494 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001497 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1498 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001499 if (ret)
1500 return ret;
1501 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001502
Liam Girdwood0168bf02011-06-07 16:08:05 +01001503 /* complete DAI probe during last probe */
1504 if (order != SND_SOC_COMP_ORDER_LAST)
1505 return 0;
1506
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001507 /* do machine specific initialization */
1508 if (dai_link->init) {
1509 ret = dai_link->init(rtd);
1510 if (ret < 0) {
1511 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1512 dai_link->name, ret);
1513 return ret;
1514 }
1515 }
1516
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001517 if (dai_link->dai_fmt)
1518 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1519
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001520 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001521 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001523
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001524#ifdef CONFIG_DEBUG_FS
1525 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001526 if (dai_link->dynamic)
1527 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001528#endif
1529
Liam Girdwooda655de82018-07-02 16:59:54 +01001530 num = rtd->num;
1531
1532 /*
1533 * most drivers will register their PCMs using DAI link ordering but
1534 * topology based drivers can use the DAI link id field to set PCM
1535 * device number and then use rtd + a base offset of the BEs.
1536 */
1537 for_each_rtdcom(rtd, rtdcom) {
1538 component = rtdcom->component;
1539
1540 if (!component->driver->use_dai_pcm_id)
1541 continue;
1542
1543 if (rtd->dai_link->no_pcm)
1544 num += component->driver->be_pcm_base;
1545 else
1546 num = rtd->dai_link->id;
1547 }
1548
Jie Yang6f0c4222015-10-13 23:41:00 +08001549 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001550 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001551 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001552 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001553 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301554 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001555 return ret;
1556 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001557 } else if (!dai_link->params) {
1558 /* create the pcm */
1559 ret = soc_new_pcm(rtd, num);
1560 if (ret < 0) {
1561 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1562 dai_link->stream_name, ret);
1563 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001564 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001565 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1566 if (ret < 0)
1567 return ret;
1568 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1569 rtd->num_codecs, rtd);
1570 if (ret < 0)
1571 return ret;
1572 } else {
1573 INIT_DELAYED_WORK(&rtd->delayed_work,
1574 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575 }
1576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 return 0;
1578}
1579
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001580static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001581{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001582 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001583 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001584 struct snd_soc_dai_link_component dlc;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001585
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001586 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1587 /* codecs, usually analog devices */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001588 dlc.name = aux_dev->codec_name;
1589 dlc.of_node = aux_dev->codec_of_node;
1590 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001591 if (!component) {
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001592 if (dlc.of_node)
1593 dlc.name = of_node_full_name(dlc.of_node);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001594 goto err_defer;
1595 }
1596 } else if (aux_dev->name) {
1597 /* generic components */
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001598 dlc.name = aux_dev->name;
1599 dlc.of_node = NULL;
1600 component = soc_find_component(&dlc);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001601 if (!component)
1602 goto err_defer;
1603 } else {
1604 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1605 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001606 }
1607
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001608 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001609 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001610
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001611 return 0;
1612
1613err_defer:
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09001614 dev_err(card->dev, "ASoC: %s not registered\n", dlc.name);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001615 return -EPROBE_DEFER;
1616}
1617
1618static int soc_probe_aux_devices(struct snd_soc_card *card)
1619{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001620 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001621 int order;
1622 int ret;
1623
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001624 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001625 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001626 if (comp->driver->probe_order == order) {
1627 ret = soc_probe_component(card, comp);
1628 if (ret < 0) {
1629 dev_err(card->dev,
1630 "ASoC: failed to probe aux component %s %d\n",
1631 comp->name, ret);
1632 return ret;
1633 }
1634 }
1635 }
1636 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001637
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001638 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001639}
1640
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001641static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001642{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001643 struct snd_soc_component *comp, *_comp;
1644 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001645
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001646 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001647 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001648 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001649
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001650 if (comp->driver->remove_order == order) {
1651 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001652 /* remove it from the card's aux_comp_list */
1653 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001654 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001655 }
1656 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001657}
1658
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001659/**
1660 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1661 * @rtd: The runtime for which the DAI link format should be changed
1662 * @dai_fmt: The new DAI link format
1663 *
1664 * This function updates the DAI link format for all DAIs connected to the DAI
1665 * link for the specified runtime.
1666 *
1667 * Note: For setups with a static format set the dai_fmt field in the
1668 * corresponding snd_dai_link struct instead of using this function.
1669 *
1670 * Returns 0 on success, otherwise a negative error code.
1671 */
1672int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1673 unsigned int dai_fmt)
1674{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001675 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001676 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001677 unsigned int i;
1678 int ret;
1679
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001680 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001681 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1682 if (ret != 0 && ret != -ENOTSUPP) {
1683 dev_warn(codec_dai->dev,
1684 "ASoC: Failed to set DAI format: %d\n", ret);
1685 return ret;
1686 }
1687 }
1688
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001689 /*
1690 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1691 * the component which has non_legacy_dai_naming is Codec
1692 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001693 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001694 unsigned int inv_dai_fmt;
1695
1696 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1697 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1698 case SND_SOC_DAIFMT_CBM_CFM:
1699 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1700 break;
1701 case SND_SOC_DAIFMT_CBM_CFS:
1702 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1703 break;
1704 case SND_SOC_DAIFMT_CBS_CFM:
1705 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1706 break;
1707 case SND_SOC_DAIFMT_CBS_CFS:
1708 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1709 break;
1710 }
1711
1712 dai_fmt = inv_dai_fmt;
1713 }
1714
1715 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1716 if (ret != 0 && ret != -ENOTSUPP) {
1717 dev_warn(cpu_dai->dev,
1718 "ASoC: Failed to set DAI format: %d\n", ret);
1719 return ret;
1720 }
1721
1722 return 0;
1723}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001724EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001725
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001726#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001727/*
1728 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001729 * separate different DMI fields in the card long name. Only number and
1730 * alphabet characters and a few separator characters are kept.
1731 */
1732static void cleanup_dmi_name(char *name)
1733{
1734 int i, j = 0;
1735
1736 for (i = 0; name[i]; i++) {
1737 if (isalnum(name[i]) || (name[i] == '.')
1738 || (name[i] == '_'))
1739 name[j++] = name[i];
1740 else if (name[i] == '-')
1741 name[j++] = '_';
1742 }
1743
1744 name[j] = '\0';
1745}
1746
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001747/*
1748 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001749 * in the black list.
1750 */
1751static int is_dmi_valid(const char *field)
1752{
1753 int i = 0;
1754
1755 while (dmi_blacklist[i]) {
1756 if (strstr(field, dmi_blacklist[i]))
1757 return 0;
1758 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001759 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001760
1761 return 1;
1762}
1763
Liam Girdwood345233d2017-01-14 16:13:02 +08001764/**
1765 * snd_soc_set_dmi_name() - Register DMI names to card
1766 * @card: The card to register DMI names
1767 * @flavour: The flavour "differentiator" for the card amongst its peers.
1768 *
1769 * An Intel machine driver may be used by many different devices but are
1770 * difficult for userspace to differentiate, since machine drivers ususally
1771 * use their own name as the card short name and leave the card long name
1772 * blank. To differentiate such devices and fix bugs due to lack of
1773 * device-specific configurations, this function allows DMI info to be used
1774 * as the sound card long name, in the format of
1775 * "vendor-product-version-board"
1776 * (Character '-' is used to separate different DMI fields here).
1777 * This will help the user space to load the device-specific Use Case Manager
1778 * (UCM) configurations for the card.
1779 *
1780 * Possible card long names may be:
1781 * DellInc.-XPS139343-01-0310JH
1782 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1783 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1784 *
1785 * This function also supports flavoring the card longname to provide
1786 * the extra differentiation, like "vendor-product-version-board-flavor".
1787 *
1788 * We only keep number and alphabet characters and a few separator characters
1789 * in the card long name since UCM in the user space uses the card long names
1790 * as card configuration directory names and AudoConf cannot support special
1791 * charactors like SPACE.
1792 *
1793 * Returns 0 on success, otherwise a negative error code.
1794 */
1795int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1796{
1797 const char *vendor, *product, *product_version, *board;
1798 size_t longname_buf_size = sizeof(card->snd_card->longname);
1799 size_t len;
1800
1801 if (card->long_name)
1802 return 0; /* long name already set by driver or from DMI */
1803
1804 /* make up dmi long name as: vendor.product.version.board */
1805 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001806 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001807 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1808 return 0;
1809 }
1810
1811 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1812 "%s", vendor);
1813 cleanup_dmi_name(card->dmi_longname);
1814
1815 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001816 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001817 len = strlen(card->dmi_longname);
1818 snprintf(card->dmi_longname + len,
1819 longname_buf_size - len,
1820 "-%s", product);
1821
1822 len++; /* skip the separator "-" */
1823 if (len < longname_buf_size)
1824 cleanup_dmi_name(card->dmi_longname + len);
1825
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001826 /*
1827 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001828 * name in the product version field
1829 */
1830 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001831 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001832 len = strlen(card->dmi_longname);
1833 snprintf(card->dmi_longname + len,
1834 longname_buf_size - len,
1835 "-%s", product_version);
1836
1837 len++;
1838 if (len < longname_buf_size)
1839 cleanup_dmi_name(card->dmi_longname + len);
1840 }
1841 }
1842
1843 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001844 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001845 len = strlen(card->dmi_longname);
1846 snprintf(card->dmi_longname + len,
1847 longname_buf_size - len,
1848 "-%s", board);
1849
1850 len++;
1851 if (len < longname_buf_size)
1852 cleanup_dmi_name(card->dmi_longname + len);
1853 } else if (!product) {
1854 /* fall back to using legacy name */
1855 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1856 return 0;
1857 }
1858
1859 /* Add flavour to dmi long name */
1860 if (flavour) {
1861 len = strlen(card->dmi_longname);
1862 snprintf(card->dmi_longname + len,
1863 longname_buf_size - len,
1864 "-%s", flavour);
1865
1866 len++;
1867 if (len < longname_buf_size)
1868 cleanup_dmi_name(card->dmi_longname + len);
1869 }
1870
1871 /* set the card long name */
1872 card->long_name = card->dmi_longname;
1873
1874 return 0;
1875}
1876EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001877#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001878
Liam Girdwooda655de82018-07-02 16:59:54 +01001879static void soc_check_tplg_fes(struct snd_soc_card *card)
1880{
1881 struct snd_soc_component *component;
1882 const struct snd_soc_component_driver *comp_drv;
1883 struct snd_soc_dai_link *dai_link;
1884 int i;
1885
Kuninori Morimoto368dee92018-09-21 05:23:01 +00001886 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001887
1888 /* does this component override FEs ? */
1889 if (!component->driver->ignore_machine)
1890 continue;
1891
1892 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001893 if (!strcmp(component->driver->ignore_machine,
1894 card->dev->driver->name))
1895 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01001896 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001897 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01001898 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06001899match:
Liam Girdwooda655de82018-07-02 16:59:54 +01001900 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00001901 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01001902
1903 /* ignore this FE */
1904 if (dai_link->dynamic) {
1905 dai_link->ignore = true;
1906 continue;
1907 }
1908
1909 dev_info(card->dev, "info: override FE DAI link %s\n",
1910 card->dai_link[i].name);
1911
1912 /* override platform component */
Kuninori Morimotoadb76b52019-06-06 13:22:19 +09001913 if (!dai_link->platforms) {
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001914 dev_err(card->dev, "init platform error");
1915 continue;
1916 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001917 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01001918
1919 /* convert non BE into BE */
1920 dai_link->no_pcm = 1;
1921
1922 /* override any BE fixups */
1923 dai_link->be_hw_params_fixup =
1924 component->driver->be_hw_params_fixup;
1925
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001926 /*
1927 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01001928 * dai link name if it's NULL to help bind widgets.
1929 */
1930 if (!dai_link->stream_name)
1931 dai_link->stream_name = dai_link->name;
1932 }
1933
1934 /* Inform userspace we are using alternate topology */
1935 if (component->driver->topology_name_prefix) {
1936
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001937 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01001938 if (!card->topology_shortname_created) {
1939 comp_drv = component->driver;
1940
1941 snprintf(card->topology_shortname, 32, "%s-%s",
1942 comp_drv->topology_name_prefix,
1943 card->name);
1944 card->topology_shortname_created = true;
1945 }
1946
1947 /* use topology shortname */
1948 card->name = card->topology_shortname;
1949 }
1950 }
1951}
1952
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001953static int soc_cleanup_card_resources(struct snd_soc_card *card)
1954{
1955 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001956 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001957 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09001958 card->snd_card = NULL;
1959 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001960
1961 /* remove and free each DAI */
1962 soc_remove_dai_links(card);
1963 soc_remove_pcm_runtimes(card);
1964
1965 /* remove auxiliary devices */
1966 soc_remove_aux_devices(card);
1967
1968 snd_soc_dapm_free(&card->dapm);
1969 soc_cleanup_card_debugfs(card);
1970
1971 /* remove the card */
1972 if (card->remove)
1973 card->remove(card);
1974
1975 return 0;
1976}
1977
Mark Brownb19e6e72012-03-14 21:18:39 +00001978static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979{
Mengdong Lin1a497982015-11-18 02:34:11 -05001980 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001981 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001982 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001984 mutex_lock(&client_mutex);
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001985 for_each_card_prelinks(card, i, dai_link) {
1986 ret = soc_init_dai_link(card, dai_link);
1987 if (ret) {
Tzung-Bi Shih70fc5372019-06-04 11:31:02 +08001988 dev_err(card->dev, "ASoC: failed to init link %s: %d\n",
1989 dai_link->name, ret);
1990 mutex_unlock(&client_mutex);
1991 return ret;
1992 }
1993 }
Liam Girdwood01b9d992012-03-07 10:38:25 +00001994 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001995
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09001996 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1997 card->dapm.dev = card->dev;
1998 card->dapm.card = card;
1999 list_add(&card->dapm.list, &card->dapm_list);
2000
Liam Girdwooda655de82018-07-02 16:59:54 +01002001 /* check whether any platform is ignore machine FE and using topology */
2002 soc_check_tplg_fes(card);
2003
Mark Brownb19e6e72012-03-14 21:18:39 +00002004 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002005 for_each_card_prelinks(card, i, dai_link) {
2006 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002007 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002008 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002009 }
2010
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002011 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002012 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002013 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002014 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002015 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002016 }
2017
Mengdong Linf8f80362015-12-02 14:11:22 +08002018 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002019 for_each_card_prelinks(card, i, dai_link)
2020 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002022 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002023 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002024 card->owner, 0, &card->snd_card);
2025 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002026 dev_err(card->dev,
2027 "ASoC: can't create sound card for card %s: %d\n",
2028 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002029 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002030 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002031
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002032 soc_init_card_debugfs(card);
2033
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002034#ifdef CONFIG_DEBUG_FS
2035 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2036#endif
2037
Mark Brown88ee1c62011-02-02 10:43:26 +00002038#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002039 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002040 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002041#endif
Andy Green6ed25972008-06-13 16:24:05 +01002042
Mark Brown9a841eb2011-04-12 17:51:37 -07002043 if (card->dapm_widgets)
2044 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2045 card->num_dapm_widgets);
2046
Nicolin Chenf23e8602015-02-14 17:22:49 -08002047 if (card->of_dapm_widgets)
2048 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2049 card->num_of_dapm_widgets);
2050
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002051 /* initialise the sound card only once */
2052 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002053 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002054 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002055 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056 }
2057
Stephen Warren62ae68f2012-06-08 12:34:23 -06002058 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002059 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002060 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002061 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002062 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002063 dev_err(card->dev,
2064 "ASoC: failed to instantiate card %d\n",
2065 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002066 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002067 }
2068 }
2069 }
2070
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002071 /* probe auxiliary components */
2072 ret = soc_probe_aux_devices(card);
2073 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002074 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002075
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002076 /*
2077 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002078 * Components with topology may bring new DAIs and DAI links.
2079 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002080 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002081 if (soc_is_dai_link_bound(card, dai_link))
2082 continue;
2083
2084 ret = soc_init_dai_link(card, dai_link);
2085 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002086 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002087 ret = soc_bind_dai_link(card, dai_link);
2088 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002089 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002090 }
2091
Stephen Warren62ae68f2012-06-08 12:34:23 -06002092 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002093 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002094 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002095 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002096 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002097 dev_err(card->dev,
2098 "ASoC: failed to instantiate card %d\n",
2099 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002100 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002101 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002102 }
2103 }
2104
Mark Brown888df392012-02-16 19:37:51 -08002105 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002106 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002107
Mark Brownb7af1da2011-04-07 19:18:44 +09002108 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002109 snd_soc_add_card_controls(card, card->controls,
2110 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002111
Mark Brownb8ad29d2011-03-02 18:35:51 +00002112 if (card->dapm_routes)
2113 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2114 card->num_dapm_routes);
2115
Nicolin Chenf23e8602015-02-14 17:22:49 -08002116 if (card->of_dapm_routes)
2117 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2118 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002119
Takashi Iwai861886d2017-04-24 08:54:42 +02002120 /* try to set some sane longname if DMI is available */
2121 snd_soc_set_dmi_name(card, NULL);
2122
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002123 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002124 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002125 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2126 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002127 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2128 "%s", card->driver_name ? card->driver_name : card->name);
2129 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2130 switch (card->snd_card->driver[i]) {
2131 case '_':
2132 case '-':
2133 case '\0':
2134 break;
2135 default:
2136 if (!isalnum(card->snd_card->driver[i]))
2137 card->snd_card->driver[i] = '_';
2138 break;
2139 }
2140 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002141
Mark Brown28e9ad92011-03-02 18:36:34 +00002142 if (card->late_probe) {
2143 ret = card->late_probe(card);
2144 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002145 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002146 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002147 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002148 }
2149 }
2150
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002151 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002153 ret = snd_card_register(card->snd_card);
2154 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002155 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2156 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002157 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002158 }
2159
Mark Brown435c5e22008-12-04 15:32:53 +00002160 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002161 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002162 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002163
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002164probe_end:
2165 if (ret < 0)
2166 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002167
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002168 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002169 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170
Mark Brownb19e6e72012-03-14 21:18:39 +00002171 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002172}
2173
2174/* probes a new socdev */
2175static int soc_probe(struct platform_device *pdev)
2176{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002177 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002178
Vinod Koul70a7ca32011-01-14 19:22:48 +05302179 /*
2180 * no card, so machine driver should be registering card
2181 * we should not be here in that case so ret error
2182 */
2183 if (!card)
2184 return -EINVAL;
2185
Mark Brownfe4085e2012-03-02 13:07:41 +00002186 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002187 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002188 card->name);
2189
Mark Brown435c5e22008-12-04 15:32:53 +00002190 /* Bodge while we unpick instantiation */
2191 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002192
Mark Brown28d528c2012-08-09 18:45:23 +01002193 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194}
2195
2196/* removes a socdev */
2197static int soc_remove(struct platform_device *pdev)
2198{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200
Mark Brownc5af3a22008-11-28 13:29:45 +00002201 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202 return 0;
2203}
2204
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002205int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002206{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002207 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002208 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002209
2210 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002211 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002212
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002213 /*
2214 * Flush out pmdown_time work - we actually do want to run it
2215 * now, we're shutting down so no imminent restart.
2216 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002217 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002219 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002220
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002221 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002222 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002223 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002224 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002225 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002226
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002227 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002228 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002229 pinctrl_pm_select_sleep_state(codec_dai->dev);
2230 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002231 }
2232
Mark Brown416356f2009-06-30 19:05:15 +01002233 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002234}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002235EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002236
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002237const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302238 .suspend = snd_soc_suspend,
2239 .resume = snd_soc_resume,
2240 .freeze = snd_soc_suspend,
2241 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002242 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302243 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002244};
Stephen Warrendeb26072011-04-05 19:35:30 -06002245EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002246
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247/* ASoC platform driver */
2248static struct platform_driver soc_driver = {
2249 .driver = {
2250 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002251 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002252 },
2253 .probe = soc_probe,
2254 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255};
2256
Mark Brown096e49d2009-07-05 15:12:22 +01002257/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258 * snd_soc_cnew - create new control
2259 * @_template: control template
2260 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002261 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002262 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263 *
2264 * Create a new mixer control from a template control.
2265 *
2266 * Returns 0 for success, else error.
2267 */
2268struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002269 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002270 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271{
2272 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002273 struct snd_kcontrol *kcontrol;
2274 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275
2276 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002277 template.index = 0;
2278
Mark Brownefb7ac32011-03-08 17:23:24 +00002279 if (!long_name)
2280 long_name = template.name;
2281
2282 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002283 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002284 if (!name)
2285 return NULL;
2286
Mark Brownefb7ac32011-03-08 17:23:24 +00002287 template.name = name;
2288 } else {
2289 template.name = long_name;
2290 }
2291
2292 kcontrol = snd_ctl_new1(&template, data);
2293
2294 kfree(name);
2295
2296 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297}
2298EXPORT_SYMBOL_GPL(snd_soc_cnew);
2299
Liam Girdwood022658b2012-02-03 17:43:09 +00002300static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2301 const struct snd_kcontrol_new *controls, int num_controls,
2302 const char *prefix, void *data)
2303{
2304 int err, i;
2305
2306 for (i = 0; i < num_controls; i++) {
2307 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002308
Liam Girdwood022658b2012-02-03 17:43:09 +00002309 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2310 control->name, prefix));
2311 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002312 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2313 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002314 return err;
2315 }
2316 }
2317
2318 return 0;
2319}
2320
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002321struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2322 const char *name)
2323{
2324 struct snd_card *card = soc_card->snd_card;
2325 struct snd_kcontrol *kctl;
2326
2327 if (unlikely(!name))
2328 return NULL;
2329
2330 list_for_each_entry(kctl, &card->controls, list)
2331 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2332 return kctl;
2333 return NULL;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2336
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002338 * snd_soc_add_component_controls - Add an array of controls to a component.
2339 *
2340 * @component: Component to add controls to
2341 * @controls: Array of controls to add
2342 * @num_controls: Number of elements in the array
2343 *
2344 * Return: 0 for success, else error.
2345 */
2346int snd_soc_add_component_controls(struct snd_soc_component *component,
2347 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2348{
2349 struct snd_card *card = component->card->snd_card;
2350
2351 return snd_soc_add_controls(card, component->dev, controls,
2352 num_controls, component->name_prefix, component);
2353}
2354EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2355
2356/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002357 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2358 * Convenience function to add a list of controls.
2359 *
2360 * @soc_card: SoC card to add controls to
2361 * @controls: array of controls to add
2362 * @num_controls: number of elements in the array
2363 *
2364 * Return 0 for success, else error.
2365 */
2366int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2367 const struct snd_kcontrol_new *controls, int num_controls)
2368{
2369 struct snd_card *card = soc_card->snd_card;
2370
2371 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2372 NULL, soc_card);
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2375
2376/**
2377 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2378 * Convienience function to add a list of controls.
2379 *
2380 * @dai: DAI to add controls to
2381 * @controls: array of controls to add
2382 * @num_controls: number of elements in the array
2383 *
2384 * Return 0 for success, else error.
2385 */
2386int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2387 const struct snd_kcontrol_new *controls, int num_controls)
2388{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002389 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002390
2391 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2392 NULL, dai);
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2395
2396/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002397 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2398 * @component: COMPONENT
2399 * @clk_id: DAI specific clock ID
2400 * @source: Source for the clock
2401 * @freq: new clock frequency in Hz
2402 * @dir: new clock direction - input/output.
2403 *
2404 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2405 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002406int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2407 int clk_id, int source, unsigned int freq,
2408 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002409{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002410 if (component->driver->set_sysclk)
2411 return component->driver->set_sysclk(component, clk_id, source,
2412 freq, dir);
2413
2414 return -ENOTSUPP;
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2417
Mark Brownec4ee522011-03-07 20:58:11 +00002418/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002419 * snd_soc_component_set_pll - configure component PLL.
2420 * @component: COMPONENT
2421 * @pll_id: DAI specific PLL ID
2422 * @source: DAI specific source for the PLL
2423 * @freq_in: PLL input clock frequency in Hz
2424 * @freq_out: requested PLL output clock frequency in Hz
2425 *
2426 * Configures and enables PLL to generate output clock based on input clock.
2427 */
2428int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2429 int source, unsigned int freq_in,
2430 unsigned int freq_out)
2431{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002432 if (component->driver->set_pll)
2433 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002434 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002435
2436 return -EINVAL;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2439
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002440static int snd_soc_bind_card(struct snd_soc_card *card)
2441{
2442 struct snd_soc_pcm_runtime *rtd;
2443 int ret;
2444
2445 ret = snd_soc_instantiate_card(card);
2446 if (ret != 0)
2447 return ret;
2448
2449 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002450 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002451 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2452 struct snd_soc_dai *codec_dai;
2453 int j;
2454
2455 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2456 if (!codec_dai->active)
2457 pinctrl_pm_select_sleep_state(codec_dai->dev);
2458 }
2459
2460 if (!cpu_dai->active)
2461 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2462 }
2463
2464 return ret;
2465}
2466
Mark Brownc5af3a22008-11-28 13:29:45 +00002467/**
2468 * snd_soc_register_card - Register a card with the ASoC core
2469 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002470 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002471 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002472 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302473int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002474{
2475 if (!card->name || !card->dev)
2476 return -EINVAL;
2477
Mark Browned77cc12011-05-03 18:25:34 +01002478 dev_set_drvdata(card->dev, card);
2479
Stephen Warren111c6412011-01-28 14:26:35 -07002480 snd_soc_initialize_card_lists(card);
2481
Mengdong Linf8f80362015-12-02 14:11:22 +08002482 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002483
Mengdong Lin1a497982015-11-18 02:34:11 -05002484 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002485 card->num_rtd = 0;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002486
Mark Browndb432b42011-10-03 21:06:40 +01002487 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002488 INIT_LIST_HEAD(&card->dobj_list);
Mark Browndee89c42008-11-18 22:11:38 +00002489 card->instantiated = 0;
2490 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002491 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002492 spin_lock_init(&card->dpcm_lock);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002493
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002494 return snd_soc_bind_card(card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002495}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302496EXPORT_SYMBOL_GPL(snd_soc_register_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002497
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002498static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2499{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002500 struct snd_soc_pcm_runtime *rtd;
2501 int order;
2502
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002503 if (card->instantiated) {
2504 card->instantiated = false;
2505 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002506 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002507
2508 /* remove all components used by DAI links on this card */
2509 for_each_comp_order(order) {
2510 for_each_card_rtds(card, rtd) {
2511 soc_remove_link_components(card, rtd, order);
2512 }
2513 }
2514
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002515 soc_cleanup_card_resources(card);
2516 if (!unregister)
2517 list_add(&card->list, &unbind_card_list);
2518 } else {
2519 if (unregister)
2520 list_del(&card->list);
2521 }
2522}
2523
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002524/**
2525 * snd_soc_unregister_card - Unregister a card with the ASoC core
2526 *
Mark Browndee89c42008-11-18 22:11:38 +00002527 * @card: Card to unregister
2528 *
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002529 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302530int snd_soc_unregister_card(struct snd_soc_card *card)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002531{
Kuninori Morimotob5455422019-06-19 10:07:19 +09002532 mutex_lock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002533 snd_soc_unbind_card(card, true);
Kuninori Morimotob5455422019-06-19 10:07:19 +09002534 mutex_unlock(&client_mutex);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002535 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002536
2537 return 0;
2538}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302539EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002540
2541/*
2542 * Simplify DAI link configuration by removing ".-1" from device names
2543 * and sanitizing names.
2544 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002545static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002546{
2547 char *found, name[NAME_SIZE];
2548 int id1, id2;
2549
2550 if (dev_name(dev) == NULL)
2551 return NULL;
2552
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002553 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002554
2555 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002556 found = strstr(name, dev->driver->name);
2557 if (found) {
2558 /* get ID */
2559 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2560
2561 /* discard ID from name if ID == -1 */
2562 if (*id == -1)
2563 found[strlen(dev->driver->name)] = '\0';
2564 }
2565
2566 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002567 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002568 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2569 char tmp[NAME_SIZE];
2570
2571 /* create unique ID number from I2C addr and bus */
2572 *id = ((id1 & 0xffff) << 16) + id2;
2573
2574 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002575 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2576 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002577 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002578 } else
2579 *id = 0;
2580 }
2581
2582 return kstrdup(name, GFP_KERNEL);
2583}
2584
2585/*
2586 * Simplify DAI link naming for single devices with multiple DAIs by removing
2587 * any ".-1" and using the DAI name (instead of device name).
2588 */
2589static inline char *fmt_multiple_name(struct device *dev,
2590 struct snd_soc_dai_driver *dai_drv)
2591{
2592 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002593 dev_err(dev,
2594 "ASoC: error - multiple DAI %s registered with no name\n",
2595 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002596 return NULL;
2597 }
2598
2599 return kstrdup(dai_drv->name, GFP_KERNEL);
2600}
2601
2602/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002603 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002604 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002605 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002606 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002607static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002608{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002609 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002610
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002611 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002612 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2613 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002614 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002615 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002616 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002617 }
Mark Brown91151712008-11-30 23:31:24 +00002618}
Mark Brown91151712008-11-30 23:31:24 +00002619
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002620/* Create a DAI and add it to the component's DAI list */
2621static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2622 struct snd_soc_dai_driver *dai_drv,
2623 bool legacy_dai_naming)
2624{
2625 struct device *dev = component->dev;
2626 struct snd_soc_dai *dai;
2627
2628 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2629
2630 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2631 if (dai == NULL)
2632 return NULL;
2633
2634 /*
2635 * Back in the old days when we still had component-less DAIs,
2636 * instead of having a static name, component-less DAIs would
2637 * inherit the name of the parent device so it is possible to
2638 * register multiple instances of the DAI. We still need to keep
2639 * the same naming style even though those DAIs are not
2640 * component-less anymore.
2641 */
2642 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002643 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002644 dai->name = fmt_single_name(dev, &dai->id);
2645 } else {
2646 dai->name = fmt_multiple_name(dev, dai_drv);
2647 if (dai_drv->id)
2648 dai->id = dai_drv->id;
2649 else
2650 dai->id = component->num_dai;
2651 }
2652 if (dai->name == NULL) {
2653 kfree(dai);
2654 return NULL;
2655 }
2656
2657 dai->component = component;
2658 dai->dev = dev;
2659 dai->driver = dai_drv;
2660 if (!dai->driver->ops)
2661 dai->driver->ops = &null_dai_ops;
2662
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00002663 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002664 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002665 component->num_dai++;
2666
2667 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2668 return dai;
2669}
2670
Mark Brown91151712008-11-30 23:31:24 +00002671/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002672 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002673 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002674 * @component: The component the DAIs are registered for
2675 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002676 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002677 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002678static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002679 struct snd_soc_dai_driver *dai_drv,
2680 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002681{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002682 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002683 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002684 unsigned int i;
2685 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002686
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002687 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002688
2689 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002690
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002691 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
2692 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002693 if (dai == NULL) {
2694 ret = -ENOMEM;
2695 goto err;
2696 }
Mark Brown91151712008-11-30 23:31:24 +00002697 }
2698
2699 return 0;
2700
2701err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002702 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002703
2704 return ret;
2705}
Mark Brown91151712008-11-30 23:31:24 +00002706
Mengdong Lin68003e62015-12-31 16:40:43 +08002707/**
2708 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2709 *
2710 * @component: The component the DAIs are registered for
2711 * @dai_drv: DAI driver to use for the DAI
2712 *
2713 * Topology can use this API to register DAIs when probing a component.
2714 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2715 * will be freed in the component cleanup.
2716 */
2717int snd_soc_register_dai(struct snd_soc_component *component,
2718 struct snd_soc_dai_driver *dai_drv)
2719{
2720 struct snd_soc_dapm_context *dapm =
2721 snd_soc_component_get_dapm(component);
2722 struct snd_soc_dai *dai;
2723 int ret;
2724
2725 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2726 dev_err(component->dev, "Invalid dai type %d\n",
2727 dai_drv->dobj.type);
2728 return -EINVAL;
2729 }
2730
2731 lockdep_assert_held(&client_mutex);
2732 dai = soc_add_dai(component, dai_drv, false);
2733 if (!dai)
2734 return -ENOMEM;
2735
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002736 /*
2737 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08002738 * also add routes that need these widgets as source or sink.
2739 */
2740 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2741 if (ret != 0) {
2742 dev_err(component->dev,
2743 "Failed to create DAI widgets %d\n", ret);
2744 }
2745
2746 return ret;
2747}
2748EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2749
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002750static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2751 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002752{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002753 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002754
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002755 component->driver->seq_notifier(component, type, subseq);
2756}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002757
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002758static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2759 int event)
2760{
2761 struct snd_soc_component *component = dapm->component;
2762
2763 return component->driver->stream_event(component, event);
2764}
2765
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002766static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2767 enum snd_soc_bias_level level)
2768{
2769 struct snd_soc_component *component = dapm->component;
2770
2771 return component->driver->set_bias_level(component, level);
2772}
2773
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002774static int snd_soc_component_initialize(struct snd_soc_component *component,
2775 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002776{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002777 struct snd_soc_dapm_context *dapm;
2778
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002779 component->name = fmt_single_name(dev, &component->id);
2780 if (!component->name) {
2781 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002782 return -ENOMEM;
2783 }
2784
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002785 component->dev = dev;
2786 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002787
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002788 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002789 dapm->dev = dev;
2790 dapm->component = component;
2791 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002792 dapm->idle_bias_off = !driver->idle_bias_on;
2793 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002794 if (driver->seq_notifier)
2795 dapm->seq_notifier = snd_soc_component_seq_notifier;
2796 if (driver->stream_event)
2797 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002798 if (driver->set_bias_level)
2799 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002800
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002801 INIT_LIST_HEAD(&component->dai_list);
2802 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002803
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002804 return 0;
2805}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002806
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002807static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002808{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002809 int val_bytes = regmap_get_val_bytes(component->regmap);
2810
2811 /* Errors are legitimate for non-integer byte multiples */
2812 if (val_bytes > 0)
2813 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002814}
2815
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002816#ifdef CONFIG_REGMAP
2817
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002818/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002819 * snd_soc_component_init_regmap() - Initialize regmap instance for the
2820 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002821 * @component: The component for which to initialize the regmap instance
2822 * @regmap: The regmap instance that should be used by the component
2823 *
2824 * This function allows deferred assignment of the regmap instance that is
2825 * associated with the component. Only use this if the regmap instance is not
2826 * yet ready when the component is registered. The function must also be called
2827 * before the first IO attempt of the component.
2828 */
2829void snd_soc_component_init_regmap(struct snd_soc_component *component,
2830 struct regmap *regmap)
2831{
2832 component->regmap = regmap;
2833 snd_soc_component_setup_regmap(component);
2834}
2835EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2836
2837/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002838 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
2839 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002840 * @component: The component for which to de-initialize the regmap instance
2841 *
2842 * Calls regmap_exit() on the regmap instance associated to the component and
2843 * removes the regmap instance from the component.
2844 *
2845 * This function should only be used if snd_soc_component_init_regmap() was used
2846 * to initialize the regmap instance.
2847 */
2848void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2849{
2850 regmap_exit(component->regmap);
2851 component->regmap = NULL;
2852}
2853EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2854
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002855#endif
2856
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002857static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002858{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002859 mutex_lock(&client_mutex);
2860
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002861 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002862 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002863 component->regmap = dev_get_regmap(component->dev,
2864 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002865 if (component->regmap)
2866 snd_soc_component_setup_regmap(component);
2867 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002868
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002869 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002870 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002871 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002872
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002873 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002874}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002875
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002876static void snd_soc_component_cleanup(struct snd_soc_component *component)
2877{
2878 snd_soc_unregister_dais(component);
2879 kfree(component->name);
2880}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002881
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002882static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2883{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002884 struct snd_soc_card *card = component->card;
2885
2886 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002887 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002888
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002889 list_del(&component->list);
2890}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002891
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002892#define ENDIANNESS_MAP(name) \
2893 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2894static u64 endianness_format_map[] = {
2895 ENDIANNESS_MAP(S16_),
2896 ENDIANNESS_MAP(U16_),
2897 ENDIANNESS_MAP(S24_),
2898 ENDIANNESS_MAP(U24_),
2899 ENDIANNESS_MAP(S32_),
2900 ENDIANNESS_MAP(U32_),
2901 ENDIANNESS_MAP(S24_3),
2902 ENDIANNESS_MAP(U24_3),
2903 ENDIANNESS_MAP(S20_3),
2904 ENDIANNESS_MAP(U20_3),
2905 ENDIANNESS_MAP(S18_3),
2906 ENDIANNESS_MAP(U18_3),
2907 ENDIANNESS_MAP(FLOAT_),
2908 ENDIANNESS_MAP(FLOAT64_),
2909 ENDIANNESS_MAP(IEC958_SUBFRAME_),
2910};
2911
2912/*
2913 * Fix up the DAI formats for endianness: codecs don't actually see
2914 * the endianness of the data but we're using the CPU format
2915 * definitions which do need to include endianness so we ensure that
2916 * codec DAIs always have both big and little endian variants set.
2917 */
2918static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2919{
2920 int i;
2921
2922 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2923 if (stream->formats & endianness_format_map[i])
2924 stream->formats |= endianness_format_map[i];
2925}
2926
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002927static void snd_soc_try_rebind_card(void)
2928{
2929 struct snd_soc_card *card, *c;
2930
2931 if (!list_empty(&unbind_card_list)) {
2932 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
2933 if (!snd_soc_bind_card(card))
2934 list_del(&card->list);
2935 }
2936 }
2937}
2938
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002939int snd_soc_add_component(struct device *dev,
2940 struct snd_soc_component *component,
2941 const struct snd_soc_component_driver *component_driver,
2942 struct snd_soc_dai_driver *dai_drv,
2943 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002944{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002945 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002946 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002947
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002948 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002949 if (ret)
2950 goto err_free;
2951
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002952 if (component_driver->endianness) {
2953 for (i = 0; i < num_dai; i++) {
2954 convert_endianness_formats(&dai_drv[i].playback);
2955 convert_endianness_formats(&dai_drv[i].capture);
2956 }
2957 }
2958
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002959 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002960 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002961 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002962 goto err_cleanup;
2963 }
2964
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002965 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002966 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002967
2968 return 0;
2969
2970err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00002971 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002972err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002973 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002974}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002975EXPORT_SYMBOL_GPL(snd_soc_add_component);
2976
2977int snd_soc_register_component(struct device *dev,
2978 const struct snd_soc_component_driver *component_driver,
2979 struct snd_soc_dai_driver *dai_drv,
2980 int num_dai)
2981{
2982 struct snd_soc_component *component;
2983
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00002984 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00002985 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002986 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00002987
2988 return snd_soc_add_component(dev, component, component_driver,
2989 dai_drv, num_dai);
2990}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002991EXPORT_SYMBOL_GPL(snd_soc_register_component);
2992
2993/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002994 * snd_soc_unregister_component - Unregister all related component
2995 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002996 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06002997 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002998 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00002999static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003000{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003001 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003002 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003003
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003004 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003005 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003006 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003007 continue;
3008
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003009 snd_soc_tplg_component_remove(component,
3010 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003011 snd_soc_component_del_unlocked(component);
3012 found = 1;
3013 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003014 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003015 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003016
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003017 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003018 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003019
3020 return found;
3021}
3022
3023void snd_soc_unregister_component(struct device *dev)
3024{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003025 while (__snd_soc_unregister_component(dev))
3026 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003027}
3028EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3029
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003030struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3031 const char *driver_name)
3032{
3033 struct snd_soc_component *component;
3034 struct snd_soc_component *ret;
3035
3036 ret = NULL;
3037 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003038 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003039 if (dev != component->dev)
3040 continue;
3041
3042 if (driver_name &&
3043 (driver_name != component->driver->name) &&
3044 (strcmp(component->driver->name, driver_name) != 0))
3045 continue;
3046
3047 ret = component;
3048 break;
3049 }
3050 mutex_unlock(&client_mutex);
3051
3052 return ret;
3053}
3054EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3055
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003056/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003057int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3058 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003059{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003060 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003061 int ret;
3062
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303063 if (!card->dev) {
3064 pr_err("card->dev is not set before calling %s\n", __func__);
3065 return -EINVAL;
3066 }
3067
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003068 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303069
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003070 ret = of_property_read_string_index(np, propname, 0, &card->name);
3071 /*
3072 * EINVAL means the property does not exist. This is fine providing
3073 * card->name was previously set, which is checked later in
3074 * snd_soc_register_card.
3075 */
3076 if (ret < 0 && ret != -EINVAL) {
3077 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003078 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003079 propname, ret);
3080 return ret;
3081 }
3082
3083 return 0;
3084}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003085EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003086
Xiubo Li9a6d4862014-02-08 15:59:52 +08003087static const struct snd_soc_dapm_widget simple_widgets[] = {
3088 SND_SOC_DAPM_MIC("Microphone", NULL),
3089 SND_SOC_DAPM_LINE("Line", NULL),
3090 SND_SOC_DAPM_HP("Headphone", NULL),
3091 SND_SOC_DAPM_SPK("Speaker", NULL),
3092};
3093
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003094int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003095 const char *propname)
3096{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003097 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003098 struct snd_soc_dapm_widget *widgets;
3099 const char *template, *wname;
3100 int i, j, num_widgets, ret;
3101
3102 num_widgets = of_property_count_strings(np, propname);
3103 if (num_widgets < 0) {
3104 dev_err(card->dev,
3105 "ASoC: Property '%s' does not exist\n", propname);
3106 return -EINVAL;
3107 }
3108 if (num_widgets & 1) {
3109 dev_err(card->dev,
3110 "ASoC: Property '%s' length is not even\n", propname);
3111 return -EINVAL;
3112 }
3113
3114 num_widgets /= 2;
3115 if (!num_widgets) {
3116 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3117 propname);
3118 return -EINVAL;
3119 }
3120
3121 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3122 GFP_KERNEL);
3123 if (!widgets) {
3124 dev_err(card->dev,
3125 "ASoC: Could not allocate memory for widgets\n");
3126 return -ENOMEM;
3127 }
3128
3129 for (i = 0; i < num_widgets; i++) {
3130 ret = of_property_read_string_index(np, propname,
3131 2 * i, &template);
3132 if (ret) {
3133 dev_err(card->dev,
3134 "ASoC: Property '%s' index %d read error:%d\n",
3135 propname, 2 * i, ret);
3136 return -EINVAL;
3137 }
3138
3139 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3140 if (!strncmp(template, simple_widgets[j].name,
3141 strlen(simple_widgets[j].name))) {
3142 widgets[i] = simple_widgets[j];
3143 break;
3144 }
3145 }
3146
3147 if (j >= ARRAY_SIZE(simple_widgets)) {
3148 dev_err(card->dev,
3149 "ASoC: DAPM widget '%s' is not supported\n",
3150 template);
3151 return -EINVAL;
3152 }
3153
3154 ret = of_property_read_string_index(np, propname,
3155 (2 * i) + 1,
3156 &wname);
3157 if (ret) {
3158 dev_err(card->dev,
3159 "ASoC: Property '%s' index %d read error:%d\n",
3160 propname, (2 * i) + 1, ret);
3161 return -EINVAL;
3162 }
3163
3164 widgets[i].name = wname;
3165 }
3166
Nicolin Chenf23e8602015-02-14 17:22:49 -08003167 card->of_dapm_widgets = widgets;
3168 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003169
3170 return 0;
3171}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003172EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003173
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003174int snd_soc_of_get_slot_mask(struct device_node *np,
3175 const char *prop_name,
3176 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003177{
3178 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003179 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003180 int i;
3181
3182 if (!of_slot_mask)
3183 return 0;
3184 val /= sizeof(u32);
3185 for (i = 0; i < val; i++)
3186 if (be32_to_cpup(&of_slot_mask[i]))
3187 *mask |= (1 << i);
3188
3189 return val;
3190}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003191EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003192
Xiubo Li89c67852014-02-14 09:34:35 +08003193int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003194 unsigned int *tx_mask,
3195 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003196 unsigned int *slots,
3197 unsigned int *slot_width)
3198{
3199 u32 val;
3200 int ret;
3201
Jyri Sarha61310842015-09-09 21:27:43 +03003202 if (tx_mask)
3203 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3204 if (rx_mask)
3205 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3206
Xiubo Li89c67852014-02-14 09:34:35 +08003207 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3208 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3209 if (ret)
3210 return ret;
3211
3212 if (slots)
3213 *slots = val;
3214 }
3215
3216 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3217 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3218 if (ret)
3219 return ret;
3220
3221 if (slot_width)
3222 *slot_width = val;
3223 }
3224
3225 return 0;
3226}
3227EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3228
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003229void snd_soc_of_parse_node_prefix(struct device_node *np,
3230 struct snd_soc_codec_conf *codec_conf,
3231 struct device_node *of_node,
3232 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003233{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003234 const char *str;
3235 int ret;
3236
3237 ret = of_property_read_string(np, propname, &str);
3238 if (ret < 0) {
3239 /* no prefix is not error */
3240 return;
3241 }
3242
3243 codec_conf->of_node = of_node;
3244 codec_conf->name_prefix = str;
3245}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003246EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003247
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003248int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003249 const char *propname)
3250{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003251 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003252 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003253 struct snd_soc_dapm_route *routes;
3254 int i, ret;
3255
3256 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003257 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003258 dev_err(card->dev,
3259 "ASoC: Property '%s' does not exist or its length is not even\n",
3260 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003261 return -EINVAL;
3262 }
3263 num_routes /= 2;
3264 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003265 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003266 propname);
3267 return -EINVAL;
3268 }
3269
Kees Cooka86854d2018-06-12 14:07:58 -07003270 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003271 GFP_KERNEL);
3272 if (!routes) {
3273 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003274 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003275 return -EINVAL;
3276 }
3277
3278 for (i = 0; i < num_routes; i++) {
3279 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003280 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003281 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003282 dev_err(card->dev,
3283 "ASoC: Property '%s' index %d could not be read: %d\n",
3284 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003285 return -EINVAL;
3286 }
3287 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003288 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003289 if (ret) {
3290 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003291 "ASoC: Property '%s' index %d could not be read: %d\n",
3292 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003293 return -EINVAL;
3294 }
3295 }
3296
Nicolin Chenf23e8602015-02-14 17:22:49 -08003297 card->num_of_dapm_routes = num_routes;
3298 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003299
3300 return 0;
3301}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003302EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003303
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003304unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003305 const char *prefix,
3306 struct device_node **bitclkmaster,
3307 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003308{
3309 int ret, i;
3310 char prop[128];
3311 unsigned int format = 0;
3312 int bit, frame;
3313 const char *str;
3314 struct {
3315 char *name;
3316 unsigned int val;
3317 } of_fmt_table[] = {
3318 { "i2s", SND_SOC_DAIFMT_I2S },
3319 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3320 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3321 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3322 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3323 { "ac97", SND_SOC_DAIFMT_AC97 },
3324 { "pdm", SND_SOC_DAIFMT_PDM},
3325 { "msb", SND_SOC_DAIFMT_MSB },
3326 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003327 };
3328
3329 if (!prefix)
3330 prefix = "";
3331
3332 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003333 * check "dai-format = xxx"
3334 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003335 * SND_SOC_DAIFMT_FORMAT_MASK area
3336 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003337 ret = of_property_read_string(np, "dai-format", &str);
3338 if (ret < 0) {
3339 snprintf(prop, sizeof(prop), "%sformat", prefix);
3340 ret = of_property_read_string(np, prop, &str);
3341 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003342 if (ret == 0) {
3343 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3344 if (strcmp(str, of_fmt_table[i].name) == 0) {
3345 format |= of_fmt_table[i].val;
3346 break;
3347 }
3348 }
3349 }
3350
3351 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003352 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003353 * SND_SOC_DAIFMT_CLOCK_MASK area
3354 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003355 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003356 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003357 format |= SND_SOC_DAIFMT_CONT;
3358 else
3359 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003360
3361 /*
3362 * check "[prefix]bitclock-inversion"
3363 * check "[prefix]frame-inversion"
3364 * SND_SOC_DAIFMT_INV_MASK area
3365 */
3366 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3367 bit = !!of_get_property(np, prop, NULL);
3368
3369 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3370 frame = !!of_get_property(np, prop, NULL);
3371
3372 switch ((bit << 4) + frame) {
3373 case 0x11:
3374 format |= SND_SOC_DAIFMT_IB_IF;
3375 break;
3376 case 0x10:
3377 format |= SND_SOC_DAIFMT_IB_NF;
3378 break;
3379 case 0x01:
3380 format |= SND_SOC_DAIFMT_NB_IF;
3381 break;
3382 default:
3383 /* SND_SOC_DAIFMT_NB_NF is default */
3384 break;
3385 }
3386
3387 /*
3388 * check "[prefix]bitclock-master"
3389 * check "[prefix]frame-master"
3390 * SND_SOC_DAIFMT_MASTER_MASK area
3391 */
3392 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3393 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003394 if (bit && bitclkmaster)
3395 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003396
3397 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3398 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003399 if (frame && framemaster)
3400 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003401
3402 switch ((bit << 4) + frame) {
3403 case 0x11:
3404 format |= SND_SOC_DAIFMT_CBM_CFM;
3405 break;
3406 case 0x10:
3407 format |= SND_SOC_DAIFMT_CBM_CFS;
3408 break;
3409 case 0x01:
3410 format |= SND_SOC_DAIFMT_CBS_CFM;
3411 break;
3412 default:
3413 format |= SND_SOC_DAIFMT_CBS_CFS;
3414 break;
3415 }
3416
3417 return format;
3418}
3419EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3420
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003421int snd_soc_get_dai_id(struct device_node *ep)
3422{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003423 struct snd_soc_component *component;
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003424 struct snd_soc_dai_link_component dlc;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003425 int ret;
3426
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003427 dlc.of_node = of_graph_get_port_parent(ep);
3428 dlc.name = NULL;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003429 /*
3430 * For example HDMI case, HDMI has video/sound port,
3431 * but ALSA SoC needs sound port number only.
3432 * Thus counting HDMI DT port/endpoint doesn't work.
3433 * Then, it should have .of_xlate_dai_id
3434 */
3435 ret = -ENOTSUPP;
3436 mutex_lock(&client_mutex);
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003437 component = soc_find_component(&dlc);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003438 if (component &&
3439 component->driver->of_xlate_dai_id)
3440 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003441 mutex_unlock(&client_mutex);
3442
Kuninori Morimotoc1e230f2019-06-20 09:49:27 +09003443 of_node_put(dlc.of_node);
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003444
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003445 return ret;
3446}
3447EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3448
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003449int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003450 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003451{
3452 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003453 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003454 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003455
3456 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003457 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003458 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003459
3460 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003461 continue;
3462
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003463 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003464 ret = pos->driver->of_xlate_dai_name(pos,
3465 args,
3466 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003467 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003468 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003469 int id = -1;
3470
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003471 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003472 case 0:
3473 id = 0; /* same as dai_drv[0] */
3474 break;
3475 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003476 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003477 break;
3478 default:
3479 /* not supported */
3480 break;
3481 }
3482
3483 if (id < 0 || id >= pos->num_dai) {
3484 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003485 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003486 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003487
3488 ret = 0;
3489
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003490 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003491 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003492 if (id == 0)
3493 break;
3494 id--;
3495 }
3496
3497 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003498 if (!*dai_name)
3499 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003500 }
3501
Kuninori Morimotocb470082013-09-10 17:39:56 -07003502 break;
3503 }
3504 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003505 return ret;
3506}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003507EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003508
3509int snd_soc_of_get_dai_name(struct device_node *of_node,
3510 const char **dai_name)
3511{
3512 struct of_phandle_args args;
3513 int ret;
3514
3515 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3516 "#sound-dai-cells", 0, &args);
3517 if (ret)
3518 return ret;
3519
3520 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003521
3522 of_node_put(args.np);
3523
3524 return ret;
3525}
3526EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3527
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003528/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003529 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3530 * @dai_link: DAI link
3531 *
3532 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3533 */
3534void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3535{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003536 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003537 int index;
3538
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003539 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003540 if (!component->of_node)
3541 break;
3542 of_node_put(component->of_node);
3543 component->of_node = NULL;
3544 }
3545}
3546EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3547
3548/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003549 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3550 * @dev: Card device
3551 * @of_node: Device node
3552 * @dai_link: DAI link
3553 *
3554 * Builds an array of CODEC DAI components from the DAI link property
3555 * 'sound-dai'.
3556 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003557 * The device nodes in the array (of_node) must be dereferenced by calling
3558 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003559 *
3560 * Returns 0 for success
3561 */
3562int snd_soc_of_get_dai_link_codecs(struct device *dev,
3563 struct device_node *of_node,
3564 struct snd_soc_dai_link *dai_link)
3565{
3566 struct of_phandle_args args;
3567 struct snd_soc_dai_link_component *component;
3568 char *name;
3569 int index, num_codecs, ret;
3570
3571 /* Count the number of CODECs */
3572 name = "sound-dai";
3573 num_codecs = of_count_phandle_with_args(of_node, name,
3574 "#sound-dai-cells");
3575 if (num_codecs <= 0) {
3576 if (num_codecs == -ENOENT)
3577 dev_err(dev, "No 'sound-dai' property\n");
3578 else
3579 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3580 return num_codecs;
3581 }
Kees Cooka86854d2018-06-12 14:07:58 -07003582 component = devm_kcalloc(dev,
3583 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003584 GFP_KERNEL);
3585 if (!component)
3586 return -ENOMEM;
3587 dai_link->codecs = component;
3588 dai_link->num_codecs = num_codecs;
3589
3590 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003591 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003592 ret = of_parse_phandle_with_args(of_node, name,
3593 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003594 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003595 if (ret)
3596 goto err;
3597 component->of_node = args.np;
3598 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3599 if (ret < 0)
3600 goto err;
3601 }
3602 return 0;
3603err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003604 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003605 dai_link->codecs = NULL;
3606 dai_link->num_codecs = 0;
3607 return ret;
3608}
3609EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3610
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003611static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003612{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003613 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003614 snd_soc_util_init();
3615
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003616 return platform_driver_register(&soc_driver);
3617}
Mark Brown4abe8e12010-10-12 17:41:03 +01003618module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003619
Mark Brown7d8c16a2008-11-30 22:11:24 +00003620static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003621{
Mark Brownfb257892011-04-28 10:57:54 +01003622 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003623 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003624
Mark Brown3ff3f642008-05-19 12:32:25 +02003625 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003626}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003627module_exit(snd_soc_exit);
3628
3629/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003630MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003631MODULE_DESCRIPTION("ALSA SoC Core");
3632MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003633MODULE_ALIAS("platform:soc-audio");