blob: 3be0310d5c810c8310be6e726e9fcffc648366fc [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);
Mark Brownc5af3a22008-11-28 13:29:45 +000055
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056/*
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
60 */
61static int pmdown_time = 5000;
62module_param(pmdown_time, int, 0);
63MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64
Mengdong Lin98faf432017-06-28 15:01:39 +080065/* If a DMI filed contain strings in this blacklist (e.g.
66 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
67 * as invalid and dropped when setting the card long name from DMI info.
68 */
69static const char * const dmi_blacklist[] = {
70 "To be filled by OEM",
71 "TBD by OEM",
72 "Default String",
73 "Board Manufacturer",
74 "Board Vendor Name",
75 "Board Product Name",
76 NULL, /* terminator */
77};
78
Mark Browndbe21402010-02-12 11:37:24 +000079static ssize_t pmdown_time_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
Mark Brown36ae1a92012-01-06 17:12:45 -080082 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000083
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000085}
86
87static ssize_t pmdown_time_set(struct device *dev,
88 struct device_attribute *attr,
89 const char *buf, size_t count)
90{
Mark Brown36ae1a92012-01-06 17:12:45 -080091 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070092 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000093
Jingoo Hanb785a492013-07-19 16:24:59 +090094 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -070095 if (ret)
96 return ret;
Mark Browndbe21402010-02-12 11:37:24 +000097
98 return count;
99}
100
101static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
102
Takashi Iwaid29697d2015-01-30 20:16:37 +0100103static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100104 &dev_attr_pmdown_time.attr,
105 NULL
106};
107
108static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
109 struct attribute *attr, int idx)
110{
111 struct device *dev = kobj_to_dev(kobj);
112 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
113
114 if (attr == &dev_attr_pmdown_time.attr)
115 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000116 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100117}
118
119static const struct attribute_group soc_dapm_dev_group = {
120 .attrs = soc_dapm_dev_attrs,
121 .is_visible = soc_dev_attr_is_visible,
122};
123
Mark Brownf7e73b262018-03-09 12:46:27 +0000124static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100125 .attrs = soc_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
129static const struct attribute_group *soc_dev_attr_groups[] = {
130 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000131 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100132 NULL
133};
134
Mark Brown2624d5f2009-11-03 21:56:13 +0000135#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200136static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100137{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200138 if (!component->card->debugfs_card_root)
139 return;
140
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141 if (component->debugfs_prefix) {
142 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100143
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200144 name = kasprintf(GFP_KERNEL, "%s:%s",
145 component->debugfs_prefix, component->name);
146 if (name) {
147 component->debugfs_root = debugfs_create_dir(name,
148 component->card->debugfs_card_root);
149 kfree(name);
150 }
151 } else {
152 component->debugfs_root = debugfs_create_dir(component->name,
153 component->card->debugfs_card_root);
154 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100155
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200156 if (!component->debugfs_root) {
157 dev_warn(component->dev,
158 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000159 return;
160 }
161
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200162 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
163 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200164}
165
166static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
167{
168 debugfs_remove_recursive(component->debugfs_root);
169}
170
Peng Donglinc15b2a12018-02-14 22:48:07 +0800171static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100172{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100173 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100174 struct snd_soc_dai *dai;
175
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100176 mutex_lock(&client_mutex);
177
Donglin Peng700c17c2018-01-18 13:31:26 +0800178 list_for_each_entry(component, &component_list, list)
179 list_for_each_entry(dai, &component->dai_list, list)
180 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100181
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100182 mutex_unlock(&client_mutex);
183
Donglin Peng700c17c2018-01-18 13:31:26 +0800184 return 0;
185}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800186DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100187
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000188static int component_list_show(struct seq_file *m, void *v)
189{
190 struct snd_soc_component *component;
191
192 mutex_lock(&client_mutex);
193
194 list_for_each_entry(component, &component_list, list)
195 seq_printf(m, "%s\n", component->name);
196
197 mutex_unlock(&client_mutex);
198
199 return 0;
200}
201DEFINE_SHOW_ATTRIBUTE(component_list);
202
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200203static void soc_init_card_debugfs(struct snd_soc_card *card)
204{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200205 if (!snd_soc_debugfs_root)
206 return;
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000209 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200210 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200211 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100212 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200213 return;
214 }
215
216 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
217 card->debugfs_card_root,
218 &card->pop_time);
219 if (!card->debugfs_pop_time)
220 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000221 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200222}
223
224static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
225{
226 debugfs_remove_recursive(card->debugfs_card_root);
227}
228
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200229static void snd_soc_debugfs_init(void)
230{
231 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300232 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200233 pr_warn("ASoC: Failed to create debugfs directory\n");
234 snd_soc_debugfs_root = NULL;
235 return;
236 }
237
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
239 &dai_list_fops))
240 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000241
242 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
243 &component_list_fops))
244 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200245}
246
247static void snd_soc_debugfs_exit(void)
248{
249 debugfs_remove_recursive(snd_soc_debugfs_root);
250}
251
Mark Brown2624d5f2009-11-03 21:56:13 +0000252#else
253
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200254static inline void soc_init_component_debugfs(
255 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000256{
257}
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_cleanup_component_debugfs(
260 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000261{
262}
263
Axel Linb95fccb2010-11-09 17:06:44 +0800264static inline void soc_init_card_debugfs(struct snd_soc_card *card)
265{
266}
267
268static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
269{
270}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200271
272static inline void snd_soc_debugfs_init(void)
273{
274}
275
276static inline void snd_soc_debugfs_exit(void)
277{
278}
279
Mark Brown2624d5f2009-11-03 21:56:13 +0000280#endif
281
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000282static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
283 struct snd_soc_component *component)
284{
285 struct snd_soc_rtdcom_list *rtdcom;
286 struct snd_soc_rtdcom_list *new_rtdcom;
287
288 for_each_rtdcom(rtd, rtdcom) {
289 /* already connected */
290 if (rtdcom->component == component)
291 return 0;
292 }
293
294 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
295 if (!new_rtdcom)
296 return -ENOMEM;
297
298 new_rtdcom->component = component;
299 INIT_LIST_HEAD(&new_rtdcom->list);
300
301 list_add_tail(&new_rtdcom->list, &rtd->component_list);
302
303 return 0;
304}
305
306static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
307{
308 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
309
310 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
311 kfree(rtdcom1);
312
313 INIT_LIST_HEAD(&rtd->component_list);
314}
315
316struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
317 const char *driver_name)
318{
319 struct snd_soc_rtdcom_list *rtdcom;
320
Kuninori Morimoto971da242018-01-23 00:41:24 +0000321 if (!driver_name)
322 return NULL;
323
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000324 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000325 const char *component_name = rtdcom->component->driver->name;
326
327 if (!component_name)
328 continue;
329
330 if ((component_name == driver_name) ||
331 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000332 return rtdcom->component;
333 }
334
335 return NULL;
336}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000337EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000338
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100339struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
340 const char *dai_link, int stream)
341{
Mengdong Lin1a497982015-11-18 02:34:11 -0500342 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100343
Mengdong Lin1a497982015-11-18 02:34:11 -0500344 list_for_each_entry(rtd, &card->rtd_list, list) {
345 if (rtd->dai_link->no_pcm &&
346 !strcmp(rtd->dai_link->name, dai_link))
347 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000349 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100350 return NULL;
351}
352EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
353
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000354static const struct snd_soc_ops null_snd_soc_ops;
355
Mengdong Lin1a497982015-11-18 02:34:11 -0500356static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
357 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
358{
359 struct snd_soc_pcm_runtime *rtd;
360
361 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
362 if (!rtd)
363 return NULL;
364
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000365 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500366 rtd->card = card;
367 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000368 if (!rtd->dai_link->ops)
369 rtd->dai_link->ops = &null_snd_soc_ops;
370
Kees Cook6396bb22018-06-12 14:03:40 -0700371 rtd->codec_dais = kcalloc(dai_link->num_codecs,
372 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500373 GFP_KERNEL);
374 if (!rtd->codec_dais) {
375 kfree(rtd);
376 return NULL;
377 }
378
379 return rtd;
380}
381
382static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
383{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000384 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000385 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500386 kfree(rtd);
387}
388
389static void soc_add_pcm_runtime(struct snd_soc_card *card,
390 struct snd_soc_pcm_runtime *rtd)
391{
392 list_add_tail(&rtd->list, &card->rtd_list);
393 rtd->num = card->num_rtd;
394 card->num_rtd++;
395}
396
397static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
398{
399 struct snd_soc_pcm_runtime *rtd, *_rtd;
400
401 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
402 list_del(&rtd->list);
403 soc_free_pcm_runtime(rtd);
404 }
405
406 card->num_rtd = 0;
407}
408
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100409struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
410 const char *dai_link)
411{
Mengdong Lin1a497982015-11-18 02:34:11 -0500412 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100413
Mengdong Lin1a497982015-11-18 02:34:11 -0500414 list_for_each_entry(rtd, &card->rtd_list, list) {
415 if (!strcmp(rtd->dai_link->name, dai_link))
416 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100417 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000418 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100419 return NULL;
420}
421EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
422
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100423static void codec2codec_close_delayed_work(struct work_struct *work)
424{
425 /* Currently nothing to do for c2c links
426 * Since c2c links are internal nodes in the DAPM graph and
427 * don't interface with the outside world or application layer
428 * we don't have to do any special handling on close.
429 */
430}
431
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000432#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000434int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200435{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000436 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000437 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500438 struct snd_soc_pcm_runtime *rtd;
439 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200440
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200441 /* If the card is not initialized yet there is nothing to do */
442 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200443 return 0;
444
Andy Green6ed25972008-06-13 16:24:05 +0100445 /* Due to the resume being scheduled into a workqueue we could
446 * suspend before that's finished - wait for it to complete.
447 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000448 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100449
450 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100452
Mark Browna00f90f2010-12-02 16:24:24 +0000453 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500454 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100455
Mengdong Lin1a497982015-11-18 02:34:11 -0500456 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100457 continue;
458
Mengdong Lin1a497982015-11-18 02:34:11 -0500459 for (i = 0; i < rtd->num_codecs; i++) {
460 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200461 struct snd_soc_dai_driver *drv = dai->driver;
462
463 if (drv->ops->digital_mute && dai->playback_active)
464 drv->ops->digital_mute(dai, 1);
465 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200466 }
467
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100468 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500469 list_for_each_entry(rtd, &card->rtd_list, list) {
470 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100471 continue;
472
Mengdong Lin1a497982015-11-18 02:34:11 -0500473 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100474 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100475
Mark Brown87506542008-11-18 20:50:34 +0000476 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000477 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200478
Mengdong Lin1a497982015-11-18 02:34:11 -0500479 list_for_each_entry(rtd, &card->rtd_list, list) {
480 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100481
Mengdong Lin1a497982015-11-18 02:34:11 -0500482 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100483 continue;
484
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100485 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000486 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100487 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200489 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500490 list_for_each_entry(rtd, &card->rtd_list, list)
491 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100492
Mengdong Lin1a497982015-11-18 02:34:11 -0500493 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100496 continue;
497
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800499 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800500 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501
Mengdong Lin1a497982015-11-18 02:34:11 -0500502 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800503 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800504 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 }
506
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200507 /* Recheck all endpoints too, their state is affected by suspend */
508 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700509 snd_soc_dapm_sync(&card->dapm);
510
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000511 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000512 list_for_each_entry(component, &card->component_dev_list, card_list) {
513 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000514
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000515 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000517 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200518 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000520 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000521 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000522 * bias off then being in STANDBY
523 * means it's doing something,
524 * otherwise fall through.
525 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200526 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000527 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200528 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000529 break;
530 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000533 if (component->driver->suspend)
534 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000535 component->suspended = 1;
536 if (component->regmap)
537 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800538 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000539 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 break;
541 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000542 dev_dbg(component->dev,
543 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 break;
545 }
546 }
547 }
548
Mengdong Lin1a497982015-11-18 02:34:11 -0500549 list_for_each_entry(rtd, &card->rtd_list, list) {
550 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551
Mengdong Lin1a497982015-11-18 02:34:11 -0500552 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 continue;
554
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100555 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800557
558 /* deactivate pins to sleep state */
559 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 }
561
Mark Brown87506542008-11-18 20:50:34 +0000562 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000563 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564
565 return 0;
566}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000567EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568
Andy Green6ed25972008-06-13 16:24:05 +0100569/* deferred resume work, so resume can complete before we finished
570 * setting our codec back up, which can be very slow on I2C
571 */
572static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 struct snd_soc_card *card =
575 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500576 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000577 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500578 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Andy Green6ed25972008-06-13 16:24:05 +0100580 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
581 * so userspace apps are blocked from touching us
582 */
583
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000584 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100585
Mark Brown9949788b2010-05-07 20:24:05 +0100586 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100588
Mark Brown87506542008-11-18 20:50:34 +0000589 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000590 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100592 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500593 list_for_each_entry(rtd, &card->rtd_list, list) {
594 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100595
Mengdong Lin1a497982015-11-18 02:34:11 -0500596 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100597 continue;
598
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100599 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601 }
602
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000603 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000604 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000605 if (component->driver->resume)
606 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000607 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100608 }
609 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200610
Mengdong Lin1a497982015-11-18 02:34:11 -0500611 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100612
Mengdong Lin1a497982015-11-18 02:34:11 -0500613 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100614 continue;
615
Mengdong Lin1a497982015-11-18 02:34:11 -0500616 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000617 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800618 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619
Mengdong Lin1a497982015-11-18 02:34:11 -0500620 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000621 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800622 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623 }
624
Mark Brown3ff3f642008-05-19 12:32:25 +0200625 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500626 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100627
Mengdong Lin1a497982015-11-18 02:34:11 -0500628 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100629 continue;
630
Mengdong Lin1a497982015-11-18 02:34:11 -0500631 for (i = 0; i < rtd->num_codecs; i++) {
632 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200633 struct snd_soc_dai_driver *drv = dai->driver;
634
635 if (drv->ops->digital_mute && dai->playback_active)
636 drv->ops->digital_mute(dai, 0);
637 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638 }
639
Mengdong Lin1a497982015-11-18 02:34:11 -0500640 list_for_each_entry(rtd, &card->rtd_list, list) {
641 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100642
Mengdong Lin1a497982015-11-18 02:34:11 -0500643 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100644 continue;
645
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100646 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200648 }
649
Mark Brown87506542008-11-18 20:50:34 +0000650 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000651 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000653 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100654
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200655 /* Recheck all endpoints too, their state is affected by suspend */
656 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700657 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530658
659 /* userspace can access us now we are back as we were before */
660 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100661}
662
663/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000664int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100665{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000666 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100667 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500668 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200669
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200670 /* If the card is not initialized yet there is nothing to do */
671 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800672 return 0;
673
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800674 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500675 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200676 struct snd_soc_dai **codec_dais = rtd->codec_dais;
677 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
678 int j;
679
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800680 if (cpu_dai->active)
681 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200682
683 for (j = 0; j < rtd->num_codecs; j++) {
684 struct snd_soc_dai *codec_dai = codec_dais[j];
685 if (codec_dai->active)
686 pinctrl_pm_select_default_state(codec_dai->dev);
687 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800688 }
689
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100690 /*
691 * DAIs that also act as the control bus master might have other drivers
692 * hanging off them so need to resume immediately. Other drivers don't
693 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100694 * due to I/O costs and anti-pop so handle them out of line.
695 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500696 list_for_each_entry(rtd, &card->rtd_list, list) {
697 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100698 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600699 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100700 if (bus_control) {
701 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600702 soc_resume_deferred(&card->deferred_resume_work);
703 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000704 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600705 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000706 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100707 }
Andy Green6ed25972008-06-13 16:24:05 +0100708
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 return 0;
710}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000711EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000713#define snd_soc_suspend NULL
714#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715#endif
716
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100717static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800718};
719
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200720static struct snd_soc_component *soc_find_component(
721 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100722{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200723 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100724
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100725 lockdep_assert_held(&client_mutex);
726
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200727 list_for_each_entry(component, &component_list, list) {
728 if (of_node) {
729 if (component->dev->of_node == of_node)
730 return component;
731 } else if (strcmp(component->name, name) == 0) {
732 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100733 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100734 }
735
736 return NULL;
737}
738
Mengdong Linfbb88b52016-04-22 12:25:33 +0800739/**
740 * snd_soc_find_dai - Find a registered DAI
741 *
Jeffy Chen49584712017-08-22 15:57:21 +0800742 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800743 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700744 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800745 * find the DAI of the same name. The component's of_node and name
746 * should also match if being specified.
747 *
748 * Return: pointer of DAI, or NULL if not found.
749 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800750struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200751 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100752{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200753 struct snd_soc_component *component;
754 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300755 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100756
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100757 lockdep_assert_held(&client_mutex);
758
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200759 /* Find CPU DAI from registered DAIs*/
760 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300761 component_of_node = component->dev->of_node;
762 if (!component_of_node && component->dev->parent)
763 component_of_node = component->dev->parent->of_node;
764
765 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200766 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100767 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200768 continue;
769 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800770 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800771 && (!dai->driver->name
772 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100773 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100774
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200775 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100776 }
777 }
778
779 return NULL;
780}
Mengdong Lin305e9022016-04-19 13:12:25 +0800781EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100782
Mengdong Lin17fb17552016-11-03 01:04:12 +0800783
784/**
785 * snd_soc_find_dai_link - Find a DAI link
786 *
787 * @card: soc card
788 * @id: DAI link ID to match
789 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000790 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800791 *
792 * This function will search all existing DAI links of the soc card to
793 * find the link of the same ID. Since DAI links may not have their
794 * unique ID, so name and stream name should also match if being
795 * specified.
796 *
797 * Return: pointer of DAI link, or NULL if not found.
798 */
799struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
800 int id, const char *name,
801 const char *stream_name)
802{
803 struct snd_soc_dai_link *link, *_link;
804
805 lockdep_assert_held(&client_mutex);
806
807 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
808 if (link->id != id)
809 continue;
810
811 if (name && (!link->name || strcmp(name, link->name)))
812 continue;
813
814 if (stream_name && (!link->stream_name
815 || strcmp(stream_name, link->stream_name)))
816 continue;
817
818 return link;
819 }
820
821 return NULL;
822}
823EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
824
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800825static bool soc_is_dai_link_bound(struct snd_soc_card *card,
826 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800828 struct snd_soc_pcm_runtime *rtd;
829
830 list_for_each_entry(rtd, &card->rtd_list, list) {
831 if (rtd->dai_link == dai_link)
832 return true;
833 }
834
835 return false;
836}
837
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500838static int soc_bind_dai_link(struct snd_soc_card *card,
839 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200840{
Mengdong Lin1a497982015-11-18 02:34:11 -0500841 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200842 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200843 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000844 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500845 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +0000846 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +0100847 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200848 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
Liam Girdwooda655de82018-07-02 16:59:54 +0100850 if (dai_link->ignore)
851 return 0;
852
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500853 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000854
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800855 if (soc_is_dai_link_bound(card, dai_link)) {
856 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
857 dai_link->name);
858 return 0;
859 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530861 rtd = soc_new_pcm_runtime(card, dai_link);
862 if (!rtd)
863 return -ENOMEM;
864
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200865 cpu_dai_component.name = dai_link->cpu_name;
866 cpu_dai_component.of_node = dai_link->cpu_of_node;
867 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
868 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000869 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100870 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
871 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500872 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000873 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000874 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000875
Benoit Cousson88bd8702014-07-08 23:19:34 +0200876 rtd->num_codecs = dai_link->num_codecs;
877
878 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500879 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200880 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200881 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200882 if (!codec_dais[i]) {
883 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
884 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500885 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200886 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000887 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000888 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100889
Benoit Cousson88bd8702014-07-08 23:19:34 +0200890 /* Single codec links expect codec and codec_dai in runtime data */
891 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892
Mark Brown848dd8b2011-04-27 18:16:32 +0100893 /* if there's no platform we match on the empty platform */
894 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700895 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100896 platform_name = "snd-soc-dummy";
897
Mark Brownb19e6e72012-03-14 21:18:39 +0000898 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000899 list_for_each_entry(component, &component_list, list) {
900 platform_of_node = component->dev->of_node;
901 if (!platform_of_node && component->dev->parent->of_node)
902 platform_of_node = component->dev->parent->of_node;
903
904 if (dai_link->platform_of_node) {
905 if (platform_of_node != dai_link->platform_of_node)
906 continue;
907 } else {
908 if (strcmp(component->name, platform_name))
909 continue;
910 }
911
912 snd_soc_rtdcom_add(rtd, component);
913 }
914
Mengdong Lin1a497982015-11-18 02:34:11 -0500915 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000916 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500917
918_err_defer:
919 soc_free_pcm_runtime(rtd);
920 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921}
922
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200923static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600924{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200925 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200926 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600927
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000928 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600929
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000930 if (component->driver->remove)
931 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600932
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200933 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600934
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200935 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200936 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200937 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600938}
939
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200940static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200941{
942 int err;
943
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200944 if (dai && dai->probed &&
945 dai->driver->remove_order == order) {
946 if (dai->driver->remove) {
947 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100948 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200949 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100950 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200951 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100952 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200953 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100954 }
955}
956
Mengdong Lin1a497982015-11-18 02:34:11 -0500957static void soc_remove_link_dais(struct snd_soc_card *card,
958 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200960 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961
962 /* unregister the rtd device */
963 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800964 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 rtd->dev_registered = 0;
966 }
967
968 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200969 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200970 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200972 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973}
974
Mengdong Lin1a497982015-11-18 02:34:11 -0500975static void soc_remove_link_components(struct snd_soc_card *card,
976 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600977{
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200978 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000979 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600980
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000981 for_each_rtdcom(rtd, rtdcom) {
982 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600983
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200984 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca5642014-08-19 15:51:21 +0200985 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600986 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600987}
988
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900989static void soc_remove_dai_links(struct snd_soc_card *card)
990{
Mengdong Lin1a497982015-11-18 02:34:11 -0500991 int order;
992 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800993 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900994
Liam Girdwood0168bf02011-06-07 16:08:05 +0100995 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
996 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500997 list_for_each_entry(rtd, &card->rtd_list, list)
998 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +0100999 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001000
1001 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1002 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001003 list_for_each_entry(rtd, &card->rtd_list, list)
1004 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001005 }
1006
Mengdong Linf8f80362015-12-02 14:11:22 +08001007 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1008 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1009 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1010 link->name);
1011
1012 list_del(&link->list);
1013 card->num_dai_links--;
1014 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001015}
1016
Mengdong Lin923c5e612015-11-23 11:01:49 -05001017static int snd_soc_init_multicodec(struct snd_soc_card *card,
1018 struct snd_soc_dai_link *dai_link)
1019{
1020 /* Legacy codec/codec_dai link is a single entry in multicodec */
1021 if (dai_link->codec_name || dai_link->codec_of_node ||
1022 dai_link->codec_dai_name) {
1023 dai_link->num_codecs = 1;
1024
1025 dai_link->codecs = devm_kzalloc(card->dev,
1026 sizeof(struct snd_soc_dai_link_component),
1027 GFP_KERNEL);
1028 if (!dai_link->codecs)
1029 return -ENOMEM;
1030
1031 dai_link->codecs[0].name = dai_link->codec_name;
1032 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1033 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1034 }
1035
1036 if (!dai_link->codecs) {
1037 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1038 return -EINVAL;
1039 }
1040
1041 return 0;
1042}
1043
1044static int soc_init_dai_link(struct snd_soc_card *card,
1045 struct snd_soc_dai_link *link)
1046{
1047 int i, ret;
1048
1049 ret = snd_soc_init_multicodec(card, link);
1050 if (ret) {
1051 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1052 return ret;
1053 }
1054
1055 for (i = 0; i < link->num_codecs; i++) {
1056 /*
1057 * Codec must be specified by 1 of name or OF node,
1058 * not both or neither.
1059 */
1060 if (!!link->codecs[i].name ==
1061 !!link->codecs[i].of_node) {
1062 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1063 link->name);
1064 return -EINVAL;
1065 }
1066 /* Codec DAI name must be specified */
1067 if (!link->codecs[i].dai_name) {
1068 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1069 link->name);
1070 return -EINVAL;
1071 }
1072 }
1073
1074 /*
1075 * Platform may be specified by either name or OF node, but
1076 * can be left unspecified, and a dummy platform will be used.
1077 */
1078 if (link->platform_name && link->platform_of_node) {
1079 dev_err(card->dev,
1080 "ASoC: Both platform name/of_node are set for %s\n",
1081 link->name);
1082 return -EINVAL;
1083 }
1084
1085 /*
1086 * CPU device may be specified by either name or OF node, but
1087 * can be left unspecified, and will be matched based on DAI
1088 * name alone..
1089 */
1090 if (link->cpu_name && link->cpu_of_node) {
1091 dev_err(card->dev,
1092 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1093 link->name);
1094 return -EINVAL;
1095 }
1096 /*
1097 * At least one of CPU DAI name or CPU device name/node must be
1098 * specified
1099 */
1100 if (!link->cpu_dai_name &&
1101 !(link->cpu_name || link->cpu_of_node)) {
1102 dev_err(card->dev,
1103 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1104 link->name);
1105 return -EINVAL;
1106 }
1107
1108 return 0;
1109}
1110
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001111void snd_soc_disconnect_sync(struct device *dev)
1112{
1113 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1114
1115 if (!component || !component->card)
1116 return;
1117
1118 snd_card_disconnect_sync(component->card->snd_card);
1119}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001120EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001121
Mengdong Linf8f80362015-12-02 14:11:22 +08001122/**
1123 * snd_soc_add_dai_link - Add a DAI link dynamically
1124 * @card: The ASoC card to which the DAI link is added
1125 * @dai_link: The new DAI link to add
1126 *
1127 * This function adds a DAI link to the ASoC card's link list.
1128 *
1129 * Note: Topology can use this API to add DAI links when probing the
1130 * topology component. And machine drivers can still define static
1131 * DAI links in dai_link array.
1132 */
1133int snd_soc_add_dai_link(struct snd_soc_card *card,
1134 struct snd_soc_dai_link *dai_link)
1135{
1136 if (dai_link->dobj.type
1137 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1138 dev_err(card->dev, "Invalid dai link type %d\n",
1139 dai_link->dobj.type);
1140 return -EINVAL;
1141 }
1142
1143 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001144 /* Notify the machine driver for extra initialization
1145 * on the link created by topology.
1146 */
1147 if (dai_link->dobj.type && card->add_dai_link)
1148 card->add_dai_link(card, dai_link);
1149
Mengdong Linf8f80362015-12-02 14:11:22 +08001150 list_add_tail(&dai_link->list, &card->dai_link_list);
1151 card->num_dai_links++;
1152
1153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1156
1157/**
1158 * snd_soc_remove_dai_link - Remove a DAI link from the list
1159 * @card: The ASoC card that owns the link
1160 * @dai_link: The DAI link to remove
1161 *
1162 * This function removes a DAI link from the ASoC card's link list.
1163 *
1164 * For DAI links previously added by topology, topology should
1165 * remove them by using the dobj embedded in the link.
1166 */
1167void snd_soc_remove_dai_link(struct snd_soc_card *card,
1168 struct snd_soc_dai_link *dai_link)
1169{
1170 struct snd_soc_dai_link *link, *_link;
1171
1172 if (dai_link->dobj.type
1173 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1174 dev_err(card->dev, "Invalid dai link type %d\n",
1175 dai_link->dobj.type);
1176 return;
1177 }
1178
1179 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001180 /* Notify the machine driver for extra destruction
1181 * on the link created by topology.
1182 */
1183 if (dai_link->dobj.type && card->remove_dai_link)
1184 card->remove_dai_link(card, dai_link);
1185
Mengdong Linf8f80362015-12-02 14:11:22 +08001186 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1187 if (link == dai_link) {
1188 list_del(&link->list);
1189 card->num_dai_links--;
1190 return;
1191 }
1192 }
1193}
1194EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1195
Jerome Brunetaefba452018-07-13 14:50:43 +02001196static void soc_set_of_name_prefix(struct snd_soc_component *component)
1197{
1198 struct device_node *component_of_node = component->dev->of_node;
1199 const char *str;
1200 int ret;
1201
1202 if (!component_of_node && component->dev->parent)
1203 component_of_node = component->dev->parent->of_node;
1204
1205 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1206 &str);
1207 if (!ret)
1208 component->name_prefix = str;
1209}
1210
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001211static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001212 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001213{
1214 int i;
1215
Jerome Brunetaefba452018-07-13 14:50:43 +02001216 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001217 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001218 struct device_node *component_of_node = component->dev->of_node;
1219
1220 if (!component_of_node && component->dev->parent)
1221 component_of_node = component->dev->parent->of_node;
1222
1223 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001224 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001225 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001226 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001227 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001228 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001229 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001230
1231 /*
1232 * If there is no configuration table or no match in the table,
1233 * check if a prefix is provided in the node
1234 */
1235 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001236}
1237
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001238static int soc_probe_component(struct snd_soc_card *card,
1239 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001241 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001242 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001243 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001245 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001246 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001247
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001248 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001249 if (component->card != card) {
1250 dev_err(component->dev,
1251 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1252 card->name, component->card->name);
1253 return -ENODEV;
1254 }
1255 return 0;
1256 }
1257
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001258 if (!try_module_get(component->dev->driver->owner))
1259 return -ENODEV;
1260
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001261 component->card = card;
1262 dapm->card = card;
1263 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001264
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001265 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001266
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001267 if (component->driver->dapm_widgets) {
1268 ret = snd_soc_dapm_new_controls(dapm,
1269 component->driver->dapm_widgets,
1270 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001271
1272 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001273 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001274 "Failed to create new controls %d\n", ret);
1275 goto err_probe;
1276 }
1277 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001278
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001279 list_for_each_entry(dai, &component->dai_list, list) {
1280 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001281 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001282 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001283 "Failed to create DAI widgets %d\n", ret);
1284 goto err_probe;
1285 }
1286 }
Mark Brown888df392012-02-16 19:37:51 -08001287
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001288 if (component->driver->probe) {
1289 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001291 dev_err(component->dev,
1292 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001293 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001294 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001295
1296 WARN(dapm->idle_bias_off &&
1297 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001298 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001299 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001300 }
1301
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001302 /* machine specific init */
1303 if (component->init) {
1304 ret = component->init(component);
1305 if (ret < 0) {
1306 dev_err(component->dev,
1307 "Failed to do machine specific init %d\n", ret);
1308 goto err_probe;
1309 }
1310 }
1311
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001312 if (component->driver->controls)
1313 snd_soc_add_component_controls(component,
1314 component->driver->controls,
1315 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001316 if (component->driver->dapm_routes)
1317 snd_soc_dapm_add_routes(dapm,
1318 component->driver->dapm_routes,
1319 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001320
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001321 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001322 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001323
Jarkko Nikula70d293312011-01-27 16:24:22 +02001324 return 0;
1325
1326err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001327 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001328 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001329 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001330
1331 return ret;
1332}
1333
Mark Brown36ae1a92012-01-06 17:12:45 -08001334static void rtd_release(struct device *dev)
1335{
1336 kfree(dev);
1337}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001338
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001339static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1340 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001341{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001342 int ret = 0;
1343
Jarkko Nikula589c3562010-12-06 16:27:07 +02001344 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001345 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1346 if (!rtd->dev)
1347 return -ENOMEM;
1348 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001349 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001350 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001351 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001352 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001353 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001354 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001355 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1356 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1357 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1358 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001359 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001360 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001361 /* calling put_device() here to free the rtd->dev */
1362 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001363 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001364 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001365 return ret;
1366 }
1367 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001368 return 0;
1369}
1370
Mengdong Lin1a497982015-11-18 02:34:11 -05001371static int soc_probe_link_components(struct snd_soc_card *card,
1372 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001373 int order)
1374{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001375 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001376 struct snd_soc_rtdcom_list *rtdcom;
1377 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001378
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001379 for_each_rtdcom(rtd, rtdcom) {
1380 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001381
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001382 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001383 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001384 if (ret < 0)
1385 return ret;
1386 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001387 }
1388
Stephen Warren62ae68f2012-06-08 12:34:23 -06001389 return 0;
1390}
1391
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001392static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001393{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001394 if (dai->probed ||
1395 dai->driver->probe_order != order)
1396 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001397
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001398 if (dai->driver->probe) {
1399 int ret = dai->driver->probe(dai);
1400 if (ret < 0) {
1401 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1402 dai->name, ret);
1403 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001404 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001405 }
1406
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001407 dai->probed = 1;
1408
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001409 return 0;
1410}
1411
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001412static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1413 struct snd_soc_pcm_runtime *rtd)
1414{
1415 int i, ret = 0;
1416
1417 for (i = 0; i < num_dais; ++i) {
1418 struct snd_soc_dai_driver *drv = dais[i]->driver;
1419
1420 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1421 ret = drv->pcm_new(rtd, dais[i]);
1422 if (ret < 0) {
1423 dev_err(dais[i]->dev,
1424 "ASoC: Failed to bind %s with pcm device\n",
1425 dais[i]->name);
1426 return ret;
1427 }
1428 }
1429
1430 return 0;
1431}
1432
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001433static int soc_link_dai_widgets(struct snd_soc_card *card,
1434 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001435 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001436{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001437 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1438 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301439 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001440 int ret;
1441
Benoit Cousson88bd8702014-07-08 23:19:34 +02001442 if (rtd->num_codecs > 1)
1443 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1444
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001445 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301446 sink = codec_dai->playback_widget;
1447 source = cpu_dai->capture_widget;
1448 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001449 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301450 dai_link->num_params,
1451 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001452 if (ret != 0) {
1453 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301454 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001455 return ret;
1456 }
1457 }
1458
Vinod Koul3de7c422015-11-09 23:19:58 +05301459 sink = cpu_dai->playback_widget;
1460 source = codec_dai->capture_widget;
1461 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001462 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301463 dai_link->num_params,
1464 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001465 if (ret != 0) {
1466 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301467 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001468 return ret;
1469 }
1470 }
1471
1472 return 0;
1473}
1474
Mengdong Lin1a497982015-11-18 02:34:11 -05001475static int soc_probe_link_dais(struct snd_soc_card *card,
1476 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477{
Mengdong Lin1a497982015-11-18 02:34:11 -05001478 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001479 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001480 struct snd_soc_rtdcom_list *rtdcom;
1481 struct snd_soc_component *component;
1482 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001484 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001485 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 /* set default power off timeout */
1488 rtd->pmdown_time = pmdown_time;
1489
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001490 ret = soc_probe_dai(cpu_dai, order);
1491 if (ret)
1492 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001494 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001495 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001496 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001497 if (ret)
1498 return ret;
1499 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001500
Liam Girdwood0168bf02011-06-07 16:08:05 +01001501 /* complete DAI probe during last probe */
1502 if (order != SND_SOC_COMP_ORDER_LAST)
1503 return 0;
1504
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001505 /* do machine specific initialization */
1506 if (dai_link->init) {
1507 ret = dai_link->init(rtd);
1508 if (ret < 0) {
1509 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1510 dai_link->name, ret);
1511 return ret;
1512 }
1513 }
1514
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001515 if (dai_link->dai_fmt)
1516 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1517
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001518 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001519 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001521
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001522#ifdef CONFIG_DEBUG_FS
1523 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001524 if (dai_link->dynamic)
1525 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001526#endif
1527
Liam Girdwooda655de82018-07-02 16:59:54 +01001528 num = rtd->num;
1529
1530 /*
1531 * most drivers will register their PCMs using DAI link ordering but
1532 * topology based drivers can use the DAI link id field to set PCM
1533 * device number and then use rtd + a base offset of the BEs.
1534 */
1535 for_each_rtdcom(rtd, rtdcom) {
1536 component = rtdcom->component;
1537
1538 if (!component->driver->use_dai_pcm_id)
1539 continue;
1540
1541 if (rtd->dai_link->no_pcm)
1542 num += component->driver->be_pcm_base;
1543 else
1544 num = rtd->dai_link->id;
1545 }
1546
Jie Yang6f0c4222015-10-13 23:41:00 +08001547 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301548 /*create compress_device"*/
Liam Girdwooda655de82018-07-02 16:59:54 +01001549 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001550 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001551 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301552 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001553 return ret;
1554 }
1555 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301556
1557 if (!dai_link->params) {
1558 /* create the pcm */
Liam Girdwooda655de82018-07-02 16:59:54 +01001559 ret = soc_new_pcm(rtd, num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301560 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001561 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301562 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001563 return ret;
1564 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001565 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;
Namarta Kohli1245b702012-08-16 17:10:41 +05301572 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001573 INIT_DELAYED_WORK(&rtd->delayed_work,
1574 codec2codec_close_delayed_work);
1575
Namarta Kohli1245b702012-08-16 17:10:41 +05301576 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001577 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001578 if (ret)
1579 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001580 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 }
1582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583 return 0;
1584}
1585
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001586static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001587{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001588 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001589 struct snd_soc_component *component;
1590 const char *name;
1591 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001592
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001593 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1594 /* codecs, usually analog devices */
1595 name = aux_dev->codec_name;
1596 codec_of_node = aux_dev->codec_of_node;
1597 component = soc_find_component(codec_of_node, name);
1598 if (!component) {
1599 if (codec_of_node)
1600 name = of_node_full_name(codec_of_node);
1601 goto err_defer;
1602 }
1603 } else if (aux_dev->name) {
1604 /* generic components */
1605 name = aux_dev->name;
1606 component = soc_find_component(NULL, name);
1607 if (!component)
1608 goto err_defer;
1609 } else {
1610 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1611 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001612 }
1613
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001614 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001615 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001616
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001617 return 0;
1618
1619err_defer:
1620 dev_err(card->dev, "ASoC: %s not registered\n", name);
1621 return -EPROBE_DEFER;
1622}
1623
1624static int soc_probe_aux_devices(struct snd_soc_card *card)
1625{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001626 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001627 int order;
1628 int ret;
1629
1630 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1631 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001632 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001633 if (comp->driver->probe_order == order) {
1634 ret = soc_probe_component(card, comp);
1635 if (ret < 0) {
1636 dev_err(card->dev,
1637 "ASoC: failed to probe aux component %s %d\n",
1638 comp->name, ret);
1639 return ret;
1640 }
1641 }
1642 }
1643 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001644
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001645 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001646}
1647
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001648static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001649{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001650 struct snd_soc_component *comp, *_comp;
1651 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001652
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001653 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1654 order++) {
1655 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001656 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001657
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001658 if (comp->driver->remove_order == order) {
1659 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001660 /* remove it from the card's aux_comp_list */
1661 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001662 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001663 }
1664 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001665}
1666
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001667/**
1668 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1669 * @rtd: The runtime for which the DAI link format should be changed
1670 * @dai_fmt: The new DAI link format
1671 *
1672 * This function updates the DAI link format for all DAIs connected to the DAI
1673 * link for the specified runtime.
1674 *
1675 * Note: For setups with a static format set the dai_fmt field in the
1676 * corresponding snd_dai_link struct instead of using this function.
1677 *
1678 * Returns 0 on success, otherwise a negative error code.
1679 */
1680int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1681 unsigned int dai_fmt)
1682{
1683 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1685 unsigned int i;
1686 int ret;
1687
1688 for (i = 0; i < rtd->num_codecs; i++) {
1689 struct snd_soc_dai *codec_dai = codec_dais[i];
1690
1691 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1692 if (ret != 0 && ret != -ENOTSUPP) {
1693 dev_warn(codec_dai->dev,
1694 "ASoC: Failed to set DAI format: %d\n", ret);
1695 return ret;
1696 }
1697 }
1698
1699 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001700 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001701 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001702 unsigned int inv_dai_fmt;
1703
1704 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1705 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1706 case SND_SOC_DAIFMT_CBM_CFM:
1707 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1708 break;
1709 case SND_SOC_DAIFMT_CBM_CFS:
1710 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1711 break;
1712 case SND_SOC_DAIFMT_CBS_CFM:
1713 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1714 break;
1715 case SND_SOC_DAIFMT_CBS_CFS:
1716 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1717 break;
1718 }
1719
1720 dai_fmt = inv_dai_fmt;
1721 }
1722
1723 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1724 if (ret != 0 && ret != -ENOTSUPP) {
1725 dev_warn(cpu_dai->dev,
1726 "ASoC: Failed to set DAI format: %d\n", ret);
1727 return ret;
1728 }
1729
1730 return 0;
1731}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001732EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001733
Liam Girdwood345233d2017-01-14 16:13:02 +08001734
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001735#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001736/* Trim special characters, and replace '-' with '_' since '-' is used to
1737 * separate different DMI fields in the card long name. Only number and
1738 * alphabet characters and a few separator characters are kept.
1739 */
1740static void cleanup_dmi_name(char *name)
1741{
1742 int i, j = 0;
1743
1744 for (i = 0; name[i]; i++) {
1745 if (isalnum(name[i]) || (name[i] == '.')
1746 || (name[i] == '_'))
1747 name[j++] = name[i];
1748 else if (name[i] == '-')
1749 name[j++] = '_';
1750 }
1751
1752 name[j] = '\0';
1753}
1754
Mengdong Lin98faf432017-06-28 15:01:39 +08001755/* Check if a DMI field is valid, i.e. not containing any string
1756 * in the black list.
1757 */
1758static int is_dmi_valid(const char *field)
1759{
1760 int i = 0;
1761
1762 while (dmi_blacklist[i]) {
1763 if (strstr(field, dmi_blacklist[i]))
1764 return 0;
1765 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001766 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001767
1768 return 1;
1769}
1770
Liam Girdwood345233d2017-01-14 16:13:02 +08001771/**
1772 * snd_soc_set_dmi_name() - Register DMI names to card
1773 * @card: The card to register DMI names
1774 * @flavour: The flavour "differentiator" for the card amongst its peers.
1775 *
1776 * An Intel machine driver may be used by many different devices but are
1777 * difficult for userspace to differentiate, since machine drivers ususally
1778 * use their own name as the card short name and leave the card long name
1779 * blank. To differentiate such devices and fix bugs due to lack of
1780 * device-specific configurations, this function allows DMI info to be used
1781 * as the sound card long name, in the format of
1782 * "vendor-product-version-board"
1783 * (Character '-' is used to separate different DMI fields here).
1784 * This will help the user space to load the device-specific Use Case Manager
1785 * (UCM) configurations for the card.
1786 *
1787 * Possible card long names may be:
1788 * DellInc.-XPS139343-01-0310JH
1789 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1790 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1791 *
1792 * This function also supports flavoring the card longname to provide
1793 * the extra differentiation, like "vendor-product-version-board-flavor".
1794 *
1795 * We only keep number and alphabet characters and a few separator characters
1796 * in the card long name since UCM in the user space uses the card long names
1797 * as card configuration directory names and AudoConf cannot support special
1798 * charactors like SPACE.
1799 *
1800 * Returns 0 on success, otherwise a negative error code.
1801 */
1802int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1803{
1804 const char *vendor, *product, *product_version, *board;
1805 size_t longname_buf_size = sizeof(card->snd_card->longname);
1806 size_t len;
1807
1808 if (card->long_name)
1809 return 0; /* long name already set by driver or from DMI */
1810
1811 /* make up dmi long name as: vendor.product.version.board */
1812 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001813 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001814 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1815 return 0;
1816 }
1817
Mengdong Lin98faf432017-06-28 15:01:39 +08001818
Liam Girdwood345233d2017-01-14 16:13:02 +08001819 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1820 "%s", vendor);
1821 cleanup_dmi_name(card->dmi_longname);
1822
1823 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001824 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001825 len = strlen(card->dmi_longname);
1826 snprintf(card->dmi_longname + len,
1827 longname_buf_size - len,
1828 "-%s", product);
1829
1830 len++; /* skip the separator "-" */
1831 if (len < longname_buf_size)
1832 cleanup_dmi_name(card->dmi_longname + len);
1833
1834 /* some vendors like Lenovo may only put a self-explanatory
1835 * name in the product version field
1836 */
1837 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001838 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001839 len = strlen(card->dmi_longname);
1840 snprintf(card->dmi_longname + len,
1841 longname_buf_size - len,
1842 "-%s", product_version);
1843
1844 len++;
1845 if (len < longname_buf_size)
1846 cleanup_dmi_name(card->dmi_longname + len);
1847 }
1848 }
1849
1850 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001851 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001852 len = strlen(card->dmi_longname);
1853 snprintf(card->dmi_longname + len,
1854 longname_buf_size - len,
1855 "-%s", board);
1856
1857 len++;
1858 if (len < longname_buf_size)
1859 cleanup_dmi_name(card->dmi_longname + len);
1860 } else if (!product) {
1861 /* fall back to using legacy name */
1862 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1863 return 0;
1864 }
1865
1866 /* Add flavour to dmi long name */
1867 if (flavour) {
1868 len = strlen(card->dmi_longname);
1869 snprintf(card->dmi_longname + len,
1870 longname_buf_size - len,
1871 "-%s", flavour);
1872
1873 len++;
1874 if (len < longname_buf_size)
1875 cleanup_dmi_name(card->dmi_longname + len);
1876 }
1877
1878 /* set the card long name */
1879 card->long_name = card->dmi_longname;
1880
1881 return 0;
1882}
1883EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001884#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001885
Liam Girdwooda655de82018-07-02 16:59:54 +01001886static void soc_check_tplg_fes(struct snd_soc_card *card)
1887{
1888 struct snd_soc_component *component;
1889 const struct snd_soc_component_driver *comp_drv;
1890 struct snd_soc_dai_link *dai_link;
1891 int i;
1892
1893 list_for_each_entry(component, &component_list, list) {
1894
1895 /* does this component override FEs ? */
1896 if (!component->driver->ignore_machine)
1897 continue;
1898
1899 /* for this machine ? */
1900 if (strcmp(component->driver->ignore_machine,
1901 card->dev->driver->name))
1902 continue;
1903
1904 /* machine matches, so override the rtd data */
1905 for (i = 0; i < card->num_links; i++) {
1906
1907 dai_link = &card->dai_link[i];
1908
1909 /* ignore this FE */
1910 if (dai_link->dynamic) {
1911 dai_link->ignore = true;
1912 continue;
1913 }
1914
1915 dev_info(card->dev, "info: override FE DAI link %s\n",
1916 card->dai_link[i].name);
1917
1918 /* override platform component */
1919 dai_link->platform_name = component->name;
1920
1921 /* convert non BE into BE */
1922 dai_link->no_pcm = 1;
1923
1924 /* override any BE fixups */
1925 dai_link->be_hw_params_fixup =
1926 component->driver->be_hw_params_fixup;
1927
1928 /* most BE links don't set stream name, so set it to
1929 * dai link name if it's NULL to help bind widgets.
1930 */
1931 if (!dai_link->stream_name)
1932 dai_link->stream_name = dai_link->name;
1933 }
1934
1935 /* Inform userspace we are using alternate topology */
1936 if (component->driver->topology_name_prefix) {
1937
1938 /* topology shortname created ? */
1939 if (!card->topology_shortname_created) {
1940 comp_drv = component->driver;
1941
1942 snprintf(card->topology_shortname, 32, "%s-%s",
1943 comp_drv->topology_name_prefix,
1944 card->name);
1945 card->topology_shortname_created = true;
1946 }
1947
1948 /* use topology shortname */
1949 card->name = card->topology_shortname;
1950 }
1951 }
1952}
1953
Mark Brownb19e6e72012-03-14 21:18:39 +00001954static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001955{
Mengdong Lin1a497982015-11-18 02:34:11 -05001956 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001957 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001958 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001959
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001960 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001961 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001962
Liam Girdwooda655de82018-07-02 16:59:54 +01001963 /* check whether any platform is ignore machine FE and using topology */
1964 soc_check_tplg_fes(card);
1965
Mark Brownb19e6e72012-03-14 21:18:39 +00001966 /* bind DAIs */
1967 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001968 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00001969 if (ret != 0)
1970 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 }
1972
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001973 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001974 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001975 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001976 if (ret != 0)
1977 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978 }
1979
Mengdong Linf8f80362015-12-02 14:11:22 +08001980 /* add predefined DAI links to the list */
1981 for (i = 0; i < card->num_links; i++)
1982 snd_soc_add_dai_link(card, card->dai_link+i);
1983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001985 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986 card->owner, 0, &card->snd_card);
1987 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001988 dev_err(card->dev,
1989 "ASoC: can't create sound card for card %s: %d\n",
1990 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001991 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001993
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001994 soc_init_card_debugfs(card);
1995
Mark Browne37a4972011-03-02 18:21:57 +00001996 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
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002001#ifdef CONFIG_DEBUG_FS
2002 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2003#endif
2004
Mark Brown88ee1c62011-02-02 10:43:26 +00002005#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002006 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002007 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002008#endif
Andy Green6ed25972008-06-13 16:24:05 +01002009
Mark Brown9a841eb2011-04-12 17:51:37 -07002010 if (card->dapm_widgets)
2011 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2012 card->num_dapm_widgets);
2013
Nicolin Chenf23e8602015-02-14 17:22:49 -08002014 if (card->of_dapm_widgets)
2015 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2016 card->num_of_dapm_widgets);
2017
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002018 /* initialise the sound card only once */
2019 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002020 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002021 if (ret < 0)
2022 goto card_probe_error;
2023 }
2024
Stephen Warren62ae68f2012-06-08 12:34:23 -06002025 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002026 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2027 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002028 list_for_each_entry(rtd, &card->rtd_list, list) {
2029 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002030 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002031 dev_err(card->dev,
2032 "ASoC: failed to instantiate card %d\n",
2033 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002034 goto probe_dai_err;
2035 }
2036 }
2037 }
2038
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002039 /* probe auxiliary components */
2040 ret = soc_probe_aux_devices(card);
2041 if (ret < 0)
2042 goto probe_dai_err;
2043
Mengdong Lin61b00882015-12-02 14:11:48 +08002044 /* Find new DAI links added during probing components and bind them.
2045 * Components with topology may bring new DAIs and DAI links.
2046 */
2047 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2048 if (soc_is_dai_link_bound(card, dai_link))
2049 continue;
2050
2051 ret = soc_init_dai_link(card, dai_link);
2052 if (ret)
2053 goto probe_dai_err;
2054 ret = soc_bind_dai_link(card, dai_link);
2055 if (ret)
2056 goto probe_dai_err;
2057 }
2058
Stephen Warren62ae68f2012-06-08 12:34:23 -06002059 /* probe all DAI links on this card */
2060 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2061 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002062 list_for_each_entry(rtd, &card->rtd_list, list) {
2063 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002064 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002065 dev_err(card->dev,
2066 "ASoC: failed to instantiate card %d\n",
2067 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002068 goto probe_dai_err;
2069 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 }
2071 }
2072
Mark Brown888df392012-02-16 19:37:51 -08002073 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002074 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002075
Mark Brownb7af1da2011-04-07 19:18:44 +09002076 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002077 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002078
Mark Brownb8ad29d2011-03-02 18:35:51 +00002079 if (card->dapm_routes)
2080 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2081 card->num_dapm_routes);
2082
Nicolin Chenf23e8602015-02-14 17:22:49 -08002083 if (card->of_dapm_routes)
2084 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2085 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002086
Takashi Iwai861886d2017-04-24 08:54:42 +02002087 /* try to set some sane longname if DMI is available */
2088 snd_soc_set_dmi_name(card, NULL);
2089
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002090 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002091 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002092 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2093 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002094 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2095 "%s", card->driver_name ? card->driver_name : card->name);
2096 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2097 switch (card->snd_card->driver[i]) {
2098 case '_':
2099 case '-':
2100 case '\0':
2101 break;
2102 default:
2103 if (!isalnum(card->snd_card->driver[i]))
2104 card->snd_card->driver[i] = '_';
2105 break;
2106 }
2107 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002108
Mark Brown28e9ad92011-03-02 18:36:34 +00002109 if (card->late_probe) {
2110 ret = card->late_probe(card);
2111 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002112 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002113 card->name, ret);
2114 goto probe_aux_dev_err;
2115 }
2116 }
2117
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002118 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002120 ret = snd_card_register(card->snd_card);
2121 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002122 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2123 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002124 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002125 }
2126
Mark Brown435c5e22008-12-04 15:32:53 +00002127 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002128 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002129 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002130 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002131
2132 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002134probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002135 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002137probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002138 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002140card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002141 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002142 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002143
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002144 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002145 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002146 snd_card_free(card->snd_card);
2147
Mark Brownb19e6e72012-03-14 21:18:39 +00002148base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002149 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002150 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002151 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002152
Mark Brownb19e6e72012-03-14 21:18:39 +00002153 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002154}
2155
2156/* probes a new socdev */
2157static int soc_probe(struct platform_device *pdev)
2158{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002159 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002160
Vinod Koul70a7ca32011-01-14 19:22:48 +05302161 /*
2162 * no card, so machine driver should be registering card
2163 * we should not be here in that case so ret error
2164 */
2165 if (!card)
2166 return -EINVAL;
2167
Mark Brownfe4085e2012-03-02 13:07:41 +00002168 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002169 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002170 card->name);
2171
Mark Brown435c5e22008-12-04 15:32:53 +00002172 /* Bodge while we unpick instantiation */
2173 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002174
Mark Brown28d528c2012-08-09 18:45:23 +01002175 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176}
2177
Vinod Koulb0e26482011-01-13 22:48:02 +05302178static int soc_cleanup_card_resources(struct snd_soc_card *card)
2179{
Mengdong Lin1a497982015-11-18 02:34:11 -05002180 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302181
2182 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002183 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002184 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302185
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002186 /* free the ALSA card at first; this syncs with pending operations */
2187 snd_card_free(card->snd_card);
2188
Vinod Koulb0e26482011-01-13 22:48:02 +05302189 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002190 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002191 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302192
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002193 /* remove auxiliary devices */
2194 soc_remove_aux_devices(card);
2195
Mark Brownd1e81422016-08-18 19:32:59 +01002196 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302197 soc_cleanup_card_debugfs(card);
2198
2199 /* remove the card */
2200 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002201 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302202
Vinod Koulb0e26482011-01-13 22:48:02 +05302203 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302204}
2205
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206/* removes a socdev */
2207static int soc_remove(struct platform_device *pdev)
2208{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002209 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002210
Mark Brownc5af3a22008-11-28 13:29:45 +00002211 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002212 return 0;
2213}
2214
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002215int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002216{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002217 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002218 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002219
2220 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002221 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002222
2223 /* Flush out pmdown_time work - we actually do want to run it
2224 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002225 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002226 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002229
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002230 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002231 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002232 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002233 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002234
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002235 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002236 for (i = 0; i < rtd->num_codecs; i++) {
2237 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002238 pinctrl_pm_select_sleep_state(codec_dai->dev);
2239 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002240 }
2241
Mark Brown416356f2009-06-30 19:05:15 +01002242 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002243}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002244EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002245
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002246const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302247 .suspend = snd_soc_suspend,
2248 .resume = snd_soc_resume,
2249 .freeze = snd_soc_suspend,
2250 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002251 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302252 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002253};
Stephen Warrendeb26072011-04-05 19:35:30 -06002254EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002255
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256/* ASoC platform driver */
2257static struct platform_driver soc_driver = {
2258 .driver = {
2259 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002260 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 },
2262 .probe = soc_probe,
2263 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264};
2265
Mark Brown096e49d2009-07-05 15:12:22 +01002266/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 * snd_soc_cnew - create new control
2268 * @_template: control template
2269 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002270 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002271 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272 *
2273 * Create a new mixer control from a template control.
2274 *
2275 * Returns 0 for success, else error.
2276 */
2277struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002278 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002279 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280{
2281 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002282 struct snd_kcontrol *kcontrol;
2283 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284
2285 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 template.index = 0;
2287
Mark Brownefb7ac32011-03-08 17:23:24 +00002288 if (!long_name)
2289 long_name = template.name;
2290
2291 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002292 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002293 if (!name)
2294 return NULL;
2295
Mark Brownefb7ac32011-03-08 17:23:24 +00002296 template.name = name;
2297 } else {
2298 template.name = long_name;
2299 }
2300
2301 kcontrol = snd_ctl_new1(&template, data);
2302
2303 kfree(name);
2304
2305 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306}
2307EXPORT_SYMBOL_GPL(snd_soc_cnew);
2308
Liam Girdwood022658b2012-02-03 17:43:09 +00002309static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2310 const struct snd_kcontrol_new *controls, int num_controls,
2311 const char *prefix, void *data)
2312{
2313 int err, i;
2314
2315 for (i = 0; i < num_controls; i++) {
2316 const struct snd_kcontrol_new *control = &controls[i];
2317 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2318 control->name, prefix));
2319 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002320 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2321 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002322 return err;
2323 }
2324 }
2325
2326 return 0;
2327}
2328
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002329struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2330 const char *name)
2331{
2332 struct snd_card *card = soc_card->snd_card;
2333 struct snd_kcontrol *kctl;
2334
2335 if (unlikely(!name))
2336 return NULL;
2337
2338 list_for_each_entry(kctl, &card->controls, list)
2339 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2340 return kctl;
2341 return NULL;
2342}
2343EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2344
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002346 * snd_soc_add_component_controls - Add an array of controls to a component.
2347 *
2348 * @component: Component to add controls to
2349 * @controls: Array of controls to add
2350 * @num_controls: Number of elements in the array
2351 *
2352 * Return: 0 for success, else error.
2353 */
2354int snd_soc_add_component_controls(struct snd_soc_component *component,
2355 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2356{
2357 struct snd_card *card = component->card->snd_card;
2358
2359 return snd_soc_add_controls(card, component->dev, controls,
2360 num_controls, component->name_prefix, component);
2361}
2362EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2363
2364/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002365 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2366 * Convenience function to add a list of controls.
2367 *
2368 * @soc_card: SoC card to add controls to
2369 * @controls: array of controls to add
2370 * @num_controls: number of elements in the array
2371 *
2372 * Return 0 for success, else error.
2373 */
2374int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2375 const struct snd_kcontrol_new *controls, int num_controls)
2376{
2377 struct snd_card *card = soc_card->snd_card;
2378
2379 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2380 NULL, soc_card);
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2383
2384/**
2385 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2386 * Convienience function to add a list of controls.
2387 *
2388 * @dai: DAI to add controls to
2389 * @controls: array of controls to add
2390 * @num_controls: number of elements in the array
2391 *
2392 * Return 0 for success, else error.
2393 */
2394int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2395 const struct snd_kcontrol_new *controls, int num_controls)
2396{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002397 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002398
2399 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2400 NULL, dai);
2401}
2402EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2403
2404/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002405 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2406 * @dai: DAI
2407 * @clk_id: DAI specific clock ID
2408 * @freq: new clock frequency in Hz
2409 * @dir: new clock direction - input/output.
2410 *
2411 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2412 */
2413int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2414 unsigned int freq, int dir)
2415{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002416 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002417 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002418
2419 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2420 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002421}
2422EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2423
2424/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002425 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2426 * @component: COMPONENT
2427 * @clk_id: DAI specific clock ID
2428 * @source: Source for the clock
2429 * @freq: new clock frequency in Hz
2430 * @dir: new clock direction - input/output.
2431 *
2432 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2433 */
2434int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2435 int source, unsigned int freq, int dir)
2436{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002437 if (component->driver->set_sysclk)
2438 return component->driver->set_sysclk(component, clk_id, source,
2439 freq, dir);
2440
2441 return -ENOTSUPP;
2442}
2443EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2444
2445/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002446 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2447 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002448 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002449 * @div: new clock divisor.
2450 *
2451 * Configures the clock dividers. This is used to derive the best DAI bit and
2452 * frame clocks from the system or master clock. It's best to set the DAI bit
2453 * and frame clocks as low as possible to save system power.
2454 */
2455int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2456 int div_id, int div)
2457{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002458 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002459 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002460 else
2461 return -EINVAL;
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2464
2465/**
2466 * snd_soc_dai_set_pll - configure DAI PLL.
2467 * @dai: DAI
2468 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002469 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002470 * @freq_in: PLL input clock frequency in Hz
2471 * @freq_out: requested PLL output clock frequency in Hz
2472 *
2473 * Configures and enables PLL to generate output clock based on input clock.
2474 */
Mark Brown85488032009-09-05 18:52:16 +01002475int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2476 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002477{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002478 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002479 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002480 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002481
2482 return snd_soc_component_set_pll(dai->component, pll_id, source,
2483 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002484}
2485EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2486
Mark Brownec4ee522011-03-07 20:58:11 +00002487/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002488 * snd_soc_component_set_pll - configure component PLL.
2489 * @component: COMPONENT
2490 * @pll_id: DAI specific PLL ID
2491 * @source: DAI specific source for the PLL
2492 * @freq_in: PLL input clock frequency in Hz
2493 * @freq_out: requested PLL output clock frequency in Hz
2494 *
2495 * Configures and enables PLL to generate output clock based on input clock.
2496 */
2497int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2498 int source, unsigned int freq_in,
2499 unsigned int freq_out)
2500{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002501 if (component->driver->set_pll)
2502 return component->driver->set_pll(component, pll_id, source,
2503 freq_in, freq_out);
2504
2505 return -EINVAL;
2506}
2507EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2508
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002509/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002510 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2511 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002512 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002513 *
2514 * Configures the DAI for a preset BCLK to sample rate ratio.
2515 */
2516int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2517{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002518 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002519 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2520 else
2521 return -EINVAL;
2522}
2523EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2524
2525/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002526 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2527 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002528 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002529 *
2530 * Configures the DAI hardware format and clocking.
2531 */
2532int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2533{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002534 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002535 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002536 if (dai->driver->ops->set_fmt == NULL)
2537 return -ENOTSUPP;
2538 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002539}
2540EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2541
2542/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002543 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002544 * @slots: Number of slots in use.
2545 * @tx_mask: bitmask representing active TX slots.
2546 * @rx_mask: bitmask representing active RX slots.
2547 *
2548 * Generates the TDM tx and rx slot default masks for DAI.
2549 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002550static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002551 unsigned int *tx_mask,
2552 unsigned int *rx_mask)
2553{
2554 if (*tx_mask || *rx_mask)
2555 return 0;
2556
2557 if (!slots)
2558 return -EINVAL;
2559
2560 *tx_mask = (1 << slots) - 1;
2561 *rx_mask = (1 << slots) - 1;
2562
2563 return 0;
2564}
2565
2566/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002567 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2568 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002569 * @tx_mask: bitmask representing active TX slots.
2570 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002571 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002572 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002573 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002574 * This function configures the specified DAI for TDM operation. @slot contains
2575 * the total number of slots of the TDM stream and @slot_with the width of each
2576 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2577 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2578 * DAI should write to or read from. If a bit is set the corresponding slot is
2579 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2580 * the first slot, bit 1 to the second slot and so on. The first active slot
2581 * maps to the first channel of the DAI, the second active slot to the second
2582 * channel and so on.
2583 *
2584 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2585 * @rx_mask and @slot_width will be ignored.
2586 *
2587 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002588 */
2589int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002590 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002591{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002592 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002593 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002594 &tx_mask, &rx_mask);
2595 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002596 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002597
Benoit Cousson88bd8702014-07-08 23:19:34 +02002598 dai->tx_mask = tx_mask;
2599 dai->rx_mask = rx_mask;
2600
Kuninori Morimoto46471922017-09-25 01:38:34 +00002601 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002602 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002603 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002604 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002605 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002606}
2607EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2608
2609/**
Barry Song472df3c2009-09-12 01:16:29 +08002610 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2611 * @dai: DAI
2612 * @tx_num: how many TX channels
2613 * @tx_slot: pointer to an array which imply the TX slot number channel
2614 * 0~num-1 uses
2615 * @rx_num: how many RX channels
2616 * @rx_slot: pointer to an array which imply the RX slot number channel
2617 * 0~num-1 uses
2618 *
2619 * configure the relationship between channel number and TDM slot number.
2620 */
2621int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2622 unsigned int tx_num, unsigned int *tx_slot,
2623 unsigned int rx_num, unsigned int *rx_slot)
2624{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002625 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002626 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002627 rx_num, rx_slot);
2628 else
2629 return -EINVAL;
2630}
2631EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2632
2633/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002634 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2635 * @dai: DAI
2636 * @tristate: tristate enable
2637 *
2638 * Tristates the DAI so that others can use it.
2639 */
2640int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2641{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002642 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002643 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002644 else
2645 return -EINVAL;
2646}
2647EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2648
2649/**
2650 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2651 * @dai: DAI
2652 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002653 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002654 *
2655 * Mutes the DAI DAC.
2656 */
Mark Brownda183962013-02-06 15:44:07 +00002657int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2658 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002659{
Mark Brownda183962013-02-06 15:44:07 +00002660 if (!dai->driver)
2661 return -ENOTSUPP;
2662
2663 if (dai->driver->ops->mute_stream)
2664 return dai->driver->ops->mute_stream(dai, mute, direction);
2665 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2666 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002667 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002668 else
Mark Brown04570c62012-04-13 19:16:03 +01002669 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002670}
2671EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2672
Mark Brownc5af3a22008-11-28 13:29:45 +00002673/**
2674 * snd_soc_register_card - Register a card with the ASoC core
2675 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002676 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002677 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002678 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302679int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002680{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002681 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002682 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002683
Mark Brownc5af3a22008-11-28 13:29:45 +00002684 if (!card->name || !card->dev)
2685 return -EINVAL;
2686
Stephen Warren5a504962011-12-21 10:40:59 -07002687 for (i = 0; i < card->num_links; i++) {
2688 struct snd_soc_dai_link *link = &card->dai_link[i];
2689
Mengdong Lin923c5e612015-11-23 11:01:49 -05002690 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002691 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002692 dev_err(card->dev, "ASoC: failed to init link %s\n",
2693 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002694 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002695 }
Stephen Warren5a504962011-12-21 10:40:59 -07002696 }
2697
Mark Browned77cc12011-05-03 18:25:34 +01002698 dev_set_drvdata(card->dev, card);
2699
Stephen Warren111c6412011-01-28 14:26:35 -07002700 snd_soc_initialize_card_lists(card);
2701
Mengdong Linf8f80362015-12-02 14:11:22 +08002702 INIT_LIST_HEAD(&card->dai_link_list);
2703 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002704
Mengdong Lin1a497982015-11-18 02:34:11 -05002705 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002706 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002707
Mark Browndb432b42011-10-03 21:06:40 +01002708 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002709 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710 card->instantiated = 0;
2711 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002712 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002713
Mark Brownb19e6e72012-03-14 21:18:39 +00002714 ret = snd_soc_instantiate_card(card);
2715 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002716 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002718 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002719 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2721 int j;
2722
2723 for (j = 0; j < rtd->num_codecs; j++) {
2724 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2725 if (!codec_dai->active)
2726 pinctrl_pm_select_sleep_state(codec_dai->dev);
2727 }
2728
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002729 if (!cpu_dai->active)
2730 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2731 }
2732
Mark Brownb19e6e72012-03-14 21:18:39 +00002733 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302735EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736
2737/**
2738 * snd_soc_unregister_card - Unregister a card with the ASoC core
2739 *
2740 * @card: Card to unregister
2741 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002742 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302743int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002744{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002745 if (card->instantiated) {
2746 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002747 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302748 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002749 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002750 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002751
2752 return 0;
2753}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302754EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002755
2756/*
2757 * Simplify DAI link configuration by removing ".-1" from device names
2758 * and sanitizing names.
2759 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002760static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002761{
2762 char *found, name[NAME_SIZE];
2763 int id1, id2;
2764
2765 if (dev_name(dev) == NULL)
2766 return NULL;
2767
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002768 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002769
2770 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002771 found = strstr(name, dev->driver->name);
2772 if (found) {
2773 /* get ID */
2774 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2775
2776 /* discard ID from name if ID == -1 */
2777 if (*id == -1)
2778 found[strlen(dev->driver->name)] = '\0';
2779 }
2780
2781 } else {
2782 /* I2C component devices are named "bus-addr" */
2783 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2784 char tmp[NAME_SIZE];
2785
2786 /* create unique ID number from I2C addr and bus */
2787 *id = ((id1 & 0xffff) << 16) + id2;
2788
2789 /* sanitize component name for DAI link creation */
2790 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002791 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002792 } else
2793 *id = 0;
2794 }
2795
2796 return kstrdup(name, GFP_KERNEL);
2797}
2798
2799/*
2800 * Simplify DAI link naming for single devices with multiple DAIs by removing
2801 * any ".-1" and using the DAI name (instead of device name).
2802 */
2803static inline char *fmt_multiple_name(struct device *dev,
2804 struct snd_soc_dai_driver *dai_drv)
2805{
2806 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002807 dev_err(dev,
2808 "ASoC: error - multiple DAI %s registered with no name\n",
2809 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002810 return NULL;
2811 }
2812
2813 return kstrdup(dai_drv->name, GFP_KERNEL);
2814}
2815
2816/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002817 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002818 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002819 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002820 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002821static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002822{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002823 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002824
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002825 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002826 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2827 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002828 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002829 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002830 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002831 }
Mark Brown91151712008-11-30 23:31:24 +00002832}
Mark Brown91151712008-11-30 23:31:24 +00002833
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002834/* Create a DAI and add it to the component's DAI list */
2835static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2836 struct snd_soc_dai_driver *dai_drv,
2837 bool legacy_dai_naming)
2838{
2839 struct device *dev = component->dev;
2840 struct snd_soc_dai *dai;
2841
2842 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2843
2844 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2845 if (dai == NULL)
2846 return NULL;
2847
2848 /*
2849 * Back in the old days when we still had component-less DAIs,
2850 * instead of having a static name, component-less DAIs would
2851 * inherit the name of the parent device so it is possible to
2852 * register multiple instances of the DAI. We still need to keep
2853 * the same naming style even though those DAIs are not
2854 * component-less anymore.
2855 */
2856 if (legacy_dai_naming &&
2857 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2858 dai->name = fmt_single_name(dev, &dai->id);
2859 } else {
2860 dai->name = fmt_multiple_name(dev, dai_drv);
2861 if (dai_drv->id)
2862 dai->id = dai_drv->id;
2863 else
2864 dai->id = component->num_dai;
2865 }
2866 if (dai->name == NULL) {
2867 kfree(dai);
2868 return NULL;
2869 }
2870
2871 dai->component = component;
2872 dai->dev = dev;
2873 dai->driver = dai_drv;
2874 if (!dai->driver->ops)
2875 dai->driver->ops = &null_dai_ops;
2876
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002877 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002878 component->num_dai++;
2879
2880 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2881 return dai;
2882}
2883
Mark Brown91151712008-11-30 23:31:24 +00002884/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002885 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002886 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002887 * @component: The component the DAIs are registered for
2888 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002889 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002890 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2891 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002892 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002893static int snd_soc_register_dais(struct snd_soc_component *component,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002894 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002895{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002896 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002897 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002898 unsigned int i;
2899 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002900
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002901 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002902
2903 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002904
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002905 dai = soc_add_dai(component, dai_drv + i,
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00002906 count == 1 && !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002907 if (dai == NULL) {
2908 ret = -ENOMEM;
2909 goto err;
2910 }
Mark Brown91151712008-11-30 23:31:24 +00002911 }
2912
2913 return 0;
2914
2915err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002916 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002917
2918 return ret;
2919}
Mark Brown91151712008-11-30 23:31:24 +00002920
Mengdong Lin68003e62015-12-31 16:40:43 +08002921/**
2922 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2923 *
2924 * @component: The component the DAIs are registered for
2925 * @dai_drv: DAI driver to use for the DAI
2926 *
2927 * Topology can use this API to register DAIs when probing a component.
2928 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2929 * will be freed in the component cleanup.
2930 */
2931int snd_soc_register_dai(struct snd_soc_component *component,
2932 struct snd_soc_dai_driver *dai_drv)
2933{
2934 struct snd_soc_dapm_context *dapm =
2935 snd_soc_component_get_dapm(component);
2936 struct snd_soc_dai *dai;
2937 int ret;
2938
2939 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2940 dev_err(component->dev, "Invalid dai type %d\n",
2941 dai_drv->dobj.type);
2942 return -EINVAL;
2943 }
2944
2945 lockdep_assert_held(&client_mutex);
2946 dai = soc_add_dai(component, dai_drv, false);
2947 if (!dai)
2948 return -ENOMEM;
2949
2950 /* Create the DAI widgets here. After adding DAIs, topology may
2951 * also add routes that need these widgets as source or sink.
2952 */
2953 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2954 if (ret != 0) {
2955 dev_err(component->dev,
2956 "Failed to create DAI widgets %d\n", ret);
2957 }
2958
2959 return ret;
2960}
2961EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2962
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002963static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2964 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002965{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002966 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002967
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002968 component->driver->seq_notifier(component, type, subseq);
2969}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002970
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002971static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2972 int event)
2973{
2974 struct snd_soc_component *component = dapm->component;
2975
2976 return component->driver->stream_event(component, event);
2977}
2978
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002979static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2980 enum snd_soc_bias_level level)
2981{
2982 struct snd_soc_component *component = dapm->component;
2983
2984 return component->driver->set_bias_level(component, level);
2985}
2986
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002987static int snd_soc_component_initialize(struct snd_soc_component *component,
2988 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002989{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002990 struct snd_soc_dapm_context *dapm;
2991
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002992 component->name = fmt_single_name(dev, &component->id);
2993 if (!component->name) {
2994 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002995 return -ENOMEM;
2996 }
2997
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002998 component->dev = dev;
2999 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003000
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003001 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003002 dapm->dev = dev;
3003 dapm->component = component;
3004 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003005 dapm->idle_bias_off = !driver->idle_bias_on;
3006 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003007 if (driver->seq_notifier)
3008 dapm->seq_notifier = snd_soc_component_seq_notifier;
3009 if (driver->stream_event)
3010 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003011 if (driver->set_bias_level)
3012 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003013
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003014 INIT_LIST_HEAD(&component->dai_list);
3015 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003016
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003017 return 0;
3018}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003019
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003020static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003021{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003022 int val_bytes = regmap_get_val_bytes(component->regmap);
3023
3024 /* Errors are legitimate for non-integer byte multiples */
3025 if (val_bytes > 0)
3026 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003027}
3028
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003029#ifdef CONFIG_REGMAP
3030
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003031/**
3032 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3033 * @component: The component for which to initialize the regmap instance
3034 * @regmap: The regmap instance that should be used by the component
3035 *
3036 * This function allows deferred assignment of the regmap instance that is
3037 * associated with the component. Only use this if the regmap instance is not
3038 * yet ready when the component is registered. The function must also be called
3039 * before the first IO attempt of the component.
3040 */
3041void snd_soc_component_init_regmap(struct snd_soc_component *component,
3042 struct regmap *regmap)
3043{
3044 component->regmap = regmap;
3045 snd_soc_component_setup_regmap(component);
3046}
3047EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3048
3049/**
3050 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3051 * @component: The component for which to de-initialize the regmap instance
3052 *
3053 * Calls regmap_exit() on the regmap instance associated to the component and
3054 * removes the regmap instance from the component.
3055 *
3056 * This function should only be used if snd_soc_component_init_regmap() was used
3057 * to initialize the regmap instance.
3058 */
3059void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3060{
3061 regmap_exit(component->regmap);
3062 component->regmap = NULL;
3063}
3064EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3065
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003066#endif
3067
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003068static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003069{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003070 mutex_lock(&client_mutex);
3071
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003072 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003073 if (!component->regmap)
3074 component->regmap = dev_get_regmap(component->dev, NULL);
3075 if (component->regmap)
3076 snd_soc_component_setup_regmap(component);
3077 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003078
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003079 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003080 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003081
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003082 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003083}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003084
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003085static void snd_soc_component_cleanup(struct snd_soc_component *component)
3086{
3087 snd_soc_unregister_dais(component);
3088 kfree(component->name);
3089}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003090
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003091static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3092{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003093 struct snd_soc_card *card = component->card;
3094
3095 if (card)
3096 snd_soc_unregister_card(card);
3097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003098 list_del(&component->list);
3099}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003100
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003101#define ENDIANNESS_MAP(name) \
3102 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3103static u64 endianness_format_map[] = {
3104 ENDIANNESS_MAP(S16_),
3105 ENDIANNESS_MAP(U16_),
3106 ENDIANNESS_MAP(S24_),
3107 ENDIANNESS_MAP(U24_),
3108 ENDIANNESS_MAP(S32_),
3109 ENDIANNESS_MAP(U32_),
3110 ENDIANNESS_MAP(S24_3),
3111 ENDIANNESS_MAP(U24_3),
3112 ENDIANNESS_MAP(S20_3),
3113 ENDIANNESS_MAP(U20_3),
3114 ENDIANNESS_MAP(S18_3),
3115 ENDIANNESS_MAP(U18_3),
3116 ENDIANNESS_MAP(FLOAT_),
3117 ENDIANNESS_MAP(FLOAT64_),
3118 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3119};
3120
3121/*
3122 * Fix up the DAI formats for endianness: codecs don't actually see
3123 * the endianness of the data but we're using the CPU format
3124 * definitions which do need to include endianness so we ensure that
3125 * codec DAIs always have both big and little endian variants set.
3126 */
3127static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3128{
3129 int i;
3130
3131 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3132 if (stream->formats & endianness_format_map[i])
3133 stream->formats |= endianness_format_map[i];
3134}
3135
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003136int snd_soc_add_component(struct device *dev,
3137 struct snd_soc_component *component,
3138 const struct snd_soc_component_driver *component_driver,
3139 struct snd_soc_dai_driver *dai_drv,
3140 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003141{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003142 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003143 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003144
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003145 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003146 if (ret)
3147 goto err_free;
3148
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003149 if (component_driver->endianness) {
3150 for (i = 0; i < num_dai; i++) {
3151 convert_endianness_formats(&dai_drv[i].playback);
3152 convert_endianness_formats(&dai_drv[i].capture);
3153 }
3154 }
3155
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003156 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003157 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003158 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003159 goto err_cleanup;
3160 }
3161
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003162 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003163
3164 return 0;
3165
3166err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003167 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003168err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003169 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003170}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003171EXPORT_SYMBOL_GPL(snd_soc_add_component);
3172
3173int snd_soc_register_component(struct device *dev,
3174 const struct snd_soc_component_driver *component_driver,
3175 struct snd_soc_dai_driver *dai_drv,
3176 int num_dai)
3177{
3178 struct snd_soc_component *component;
3179
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003180 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003181 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003182 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003183
3184 return snd_soc_add_component(dev, component, component_driver,
3185 dai_drv, num_dai);
3186}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003187EXPORT_SYMBOL_GPL(snd_soc_register_component);
3188
3189/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003190 * snd_soc_unregister_component - Unregister all related component
3191 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003192 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003193 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003194 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003195static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003196{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003197 struct snd_soc_component *component;
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003198 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003199
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003200 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003201 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003202 if (dev != component->dev)
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003203 continue;
3204
3205 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3206 snd_soc_component_del_unlocked(component);
3207 found = 1;
3208 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003209 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003210 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003211
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003212 if (found) {
3213 snd_soc_component_cleanup(component);
Kuninori Morimoto21a03522017-08-07 02:06:40 +00003214 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003215
3216 return found;
3217}
3218
3219void snd_soc_unregister_component(struct device *dev)
3220{
3221 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003222}
3223EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3224
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003225struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3226 const char *driver_name)
3227{
3228 struct snd_soc_component *component;
3229 struct snd_soc_component *ret;
3230
3231 ret = NULL;
3232 mutex_lock(&client_mutex);
3233 list_for_each_entry(component, &component_list, list) {
3234 if (dev != component->dev)
3235 continue;
3236
3237 if (driver_name &&
3238 (driver_name != component->driver->name) &&
3239 (strcmp(component->driver->name, driver_name) != 0))
3240 continue;
3241
3242 ret = component;
3243 break;
3244 }
3245 mutex_unlock(&client_mutex);
3246
3247 return ret;
3248}
3249EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3250
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003251/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003252int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3253 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003254{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003255 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003256 int ret;
3257
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303258 if (!card->dev) {
3259 pr_err("card->dev is not set before calling %s\n", __func__);
3260 return -EINVAL;
3261 }
3262
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003263 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303264
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003265 ret = of_property_read_string_index(np, propname, 0, &card->name);
3266 /*
3267 * EINVAL means the property does not exist. This is fine providing
3268 * card->name was previously set, which is checked later in
3269 * snd_soc_register_card.
3270 */
3271 if (ret < 0 && ret != -EINVAL) {
3272 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003273 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003274 propname, ret);
3275 return ret;
3276 }
3277
3278 return 0;
3279}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003280EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003281
Xiubo Li9a6d4862014-02-08 15:59:52 +08003282static const struct snd_soc_dapm_widget simple_widgets[] = {
3283 SND_SOC_DAPM_MIC("Microphone", NULL),
3284 SND_SOC_DAPM_LINE("Line", NULL),
3285 SND_SOC_DAPM_HP("Headphone", NULL),
3286 SND_SOC_DAPM_SPK("Speaker", NULL),
3287};
3288
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003289int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003290 const char *propname)
3291{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003292 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003293 struct snd_soc_dapm_widget *widgets;
3294 const char *template, *wname;
3295 int i, j, num_widgets, ret;
3296
3297 num_widgets = of_property_count_strings(np, propname);
3298 if (num_widgets < 0) {
3299 dev_err(card->dev,
3300 "ASoC: Property '%s' does not exist\n", propname);
3301 return -EINVAL;
3302 }
3303 if (num_widgets & 1) {
3304 dev_err(card->dev,
3305 "ASoC: Property '%s' length is not even\n", propname);
3306 return -EINVAL;
3307 }
3308
3309 num_widgets /= 2;
3310 if (!num_widgets) {
3311 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3312 propname);
3313 return -EINVAL;
3314 }
3315
3316 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3317 GFP_KERNEL);
3318 if (!widgets) {
3319 dev_err(card->dev,
3320 "ASoC: Could not allocate memory for widgets\n");
3321 return -ENOMEM;
3322 }
3323
3324 for (i = 0; i < num_widgets; i++) {
3325 ret = of_property_read_string_index(np, propname,
3326 2 * i, &template);
3327 if (ret) {
3328 dev_err(card->dev,
3329 "ASoC: Property '%s' index %d read error:%d\n",
3330 propname, 2 * i, ret);
3331 return -EINVAL;
3332 }
3333
3334 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3335 if (!strncmp(template, simple_widgets[j].name,
3336 strlen(simple_widgets[j].name))) {
3337 widgets[i] = simple_widgets[j];
3338 break;
3339 }
3340 }
3341
3342 if (j >= ARRAY_SIZE(simple_widgets)) {
3343 dev_err(card->dev,
3344 "ASoC: DAPM widget '%s' is not supported\n",
3345 template);
3346 return -EINVAL;
3347 }
3348
3349 ret = of_property_read_string_index(np, propname,
3350 (2 * i) + 1,
3351 &wname);
3352 if (ret) {
3353 dev_err(card->dev,
3354 "ASoC: Property '%s' index %d read error:%d\n",
3355 propname, (2 * i) + 1, ret);
3356 return -EINVAL;
3357 }
3358
3359 widgets[i].name = wname;
3360 }
3361
Nicolin Chenf23e8602015-02-14 17:22:49 -08003362 card->of_dapm_widgets = widgets;
3363 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003364
3365 return 0;
3366}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003367EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003368
Jyri Sarha61310842015-09-09 21:27:43 +03003369static int snd_soc_of_get_slot_mask(struct device_node *np,
3370 const char *prop_name,
3371 unsigned int *mask)
3372{
3373 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003374 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003375 int i;
3376
3377 if (!of_slot_mask)
3378 return 0;
3379 val /= sizeof(u32);
3380 for (i = 0; i < val; i++)
3381 if (be32_to_cpup(&of_slot_mask[i]))
3382 *mask |= (1 << i);
3383
3384 return val;
3385}
3386
Xiubo Li89c67852014-02-14 09:34:35 +08003387int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003388 unsigned int *tx_mask,
3389 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003390 unsigned int *slots,
3391 unsigned int *slot_width)
3392{
3393 u32 val;
3394 int ret;
3395
Jyri Sarha61310842015-09-09 21:27:43 +03003396 if (tx_mask)
3397 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3398 if (rx_mask)
3399 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3400
Xiubo Li89c67852014-02-14 09:34:35 +08003401 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3402 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3403 if (ret)
3404 return ret;
3405
3406 if (slots)
3407 *slots = val;
3408 }
3409
3410 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3411 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3412 if (ret)
3413 return ret;
3414
3415 if (slot_width)
3416 *slot_width = val;
3417 }
3418
3419 return 0;
3420}
3421EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3422
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003423void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003424 struct snd_soc_codec_conf *codec_conf,
3425 struct device_node *of_node,
3426 const char *propname)
3427{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003428 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003429 const char *str;
3430 int ret;
3431
3432 ret = of_property_read_string(np, propname, &str);
3433 if (ret < 0) {
3434 /* no prefix is not error */
3435 return;
3436 }
3437
3438 codec_conf->of_node = of_node;
3439 codec_conf->name_prefix = str;
3440}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003441EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003442
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003443int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003444 const char *propname)
3445{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003446 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003447 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003448 struct snd_soc_dapm_route *routes;
3449 int i, ret;
3450
3451 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003452 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003453 dev_err(card->dev,
3454 "ASoC: Property '%s' does not exist or its length is not even\n",
3455 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003456 return -EINVAL;
3457 }
3458 num_routes /= 2;
3459 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003460 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003461 propname);
3462 return -EINVAL;
3463 }
3464
Kees Cooka86854d2018-06-12 14:07:58 -07003465 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003466 GFP_KERNEL);
3467 if (!routes) {
3468 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003469 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003470 return -EINVAL;
3471 }
3472
3473 for (i = 0; i < num_routes; i++) {
3474 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003475 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003476 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003477 dev_err(card->dev,
3478 "ASoC: Property '%s' index %d could not be read: %d\n",
3479 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003480 return -EINVAL;
3481 }
3482 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003483 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003484 if (ret) {
3485 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003486 "ASoC: Property '%s' index %d could not be read: %d\n",
3487 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003488 return -EINVAL;
3489 }
3490 }
3491
Nicolin Chenf23e8602015-02-14 17:22:49 -08003492 card->num_of_dapm_routes = num_routes;
3493 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003494
3495 return 0;
3496}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003497EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003498
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003499unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003500 const char *prefix,
3501 struct device_node **bitclkmaster,
3502 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003503{
3504 int ret, i;
3505 char prop[128];
3506 unsigned int format = 0;
3507 int bit, frame;
3508 const char *str;
3509 struct {
3510 char *name;
3511 unsigned int val;
3512 } of_fmt_table[] = {
3513 { "i2s", SND_SOC_DAIFMT_I2S },
3514 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3515 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3516 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3517 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3518 { "ac97", SND_SOC_DAIFMT_AC97 },
3519 { "pdm", SND_SOC_DAIFMT_PDM},
3520 { "msb", SND_SOC_DAIFMT_MSB },
3521 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003522 };
3523
3524 if (!prefix)
3525 prefix = "";
3526
3527 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003528 * check "dai-format = xxx"
3529 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003530 * SND_SOC_DAIFMT_FORMAT_MASK area
3531 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003532 ret = of_property_read_string(np, "dai-format", &str);
3533 if (ret < 0) {
3534 snprintf(prop, sizeof(prop), "%sformat", prefix);
3535 ret = of_property_read_string(np, prop, &str);
3536 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003537 if (ret == 0) {
3538 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3539 if (strcmp(str, of_fmt_table[i].name) == 0) {
3540 format |= of_fmt_table[i].val;
3541 break;
3542 }
3543 }
3544 }
3545
3546 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003547 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003548 * SND_SOC_DAIFMT_CLOCK_MASK area
3549 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003550 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003551 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003552 format |= SND_SOC_DAIFMT_CONT;
3553 else
3554 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003555
3556 /*
3557 * check "[prefix]bitclock-inversion"
3558 * check "[prefix]frame-inversion"
3559 * SND_SOC_DAIFMT_INV_MASK area
3560 */
3561 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3562 bit = !!of_get_property(np, prop, NULL);
3563
3564 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3565 frame = !!of_get_property(np, prop, NULL);
3566
3567 switch ((bit << 4) + frame) {
3568 case 0x11:
3569 format |= SND_SOC_DAIFMT_IB_IF;
3570 break;
3571 case 0x10:
3572 format |= SND_SOC_DAIFMT_IB_NF;
3573 break;
3574 case 0x01:
3575 format |= SND_SOC_DAIFMT_NB_IF;
3576 break;
3577 default:
3578 /* SND_SOC_DAIFMT_NB_NF is default */
3579 break;
3580 }
3581
3582 /*
3583 * check "[prefix]bitclock-master"
3584 * check "[prefix]frame-master"
3585 * SND_SOC_DAIFMT_MASTER_MASK area
3586 */
3587 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3588 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003589 if (bit && bitclkmaster)
3590 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003591
3592 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3593 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003594 if (frame && framemaster)
3595 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003596
3597 switch ((bit << 4) + frame) {
3598 case 0x11:
3599 format |= SND_SOC_DAIFMT_CBM_CFM;
3600 break;
3601 case 0x10:
3602 format |= SND_SOC_DAIFMT_CBM_CFS;
3603 break;
3604 case 0x01:
3605 format |= SND_SOC_DAIFMT_CBS_CFM;
3606 break;
3607 default:
3608 format |= SND_SOC_DAIFMT_CBS_CFS;
3609 break;
3610 }
3611
3612 return format;
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3615
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003616int snd_soc_get_dai_id(struct device_node *ep)
3617{
3618 struct snd_soc_component *pos;
3619 struct device_node *node;
3620 int ret;
3621
3622 node = of_graph_get_port_parent(ep);
3623
3624 /*
3625 * For example HDMI case, HDMI has video/sound port,
3626 * but ALSA SoC needs sound port number only.
3627 * Thus counting HDMI DT port/endpoint doesn't work.
3628 * Then, it should have .of_xlate_dai_id
3629 */
3630 ret = -ENOTSUPP;
3631 mutex_lock(&client_mutex);
3632 list_for_each_entry(pos, &component_list, list) {
3633 struct device_node *component_of_node = pos->dev->of_node;
3634
3635 if (!component_of_node && pos->dev->parent)
3636 component_of_node = pos->dev->parent->of_node;
3637
3638 if (component_of_node != node)
3639 continue;
3640
3641 if (pos->driver->of_xlate_dai_id)
3642 ret = pos->driver->of_xlate_dai_id(pos, ep);
3643
3644 break;
3645 }
3646 mutex_unlock(&client_mutex);
3647
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003648 of_node_put(node);
3649
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003650 return ret;
3651}
3652EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3653
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003654int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003655 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003656{
3657 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003658 struct device_node *component_of_node;
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003659 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003660
3661 mutex_lock(&client_mutex);
3662 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003663 component_of_node = pos->dev->of_node;
3664 if (!component_of_node && pos->dev->parent)
3665 component_of_node = pos->dev->parent->of_node;
3666
3667 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003668 continue;
3669
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003670 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003671 ret = pos->driver->of_xlate_dai_name(pos,
3672 args,
3673 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003674 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003675 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003676 int id = -1;
3677
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003678 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003679 case 0:
3680 id = 0; /* same as dai_drv[0] */
3681 break;
3682 case 1:
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003683 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003684 break;
3685 default:
3686 /* not supported */
3687 break;
3688 }
3689
3690 if (id < 0 || id >= pos->num_dai) {
3691 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003692 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003693 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003694
3695 ret = 0;
3696
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003697 /* find target DAI */
3698 list_for_each_entry(dai, &pos->dai_list, list) {
3699 if (id == 0)
3700 break;
3701 id--;
3702 }
3703
3704 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003705 if (!*dai_name)
3706 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003707 }
3708
Kuninori Morimotocb470082013-09-10 17:39:56 -07003709 break;
3710 }
3711 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003712 return ret;
3713}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003714EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003715
3716int snd_soc_of_get_dai_name(struct device_node *of_node,
3717 const char **dai_name)
3718{
3719 struct of_phandle_args args;
3720 int ret;
3721
3722 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3723 "#sound-dai-cells", 0, &args);
3724 if (ret)
3725 return ret;
3726
3727 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003728
3729 of_node_put(args.np);
3730
3731 return ret;
3732}
3733EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3734
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003735/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003736 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3737 * @dai_link: DAI link
3738 *
3739 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3740 */
3741void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3742{
3743 struct snd_soc_dai_link_component *component = dai_link->codecs;
3744 int index;
3745
3746 for (index = 0; index < dai_link->num_codecs; index++, component++) {
3747 if (!component->of_node)
3748 break;
3749 of_node_put(component->of_node);
3750 component->of_node = NULL;
3751 }
3752}
3753EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3754
3755/*
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003756 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3757 * @dev: Card device
3758 * @of_node: Device node
3759 * @dai_link: DAI link
3760 *
3761 * Builds an array of CODEC DAI components from the DAI link property
3762 * 'sound-dai'.
3763 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003764 * The device nodes in the array (of_node) must be dereferenced by calling
3765 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003766 *
3767 * Returns 0 for success
3768 */
3769int snd_soc_of_get_dai_link_codecs(struct device *dev,
3770 struct device_node *of_node,
3771 struct snd_soc_dai_link *dai_link)
3772{
3773 struct of_phandle_args args;
3774 struct snd_soc_dai_link_component *component;
3775 char *name;
3776 int index, num_codecs, ret;
3777
3778 /* Count the number of CODECs */
3779 name = "sound-dai";
3780 num_codecs = of_count_phandle_with_args(of_node, name,
3781 "#sound-dai-cells");
3782 if (num_codecs <= 0) {
3783 if (num_codecs == -ENOENT)
3784 dev_err(dev, "No 'sound-dai' property\n");
3785 else
3786 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3787 return num_codecs;
3788 }
Kees Cooka86854d2018-06-12 14:07:58 -07003789 component = devm_kcalloc(dev,
3790 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003791 GFP_KERNEL);
3792 if (!component)
3793 return -ENOMEM;
3794 dai_link->codecs = component;
3795 dai_link->num_codecs = num_codecs;
3796
3797 /* Parse the list */
3798 for (index = 0, component = dai_link->codecs;
3799 index < dai_link->num_codecs;
3800 index++, component++) {
3801 ret = of_parse_phandle_with_args(of_node, name,
3802 "#sound-dai-cells",
3803 index, &args);
3804 if (ret)
3805 goto err;
3806 component->of_node = args.np;
3807 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3808 if (ret < 0)
3809 goto err;
3810 }
3811 return 0;
3812err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003813 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3ee2014-11-25 13:16:12 +01003814 dai_link->codecs = NULL;
3815 dai_link->num_codecs = 0;
3816 return ret;
3817}
3818EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3819
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003820static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003821{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003822 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003823 snd_soc_util_init();
3824
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003825 return platform_driver_register(&soc_driver);
3826}
Mark Brown4abe8e12010-10-12 17:41:03 +01003827module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003828
Mark Brown7d8c16a2008-11-30 22:11:24 +00003829static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003830{
Mark Brownfb257892011-04-28 10:57:54 +01003831 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003832 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003833
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003834 platform_driver_unregister(&soc_driver);
3835}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003836module_exit(snd_soc_exit);
3837
3838/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003839MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003840MODULE_DESCRIPTION("ALSA SoC Core");
3841MODULE_LICENSE("GPL");
3842MODULE_ALIAS("platform:soc-audio");