blob: 2d3abb3f1231a1ff0da548eb7c4767492b3aed50 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000037#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080038#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000040#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010044#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010045#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070059static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Mengdong Lin98faf432017-06-28 15:01:39 +080070/* If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
72 * as invalid and dropped when setting the card long name from DMI info.
73 */
74static const char * const dmi_blacklist[] = {
75 "To be filled by OEM",
76 "TBD by OEM",
77 "Default String",
78 "Board Manufacturer",
79 "Board Vendor Name",
80 "Board Product Name",
81 NULL, /* terminator */
82};
83
Mark Browndbe21402010-02-12 11:37:24 +000084static ssize_t pmdown_time_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
Mark Brown36ae1a92012-01-06 17:12:45 -080087 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +000088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000089 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +000090}
91
92static ssize_t pmdown_time_set(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
Mark Brown36ae1a92012-01-06 17:12:45 -080096 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -070097 int ret;
Mark Browndbe21402010-02-12 11:37:24 +000098
Jingoo Hanb785a492013-07-19 16:24:59 +090099 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700100 if (ret)
101 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000102
103 return count;
104}
105
106static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
107
Takashi Iwaid29697d2015-01-30 20:16:37 +0100108static struct attribute *soc_dev_attrs[] = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100109 &dev_attr_pmdown_time.attr,
110 NULL
111};
112
113static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
114 struct attribute *attr, int idx)
115{
116 struct device *dev = kobj_to_dev(kobj);
117 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
118
119 if (attr == &dev_attr_pmdown_time.attr)
120 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000121 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100122}
123
124static const struct attribute_group soc_dapm_dev_group = {
125 .attrs = soc_dapm_dev_attrs,
126 .is_visible = soc_dev_attr_is_visible,
127};
128
Mark Brownf7e73b262018-03-09 12:46:27 +0000129static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100130 .attrs = soc_dev_attrs,
131 .is_visible = soc_dev_attr_is_visible,
132};
133
134static const struct attribute_group *soc_dev_attr_groups[] = {
135 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000136 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100137 NULL
138};
139
Mark Brown2624d5f2009-11-03 21:56:13 +0000140#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200141static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100142{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200143 if (!component->card->debugfs_card_root)
144 return;
145
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200146 if (component->debugfs_prefix) {
147 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100148
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200149 name = kasprintf(GFP_KERNEL, "%s:%s",
150 component->debugfs_prefix, component->name);
151 if (name) {
152 component->debugfs_root = debugfs_create_dir(name,
153 component->card->debugfs_card_root);
154 kfree(name);
155 }
156 } else {
157 component->debugfs_root = debugfs_create_dir(component->name,
158 component->card->debugfs_card_root);
159 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100160
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200161 if (!component->debugfs_root) {
162 dev_warn(component->dev,
163 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 return;
165 }
166
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200167 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
168 component->debugfs_root);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200169}
170
171static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
172{
173 debugfs_remove_recursive(component->debugfs_root);
174}
175
Peng Donglinc15b2a12018-02-14 22:48:07 +0800176static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100177{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100178 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100179 struct snd_soc_dai *dai;
180
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100181 mutex_lock(&client_mutex);
182
Donglin Peng700c17c2018-01-18 13:31:26 +0800183 list_for_each_entry(component, &component_list, list)
184 list_for_each_entry(dai, &component->dai_list, list)
185 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100186
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100187 mutex_unlock(&client_mutex);
188
Donglin Peng700c17c2018-01-18 13:31:26 +0800189 return 0;
190}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800191DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100192
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000193static int component_list_show(struct seq_file *m, void *v)
194{
195 struct snd_soc_component *component;
196
197 mutex_lock(&client_mutex);
198
199 list_for_each_entry(component, &component_list, list)
200 seq_printf(m, "%s\n", component->name);
201
202 mutex_unlock(&client_mutex);
203
204 return 0;
205}
206DEFINE_SHOW_ATTRIBUTE(component_list);
207
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200208static void soc_init_card_debugfs(struct snd_soc_card *card)
209{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200210 if (!snd_soc_debugfs_root)
211 return;
212
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200213 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000214 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200215 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200216 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100217 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200218 return;
219 }
220
221 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
222 card->debugfs_card_root,
223 &card->pop_time);
224 if (!card->debugfs_pop_time)
225 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000226 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200227}
228
229static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
230{
231 debugfs_remove_recursive(card->debugfs_card_root);
232}
233
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200234static void snd_soc_debugfs_init(void)
235{
236 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300237 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200238 pr_warn("ASoC: Failed to create debugfs directory\n");
239 snd_soc_debugfs_root = NULL;
240 return;
241 }
242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200243 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
244 &dai_list_fops))
245 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000246
247 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
248 &component_list_fops))
249 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200250}
251
252static void snd_soc_debugfs_exit(void)
253{
254 debugfs_remove_recursive(snd_soc_debugfs_root);
255}
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257#else
258
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200259static inline void soc_init_component_debugfs(
260 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261{
262}
263
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200264static inline void soc_cleanup_component_debugfs(
265 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000266{
267}
268
Axel Linb95fccb2010-11-09 17:06:44 +0800269static inline void soc_init_card_debugfs(struct snd_soc_card *card)
270{
271}
272
273static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
274{
275}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200276
277static inline void snd_soc_debugfs_init(void)
278{
279}
280
281static inline void snd_soc_debugfs_exit(void)
282{
283}
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285#endif
286
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000287static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
288 struct snd_soc_component *component)
289{
290 struct snd_soc_rtdcom_list *rtdcom;
291 struct snd_soc_rtdcom_list *new_rtdcom;
292
293 for_each_rtdcom(rtd, rtdcom) {
294 /* already connected */
295 if (rtdcom->component == component)
296 return 0;
297 }
298
299 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
300 if (!new_rtdcom)
301 return -ENOMEM;
302
303 new_rtdcom->component = component;
304 INIT_LIST_HEAD(&new_rtdcom->list);
305
306 list_add_tail(&new_rtdcom->list, &rtd->component_list);
307
308 return 0;
309}
310
311static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
312{
313 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
314
315 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
316 kfree(rtdcom1);
317
318 INIT_LIST_HEAD(&rtd->component_list);
319}
320
321struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
322 const char *driver_name)
323{
324 struct snd_soc_rtdcom_list *rtdcom;
325
Kuninori Morimoto971da242018-01-23 00:41:24 +0000326 if (!driver_name)
327 return NULL;
328
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000329 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000330 const char *component_name = rtdcom->component->driver->name;
331
332 if (!component_name)
333 continue;
334
335 if ((component_name == driver_name) ||
336 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000337 return rtdcom->component;
338 }
339
340 return NULL;
341}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000342EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000343
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100344struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
345 const char *dai_link, int stream)
346{
Mengdong Lin1a497982015-11-18 02:34:11 -0500347 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100348
Mengdong Lin1a497982015-11-18 02:34:11 -0500349 list_for_each_entry(rtd, &card->rtd_list, list) {
350 if (rtd->dai_link->no_pcm &&
351 !strcmp(rtd->dai_link->name, dai_link))
352 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100353 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000354 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100355 return NULL;
356}
357EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
358
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000359static const struct snd_soc_ops null_snd_soc_ops;
360
Mengdong Lin1a497982015-11-18 02:34:11 -0500361static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
362 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
363{
364 struct snd_soc_pcm_runtime *rtd;
365
366 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
367 if (!rtd)
368 return NULL;
369
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000370 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500371 rtd->card = card;
372 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000373 if (!rtd->dai_link->ops)
374 rtd->dai_link->ops = &null_snd_soc_ops;
375
Mengdong Lin1a497982015-11-18 02:34:11 -0500376 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
377 dai_link->num_codecs,
378 GFP_KERNEL);
379 if (!rtd->codec_dais) {
380 kfree(rtd);
381 return NULL;
382 }
383
384 return rtd;
385}
386
387static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
388{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000389 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000390 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500391 kfree(rtd);
392}
393
394static void soc_add_pcm_runtime(struct snd_soc_card *card,
395 struct snd_soc_pcm_runtime *rtd)
396{
397 list_add_tail(&rtd->list, &card->rtd_list);
398 rtd->num = card->num_rtd;
399 card->num_rtd++;
400}
401
402static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
403{
404 struct snd_soc_pcm_runtime *rtd, *_rtd;
405
406 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
407 list_del(&rtd->list);
408 soc_free_pcm_runtime(rtd);
409 }
410
411 card->num_rtd = 0;
412}
413
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100414struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
415 const char *dai_link)
416{
Mengdong Lin1a497982015-11-18 02:34:11 -0500417 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100418
Mengdong Lin1a497982015-11-18 02:34:11 -0500419 list_for_each_entry(rtd, &card->rtd_list, list) {
420 if (!strcmp(rtd->dai_link->name, dai_link))
421 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100422 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000423 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100424 return NULL;
425}
426EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
427
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100428static void codec2codec_close_delayed_work(struct work_struct *work)
429{
430 /* Currently nothing to do for c2c links
431 * Since c2c links are internal nodes in the DAPM graph and
432 * don't interface with the outside world or application layer
433 * we don't have to do any special handling on close.
434 */
435}
436
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000437#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200438/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000439int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200440{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000441 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000442 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500443 struct snd_soc_pcm_runtime *rtd;
444 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200445
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200446 /* If the card is not initialized yet there is nothing to do */
447 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200448 return 0;
449
Andy Green6ed25972008-06-13 16:24:05 +0100450 /* Due to the resume being scheduled into a workqueue we could
451 * suspend before that's finished - wait for it to complete.
452 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100454
455 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100457
Mark Browna00f90f2010-12-02 16:24:24 +0000458 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500459 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100460
Mengdong Lin1a497982015-11-18 02:34:11 -0500461 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100462 continue;
463
Mengdong Lin1a497982015-11-18 02:34:11 -0500464 for (i = 0; i < rtd->num_codecs; i++) {
465 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200466 struct snd_soc_dai_driver *drv = dai->driver;
467
468 if (drv->ops->digital_mute && dai->playback_active)
469 drv->ops->digital_mute(dai, 1);
470 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 }
472
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100473 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500474 list_for_each_entry(rtd, &card->rtd_list, list) {
475 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100476 continue;
477
Mengdong Lin1a497982015-11-18 02:34:11 -0500478 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100479 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100480
Mark Brown87506542008-11-18 20:50:34 +0000481 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000482 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483
Mengdong Lin1a497982015-11-18 02:34:11 -0500484 list_for_each_entry(rtd, &card->rtd_list, list) {
485 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100486
Mengdong Lin1a497982015-11-18 02:34:11 -0500487 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100488 continue;
489
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100490 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100492 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200494 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500495 list_for_each_entry(rtd, &card->rtd_list, list)
496 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100497
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499
Mengdong Lin1a497982015-11-18 02:34:11 -0500500 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100501 continue;
502
Mengdong Lin1a497982015-11-18 02:34:11 -0500503 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800504 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800505 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506
Mengdong Lin1a497982015-11-18 02:34:11 -0500507 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800508 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800509 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 }
511
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200512 /* Recheck all endpoints too, their state is affected by suspend */
513 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700514 snd_soc_dapm_sync(&card->dapm);
515
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000516 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000517 list_for_each_entry(component, &card->component_dev_list, card_list) {
518 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000519
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000520 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000522 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200523 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000525 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000526 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000527 * bias off then being in STANDBY
528 * means it's doing something,
529 * otherwise fall through.
530 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200531 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000532 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200533 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000534 break;
535 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000538 if (component->driver->suspend)
539 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000540 component->suspended = 1;
541 if (component->regmap)
542 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800543 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000544 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 break;
546 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000547 dev_dbg(component->dev,
548 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 break;
550 }
551 }
552 }
553
Mengdong Lin1a497982015-11-18 02:34:11 -0500554 list_for_each_entry(rtd, &card->rtd_list, list) {
555 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556
Mengdong Lin1a497982015-11-18 02:34:11 -0500557 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 continue;
559
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100560 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800562
563 /* deactivate pins to sleep state */
564 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565 }
566
Mark Brown87506542008-11-18 20:50:34 +0000567 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000568 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569
570 return 0;
571}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000572EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573
Andy Green6ed25972008-06-13 16:24:05 +0100574/* deferred resume work, so resume can complete before we finished
575 * setting our codec back up, which can be very slow on I2C
576 */
577static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 struct snd_soc_card *card =
580 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500581 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000582 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500583 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584
Andy Green6ed25972008-06-13 16:24:05 +0100585 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
586 * so userspace apps are blocked from touching us
587 */
588
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000589 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100590
Mark Brown9949788b2010-05-07 20:24:05 +0100591 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100597 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500598 list_for_each_entry(rtd, &card->rtd_list, list) {
599 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Mengdong Lin1a497982015-11-18 02:34:11 -0500601 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100604 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200606 }
607
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000608 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000609 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000610 if (component->driver->resume)
611 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000612 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100613 }
614 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615
Mengdong Lin1a497982015-11-18 02:34:11 -0500616 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100617
Mengdong Lin1a497982015-11-18 02:34:11 -0500618 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100619 continue;
620
Mengdong Lin1a497982015-11-18 02:34:11 -0500621 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000622 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800623 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624
Mengdong Lin1a497982015-11-18 02:34:11 -0500625 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000626 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 }
629
Mark Brown3ff3f642008-05-19 12:32:25 +0200630 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500631 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100632
Mengdong Lin1a497982015-11-18 02:34:11 -0500633 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100634 continue;
635
Mengdong Lin1a497982015-11-18 02:34:11 -0500636 for (i = 0; i < rtd->num_codecs; i++) {
637 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200638 struct snd_soc_dai_driver *drv = dai->driver;
639
640 if (drv->ops->digital_mute && dai->playback_active)
641 drv->ops->digital_mute(dai, 0);
642 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 }
644
Mengdong Lin1a497982015-11-18 02:34:11 -0500645 list_for_each_entry(rtd, &card->rtd_list, list) {
646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100647
Mengdong Lin1a497982015-11-18 02:34:11 -0500648 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100649 continue;
650
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100651 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653 }
654
Mark Brown87506542008-11-18 20:50:34 +0000655 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000656 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000658 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100659
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200660 /* Recheck all endpoints too, their state is affected by suspend */
661 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700662 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530663
664 /* userspace can access us now we are back as we were before */
665 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100666}
667
668/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000669int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100670{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000671 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100672 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500673 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200674
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200675 /* If the card is not initialized yet there is nothing to do */
676 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800677 return 0;
678
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800679 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500680 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200681 struct snd_soc_dai **codec_dais = rtd->codec_dais;
682 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
683 int j;
684
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800685 if (cpu_dai->active)
686 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200687
688 for (j = 0; j < rtd->num_codecs; j++) {
689 struct snd_soc_dai *codec_dai = codec_dais[j];
690 if (codec_dai->active)
691 pinctrl_pm_select_default_state(codec_dai->dev);
692 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800693 }
694
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100695 /*
696 * DAIs that also act as the control bus master might have other drivers
697 * hanging off them so need to resume immediately. Other drivers don't
698 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100699 * due to I/O costs and anti-pop so handle them out of line.
700 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500701 list_for_each_entry(rtd, &card->rtd_list, list) {
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100703 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600704 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100705 if (bus_control) {
706 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600707 soc_resume_deferred(&card->deferred_resume_work);
708 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600710 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000711 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100712 }
Andy Green6ed25972008-06-13 16:24:05 +0100713
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200714 return 0;
715}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000716EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000718#define snd_soc_suspend NULL
719#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720#endif
721
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100722static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800723};
724
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200725static struct snd_soc_component *soc_find_component(
726 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100727{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200728 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100729
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100730 lockdep_assert_held(&client_mutex);
731
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200732 list_for_each_entry(component, &component_list, list) {
733 if (of_node) {
734 if (component->dev->of_node == of_node)
735 return component;
736 } else if (strcmp(component->name, name) == 0) {
737 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100738 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100739 }
740
741 return NULL;
742}
743
Mengdong Linfbb88b52016-04-22 12:25:33 +0800744/**
745 * snd_soc_find_dai - Find a registered DAI
746 *
Jeffy Chen49584712017-08-22 15:57:21 +0800747 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800748 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700749 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800750 * find the DAI of the same name. The component's of_node and name
751 * should also match if being specified.
752 *
753 * Return: pointer of DAI, or NULL if not found.
754 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800755struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200756 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100757{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200758 struct snd_soc_component *component;
759 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300760 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100761
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100762 lockdep_assert_held(&client_mutex);
763
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200764 /* Find CPU DAI from registered DAIs*/
765 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300766 component_of_node = component->dev->of_node;
767 if (!component_of_node && component->dev->parent)
768 component_of_node = component->dev->parent->of_node;
769
770 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200771 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100772 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200773 continue;
774 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800775 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800776 && (!dai->driver->name
777 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100778 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100779
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200780 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100781 }
782 }
783
784 return NULL;
785}
Mengdong Lin305e9022016-04-19 13:12:25 +0800786EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100787
Mengdong Lin17fb17552016-11-03 01:04:12 +0800788
789/**
790 * snd_soc_find_dai_link - Find a DAI link
791 *
792 * @card: soc card
793 * @id: DAI link ID to match
794 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000795 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800796 *
797 * This function will search all existing DAI links of the soc card to
798 * find the link of the same ID. Since DAI links may not have their
799 * unique ID, so name and stream name should also match if being
800 * specified.
801 *
802 * Return: pointer of DAI link, or NULL if not found.
803 */
804struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
805 int id, const char *name,
806 const char *stream_name)
807{
808 struct snd_soc_dai_link *link, *_link;
809
810 lockdep_assert_held(&client_mutex);
811
812 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
813 if (link->id != id)
814 continue;
815
816 if (name && (!link->name || strcmp(name, link->name)))
817 continue;
818
819 if (stream_name && (!link->stream_name
820 || strcmp(stream_name, link->stream_name)))
821 continue;
822
823 return link;
824 }
825
826 return NULL;
827}
828EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
829
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800830static bool soc_is_dai_link_bound(struct snd_soc_card *card,
831 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800833 struct snd_soc_pcm_runtime *rtd;
834
835 list_for_each_entry(rtd, &card->rtd_list, list) {
836 if (rtd->dai_link == dai_link)
837 return true;
838 }
839
840 return false;
841}
842
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500843static int soc_bind_dai_link(struct snd_soc_card *card,
844 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845{
Mengdong Lin1a497982015-11-18 02:34:11 -0500846 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200847 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200848 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000849 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500850 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +0000851 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +0100852 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200853 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500855 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000856
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800857 if (soc_is_dai_link_bound(card, dai_link)) {
858 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
859 dai_link->name);
860 return 0;
861 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200862
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530863 rtd = soc_new_pcm_runtime(card, dai_link);
864 if (!rtd)
865 return -ENOMEM;
866
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200867 cpu_dai_component.name = dai_link->cpu_name;
868 cpu_dai_component.of_node = dai_link->cpu_of_node;
869 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
870 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000871 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100872 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
873 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500874 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000875 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000876 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000877
Benoit Cousson88bd8702014-07-08 23:19:34 +0200878 rtd->num_codecs = dai_link->num_codecs;
879
880 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500881 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200882 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200883 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200884 if (!codec_dais[i]) {
885 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
886 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500887 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200888 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000889 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100891
Benoit Cousson88bd8702014-07-08 23:19:34 +0200892 /* Single codec links expect codec and codec_dai in runtime data */
893 rtd->codec_dai = codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100894
Mark Brown848dd8b2011-04-27 18:16:32 +0100895 /* if there's no platform we match on the empty platform */
896 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700897 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100898 platform_name = "snd-soc-dummy";
899
Mark Brownb19e6e72012-03-14 21:18:39 +0000900 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000901 list_for_each_entry(component, &component_list, list) {
902 platform_of_node = component->dev->of_node;
903 if (!platform_of_node && component->dev->parent->of_node)
904 platform_of_node = component->dev->parent->of_node;
905
906 if (dai_link->platform_of_node) {
907 if (platform_of_node != dai_link->platform_of_node)
908 continue;
909 } else {
910 if (strcmp(component->name, platform_name))
911 continue;
912 }
913
914 snd_soc_rtdcom_add(rtd, component);
915 }
916
Mengdong Lin1a497982015-11-18 02:34:11 -0500917 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000918 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500919
920_err_defer:
921 soc_free_pcm_runtime(rtd);
922 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000923}
924
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200925static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600926{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200927 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200928 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600929
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000930 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600931
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000932 if (component->driver->remove)
933 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600934
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200935 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600936
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200937 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200938 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200939 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600940}
941
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200942static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200943{
944 int err;
945
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200946 if (dai && dai->probed &&
947 dai->driver->remove_order == order) {
948 if (dai->driver->remove) {
949 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100950 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200951 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100952 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200953 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100954 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200955 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100956 }
957}
958
Mengdong Lin1a497982015-11-18 02:34:11 -0500959static void soc_remove_link_dais(struct snd_soc_card *card,
960 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200962 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800966 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000967 rtd->dev_registered = 0;
968 }
969
970 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200971 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200972 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200974 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975}
976
Mengdong Lin1a497982015-11-18 02:34:11 -0500977static void soc_remove_link_components(struct snd_soc_card *card,
978 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -0600979{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200980 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000981 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600982
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000983 for_each_rtdcom(rtd, rtdcom) {
984 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600985
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200986 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200987 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600988 }
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989}
990
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900991static void soc_remove_dai_links(struct snd_soc_card *card)
992{
Mengdong Lin1a497982015-11-18 02:34:11 -0500993 int order;
994 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +0800995 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900996
Liam Girdwood0168bf02011-06-07 16:08:05 +0100997 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
998 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500999 list_for_each_entry(rtd, &card->rtd_list, list)
1000 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001001 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001002
1003 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1004 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001005 list_for_each_entry(rtd, &card->rtd_list, list)
1006 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007 }
1008
Mengdong Linf8f80362015-12-02 14:11:22 +08001009 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1010 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1011 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1012 link->name);
1013
1014 list_del(&link->list);
1015 card->num_dai_links--;
1016 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001017}
1018
Mengdong Lin923c5e612015-11-23 11:01:49 -05001019static int snd_soc_init_multicodec(struct snd_soc_card *card,
1020 struct snd_soc_dai_link *dai_link)
1021{
1022 /* Legacy codec/codec_dai link is a single entry in multicodec */
1023 if (dai_link->codec_name || dai_link->codec_of_node ||
1024 dai_link->codec_dai_name) {
1025 dai_link->num_codecs = 1;
1026
1027 dai_link->codecs = devm_kzalloc(card->dev,
1028 sizeof(struct snd_soc_dai_link_component),
1029 GFP_KERNEL);
1030 if (!dai_link->codecs)
1031 return -ENOMEM;
1032
1033 dai_link->codecs[0].name = dai_link->codec_name;
1034 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1035 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1036 }
1037
1038 if (!dai_link->codecs) {
1039 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1040 return -EINVAL;
1041 }
1042
1043 return 0;
1044}
1045
1046static int soc_init_dai_link(struct snd_soc_card *card,
1047 struct snd_soc_dai_link *link)
1048{
1049 int i, ret;
1050
1051 ret = snd_soc_init_multicodec(card, link);
1052 if (ret) {
1053 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1054 return ret;
1055 }
1056
1057 for (i = 0; i < link->num_codecs; i++) {
1058 /*
1059 * Codec must be specified by 1 of name or OF node,
1060 * not both or neither.
1061 */
1062 if (!!link->codecs[i].name ==
1063 !!link->codecs[i].of_node) {
1064 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1065 link->name);
1066 return -EINVAL;
1067 }
1068 /* Codec DAI name must be specified */
1069 if (!link->codecs[i].dai_name) {
1070 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1071 link->name);
1072 return -EINVAL;
1073 }
1074 }
1075
1076 /*
1077 * Platform may be specified by either name or OF node, but
1078 * can be left unspecified, and a dummy platform will be used.
1079 */
1080 if (link->platform_name && link->platform_of_node) {
1081 dev_err(card->dev,
1082 "ASoC: Both platform name/of_node are set for %s\n",
1083 link->name);
1084 return -EINVAL;
1085 }
1086
1087 /*
1088 * CPU device may be specified by either name or OF node, but
1089 * can be left unspecified, and will be matched based on DAI
1090 * name alone..
1091 */
1092 if (link->cpu_name && link->cpu_of_node) {
1093 dev_err(card->dev,
1094 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1095 link->name);
1096 return -EINVAL;
1097 }
1098 /*
1099 * At least one of CPU DAI name or CPU device name/node must be
1100 * specified
1101 */
1102 if (!link->cpu_dai_name &&
1103 !(link->cpu_name || link->cpu_of_node)) {
1104 dev_err(card->dev,
1105 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1106 link->name);
1107 return -EINVAL;
1108 }
1109
1110 return 0;
1111}
1112
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001113void snd_soc_disconnect_sync(struct device *dev)
1114{
1115 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1116
1117 if (!component || !component->card)
1118 return;
1119
1120 snd_card_disconnect_sync(component->card->snd_card);
1121}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001122EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001123
Mengdong Linf8f80362015-12-02 14:11:22 +08001124/**
1125 * snd_soc_add_dai_link - Add a DAI link dynamically
1126 * @card: The ASoC card to which the DAI link is added
1127 * @dai_link: The new DAI link to add
1128 *
1129 * This function adds a DAI link to the ASoC card's link list.
1130 *
1131 * Note: Topology can use this API to add DAI links when probing the
1132 * topology component. And machine drivers can still define static
1133 * DAI links in dai_link array.
1134 */
1135int snd_soc_add_dai_link(struct snd_soc_card *card,
1136 struct snd_soc_dai_link *dai_link)
1137{
1138 if (dai_link->dobj.type
1139 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1140 dev_err(card->dev, "Invalid dai link type %d\n",
1141 dai_link->dobj.type);
1142 return -EINVAL;
1143 }
1144
1145 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001146 /* Notify the machine driver for extra initialization
1147 * on the link created by topology.
1148 */
1149 if (dai_link->dobj.type && card->add_dai_link)
1150 card->add_dai_link(card, dai_link);
1151
Mengdong Linf8f80362015-12-02 14:11:22 +08001152 list_add_tail(&dai_link->list, &card->dai_link_list);
1153 card->num_dai_links++;
1154
1155 return 0;
1156}
1157EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1158
1159/**
1160 * snd_soc_remove_dai_link - Remove a DAI link from the list
1161 * @card: The ASoC card that owns the link
1162 * @dai_link: The DAI link to remove
1163 *
1164 * This function removes a DAI link from the ASoC card's link list.
1165 *
1166 * For DAI links previously added by topology, topology should
1167 * remove them by using the dobj embedded in the link.
1168 */
1169void snd_soc_remove_dai_link(struct snd_soc_card *card,
1170 struct snd_soc_dai_link *dai_link)
1171{
1172 struct snd_soc_dai_link *link, *_link;
1173
1174 if (dai_link->dobj.type
1175 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1176 dev_err(card->dev, "Invalid dai link type %d\n",
1177 dai_link->dobj.type);
1178 return;
1179 }
1180
1181 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001182 /* Notify the machine driver for extra destruction
1183 * on the link created by topology.
1184 */
1185 if (dai_link->dobj.type && card->remove_dai_link)
1186 card->remove_dai_link(card, dai_link);
1187
Mengdong Linf8f80362015-12-02 14:11:22 +08001188 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1189 if (link == dai_link) {
1190 list_del(&link->list);
1191 card->num_dai_links--;
1192 return;
1193 }
1194 }
1195}
1196EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1197
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001198static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001199 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001200{
1201 int i;
1202
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001203 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001204 return;
1205
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001206 for (i = 0; i < card->num_configs; i++) {
1207 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001208 struct device_node *component_of_node = component->dev->of_node;
1209
1210 if (!component_of_node && component->dev->parent)
1211 component_of_node = component->dev->parent->of_node;
1212
1213 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001214 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001215 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001216 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001217 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001218 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001219 }
1220}
1221
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001222static int soc_probe_component(struct snd_soc_card *card,
1223 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001224{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001225 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001226 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001227 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001228
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001229 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001230 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001231
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001232 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001233 if (component->card != card) {
1234 dev_err(component->dev,
1235 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1236 card->name, component->card->name);
1237 return -ENODEV;
1238 }
1239 return 0;
1240 }
1241
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001242 if (!try_module_get(component->dev->driver->owner))
1243 return -ENODEV;
1244
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001245 component->card = card;
1246 dapm->card = card;
1247 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001248
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001249 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001250
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001251 if (component->driver->dapm_widgets) {
1252 ret = snd_soc_dapm_new_controls(dapm,
1253 component->driver->dapm_widgets,
1254 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001255
1256 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001257 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001258 "Failed to create new controls %d\n", ret);
1259 goto err_probe;
1260 }
1261 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001262
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001263 list_for_each_entry(dai, &component->dai_list, list) {
1264 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001265 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001266 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001267 "Failed to create DAI widgets %d\n", ret);
1268 goto err_probe;
1269 }
1270 }
Mark Brown888df392012-02-16 19:37:51 -08001271
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001272 if (component->driver->probe) {
1273 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001274 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001275 dev_err(component->dev,
1276 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001277 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001279
1280 WARN(dapm->idle_bias_off &&
1281 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001282 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001283 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001284 }
1285
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001286 /* machine specific init */
1287 if (component->init) {
1288 ret = component->init(component);
1289 if (ret < 0) {
1290 dev_err(component->dev,
1291 "Failed to do machine specific init %d\n", ret);
1292 goto err_probe;
1293 }
1294 }
1295
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001296 if (component->driver->controls)
1297 snd_soc_add_component_controls(component,
1298 component->driver->controls,
1299 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001300 if (component->driver->dapm_routes)
1301 snd_soc_dapm_add_routes(dapm,
1302 component->driver->dapm_routes,
1303 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001304
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001305 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001306 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307
Jarkko Nikula70d293312011-01-27 16:24:22 +02001308 return 0;
1309
1310err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001311 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001312 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001313 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001314
1315 return ret;
1316}
1317
Mark Brown36ae1a92012-01-06 17:12:45 -08001318static void rtd_release(struct device *dev)
1319{
1320 kfree(dev);
1321}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001322
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001323static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1324 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001325{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001326 int ret = 0;
1327
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001329 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1330 if (!rtd->dev)
1331 return -ENOMEM;
1332 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001333 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001334 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001335 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001336 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001337 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001338 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001339 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1340 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1341 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1342 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001343 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001344 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001345 /* calling put_device() here to free the rtd->dev */
1346 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001347 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001348 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349 return ret;
1350 }
1351 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001352 return 0;
1353}
1354
Mengdong Lin1a497982015-11-18 02:34:11 -05001355static int soc_probe_link_components(struct snd_soc_card *card,
1356 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001357 int order)
1358{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001359 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001360 struct snd_soc_rtdcom_list *rtdcom;
1361 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001362
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001363 for_each_rtdcom(rtd, rtdcom) {
1364 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001365
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001366 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001367 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001368 if (ret < 0)
1369 return ret;
1370 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001371 }
1372
Stephen Warren62ae68f2012-06-08 12:34:23 -06001373 return 0;
1374}
1375
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001376static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001377{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001378 if (dai->probed ||
1379 dai->driver->probe_order != order)
1380 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001381
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001382 if (dai->driver->probe) {
1383 int ret = dai->driver->probe(dai);
1384 if (ret < 0) {
1385 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1386 dai->name, ret);
1387 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001388 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001389 }
1390
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001391 dai->probed = 1;
1392
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001393 return 0;
1394}
1395
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001396static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1397 struct snd_soc_pcm_runtime *rtd)
1398{
1399 int i, ret = 0;
1400
1401 for (i = 0; i < num_dais; ++i) {
1402 struct snd_soc_dai_driver *drv = dais[i]->driver;
1403
1404 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1405 ret = drv->pcm_new(rtd, dais[i]);
1406 if (ret < 0) {
1407 dev_err(dais[i]->dev,
1408 "ASoC: Failed to bind %s with pcm device\n",
1409 dais[i]->name);
1410 return ret;
1411 }
1412 }
1413
1414 return 0;
1415}
1416
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001417static int soc_link_dai_widgets(struct snd_soc_card *card,
1418 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001419 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001420{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001421 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1422 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301423 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001424 int ret;
1425
Benoit Cousson88bd8702014-07-08 23:19:34 +02001426 if (rtd->num_codecs > 1)
1427 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1428
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001429 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301430 sink = codec_dai->playback_widget;
1431 source = cpu_dai->capture_widget;
1432 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001433 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301434 dai_link->num_params,
1435 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001436 if (ret != 0) {
1437 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301438 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001439 return ret;
1440 }
1441 }
1442
Vinod Koul3de7c422015-11-09 23:19:58 +05301443 sink = cpu_dai->playback_widget;
1444 source = codec_dai->capture_widget;
1445 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001446 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301447 dai_link->num_params,
1448 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001449 if (ret != 0) {
1450 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301451 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001452 return ret;
1453 }
1454 }
1455
1456 return 0;
1457}
1458
Mengdong Lin1a497982015-11-18 02:34:11 -05001459static int soc_probe_link_dais(struct snd_soc_card *card,
1460 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001461{
Mengdong Lin1a497982015-11-18 02:34:11 -05001462 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001463 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown291bfb92018-04-19 12:14:10 +01001464 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001465
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001466 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001467 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001469 /* set default power off timeout */
1470 rtd->pmdown_time = pmdown_time;
1471
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001472 ret = soc_probe_dai(cpu_dai, order);
1473 if (ret)
1474 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001477 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001478 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001479 if (ret)
1480 return ret;
1481 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001482
Liam Girdwood0168bf02011-06-07 16:08:05 +01001483 /* complete DAI probe during last probe */
1484 if (order != SND_SOC_COMP_ORDER_LAST)
1485 return 0;
1486
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001487 /* do machine specific initialization */
1488 if (dai_link->init) {
1489 ret = dai_link->init(rtd);
1490 if (ret < 0) {
1491 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1492 dai_link->name, ret);
1493 return ret;
1494 }
1495 }
1496
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001497 if (dai_link->dai_fmt)
1498 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1499
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001500 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001501 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001504#ifdef CONFIG_DEBUG_FS
1505 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001506 if (dai_link->dynamic)
1507 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001508#endif
1509
Jie Yang6f0c4222015-10-13 23:41:00 +08001510 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301511 /*create compress_device"*/
Mark Brown291bfb92018-04-19 12:14:10 +01001512 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
Mark Brownc74184e2012-04-04 22:12:09 +01001513 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001514 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301515 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001516 return ret;
1517 }
1518 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301519
1520 if (!dai_link->params) {
1521 /* create the pcm */
Mark Brown291bfb92018-04-19 12:14:10 +01001522 ret = soc_new_pcm(rtd, rtd->num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301523 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001524 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301525 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001526 return ret;
1527 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001528 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1529 if (ret < 0)
1530 return ret;
1531 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1532 rtd->num_codecs, rtd);
1533 if (ret < 0)
1534 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301535 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001536 INIT_DELAYED_WORK(&rtd->delayed_work,
1537 codec2codec_close_delayed_work);
1538
Namarta Kohli1245b702012-08-16 17:10:41 +05301539 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001540 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001541 if (ret)
1542 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001543 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001544 }
1545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001546 return 0;
1547}
1548
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001549static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001550{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001551 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001552 struct snd_soc_component *component;
1553 const char *name;
1554 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001555
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001556 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1557 /* codecs, usually analog devices */
1558 name = aux_dev->codec_name;
1559 codec_of_node = aux_dev->codec_of_node;
1560 component = soc_find_component(codec_of_node, name);
1561 if (!component) {
1562 if (codec_of_node)
1563 name = of_node_full_name(codec_of_node);
1564 goto err_defer;
1565 }
1566 } else if (aux_dev->name) {
1567 /* generic components */
1568 name = aux_dev->name;
1569 component = soc_find_component(NULL, name);
1570 if (!component)
1571 goto err_defer;
1572 } else {
1573 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1574 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001575 }
1576
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001577 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001578 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001579
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001580 return 0;
1581
1582err_defer:
1583 dev_err(card->dev, "ASoC: %s not registered\n", name);
1584 return -EPROBE_DEFER;
1585}
1586
1587static int soc_probe_aux_devices(struct snd_soc_card *card)
1588{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001589 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001590 int order;
1591 int ret;
1592
1593 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1594 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001595 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001596 if (comp->driver->probe_order == order) {
1597 ret = soc_probe_component(card, comp);
1598 if (ret < 0) {
1599 dev_err(card->dev,
1600 "ASoC: failed to probe aux component %s %d\n",
1601 comp->name, ret);
1602 return ret;
1603 }
1604 }
1605 }
1606 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001607
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001608 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001609}
1610
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001611static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001612{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001613 struct snd_soc_component *comp, *_comp;
1614 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001615
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001616 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1617 order++) {
1618 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001619 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001620
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001621 if (comp->driver->remove_order == order) {
1622 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001623 /* remove it from the card's aux_comp_list */
1624 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001625 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001626 }
1627 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001628}
1629
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001630/**
1631 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1632 * @rtd: The runtime for which the DAI link format should be changed
1633 * @dai_fmt: The new DAI link format
1634 *
1635 * This function updates the DAI link format for all DAIs connected to the DAI
1636 * link for the specified runtime.
1637 *
1638 * Note: For setups with a static format set the dai_fmt field in the
1639 * corresponding snd_dai_link struct instead of using this function.
1640 *
1641 * Returns 0 on success, otherwise a negative error code.
1642 */
1643int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1644 unsigned int dai_fmt)
1645{
1646 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1647 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1648 unsigned int i;
1649 int ret;
1650
1651 for (i = 0; i < rtd->num_codecs; i++) {
1652 struct snd_soc_dai *codec_dai = codec_dais[i];
1653
1654 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1655 if (ret != 0 && ret != -ENOTSUPP) {
1656 dev_warn(codec_dai->dev,
1657 "ASoC: Failed to set DAI format: %d\n", ret);
1658 return ret;
1659 }
1660 }
1661
1662 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001663 /* the component which has non_legacy_dai_naming is Codec */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001664 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001665 unsigned int inv_dai_fmt;
1666
1667 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1668 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1669 case SND_SOC_DAIFMT_CBM_CFM:
1670 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1671 break;
1672 case SND_SOC_DAIFMT_CBM_CFS:
1673 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1674 break;
1675 case SND_SOC_DAIFMT_CBS_CFM:
1676 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1677 break;
1678 case SND_SOC_DAIFMT_CBS_CFS:
1679 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1680 break;
1681 }
1682
1683 dai_fmt = inv_dai_fmt;
1684 }
1685
1686 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1687 if (ret != 0 && ret != -ENOTSUPP) {
1688 dev_warn(cpu_dai->dev,
1689 "ASoC: Failed to set DAI format: %d\n", ret);
1690 return ret;
1691 }
1692
1693 return 0;
1694}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001695EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001696
Liam Girdwood345233d2017-01-14 16:13:02 +08001697
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001698#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001699/* Trim special characters, and replace '-' with '_' since '-' is used to
1700 * separate different DMI fields in the card long name. Only number and
1701 * alphabet characters and a few separator characters are kept.
1702 */
1703static void cleanup_dmi_name(char *name)
1704{
1705 int i, j = 0;
1706
1707 for (i = 0; name[i]; i++) {
1708 if (isalnum(name[i]) || (name[i] == '.')
1709 || (name[i] == '_'))
1710 name[j++] = name[i];
1711 else if (name[i] == '-')
1712 name[j++] = '_';
1713 }
1714
1715 name[j] = '\0';
1716}
1717
Mengdong Lin98faf432017-06-28 15:01:39 +08001718/* Check if a DMI field is valid, i.e. not containing any string
1719 * in the black list.
1720 */
1721static int is_dmi_valid(const char *field)
1722{
1723 int i = 0;
1724
1725 while (dmi_blacklist[i]) {
1726 if (strstr(field, dmi_blacklist[i]))
1727 return 0;
1728 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001729 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001730
1731 return 1;
1732}
1733
Liam Girdwood345233d2017-01-14 16:13:02 +08001734/**
1735 * snd_soc_set_dmi_name() - Register DMI names to card
1736 * @card: The card to register DMI names
1737 * @flavour: The flavour "differentiator" for the card amongst its peers.
1738 *
1739 * An Intel machine driver may be used by many different devices but are
1740 * difficult for userspace to differentiate, since machine drivers ususally
1741 * use their own name as the card short name and leave the card long name
1742 * blank. To differentiate such devices and fix bugs due to lack of
1743 * device-specific configurations, this function allows DMI info to be used
1744 * as the sound card long name, in the format of
1745 * "vendor-product-version-board"
1746 * (Character '-' is used to separate different DMI fields here).
1747 * This will help the user space to load the device-specific Use Case Manager
1748 * (UCM) configurations for the card.
1749 *
1750 * Possible card long names may be:
1751 * DellInc.-XPS139343-01-0310JH
1752 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1753 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1754 *
1755 * This function also supports flavoring the card longname to provide
1756 * the extra differentiation, like "vendor-product-version-board-flavor".
1757 *
1758 * We only keep number and alphabet characters and a few separator characters
1759 * in the card long name since UCM in the user space uses the card long names
1760 * as card configuration directory names and AudoConf cannot support special
1761 * charactors like SPACE.
1762 *
1763 * Returns 0 on success, otherwise a negative error code.
1764 */
1765int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1766{
1767 const char *vendor, *product, *product_version, *board;
1768 size_t longname_buf_size = sizeof(card->snd_card->longname);
1769 size_t len;
1770
1771 if (card->long_name)
1772 return 0; /* long name already set by driver or from DMI */
1773
1774 /* make up dmi long name as: vendor.product.version.board */
1775 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001776 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001777 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1778 return 0;
1779 }
1780
Mengdong Lin98faf432017-06-28 15:01:39 +08001781
Liam Girdwood345233d2017-01-14 16:13:02 +08001782 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1783 "%s", vendor);
1784 cleanup_dmi_name(card->dmi_longname);
1785
1786 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001787 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001788 len = strlen(card->dmi_longname);
1789 snprintf(card->dmi_longname + len,
1790 longname_buf_size - len,
1791 "-%s", product);
1792
1793 len++; /* skip the separator "-" */
1794 if (len < longname_buf_size)
1795 cleanup_dmi_name(card->dmi_longname + len);
1796
1797 /* some vendors like Lenovo may only put a self-explanatory
1798 * name in the product version field
1799 */
1800 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001801 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001802 len = strlen(card->dmi_longname);
1803 snprintf(card->dmi_longname + len,
1804 longname_buf_size - len,
1805 "-%s", product_version);
1806
1807 len++;
1808 if (len < longname_buf_size)
1809 cleanup_dmi_name(card->dmi_longname + len);
1810 }
1811 }
1812
1813 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001814 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001815 len = strlen(card->dmi_longname);
1816 snprintf(card->dmi_longname + len,
1817 longname_buf_size - len,
1818 "-%s", board);
1819
1820 len++;
1821 if (len < longname_buf_size)
1822 cleanup_dmi_name(card->dmi_longname + len);
1823 } else if (!product) {
1824 /* fall back to using legacy name */
1825 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1826 return 0;
1827 }
1828
1829 /* Add flavour to dmi long name */
1830 if (flavour) {
1831 len = strlen(card->dmi_longname);
1832 snprintf(card->dmi_longname + len,
1833 longname_buf_size - len,
1834 "-%s", flavour);
1835
1836 len++;
1837 if (len < longname_buf_size)
1838 cleanup_dmi_name(card->dmi_longname + len);
1839 }
1840
1841 /* set the card long name */
1842 card->long_name = card->dmi_longname;
1843
1844 return 0;
1845}
1846EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001847#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08001848
Mark Brownb19e6e72012-03-14 21:18:39 +00001849static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850{
Mengdong Lin1a497982015-11-18 02:34:11 -05001851 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08001852 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001853 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001854
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001855 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001856 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001857
Mark Brownb19e6e72012-03-14 21:18:39 +00001858 /* bind DAIs */
1859 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001860 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00001861 if (ret != 0)
1862 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001863 }
1864
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001865 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001866 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001867 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001868 if (ret != 0)
1869 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870 }
1871
Mengdong Linf8f80362015-12-02 14:11:22 +08001872 /* add predefined DAI links to the list */
1873 for (i = 0; i < card->num_links; i++)
1874 snd_soc_add_dai_link(card, card->dai_link+i);
1875
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001876 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001877 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001878 card->owner, 0, &card->snd_card);
1879 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001880 dev_err(card->dev,
1881 "ASoC: can't create sound card for card %s: %d\n",
1882 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001883 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001884 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001885
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001886 soc_init_card_debugfs(card);
1887
Mark Browne37a4972011-03-02 18:21:57 +00001888 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1889 card->dapm.dev = card->dev;
1890 card->dapm.card = card;
1891 list_add(&card->dapm.list, &card->dapm_list);
1892
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001893#ifdef CONFIG_DEBUG_FS
1894 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1895#endif
1896
Mark Brown88ee1c62011-02-02 10:43:26 +00001897#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001898 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001899 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001900#endif
Andy Green6ed25972008-06-13 16:24:05 +01001901
Mark Brown9a841eb2011-04-12 17:51:37 -07001902 if (card->dapm_widgets)
1903 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1904 card->num_dapm_widgets);
1905
Nicolin Chenf23e8602015-02-14 17:22:49 -08001906 if (card->of_dapm_widgets)
1907 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1908 card->num_of_dapm_widgets);
1909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910 /* initialise the sound card only once */
1911 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001912 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913 if (ret < 0)
1914 goto card_probe_error;
1915 }
1916
Stephen Warren62ae68f2012-06-08 12:34:23 -06001917 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001918 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1919 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001920 list_for_each_entry(rtd, &card->rtd_list, list) {
1921 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001922 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001923 dev_err(card->dev,
1924 "ASoC: failed to instantiate card %d\n",
1925 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001926 goto probe_dai_err;
1927 }
1928 }
1929 }
1930
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001931 /* probe auxiliary components */
1932 ret = soc_probe_aux_devices(card);
1933 if (ret < 0)
1934 goto probe_dai_err;
1935
Mengdong Lin61b00882015-12-02 14:11:48 +08001936 /* Find new DAI links added during probing components and bind them.
1937 * Components with topology may bring new DAIs and DAI links.
1938 */
1939 list_for_each_entry(dai_link, &card->dai_link_list, list) {
1940 if (soc_is_dai_link_bound(card, dai_link))
1941 continue;
1942
1943 ret = soc_init_dai_link(card, dai_link);
1944 if (ret)
1945 goto probe_dai_err;
1946 ret = soc_bind_dai_link(card, dai_link);
1947 if (ret)
1948 goto probe_dai_err;
1949 }
1950
Stephen Warren62ae68f2012-06-08 12:34:23 -06001951 /* probe all DAI links on this card */
1952 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1953 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001954 list_for_each_entry(rtd, &card->rtd_list, list) {
1955 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001956 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001957 dev_err(card->dev,
1958 "ASoC: failed to instantiate card %d\n",
1959 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001960 goto probe_dai_err;
1961 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001962 }
1963 }
1964
Mark Brown888df392012-02-16 19:37:51 -08001965 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001966 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001967
Mark Brownb7af1da2011-04-07 19:18:44 +09001968 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001969 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001970
Mark Brownb8ad29d2011-03-02 18:35:51 +00001971 if (card->dapm_routes)
1972 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1973 card->num_dapm_routes);
1974
Nicolin Chenf23e8602015-02-14 17:22:49 -08001975 if (card->of_dapm_routes)
1976 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1977 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01001978
Takashi Iwai861886d2017-04-24 08:54:42 +02001979 /* try to set some sane longname if DMI is available */
1980 snd_soc_set_dmi_name(card, NULL);
1981
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001984 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1985 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001986 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1987 "%s", card->driver_name ? card->driver_name : card->name);
1988 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1989 switch (card->snd_card->driver[i]) {
1990 case '_':
1991 case '-':
1992 case '\0':
1993 break;
1994 default:
1995 if (!isalnum(card->snd_card->driver[i]))
1996 card->snd_card->driver[i] = '_';
1997 break;
1998 }
1999 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000
Mark Brown28e9ad92011-03-02 18:36:34 +00002001 if (card->late_probe) {
2002 ret = card->late_probe(card);
2003 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002004 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002005 card->name, ret);
2006 goto probe_aux_dev_err;
2007 }
2008 }
2009
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002010 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002011
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012 ret = snd_card_register(card->snd_card);
2013 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002014 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2015 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002016 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002017 }
2018
Mark Brown435c5e22008-12-04 15:32:53 +00002019 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002020 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002021 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002022 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002023
2024 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002025
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002026probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002027 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002028
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002029probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002030 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002032card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002033 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002034 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002035
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002036 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002037 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002038 snd_card_free(card->snd_card);
2039
Mark Brownb19e6e72012-03-14 21:18:39 +00002040base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002041 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002042 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002043 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002044
Mark Brownb19e6e72012-03-14 21:18:39 +00002045 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002046}
2047
2048/* probes a new socdev */
2049static int soc_probe(struct platform_device *pdev)
2050{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002051 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002052
Vinod Koul70a7ca32011-01-14 19:22:48 +05302053 /*
2054 * no card, so machine driver should be registering card
2055 * we should not be here in that case so ret error
2056 */
2057 if (!card)
2058 return -EINVAL;
2059
Mark Brownfe4085e2012-03-02 13:07:41 +00002060 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002061 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002062 card->name);
2063
Mark Brown435c5e22008-12-04 15:32:53 +00002064 /* Bodge while we unpick instantiation */
2065 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002066
Mark Brown28d528c2012-08-09 18:45:23 +01002067 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002068}
2069
Vinod Koulb0e26482011-01-13 22:48:02 +05302070static int soc_cleanup_card_resources(struct snd_soc_card *card)
2071{
Mengdong Lin1a497982015-11-18 02:34:11 -05002072 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302073
2074 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002075 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002076 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302077
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002078 /* free the ALSA card at first; this syncs with pending operations */
2079 snd_card_free(card->snd_card);
2080
Vinod Koulb0e26482011-01-13 22:48:02 +05302081 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002082 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002083 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302084
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002085 /* remove auxiliary devices */
2086 soc_remove_aux_devices(card);
2087
Mark Brownd1e81422016-08-18 19:32:59 +01002088 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302089 soc_cleanup_card_debugfs(card);
2090
2091 /* remove the card */
2092 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002093 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302094
Vinod Koulb0e26482011-01-13 22:48:02 +05302095 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302096}
2097
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098/* removes a socdev */
2099static int soc_remove(struct platform_device *pdev)
2100{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002101 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002102
Mark Brownc5af3a22008-11-28 13:29:45 +00002103 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104 return 0;
2105}
2106
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002107int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002108{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002109 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002110 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002111
2112 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002113 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002114
2115 /* Flush out pmdown_time work - we actually do want to run it
2116 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002117 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002118 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002120 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002121
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002122 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002123 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002124 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002125 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002126
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002127 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002128 for (i = 0; i < rtd->num_codecs; i++) {
2129 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002130 pinctrl_pm_select_sleep_state(codec_dai->dev);
2131 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002132 }
2133
Mark Brown416356f2009-06-30 19:05:15 +01002134 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002135}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002136EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002137
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002138const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302139 .suspend = snd_soc_suspend,
2140 .resume = snd_soc_resume,
2141 .freeze = snd_soc_suspend,
2142 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002143 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302144 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002145};
Stephen Warrendeb26072011-04-05 19:35:30 -06002146EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002147
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148/* ASoC platform driver */
2149static struct platform_driver soc_driver = {
2150 .driver = {
2151 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002152 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153 },
2154 .probe = soc_probe,
2155 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002156};
2157
Mark Brown096e49d2009-07-05 15:12:22 +01002158/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159 * snd_soc_cnew - create new control
2160 * @_template: control template
2161 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002162 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002163 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002164 *
2165 * Create a new mixer control from a template control.
2166 *
2167 * Returns 0 for success, else error.
2168 */
2169struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002170 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002171 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002172{
2173 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002174 struct snd_kcontrol *kcontrol;
2175 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176
2177 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 template.index = 0;
2179
Mark Brownefb7ac32011-03-08 17:23:24 +00002180 if (!long_name)
2181 long_name = template.name;
2182
2183 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002184 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002185 if (!name)
2186 return NULL;
2187
Mark Brownefb7ac32011-03-08 17:23:24 +00002188 template.name = name;
2189 } else {
2190 template.name = long_name;
2191 }
2192
2193 kcontrol = snd_ctl_new1(&template, data);
2194
2195 kfree(name);
2196
2197 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198}
2199EXPORT_SYMBOL_GPL(snd_soc_cnew);
2200
Liam Girdwood022658b2012-02-03 17:43:09 +00002201static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2202 const struct snd_kcontrol_new *controls, int num_controls,
2203 const char *prefix, void *data)
2204{
2205 int err, i;
2206
2207 for (i = 0; i < num_controls; i++) {
2208 const struct snd_kcontrol_new *control = &controls[i];
2209 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2210 control->name, prefix));
2211 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002212 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2213 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002214 return err;
2215 }
2216 }
2217
2218 return 0;
2219}
2220
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002221struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2222 const char *name)
2223{
2224 struct snd_card *card = soc_card->snd_card;
2225 struct snd_kcontrol *kctl;
2226
2227 if (unlikely(!name))
2228 return NULL;
2229
2230 list_for_each_entry(kctl, &card->controls, list)
2231 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2232 return kctl;
2233 return NULL;
2234}
2235EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2236
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002237/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002238 * snd_soc_add_component_controls - Add an array of controls to a component.
2239 *
2240 * @component: Component to add controls to
2241 * @controls: Array of controls to add
2242 * @num_controls: Number of elements in the array
2243 *
2244 * Return: 0 for success, else error.
2245 */
2246int snd_soc_add_component_controls(struct snd_soc_component *component,
2247 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2248{
2249 struct snd_card *card = component->card->snd_card;
2250
2251 return snd_soc_add_controls(card, component->dev, controls,
2252 num_controls, component->name_prefix, component);
2253}
2254EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2255
2256/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002257 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2258 * Convenience function to add a list of controls.
2259 *
2260 * @soc_card: SoC card to add controls to
2261 * @controls: array of controls to add
2262 * @num_controls: number of elements in the array
2263 *
2264 * Return 0 for success, else error.
2265 */
2266int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2267 const struct snd_kcontrol_new *controls, int num_controls)
2268{
2269 struct snd_card *card = soc_card->snd_card;
2270
2271 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2272 NULL, soc_card);
2273}
2274EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2275
2276/**
2277 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2278 * Convienience function to add a list of controls.
2279 *
2280 * @dai: DAI to add controls to
2281 * @controls: array of controls to add
2282 * @num_controls: number of elements in the array
2283 *
2284 * Return 0 for success, else error.
2285 */
2286int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2287 const struct snd_kcontrol_new *controls, int num_controls)
2288{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002289 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002290
2291 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2292 NULL, dai);
2293}
2294EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2295
2296/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002297 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2298 * @dai: DAI
2299 * @clk_id: DAI specific clock ID
2300 * @freq: new clock frequency in Hz
2301 * @dir: new clock direction - input/output.
2302 *
2303 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2304 */
2305int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2306 unsigned int freq, int dir)
2307{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002308 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002309 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002310
2311 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2312 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002313}
2314EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2315
2316/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002317 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2318 * @component: COMPONENT
2319 * @clk_id: DAI specific clock ID
2320 * @source: Source for the clock
2321 * @freq: new clock frequency in Hz
2322 * @dir: new clock direction - input/output.
2323 *
2324 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2325 */
2326int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2327 int source, unsigned int freq, int dir)
2328{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002329 if (component->driver->set_sysclk)
2330 return component->driver->set_sysclk(component, clk_id, source,
2331 freq, dir);
2332
2333 return -ENOTSUPP;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2336
2337/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002338 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2339 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002340 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002341 * @div: new clock divisor.
2342 *
2343 * Configures the clock dividers. This is used to derive the best DAI bit and
2344 * frame clocks from the system or master clock. It's best to set the DAI bit
2345 * and frame clocks as low as possible to save system power.
2346 */
2347int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2348 int div_id, int div)
2349{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002350 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002351 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002352 else
2353 return -EINVAL;
2354}
2355EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2356
2357/**
2358 * snd_soc_dai_set_pll - configure DAI PLL.
2359 * @dai: DAI
2360 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002361 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002362 * @freq_in: PLL input clock frequency in Hz
2363 * @freq_out: requested PLL output clock frequency in Hz
2364 *
2365 * Configures and enables PLL to generate output clock based on input clock.
2366 */
Mark Brown85488032009-09-05 18:52:16 +01002367int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2368 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002369{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002370 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002371 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002372 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002373
2374 return snd_soc_component_set_pll(dai->component, pll_id, source,
2375 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002376}
2377EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2378
Mark Brownec4ee522011-03-07 20:58:11 +00002379/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002380 * snd_soc_component_set_pll - configure component PLL.
2381 * @component: COMPONENT
2382 * @pll_id: DAI specific PLL ID
2383 * @source: DAI specific source for the PLL
2384 * @freq_in: PLL input clock frequency in Hz
2385 * @freq_out: requested PLL output clock frequency in Hz
2386 *
2387 * Configures and enables PLL to generate output clock based on input clock.
2388 */
2389int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2390 int source, unsigned int freq_in,
2391 unsigned int freq_out)
2392{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002393 if (component->driver->set_pll)
2394 return component->driver->set_pll(component, pll_id, source,
2395 freq_in, freq_out);
2396
2397 return -EINVAL;
2398}
2399EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2400
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002401/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002402 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2403 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002404 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002405 *
2406 * Configures the DAI for a preset BCLK to sample rate ratio.
2407 */
2408int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2409{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002410 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002411 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2412 else
2413 return -EINVAL;
2414}
2415EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2416
2417/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002418 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2419 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002420 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002421 *
2422 * Configures the DAI hardware format and clocking.
2423 */
2424int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2425{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002426 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002427 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002428 if (dai->driver->ops->set_fmt == NULL)
2429 return -ENOTSUPP;
2430 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002431}
2432EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2433
2434/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002435 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002436 * @slots: Number of slots in use.
2437 * @tx_mask: bitmask representing active TX slots.
2438 * @rx_mask: bitmask representing active RX slots.
2439 *
2440 * Generates the TDM tx and rx slot default masks for DAI.
2441 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002442static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002443 unsigned int *tx_mask,
2444 unsigned int *rx_mask)
2445{
2446 if (*tx_mask || *rx_mask)
2447 return 0;
2448
2449 if (!slots)
2450 return -EINVAL;
2451
2452 *tx_mask = (1 << slots) - 1;
2453 *rx_mask = (1 << slots) - 1;
2454
2455 return 0;
2456}
2457
2458/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002459 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2460 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002461 * @tx_mask: bitmask representing active TX slots.
2462 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002463 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002464 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002465 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002466 * This function configures the specified DAI for TDM operation. @slot contains
2467 * the total number of slots of the TDM stream and @slot_with the width of each
2468 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2469 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2470 * DAI should write to or read from. If a bit is set the corresponding slot is
2471 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2472 * the first slot, bit 1 to the second slot and so on. The first active slot
2473 * maps to the first channel of the DAI, the second active slot to the second
2474 * channel and so on.
2475 *
2476 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2477 * @rx_mask and @slot_width will be ignored.
2478 *
2479 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002480 */
2481int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002482 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002483{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002484 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002485 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002486 &tx_mask, &rx_mask);
2487 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002488 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002489
Benoit Cousson88bd8702014-07-08 23:19:34 +02002490 dai->tx_mask = tx_mask;
2491 dai->rx_mask = rx_mask;
2492
Kuninori Morimoto46471922017-09-25 01:38:34 +00002493 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002494 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002495 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002496 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002497 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002498}
2499EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2500
2501/**
Barry Song472df3c2009-09-12 01:16:29 +08002502 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2503 * @dai: DAI
2504 * @tx_num: how many TX channels
2505 * @tx_slot: pointer to an array which imply the TX slot number channel
2506 * 0~num-1 uses
2507 * @rx_num: how many RX channels
2508 * @rx_slot: pointer to an array which imply the RX slot number channel
2509 * 0~num-1 uses
2510 *
2511 * configure the relationship between channel number and TDM slot number.
2512 */
2513int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2514 unsigned int tx_num, unsigned int *tx_slot,
2515 unsigned int rx_num, unsigned int *rx_slot)
2516{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002517 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002518 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002519 rx_num, rx_slot);
2520 else
2521 return -EINVAL;
2522}
2523EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2524
2525/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002526 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2527 * @dai: DAI
2528 * @tristate: tristate enable
2529 *
2530 * Tristates the DAI so that others can use it.
2531 */
2532int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2533{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002534 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002535 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002536 else
2537 return -EINVAL;
2538}
2539EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2540
2541/**
2542 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2543 * @dai: DAI
2544 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002545 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002546 *
2547 * Mutes the DAI DAC.
2548 */
Mark Brownda183962013-02-06 15:44:07 +00002549int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2550 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002551{
Mark Brownda183962013-02-06 15:44:07 +00002552 if (!dai->driver)
2553 return -ENOTSUPP;
2554
2555 if (dai->driver->ops->mute_stream)
2556 return dai->driver->ops->mute_stream(dai, mute, direction);
2557 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2558 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002559 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002560 else
Mark Brown04570c62012-04-13 19:16:03 +01002561 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002562}
2563EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2564
Mark Brownc5af3a22008-11-28 13:29:45 +00002565/**
2566 * snd_soc_register_card - Register a card with the ASoC core
2567 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002568 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002569 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002570 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302571int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002572{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002573 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002574 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002575
Mark Brownc5af3a22008-11-28 13:29:45 +00002576 if (!card->name || !card->dev)
2577 return -EINVAL;
2578
Stephen Warren5a504962011-12-21 10:40:59 -07002579 for (i = 0; i < card->num_links; i++) {
2580 struct snd_soc_dai_link *link = &card->dai_link[i];
2581
Mengdong Lin923c5e612015-11-23 11:01:49 -05002582 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002583 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002584 dev_err(card->dev, "ASoC: failed to init link %s\n",
2585 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002586 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002587 }
Stephen Warren5a504962011-12-21 10:40:59 -07002588 }
2589
Mark Browned77cc12011-05-03 18:25:34 +01002590 dev_set_drvdata(card->dev, card);
2591
Stephen Warren111c6412011-01-28 14:26:35 -07002592 snd_soc_initialize_card_lists(card);
2593
Mengdong Linf8f80362015-12-02 14:11:22 +08002594 INIT_LIST_HEAD(&card->dai_link_list);
2595 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002596
Mengdong Lin1a497982015-11-18 02:34:11 -05002597 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002598 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599
Mark Browndb432b42011-10-03 21:06:40 +01002600 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002601 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002602 card->instantiated = 0;
2603 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002604 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605
Mark Brownb19e6e72012-03-14 21:18:39 +00002606 ret = snd_soc_instantiate_card(card);
2607 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002608 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002610 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002611 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002612 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2613 int j;
2614
2615 for (j = 0; j < rtd->num_codecs; j++) {
2616 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2617 if (!codec_dai->active)
2618 pinctrl_pm_select_sleep_state(codec_dai->dev);
2619 }
2620
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002621 if (!cpu_dai->active)
2622 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2623 }
2624
Mark Brownb19e6e72012-03-14 21:18:39 +00002625 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302627EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628
2629/**
2630 * snd_soc_unregister_card - Unregister a card with the ASoC core
2631 *
2632 * @card: Card to unregister
2633 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002634 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302635int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002637 if (card->instantiated) {
2638 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002639 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302640 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002641 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002642 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643
2644 return 0;
2645}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302646EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002647
2648/*
2649 * Simplify DAI link configuration by removing ".-1" from device names
2650 * and sanitizing names.
2651 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002652static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002653{
2654 char *found, name[NAME_SIZE];
2655 int id1, id2;
2656
2657 if (dev_name(dev) == NULL)
2658 return NULL;
2659
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002660 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002661
2662 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002663 found = strstr(name, dev->driver->name);
2664 if (found) {
2665 /* get ID */
2666 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2667
2668 /* discard ID from name if ID == -1 */
2669 if (*id == -1)
2670 found[strlen(dev->driver->name)] = '\0';
2671 }
2672
2673 } else {
2674 /* I2C component devices are named "bus-addr" */
2675 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2676 char tmp[NAME_SIZE];
2677
2678 /* create unique ID number from I2C addr and bus */
2679 *id = ((id1 & 0xffff) << 16) + id2;
2680
2681 /* sanitize component name for DAI link creation */
2682 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002683 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002684 } else
2685 *id = 0;
2686 }
2687
2688 return kstrdup(name, GFP_KERNEL);
2689}
2690
2691/*
2692 * Simplify DAI link naming for single devices with multiple DAIs by removing
2693 * any ".-1" and using the DAI name (instead of device name).
2694 */
2695static inline char *fmt_multiple_name(struct device *dev,
2696 struct snd_soc_dai_driver *dai_drv)
2697{
2698 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002699 dev_err(dev,
2700 "ASoC: error - multiple DAI %s registered with no name\n",
2701 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002702 return NULL;
2703 }
2704
2705 return kstrdup(dai_drv->name, GFP_KERNEL);
2706}
2707
2708/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002709 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002710 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002711 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002712 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002713static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002714{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002715 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002716
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002717 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002718 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2719 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002720 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002721 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002722 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002723 }
Mark Brown91151712008-11-30 23:31:24 +00002724}
Mark Brown91151712008-11-30 23:31:24 +00002725
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002726/* Create a DAI and add it to the component's DAI list */
2727static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2728 struct snd_soc_dai_driver *dai_drv,
2729 bool legacy_dai_naming)
2730{
2731 struct device *dev = component->dev;
2732 struct snd_soc_dai *dai;
2733
2734 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2735
2736 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2737 if (dai == NULL)
2738 return NULL;
2739
2740 /*
2741 * Back in the old days when we still had component-less DAIs,
2742 * instead of having a static name, component-less DAIs would
2743 * inherit the name of the parent device so it is possible to
2744 * register multiple instances of the DAI. We still need to keep
2745 * the same naming style even though those DAIs are not
2746 * component-less anymore.
2747 */
2748 if (legacy_dai_naming &&
2749 (dai_drv->id == 0 || dai_drv->name == NULL)) {
2750 dai->name = fmt_single_name(dev, &dai->id);
2751 } else {
2752 dai->name = fmt_multiple_name(dev, dai_drv);
2753 if (dai_drv->id)
2754 dai->id = dai_drv->id;
2755 else
2756 dai->id = component->num_dai;
2757 }
2758 if (dai->name == NULL) {
2759 kfree(dai);
2760 return NULL;
2761 }
2762
2763 dai->component = component;
2764 dai->dev = dev;
2765 dai->driver = dai_drv;
2766 if (!dai->driver->ops)
2767 dai->driver->ops = &null_dai_ops;
2768
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00002769 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002770 component->num_dai++;
2771
2772 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2773 return dai;
2774}
2775
Mark Brown91151712008-11-30 23:31:24 +00002776/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002777 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002778 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002779 * @component: The component the DAIs are registered for
2780 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002781 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002782 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2783 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002784 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002785static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002786 struct snd_soc_dai_driver *dai_drv, size_t count,
2787 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00002788{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002789 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002790 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002791 unsigned int i;
2792 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002793
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002794 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002795
2796 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002797
Mengdong Lin5e4fb372015-12-31 16:40:20 +08002798 dai = soc_add_dai(component, dai_drv + i,
2799 count == 1 && legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08002800 if (dai == NULL) {
2801 ret = -ENOMEM;
2802 goto err;
2803 }
Mark Brown91151712008-11-30 23:31:24 +00002804 }
2805
2806 return 0;
2807
2808err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002809 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002810
2811 return ret;
2812}
Mark Brown91151712008-11-30 23:31:24 +00002813
Mengdong Lin68003e62015-12-31 16:40:43 +08002814/**
2815 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2816 *
2817 * @component: The component the DAIs are registered for
2818 * @dai_drv: DAI driver to use for the DAI
2819 *
2820 * Topology can use this API to register DAIs when probing a component.
2821 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2822 * will be freed in the component cleanup.
2823 */
2824int snd_soc_register_dai(struct snd_soc_component *component,
2825 struct snd_soc_dai_driver *dai_drv)
2826{
2827 struct snd_soc_dapm_context *dapm =
2828 snd_soc_component_get_dapm(component);
2829 struct snd_soc_dai *dai;
2830 int ret;
2831
2832 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
2833 dev_err(component->dev, "Invalid dai type %d\n",
2834 dai_drv->dobj.type);
2835 return -EINVAL;
2836 }
2837
2838 lockdep_assert_held(&client_mutex);
2839 dai = soc_add_dai(component, dai_drv, false);
2840 if (!dai)
2841 return -ENOMEM;
2842
2843 /* Create the DAI widgets here. After adding DAIs, topology may
2844 * also add routes that need these widgets as source or sink.
2845 */
2846 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
2847 if (ret != 0) {
2848 dev_err(component->dev,
2849 "Failed to create DAI widgets %d\n", ret);
2850 }
2851
2852 return ret;
2853}
2854EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2855
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002856static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2857 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002858{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002859 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002860
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002861 component->driver->seq_notifier(component, type, subseq);
2862}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002863
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002864static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2865 int event)
2866{
2867 struct snd_soc_component *component = dapm->component;
2868
2869 return component->driver->stream_event(component, event);
2870}
2871
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002872static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
2873 enum snd_soc_bias_level level)
2874{
2875 struct snd_soc_component *component = dapm->component;
2876
2877 return component->driver->set_bias_level(component, level);
2878}
2879
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002880static int snd_soc_component_initialize(struct snd_soc_component *component,
2881 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002882{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002883 struct snd_soc_dapm_context *dapm;
2884
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002885 component->name = fmt_single_name(dev, &component->id);
2886 if (!component->name) {
2887 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002888 return -ENOMEM;
2889 }
2890
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002891 component->dev = dev;
2892 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002893
Kuninori Morimoto88c27462017-08-25 01:06:04 +00002894 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002895 dapm->dev = dev;
2896 dapm->component = component;
2897 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002898 dapm->idle_bias_off = !driver->idle_bias_on;
2899 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002900 if (driver->seq_notifier)
2901 dapm->seq_notifier = snd_soc_component_seq_notifier;
2902 if (driver->stream_event)
2903 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00002904 if (driver->set_bias_level)
2905 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002906
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002907 INIT_LIST_HEAD(&component->dai_list);
2908 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002909
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002910 return 0;
2911}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002912
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002913static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002914{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002915 int val_bytes = regmap_get_val_bytes(component->regmap);
2916
2917 /* Errors are legitimate for non-integer byte multiples */
2918 if (val_bytes > 0)
2919 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002920}
2921
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002922#ifdef CONFIG_REGMAP
2923
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002924/**
2925 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2926 * @component: The component for which to initialize the regmap instance
2927 * @regmap: The regmap instance that should be used by the component
2928 *
2929 * This function allows deferred assignment of the regmap instance that is
2930 * associated with the component. Only use this if the regmap instance is not
2931 * yet ready when the component is registered. The function must also be called
2932 * before the first IO attempt of the component.
2933 */
2934void snd_soc_component_init_regmap(struct snd_soc_component *component,
2935 struct regmap *regmap)
2936{
2937 component->regmap = regmap;
2938 snd_soc_component_setup_regmap(component);
2939}
2940EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2941
2942/**
2943 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2944 * @component: The component for which to de-initialize the regmap instance
2945 *
2946 * Calls regmap_exit() on the regmap instance associated to the component and
2947 * removes the regmap instance from the component.
2948 *
2949 * This function should only be used if snd_soc_component_init_regmap() was used
2950 * to initialize the regmap instance.
2951 */
2952void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2953{
2954 regmap_exit(component->regmap);
2955 component->regmap = NULL;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2958
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002959#endif
2960
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002961static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002962{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00002963 mutex_lock(&client_mutex);
2964
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00002965 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002966 if (!component->regmap)
2967 component->regmap = dev_get_regmap(component->dev, NULL);
2968 if (component->regmap)
2969 snd_soc_component_setup_regmap(component);
2970 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002971
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002972 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002973 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002974
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002975 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002976}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002977
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002978static void snd_soc_component_cleanup(struct snd_soc_component *component)
2979{
2980 snd_soc_unregister_dais(component);
2981 kfree(component->name);
2982}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002983
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002984static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2985{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00002986 struct snd_soc_card *card = component->card;
2987
2988 if (card)
2989 snd_soc_unregister_card(card);
2990
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002991 list_del(&component->list);
2992}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002993
Kuninori Morimoto273d7782017-10-11 01:38:29 +00002994#define ENDIANNESS_MAP(name) \
2995 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2996static u64 endianness_format_map[] = {
2997 ENDIANNESS_MAP(S16_),
2998 ENDIANNESS_MAP(U16_),
2999 ENDIANNESS_MAP(S24_),
3000 ENDIANNESS_MAP(U24_),
3001 ENDIANNESS_MAP(S32_),
3002 ENDIANNESS_MAP(U32_),
3003 ENDIANNESS_MAP(S24_3),
3004 ENDIANNESS_MAP(U24_3),
3005 ENDIANNESS_MAP(S20_3),
3006 ENDIANNESS_MAP(U20_3),
3007 ENDIANNESS_MAP(S18_3),
3008 ENDIANNESS_MAP(U18_3),
3009 ENDIANNESS_MAP(FLOAT_),
3010 ENDIANNESS_MAP(FLOAT64_),
3011 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3012};
3013
3014/*
3015 * Fix up the DAI formats for endianness: codecs don't actually see
3016 * the endianness of the data but we're using the CPU format
3017 * definitions which do need to include endianness so we ensure that
3018 * codec DAIs always have both big and little endian variants set.
3019 */
3020static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3021{
3022 int i;
3023
3024 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3025 if (stream->formats & endianness_format_map[i])
3026 stream->formats |= endianness_format_map[i];
3027}
3028
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003029int snd_soc_add_component(struct device *dev,
3030 struct snd_soc_component *component,
3031 const struct snd_soc_component_driver *component_driver,
3032 struct snd_soc_dai_driver *dai_drv,
3033 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003034{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003035 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003036 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003037
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003038 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003039 if (ret)
3040 goto err_free;
3041
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003042 if (component_driver->endianness) {
3043 for (i = 0; i < num_dai; i++) {
3044 convert_endianness_formats(&dai_drv[i].playback);
3045 convert_endianness_formats(&dai_drv[i].capture);
3046 }
3047 }
3048
Kuninori Morimoto69941bab2017-10-11 01:38:51 +00003049 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3050 !component_driver->non_legacy_dai_naming);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003051 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003052 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003053 goto err_cleanup;
3054 }
3055
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003056 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003057
3058 return 0;
3059
3060err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003061 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003062err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003063 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003064}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003065EXPORT_SYMBOL_GPL(snd_soc_add_component);
3066
3067int snd_soc_register_component(struct device *dev,
3068 const struct snd_soc_component_driver *component_driver,
3069 struct snd_soc_dai_driver *dai_drv,
3070 int num_dai)
3071{
3072 struct snd_soc_component *component;
3073
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003074 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003075 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003076 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003077
3078 return snd_soc_add_component(dev, component, component_driver,
3079 dai_drv, num_dai);
3080}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003081EXPORT_SYMBOL_GPL(snd_soc_register_component);
3082
3083/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003084 * snd_soc_unregister_component - Unregister all related component
3085 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003086 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003087 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003088 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003089static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003090{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003091 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003092 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003093
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003094 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003095 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003096 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003097 continue;
3098
3099 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3100 snd_soc_component_del_unlocked(component);
3101 found = 1;
3102 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003103 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003104 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003105
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003106 if (found) {
3107 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003108 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003109
3110 return found;
3111}
3112
3113void snd_soc_unregister_component(struct device *dev)
3114{
3115 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003116}
3117EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3118
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003119struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3120 const char *driver_name)
3121{
3122 struct snd_soc_component *component;
3123 struct snd_soc_component *ret;
3124
3125 ret = NULL;
3126 mutex_lock(&client_mutex);
3127 list_for_each_entry(component, &component_list, list) {
3128 if (dev != component->dev)
3129 continue;
3130
3131 if (driver_name &&
3132 (driver_name != component->driver->name) &&
3133 (strcmp(component->driver->name, driver_name) != 0))
3134 continue;
3135
3136 ret = component;
3137 break;
3138 }
3139 mutex_unlock(&client_mutex);
3140
3141 return ret;
3142}
3143EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3144
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003145/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003146int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3147 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003148{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003149 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003150 int ret;
3151
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303152 if (!card->dev) {
3153 pr_err("card->dev is not set before calling %s\n", __func__);
3154 return -EINVAL;
3155 }
3156
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003157 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303158
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003159 ret = of_property_read_string_index(np, propname, 0, &card->name);
3160 /*
3161 * EINVAL means the property does not exist. This is fine providing
3162 * card->name was previously set, which is checked later in
3163 * snd_soc_register_card.
3164 */
3165 if (ret < 0 && ret != -EINVAL) {
3166 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003167 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003168 propname, ret);
3169 return ret;
3170 }
3171
3172 return 0;
3173}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003174EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003175
Xiubo Li9a6d4862014-02-08 15:59:52 +08003176static const struct snd_soc_dapm_widget simple_widgets[] = {
3177 SND_SOC_DAPM_MIC("Microphone", NULL),
3178 SND_SOC_DAPM_LINE("Line", NULL),
3179 SND_SOC_DAPM_HP("Headphone", NULL),
3180 SND_SOC_DAPM_SPK("Speaker", NULL),
3181};
3182
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003183int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003184 const char *propname)
3185{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003186 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003187 struct snd_soc_dapm_widget *widgets;
3188 const char *template, *wname;
3189 int i, j, num_widgets, ret;
3190
3191 num_widgets = of_property_count_strings(np, propname);
3192 if (num_widgets < 0) {
3193 dev_err(card->dev,
3194 "ASoC: Property '%s' does not exist\n", propname);
3195 return -EINVAL;
3196 }
3197 if (num_widgets & 1) {
3198 dev_err(card->dev,
3199 "ASoC: Property '%s' length is not even\n", propname);
3200 return -EINVAL;
3201 }
3202
3203 num_widgets /= 2;
3204 if (!num_widgets) {
3205 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3206 propname);
3207 return -EINVAL;
3208 }
3209
3210 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3211 GFP_KERNEL);
3212 if (!widgets) {
3213 dev_err(card->dev,
3214 "ASoC: Could not allocate memory for widgets\n");
3215 return -ENOMEM;
3216 }
3217
3218 for (i = 0; i < num_widgets; i++) {
3219 ret = of_property_read_string_index(np, propname,
3220 2 * i, &template);
3221 if (ret) {
3222 dev_err(card->dev,
3223 "ASoC: Property '%s' index %d read error:%d\n",
3224 propname, 2 * i, ret);
3225 return -EINVAL;
3226 }
3227
3228 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3229 if (!strncmp(template, simple_widgets[j].name,
3230 strlen(simple_widgets[j].name))) {
3231 widgets[i] = simple_widgets[j];
3232 break;
3233 }
3234 }
3235
3236 if (j >= ARRAY_SIZE(simple_widgets)) {
3237 dev_err(card->dev,
3238 "ASoC: DAPM widget '%s' is not supported\n",
3239 template);
3240 return -EINVAL;
3241 }
3242
3243 ret = of_property_read_string_index(np, propname,
3244 (2 * i) + 1,
3245 &wname);
3246 if (ret) {
3247 dev_err(card->dev,
3248 "ASoC: Property '%s' index %d read error:%d\n",
3249 propname, (2 * i) + 1, ret);
3250 return -EINVAL;
3251 }
3252
3253 widgets[i].name = wname;
3254 }
3255
Nicolin Chenf23e8602015-02-14 17:22:49 -08003256 card->of_dapm_widgets = widgets;
3257 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003258
3259 return 0;
3260}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003261EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003262
Jyri Sarha61310842015-09-09 21:27:43 +03003263static int snd_soc_of_get_slot_mask(struct device_node *np,
3264 const char *prop_name,
3265 unsigned int *mask)
3266{
3267 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003268 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003269 int i;
3270
3271 if (!of_slot_mask)
3272 return 0;
3273 val /= sizeof(u32);
3274 for (i = 0; i < val; i++)
3275 if (be32_to_cpup(&of_slot_mask[i]))
3276 *mask |= (1 << i);
3277
3278 return val;
3279}
3280
Xiubo Li89c67852014-02-14 09:34:35 +08003281int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003282 unsigned int *tx_mask,
3283 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003284 unsigned int *slots,
3285 unsigned int *slot_width)
3286{
3287 u32 val;
3288 int ret;
3289
Jyri Sarha61310842015-09-09 21:27:43 +03003290 if (tx_mask)
3291 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3292 if (rx_mask)
3293 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3294
Xiubo Li89c67852014-02-14 09:34:35 +08003295 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3296 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3297 if (ret)
3298 return ret;
3299
3300 if (slots)
3301 *slots = val;
3302 }
3303
3304 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3305 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3306 if (ret)
3307 return ret;
3308
3309 if (slot_width)
3310 *slot_width = val;
3311 }
3312
3313 return 0;
3314}
3315EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3316
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003317void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003318 struct snd_soc_codec_conf *codec_conf,
3319 struct device_node *of_node,
3320 const char *propname)
3321{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003322 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003323 const char *str;
3324 int ret;
3325
3326 ret = of_property_read_string(np, propname, &str);
3327 if (ret < 0) {
3328 /* no prefix is not error */
3329 return;
3330 }
3331
3332 codec_conf->of_node = of_node;
3333 codec_conf->name_prefix = str;
3334}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003335EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003336
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003337int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003338 const char *propname)
3339{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003340 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003341 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003342 struct snd_soc_dapm_route *routes;
3343 int i, ret;
3344
3345 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003346 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003347 dev_err(card->dev,
3348 "ASoC: Property '%s' does not exist or its length is not even\n",
3349 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003350 return -EINVAL;
3351 }
3352 num_routes /= 2;
3353 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003354 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003355 propname);
3356 return -EINVAL;
3357 }
3358
Mark Browne3b1e6a2014-12-18 11:46:38 +00003359 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003360 GFP_KERNEL);
3361 if (!routes) {
3362 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003363 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003364 return -EINVAL;
3365 }
3366
3367 for (i = 0; i < num_routes; i++) {
3368 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003369 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003370 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003371 dev_err(card->dev,
3372 "ASoC: Property '%s' index %d could not be read: %d\n",
3373 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003374 return -EINVAL;
3375 }
3376 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003377 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003378 if (ret) {
3379 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003380 "ASoC: Property '%s' index %d could not be read: %d\n",
3381 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003382 return -EINVAL;
3383 }
3384 }
3385
Nicolin Chenf23e8602015-02-14 17:22:49 -08003386 card->num_of_dapm_routes = num_routes;
3387 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003388
3389 return 0;
3390}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003391EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003392
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003393unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003394 const char *prefix,
3395 struct device_node **bitclkmaster,
3396 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003397{
3398 int ret, i;
3399 char prop[128];
3400 unsigned int format = 0;
3401 int bit, frame;
3402 const char *str;
3403 struct {
3404 char *name;
3405 unsigned int val;
3406 } of_fmt_table[] = {
3407 { "i2s", SND_SOC_DAIFMT_I2S },
3408 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3409 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3410 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3411 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3412 { "ac97", SND_SOC_DAIFMT_AC97 },
3413 { "pdm", SND_SOC_DAIFMT_PDM},
3414 { "msb", SND_SOC_DAIFMT_MSB },
3415 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003416 };
3417
3418 if (!prefix)
3419 prefix = "";
3420
3421 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003422 * check "dai-format = xxx"
3423 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003424 * SND_SOC_DAIFMT_FORMAT_MASK area
3425 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003426 ret = of_property_read_string(np, "dai-format", &str);
3427 if (ret < 0) {
3428 snprintf(prop, sizeof(prop), "%sformat", prefix);
3429 ret = of_property_read_string(np, prop, &str);
3430 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003431 if (ret == 0) {
3432 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3433 if (strcmp(str, of_fmt_table[i].name) == 0) {
3434 format |= of_fmt_table[i].val;
3435 break;
3436 }
3437 }
3438 }
3439
3440 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003441 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003442 * SND_SOC_DAIFMT_CLOCK_MASK area
3443 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003444 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003445 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003446 format |= SND_SOC_DAIFMT_CONT;
3447 else
3448 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003449
3450 /*
3451 * check "[prefix]bitclock-inversion"
3452 * check "[prefix]frame-inversion"
3453 * SND_SOC_DAIFMT_INV_MASK area
3454 */
3455 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3456 bit = !!of_get_property(np, prop, NULL);
3457
3458 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3459 frame = !!of_get_property(np, prop, NULL);
3460
3461 switch ((bit << 4) + frame) {
3462 case 0x11:
3463 format |= SND_SOC_DAIFMT_IB_IF;
3464 break;
3465 case 0x10:
3466 format |= SND_SOC_DAIFMT_IB_NF;
3467 break;
3468 case 0x01:
3469 format |= SND_SOC_DAIFMT_NB_IF;
3470 break;
3471 default:
3472 /* SND_SOC_DAIFMT_NB_NF is default */
3473 break;
3474 }
3475
3476 /*
3477 * check "[prefix]bitclock-master"
3478 * check "[prefix]frame-master"
3479 * SND_SOC_DAIFMT_MASTER_MASK area
3480 */
3481 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3482 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003483 if (bit && bitclkmaster)
3484 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003485
3486 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3487 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003488 if (frame && framemaster)
3489 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003490
3491 switch ((bit << 4) + frame) {
3492 case 0x11:
3493 format |= SND_SOC_DAIFMT_CBM_CFM;
3494 break;
3495 case 0x10:
3496 format |= SND_SOC_DAIFMT_CBM_CFS;
3497 break;
3498 case 0x01:
3499 format |= SND_SOC_DAIFMT_CBS_CFM;
3500 break;
3501 default:
3502 format |= SND_SOC_DAIFMT_CBS_CFS;
3503 break;
3504 }
3505
3506 return format;
3507}
3508EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3509
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003510int snd_soc_get_dai_id(struct device_node *ep)
3511{
3512 struct snd_soc_component *pos;
3513 struct device_node *node;
3514 int ret;
3515
3516 node = of_graph_get_port_parent(ep);
3517
3518 /*
3519 * For example HDMI case, HDMI has video/sound port,
3520 * but ALSA SoC needs sound port number only.
3521 * Thus counting HDMI DT port/endpoint doesn't work.
3522 * Then, it should have .of_xlate_dai_id
3523 */
3524 ret = -ENOTSUPP;
3525 mutex_lock(&client_mutex);
3526 list_for_each_entry(pos, &component_list, list) {
3527 struct device_node *component_of_node = pos->dev->of_node;
3528
3529 if (!component_of_node && pos->dev->parent)
3530 component_of_node = pos->dev->parent->of_node;
3531
3532 if (component_of_node != node)
3533 continue;
3534
3535 if (pos->driver->of_xlate_dai_id)
3536 ret = pos->driver->of_xlate_dai_id(pos, ep);
3537
3538 break;
3539 }
3540 mutex_unlock(&client_mutex);
3541
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003542 of_node_put(node);
3543
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003544 return ret;
3545}
3546EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3547
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003548int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003549 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003550{
3551 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003552 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003553 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003554
3555 mutex_lock(&client_mutex);
3556 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003557 component_of_node = pos->dev->of_node;
3558 if (!component_of_node && pos->dev->parent)
3559 component_of_node = pos->dev->parent->of_node;
3560
3561 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003562 continue;
3563
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003564 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003565 ret = pos->driver->of_xlate_dai_name(pos,
3566 args,
3567 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003568 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003569 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003570 int id = -1;
3571
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003572 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003573 case 0:
3574 id = 0; /* same as dai_drv[0] */
3575 break;
3576 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003577 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003578 break;
3579 default:
3580 /* not supported */
3581 break;
3582 }
3583
3584 if (id < 0 || id >= pos->num_dai) {
3585 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003586 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003587 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003588
3589 ret = 0;
3590
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003591 /* find target DAI */
3592 list_for_each_entry(dai, &pos->dai_list, list) {
3593 if (id == 0)
3594 break;
3595 id--;
3596 }
3597
3598 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003599 if (!*dai_name)
3600 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003601 }
3602
Kuninori Morimotocb470082013-09-10 17:39:56 -07003603 break;
3604 }
3605 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003606 return ret;
3607}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003608EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003609
3610int snd_soc_of_get_dai_name(struct device_node *of_node,
3611 const char **dai_name)
3612{
3613 struct of_phandle_args args;
3614 int ret;
3615
3616 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3617 "#sound-dai-cells", 0, &args);
3618 if (ret)
3619 return ret;
3620
3621 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003622
3623 of_node_put(args.np);
3624
3625 return ret;
3626}
3627EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3628
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003629/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003630 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3631 * @dai_link: DAI link
3632 *
3633 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3634 */
3635void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3636{
3637 struct snd_soc_dai_link_component *component = dai_link->codecs;
3638 int index;
3639
3640 for (index = 0; index < dai_link->num_codecs; index++, component++) {
3641 if (!component->of_node)
3642 break;
3643 of_node_put(component->of_node);
3644 component->of_node = NULL;
3645 }
3646}
3647EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3648
3649/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003650 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3651 * @dev: Card device
3652 * @of_node: Device node
3653 * @dai_link: DAI link
3654 *
3655 * Builds an array of CODEC DAI components from the DAI link property
3656 * 'sound-dai'.
3657 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003658 * The device nodes in the array (of_node) must be dereferenced by calling
3659 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003660 *
3661 * Returns 0 for success
3662 */
3663int snd_soc_of_get_dai_link_codecs(struct device *dev,
3664 struct device_node *of_node,
3665 struct snd_soc_dai_link *dai_link)
3666{
3667 struct of_phandle_args args;
3668 struct snd_soc_dai_link_component *component;
3669 char *name;
3670 int index, num_codecs, ret;
3671
3672 /* Count the number of CODECs */
3673 name = "sound-dai";
3674 num_codecs = of_count_phandle_with_args(of_node, name,
3675 "#sound-dai-cells");
3676 if (num_codecs <= 0) {
3677 if (num_codecs == -ENOENT)
3678 dev_err(dev, "No 'sound-dai' property\n");
3679 else
3680 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3681 return num_codecs;
3682 }
3683 component = devm_kzalloc(dev,
3684 sizeof *component * num_codecs,
3685 GFP_KERNEL);
3686 if (!component)
3687 return -ENOMEM;
3688 dai_link->codecs = component;
3689 dai_link->num_codecs = num_codecs;
3690
3691 /* Parse the list */
3692 for (index = 0, component = dai_link->codecs;
3693 index < dai_link->num_codecs;
3694 index++, component++) {
3695 ret = of_parse_phandle_with_args(of_node, name,
3696 "#sound-dai-cells",
3697 index, &args);
3698 if (ret)
3699 goto err;
3700 component->of_node = args.np;
3701 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3702 if (ret < 0)
3703 goto err;
3704 }
3705 return 0;
3706err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003707 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003708 dai_link->codecs = NULL;
3709 dai_link->num_codecs = 0;
3710 return ret;
3711}
3712EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3713
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003714static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003715{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003716 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003717 snd_soc_util_init();
3718
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003719 return platform_driver_register(&soc_driver);
3720}
Mark Brown4abe8e12010-10-12 17:41:03 +01003721module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003722
Mark Brown7d8c16a2008-11-30 22:11:24 +00003723static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003724{
Mark Brownfb257892011-04-28 10:57:54 +01003725 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003726 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003727
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003728 platform_driver_unregister(&soc_driver);
3729}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003730module_exit(snd_soc_exit);
3731
3732/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003733MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003734MODULE_DESCRIPTION("ALSA SoC Core");
3735MODULE_LICENSE("GPL");
3736MODULE_ALIAS("platform:soc-audio");