blob: f86ee4f48f06f0be8fdad5c6127d802793b43acc [file] [log] [blame]
Kuninori Morimoto873486e2018-07-02 06:24:18 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-core.c -- ALSA SoC Audio Layer
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
13//
14// TODO:
15// o Add hw rules to enforce rates, etc.
16// o More testing with other codecs/machines.
17// o Add more codecs and platforms to ensure good API coverage.
18// o Support TDM on PCM and I2S
Frank Mandarinodb2a4162006-10-06 18:31:09 +020019
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
25#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070026#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020027#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020028#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010029#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070031#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000032#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080033#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020034#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000035#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010039#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010040#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Mark Brown384c89e2008-12-03 17:34:03 +000048#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000049struct dentry *snd_soc_debugfs_root;
50EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000051#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070054static LIST_HEAD(component_list);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +010055static LIST_HEAD(unbind_card_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
Kuninori Morimoto368dee92018-09-21 05:23:01 +000057#define for_each_component(component) \
58 list_for_each_entry(component, &component_list, list)
59
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +020069/*
70 * If a DMI filed contain strings in this blacklist (e.g.
71 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
Mengdong Lin98faf432017-06-28 15:01:39 +080072 * 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
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000183 for_each_component(component)
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000184 for_each_component_dais(component, dai)
Donglin Peng700c17c2018-01-18 13:31:26 +0800185 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
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000199 for_each_component(component)
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000200 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,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200226 "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{
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900231 if (!card->debugfs_card_root)
232 return;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200233 debugfs_remove_recursive(card->debugfs_card_root);
Kuninori Morimoto29040d12019-05-27 16:51:34 +0900234 card->debugfs_card_root = NULL;
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200235}
236
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200237static void snd_soc_debugfs_init(void)
238{
239 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300240 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200241 pr_warn("ASoC: Failed to create debugfs directory\n");
242 snd_soc_debugfs_root = NULL;
243 return;
244 }
245
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200246 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
247 &dai_list_fops))
248 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Kuninori Morimotodb795f92018-05-08 03:21:00 +0000249
250 if (!debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
251 &component_list_fops))
252 pr_warn("ASoC: Failed to create component list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200253}
254
255static void snd_soc_debugfs_exit(void)
256{
257 debugfs_remove_recursive(snd_soc_debugfs_root);
258}
259
Mark Brown2624d5f2009-11-03 21:56:13 +0000260#else
261
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200262static inline void soc_init_component_debugfs(
263 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000264{
265}
266
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200267static inline void soc_cleanup_component_debugfs(
268 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000269{
270}
271
Axel Linb95fccb2010-11-09 17:06:44 +0800272static inline void soc_init_card_debugfs(struct snd_soc_card *card)
273{
274}
275
276static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
277{
278}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200279
280static inline void snd_soc_debugfs_init(void)
281{
282}
283
284static inline void snd_soc_debugfs_exit(void)
285{
286}
287
Mark Brown2624d5f2009-11-03 21:56:13 +0000288#endif
289
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000290static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
291 struct snd_soc_component *component)
292{
293 struct snd_soc_rtdcom_list *rtdcom;
294 struct snd_soc_rtdcom_list *new_rtdcom;
295
296 for_each_rtdcom(rtd, rtdcom) {
297 /* already connected */
298 if (rtdcom->component == component)
299 return 0;
300 }
301
302 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
303 if (!new_rtdcom)
304 return -ENOMEM;
305
306 new_rtdcom->component = component;
307 INIT_LIST_HEAD(&new_rtdcom->list);
308
309 list_add_tail(&new_rtdcom->list, &rtd->component_list);
310
311 return 0;
312}
313
314static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
315{
316 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
317
318 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
319 kfree(rtdcom1);
320
321 INIT_LIST_HEAD(&rtd->component_list);
322}
323
324struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
325 const char *driver_name)
326{
327 struct snd_soc_rtdcom_list *rtdcom;
328
Kuninori Morimoto971da242018-01-23 00:41:24 +0000329 if (!driver_name)
330 return NULL;
331
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000332 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000333 const char *component_name = rtdcom->component->driver->name;
334
335 if (!component_name)
336 continue;
337
338 if ((component_name == driver_name) ||
339 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000340 return rtdcom->component;
341 }
342
343 return NULL;
344}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000345EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000346
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100347struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
348 const char *dai_link, int stream)
349{
Mengdong Lin1a497982015-11-18 02:34:11 -0500350 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100351
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000352 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500353 if (rtd->dai_link->no_pcm &&
354 !strcmp(rtd->dai_link->name, dai_link))
355 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100356 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000357 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100358 return NULL;
359}
360EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
361
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000362static const struct snd_soc_ops null_snd_soc_ops;
363
Mengdong Lin1a497982015-11-18 02:34:11 -0500364static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
365 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
366{
367 struct snd_soc_pcm_runtime *rtd;
368
369 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
370 if (!rtd)
371 return NULL;
372
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000373 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500374 rtd->card = card;
375 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000376 if (!rtd->dai_link->ops)
377 rtd->dai_link->ops = &null_snd_soc_ops;
378
Kees Cook6396bb22018-06-12 14:03:40 -0700379 rtd->codec_dais = kcalloc(dai_link->num_codecs,
380 sizeof(struct snd_soc_dai *),
Mengdong Lin1a497982015-11-18 02:34:11 -0500381 GFP_KERNEL);
382 if (!rtd->codec_dais) {
383 kfree(rtd);
384 return NULL;
385 }
386
387 return rtd;
388}
389
390static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
391{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000392 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000393 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500394 kfree(rtd);
395}
396
397static void soc_add_pcm_runtime(struct snd_soc_card *card,
398 struct snd_soc_pcm_runtime *rtd)
399{
400 list_add_tail(&rtd->list, &card->rtd_list);
401 rtd->num = card->num_rtd;
402 card->num_rtd++;
403}
404
405static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
406{
407 struct snd_soc_pcm_runtime *rtd, *_rtd;
408
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000409 for_each_card_rtds_safe(card, rtd, _rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500410 list_del(&rtd->list);
411 soc_free_pcm_runtime(rtd);
412 }
413
414 card->num_rtd = 0;
415}
416
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100417struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
418 const char *dai_link)
419{
Mengdong Lin1a497982015-11-18 02:34:11 -0500420 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100421
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000422 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500423 if (!strcmp(rtd->dai_link->name, dai_link))
424 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100425 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000426 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100427 return NULL;
428}
429EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
430
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900431static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
432{
433 struct snd_soc_pcm_runtime *rtd;
434
435 for_each_card_rtds(card, rtd)
436 flush_delayed_work(&rtd->delayed_work);
437}
438
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100439static void codec2codec_close_delayed_work(struct work_struct *work)
440{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200441 /*
442 * Currently nothing to do for c2c links
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100443 * Since c2c links are internal nodes in the DAPM graph and
444 * don't interface with the outside world or application layer
445 * we don't have to do any special handling on close.
446 */
447}
448
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000449#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000451int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200452{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000453 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000454 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500455 struct snd_soc_pcm_runtime *rtd;
456 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200458 /* If the card is not initialized yet there is nothing to do */
459 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200460 return 0;
461
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200462 /*
463 * Due to the resume being scheduled into a workqueue we could
464 * suspend before that's finished - wait for it to complete.
Andy Green6ed25972008-06-13 16:24:05 +0100465 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100467
468 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100470
Mark Browna00f90f2010-12-02 16:24:24 +0000471 /* mute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000472 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000473 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100474
Mengdong Lin1a497982015-11-18 02:34:11 -0500475 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100476 continue;
477
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000478 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200479 struct snd_soc_dai_driver *drv = dai->driver;
480
481 if (drv->ops->digital_mute && dai->playback_active)
482 drv->ops->digital_mute(dai, 1);
483 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 }
485
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100486 /* suspend all pcms */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000487 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500488 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100489 continue;
490
Mengdong Lin1a497982015-11-18 02:34:11 -0500491 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100492 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100493
Mark Brown87506542008-11-18 20:50:34 +0000494 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000495 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000497 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500498 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100499
Mengdong Lin1a497982015-11-18 02:34:11 -0500500 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100501 continue;
502
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100503 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100505 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200506
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200507 /* close any waiting streams */
Kuninori Morimoto65462e442019-01-21 09:32:37 +0900508 snd_soc_flush_all_delayed_work(card);
Mark Brown3efab7d2010-05-09 13:25:43 +0100509
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000510 for_each_card_rtds(card, rtd) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511
Mengdong Lin1a497982015-11-18 02:34:11 -0500512 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100513 continue;
514
Mengdong Lin1a497982015-11-18 02:34:11 -0500515 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800516 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518
Mengdong Lin1a497982015-11-18 02:34:11 -0500519 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800520 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800521 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 }
523
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200524 /* Recheck all endpoints too, their state is affected by suspend */
525 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700526 snd_soc_dapm_sync(&card->dapm);
527
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000528 /* suspend all COMPONENTs */
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000529 for_each_card_components(card, component) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200530 struct snd_soc_dapm_context *dapm =
531 snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000532
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200533 /*
534 * If there are paths active then the COMPONENT will be held
535 * with bias _ON and should not be suspended.
536 */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000537 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200538 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000540 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000541 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000542 * bias off then being in STANDBY
543 * means it's doing something,
544 * otherwise fall through.
545 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200546 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000547 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200548 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000549 break;
550 }
Gustavo A. R. Silva1a12d5d2018-08-03 11:34:30 -0500551 /* fall through */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 case SND_SOC_BIAS_OFF:
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000554 if (component->driver->suspend)
555 component->driver->suspend(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000556 component->suspended = 1;
557 if (component->regmap)
558 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800559 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000560 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 break;
562 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000563 dev_dbg(component->dev,
564 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 break;
566 }
567 }
568 }
569
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000570 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500571 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572
Mengdong Lin1a497982015-11-18 02:34:11 -0500573 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 continue;
575
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100576 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800578
579 /* deactivate pins to sleep state */
580 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200581 }
582
Mark Brown87506542008-11-18 20:50:34 +0000583 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000584 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585
586 return 0;
587}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000588EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200589
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200590/*
591 * deferred resume work, so resume can complete before we finished
Andy Green6ed25972008-06-13 16:24:05 +0100592 * setting our codec back up, which can be very slow on I2C
593 */
594static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 struct snd_soc_card *card =
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200597 container_of(work, struct snd_soc_card,
598 deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500599 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000600 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500601 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200603 /*
604 * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
Andy Green6ed25972008-06-13 16:24:05 +0100605 * so userspace apps are blocked from touching us
606 */
607
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000608 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100609
Mark Brown9949788b2010-05-07 20:24:05 +0100610 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100612
Mark Brown87506542008-11-18 20:50:34 +0000613 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000614 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100616 /* resume control bus DAIs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000617 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500618 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100619
Mengdong Lin1a497982015-11-18 02:34:11 -0500620 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100621 continue;
622
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100623 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625 }
626
Kuninori Morimotof70f18f72018-09-18 01:29:55 +0000627 for_each_card_components(card, component) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000628 if (component->suspended) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000629 if (component->driver->resume)
630 component->driver->resume(component);
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000631 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100632 }
633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000635 for_each_card_rtds(card, rtd) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100636
Mengdong Lin1a497982015-11-18 02:34:11 -0500637 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100638 continue;
639
Mengdong Lin1a497982015-11-18 02:34:11 -0500640 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000641 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800642 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643
Mengdong Lin1a497982015-11-18 02:34:11 -0500644 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000645 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800646 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 }
648
Mark Brown3ff3f642008-05-19 12:32:25 +0200649 /* unmute any active DACs */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000650 for_each_card_rtds(card, rtd) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000651 struct snd_soc_dai *dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100652
Mengdong Lin1a497982015-11-18 02:34:11 -0500653 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100654 continue;
655
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000656 for_each_rtd_codec_dai(rtd, i, dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200657 struct snd_soc_dai_driver *drv = dai->driver;
658
659 if (drv->ops->digital_mute && dai->playback_active)
660 drv->ops->digital_mute(dai, 0);
661 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000664 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500665 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100666
Mengdong Lin1a497982015-11-18 02:34:11 -0500667 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100668 continue;
669
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100670 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200672 }
673
Mark Brown87506542008-11-18 20:50:34 +0000674 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000675 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000677 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100678
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200679 /* Recheck all endpoints too, their state is affected by suspend */
680 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700681 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530682
683 /* userspace can access us now we are back as we were before */
684 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100685}
686
687/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000688int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100689{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000690 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100691 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500692 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900693 struct snd_soc_dai *codec_dai;
694 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200695
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200696 /* If the card is not initialized yet there is nothing to do */
697 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800698 return 0;
699
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800700 /* activate pins from sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000701 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800704 if (cpu_dai->active)
705 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200706
Kuninori Morimoto22d251a2019-05-13 16:06:07 +0900707 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200708 if (codec_dai->active)
709 pinctrl_pm_select_default_state(codec_dai->dev);
710 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800711 }
712
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100713 /*
714 * DAIs that also act as the control bus master might have other drivers
715 * hanging off them so need to resume immediately. Other drivers don't
716 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100717 * due to I/O costs and anti-pop so handle them out of line.
718 */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000719 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -0500720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200721
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100722 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600723 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100724 if (bus_control) {
725 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 soc_resume_deferred(&card->deferred_resume_work);
727 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000728 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600729 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000730 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100731 }
Andy Green6ed25972008-06-13 16:24:05 +0100732
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733 return 0;
734}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000735EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000737#define snd_soc_suspend NULL
738#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739#endif
740
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100741static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800742};
743
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900744static struct device_node
745*soc_component_to_node(struct snd_soc_component *component)
746{
747 struct device_node *of_node;
748
749 of_node = component->dev->of_node;
750 if (!of_node && component->dev->parent)
751 of_node = component->dev->parent->of_node;
752
753 return of_node;
754}
755
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200756static struct snd_soc_component *soc_find_component(
757 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100758{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200759 struct snd_soc_component *component;
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000760 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
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000764 for_each_component(component) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200765 if (of_node) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900766 component_of_node = soc_component_to_node(component);
Charles Keepaxd0b95e62019-01-25 16:04:06 +0000767
768 if (component_of_node == of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200769 return component;
Mark Brown5a7b2aa2019-01-14 23:29:36 +0000770 } else if (name && strcmp(component->name, name) == 0) {
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200771 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100772 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100773 }
774
775 return NULL;
776}
777
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000778static int snd_soc_is_matching_component(
779 const struct snd_soc_dai_link_component *dlc,
780 struct snd_soc_component *component)
781{
782 struct device_node *component_of_node;
783
Kuninori Morimotoc0834442019-05-13 16:06:59 +0900784 component_of_node = soc_component_to_node(component);
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000785
786 if (dlc->of_node && component_of_node != dlc->of_node)
787 return 0;
788 if (dlc->name && strcmp(component->name, dlc->name))
789 return 0;
790
791 return 1;
792}
793
Mengdong Linfbb88b52016-04-22 12:25:33 +0800794/**
795 * snd_soc_find_dai - Find a registered DAI
796 *
Jeffy Chen49584712017-08-22 15:57:21 +0800797 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800798 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700799 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800800 * find the DAI of the same name. The component's of_node and name
801 * should also match if being specified.
802 *
803 * Return: pointer of DAI, or NULL if not found.
804 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800805struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200806 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100807{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200808 struct snd_soc_component *component;
809 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100810
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100811 lockdep_assert_held(&client_mutex);
812
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200813 /* Find CPU DAI from registered DAIs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000814 for_each_component(component) {
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000815 if (!snd_soc_is_matching_component(dlc, component))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200816 continue;
Kuninori Morimoto15a0c642018-09-21 05:23:17 +0000817 for_each_component_dais(component, dai) {
Jeffy Chen49584712017-08-22 15:57:21 +0800818 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800819 && (!dai->driver->name
820 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100821 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100822
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200823 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100824 }
825 }
826
827 return NULL;
828}
Mengdong Lin305e9022016-04-19 13:12:25 +0800829EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100830
Mengdong Lin17fb17552016-11-03 01:04:12 +0800831/**
832 * snd_soc_find_dai_link - Find a DAI link
833 *
834 * @card: soc card
835 * @id: DAI link ID to match
836 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000837 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800838 *
839 * This function will search all existing DAI links of the soc card to
840 * find the link of the same ID. Since DAI links may not have their
841 * unique ID, so name and stream name should also match if being
842 * specified.
843 *
844 * Return: pointer of DAI link, or NULL if not found.
845 */
846struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
847 int id, const char *name,
848 const char *stream_name)
849{
850 struct snd_soc_dai_link *link, *_link;
851
852 lockdep_assert_held(&client_mutex);
853
Kuninori Morimoto98061fd2018-09-18 01:29:16 +0000854 for_each_card_links_safe(card, link, _link) {
Mengdong Lin17fb17552016-11-03 01:04:12 +0800855 if (link->id != id)
856 continue;
857
858 if (name && (!link->name || strcmp(name, link->name)))
859 continue;
860
861 if (stream_name && (!link->stream_name
862 || strcmp(stream_name, link->stream_name)))
863 continue;
864
865 return link;
866 }
867
868 return NULL;
869}
870EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
871
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800872static bool soc_is_dai_link_bound(struct snd_soc_card *card,
873 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874{
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800875 struct snd_soc_pcm_runtime *rtd;
876
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +0000877 for_each_card_rtds(card, rtd) {
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800878 if (rtd->dai_link == dai_link)
879 return true;
880 }
881
882 return false;
883}
884
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500885static int soc_bind_dai_link(struct snd_soc_card *card,
886 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887{
Mengdong Lin1a497982015-11-18 02:34:11 -0500888 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900889 struct snd_soc_dai_link_component *codecs;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000890 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200891 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
Liam Girdwooda655de82018-07-02 16:59:54 +0100893 if (dai_link->ignore)
894 return 0;
895
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -0500896 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +0000897
Mengdong Lin49a5ba12015-12-02 14:11:40 +0800898 if (soc_is_dai_link_bound(card, dai_link)) {
899 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
900 dai_link->name);
901 return 0;
902 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200903
Sudip Mukherjee513cb3112016-02-22 14:14:31 +0530904 rtd = soc_new_pcm_runtime(card, dai_link);
905 if (!rtd)
906 return -ENOMEM;
907
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900908 /* FIXME: we need multi CPU support in the future */
909 rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus);
Mark Brownb19e6e72012-03-14 21:18:39 +0000910 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +0100911 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
Kuninori Morimoto08a58412019-06-06 13:07:22 +0900912 dai_link->cpus->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500913 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +0000914 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000915 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +0000916
Benoit Cousson88bd8702014-07-08 23:19:34 +0200917 /* Find CODEC from registered CODECs */
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900918 rtd->num_codecs = dai_link->num_codecs;
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900919 for_each_link_codecs(dai_link, i, codecs) {
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900920 rtd->codec_dais[i] = snd_soc_find_dai(codecs);
921 if (!rtd->codec_dais[i]) {
Stefan Agner7c7e2d62019-01-18 10:55:04 +0100922 dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
Kuninori Morimoto720734a2019-01-28 10:40:24 +0900923 codecs->dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -0500924 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 }
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900926 snd_soc_rtdcom_add(rtd, rtd->codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000927 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100928
Benoit Cousson88bd8702014-07-08 23:19:34 +0200929 /* Single codec links expect codec and codec_dai in runtime data */
Kuninori Morimoto0a2cfcd2019-05-13 16:06:30 +0900930 rtd->codec_dai = rtd->codec_dais[0];
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100931
Kuninori Morimotoe2b30ed2019-05-13 16:06:44 +0900932 /* Find PLATFORM from registered PLATFORMs */
Kuninori Morimoto368dee92018-09-21 05:23:01 +0000933 for_each_component(component) {
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900934 if (!snd_soc_is_matching_component(dai_link->platforms,
Kuninori Morimotobe6ac0a2018-09-11 06:51:45 +0000935 component))
936 continue;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000937
938 snd_soc_rtdcom_add(rtd, component);
939 }
940
Mengdong Lin1a497982015-11-18 02:34:11 -0500941 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +0000942 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -0500943
944_err_defer:
945 soc_free_pcm_runtime(rtd);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +0200946 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947}
948
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900949static void soc_cleanup_component(struct snd_soc_component *component)
950{
Amadeusz Sławiński3bb936f52019-06-05 15:45:53 +0200951 snd_soc_component_set_jack(component, NULL, NULL);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900952 list_del(&component->card_list);
953 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
954 soc_cleanup_component_debugfs(component);
955 component->card = NULL;
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -0700956 if (!component->driver->module_get_upon_open)
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -0600957 module_put(component->dev->driver->owner);
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900958}
959
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200960static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600961{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +0200962 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200963 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600964
Kuninori Morimoto999f7f52018-05-08 03:20:24 +0000965 if (component->driver->remove)
966 component->driver->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600967
Kuninori Morimoto22d14232019-01-21 09:32:55 +0900968 soc_cleanup_component(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600969}
970
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200971static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200972{
973 int err;
974
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600975 if (!dai || !dai->probed || !dai->driver ||
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000976 dai->driver->remove_order != order)
977 return;
978
979 if (dai->driver->remove) {
980 err = dai->driver->remove(dai);
981 if (err < 0)
982 dev_err(dai->dev,
983 "ASoC: failed to remove %s: %d\n",
984 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100985 }
Kuninori Morimoto2eda3cb2018-09-11 06:54:26 +0000986 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100987}
988
Mengdong Lin1a497982015-11-18 02:34:11 -0500989static void soc_remove_link_dais(struct snd_soc_card *card,
990 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200992 int i;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000993 struct snd_soc_dai *codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994
995 /* unregister the rtd device */
996 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800997 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 rtd->dev_registered = 0;
999 }
1000
1001 /* remove the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001002 for_each_rtd_codec_dai(rtd, i, codec_dai)
1003 soc_remove_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001005 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006}
1007
Mengdong Lin1a497982015-11-18 02:34:11 -05001008static void soc_remove_link_components(struct snd_soc_card *card,
1009 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001010{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001011 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001012 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001013
Ranjani Sridharan34ac3c32019-05-23 10:12:01 -07001014 mutex_lock(&client_mutex);
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001015 for_each_rtdcom(rtd, rtdcom) {
1016 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001017
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001018 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001019 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001020 }
Ranjani Sridharan34ac3c32019-05-23 10:12:01 -07001021 mutex_unlock(&client_mutex);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001022}
1023
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001024static void soc_remove_dai_links(struct snd_soc_card *card)
1025{
Mengdong Lin1a497982015-11-18 02:34:11 -05001026 int order;
1027 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001028 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001029
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001030 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001031 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001032 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001033 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001034
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001035 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001036 for_each_card_rtds(card, rtd)
Mengdong Lin1a497982015-11-18 02:34:11 -05001037 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001038 }
1039
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001040 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001041 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1042 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1043 link->name);
1044
1045 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001046 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001047}
1048
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001049static int snd_soc_init_cpu(struct snd_soc_card *card,
1050 struct snd_soc_dai_link *dai_link)
1051{
1052 struct snd_soc_dai_link_component *cpu = dai_link->cpus;
1053
1054 /*
1055 * REMOVE ME
1056 *
1057 * This is glue code for Legacy vs Modern dai_link.
1058 * This function will be removed if all derivers are switched to
1059 * modern style dai_link.
1060 * Driver shouldn't use both legacy and modern style in the same time.
1061 * see
1062 * soc.h :: struct snd_soc_dai_link
1063 */
1064 /* convert Legacy platform link */
1065 if (!cpu) {
1066 cpu = devm_kzalloc(card->dev,
1067 sizeof(struct snd_soc_dai_link_component),
1068 GFP_KERNEL);
1069 if (!cpu)
1070 return -ENOMEM;
1071
1072 dai_link->cpus = cpu;
1073 dai_link->num_cpus = 1;
1074 dai_link->legacy_cpu = 1;
1075
1076 cpu->name = dai_link->cpu_name;
1077 cpu->of_node = dai_link->cpu_of_node;
1078 cpu->dai_name = dai_link->cpu_dai_name;
1079 }
1080
1081 if (!dai_link->cpus) {
1082 dev_err(card->dev, "ASoC: DAI link has no CPUs\n");
1083 return -EINVAL;
1084 }
1085
1086 return 0;
1087}
1088
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001089static int snd_soc_init_platform(struct snd_soc_card *card,
1090 struct snd_soc_dai_link *dai_link)
1091{
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001092 struct snd_soc_dai_link_component *platform = dai_link->platforms;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001093
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001094 /*
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001095 * REMOVE ME
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001096 *
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001097 * This is glue code for Legacy vs Modern dai_link.
1098 * This function will be removed if all derivers are switched to
1099 * modern style dai_link.
1100 * Driver shouldn't use both legacy and modern style in the same time.
1101 * see
1102 * soc.h :: struct snd_soc_dai_link
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001103 */
1104 /* convert Legacy platform link */
Curtis Malainey78a24e12019-01-29 13:47:09 -08001105 if (!platform) {
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001106 platform = devm_kzalloc(card->dev,
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001107 sizeof(struct snd_soc_dai_link_component),
1108 GFP_KERNEL);
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001109 if (!platform)
1110 return -ENOMEM;
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001111
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001112 dai_link->platforms = platform;
1113 dai_link->num_platforms = 1;
Curtis Malainey09ac6a82019-01-10 16:21:04 -08001114 dai_link->legacy_platform = 1;
1115 platform->name = dai_link->platform_name;
1116 platform->of_node = dai_link->platform_of_node;
1117 platform->dai_name = NULL;
Kuninori Morimoto4a9ed392018-09-11 06:51:14 +00001118 }
1119
1120 /* if there's no platform we match on the empty platform */
1121 if (!platform->name &&
1122 !platform->of_node)
1123 platform->name = "snd-soc-dummy";
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001124
1125 return 0;
1126}
1127
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001128static void soc_cleanup_legacy(struct snd_soc_card *card)
Curtis Malainey78a24e12019-01-29 13:47:09 -08001129{
1130 struct snd_soc_dai_link *link;
1131 int i;
1132 /*
1133 * FIXME
1134 *
1135 * this function should be removed with snd_soc_init_platform
1136 */
1137
1138 for_each_card_prelinks(card, i, link) {
1139 if (link->legacy_platform) {
1140 link->legacy_platform = 0;
1141 link->platforms = NULL;
1142 }
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001143 if (link->legacy_cpu) {
1144 link->legacy_cpu = 0;
1145 link->cpus = NULL;
1146 }
Curtis Malainey78a24e12019-01-29 13:47:09 -08001147 }
1148}
1149
Mengdong Lin923c5e612015-11-23 11:01:49 -05001150static int snd_soc_init_multicodec(struct snd_soc_card *card,
1151 struct snd_soc_dai_link *dai_link)
1152{
Kuninori Morimoto62bc79d2019-01-18 11:20:41 +09001153 /*
1154 * REMOVE ME
1155 *
1156 * This is glue code for Legacy vs Modern dai_link.
1157 * This function will be removed if all derivers are switched to
1158 * modern style dai_link.
1159 * Driver shouldn't use both legacy and modern style in the same time.
1160 * see
1161 * soc.h :: struct snd_soc_dai_link
1162 */
1163
Mengdong Lin923c5e612015-11-23 11:01:49 -05001164 /* Legacy codec/codec_dai link is a single entry in multicodec */
1165 if (dai_link->codec_name || dai_link->codec_of_node ||
1166 dai_link->codec_dai_name) {
1167 dai_link->num_codecs = 1;
1168
1169 dai_link->codecs = devm_kzalloc(card->dev,
1170 sizeof(struct snd_soc_dai_link_component),
1171 GFP_KERNEL);
1172 if (!dai_link->codecs)
1173 return -ENOMEM;
1174
1175 dai_link->codecs[0].name = dai_link->codec_name;
1176 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1177 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1178 }
1179
1180 if (!dai_link->codecs) {
1181 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1182 return -EINVAL;
1183 }
1184
1185 return 0;
1186}
1187
1188static int soc_init_dai_link(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001189 struct snd_soc_dai_link *link)
Mengdong Lin923c5e612015-11-23 11:01:49 -05001190{
1191 int i, ret;
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001192 struct snd_soc_dai_link_component *codec;
Mengdong Lin923c5e612015-11-23 11:01:49 -05001193
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001194 ret = snd_soc_init_cpu(card, link);
1195 if (ret) {
1196 dev_err(card->dev, "ASoC: failed to init cpu\n");
1197 return ret;
1198 }
1199
Kuninori Morimotodaecf462018-08-31 03:10:08 +00001200 ret = snd_soc_init_platform(card, link);
1201 if (ret) {
1202 dev_err(card->dev, "ASoC: failed to init multiplatform\n");
1203 return ret;
1204 }
1205
Mengdong Lin923c5e612015-11-23 11:01:49 -05001206 ret = snd_soc_init_multicodec(card, link);
1207 if (ret) {
1208 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1209 return ret;
1210 }
1211
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001212 for_each_link_codecs(link, i, codec) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001213 /*
1214 * Codec must be specified by 1 of name or OF node,
1215 * not both or neither.
1216 */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001217 if (!!codec->name ==
1218 !!codec->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001219 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1220 link->name);
1221 return -EINVAL;
1222 }
1223 /* Codec DAI name must be specified */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00001224 if (!codec->dai_name) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001225 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1226 link->name);
1227 return -EINVAL;
1228 }
1229 }
1230
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001231 /* FIXME */
1232 if (link->num_platforms > 1) {
1233 dev_err(card->dev,
1234 "ASoC: multi platform is not yet supported %s\n",
1235 link->name);
1236 return -EINVAL;
1237 }
1238
Mengdong Lin923c5e612015-11-23 11:01:49 -05001239 /*
1240 * Platform may be specified by either name or OF node, but
1241 * can be left unspecified, and a dummy platform will be used.
1242 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001243 if (link->platforms->name && link->platforms->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001244 dev_err(card->dev,
1245 "ASoC: Both platform name/of_node are set for %s\n",
1246 link->name);
1247 return -EINVAL;
1248 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301249
1250 /*
1251 * Defer card registartion if platform dai component is not added to
1252 * component list.
1253 */
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09001254 if ((link->platforms->of_node || link->platforms->name) &&
1255 !soc_find_component(link->platforms->of_node, link->platforms->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301256 return -EPROBE_DEFER;
1257
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001258 /* FIXME */
1259 if (link->num_cpus > 1) {
1260 dev_err(card->dev,
1261 "ASoC: multi cpu is not yet supported %s\n",
1262 link->name);
1263 return -EINVAL;
1264 }
1265
Mengdong Lin923c5e612015-11-23 11:01:49 -05001266 /*
1267 * CPU device may be specified by either name or OF node, but
1268 * can be left unspecified, and will be matched based on DAI
1269 * name alone..
1270 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001271 if (link->cpus->name && link->cpus->of_node) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001272 dev_err(card->dev,
1273 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1274 link->name);
1275 return -EINVAL;
1276 }
Ajit Pandey8780cf12019-01-09 14:17:07 +05301277
1278 /*
1279 * Defer card registartion if cpu dai component is not added to
1280 * component list.
1281 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001282 if ((link->cpus->of_node || link->cpus->name) &&
1283 !soc_find_component(link->cpus->of_node, link->cpus->name))
Ajit Pandey8780cf12019-01-09 14:17:07 +05301284 return -EPROBE_DEFER;
1285
Mengdong Lin923c5e612015-11-23 11:01:49 -05001286 /*
1287 * At least one of CPU DAI name or CPU device name/node must be
1288 * specified
1289 */
Kuninori Morimoto08a58412019-06-06 13:07:22 +09001290 if (!link->cpus->dai_name &&
1291 !(link->cpus->name || link->cpus->of_node)) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05001292 dev_err(card->dev,
1293 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1294 link->name);
1295 return -EINVAL;
1296 }
1297
1298 return 0;
1299}
1300
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001301void snd_soc_disconnect_sync(struct device *dev)
1302{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001303 struct snd_soc_component *component =
1304 snd_soc_lookup_component(dev, NULL);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001305
1306 if (!component || !component->card)
1307 return;
1308
1309 snd_card_disconnect_sync(component->card->snd_card);
1310}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001311EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001312
Mengdong Linf8f80362015-12-02 14:11:22 +08001313/**
1314 * snd_soc_add_dai_link - Add a DAI link dynamically
1315 * @card: The ASoC card to which the DAI link is added
1316 * @dai_link: The new DAI link to add
1317 *
1318 * This function adds a DAI link to the ASoC card's link list.
1319 *
1320 * Note: Topology can use this API to add DAI links when probing the
1321 * topology component. And machine drivers can still define static
1322 * DAI links in dai_link array.
1323 */
1324int snd_soc_add_dai_link(struct snd_soc_card *card,
1325 struct snd_soc_dai_link *dai_link)
1326{
1327 if (dai_link->dobj.type
1328 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1329 dev_err(card->dev, "Invalid dai link type %d\n",
1330 dai_link->dobj.type);
1331 return -EINVAL;
1332 }
1333
1334 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001335 /*
1336 * Notify the machine driver for extra initialization
Mengdong Lind6f220e2015-12-02 14:11:32 +08001337 * on the link created by topology.
1338 */
1339 if (dai_link->dobj.type && card->add_dai_link)
1340 card->add_dai_link(card, dai_link);
1341
Mengdong Linf8f80362015-12-02 14:11:22 +08001342 list_add_tail(&dai_link->list, &card->dai_link_list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001343
1344 return 0;
1345}
1346EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1347
1348/**
1349 * snd_soc_remove_dai_link - Remove a DAI link from the list
1350 * @card: The ASoC card that owns the link
1351 * @dai_link: The DAI link to remove
1352 *
1353 * This function removes a DAI link from the ASoC card's link list.
1354 *
1355 * For DAI links previously added by topology, topology should
1356 * remove them by using the dobj embedded in the link.
1357 */
1358void snd_soc_remove_dai_link(struct snd_soc_card *card,
1359 struct snd_soc_dai_link *dai_link)
1360{
1361 struct snd_soc_dai_link *link, *_link;
1362
1363 if (dai_link->dobj.type
1364 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1365 dev_err(card->dev, "Invalid dai link type %d\n",
1366 dai_link->dobj.type);
1367 return;
1368 }
1369
1370 lockdep_assert_held(&client_mutex);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001371 /*
1372 * Notify the machine driver for extra destruction
Mengdong Lind6f220e2015-12-02 14:11:32 +08001373 * on the link created by topology.
1374 */
1375 if (dai_link->dobj.type && card->remove_dai_link)
1376 card->remove_dai_link(card, dai_link);
1377
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00001378 for_each_card_links_safe(card, link, _link) {
Mengdong Linf8f80362015-12-02 14:11:22 +08001379 if (link == dai_link) {
1380 list_del(&link->list);
Mengdong Linf8f80362015-12-02 14:11:22 +08001381 return;
1382 }
1383 }
1384}
1385EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1386
Jerome Brunetaefba452018-07-13 14:50:43 +02001387static void soc_set_of_name_prefix(struct snd_soc_component *component)
1388{
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001389 struct device_node *component_of_node = soc_component_to_node(component);
Jerome Brunetaefba452018-07-13 14:50:43 +02001390 const char *str;
1391 int ret;
1392
Jerome Brunetaefba452018-07-13 14:50:43 +02001393 ret = of_property_read_string(component_of_node, "sound-name-prefix",
1394 &str);
1395 if (!ret)
1396 component->name_prefix = str;
1397}
1398
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001399static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001400 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001401{
1402 int i;
1403
Jerome Brunetaefba452018-07-13 14:50:43 +02001404 for (i = 0; i < card->num_configs && card->codec_conf; i++) {
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001405 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Kuninori Morimotoc0834442019-05-13 16:06:59 +09001406 struct device_node *component_of_node = soc_component_to_node(component);
Charles Keepaxb24c5392018-04-27 13:54:36 +01001407
1408 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001409 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001410 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001411 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001412 component->name_prefix = map->name_prefix;
Jerome Brunetaefba452018-07-13 14:50:43 +02001413 return;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001414 }
Jerome Brunetaefba452018-07-13 14:50:43 +02001415
1416 /*
1417 * If there is no configuration table or no match in the table,
1418 * check if a prefix is provided in the node
1419 */
1420 soc_set_of_name_prefix(component);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001421}
1422
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001423static int soc_probe_component(struct snd_soc_card *card,
1424 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001425{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001426 struct snd_soc_dapm_context *dapm =
1427 snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001428 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001429 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001430
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001431 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001432 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001433
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001434 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001435 if (component->card != card) {
1436 dev_err(component->dev,
1437 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1438 card->name, component->card->name);
1439 return -ENODEV;
1440 }
1441 return 0;
1442 }
1443
Ranjani Sridharanb4ed6b52019-04-05 09:57:08 -07001444 if (!component->driver->module_get_upon_open &&
Pierre-Louis Bossartb450b872019-02-01 11:22:23 -06001445 !try_module_get(component->dev->driver->owner))
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001446 return -ENODEV;
1447
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001448 component->card = card;
1449 dapm->card = card;
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001450 INIT_LIST_HEAD(&component->card_list);
1451 INIT_LIST_HEAD(&dapm->list);
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001452 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001453
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001454 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001455
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001456 if (component->driver->dapm_widgets) {
1457 ret = snd_soc_dapm_new_controls(dapm,
1458 component->driver->dapm_widgets,
1459 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001460
1461 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001462 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001463 "Failed to create new controls %d\n", ret);
1464 goto err_probe;
1465 }
1466 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001467
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00001468 for_each_component_dais(component, dai) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001469 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001470 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001471 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001472 "Failed to create DAI widgets %d\n", ret);
1473 goto err_probe;
1474 }
1475 }
Mark Brown888df392012-02-16 19:37:51 -08001476
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001477 if (component->driver->probe) {
1478 ret = component->driver->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001479 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001480 dev_err(component->dev,
1481 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001482 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001483 }
1484 }
Kuninori Morimoto2ffb0f52019-05-17 15:06:37 +09001485 WARN(dapm->idle_bias_off &&
1486 dapm->bias_level != SND_SOC_BIAS_OFF,
1487 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1488 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001489
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001490 /* machine specific init */
1491 if (component->init) {
1492 ret = component->init(component);
1493 if (ret < 0) {
1494 dev_err(component->dev,
1495 "Failed to do machine specific init %d\n", ret);
1496 goto err_probe;
1497 }
1498 }
1499
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001500 if (component->driver->controls)
1501 snd_soc_add_component_controls(component,
1502 component->driver->controls,
1503 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001504 if (component->driver->dapm_routes)
1505 snd_soc_dapm_add_routes(dapm,
1506 component->driver->dapm_routes,
1507 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001508
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001509 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotof70f18f72018-09-18 01:29:55 +00001510 /* see for_each_card_components */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001511 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001512
Jarkko Nikula70d293312011-01-27 16:24:22 +02001513err_probe:
Kuninori Morimoto22d14232019-01-21 09:32:55 +09001514 if (ret < 0)
1515 soc_cleanup_component(component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001516
1517 return ret;
1518}
1519
Mark Brown36ae1a92012-01-06 17:12:45 -08001520static void rtd_release(struct device *dev)
1521{
1522 kfree(dev);
1523}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001525static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1526 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001527{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001528 int ret = 0;
1529
Jarkko Nikula589c3562010-12-06 16:27:07 +02001530 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001531 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1532 if (!rtd->dev)
1533 return -ENOMEM;
1534 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001535 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001536 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001537 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001538 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001539 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001540 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001541 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1542 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1543 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1544 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001545 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001546 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001547 /* calling put_device() here to free the rtd->dev */
1548 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001549 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001550 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001551 return ret;
1552 }
1553 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001554 return 0;
1555}
1556
Mengdong Lin1a497982015-11-18 02:34:11 -05001557static int soc_probe_link_components(struct snd_soc_card *card,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001558 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001559{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001560 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001561 struct snd_soc_rtdcom_list *rtdcom;
1562 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001563
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001564 for_each_rtdcom(rtd, rtdcom) {
1565 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001566
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001567 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001568 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001569 if (ret < 0)
1570 return ret;
1571 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001572 }
1573
Stephen Warren62ae68f2012-06-08 12:34:23 -06001574 return 0;
1575}
1576
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001577static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001578{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001579 if (dai->probed ||
1580 dai->driver->probe_order != order)
1581 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001582
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001583 if (dai->driver->probe) {
1584 int ret = dai->driver->probe(dai);
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001585
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001586 if (ret < 0) {
1587 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1588 dai->name, ret);
1589 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001590 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001591 }
1592
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001593 dai->probed = 1;
1594
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001595 return 0;
1596}
1597
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001598static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1599 struct snd_soc_pcm_runtime *rtd)
1600{
1601 int i, ret = 0;
1602
1603 for (i = 0; i < num_dais; ++i) {
1604 struct snd_soc_dai_driver *drv = dais[i]->driver;
1605
Rohit kumarde17f142018-11-01 18:08:49 +05301606 if (drv->pcm_new)
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001607 ret = drv->pcm_new(rtd, dais[i]);
1608 if (ret < 0) {
1609 dev_err(dais[i]->dev,
1610 "ASoC: Failed to bind %s with pcm device\n",
1611 dais[i]->name);
1612 return ret;
1613 }
1614 }
1615
1616 return 0;
1617}
1618
Mengdong Lin1a497982015-11-18 02:34:11 -05001619static int soc_probe_link_dais(struct snd_soc_card *card,
1620 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621{
Mengdong Lin1a497982015-11-18 02:34:11 -05001622 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001624 struct snd_soc_rtdcom_list *rtdcom;
1625 struct snd_soc_component *component;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001626 struct snd_soc_dai *codec_dai;
Liam Girdwooda655de82018-07-02 16:59:54 +01001627 int i, ret, num;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001629 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001630 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632 /* set default power off timeout */
1633 rtd->pmdown_time = pmdown_time;
1634
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001635 ret = soc_probe_dai(cpu_dai, order);
1636 if (ret)
1637 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639 /* probe the CODEC DAI */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001640 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1641 ret = soc_probe_dai(codec_dai, order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001642 if (ret)
1643 return ret;
1644 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001645
Liam Girdwood0168bf02011-06-07 16:08:05 +01001646 /* complete DAI probe during last probe */
1647 if (order != SND_SOC_COMP_ORDER_LAST)
1648 return 0;
1649
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001650 /* do machine specific initialization */
1651 if (dai_link->init) {
1652 ret = dai_link->init(rtd);
1653 if (ret < 0) {
1654 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1655 dai_link->name, ret);
1656 return ret;
1657 }
1658 }
1659
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001660 if (dai_link->dai_fmt)
1661 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1662
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001663 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001664 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001666
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001667#ifdef CONFIG_DEBUG_FS
1668 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001669 if (dai_link->dynamic)
1670 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001671#endif
1672
Liam Girdwooda655de82018-07-02 16:59:54 +01001673 num = rtd->num;
1674
1675 /*
1676 * most drivers will register their PCMs using DAI link ordering but
1677 * topology based drivers can use the DAI link id field to set PCM
1678 * device number and then use rtd + a base offset of the BEs.
1679 */
1680 for_each_rtdcom(rtd, rtdcom) {
1681 component = rtdcom->component;
1682
1683 if (!component->driver->use_dai_pcm_id)
1684 continue;
1685
1686 if (rtd->dai_link->no_pcm)
1687 num += component->driver->be_pcm_base;
1688 else
1689 num = rtd->dai_link->id;
1690 }
1691
Jie Yang6f0c4222015-10-13 23:41:00 +08001692 if (cpu_dai->driver->compress_new) {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001693 /* create compress_device" */
Liam Girdwooda655de82018-07-02 16:59:54 +01001694 ret = cpu_dai->driver->compress_new(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001695 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001696 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301697 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001698 return ret;
1699 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001700 } else if (!dai_link->params) {
1701 /* create the pcm */
1702 ret = soc_new_pcm(rtd, num);
1703 if (ret < 0) {
1704 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1705 dai_link->stream_name, ret);
1706 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001707 }
Kuninori Morimoto52293592019-01-21 09:32:50 +09001708 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1709 if (ret < 0)
1710 return ret;
1711 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1712 rtd->num_codecs, rtd);
1713 if (ret < 0)
1714 return ret;
1715 } else {
1716 INIT_DELAYED_WORK(&rtd->delayed_work,
1717 codec2codec_close_delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718 }
1719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720 return 0;
1721}
1722
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001723static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001724{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001725 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001726 struct snd_soc_component *component;
1727 const char *name;
1728 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001729
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001730 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1731 /* codecs, usually analog devices */
1732 name = aux_dev->codec_name;
1733 codec_of_node = aux_dev->codec_of_node;
1734 component = soc_find_component(codec_of_node, name);
1735 if (!component) {
1736 if (codec_of_node)
1737 name = of_node_full_name(codec_of_node);
1738 goto err_defer;
1739 }
1740 } else if (aux_dev->name) {
1741 /* generic components */
1742 name = aux_dev->name;
1743 component = soc_find_component(NULL, name);
1744 if (!component)
1745 goto err_defer;
1746 } else {
1747 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1748 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001749 }
1750
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001751 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001752 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001753
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001754 return 0;
1755
1756err_defer:
1757 dev_err(card->dev, "ASoC: %s not registered\n", name);
1758 return -EPROBE_DEFER;
1759}
1760
1761static int soc_probe_aux_devices(struct snd_soc_card *card)
1762{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001763 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001764 int order;
1765 int ret;
1766
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001767 for_each_comp_order(order) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001768 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001769 if (comp->driver->probe_order == order) {
1770 ret = soc_probe_component(card, comp);
1771 if (ret < 0) {
1772 dev_err(card->dev,
1773 "ASoC: failed to probe aux component %s %d\n",
1774 comp->name, ret);
1775 return ret;
1776 }
1777 }
1778 }
1779 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001780
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001781 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001782}
1783
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001784static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001785{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001786 struct snd_soc_component *comp, *_comp;
1787 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001788
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00001789 for_each_comp_order(order) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001790 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001791 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001792
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001793 if (comp->driver->remove_order == order) {
1794 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001795 /* remove it from the card's aux_comp_list */
1796 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001797 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001798 }
1799 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001800}
1801
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001802/**
1803 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1804 * @rtd: The runtime for which the DAI link format should be changed
1805 * @dai_fmt: The new DAI link format
1806 *
1807 * This function updates the DAI link format for all DAIs connected to the DAI
1808 * link for the specified runtime.
1809 *
1810 * Note: For setups with a static format set the dai_fmt field in the
1811 * corresponding snd_dai_link struct instead of using this function.
1812 *
1813 * Returns 0 on success, otherwise a negative error code.
1814 */
1815int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1816 unsigned int dai_fmt)
1817{
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001818 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001819 struct snd_soc_dai *codec_dai;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001820 unsigned int i;
1821 int ret;
1822
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001823 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001824 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1825 if (ret != 0 && ret != -ENOTSUPP) {
1826 dev_warn(codec_dai->dev,
1827 "ASoC: Failed to set DAI format: %d\n", ret);
1828 return ret;
1829 }
1830 }
1831
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001832 /*
1833 * Flip the polarity for the "CPU" end of a CODEC<->CODEC link
1834 * the component which has non_legacy_dai_naming is Codec
1835 */
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00001836 if (cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001837 unsigned int inv_dai_fmt;
1838
1839 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1840 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1841 case SND_SOC_DAIFMT_CBM_CFM:
1842 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1843 break;
1844 case SND_SOC_DAIFMT_CBM_CFS:
1845 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1846 break;
1847 case SND_SOC_DAIFMT_CBS_CFM:
1848 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1849 break;
1850 case SND_SOC_DAIFMT_CBS_CFS:
1851 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1852 break;
1853 }
1854
1855 dai_fmt = inv_dai_fmt;
1856 }
1857
1858 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1859 if (ret != 0 && ret != -ENOTSUPP) {
1860 dev_warn(cpu_dai->dev,
1861 "ASoC: Failed to set DAI format: %d\n", ret);
1862 return ret;
1863 }
1864
1865 return 0;
1866}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001867EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001868
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001869#ifdef CONFIG_DMI
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001870/*
1871 * Trim special characters, and replace '-' with '_' since '-' is used to
Liam Girdwood345233d2017-01-14 16:13:02 +08001872 * separate different DMI fields in the card long name. Only number and
1873 * alphabet characters and a few separator characters are kept.
1874 */
1875static void cleanup_dmi_name(char *name)
1876{
1877 int i, j = 0;
1878
1879 for (i = 0; name[i]; i++) {
1880 if (isalnum(name[i]) || (name[i] == '.')
1881 || (name[i] == '_'))
1882 name[j++] = name[i];
1883 else if (name[i] == '-')
1884 name[j++] = '_';
1885 }
1886
1887 name[j] = '\0';
1888}
1889
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001890/*
1891 * Check if a DMI field is valid, i.e. not containing any string
Mengdong Lin98faf432017-06-28 15:01:39 +08001892 * in the black list.
1893 */
1894static int is_dmi_valid(const char *field)
1895{
1896 int i = 0;
1897
1898 while (dmi_blacklist[i]) {
1899 if (strstr(field, dmi_blacklist[i]))
1900 return 0;
1901 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001902 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001903
1904 return 1;
1905}
1906
Liam Girdwood345233d2017-01-14 16:13:02 +08001907/**
1908 * snd_soc_set_dmi_name() - Register DMI names to card
1909 * @card: The card to register DMI names
1910 * @flavour: The flavour "differentiator" for the card amongst its peers.
1911 *
1912 * An Intel machine driver may be used by many different devices but are
1913 * difficult for userspace to differentiate, since machine drivers ususally
1914 * use their own name as the card short name and leave the card long name
1915 * blank. To differentiate such devices and fix bugs due to lack of
1916 * device-specific configurations, this function allows DMI info to be used
1917 * as the sound card long name, in the format of
1918 * "vendor-product-version-board"
1919 * (Character '-' is used to separate different DMI fields here).
1920 * This will help the user space to load the device-specific Use Case Manager
1921 * (UCM) configurations for the card.
1922 *
1923 * Possible card long names may be:
1924 * DellInc.-XPS139343-01-0310JH
1925 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1926 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1927 *
1928 * This function also supports flavoring the card longname to provide
1929 * the extra differentiation, like "vendor-product-version-board-flavor".
1930 *
1931 * We only keep number and alphabet characters and a few separator characters
1932 * in the card long name since UCM in the user space uses the card long names
1933 * as card configuration directory names and AudoConf cannot support special
1934 * charactors like SPACE.
1935 *
1936 * Returns 0 on success, otherwise a negative error code.
1937 */
1938int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1939{
1940 const char *vendor, *product, *product_version, *board;
1941 size_t longname_buf_size = sizeof(card->snd_card->longname);
1942 size_t len;
1943
1944 if (card->long_name)
1945 return 0; /* long name already set by driver or from DMI */
1946
1947 /* make up dmi long name as: vendor.product.version.board */
1948 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001949 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001950 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1951 return 0;
1952 }
1953
1954 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1955 "%s", vendor);
1956 cleanup_dmi_name(card->dmi_longname);
1957
1958 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001959 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001960 len = strlen(card->dmi_longname);
1961 snprintf(card->dmi_longname + len,
1962 longname_buf_size - len,
1963 "-%s", product);
1964
1965 len++; /* skip the separator "-" */
1966 if (len < longname_buf_size)
1967 cleanup_dmi_name(card->dmi_longname + len);
1968
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02001969 /*
1970 * some vendors like Lenovo may only put a self-explanatory
Liam Girdwood345233d2017-01-14 16:13:02 +08001971 * name in the product version field
1972 */
1973 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001974 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001975 len = strlen(card->dmi_longname);
1976 snprintf(card->dmi_longname + len,
1977 longname_buf_size - len,
1978 "-%s", product_version);
1979
1980 len++;
1981 if (len < longname_buf_size)
1982 cleanup_dmi_name(card->dmi_longname + len);
1983 }
1984 }
1985
1986 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001987 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001988 len = strlen(card->dmi_longname);
1989 snprintf(card->dmi_longname + len,
1990 longname_buf_size - len,
1991 "-%s", board);
1992
1993 len++;
1994 if (len < longname_buf_size)
1995 cleanup_dmi_name(card->dmi_longname + len);
1996 } else if (!product) {
1997 /* fall back to using legacy name */
1998 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1999 return 0;
2000 }
2001
2002 /* Add flavour to dmi long name */
2003 if (flavour) {
2004 len = strlen(card->dmi_longname);
2005 snprintf(card->dmi_longname + len,
2006 longname_buf_size - len,
2007 "-%s", flavour);
2008
2009 len++;
2010 if (len < longname_buf_size)
2011 cleanup_dmi_name(card->dmi_longname + len);
2012 }
2013
2014 /* set the card long name */
2015 card->long_name = card->dmi_longname;
2016
2017 return 0;
2018}
2019EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02002020#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08002021
Liam Girdwooda655de82018-07-02 16:59:54 +01002022static void soc_check_tplg_fes(struct snd_soc_card *card)
2023{
2024 struct snd_soc_component *component;
2025 const struct snd_soc_component_driver *comp_drv;
2026 struct snd_soc_dai_link *dai_link;
2027 int i;
2028
Kuninori Morimoto368dee92018-09-21 05:23:01 +00002029 for_each_component(component) {
Liam Girdwooda655de82018-07-02 16:59:54 +01002030
2031 /* does this component override FEs ? */
2032 if (!component->driver->ignore_machine)
2033 continue;
2034
2035 /* for this machine ? */
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06002036 if (!strcmp(component->driver->ignore_machine,
2037 card->dev->driver->name))
2038 goto match;
Liam Girdwooda655de82018-07-02 16:59:54 +01002039 if (strcmp(component->driver->ignore_machine,
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06002040 dev_name(card->dev)))
Liam Girdwooda655de82018-07-02 16:59:54 +01002041 continue;
Pierre-Louis Bossarte194098b2019-03-01 19:08:51 -06002042match:
Liam Girdwooda655de82018-07-02 16:59:54 +01002043 /* machine matches, so override the rtd data */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002044 for_each_card_prelinks(card, i, dai_link) {
Liam Girdwooda655de82018-07-02 16:59:54 +01002045
2046 /* ignore this FE */
2047 if (dai_link->dynamic) {
2048 dai_link->ignore = true;
2049 continue;
2050 }
2051
2052 dev_info(card->dev, "info: override FE DAI link %s\n",
2053 card->dai_link[i].name);
2054
2055 /* override platform component */
Kuninori Morimotodaecf462018-08-31 03:10:08 +00002056 if (snd_soc_init_platform(card, dai_link) < 0) {
2057 dev_err(card->dev, "init platform error");
2058 continue;
2059 }
Kuninori Morimoto910fdca2019-01-21 09:32:32 +09002060 dai_link->platforms->name = component->name;
Liam Girdwooda655de82018-07-02 16:59:54 +01002061
2062 /* convert non BE into BE */
2063 dai_link->no_pcm = 1;
2064
2065 /* override any BE fixups */
2066 dai_link->be_hw_params_fixup =
2067 component->driver->be_hw_params_fixup;
2068
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002069 /*
2070 * most BE links don't set stream name, so set it to
Liam Girdwooda655de82018-07-02 16:59:54 +01002071 * dai link name if it's NULL to help bind widgets.
2072 */
2073 if (!dai_link->stream_name)
2074 dai_link->stream_name = dai_link->name;
2075 }
2076
2077 /* Inform userspace we are using alternate topology */
2078 if (component->driver->topology_name_prefix) {
2079
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002080 /* topology shortname created? */
Liam Girdwooda655de82018-07-02 16:59:54 +01002081 if (!card->topology_shortname_created) {
2082 comp_drv = component->driver;
2083
2084 snprintf(card->topology_shortname, 32, "%s-%s",
2085 comp_drv->topology_name_prefix,
2086 card->name);
2087 card->topology_shortname_created = true;
2088 }
2089
2090 /* use topology shortname */
2091 card->name = card->topology_shortname;
2092 }
2093 }
2094}
2095
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002096static int soc_cleanup_card_resources(struct snd_soc_card *card)
2097{
2098 /* free the ALSA card at first; this syncs with pending operations */
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002099 if (card->snd_card) {
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002100 snd_card_free(card->snd_card);
Kuninori Morimoto29040d12019-05-27 16:51:34 +09002101 card->snd_card = NULL;
2102 }
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002103
2104 /* remove and free each DAI */
2105 soc_remove_dai_links(card);
2106 soc_remove_pcm_runtimes(card);
Kuninori Morimoto08a58412019-06-06 13:07:22 +09002107 soc_cleanup_legacy(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002108
2109 /* remove auxiliary devices */
2110 soc_remove_aux_devices(card);
2111
2112 snd_soc_dapm_free(&card->dapm);
2113 soc_cleanup_card_debugfs(card);
2114
2115 /* remove the card */
2116 if (card->remove)
2117 card->remove(card);
2118
2119 return 0;
2120}
2121
Mark Brownb19e6e72012-03-14 21:18:39 +00002122static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002123{
Mengdong Lin1a497982015-11-18 02:34:11 -05002124 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002125 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002126 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002127
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002128 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002129 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002130
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002131 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2132 card->dapm.dev = card->dev;
2133 card->dapm.card = card;
2134 list_add(&card->dapm.list, &card->dapm_list);
2135
Liam Girdwooda655de82018-07-02 16:59:54 +01002136 /* check whether any platform is ignore machine FE and using topology */
2137 soc_check_tplg_fes(card);
2138
Mark Brownb19e6e72012-03-14 21:18:39 +00002139 /* bind DAIs */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002140 for_each_card_prelinks(card, i, dai_link) {
2141 ret = soc_bind_dai_link(card, dai_link);
Mark Brownb19e6e72012-03-14 21:18:39 +00002142 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002143 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002144 }
2145
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002146 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002147 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002148 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002149 if (ret != 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002150 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002151 }
2152
Mengdong Linf8f80362015-12-02 14:11:22 +08002153 /* add predefined DAI links to the list */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002154 for_each_card_prelinks(card, i, dai_link)
2155 snd_soc_add_dai_link(card, dai_link);
Mengdong Linf8f80362015-12-02 14:11:22 +08002156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002157 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002158 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002159 card->owner, 0, &card->snd_card);
2160 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002161 dev_err(card->dev,
2162 "ASoC: can't create sound card for card %s: %d\n",
2163 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002164 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002165 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002166
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002167 soc_init_card_debugfs(card);
2168
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002169#ifdef CONFIG_DEBUG_FS
2170 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2171#endif
2172
Mark Brown88ee1c62011-02-02 10:43:26 +00002173#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002174 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002175 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002176#endif
Andy Green6ed25972008-06-13 16:24:05 +01002177
Mark Brown9a841eb2011-04-12 17:51:37 -07002178 if (card->dapm_widgets)
2179 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2180 card->num_dapm_widgets);
2181
Nicolin Chenf23e8602015-02-14 17:22:49 -08002182 if (card->of_dapm_widgets)
2183 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2184 card->num_of_dapm_widgets);
2185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002186 /* initialise the sound card only once */
2187 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002188 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002189 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002190 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002191 }
2192
Stephen Warren62ae68f2012-06-08 12:34:23 -06002193 /* probe all components used by DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002194 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002195 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002196 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002197 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002198 dev_err(card->dev,
2199 "ASoC: failed to instantiate card %d\n",
2200 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002201 goto probe_end;
Stephen Warren62ae68f2012-06-08 12:34:23 -06002202 }
2203 }
2204 }
2205
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002206 /* probe auxiliary components */
2207 ret = soc_probe_aux_devices(card);
2208 if (ret < 0)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002209 goto probe_end;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002210
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002211 /*
2212 * Find new DAI links added during probing components and bind them.
Mengdong Lin61b00882015-12-02 14:11:48 +08002213 * Components with topology may bring new DAIs and DAI links.
2214 */
Kuninori Morimoto98061fd2018-09-18 01:29:16 +00002215 for_each_card_links(card, dai_link) {
Mengdong Lin61b00882015-12-02 14:11:48 +08002216 if (soc_is_dai_link_bound(card, dai_link))
2217 continue;
2218
2219 ret = soc_init_dai_link(card, dai_link);
2220 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002221 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002222 ret = soc_bind_dai_link(card, dai_link);
2223 if (ret)
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002224 goto probe_end;
Mengdong Lin61b00882015-12-02 14:11:48 +08002225 }
2226
Stephen Warren62ae68f2012-06-08 12:34:23 -06002227 /* probe all DAI links on this card */
Kuninori Morimoto1a1035a2018-09-18 01:30:41 +00002228 for_each_comp_order(order) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002229 for_each_card_rtds(card, rtd) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002230 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002231 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002232 dev_err(card->dev,
2233 "ASoC: failed to instantiate card %d\n",
2234 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002235 goto probe_end;
Liam Girdwood0168bf02011-06-07 16:08:05 +01002236 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002237 }
2238 }
2239
Mark Brown888df392012-02-16 19:37:51 -08002240 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002241 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002242
Mark Brownb7af1da2011-04-07 19:18:44 +09002243 if (card->controls)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002244 snd_soc_add_card_controls(card, card->controls,
2245 card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002246
Mark Brownb8ad29d2011-03-02 18:35:51 +00002247 if (card->dapm_routes)
2248 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2249 card->num_dapm_routes);
2250
Nicolin Chenf23e8602015-02-14 17:22:49 -08002251 if (card->of_dapm_routes)
2252 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2253 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002254
Takashi Iwai861886d2017-04-24 08:54:42 +02002255 /* try to set some sane longname if DMI is available */
2256 snd_soc_set_dmi_name(card, NULL);
2257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002259 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002260 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2261 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002262 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2263 "%s", card->driver_name ? card->driver_name : card->name);
2264 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2265 switch (card->snd_card->driver[i]) {
2266 case '_':
2267 case '-':
2268 case '\0':
2269 break;
2270 default:
2271 if (!isalnum(card->snd_card->driver[i]))
2272 card->snd_card->driver[i] = '_';
2273 break;
2274 }
2275 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002276
Mark Brown28e9ad92011-03-02 18:36:34 +00002277 if (card->late_probe) {
2278 ret = card->late_probe(card);
2279 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002280 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002281 card->name, ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002282 goto probe_end;
Mark Brown28e9ad92011-03-02 18:36:34 +00002283 }
2284 }
2285
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002286 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002287
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002288 ret = snd_card_register(card->snd_card);
2289 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002290 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2291 ret);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002292 goto probe_end;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002293 }
2294
Mark Brown435c5e22008-12-04 15:32:53 +00002295 card->instantiated = 1;
Tzung-Bi Shih882eab62018-11-14 17:06:13 +08002296 dapm_mark_endpoints_dirty(card);
Mark Brown4f4c0072011-10-07 14:29:19 +01002297 snd_soc_dapm_sync(&card->dapm);
Mark Brownb19e6e72012-03-14 21:18:39 +00002298
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002299probe_end:
2300 if (ret < 0)
2301 soc_cleanup_card_resources(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002302
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002303 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002304 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305
Mark Brownb19e6e72012-03-14 21:18:39 +00002306 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002307}
2308
2309/* probes a new socdev */
2310static int soc_probe(struct platform_device *pdev)
2311{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002312 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002313
Vinod Koul70a7ca32011-01-14 19:22:48 +05302314 /*
2315 * no card, so machine driver should be registering card
2316 * we should not be here in that case so ret error
2317 */
2318 if (!card)
2319 return -EINVAL;
2320
Mark Brownfe4085e2012-03-02 13:07:41 +00002321 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002322 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002323 card->name);
2324
Mark Brown435c5e22008-12-04 15:32:53 +00002325 /* Bodge while we unpick instantiation */
2326 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002327
Mark Brown28d528c2012-08-09 18:45:23 +01002328 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329}
2330
2331/* removes a socdev */
2332static int soc_remove(struct platform_device *pdev)
2333{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002334 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335
Mark Brownc5af3a22008-11-28 13:29:45 +00002336 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337 return 0;
2338}
2339
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002340int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002341{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002342 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002343 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002344
2345 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002346 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002347
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002348 /*
2349 * Flush out pmdown_time work - we actually do want to run it
2350 * now, we're shutting down so no imminent restart.
2351 */
Kuninori Morimoto65462e442019-01-21 09:32:37 +09002352 snd_soc_flush_all_delayed_work(card);
Mark Brown51737472009-06-22 13:16:51 +01002353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002354 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002355
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002356 /* deactivate pins to sleep state */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002357 for_each_card_rtds(card, rtd) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002358 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002359 struct snd_soc_dai *codec_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002360 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002361
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002362 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002363 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002364 pinctrl_pm_select_sleep_state(codec_dai->dev);
2365 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002366 }
2367
Mark Brown416356f2009-06-30 19:05:15 +01002368 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002369}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002370EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002371
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002372const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302373 .suspend = snd_soc_suspend,
2374 .resume = snd_soc_resume,
2375 .freeze = snd_soc_suspend,
2376 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002377 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302378 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002379};
Stephen Warrendeb26072011-04-05 19:35:30 -06002380EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002381
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002382/* ASoC platform driver */
2383static struct platform_driver soc_driver = {
2384 .driver = {
2385 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002386 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387 },
2388 .probe = soc_probe,
2389 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390};
2391
Mark Brown096e49d2009-07-05 15:12:22 +01002392/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 * snd_soc_cnew - create new control
2394 * @_template: control template
2395 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002396 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002397 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398 *
2399 * Create a new mixer control from a template control.
2400 *
2401 * Returns 0 for success, else error.
2402 */
2403struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002404 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002405 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406{
2407 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002408 struct snd_kcontrol *kcontrol;
2409 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002410
2411 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412 template.index = 0;
2413
Mark Brownefb7ac32011-03-08 17:23:24 +00002414 if (!long_name)
2415 long_name = template.name;
2416
2417 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002418 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002419 if (!name)
2420 return NULL;
2421
Mark Brownefb7ac32011-03-08 17:23:24 +00002422 template.name = name;
2423 } else {
2424 template.name = long_name;
2425 }
2426
2427 kcontrol = snd_ctl_new1(&template, data);
2428
2429 kfree(name);
2430
2431 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432}
2433EXPORT_SYMBOL_GPL(snd_soc_cnew);
2434
Liam Girdwood022658b2012-02-03 17:43:09 +00002435static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2436 const struct snd_kcontrol_new *controls, int num_controls,
2437 const char *prefix, void *data)
2438{
2439 int err, i;
2440
2441 for (i = 0; i < num_controls; i++) {
2442 const struct snd_kcontrol_new *control = &controls[i];
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002443
Liam Girdwood022658b2012-02-03 17:43:09 +00002444 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2445 control->name, prefix));
2446 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002447 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2448 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002449 return err;
2450 }
2451 }
2452
2453 return 0;
2454}
2455
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002456struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2457 const char *name)
2458{
2459 struct snd_card *card = soc_card->snd_card;
2460 struct snd_kcontrol *kctl;
2461
2462 if (unlikely(!name))
2463 return NULL;
2464
2465 list_for_each_entry(kctl, &card->controls, list)
2466 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2467 return kctl;
2468 return NULL;
2469}
2470EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2471
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002473 * snd_soc_add_component_controls - Add an array of controls to a component.
2474 *
2475 * @component: Component to add controls to
2476 * @controls: Array of controls to add
2477 * @num_controls: Number of elements in the array
2478 *
2479 * Return: 0 for success, else error.
2480 */
2481int snd_soc_add_component_controls(struct snd_soc_component *component,
2482 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2483{
2484 struct snd_card *card = component->card->snd_card;
2485
2486 return snd_soc_add_controls(card, component->dev, controls,
2487 num_controls, component->name_prefix, component);
2488}
2489EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2490
2491/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002492 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2493 * Convenience function to add a list of controls.
2494 *
2495 * @soc_card: SoC card to add controls to
2496 * @controls: array of controls to add
2497 * @num_controls: number of elements in the array
2498 *
2499 * Return 0 for success, else error.
2500 */
2501int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2502 const struct snd_kcontrol_new *controls, int num_controls)
2503{
2504 struct snd_card *card = soc_card->snd_card;
2505
2506 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2507 NULL, soc_card);
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2510
2511/**
2512 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2513 * Convienience function to add a list of controls.
2514 *
2515 * @dai: DAI to add controls to
2516 * @controls: array of controls to add
2517 * @num_controls: number of elements in the array
2518 *
2519 * Return 0 for success, else error.
2520 */
2521int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2522 const struct snd_kcontrol_new *controls, int num_controls)
2523{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002524 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002525
2526 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2527 NULL, dai);
2528}
2529EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2530
2531/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002532 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2533 * @dai: DAI
2534 * @clk_id: DAI specific clock ID
2535 * @freq: new clock frequency in Hz
2536 * @dir: new clock direction - input/output.
2537 *
2538 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2539 */
2540int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2541 unsigned int freq, int dir)
2542{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002543 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002544 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002545
2546 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2547 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002548}
2549EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2550
2551/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002552 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2553 * @component: COMPONENT
2554 * @clk_id: DAI specific clock ID
2555 * @source: Source for the clock
2556 * @freq: new clock frequency in Hz
2557 * @dir: new clock direction - input/output.
2558 *
2559 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2560 */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002561int snd_soc_component_set_sysclk(struct snd_soc_component *component,
2562 int clk_id, int source, unsigned int freq,
2563 int dir)
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002564{
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002565 if (component->driver->set_sysclk)
2566 return component->driver->set_sysclk(component, clk_id, source,
2567 freq, dir);
2568
2569 return -ENOTSUPP;
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2572
2573/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002574 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2575 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002576 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002577 * @div: new clock divisor.
2578 *
2579 * Configures the clock dividers. This is used to derive the best DAI bit and
2580 * frame clocks from the system or master clock. It's best to set the DAI bit
2581 * and frame clocks as low as possible to save system power.
2582 */
2583int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2584 int div_id, int div)
2585{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002586 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002587 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002588 else
2589 return -EINVAL;
2590}
2591EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2592
2593/**
2594 * snd_soc_dai_set_pll - configure DAI PLL.
2595 * @dai: DAI
2596 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002597 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002598 * @freq_in: PLL input clock frequency in Hz
2599 * @freq_out: requested PLL output clock frequency in Hz
2600 *
2601 * Configures and enables PLL to generate output clock based on input clock.
2602 */
Mark Brown85488032009-09-05 18:52:16 +01002603int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2604 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002605{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002606 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002607 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002608 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002609
2610 return snd_soc_component_set_pll(dai->component, pll_id, source,
2611 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002612}
2613EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2614
Mark Brownec4ee522011-03-07 20:58:11 +00002615/*
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002616 * snd_soc_component_set_pll - configure component PLL.
2617 * @component: COMPONENT
2618 * @pll_id: DAI specific PLL ID
2619 * @source: DAI specific source for the PLL
2620 * @freq_in: PLL input clock frequency in Hz
2621 * @freq_out: requested PLL output clock frequency in Hz
2622 *
2623 * Configures and enables PLL to generate output clock based on input clock.
2624 */
2625int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2626 int source, unsigned int freq_in,
2627 unsigned int freq_out)
2628{
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002629 if (component->driver->set_pll)
2630 return component->driver->set_pll(component, pll_id, source,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002631 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002632
2633 return -EINVAL;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2636
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002637/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002638 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2639 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002640 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002641 *
2642 * Configures the DAI for a preset BCLK to sample rate ratio.
2643 */
2644int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2645{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002646 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002647 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2648 else
2649 return -EINVAL;
2650}
2651EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2652
2653/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002654 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2655 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002656 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002657 *
2658 * Configures the DAI hardware format and clocking.
2659 */
2660int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2661{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002662 if (dai->driver->ops->set_fmt == NULL)
2663 return -ENOTSUPP;
2664 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002665}
2666EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2667
2668/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002669 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002670 * @slots: Number of slots in use.
2671 * @tx_mask: bitmask representing active TX slots.
2672 * @rx_mask: bitmask representing active RX slots.
2673 *
2674 * Generates the TDM tx and rx slot default masks for DAI.
2675 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002676static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002677 unsigned int *tx_mask,
2678 unsigned int *rx_mask)
Xiubo Li89c67852014-02-14 09:34:35 +08002679{
2680 if (*tx_mask || *rx_mask)
2681 return 0;
2682
2683 if (!slots)
2684 return -EINVAL;
2685
2686 *tx_mask = (1 << slots) - 1;
2687 *rx_mask = (1 << slots) - 1;
2688
2689 return 0;
2690}
2691
2692/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002693 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2694 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002695 * @tx_mask: bitmask representing active TX slots.
2696 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002697 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002698 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002699 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002700 * This function configures the specified DAI for TDM operation. @slot contains
2701 * the total number of slots of the TDM stream and @slot_with the width of each
2702 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2703 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2704 * DAI should write to or read from. If a bit is set the corresponding slot is
2705 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2706 * the first slot, bit 1 to the second slot and so on. The first active slot
2707 * maps to the first channel of the DAI, the second active slot to the second
2708 * channel and so on.
2709 *
2710 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2711 * @rx_mask and @slot_width will be ignored.
2712 *
2713 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002714 */
2715int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002716 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002717{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002718 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002719 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002720 &tx_mask, &rx_mask);
2721 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002722 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002723
Benoit Cousson88bd8702014-07-08 23:19:34 +02002724 dai->tx_mask = tx_mask;
2725 dai->rx_mask = rx_mask;
2726
Kuninori Morimoto46471922017-09-25 01:38:34 +00002727 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002728 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002729 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002730 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002731 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002732}
2733EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2734
2735/**
Barry Song472df3c2009-09-12 01:16:29 +08002736 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2737 * @dai: DAI
2738 * @tx_num: how many TX channels
2739 * @tx_slot: pointer to an array which imply the TX slot number channel
2740 * 0~num-1 uses
2741 * @rx_num: how many RX channels
2742 * @rx_slot: pointer to an array which imply the RX slot number channel
2743 * 0~num-1 uses
2744 *
2745 * configure the relationship between channel number and TDM slot number.
2746 */
2747int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2748 unsigned int tx_num, unsigned int *tx_slot,
2749 unsigned int rx_num, unsigned int *rx_slot)
2750{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002751 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002752 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002753 rx_num, rx_slot);
2754 else
2755 return -EINVAL;
2756}
2757EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2758
2759/**
Srinivas Kandagatla467b0612018-07-23 16:54:03 +01002760 * snd_soc_dai_get_channel_map - Get DAI audio channel map
2761 * @dai: DAI
2762 * @tx_num: how many TX channels
2763 * @tx_slot: pointer to an array which imply the TX slot number channel
2764 * 0~num-1 uses
2765 * @rx_num: how many RX channels
2766 * @rx_slot: pointer to an array which imply the RX slot number channel
2767 * 0~num-1 uses
2768 */
2769int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
2770 unsigned int *tx_num, unsigned int *tx_slot,
2771 unsigned int *rx_num, unsigned int *rx_slot)
2772{
2773 if (dai->driver->ops->get_channel_map)
2774 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
2775 rx_num, rx_slot);
2776 else
2777 return -ENOTSUPP;
2778}
2779EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
2780
2781/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002782 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2783 * @dai: DAI
2784 * @tristate: tristate enable
2785 *
2786 * Tristates the DAI so that others can use it.
2787 */
2788int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2789{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002790 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002791 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002792 else
2793 return -EINVAL;
2794}
2795EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2796
2797/**
2798 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2799 * @dai: DAI
2800 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002801 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002802 *
2803 * Mutes the DAI DAC.
2804 */
Mark Brownda183962013-02-06 15:44:07 +00002805int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2806 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002807{
Mark Brownda183962013-02-06 15:44:07 +00002808 if (dai->driver->ops->mute_stream)
2809 return dai->driver->ops->mute_stream(dai, mute, direction);
2810 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2811 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002812 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002813 else
Mark Brown04570c62012-04-13 19:16:03 +01002814 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002815}
2816EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2817
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002818static int snd_soc_bind_card(struct snd_soc_card *card)
2819{
2820 struct snd_soc_pcm_runtime *rtd;
2821 int ret;
2822
2823 ret = snd_soc_instantiate_card(card);
2824 if (ret != 0)
2825 return ret;
2826
2827 /* deactivate pins to sleep state */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002828 for_each_card_rtds(card, rtd) {
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002829 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2830 struct snd_soc_dai *codec_dai;
2831 int j;
2832
2833 for_each_rtd_codec_dai(rtd, j, codec_dai) {
2834 if (!codec_dai->active)
2835 pinctrl_pm_select_sleep_state(codec_dai->dev);
2836 }
2837
2838 if (!cpu_dai->active)
2839 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2840 }
2841
2842 return ret;
2843}
2844
Mark Brownc5af3a22008-11-28 13:29:45 +00002845/**
2846 * snd_soc_register_card - Register a card with the ASoC core
2847 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002848 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002849 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002850 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302851int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002852{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002853 int i, ret;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002854 struct snd_soc_dai_link *link;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002855
Mark Brownc5af3a22008-11-28 13:29:45 +00002856 if (!card->name || !card->dev)
2857 return -EINVAL;
2858
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302859 mutex_lock(&client_mutex);
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +00002860 for_each_card_prelinks(card, i, link) {
Stephen Warren5a504962011-12-21 10:40:59 -07002861
Mengdong Lin923c5e612015-11-23 11:01:49 -05002862 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002863 if (ret) {
Kuninori Morimoto08a58412019-06-06 13:07:22 +09002864 soc_cleanup_legacy(card);
Mengdong Lin923c5e612015-11-23 11:01:49 -05002865 dev_err(card->dev, "ASoC: failed to init link %s\n",
2866 link->name);
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302867 mutex_unlock(&client_mutex);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002868 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002869 }
Stephen Warren5a504962011-12-21 10:40:59 -07002870 }
Rohit kumar04eb1ef2019-01-10 14:32:41 +05302871 mutex_unlock(&client_mutex);
Stephen Warren5a504962011-12-21 10:40:59 -07002872
Mark Browned77cc12011-05-03 18:25:34 +01002873 dev_set_drvdata(card->dev, card);
2874
Stephen Warren111c6412011-01-28 14:26:35 -07002875 snd_soc_initialize_card_lists(card);
2876
Mengdong Linf8f80362015-12-02 14:11:22 +08002877 INIT_LIST_HEAD(&card->dai_link_list);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002878
Mengdong Lin1a497982015-11-18 02:34:11 -05002879 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002880 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002881
Mark Browndb432b42011-10-03 21:06:40 +01002882 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002883 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002884 card->instantiated = 0;
2885 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002886 mutex_init(&card->dapm_mutex);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002887 spin_lock_init(&card->dpcm_lock);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002888
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002889 return snd_soc_bind_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002890}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302891EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002892
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002893static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister)
2894{
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002895 struct snd_soc_pcm_runtime *rtd;
2896 int order;
2897
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002898 if (card->instantiated) {
2899 card->instantiated = false;
2900 snd_soc_dapm_shutdown(card);
Kuninori Morimoto53e947a2019-01-21 09:32:42 +09002901 snd_soc_flush_all_delayed_work(card);
Ranjani Sridharanf96fb7d2019-04-04 17:30:40 -07002902
2903 /* remove all components used by DAI links on this card */
2904 for_each_comp_order(order) {
2905 for_each_card_rtds(card, rtd) {
2906 soc_remove_link_components(card, rtd, order);
2907 }
2908 }
2909
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002910 soc_cleanup_card_resources(card);
2911 if (!unregister)
2912 list_add(&card->list, &unbind_card_list);
2913 } else {
2914 if (unregister)
2915 list_del(&card->list);
2916 }
2917}
2918
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002919/**
2920 * snd_soc_unregister_card - Unregister a card with the ASoC core
2921 *
2922 * @card: Card to unregister
2923 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002924 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302925int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002926{
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01002927 snd_soc_unbind_card(card, true);
2928 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002929
2930 return 0;
2931}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302932EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002933
2934/*
2935 * Simplify DAI link configuration by removing ".-1" from device names
2936 * and sanitizing names.
2937 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002938static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002939{
2940 char *found, name[NAME_SIZE];
2941 int id1, id2;
2942
2943 if (dev_name(dev) == NULL)
2944 return NULL;
2945
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002946 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002947
2948 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002949 found = strstr(name, dev->driver->name);
2950 if (found) {
2951 /* get ID */
2952 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2953
2954 /* discard ID from name if ID == -1 */
2955 if (*id == -1)
2956 found[strlen(dev->driver->name)] = '\0';
2957 }
2958
2959 } else {
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002960 /* I2C component devices are named "bus-addr" */
Mark Brownc5af3a22008-11-28 13:29:45 +00002961 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2962 char tmp[NAME_SIZE];
2963
2964 /* create unique ID number from I2C addr and bus */
2965 *id = ((id1 & 0xffff) << 16) + id2;
2966
2967 /* sanitize component name for DAI link creation */
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02002968 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name,
2969 name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002970 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002971 } else
2972 *id = 0;
2973 }
2974
2975 return kstrdup(name, GFP_KERNEL);
2976}
2977
2978/*
2979 * Simplify DAI link naming for single devices with multiple DAIs by removing
2980 * any ".-1" and using the DAI name (instead of device name).
2981 */
2982static inline char *fmt_multiple_name(struct device *dev,
2983 struct snd_soc_dai_driver *dai_drv)
2984{
2985 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002986 dev_err(dev,
2987 "ASoC: error - multiple DAI %s registered with no name\n",
2988 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002989 return NULL;
2990 }
2991
2992 return kstrdup(dai_drv->name, GFP_KERNEL);
2993}
2994
2995/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002996 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002997 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002998 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002999 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003000static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003001{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003002 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003003
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003004 for_each_component_dais_safe(component, dai, _dai) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003005 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3006 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003007 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003008 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003009 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003010 }
Mark Brown91151712008-11-30 23:31:24 +00003011}
Mark Brown91151712008-11-30 23:31:24 +00003012
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003013/* Create a DAI and add it to the component's DAI list */
3014static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3015 struct snd_soc_dai_driver *dai_drv,
3016 bool legacy_dai_naming)
3017{
3018 struct device *dev = component->dev;
3019 struct snd_soc_dai *dai;
3020
3021 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3022
3023 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3024 if (dai == NULL)
3025 return NULL;
3026
3027 /*
3028 * Back in the old days when we still had component-less DAIs,
3029 * instead of having a static name, component-less DAIs would
3030 * inherit the name of the parent device so it is possible to
3031 * register multiple instances of the DAI. We still need to keep
3032 * the same naming style even though those DAIs are not
3033 * component-less anymore.
3034 */
3035 if (legacy_dai_naming &&
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003036 (dai_drv->id == 0 || dai_drv->name == NULL)) {
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003037 dai->name = fmt_single_name(dev, &dai->id);
3038 } else {
3039 dai->name = fmt_multiple_name(dev, dai_drv);
3040 if (dai_drv->id)
3041 dai->id = dai_drv->id;
3042 else
3043 dai->id = component->num_dai;
3044 }
3045 if (dai->name == NULL) {
3046 kfree(dai);
3047 return NULL;
3048 }
3049
3050 dai->component = component;
3051 dai->dev = dev;
3052 dai->driver = dai_drv;
3053 if (!dai->driver->ops)
3054 dai->driver->ops = &null_dai_ops;
3055
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003056 /* see for_each_component_dais */
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003057 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003058 component->num_dai++;
3059
3060 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3061 return dai;
3062}
3063
Mark Brown91151712008-11-30 23:31:24 +00003064/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003065 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003066 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003067 * @component: The component the DAIs are registered for
3068 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003069 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003070 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003071static int snd_soc_register_dais(struct snd_soc_component *component,
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003072 struct snd_soc_dai_driver *dai_drv,
3073 size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003074{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003075 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003076 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003077 unsigned int i;
3078 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003080 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003081
3082 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003083
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003084 dai = soc_add_dai(component, dai_drv + i, count == 1 &&
3085 !component->driver->non_legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003086 if (dai == NULL) {
3087 ret = -ENOMEM;
3088 goto err;
3089 }
Mark Brown91151712008-11-30 23:31:24 +00003090 }
3091
3092 return 0;
3093
3094err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003095 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003096
3097 return ret;
3098}
Mark Brown91151712008-11-30 23:31:24 +00003099
Mengdong Lin68003e62015-12-31 16:40:43 +08003100/**
3101 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3102 *
3103 * @component: The component the DAIs are registered for
3104 * @dai_drv: DAI driver to use for the DAI
3105 *
3106 * Topology can use this API to register DAIs when probing a component.
3107 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3108 * will be freed in the component cleanup.
3109 */
3110int snd_soc_register_dai(struct snd_soc_component *component,
3111 struct snd_soc_dai_driver *dai_drv)
3112{
3113 struct snd_soc_dapm_context *dapm =
3114 snd_soc_component_get_dapm(component);
3115 struct snd_soc_dai *dai;
3116 int ret;
3117
3118 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3119 dev_err(component->dev, "Invalid dai type %d\n",
3120 dai_drv->dobj.type);
3121 return -EINVAL;
3122 }
3123
3124 lockdep_assert_held(&client_mutex);
3125 dai = soc_add_dai(component, dai_drv, false);
3126 if (!dai)
3127 return -ENOMEM;
3128
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003129 /*
3130 * Create the DAI widgets here. After adding DAIs, topology may
Mengdong Lin68003e62015-12-31 16:40:43 +08003131 * also add routes that need these widgets as source or sink.
3132 */
3133 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3134 if (ret != 0) {
3135 dev_err(component->dev,
3136 "Failed to create DAI widgets %d\n", ret);
3137 }
3138
3139 return ret;
3140}
3141EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3142
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003143static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3144 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003145{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003146 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003147
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003148 component->driver->seq_notifier(component, type, subseq);
3149}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003150
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003151static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3152 int event)
3153{
3154 struct snd_soc_component *component = dapm->component;
3155
3156 return component->driver->stream_event(component, event);
3157}
3158
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003159static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3160 enum snd_soc_bias_level level)
3161{
3162 struct snd_soc_component *component = dapm->component;
3163
3164 return component->driver->set_bias_level(component, level);
3165}
3166
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003167static int snd_soc_component_initialize(struct snd_soc_component *component,
3168 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003169{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003170 struct snd_soc_dapm_context *dapm;
3171
Krzysztof Kozlowski9d563eb2019-05-31 12:34:02 +02003172 component->name = fmt_single_name(dev, &component->id);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003173 if (!component->name) {
3174 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003175 return -ENOMEM;
3176 }
3177
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003178 component->dev = dev;
3179 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003180
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003181 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003182 dapm->dev = dev;
3183 dapm->component = component;
3184 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003185 dapm->idle_bias_off = !driver->idle_bias_on;
3186 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003187 if (driver->seq_notifier)
3188 dapm->seq_notifier = snd_soc_component_seq_notifier;
3189 if (driver->stream_event)
3190 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003191 if (driver->set_bias_level)
3192 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003193
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003194 INIT_LIST_HEAD(&component->dai_list);
3195 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003196
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003197 return 0;
3198}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003199
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003200static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003201{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003202 int val_bytes = regmap_get_val_bytes(component->regmap);
3203
3204 /* Errors are legitimate for non-integer byte multiples */
3205 if (val_bytes > 0)
3206 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003207}
3208
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003209#ifdef CONFIG_REGMAP
3210
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003211/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003212 * snd_soc_component_init_regmap() - Initialize regmap instance for the
3213 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003214 * @component: The component for which to initialize the regmap instance
3215 * @regmap: The regmap instance that should be used by the component
3216 *
3217 * This function allows deferred assignment of the regmap instance that is
3218 * associated with the component. Only use this if the regmap instance is not
3219 * yet ready when the component is registered. The function must also be called
3220 * before the first IO attempt of the component.
3221 */
3222void snd_soc_component_init_regmap(struct snd_soc_component *component,
3223 struct regmap *regmap)
3224{
3225 component->regmap = regmap;
3226 snd_soc_component_setup_regmap(component);
3227}
3228EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3229
3230/**
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003231 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
3232 * component
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003233 * @component: The component for which to de-initialize the regmap instance
3234 *
3235 * Calls regmap_exit() on the regmap instance associated to the component and
3236 * removes the regmap instance from the component.
3237 *
3238 * This function should only be used if snd_soc_component_init_regmap() was used
3239 * to initialize the regmap instance.
3240 */
3241void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3242{
3243 regmap_exit(component->regmap);
3244 component->regmap = NULL;
3245}
3246EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3247
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003248#endif
3249
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003250static void snd_soc_component_add(struct snd_soc_component *component)
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003251{
Kuninori Morimoto359c71e2018-05-08 03:22:11 +00003252 mutex_lock(&client_mutex);
3253
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003254 if (!component->driver->write && !component->driver->read) {
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003255 if (!component->regmap)
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003256 component->regmap = dev_get_regmap(component->dev,
3257 NULL);
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003258 if (component->regmap)
3259 snd_soc_component_setup_regmap(component);
3260 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003261
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003262 /* see for_each_component */
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003263 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003264 INIT_LIST_HEAD(&component->dobj_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003265
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003266 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003267}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003268
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003269static void snd_soc_component_cleanup(struct snd_soc_component *component)
3270{
3271 snd_soc_unregister_dais(component);
3272 kfree(component->name);
3273}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003274
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003275static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3276{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003277 struct snd_soc_card *card = component->card;
3278
3279 if (card)
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003280 snd_soc_unbind_card(card, false);
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003281
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003282 list_del(&component->list);
3283}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003284
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003285#define ENDIANNESS_MAP(name) \
3286 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3287static u64 endianness_format_map[] = {
3288 ENDIANNESS_MAP(S16_),
3289 ENDIANNESS_MAP(U16_),
3290 ENDIANNESS_MAP(S24_),
3291 ENDIANNESS_MAP(U24_),
3292 ENDIANNESS_MAP(S32_),
3293 ENDIANNESS_MAP(U32_),
3294 ENDIANNESS_MAP(S24_3),
3295 ENDIANNESS_MAP(U24_3),
3296 ENDIANNESS_MAP(S20_3),
3297 ENDIANNESS_MAP(U20_3),
3298 ENDIANNESS_MAP(S18_3),
3299 ENDIANNESS_MAP(U18_3),
3300 ENDIANNESS_MAP(FLOAT_),
3301 ENDIANNESS_MAP(FLOAT64_),
3302 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3303};
3304
3305/*
3306 * Fix up the DAI formats for endianness: codecs don't actually see
3307 * the endianness of the data but we're using the CPU format
3308 * definitions which do need to include endianness so we ensure that
3309 * codec DAIs always have both big and little endian variants set.
3310 */
3311static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3312{
3313 int i;
3314
3315 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3316 if (stream->formats & endianness_format_map[i])
3317 stream->formats |= endianness_format_map[i];
3318}
3319
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003320static void snd_soc_try_rebind_card(void)
3321{
3322 struct snd_soc_card *card, *c;
3323
3324 if (!list_empty(&unbind_card_list)) {
3325 list_for_each_entry_safe(card, c, &unbind_card_list, list) {
3326 if (!snd_soc_bind_card(card))
3327 list_del(&card->list);
3328 }
3329 }
3330}
3331
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003332int snd_soc_add_component(struct device *dev,
3333 struct snd_soc_component *component,
3334 const struct snd_soc_component_driver *component_driver,
3335 struct snd_soc_dai_driver *dai_drv,
3336 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003337{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003338 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003339 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003340
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003341 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003342 if (ret)
3343 goto err_free;
3344
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003345 if (component_driver->endianness) {
3346 for (i = 0; i < num_dai; i++) {
3347 convert_endianness_formats(&dai_drv[i].playback);
3348 convert_endianness_formats(&dai_drv[i].capture);
3349 }
3350 }
3351
Kuninori Morimoto0e7b25c2018-05-08 03:23:01 +00003352 ret = snd_soc_register_dais(component, dai_drv, num_dai);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003353 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003354 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003355 goto err_cleanup;
3356 }
3357
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003358 snd_soc_component_add(component);
Srinivas Kandagatlae894efe2018-09-12 10:15:00 +01003359 snd_soc_try_rebind_card();
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003360
3361 return 0;
3362
3363err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003364 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003365err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003366 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003367}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003368EXPORT_SYMBOL_GPL(snd_soc_add_component);
3369
3370int snd_soc_register_component(struct device *dev,
3371 const struct snd_soc_component_driver *component_driver,
3372 struct snd_soc_dai_driver *dai_drv,
3373 int num_dai)
3374{
3375 struct snd_soc_component *component;
3376
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003377 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003378 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003379 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003380
3381 return snd_soc_add_component(dev, component, component_driver,
3382 dai_drv, num_dai);
3383}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003384EXPORT_SYMBOL_GPL(snd_soc_register_component);
3385
3386/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003387 * snd_soc_unregister_component - Unregister all related component
3388 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003389 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003390 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003391 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003392static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003393{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003394 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003395 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003396
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003397 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003398 for_each_component(component) {
Kuninori Morimoto999f7f52018-05-08 03:20:24 +00003399 if (dev != component->dev)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003400 continue;
3401
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003402 snd_soc_tplg_component_remove(component,
3403 SND_SOC_TPLG_INDEX_ALL);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003404 snd_soc_component_del_unlocked(component);
3405 found = 1;
3406 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003407 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003408 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003409
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003410 if (found)
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003411 snd_soc_component_cleanup(component);
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003412
3413 return found;
3414}
3415
3416void snd_soc_unregister_component(struct device *dev)
3417{
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003418 while (__snd_soc_unregister_component(dev))
3419 ;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003420}
3421EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3422
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003423struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3424 const char *driver_name)
3425{
3426 struct snd_soc_component *component;
3427 struct snd_soc_component *ret;
3428
3429 ret = NULL;
3430 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003431 for_each_component(component) {
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003432 if (dev != component->dev)
3433 continue;
3434
3435 if (driver_name &&
3436 (driver_name != component->driver->name) &&
3437 (strcmp(component->driver->name, driver_name) != 0))
3438 continue;
3439
3440 ret = component;
3441 break;
3442 }
3443 mutex_unlock(&client_mutex);
3444
3445 return ret;
3446}
3447EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3448
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003449/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003450int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3451 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003452{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003453 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003454 int ret;
3455
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303456 if (!card->dev) {
3457 pr_err("card->dev is not set before calling %s\n", __func__);
3458 return -EINVAL;
3459 }
3460
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003461 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303462
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003463 ret = of_property_read_string_index(np, propname, 0, &card->name);
3464 /*
3465 * EINVAL means the property does not exist. This is fine providing
3466 * card->name was previously set, which is checked later in
3467 * snd_soc_register_card.
3468 */
3469 if (ret < 0 && ret != -EINVAL) {
3470 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003471 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003472 propname, ret);
3473 return ret;
3474 }
3475
3476 return 0;
3477}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003478EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003479
Xiubo Li9a6d4862014-02-08 15:59:52 +08003480static const struct snd_soc_dapm_widget simple_widgets[] = {
3481 SND_SOC_DAPM_MIC("Microphone", NULL),
3482 SND_SOC_DAPM_LINE("Line", NULL),
3483 SND_SOC_DAPM_HP("Headphone", NULL),
3484 SND_SOC_DAPM_SPK("Speaker", NULL),
3485};
3486
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003487int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003488 const char *propname)
3489{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003490 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003491 struct snd_soc_dapm_widget *widgets;
3492 const char *template, *wname;
3493 int i, j, num_widgets, ret;
3494
3495 num_widgets = of_property_count_strings(np, propname);
3496 if (num_widgets < 0) {
3497 dev_err(card->dev,
3498 "ASoC: Property '%s' does not exist\n", propname);
3499 return -EINVAL;
3500 }
3501 if (num_widgets & 1) {
3502 dev_err(card->dev,
3503 "ASoC: Property '%s' length is not even\n", propname);
3504 return -EINVAL;
3505 }
3506
3507 num_widgets /= 2;
3508 if (!num_widgets) {
3509 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3510 propname);
3511 return -EINVAL;
3512 }
3513
3514 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3515 GFP_KERNEL);
3516 if (!widgets) {
3517 dev_err(card->dev,
3518 "ASoC: Could not allocate memory for widgets\n");
3519 return -ENOMEM;
3520 }
3521
3522 for (i = 0; i < num_widgets; i++) {
3523 ret = of_property_read_string_index(np, propname,
3524 2 * i, &template);
3525 if (ret) {
3526 dev_err(card->dev,
3527 "ASoC: Property '%s' index %d read error:%d\n",
3528 propname, 2 * i, ret);
3529 return -EINVAL;
3530 }
3531
3532 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3533 if (!strncmp(template, simple_widgets[j].name,
3534 strlen(simple_widgets[j].name))) {
3535 widgets[i] = simple_widgets[j];
3536 break;
3537 }
3538 }
3539
3540 if (j >= ARRAY_SIZE(simple_widgets)) {
3541 dev_err(card->dev,
3542 "ASoC: DAPM widget '%s' is not supported\n",
3543 template);
3544 return -EINVAL;
3545 }
3546
3547 ret = of_property_read_string_index(np, propname,
3548 (2 * i) + 1,
3549 &wname);
3550 if (ret) {
3551 dev_err(card->dev,
3552 "ASoC: Property '%s' index %d read error:%d\n",
3553 propname, (2 * i) + 1, ret);
3554 return -EINVAL;
3555 }
3556
3557 widgets[i].name = wname;
3558 }
3559
Nicolin Chenf23e8602015-02-14 17:22:49 -08003560 card->of_dapm_widgets = widgets;
3561 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003562
3563 return 0;
3564}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003565EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003566
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003567int snd_soc_of_get_slot_mask(struct device_node *np,
3568 const char *prop_name,
3569 unsigned int *mask)
Jyri Sarha61310842015-09-09 21:27:43 +03003570{
3571 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003572 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003573 int i;
3574
3575 if (!of_slot_mask)
3576 return 0;
3577 val /= sizeof(u32);
3578 for (i = 0; i < val; i++)
3579 if (be32_to_cpup(&of_slot_mask[i]))
3580 *mask |= (1 << i);
3581
3582 return val;
3583}
Jerome Brunetcbdfab32018-07-17 17:43:02 +02003584EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
Jyri Sarha61310842015-09-09 21:27:43 +03003585
Xiubo Li89c67852014-02-14 09:34:35 +08003586int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003587 unsigned int *tx_mask,
3588 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003589 unsigned int *slots,
3590 unsigned int *slot_width)
3591{
3592 u32 val;
3593 int ret;
3594
Jyri Sarha61310842015-09-09 21:27:43 +03003595 if (tx_mask)
3596 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3597 if (rx_mask)
3598 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3599
Xiubo Li89c67852014-02-14 09:34:35 +08003600 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3601 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3602 if (ret)
3603 return ret;
3604
3605 if (slots)
3606 *slots = val;
3607 }
3608
3609 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3610 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3611 if (ret)
3612 return ret;
3613
3614 if (slot_width)
3615 *slot_width = val;
3616 }
3617
3618 return 0;
3619}
3620EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3621
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003622void snd_soc_of_parse_node_prefix(struct device_node *np,
3623 struct snd_soc_codec_conf *codec_conf,
3624 struct device_node *of_node,
3625 const char *propname)
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003626{
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003627 const char *str;
3628 int ret;
3629
3630 ret = of_property_read_string(np, propname, &str);
3631 if (ret < 0) {
3632 /* no prefix is not error */
3633 return;
3634 }
3635
3636 codec_conf->of_node = of_node;
3637 codec_conf->name_prefix = str;
3638}
Kuninori Morimoto3b710352018-11-22 00:55:09 +00003639EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003640
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003641int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003642 const char *propname)
3643{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003644 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003645 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003646 struct snd_soc_dapm_route *routes;
3647 int i, ret;
3648
3649 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003650 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003651 dev_err(card->dev,
3652 "ASoC: Property '%s' does not exist or its length is not even\n",
3653 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003654 return -EINVAL;
3655 }
3656 num_routes /= 2;
3657 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003658 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003659 propname);
3660 return -EINVAL;
3661 }
3662
Kees Cooka86854d2018-06-12 14:07:58 -07003663 routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003664 GFP_KERNEL);
3665 if (!routes) {
3666 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003667 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003668 return -EINVAL;
3669 }
3670
3671 for (i = 0; i < num_routes; i++) {
3672 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003673 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003674 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003675 dev_err(card->dev,
3676 "ASoC: Property '%s' index %d could not be read: %d\n",
3677 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003678 return -EINVAL;
3679 }
3680 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003681 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003682 if (ret) {
3683 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003684 "ASoC: Property '%s' index %d could not be read: %d\n",
3685 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003686 return -EINVAL;
3687 }
3688 }
3689
Nicolin Chenf23e8602015-02-14 17:22:49 -08003690 card->num_of_dapm_routes = num_routes;
3691 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003692
3693 return 0;
3694}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003695EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003696
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003697unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003698 const char *prefix,
3699 struct device_node **bitclkmaster,
3700 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003701{
3702 int ret, i;
3703 char prop[128];
3704 unsigned int format = 0;
3705 int bit, frame;
3706 const char *str;
3707 struct {
3708 char *name;
3709 unsigned int val;
3710 } of_fmt_table[] = {
3711 { "i2s", SND_SOC_DAIFMT_I2S },
3712 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3713 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3714 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3715 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3716 { "ac97", SND_SOC_DAIFMT_AC97 },
3717 { "pdm", SND_SOC_DAIFMT_PDM},
3718 { "msb", SND_SOC_DAIFMT_MSB },
3719 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003720 };
3721
3722 if (!prefix)
3723 prefix = "";
3724
3725 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003726 * check "dai-format = xxx"
3727 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003728 * SND_SOC_DAIFMT_FORMAT_MASK area
3729 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003730 ret = of_property_read_string(np, "dai-format", &str);
3731 if (ret < 0) {
3732 snprintf(prop, sizeof(prop), "%sformat", prefix);
3733 ret = of_property_read_string(np, prop, &str);
3734 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003735 if (ret == 0) {
3736 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3737 if (strcmp(str, of_fmt_table[i].name) == 0) {
3738 format |= of_fmt_table[i].val;
3739 break;
3740 }
3741 }
3742 }
3743
3744 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003745 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003746 * SND_SOC_DAIFMT_CLOCK_MASK area
3747 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003748 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003749 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003750 format |= SND_SOC_DAIFMT_CONT;
3751 else
3752 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003753
3754 /*
3755 * check "[prefix]bitclock-inversion"
3756 * check "[prefix]frame-inversion"
3757 * SND_SOC_DAIFMT_INV_MASK area
3758 */
3759 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3760 bit = !!of_get_property(np, prop, NULL);
3761
3762 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3763 frame = !!of_get_property(np, prop, NULL);
3764
3765 switch ((bit << 4) + frame) {
3766 case 0x11:
3767 format |= SND_SOC_DAIFMT_IB_IF;
3768 break;
3769 case 0x10:
3770 format |= SND_SOC_DAIFMT_IB_NF;
3771 break;
3772 case 0x01:
3773 format |= SND_SOC_DAIFMT_NB_IF;
3774 break;
3775 default:
3776 /* SND_SOC_DAIFMT_NB_NF is default */
3777 break;
3778 }
3779
3780 /*
3781 * check "[prefix]bitclock-master"
3782 * check "[prefix]frame-master"
3783 * SND_SOC_DAIFMT_MASTER_MASK area
3784 */
3785 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3786 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003787 if (bit && bitclkmaster)
3788 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003789
3790 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3791 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003792 if (frame && framemaster)
3793 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003794
3795 switch ((bit << 4) + frame) {
3796 case 0x11:
3797 format |= SND_SOC_DAIFMT_CBM_CFM;
3798 break;
3799 case 0x10:
3800 format |= SND_SOC_DAIFMT_CBM_CFS;
3801 break;
3802 case 0x01:
3803 format |= SND_SOC_DAIFMT_CBS_CFM;
3804 break;
3805 default:
3806 format |= SND_SOC_DAIFMT_CBS_CFS;
3807 break;
3808 }
3809
3810 return format;
3811}
3812EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3813
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003814int snd_soc_get_dai_id(struct device_node *ep)
3815{
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003816 struct snd_soc_component *component;
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003817 struct device_node *node;
3818 int ret;
3819
3820 node = of_graph_get_port_parent(ep);
3821
3822 /*
3823 * For example HDMI case, HDMI has video/sound port,
3824 * but ALSA SoC needs sound port number only.
3825 * Thus counting HDMI DT port/endpoint doesn't work.
3826 * Then, it should have .of_xlate_dai_id
3827 */
3828 ret = -ENOTSUPP;
3829 mutex_lock(&client_mutex);
Kuninori Morimoto09d4cc02019-05-13 16:07:20 +09003830 component = soc_find_component(node, NULL);
3831 if (component &&
3832 component->driver->of_xlate_dai_id)
3833 ret = component->driver->of_xlate_dai_id(component, ep);
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003834 mutex_unlock(&client_mutex);
3835
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07003836 of_node_put(node);
3837
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00003838 return ret;
3839}
3840EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3841
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003842int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003843 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003844{
3845 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003846 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003847 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003848
3849 mutex_lock(&client_mutex);
Kuninori Morimoto368dee92018-09-21 05:23:01 +00003850 for_each_component(pos) {
Kuninori Morimotoc0834442019-05-13 16:06:59 +09003851 component_of_node = soc_component_to_node(pos);
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03003852
3853 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003854 continue;
3855
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003856 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003857 ret = pos->driver->of_xlate_dai_name(pos,
3858 args,
3859 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003860 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003861 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003862 int id = -1;
3863
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003864 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003865 case 0:
3866 id = 0; /* same as dai_drv[0] */
3867 break;
3868 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003869 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003870 break;
3871 default:
3872 /* not supported */
3873 break;
3874 }
3875
3876 if (id < 0 || id >= pos->num_dai) {
3877 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003878 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003879 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003880
3881 ret = 0;
3882
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003883 /* find target DAI */
Kuninori Morimoto15a0c642018-09-21 05:23:17 +00003884 for_each_component_dais(pos, dai) {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003885 if (id == 0)
3886 break;
3887 id--;
3888 }
3889
3890 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08003891 if (!*dai_name)
3892 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003893 }
3894
Kuninori Morimotocb470082013-09-10 17:39:56 -07003895 break;
3896 }
3897 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003898 return ret;
3899}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00003900EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003901
3902int snd_soc_of_get_dai_name(struct device_node *of_node,
3903 const char **dai_name)
3904{
3905 struct of_phandle_args args;
3906 int ret;
3907
3908 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3909 "#sound-dai-cells", 0, &args);
3910 if (ret)
3911 return ret;
3912
3913 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003914
3915 of_node_put(args.np);
3916
3917 return ret;
3918}
3919EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3920
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003921/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003922 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3923 * @dai_link: DAI link
3924 *
3925 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3926 */
3927void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3928{
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003929 struct snd_soc_dai_link_component *component;
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003930 int index;
3931
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003932 for_each_link_codecs(dai_link, index, component) {
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003933 if (!component->of_node)
3934 break;
3935 of_node_put(component->of_node);
3936 component->of_node = NULL;
3937 }
3938}
3939EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3940
3941/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003942 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3943 * @dev: Card device
3944 * @of_node: Device node
3945 * @dai_link: DAI link
3946 *
3947 * Builds an array of CODEC DAI components from the DAI link property
3948 * 'sound-dai'.
3949 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003950 * The device nodes in the array (of_node) must be dereferenced by calling
3951 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003952 *
3953 * Returns 0 for success
3954 */
3955int snd_soc_of_get_dai_link_codecs(struct device *dev,
3956 struct device_node *of_node,
3957 struct snd_soc_dai_link *dai_link)
3958{
3959 struct of_phandle_args args;
3960 struct snd_soc_dai_link_component *component;
3961 char *name;
3962 int index, num_codecs, ret;
3963
3964 /* Count the number of CODECs */
3965 name = "sound-dai";
3966 num_codecs = of_count_phandle_with_args(of_node, name,
3967 "#sound-dai-cells");
3968 if (num_codecs <= 0) {
3969 if (num_codecs == -ENOENT)
3970 dev_err(dev, "No 'sound-dai' property\n");
3971 else
3972 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3973 return num_codecs;
3974 }
Kees Cooka86854d2018-06-12 14:07:58 -07003975 component = devm_kcalloc(dev,
3976 num_codecs, sizeof(*component),
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003977 GFP_KERNEL);
3978 if (!component)
3979 return -ENOMEM;
3980 dai_link->codecs = component;
3981 dai_link->num_codecs = num_codecs;
3982
3983 /* Parse the list */
Kuninori Morimoto3db769f2018-09-03 02:12:40 +00003984 for_each_link_codecs(dai_link, index, component) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003985 ret = of_parse_phandle_with_args(of_node, name,
3986 "#sound-dai-cells",
Marcel Ziswiler2c7b6962018-10-18 13:18:28 +02003987 index, &args);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003988 if (ret)
3989 goto err;
3990 component->of_node = args.np;
3991 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3992 if (ret < 0)
3993 goto err;
3994 }
3995 return 0;
3996err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01003997 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003998 dai_link->codecs = NULL;
3999 dai_link->num_codecs = 0;
4000 return ret;
4001}
4002EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4003
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004004static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004005{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004006 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01004007 snd_soc_util_init();
4008
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004009 return platform_driver_register(&soc_driver);
4010}
Mark Brown4abe8e12010-10-12 17:41:03 +01004011module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004012
Mark Brown7d8c16a2008-11-30 22:11:24 +00004013static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004014{
Mark Brownfb257892011-04-28 10:57:54 +01004015 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004016 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01004017
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004018 platform_driver_unregister(&soc_driver);
4019}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004020module_exit(snd_soc_exit);
4021
4022/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004023MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004024MODULE_DESCRIPTION("ALSA SoC Core");
4025MODULE_LICENSE("GPL");
4026MODULE_ALIAS("platform:soc-audio");