blob: 4b068ccf4e13a77b1089f1f7a01440a0dd37bfca [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +000037#include <linux/of_graph.h>
Liam Girdwood345233d2017-01-14 16:13:02 +080038#include <linux/dmi.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000040#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010044#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010045#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070060static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Mengdong Lin98faf432017-06-28 15:01:39 +080071/* If a DMI filed contain strings in this blacklist (e.g.
72 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
73 * as invalid and dropped when setting the card long name from DMI info.
74 */
75static const char * const dmi_blacklist[] = {
76 "To be filled by OEM",
77 "TBD by OEM",
78 "Default String",
79 "Board Manufacturer",
80 "Board Vendor Name",
81 "Board Product Name",
82 NULL, /* terminator */
83};
84
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000085/* returns the minimum number of bytes needed to represent
86 * a particular given value */
87static int min_bytes_needed(unsigned long val)
88{
89 int c = 0;
90 int i;
91
92 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
93 if (val & (1UL << i))
94 break;
95 c = (sizeof val * 8) - c;
96 if (!c || (c % 8))
97 c = (c + 8) / 8;
98 else
99 c /= 8;
100 return c;
101}
102
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000103/* fill buf which is 'len' bytes with a formatted
104 * string of the form 'reg: value\n' */
105static int format_register_str(struct snd_soc_codec *codec,
106 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000107{
Stephen Warren00b317a2011-04-01 14:50:44 -0600108 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
109 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000110 int ret;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000111
112 /* +2 for ': ' and + 1 for '\n' */
113 if (wordsize + regsize + 2 + 1 != len)
114 return -EINVAL;
115
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200116 sprintf(buf, "%.*x: ", wordsize, reg);
117 buf += wordsize + 2;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200120 if (ret < 0)
121 memset(buf, 'X', regsize);
122 else
123 sprintf(buf, "%.*x", regsize, ret);
124 buf[regsize] = '\n';
125 /* no NUL-termination needed */
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000126 return 0;
127}
128
129/* codec register dump */
130static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
131 size_t count, loff_t pos)
132{
133 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000134 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000135 int len;
136 size_t total = 0;
137 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000138
Stephen Warren00b317a2011-04-01 14:50:44 -0600139 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
140 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000141
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000142 len = wordsize + regsize + 2 + 1;
143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000145 return 0;
146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 if (codec->driver->reg_cache_step)
148 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100151 /* only support larger than PAGE_SIZE bytes debugfs
152 * entries for the default case */
153 if (p >= pos) {
154 if (total + len >= count - 1)
155 break;
156 format_register_str(codec, i, buf + total, len);
157 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100158 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100159 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000160 }
161
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000162 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000165}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166
Mark Brown2624d5f2009-11-03 21:56:13 +0000167static ssize_t codec_reg_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
Mark Brown36ae1a92012-01-06 17:12:45 -0800170 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000171
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000173}
174
Joe Perchesc828a892017-12-19 10:15:08 -0800175static DEVICE_ATTR_RO(codec_reg);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176
Mark Browndbe21402010-02-12 11:37:24 +0000177static ssize_t pmdown_time_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
Mark Brown36ae1a92012-01-06 17:12:45 -0800180 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000181
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000182 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000183}
184
185static ssize_t pmdown_time_set(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t count)
188{
Mark Brown36ae1a92012-01-06 17:12:45 -0800189 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700190 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000191
Jingoo Hanb785a492013-07-19 16:24:59 +0900192 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700193 if (ret)
194 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000195
196 return count;
197}
198
199static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
200
Takashi Iwaid29697d2015-01-30 20:16:37 +0100201static struct attribute *soc_dev_attrs[] = {
202 &dev_attr_codec_reg.attr,
203 &dev_attr_pmdown_time.attr,
204 NULL
205};
206
207static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
208 struct attribute *attr, int idx)
209{
210 struct device *dev = kobj_to_dev(kobj);
211 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
212
213 if (attr == &dev_attr_pmdown_time.attr)
214 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000215 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100216}
217
218static const struct attribute_group soc_dapm_dev_group = {
219 .attrs = soc_dapm_dev_attrs,
220 .is_visible = soc_dev_attr_is_visible,
221};
222
Mark Brownf7e73b262018-03-09 12:46:27 +0000223static const struct attribute_group soc_dev_group = {
Takashi Iwaid29697d2015-01-30 20:16:37 +0100224 .attrs = soc_dev_attrs,
225 .is_visible = soc_dev_attr_is_visible,
226};
227
228static const struct attribute_group *soc_dev_attr_groups[] = {
229 &soc_dapm_dev_group,
Mark Brownf7e73b262018-03-09 12:46:27 +0000230 &soc_dev_group,
Takashi Iwaid29697d2015-01-30 20:16:37 +0100231 NULL
232};
233
Mark Brown2624d5f2009-11-03 21:56:13 +0000234#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000235static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000236 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000237{
238 ssize_t ret;
239 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000240 char *buf;
241
242 if (*ppos < 0 || !count)
243 return -EINVAL;
244
245 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000246 if (!buf)
247 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000248
249 ret = soc_codec_reg_show(codec, buf, count, *ppos);
250 if (ret >= 0) {
251 if (copy_to_user(user_buf, buf, ret)) {
252 kfree(buf);
253 return -EFAULT;
254 }
255 *ppos += ret;
256 }
257
Mark Brown2624d5f2009-11-03 21:56:13 +0000258 kfree(buf);
259 return ret;
260}
261
262static ssize_t codec_reg_write_file(struct file *file,
263 const char __user *user_buf, size_t count, loff_t *ppos)
264{
265 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700266 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000267 char *start = buf;
268 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000269 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900270 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000271
272 buf_size = min(count, (sizeof(buf)-1));
273 if (copy_from_user(buf, user_buf, buf_size))
274 return -EFAULT;
275 buf[buf_size] = 0;
276
Mark Brown2624d5f2009-11-03 21:56:13 +0000277 while (*start == ' ')
278 start++;
279 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000280 while (*start == ' ')
281 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900282 ret = kstrtoul(start, 16, &value);
283 if (ret)
284 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000285
286 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030287 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000288
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000289 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290 return buf_size;
291}
292
293static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700294 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000295 .read = codec_reg_read_file,
296 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200297 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000298};
299
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200300static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100301{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200302 if (!component->card->debugfs_card_root)
303 return;
304
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200305 if (component->debugfs_prefix) {
306 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100307
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200308 name = kasprintf(GFP_KERNEL, "%s:%s",
309 component->debugfs_prefix, component->name);
310 if (name) {
311 component->debugfs_root = debugfs_create_dir(name,
312 component->card->debugfs_card_root);
313 kfree(name);
314 }
315 } else {
316 component->debugfs_root = debugfs_create_dir(component->name,
317 component->card->debugfs_card_root);
318 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100319
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200320 if (!component->debugfs_root) {
321 dev_warn(component->dev,
322 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000323 return;
324 }
325
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200326 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
327 component->debugfs_root);
328
329 if (component->init_debugfs)
330 component->init_debugfs(component);
331}
332
333static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
334{
335 debugfs_remove_recursive(component->debugfs_root);
336}
337
338static void soc_init_codec_debugfs(struct snd_soc_component *component)
339{
340 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
Fabio Estevam353c64d2017-08-07 09:08:52 -0300341 struct dentry *debugfs_reg;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200342
Fabio Estevam353c64d2017-08-07 09:08:52 -0300343 debugfs_reg = debugfs_create_file("codec_reg", 0644,
344 codec->component.debugfs_root,
345 codec, &codec_reg_fops);
346 if (!debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200347 dev_warn(codec->dev,
348 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000349}
350
Peng Donglinc15b2a12018-02-14 22:48:07 +0800351static int codec_list_show(struct seq_file *m, void *v)
Mark Brownc3c5a192010-09-15 18:15:14 +0100352{
Mark Brownc3c5a192010-09-15 18:15:14 +0100353 struct snd_soc_codec *codec;
354
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100355 mutex_lock(&client_mutex);
356
Donglin Peng700c17c2018-01-18 13:31:26 +0800357 list_for_each_entry(codec, &codec_list, list)
358 seq_printf(m, "%s\n", codec->component.name);
Mark Brownc3c5a192010-09-15 18:15:14 +0100359
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100360 mutex_unlock(&client_mutex);
361
Donglin Peng700c17c2018-01-18 13:31:26 +0800362 return 0;
363}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800364DEFINE_SHOW_ATTRIBUTE(codec_list);
Mark Brownc3c5a192010-09-15 18:15:14 +0100365
Peng Donglinc15b2a12018-02-14 22:48:07 +0800366static int dai_list_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100367{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100368 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100369 struct snd_soc_dai *dai;
370
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100371 mutex_lock(&client_mutex);
372
Donglin Peng700c17c2018-01-18 13:31:26 +0800373 list_for_each_entry(component, &component_list, list)
374 list_for_each_entry(dai, &component->dai_list, list)
375 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100376
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100377 mutex_unlock(&client_mutex);
378
Donglin Peng700c17c2018-01-18 13:31:26 +0800379 return 0;
380}
Peng Donglinc15b2a12018-02-14 22:48:07 +0800381DEFINE_SHOW_ATTRIBUTE(dai_list);
Mark Brownf3208782010-09-15 18:19:07 +0100382
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200383static void soc_init_card_debugfs(struct snd_soc_card *card)
384{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200385 if (!snd_soc_debugfs_root)
386 return;
387
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200388 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000389 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200390 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200391 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100392 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200393 return;
394 }
395
396 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
397 card->debugfs_card_root,
398 &card->pop_time);
399 if (!card->debugfs_pop_time)
400 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000401 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200402}
403
404static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
405{
406 debugfs_remove_recursive(card->debugfs_card_root);
407}
408
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200409static void snd_soc_debugfs_init(void)
410{
411 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300412 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200413 pr_warn("ASoC: Failed to create debugfs directory\n");
414 snd_soc_debugfs_root = NULL;
415 return;
416 }
417
418 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
419 &codec_list_fops))
420 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
421
422 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
423 &dai_list_fops))
424 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200425}
426
427static void snd_soc_debugfs_exit(void)
428{
429 debugfs_remove_recursive(snd_soc_debugfs_root);
430}
431
Mark Brown2624d5f2009-11-03 21:56:13 +0000432#else
433
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200434#define soc_init_codec_debugfs NULL
435
436static inline void soc_init_component_debugfs(
437 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000438{
439}
440
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200441static inline void soc_cleanup_component_debugfs(
442 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000443{
444}
445
Axel Linb95fccb2010-11-09 17:06:44 +0800446static inline void soc_init_card_debugfs(struct snd_soc_card *card)
447{
448}
449
450static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
451{
452}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200453
454static inline void snd_soc_debugfs_init(void)
455{
456}
457
458static inline void snd_soc_debugfs_exit(void)
459{
460}
461
Mark Brown2624d5f2009-11-03 21:56:13 +0000462#endif
463
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000464static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
465 struct snd_soc_component *component)
466{
467 struct snd_soc_rtdcom_list *rtdcom;
468 struct snd_soc_rtdcom_list *new_rtdcom;
469
470 for_each_rtdcom(rtd, rtdcom) {
471 /* already connected */
472 if (rtdcom->component == component)
473 return 0;
474 }
475
476 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
477 if (!new_rtdcom)
478 return -ENOMEM;
479
480 new_rtdcom->component = component;
481 INIT_LIST_HEAD(&new_rtdcom->list);
482
483 list_add_tail(&new_rtdcom->list, &rtd->component_list);
484
485 return 0;
486}
487
488static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
489{
490 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
491
492 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
493 kfree(rtdcom1);
494
495 INIT_LIST_HEAD(&rtd->component_list);
496}
497
498struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
499 const char *driver_name)
500{
501 struct snd_soc_rtdcom_list *rtdcom;
502
Kuninori Morimoto971da242018-01-23 00:41:24 +0000503 if (!driver_name)
504 return NULL;
505
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000506 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000507 const char *component_name = rtdcom->component->driver->name;
508
509 if (!component_name)
510 continue;
511
512 if ((component_name == driver_name) ||
513 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000514 return rtdcom->component;
515 }
516
517 return NULL;
518}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000519EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000520
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100521struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
522 const char *dai_link, int stream)
523{
Mengdong Lin1a497982015-11-18 02:34:11 -0500524 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100525
Mengdong Lin1a497982015-11-18 02:34:11 -0500526 list_for_each_entry(rtd, &card->rtd_list, list) {
527 if (rtd->dai_link->no_pcm &&
528 !strcmp(rtd->dai_link->name, dai_link))
529 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100530 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000531 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100532 return NULL;
533}
534EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
535
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000536static const struct snd_soc_ops null_snd_soc_ops;
537
Mengdong Lin1a497982015-11-18 02:34:11 -0500538static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
539 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
540{
541 struct snd_soc_pcm_runtime *rtd;
542
543 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
544 if (!rtd)
545 return NULL;
546
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000547 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500548 rtd->card = card;
549 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000550 if (!rtd->dai_link->ops)
551 rtd->dai_link->ops = &null_snd_soc_ops;
552
Mengdong Lin1a497982015-11-18 02:34:11 -0500553 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
554 dai_link->num_codecs,
555 GFP_KERNEL);
556 if (!rtd->codec_dais) {
557 kfree(rtd);
558 return NULL;
559 }
560
561 return rtd;
562}
563
564static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
565{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000566 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000567 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500568 kfree(rtd);
569}
570
571static void soc_add_pcm_runtime(struct snd_soc_card *card,
572 struct snd_soc_pcm_runtime *rtd)
573{
574 list_add_tail(&rtd->list, &card->rtd_list);
575 rtd->num = card->num_rtd;
576 card->num_rtd++;
577}
578
579static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
580{
581 struct snd_soc_pcm_runtime *rtd, *_rtd;
582
583 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
584 list_del(&rtd->list);
585 soc_free_pcm_runtime(rtd);
586 }
587
588 card->num_rtd = 0;
589}
590
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100591struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
592 const char *dai_link)
593{
Mengdong Lin1a497982015-11-18 02:34:11 -0500594 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100595
Mengdong Lin1a497982015-11-18 02:34:11 -0500596 list_for_each_entry(rtd, &card->rtd_list, list) {
597 if (!strcmp(rtd->dai_link->name, dai_link))
598 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100599 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000600 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100601 return NULL;
602}
603EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
604
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100605static void codec2codec_close_delayed_work(struct work_struct *work)
606{
607 /* Currently nothing to do for c2c links
608 * Since c2c links are internal nodes in the DAPM graph and
609 * don't interface with the outside world or application layer
610 * we don't have to do any special handling on close.
611 */
612}
613
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000614#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000616int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000618 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000619 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500620 struct snd_soc_pcm_runtime *rtd;
621 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200623 /* If the card is not initialized yet there is nothing to do */
624 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200625 return 0;
626
Andy Green6ed25972008-06-13 16:24:05 +0100627 /* Due to the resume being scheduled into a workqueue we could
628 * suspend before that's finished - wait for it to complete.
629 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100631
632 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100634
Mark Browna00f90f2010-12-02 16:24:24 +0000635 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500636 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100637
Mengdong Lin1a497982015-11-18 02:34:11 -0500638 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100639 continue;
640
Mengdong Lin1a497982015-11-18 02:34:11 -0500641 for (i = 0; i < rtd->num_codecs; i++) {
642 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200643 struct snd_soc_dai_driver *drv = dai->driver;
644
645 if (drv->ops->digital_mute && dai->playback_active)
646 drv->ops->digital_mute(dai, 1);
647 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200648 }
649
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100650 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500651 list_for_each_entry(rtd, &card->rtd_list, list) {
652 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100653 continue;
654
Mengdong Lin1a497982015-11-18 02:34:11 -0500655 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100656 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100657
Mark Brown87506542008-11-18 20:50:34 +0000658 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000659 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660
Mengdong Lin1a497982015-11-18 02:34:11 -0500661 list_for_each_entry(rtd, &card->rtd_list, list) {
662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100663
Mengdong Lin1a497982015-11-18 02:34:11 -0500664 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100665 continue;
666
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100667 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100669 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200671 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500672 list_for_each_entry(rtd, &card->rtd_list, list)
673 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100674
Mengdong Lin1a497982015-11-18 02:34:11 -0500675 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676
Mengdong Lin1a497982015-11-18 02:34:11 -0500677 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100678 continue;
679
Mengdong Lin1a497982015-11-18 02:34:11 -0500680 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800681 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800682 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683
Mengdong Lin1a497982015-11-18 02:34:11 -0500684 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800685 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800686 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 }
688
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200689 /* Recheck all endpoints too, their state is affected by suspend */
690 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700691 snd_soc_dapm_sync(&card->dapm);
692
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000693 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000694 list_for_each_entry(component, &card->component_dev_list, card_list) {
695 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000696
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000697 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000699 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200700 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000702 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000703 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000704 * bias off then being in STANDBY
705 * means it's doing something,
706 * otherwise fall through.
707 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200708 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000709 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200710 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000711 break;
712 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 case SND_SOC_BIAS_OFF:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000715 if (component->suspend)
716 component->suspend(component);
717 component->suspended = 1;
718 if (component->regmap)
719 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800720 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000721 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 break;
723 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000724 dev_dbg(component->dev,
725 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 break;
727 }
728 }
729 }
730
Mengdong Lin1a497982015-11-18 02:34:11 -0500731 list_for_each_entry(rtd, &card->rtd_list, list) {
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733
Mengdong Lin1a497982015-11-18 02:34:11 -0500734 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 continue;
736
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100737 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800739
740 /* deactivate pins to sleep state */
741 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 }
743
Mark Brown87506542008-11-18 20:50:34 +0000744 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000745 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746
747 return 0;
748}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000749EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750
Andy Green6ed25972008-06-13 16:24:05 +0100751/* deferred resume work, so resume can complete before we finished
752 * setting our codec back up, which can be very slow on I2C
753 */
754static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 struct snd_soc_card *card =
757 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500758 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000759 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500760 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761
Andy Green6ed25972008-06-13 16:24:05 +0100762 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
763 * so userspace apps are blocked from touching us
764 */
765
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000766 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100767
Mark Brown9949788b2010-05-07 20:24:05 +0100768 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100770
Mark Brown87506542008-11-18 20:50:34 +0000771 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000772 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100774 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500775 list_for_each_entry(rtd, &card->rtd_list, list) {
776 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100777
Mengdong Lin1a497982015-11-18 02:34:11 -0500778 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100779 continue;
780
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100781 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783 }
784
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000785 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000786 if (component->suspended) {
787 if (component->resume)
788 component->resume(component);
789 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100790 }
791 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792
Mengdong Lin1a497982015-11-18 02:34:11 -0500793 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100794
Mengdong Lin1a497982015-11-18 02:34:11 -0500795 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100796 continue;
797
Mengdong Lin1a497982015-11-18 02:34:11 -0500798 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000799 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800800 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000801
Mengdong Lin1a497982015-11-18 02:34:11 -0500802 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000803 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800804 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 }
806
Mark Brown3ff3f642008-05-19 12:32:25 +0200807 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500808 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100809
Mengdong Lin1a497982015-11-18 02:34:11 -0500810 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100811 continue;
812
Mengdong Lin1a497982015-11-18 02:34:11 -0500813 for (i = 0; i < rtd->num_codecs; i++) {
814 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200815 struct snd_soc_dai_driver *drv = dai->driver;
816
817 if (drv->ops->digital_mute && dai->playback_active)
818 drv->ops->digital_mute(dai, 0);
819 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820 }
821
Mengdong Lin1a497982015-11-18 02:34:11 -0500822 list_for_each_entry(rtd, &card->rtd_list, list) {
823 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100824
Mengdong Lin1a497982015-11-18 02:34:11 -0500825 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100826 continue;
827
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100828 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200830 }
831
Mark Brown87506542008-11-18 20:50:34 +0000832 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000833 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200834
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000835 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100836
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200837 /* Recheck all endpoints too, their state is affected by suspend */
838 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700839 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530840
841 /* userspace can access us now we are back as we were before */
842 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100843}
844
845/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000846int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100847{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000848 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100849 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500850 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200851
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200852 /* If the card is not initialized yet there is nothing to do */
853 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800854 return 0;
855
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800856 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500857 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200858 struct snd_soc_dai **codec_dais = rtd->codec_dais;
859 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
860 int j;
861
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800862 if (cpu_dai->active)
863 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200864
865 for (j = 0; j < rtd->num_codecs; j++) {
866 struct snd_soc_dai *codec_dai = codec_dais[j];
867 if (codec_dai->active)
868 pinctrl_pm_select_default_state(codec_dai->dev);
869 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800870 }
871
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100872 /*
873 * DAIs that also act as the control bus master might have other drivers
874 * hanging off them so need to resume immediately. Other drivers don't
875 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100876 * due to I/O costs and anti-pop so handle them out of line.
877 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500878 list_for_each_entry(rtd, &card->rtd_list, list) {
879 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100880 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600881 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100882 if (bus_control) {
883 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600884 soc_resume_deferred(&card->deferred_resume_work);
885 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000886 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600887 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000888 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100889 }
Andy Green6ed25972008-06-13 16:24:05 +0100890
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200891 return 0;
892}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000893EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000895#define snd_soc_suspend NULL
896#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897#endif
898
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100899static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800900};
901
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200902static struct snd_soc_component *soc_find_component(
903 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100904{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200905 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100907 lockdep_assert_held(&client_mutex);
908
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200909 list_for_each_entry(component, &component_list, list) {
910 if (of_node) {
911 if (component->dev->of_node == of_node)
912 return component;
913 } else if (strcmp(component->name, name) == 0) {
914 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100915 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100916 }
917
918 return NULL;
919}
920
Mengdong Linfbb88b52016-04-22 12:25:33 +0800921/**
922 * snd_soc_find_dai - Find a registered DAI
923 *
Jeffy Chen49584712017-08-22 15:57:21 +0800924 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800925 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700926 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800927 * find the DAI of the same name. The component's of_node and name
928 * should also match if being specified.
929 *
930 * Return: pointer of DAI, or NULL if not found.
931 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800932struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200933 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100934{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200935 struct snd_soc_component *component;
936 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300937 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100938
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100939 lockdep_assert_held(&client_mutex);
940
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200941 /* Find CPU DAI from registered DAIs*/
942 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300943 component_of_node = component->dev->of_node;
944 if (!component_of_node && component->dev->parent)
945 component_of_node = component->dev->parent->of_node;
946
947 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200948 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100949 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200950 continue;
951 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +0800952 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +0800953 && (!dai->driver->name
954 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100955 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100956
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200957 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100958 }
959 }
960
961 return NULL;
962}
Mengdong Lin305e9022016-04-19 13:12:25 +0800963EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100964
Mengdong Lin17fb17552016-11-03 01:04:12 +0800965
966/**
967 * snd_soc_find_dai_link - Find a DAI link
968 *
969 * @card: soc card
970 * @id: DAI link ID to match
971 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +0000972 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +0800973 *
974 * This function will search all existing DAI links of the soc card to
975 * find the link of the same ID. Since DAI links may not have their
976 * unique ID, so name and stream name should also match if being
977 * specified.
978 *
979 * Return: pointer of DAI link, or NULL if not found.
980 */
981struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
982 int id, const char *name,
983 const char *stream_name)
984{
985 struct snd_soc_dai_link *link, *_link;
986
987 lockdep_assert_held(&client_mutex);
988
989 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
990 if (link->id != id)
991 continue;
992
993 if (name && (!link->name || strcmp(name, link->name)))
994 continue;
995
996 if (stream_name && (!link->stream_name
997 || strcmp(stream_name, link->stream_name)))
998 continue;
999
1000 return link;
1001 }
1002
1003 return NULL;
1004}
1005EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1006
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001007static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1008 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001009{
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001010 struct snd_soc_pcm_runtime *rtd;
1011
1012 list_for_each_entry(rtd, &card->rtd_list, list) {
1013 if (rtd->dai_link == dai_link)
1014 return true;
1015 }
1016
1017 return false;
1018}
1019
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001020static int soc_bind_dai_link(struct snd_soc_card *card,
1021 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001022{
Mengdong Lin1a497982015-11-18 02:34:11 -05001023 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001024 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001025 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001026 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -05001027 struct snd_soc_dai **codec_dais;
Charles Keepax06859fc2016-11-07 13:03:17 +00001028 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +01001029 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001030 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001031
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001032 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001033
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001034 if (soc_is_dai_link_bound(card, dai_link)) {
1035 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1036 dai_link->name);
1037 return 0;
1038 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001039
Sudip Mukherjee513cb3112016-02-22 14:14:31 +05301040 rtd = soc_new_pcm_runtime(card, dai_link);
1041 if (!rtd)
1042 return -ENOMEM;
1043
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001044 cpu_dai_component.name = dai_link->cpu_name;
1045 cpu_dai_component.of_node = dai_link->cpu_of_node;
1046 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1047 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001048 if (!rtd->cpu_dai) {
Martin Hundebøll6b490872018-02-01 11:09:41 +01001049 dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1050 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001051 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001052 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001053 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001054
Benoit Cousson88bd8702014-07-08 23:19:34 +02001055 rtd->num_codecs = dai_link->num_codecs;
1056
1057 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -05001058 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001059 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001060 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001061 if (!codec_dais[i]) {
1062 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1063 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001064 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001065 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001066 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001067 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001068
Benoit Cousson88bd8702014-07-08 23:19:34 +02001069 /* Single codec links expect codec and codec_dai in runtime data */
1070 rtd->codec_dai = codec_dais[0];
1071 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001072
Mark Brown848dd8b2011-04-27 18:16:32 +01001073 /* if there's no platform we match on the empty platform */
1074 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -07001075 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +01001076 platform_name = "snd-soc-dummy";
1077
Mark Brownb19e6e72012-03-14 21:18:39 +00001078 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001079 list_for_each_entry(component, &component_list, list) {
1080 platform_of_node = component->dev->of_node;
1081 if (!platform_of_node && component->dev->parent->of_node)
1082 platform_of_node = component->dev->parent->of_node;
1083
1084 if (dai_link->platform_of_node) {
1085 if (platform_of_node != dai_link->platform_of_node)
1086 continue;
1087 } else {
1088 if (strcmp(component->name, platform_name))
1089 continue;
1090 }
1091
1092 snd_soc_rtdcom_add(rtd, component);
1093 }
1094
Mengdong Lin1a497982015-11-18 02:34:11 -05001095 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +00001096 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001097
1098_err_defer:
1099 soc_free_pcm_runtime(rtd);
1100 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001101}
1102
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001103static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001104{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001105 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001106 return;
Stephen Warrend12cd192012-06-08 12:34:22 -06001107
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001108 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001109
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001110 if (component->remove)
1111 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001112
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001113 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001114
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001115 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001116 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001117 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -06001118}
1119
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001120static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001121{
1122 int err;
1123
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001124 if (dai && dai->probed &&
1125 dai->driver->remove_order == order) {
1126 if (dai->driver->remove) {
1127 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001128 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001129 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001130 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001131 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001132 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001133 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001134 }
1135}
1136
Mengdong Lin1a497982015-11-18 02:34:11 -05001137static void soc_remove_link_dais(struct snd_soc_card *card,
1138 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001139{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001140 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001141
1142 /* unregister the rtd device */
1143 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001144 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001145 rtd->dev_registered = 0;
1146 }
1147
1148 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001149 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001150 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001152 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153}
1154
Mengdong Lin1a497982015-11-18 02:34:11 -05001155static void soc_remove_link_components(struct snd_soc_card *card,
1156 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001157{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001158 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001159 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001160
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001161 for_each_rtdcom(rtd, rtdcom) {
1162 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001163
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001164 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001165 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001166 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001167}
1168
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001169static void soc_remove_dai_links(struct snd_soc_card *card)
1170{
Mengdong Lin1a497982015-11-18 02:34:11 -05001171 int order;
1172 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001173 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001174
Liam Girdwood0168bf02011-06-07 16:08:05 +01001175 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1176 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001177 list_for_each_entry(rtd, &card->rtd_list, list)
1178 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001179 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001180
1181 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1182 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001183 list_for_each_entry(rtd, &card->rtd_list, list)
1184 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001185 }
1186
Mengdong Linf8f80362015-12-02 14:11:22 +08001187 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1188 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1189 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1190 link->name);
1191
1192 list_del(&link->list);
1193 card->num_dai_links--;
1194 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001195}
1196
Mengdong Lin923c5e612015-11-23 11:01:49 -05001197static int snd_soc_init_multicodec(struct snd_soc_card *card,
1198 struct snd_soc_dai_link *dai_link)
1199{
1200 /* Legacy codec/codec_dai link is a single entry in multicodec */
1201 if (dai_link->codec_name || dai_link->codec_of_node ||
1202 dai_link->codec_dai_name) {
1203 dai_link->num_codecs = 1;
1204
1205 dai_link->codecs = devm_kzalloc(card->dev,
1206 sizeof(struct snd_soc_dai_link_component),
1207 GFP_KERNEL);
1208 if (!dai_link->codecs)
1209 return -ENOMEM;
1210
1211 dai_link->codecs[0].name = dai_link->codec_name;
1212 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1213 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1214 }
1215
1216 if (!dai_link->codecs) {
1217 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1218 return -EINVAL;
1219 }
1220
1221 return 0;
1222}
1223
1224static int soc_init_dai_link(struct snd_soc_card *card,
1225 struct snd_soc_dai_link *link)
1226{
1227 int i, ret;
1228
1229 ret = snd_soc_init_multicodec(card, link);
1230 if (ret) {
1231 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1232 return ret;
1233 }
1234
1235 for (i = 0; i < link->num_codecs; i++) {
1236 /*
1237 * Codec must be specified by 1 of name or OF node,
1238 * not both or neither.
1239 */
1240 if (!!link->codecs[i].name ==
1241 !!link->codecs[i].of_node) {
1242 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1243 link->name);
1244 return -EINVAL;
1245 }
1246 /* Codec DAI name must be specified */
1247 if (!link->codecs[i].dai_name) {
1248 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1249 link->name);
1250 return -EINVAL;
1251 }
1252 }
1253
1254 /*
1255 * Platform may be specified by either name or OF node, but
1256 * can be left unspecified, and a dummy platform will be used.
1257 */
1258 if (link->platform_name && link->platform_of_node) {
1259 dev_err(card->dev,
1260 "ASoC: Both platform name/of_node are set for %s\n",
1261 link->name);
1262 return -EINVAL;
1263 }
1264
1265 /*
1266 * CPU device may be specified by either name or OF node, but
1267 * can be left unspecified, and will be matched based on DAI
1268 * name alone..
1269 */
1270 if (link->cpu_name && link->cpu_of_node) {
1271 dev_err(card->dev,
1272 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1273 link->name);
1274 return -EINVAL;
1275 }
1276 /*
1277 * At least one of CPU DAI name or CPU device name/node must be
1278 * specified
1279 */
1280 if (!link->cpu_dai_name &&
1281 !(link->cpu_name || link->cpu_of_node)) {
1282 dev_err(card->dev,
1283 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1284 link->name);
1285 return -EINVAL;
1286 }
1287
1288 return 0;
1289}
1290
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001291void snd_soc_disconnect_sync(struct device *dev)
1292{
1293 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1294
1295 if (!component || !component->card)
1296 return;
1297
1298 snd_card_disconnect_sync(component->card->snd_card);
1299}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001300EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001301
Mengdong Linf8f80362015-12-02 14:11:22 +08001302/**
1303 * snd_soc_add_dai_link - Add a DAI link dynamically
1304 * @card: The ASoC card to which the DAI link is added
1305 * @dai_link: The new DAI link to add
1306 *
1307 * This function adds a DAI link to the ASoC card's link list.
1308 *
1309 * Note: Topology can use this API to add DAI links when probing the
1310 * topology component. And machine drivers can still define static
1311 * DAI links in dai_link array.
1312 */
1313int snd_soc_add_dai_link(struct snd_soc_card *card,
1314 struct snd_soc_dai_link *dai_link)
1315{
1316 if (dai_link->dobj.type
1317 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1318 dev_err(card->dev, "Invalid dai link type %d\n",
1319 dai_link->dobj.type);
1320 return -EINVAL;
1321 }
1322
1323 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001324 /* Notify the machine driver for extra initialization
1325 * on the link created by topology.
1326 */
1327 if (dai_link->dobj.type && card->add_dai_link)
1328 card->add_dai_link(card, dai_link);
1329
Mengdong Linf8f80362015-12-02 14:11:22 +08001330 list_add_tail(&dai_link->list, &card->dai_link_list);
1331 card->num_dai_links++;
1332
1333 return 0;
1334}
1335EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1336
1337/**
1338 * snd_soc_remove_dai_link - Remove a DAI link from the list
1339 * @card: The ASoC card that owns the link
1340 * @dai_link: The DAI link to remove
1341 *
1342 * This function removes a DAI link from the ASoC card's link list.
1343 *
1344 * For DAI links previously added by topology, topology should
1345 * remove them by using the dobj embedded in the link.
1346 */
1347void snd_soc_remove_dai_link(struct snd_soc_card *card,
1348 struct snd_soc_dai_link *dai_link)
1349{
1350 struct snd_soc_dai_link *link, *_link;
1351
1352 if (dai_link->dobj.type
1353 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1354 dev_err(card->dev, "Invalid dai link type %d\n",
1355 dai_link->dobj.type);
1356 return;
1357 }
1358
1359 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001360 /* Notify the machine driver for extra destruction
1361 * on the link created by topology.
1362 */
1363 if (dai_link->dobj.type && card->remove_dai_link)
1364 card->remove_dai_link(card, dai_link);
1365
Mengdong Linf8f80362015-12-02 14:11:22 +08001366 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1367 if (link == dai_link) {
1368 list_del(&link->list);
1369 card->num_dai_links--;
1370 return;
1371 }
1372 }
1373}
1374EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1375
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001376static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001377 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001378{
1379 int i;
1380
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001381 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001382 return;
1383
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001384 for (i = 0; i < card->num_configs; i++) {
1385 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Charles Keepaxb24c5392018-04-27 13:54:36 +01001386 struct device_node *component_of_node = component->dev->of_node;
1387
1388 if (!component_of_node && component->dev->parent)
1389 component_of_node = component->dev->parent->of_node;
1390
1391 if (map->of_node && component_of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001392 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001393 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001394 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001395 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001396 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001397 }
1398}
1399
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001400static int soc_probe_component(struct snd_soc_card *card,
1401 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001402{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001403 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001404 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001405 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001406
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001407 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001408 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001409
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001410 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001411 if (component->card != card) {
1412 dev_err(component->dev,
1413 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1414 card->name, component->card->name);
1415 return -ENODEV;
1416 }
1417 return 0;
1418 }
1419
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001420 if (!try_module_get(component->dev->driver->owner))
1421 return -ENODEV;
1422
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001423 component->card = card;
1424 dapm->card = card;
1425 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001426
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001427 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001428
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001429 if (component->driver->dapm_widgets) {
1430 ret = snd_soc_dapm_new_controls(dapm,
1431 component->driver->dapm_widgets,
1432 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001433
1434 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001435 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001436 "Failed to create new controls %d\n", ret);
1437 goto err_probe;
1438 }
1439 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001440
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001441 list_for_each_entry(dai, &component->dai_list, list) {
1442 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001443 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001444 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001445 "Failed to create DAI widgets %d\n", ret);
1446 goto err_probe;
1447 }
1448 }
Mark Brown888df392012-02-16 19:37:51 -08001449
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001450 if (component->probe) {
1451 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001452 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001453 dev_err(component->dev,
1454 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001455 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001456 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001457
1458 WARN(dapm->idle_bias_off &&
1459 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001460 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001461 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001462 }
1463
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001464 /* machine specific init */
1465 if (component->init) {
1466 ret = component->init(component);
1467 if (ret < 0) {
1468 dev_err(component->dev,
1469 "Failed to do machine specific init %d\n", ret);
1470 goto err_probe;
1471 }
1472 }
1473
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001474 if (component->driver->controls)
1475 snd_soc_add_component_controls(component,
1476 component->driver->controls,
1477 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001478 if (component->driver->dapm_routes)
1479 snd_soc_dapm_add_routes(dapm,
1480 component->driver->dapm_routes,
1481 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001482
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001483 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001484 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001485
Jarkko Nikula70d293312011-01-27 16:24:22 +02001486 return 0;
1487
1488err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001489 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001490 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001491 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001492
1493 return ret;
1494}
1495
Mark Brown36ae1a92012-01-06 17:12:45 -08001496static void rtd_release(struct device *dev)
1497{
1498 kfree(dev);
1499}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001501static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1502 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001503{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001504 int ret = 0;
1505
Jarkko Nikula589c3562010-12-06 16:27:07 +02001506 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001507 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1508 if (!rtd->dev)
1509 return -ENOMEM;
1510 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001511 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001512 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001513 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001514 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001515 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001516 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001517 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1518 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1519 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1520 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001521 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001522 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001523 /* calling put_device() here to free the rtd->dev */
1524 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001525 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001526 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001527 return ret;
1528 }
1529 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001530 return 0;
1531}
1532
Mengdong Lin1a497982015-11-18 02:34:11 -05001533static int soc_probe_link_components(struct snd_soc_card *card,
1534 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001535 int order)
1536{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001537 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001538 struct snd_soc_rtdcom_list *rtdcom;
1539 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001540
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001541 for_each_rtdcom(rtd, rtdcom) {
1542 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001543
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001544 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001545 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001546 if (ret < 0)
1547 return ret;
1548 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001549 }
1550
Stephen Warren62ae68f2012-06-08 12:34:23 -06001551 return 0;
1552}
1553
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001554static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001555{
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001556 if (dai->probed ||
1557 dai->driver->probe_order != order)
1558 return 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001559
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001560 if (dai->driver->probe) {
1561 int ret = dai->driver->probe(dai);
1562 if (ret < 0) {
1563 dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n",
1564 dai->name, ret);
1565 return ret;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001566 }
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001567 }
1568
Kuninori Morimoto7a2ccad2018-02-14 02:58:03 +00001569 dai->probed = 1;
1570
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001571 return 0;
1572}
1573
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001574static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1575 struct snd_soc_pcm_runtime *rtd)
1576{
1577 int i, ret = 0;
1578
1579 for (i = 0; i < num_dais; ++i) {
1580 struct snd_soc_dai_driver *drv = dais[i]->driver;
1581
1582 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1583 ret = drv->pcm_new(rtd, dais[i]);
1584 if (ret < 0) {
1585 dev_err(dais[i]->dev,
1586 "ASoC: Failed to bind %s with pcm device\n",
1587 dais[i]->name);
1588 return ret;
1589 }
1590 }
1591
1592 return 0;
1593}
1594
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001595static int soc_link_dai_widgets(struct snd_soc_card *card,
1596 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001597 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001598{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001599 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1600 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301601 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001602 int ret;
1603
Benoit Cousson88bd8702014-07-08 23:19:34 +02001604 if (rtd->num_codecs > 1)
1605 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1606
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001607 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301608 sink = codec_dai->playback_widget;
1609 source = cpu_dai->capture_widget;
1610 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001611 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301612 dai_link->num_params,
1613 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001614 if (ret != 0) {
1615 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301616 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001617 return ret;
1618 }
1619 }
1620
Vinod Koul3de7c422015-11-09 23:19:58 +05301621 sink = cpu_dai->playback_widget;
1622 source = codec_dai->capture_widget;
1623 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001624 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301625 dai_link->num_params,
1626 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001627 if (ret != 0) {
1628 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301629 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001630 return ret;
1631 }
1632 }
1633
1634 return 0;
1635}
1636
Mengdong Lin1a497982015-11-18 02:34:11 -05001637static int soc_probe_link_dais(struct snd_soc_card *card,
1638 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639{
Mengdong Lin1a497982015-11-18 02:34:11 -05001640 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001641 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown291bfb92018-04-19 12:14:10 +01001642 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001644 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001645 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001646
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647 /* set default power off timeout */
1648 rtd->pmdown_time = pmdown_time;
1649
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001650 ret = soc_probe_dai(cpu_dai, order);
1651 if (ret)
1652 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001655 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001656 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001657 if (ret)
1658 return ret;
1659 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660
Liam Girdwood0168bf02011-06-07 16:08:05 +01001661 /* complete DAI probe during last probe */
1662 if (order != SND_SOC_COMP_ORDER_LAST)
1663 return 0;
1664
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001665 /* do machine specific initialization */
1666 if (dai_link->init) {
1667 ret = dai_link->init(rtd);
1668 if (ret < 0) {
1669 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1670 dai_link->name, ret);
1671 return ret;
1672 }
1673 }
1674
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001675 if (dai_link->dai_fmt)
1676 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1677
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001678 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001679 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001680 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001681
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001682#ifdef CONFIG_DEBUG_FS
1683 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001684 if (dai_link->dynamic)
1685 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001686#endif
1687
Jie Yang6f0c4222015-10-13 23:41:00 +08001688 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301689 /*create compress_device"*/
Mark Brown291bfb92018-04-19 12:14:10 +01001690 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
Mark Brownc74184e2012-04-04 22:12:09 +01001691 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001692 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301693 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001694 return ret;
1695 }
1696 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301697
1698 if (!dai_link->params) {
1699 /* create the pcm */
Mark Brown291bfb92018-04-19 12:14:10 +01001700 ret = soc_new_pcm(rtd, rtd->num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301701 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001702 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301703 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001704 return ret;
1705 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001706 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1707 if (ret < 0)
1708 return ret;
1709 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1710 rtd->num_codecs, rtd);
1711 if (ret < 0)
1712 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301713 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001714 INIT_DELAYED_WORK(&rtd->delayed_work,
1715 codec2codec_close_delayed_work);
1716
Namarta Kohli1245b702012-08-16 17:10:41 +05301717 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001718 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001719 if (ret)
1720 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001721 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001722 }
1723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724 return 0;
1725}
1726
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001727static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001728{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001729 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001730 struct snd_soc_component *component;
1731 const char *name;
1732 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001733
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001734 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1735 /* codecs, usually analog devices */
1736 name = aux_dev->codec_name;
1737 codec_of_node = aux_dev->codec_of_node;
1738 component = soc_find_component(codec_of_node, name);
1739 if (!component) {
1740 if (codec_of_node)
1741 name = of_node_full_name(codec_of_node);
1742 goto err_defer;
1743 }
1744 } else if (aux_dev->name) {
1745 /* generic components */
1746 name = aux_dev->name;
1747 component = soc_find_component(NULL, name);
1748 if (!component)
1749 goto err_defer;
1750 } else {
1751 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1752 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001753 }
1754
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001755 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001756 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001757
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001758 return 0;
1759
1760err_defer:
1761 dev_err(card->dev, "ASoC: %s not registered\n", name);
1762 return -EPROBE_DEFER;
1763}
1764
1765static int soc_probe_aux_devices(struct snd_soc_card *card)
1766{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001767 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001768 int order;
1769 int ret;
1770
1771 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1772 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001773 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001774 if (comp->driver->probe_order == order) {
1775 ret = soc_probe_component(card, comp);
1776 if (ret < 0) {
1777 dev_err(card->dev,
1778 "ASoC: failed to probe aux component %s %d\n",
1779 comp->name, ret);
1780 return ret;
1781 }
1782 }
1783 }
1784 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001785
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001786 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001787}
1788
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001789static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001790{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001791 struct snd_soc_component *comp, *_comp;
1792 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001793
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001794 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1795 order++) {
1796 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001797 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001798
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001799 if (comp->driver->remove_order == order) {
1800 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001801 /* remove it from the card's aux_comp_list */
1802 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001803 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001804 }
1805 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001806}
1807
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001808static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001809{
1810 int ret;
1811
1812 if (codec->cache_init)
1813 return 0;
1814
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001815 ret = snd_soc_cache_init(codec);
1816 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001817 dev_err(codec->dev,
1818 "ASoC: Failed to set cache compression type: %d\n",
1819 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001820 return ret;
1821 }
1822 codec->cache_init = 1;
1823 return 0;
1824}
1825
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001826/**
1827 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1828 * @rtd: The runtime for which the DAI link format should be changed
1829 * @dai_fmt: The new DAI link format
1830 *
1831 * This function updates the DAI link format for all DAIs connected to the DAI
1832 * link for the specified runtime.
1833 *
1834 * Note: For setups with a static format set the dai_fmt field in the
1835 * corresponding snd_dai_link struct instead of using this function.
1836 *
1837 * Returns 0 on success, otherwise a negative error code.
1838 */
1839int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1840 unsigned int dai_fmt)
1841{
1842 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1843 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1844 unsigned int i;
1845 int ret;
1846
1847 for (i = 0; i < rtd->num_codecs; i++) {
1848 struct snd_soc_dai *codec_dai = codec_dais[i];
1849
1850 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1851 if (ret != 0 && ret != -ENOTSUPP) {
1852 dev_warn(codec_dai->dev,
1853 "ASoC: Failed to set DAI format: %d\n", ret);
1854 return ret;
1855 }
1856 }
1857
1858 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001859 /* the component which has non_legacy_dai_naming is Codec */
1860 if (cpu_dai->codec ||
1861 cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001862 unsigned int inv_dai_fmt;
1863
1864 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1865 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1866 case SND_SOC_DAIFMT_CBM_CFM:
1867 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1868 break;
1869 case SND_SOC_DAIFMT_CBM_CFS:
1870 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1871 break;
1872 case SND_SOC_DAIFMT_CBS_CFM:
1873 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1874 break;
1875 case SND_SOC_DAIFMT_CBS_CFS:
1876 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1877 break;
1878 }
1879
1880 dai_fmt = inv_dai_fmt;
1881 }
1882
1883 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1884 if (ret != 0 && ret != -ENOTSUPP) {
1885 dev_warn(cpu_dai->dev,
1886 "ASoC: Failed to set DAI format: %d\n", ret);
1887 return ret;
1888 }
1889
1890 return 0;
1891}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001892EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001893
Liam Girdwood345233d2017-01-14 16:13:02 +08001894
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001895#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001896/* Trim special characters, and replace '-' with '_' since '-' is used to
1897 * separate different DMI fields in the card long name. Only number and
1898 * alphabet characters and a few separator characters are kept.
1899 */
1900static void cleanup_dmi_name(char *name)
1901{
1902 int i, j = 0;
1903
1904 for (i = 0; name[i]; i++) {
1905 if (isalnum(name[i]) || (name[i] == '.')
1906 || (name[i] == '_'))
1907 name[j++] = name[i];
1908 else if (name[i] == '-')
1909 name[j++] = '_';
1910 }
1911
1912 name[j] = '\0';
1913}
1914
Mengdong Lin98faf432017-06-28 15:01:39 +08001915/* Check if a DMI field is valid, i.e. not containing any string
1916 * in the black list.
1917 */
1918static int is_dmi_valid(const char *field)
1919{
1920 int i = 0;
1921
1922 while (dmi_blacklist[i]) {
1923 if (strstr(field, dmi_blacklist[i]))
1924 return 0;
1925 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001926 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001927
1928 return 1;
1929}
1930
Liam Girdwood345233d2017-01-14 16:13:02 +08001931/**
1932 * snd_soc_set_dmi_name() - Register DMI names to card
1933 * @card: The card to register DMI names
1934 * @flavour: The flavour "differentiator" for the card amongst its peers.
1935 *
1936 * An Intel machine driver may be used by many different devices but are
1937 * difficult for userspace to differentiate, since machine drivers ususally
1938 * use their own name as the card short name and leave the card long name
1939 * blank. To differentiate such devices and fix bugs due to lack of
1940 * device-specific configurations, this function allows DMI info to be used
1941 * as the sound card long name, in the format of
1942 * "vendor-product-version-board"
1943 * (Character '-' is used to separate different DMI fields here).
1944 * This will help the user space to load the device-specific Use Case Manager
1945 * (UCM) configurations for the card.
1946 *
1947 * Possible card long names may be:
1948 * DellInc.-XPS139343-01-0310JH
1949 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1950 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1951 *
1952 * This function also supports flavoring the card longname to provide
1953 * the extra differentiation, like "vendor-product-version-board-flavor".
1954 *
1955 * We only keep number and alphabet characters and a few separator characters
1956 * in the card long name since UCM in the user space uses the card long names
1957 * as card configuration directory names and AudoConf cannot support special
1958 * charactors like SPACE.
1959 *
1960 * Returns 0 on success, otherwise a negative error code.
1961 */
1962int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1963{
1964 const char *vendor, *product, *product_version, *board;
1965 size_t longname_buf_size = sizeof(card->snd_card->longname);
1966 size_t len;
1967
1968 if (card->long_name)
1969 return 0; /* long name already set by driver or from DMI */
1970
1971 /* make up dmi long name as: vendor.product.version.board */
1972 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08001973 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001974 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1975 return 0;
1976 }
1977
Mengdong Lin98faf432017-06-28 15:01:39 +08001978
Liam Girdwood345233d2017-01-14 16:13:02 +08001979 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1980 "%s", vendor);
1981 cleanup_dmi_name(card->dmi_longname);
1982
1983 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08001984 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001985 len = strlen(card->dmi_longname);
1986 snprintf(card->dmi_longname + len,
1987 longname_buf_size - len,
1988 "-%s", product);
1989
1990 len++; /* skip the separator "-" */
1991 if (len < longname_buf_size)
1992 cleanup_dmi_name(card->dmi_longname + len);
1993
1994 /* some vendors like Lenovo may only put a self-explanatory
1995 * name in the product version field
1996 */
1997 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08001998 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08001999 len = strlen(card->dmi_longname);
2000 snprintf(card->dmi_longname + len,
2001 longname_buf_size - len,
2002 "-%s", product_version);
2003
2004 len++;
2005 if (len < longname_buf_size)
2006 cleanup_dmi_name(card->dmi_longname + len);
2007 }
2008 }
2009
2010 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08002011 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08002012 len = strlen(card->dmi_longname);
2013 snprintf(card->dmi_longname + len,
2014 longname_buf_size - len,
2015 "-%s", board);
2016
2017 len++;
2018 if (len < longname_buf_size)
2019 cleanup_dmi_name(card->dmi_longname + len);
2020 } else if (!product) {
2021 /* fall back to using legacy name */
2022 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2023 return 0;
2024 }
2025
2026 /* Add flavour to dmi long name */
2027 if (flavour) {
2028 len = strlen(card->dmi_longname);
2029 snprintf(card->dmi_longname + len,
2030 longname_buf_size - len,
2031 "-%s", flavour);
2032
2033 len++;
2034 if (len < longname_buf_size)
2035 cleanup_dmi_name(card->dmi_longname + len);
2036 }
2037
2038 /* set the card long name */
2039 card->long_name = card->dmi_longname;
2040
2041 return 0;
2042}
2043EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02002044#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08002045
Mark Brownb19e6e72012-03-14 21:18:39 +00002046static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002047{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002048 struct snd_soc_codec *codec;
Mengdong Lin1a497982015-11-18 02:34:11 -05002049 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002050 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002051 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002052
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002053 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002054 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055
Mark Brownb19e6e72012-03-14 21:18:39 +00002056 /* bind DAIs */
2057 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002058 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002059 if (ret != 0)
2060 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002061 }
2062
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002063 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002064 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002065 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002066 if (ret != 0)
2067 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002068 }
2069
Mengdong Linf8f80362015-12-02 14:11:22 +08002070 /* add predefined DAI links to the list */
2071 for (i = 0; i < card->num_links; i++)
2072 snd_soc_add_dai_link(card, card->dai_link+i);
2073
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002074 /* initialize the register cache for each available codec */
2075 list_for_each_entry(codec, &codec_list, list) {
2076 if (codec->cache_init)
2077 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02002078 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00002079 if (ret < 0)
2080 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002081 }
2082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002083 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002084 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002085 card->owner, 0, &card->snd_card);
2086 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002087 dev_err(card->dev,
2088 "ASoC: can't create sound card for card %s: %d\n",
2089 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002090 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002091 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002092
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002093 soc_init_card_debugfs(card);
2094
Mark Browne37a4972011-03-02 18:21:57 +00002095 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2096 card->dapm.dev = card->dev;
2097 card->dapm.card = card;
2098 list_add(&card->dapm.list, &card->dapm_list);
2099
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002100#ifdef CONFIG_DEBUG_FS
2101 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2102#endif
2103
Mark Brown88ee1c62011-02-02 10:43:26 +00002104#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002105 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002106 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002107#endif
Andy Green6ed25972008-06-13 16:24:05 +01002108
Mark Brown9a841eb2011-04-12 17:51:37 -07002109 if (card->dapm_widgets)
2110 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2111 card->num_dapm_widgets);
2112
Nicolin Chenf23e8602015-02-14 17:22:49 -08002113 if (card->of_dapm_widgets)
2114 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2115 card->num_of_dapm_widgets);
2116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002117 /* initialise the sound card only once */
2118 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002119 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002120 if (ret < 0)
2121 goto card_probe_error;
2122 }
2123
Stephen Warren62ae68f2012-06-08 12:34:23 -06002124 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002125 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2126 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002127 list_for_each_entry(rtd, &card->rtd_list, list) {
2128 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002129 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002130 dev_err(card->dev,
2131 "ASoC: failed to instantiate card %d\n",
2132 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002133 goto probe_dai_err;
2134 }
2135 }
2136 }
2137
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002138 /* probe auxiliary components */
2139 ret = soc_probe_aux_devices(card);
2140 if (ret < 0)
2141 goto probe_dai_err;
2142
Mengdong Lin61b00882015-12-02 14:11:48 +08002143 /* Find new DAI links added during probing components and bind them.
2144 * Components with topology may bring new DAIs and DAI links.
2145 */
2146 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2147 if (soc_is_dai_link_bound(card, dai_link))
2148 continue;
2149
2150 ret = soc_init_dai_link(card, dai_link);
2151 if (ret)
2152 goto probe_dai_err;
2153 ret = soc_bind_dai_link(card, dai_link);
2154 if (ret)
2155 goto probe_dai_err;
2156 }
2157
Stephen Warren62ae68f2012-06-08 12:34:23 -06002158 /* probe all DAI links on this card */
2159 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2160 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002161 list_for_each_entry(rtd, &card->rtd_list, list) {
2162 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002163 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002164 dev_err(card->dev,
2165 "ASoC: failed to instantiate card %d\n",
2166 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002167 goto probe_dai_err;
2168 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002169 }
2170 }
2171
Mark Brown888df392012-02-16 19:37:51 -08002172 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002173 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002174
Mark Brownb7af1da2011-04-07 19:18:44 +09002175 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002176 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002177
Mark Brownb8ad29d2011-03-02 18:35:51 +00002178 if (card->dapm_routes)
2179 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2180 card->num_dapm_routes);
2181
Nicolin Chenf23e8602015-02-14 17:22:49 -08002182 if (card->of_dapm_routes)
2183 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2184 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002185
Takashi Iwai861886d2017-04-24 08:54:42 +02002186 /* try to set some sane longname if DMI is available */
2187 snd_soc_set_dmi_name(card, NULL);
2188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002189 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002190 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002191 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2192 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002193 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2194 "%s", card->driver_name ? card->driver_name : card->name);
2195 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2196 switch (card->snd_card->driver[i]) {
2197 case '_':
2198 case '-':
2199 case '\0':
2200 break;
2201 default:
2202 if (!isalnum(card->snd_card->driver[i]))
2203 card->snd_card->driver[i] = '_';
2204 break;
2205 }
2206 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002207
Mark Brown28e9ad92011-03-02 18:36:34 +00002208 if (card->late_probe) {
2209 ret = card->late_probe(card);
2210 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002211 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002212 card->name, ret);
2213 goto probe_aux_dev_err;
2214 }
2215 }
2216
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002217 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002219 ret = snd_card_register(card->snd_card);
2220 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002221 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2222 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002223 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002224 }
2225
Mark Brown435c5e22008-12-04 15:32:53 +00002226 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002227 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002229 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002230
2231 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002232
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002233probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002234 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002236probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002237 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002238
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002239card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002240 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002241 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002242
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002243 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002244 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002245 snd_card_free(card->snd_card);
2246
Mark Brownb19e6e72012-03-14 21:18:39 +00002247base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002248 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002249 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002250 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251
Mark Brownb19e6e72012-03-14 21:18:39 +00002252 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002253}
2254
2255/* probes a new socdev */
2256static int soc_probe(struct platform_device *pdev)
2257{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002259
Vinod Koul70a7ca32011-01-14 19:22:48 +05302260 /*
2261 * no card, so machine driver should be registering card
2262 * we should not be here in that case so ret error
2263 */
2264 if (!card)
2265 return -EINVAL;
2266
Mark Brownfe4085e2012-03-02 13:07:41 +00002267 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002268 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002269 card->name);
2270
Mark Brown435c5e22008-12-04 15:32:53 +00002271 /* Bodge while we unpick instantiation */
2272 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002273
Mark Brown28d528c2012-08-09 18:45:23 +01002274 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275}
2276
Vinod Koulb0e26482011-01-13 22:48:02 +05302277static int soc_cleanup_card_resources(struct snd_soc_card *card)
2278{
Mengdong Lin1a497982015-11-18 02:34:11 -05002279 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302280
2281 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002282 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002283 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302284
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002285 /* free the ALSA card at first; this syncs with pending operations */
2286 snd_card_free(card->snd_card);
2287
Vinod Koulb0e26482011-01-13 22:48:02 +05302288 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002289 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002290 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302291
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002292 /* remove auxiliary devices */
2293 soc_remove_aux_devices(card);
2294
Mark Brownd1e81422016-08-18 19:32:59 +01002295 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302296 soc_cleanup_card_debugfs(card);
2297
2298 /* remove the card */
2299 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002300 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302301
Vinod Koulb0e26482011-01-13 22:48:02 +05302302 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302303}
2304
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305/* removes a socdev */
2306static int soc_remove(struct platform_device *pdev)
2307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002308 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309
Mark Brownc5af3a22008-11-28 13:29:45 +00002310 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 return 0;
2312}
2313
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002314int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002315{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002316 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002317 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002318
2319 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002320 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002321
2322 /* Flush out pmdown_time work - we actually do want to run it
2323 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002324 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002325 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002327 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002328
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002329 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002330 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002331 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002332 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002333
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002334 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002335 for (i = 0; i < rtd->num_codecs; i++) {
2336 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002337 pinctrl_pm_select_sleep_state(codec_dai->dev);
2338 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002339 }
2340
Mark Brown416356f2009-06-30 19:05:15 +01002341 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002342}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002343EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002344
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002345const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302346 .suspend = snd_soc_suspend,
2347 .resume = snd_soc_resume,
2348 .freeze = snd_soc_suspend,
2349 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002350 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302351 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002352};
Stephen Warrendeb26072011-04-05 19:35:30 -06002353EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002354
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002355/* ASoC platform driver */
2356static struct platform_driver soc_driver = {
2357 .driver = {
2358 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002359 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 },
2361 .probe = soc_probe,
2362 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363};
2364
Mark Brown096e49d2009-07-05 15:12:22 +01002365/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002366 * snd_soc_cnew - create new control
2367 * @_template: control template
2368 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002369 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002370 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371 *
2372 * Create a new mixer control from a template control.
2373 *
2374 * Returns 0 for success, else error.
2375 */
2376struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002377 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002378 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002379{
2380 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002381 struct snd_kcontrol *kcontrol;
2382 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383
2384 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002385 template.index = 0;
2386
Mark Brownefb7ac32011-03-08 17:23:24 +00002387 if (!long_name)
2388 long_name = template.name;
2389
2390 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002391 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002392 if (!name)
2393 return NULL;
2394
Mark Brownefb7ac32011-03-08 17:23:24 +00002395 template.name = name;
2396 } else {
2397 template.name = long_name;
2398 }
2399
2400 kcontrol = snd_ctl_new1(&template, data);
2401
2402 kfree(name);
2403
2404 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002405}
2406EXPORT_SYMBOL_GPL(snd_soc_cnew);
2407
Liam Girdwood022658b2012-02-03 17:43:09 +00002408static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2409 const struct snd_kcontrol_new *controls, int num_controls,
2410 const char *prefix, void *data)
2411{
2412 int err, i;
2413
2414 for (i = 0; i < num_controls; i++) {
2415 const struct snd_kcontrol_new *control = &controls[i];
2416 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2417 control->name, prefix));
2418 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002419 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2420 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002421 return err;
2422 }
2423 }
2424
2425 return 0;
2426}
2427
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002428struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2429 const char *name)
2430{
2431 struct snd_card *card = soc_card->snd_card;
2432 struct snd_kcontrol *kctl;
2433
2434 if (unlikely(!name))
2435 return NULL;
2436
2437 list_for_each_entry(kctl, &card->controls, list)
2438 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2439 return kctl;
2440 return NULL;
2441}
2442EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2443
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002445 * snd_soc_add_component_controls - Add an array of controls to a component.
2446 *
2447 * @component: Component to add controls to
2448 * @controls: Array of controls to add
2449 * @num_controls: Number of elements in the array
2450 *
2451 * Return: 0 for success, else error.
2452 */
2453int snd_soc_add_component_controls(struct snd_soc_component *component,
2454 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2455{
2456 struct snd_card *card = component->card->snd_card;
2457
2458 return snd_soc_add_controls(card, component->dev, controls,
2459 num_controls, component->name_prefix, component);
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2462
2463/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002464 * snd_soc_add_codec_controls - add an array of controls to a codec.
2465 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002466 * duplicating this code.
2467 *
2468 * @codec: codec to add controls to
2469 * @controls: array of controls to add
2470 * @num_controls: number of elements in the array
2471 *
2472 * Return 0 for success, else error.
2473 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002474int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002475 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002476{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002477 return snd_soc_add_component_controls(&codec->component, controls,
2478 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002479}
Liam Girdwood022658b2012-02-03 17:43:09 +00002480EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002481
2482/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002483 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2484 * Convenience function to add a list of controls.
2485 *
2486 * @soc_card: SoC card to add controls to
2487 * @controls: array of controls to add
2488 * @num_controls: number of elements in the array
2489 *
2490 * Return 0 for success, else error.
2491 */
2492int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2493 const struct snd_kcontrol_new *controls, int num_controls)
2494{
2495 struct snd_card *card = soc_card->snd_card;
2496
2497 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2498 NULL, soc_card);
2499}
2500EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2501
2502/**
2503 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2504 * Convienience function to add a list of controls.
2505 *
2506 * @dai: DAI to add controls to
2507 * @controls: array of controls to add
2508 * @num_controls: number of elements in the array
2509 *
2510 * Return 0 for success, else error.
2511 */
2512int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2513 const struct snd_kcontrol_new *controls, int num_controls)
2514{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002515 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002516
2517 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2518 NULL, dai);
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2521
2522/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002523 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2524 * @dai: DAI
2525 * @clk_id: DAI specific clock ID
2526 * @freq: new clock frequency in Hz
2527 * @dir: new clock direction - input/output.
2528 *
2529 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2530 */
2531int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2532 unsigned int freq, int dir)
2533{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002534 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002535 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002536
2537 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2538 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002539}
2540EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2541
2542/**
Mark Brownec4ee522011-03-07 20:58:11 +00002543 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2544 * @codec: CODEC
2545 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002546 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002547 * @freq: new clock frequency in Hz
2548 * @dir: new clock direction - input/output.
2549 *
2550 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2551 */
2552int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002553 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002554{
2555 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002556 return codec->driver->set_sysclk(codec, clk_id, source,
2557 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002558 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002559 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002560}
2561EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2562
2563/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002564 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2565 * @component: COMPONENT
2566 * @clk_id: DAI specific clock ID
2567 * @source: Source for the clock
2568 * @freq: new clock frequency in Hz
2569 * @dir: new clock direction - input/output.
2570 *
2571 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2572 */
2573int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2574 int source, unsigned int freq, int dir)
2575{
2576 /* will be removed */
2577 if (component->set_sysclk)
2578 return component->set_sysclk(component, clk_id, source,
2579 freq, dir);
2580
2581 if (component->driver->set_sysclk)
2582 return component->driver->set_sysclk(component, clk_id, source,
2583 freq, dir);
2584
2585 return -ENOTSUPP;
2586}
2587EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2588
2589/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002590 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2591 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002592 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002593 * @div: new clock divisor.
2594 *
2595 * Configures the clock dividers. This is used to derive the best DAI bit and
2596 * frame clocks from the system or master clock. It's best to set the DAI bit
2597 * and frame clocks as low as possible to save system power.
2598 */
2599int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2600 int div_id, int div)
2601{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002602 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002603 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002604 else
2605 return -EINVAL;
2606}
2607EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2608
2609/**
2610 * snd_soc_dai_set_pll - configure DAI PLL.
2611 * @dai: DAI
2612 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002613 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002614 * @freq_in: PLL input clock frequency in Hz
2615 * @freq_out: requested PLL output clock frequency in Hz
2616 *
2617 * Configures and enables PLL to generate output clock based on input clock.
2618 */
Mark Brown85488032009-09-05 18:52:16 +01002619int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2620 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002621{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002622 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002623 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002624 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002625
2626 return snd_soc_component_set_pll(dai->component, pll_id, source,
2627 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002628}
2629EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2630
Mark Brownec4ee522011-03-07 20:58:11 +00002631/*
2632 * snd_soc_codec_set_pll - configure codec PLL.
2633 * @codec: CODEC
2634 * @pll_id: DAI specific PLL ID
2635 * @source: DAI specific source for the PLL
2636 * @freq_in: PLL input clock frequency in Hz
2637 * @freq_out: requested PLL output clock frequency in Hz
2638 *
2639 * Configures and enables PLL to generate output clock based on input clock.
2640 */
2641int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2642 unsigned int freq_in, unsigned int freq_out)
2643{
2644 if (codec->driver->set_pll)
2645 return codec->driver->set_pll(codec, pll_id, source,
2646 freq_in, freq_out);
2647 else
2648 return -EINVAL;
2649}
2650EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2651
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002652/*
2653 * snd_soc_component_set_pll - configure component PLL.
2654 * @component: COMPONENT
2655 * @pll_id: DAI specific PLL ID
2656 * @source: DAI specific source for the PLL
2657 * @freq_in: PLL input clock frequency in Hz
2658 * @freq_out: requested PLL output clock frequency in Hz
2659 *
2660 * Configures and enables PLL to generate output clock based on input clock.
2661 */
2662int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2663 int source, unsigned int freq_in,
2664 unsigned int freq_out)
2665{
2666 /* will be removed */
2667 if (component->set_pll)
2668 return component->set_pll(component, pll_id, source,
2669 freq_in, freq_out);
2670
2671 if (component->driver->set_pll)
2672 return component->driver->set_pll(component, pll_id, source,
2673 freq_in, freq_out);
2674
2675 return -EINVAL;
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2678
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002679/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002680 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2681 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002682 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002683 *
2684 * Configures the DAI for a preset BCLK to sample rate ratio.
2685 */
2686int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2687{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002688 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002689 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2690 else
2691 return -EINVAL;
2692}
2693EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2694
2695/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002696 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2697 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002698 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002699 *
2700 * Configures the DAI hardware format and clocking.
2701 */
2702int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2703{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002704 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002705 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002706 if (dai->driver->ops->set_fmt == NULL)
2707 return -ENOTSUPP;
2708 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002709}
2710EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2711
2712/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002713 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002714 * @slots: Number of slots in use.
2715 * @tx_mask: bitmask representing active TX slots.
2716 * @rx_mask: bitmask representing active RX slots.
2717 *
2718 * Generates the TDM tx and rx slot default masks for DAI.
2719 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002720static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002721 unsigned int *tx_mask,
2722 unsigned int *rx_mask)
2723{
2724 if (*tx_mask || *rx_mask)
2725 return 0;
2726
2727 if (!slots)
2728 return -EINVAL;
2729
2730 *tx_mask = (1 << slots) - 1;
2731 *rx_mask = (1 << slots) - 1;
2732
2733 return 0;
2734}
2735
2736/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002737 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2738 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002739 * @tx_mask: bitmask representing active TX slots.
2740 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002741 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002742 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002743 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002744 * This function configures the specified DAI for TDM operation. @slot contains
2745 * the total number of slots of the TDM stream and @slot_with the width of each
2746 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2747 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2748 * DAI should write to or read from. If a bit is set the corresponding slot is
2749 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2750 * the first slot, bit 1 to the second slot and so on. The first active slot
2751 * maps to the first channel of the DAI, the second active slot to the second
2752 * channel and so on.
2753 *
2754 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2755 * @rx_mask and @slot_width will be ignored.
2756 *
2757 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002758 */
2759int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002760 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002761{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002762 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002763 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002764 &tx_mask, &rx_mask);
2765 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002766 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002767
Benoit Cousson88bd8702014-07-08 23:19:34 +02002768 dai->tx_mask = tx_mask;
2769 dai->rx_mask = rx_mask;
2770
Kuninori Morimoto46471922017-09-25 01:38:34 +00002771 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002772 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002773 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002774 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002775 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002776}
2777EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2778
2779/**
Barry Song472df3c2009-09-12 01:16:29 +08002780 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2781 * @dai: DAI
2782 * @tx_num: how many TX channels
2783 * @tx_slot: pointer to an array which imply the TX slot number channel
2784 * 0~num-1 uses
2785 * @rx_num: how many RX channels
2786 * @rx_slot: pointer to an array which imply the RX slot number channel
2787 * 0~num-1 uses
2788 *
2789 * configure the relationship between channel number and TDM slot number.
2790 */
2791int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2792 unsigned int tx_num, unsigned int *tx_slot,
2793 unsigned int rx_num, unsigned int *rx_slot)
2794{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002795 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002796 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002797 rx_num, rx_slot);
2798 else
2799 return -EINVAL;
2800}
2801EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2802
2803/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002804 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2805 * @dai: DAI
2806 * @tristate: tristate enable
2807 *
2808 * Tristates the DAI so that others can use it.
2809 */
2810int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2811{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002812 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002813 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002814 else
2815 return -EINVAL;
2816}
2817EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2818
2819/**
2820 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2821 * @dai: DAI
2822 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002823 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002824 *
2825 * Mutes the DAI DAC.
2826 */
Mark Brownda183962013-02-06 15:44:07 +00002827int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2828 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002829{
Mark Brownda183962013-02-06 15:44:07 +00002830 if (!dai->driver)
2831 return -ENOTSUPP;
2832
2833 if (dai->driver->ops->mute_stream)
2834 return dai->driver->ops->mute_stream(dai, mute, direction);
2835 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2836 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002837 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002838 else
Mark Brown04570c62012-04-13 19:16:03 +01002839 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002840}
2841EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2842
Mark Brownc5af3a22008-11-28 13:29:45 +00002843/**
2844 * snd_soc_register_card - Register a card with the ASoC core
2845 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002846 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002847 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002848 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302849int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002850{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002851 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002852 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002853
Mark Brownc5af3a22008-11-28 13:29:45 +00002854 if (!card->name || !card->dev)
2855 return -EINVAL;
2856
Stephen Warren5a504962011-12-21 10:40:59 -07002857 for (i = 0; i < card->num_links; i++) {
2858 struct snd_soc_dai_link *link = &card->dai_link[i];
2859
Mengdong Lin923c5e612015-11-23 11:01:49 -05002860 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002861 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002862 dev_err(card->dev, "ASoC: failed to init link %s\n",
2863 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002864 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002865 }
Stephen Warren5a504962011-12-21 10:40:59 -07002866 }
2867
Mark Browned77cc12011-05-03 18:25:34 +01002868 dev_set_drvdata(card->dev, card);
2869
Stephen Warren111c6412011-01-28 14:26:35 -07002870 snd_soc_initialize_card_lists(card);
2871
Mengdong Linf8f80362015-12-02 14:11:22 +08002872 INIT_LIST_HEAD(&card->dai_link_list);
2873 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002874
Mengdong Lin1a497982015-11-18 02:34:11 -05002875 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002876 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002877
Mark Browndb432b42011-10-03 21:06:40 +01002878 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002879 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002880 card->instantiated = 0;
2881 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002882 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002883
Mark Brownb19e6e72012-03-14 21:18:39 +00002884 ret = snd_soc_instantiate_card(card);
2885 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002886 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002887
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002888 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002889 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002890 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2891 int j;
2892
2893 for (j = 0; j < rtd->num_codecs; j++) {
2894 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2895 if (!codec_dai->active)
2896 pinctrl_pm_select_sleep_state(codec_dai->dev);
2897 }
2898
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002899 if (!cpu_dai->active)
2900 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2901 }
2902
Mark Brownb19e6e72012-03-14 21:18:39 +00002903 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002904}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302905EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002906
2907/**
2908 * snd_soc_unregister_card - Unregister a card with the ASoC core
2909 *
2910 * @card: Card to unregister
2911 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002912 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302913int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002914{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002915 if (card->instantiated) {
2916 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002917 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302918 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002919 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002920 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002921
2922 return 0;
2923}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302924EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002925
2926/*
2927 * Simplify DAI link configuration by removing ".-1" from device names
2928 * and sanitizing names.
2929 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002930static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002931{
2932 char *found, name[NAME_SIZE];
2933 int id1, id2;
2934
2935 if (dev_name(dev) == NULL)
2936 return NULL;
2937
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002938 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002939
2940 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00002941 found = strstr(name, dev->driver->name);
2942 if (found) {
2943 /* get ID */
2944 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2945
2946 /* discard ID from name if ID == -1 */
2947 if (*id == -1)
2948 found[strlen(dev->driver->name)] = '\0';
2949 }
2950
2951 } else {
2952 /* I2C component devices are named "bus-addr" */
2953 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2954 char tmp[NAME_SIZE];
2955
2956 /* create unique ID number from I2C addr and bus */
2957 *id = ((id1 & 0xffff) << 16) + id2;
2958
2959 /* sanitize component name for DAI link creation */
2960 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002961 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00002962 } else
2963 *id = 0;
2964 }
2965
2966 return kstrdup(name, GFP_KERNEL);
2967}
2968
2969/*
2970 * Simplify DAI link naming for single devices with multiple DAIs by removing
2971 * any ".-1" and using the DAI name (instead of device name).
2972 */
2973static inline char *fmt_multiple_name(struct device *dev,
2974 struct snd_soc_dai_driver *dai_drv)
2975{
2976 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002977 dev_err(dev,
2978 "ASoC: error - multiple DAI %s registered with no name\n",
2979 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00002980 return NULL;
2981 }
2982
2983 return kstrdup(dai_drv->name, GFP_KERNEL);
2984}
2985
2986/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002987 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002988 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002989 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002990 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002991static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002992{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002993 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002994
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002995 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002996 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2997 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002998 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002999 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003000 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003001 }
Mark Brown91151712008-11-30 23:31:24 +00003002}
Mark Brown91151712008-11-30 23:31:24 +00003003
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003004/* Create a DAI and add it to the component's DAI list */
3005static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3006 struct snd_soc_dai_driver *dai_drv,
3007 bool legacy_dai_naming)
3008{
3009 struct device *dev = component->dev;
3010 struct snd_soc_dai *dai;
3011
3012 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3013
3014 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3015 if (dai == NULL)
3016 return NULL;
3017
3018 /*
3019 * Back in the old days when we still had component-less DAIs,
3020 * instead of having a static name, component-less DAIs would
3021 * inherit the name of the parent device so it is possible to
3022 * register multiple instances of the DAI. We still need to keep
3023 * the same naming style even though those DAIs are not
3024 * component-less anymore.
3025 */
3026 if (legacy_dai_naming &&
3027 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3028 dai->name = fmt_single_name(dev, &dai->id);
3029 } else {
3030 dai->name = fmt_multiple_name(dev, dai_drv);
3031 if (dai_drv->id)
3032 dai->id = dai_drv->id;
3033 else
3034 dai->id = component->num_dai;
3035 }
3036 if (dai->name == NULL) {
3037 kfree(dai);
3038 return NULL;
3039 }
3040
3041 dai->component = component;
3042 dai->dev = dev;
3043 dai->driver = dai_drv;
3044 if (!dai->driver->ops)
3045 dai->driver->ops = &null_dai_ops;
3046
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003047 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003048 component->num_dai++;
3049
3050 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3051 return dai;
3052}
3053
Mark Brown91151712008-11-30 23:31:24 +00003054/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003055 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003056 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003057 * @component: The component the DAIs are registered for
3058 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003059 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003060 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3061 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003062 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003063static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003064 struct snd_soc_dai_driver *dai_drv, size_t count,
3065 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003066{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003067 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003068 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003069 unsigned int i;
3070 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003071
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003072 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003073
3074 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003075
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003076 dai = soc_add_dai(component, dai_drv + i,
3077 count == 1 && legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003078 if (dai == NULL) {
3079 ret = -ENOMEM;
3080 goto err;
3081 }
Mark Brown91151712008-11-30 23:31:24 +00003082 }
3083
3084 return 0;
3085
3086err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003087 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003088
3089 return ret;
3090}
Mark Brown91151712008-11-30 23:31:24 +00003091
Mengdong Lin68003e62015-12-31 16:40:43 +08003092/**
3093 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3094 *
3095 * @component: The component the DAIs are registered for
3096 * @dai_drv: DAI driver to use for the DAI
3097 *
3098 * Topology can use this API to register DAIs when probing a component.
3099 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3100 * will be freed in the component cleanup.
3101 */
3102int snd_soc_register_dai(struct snd_soc_component *component,
3103 struct snd_soc_dai_driver *dai_drv)
3104{
3105 struct snd_soc_dapm_context *dapm =
3106 snd_soc_component_get_dapm(component);
3107 struct snd_soc_dai *dai;
3108 int ret;
3109
3110 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3111 dev_err(component->dev, "Invalid dai type %d\n",
3112 dai_drv->dobj.type);
3113 return -EINVAL;
3114 }
3115
3116 lockdep_assert_held(&client_mutex);
3117 dai = soc_add_dai(component, dai_drv, false);
3118 if (!dai)
3119 return -ENOMEM;
3120
3121 /* Create the DAI widgets here. After adding DAIs, topology may
3122 * also add routes that need these widgets as source or sink.
3123 */
3124 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3125 if (ret != 0) {
3126 dev_err(component->dev,
3127 "Failed to create DAI widgets %d\n", ret);
3128 }
3129
3130 return ret;
3131}
3132EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3133
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003134static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3135 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003136{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003137 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003138
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003139 component->driver->seq_notifier(component, type, subseq);
3140}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003141
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003142static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3143 int event)
3144{
3145 struct snd_soc_component *component = dapm->component;
3146
3147 return component->driver->stream_event(component, event);
3148}
3149
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003150static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3151 struct snd_soc_pcm_runtime *rtd)
3152{
3153 if (component->driver->pcm_new)
3154 return component->driver->pcm_new(rtd);
3155
3156 return 0;
3157}
3158
3159static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3160 struct snd_pcm *pcm)
3161{
3162 if (component->driver->pcm_free)
3163 component->driver->pcm_free(pcm);
3164}
3165
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003166static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3167 enum snd_soc_bias_level level)
3168{
3169 struct snd_soc_component *component = dapm->component;
3170
3171 return component->driver->set_bias_level(component, level);
3172}
3173
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003174static int snd_soc_component_initialize(struct snd_soc_component *component,
3175 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003176{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003177 struct snd_soc_dapm_context *dapm;
3178
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003179 component->name = fmt_single_name(dev, &component->id);
3180 if (!component->name) {
3181 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003182 return -ENOMEM;
3183 }
3184
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003185 component->dev = dev;
3186 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003187 component->probe = component->driver->probe;
3188 component->remove = component->driver->remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003189 component->suspend = component->driver->suspend;
3190 component->resume = component->driver->resume;
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003191 component->set_sysclk = component->driver->set_sysclk;
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003192 component->set_pll = component->driver->set_pll;
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003193 component->set_jack = component->driver->set_jack;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003194 component->pcm_new = snd_soc_component_drv_pcm_new;
3195 component->pcm_free = snd_soc_component_drv_pcm_free;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003196
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003197 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003198 dapm->dev = dev;
3199 dapm->component = component;
3200 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003201 dapm->idle_bias_off = !driver->idle_bias_on;
3202 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003203 if (driver->seq_notifier)
3204 dapm->seq_notifier = snd_soc_component_seq_notifier;
3205 if (driver->stream_event)
3206 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003207 if (driver->set_bias_level)
3208 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003209
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003210 INIT_LIST_HEAD(&component->dai_list);
3211 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003212
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003213 return 0;
3214}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003215
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003216static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003217{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003218 int val_bytes = regmap_get_val_bytes(component->regmap);
3219
3220 /* Errors are legitimate for non-integer byte multiples */
3221 if (val_bytes > 0)
3222 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003223}
3224
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003225#ifdef CONFIG_REGMAP
3226
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003227/**
3228 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3229 * @component: The component for which to initialize the regmap instance
3230 * @regmap: The regmap instance that should be used by the component
3231 *
3232 * This function allows deferred assignment of the regmap instance that is
3233 * associated with the component. Only use this if the regmap instance is not
3234 * yet ready when the component is registered. The function must also be called
3235 * before the first IO attempt of the component.
3236 */
3237void snd_soc_component_init_regmap(struct snd_soc_component *component,
3238 struct regmap *regmap)
3239{
3240 component->regmap = regmap;
3241 snd_soc_component_setup_regmap(component);
3242}
3243EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3244
3245/**
3246 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3247 * @component: The component for which to de-initialize the regmap instance
3248 *
3249 * Calls regmap_exit() on the regmap instance associated to the component and
3250 * removes the regmap instance from the component.
3251 *
3252 * This function should only be used if snd_soc_component_init_regmap() was used
3253 * to initialize the regmap instance.
3254 */
3255void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3256{
3257 regmap_exit(component->regmap);
3258 component->regmap = NULL;
3259}
3260EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3261
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003262#endif
3263
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003264static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3265{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003266 if (!component->write && !component->read) {
3267 if (!component->regmap)
3268 component->regmap = dev_get_regmap(component->dev, NULL);
3269 if (component->regmap)
3270 snd_soc_component_setup_regmap(component);
3271 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003272
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003273 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003274 INIT_LIST_HEAD(&component->dobj_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003275}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003276
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003277static void snd_soc_component_add(struct snd_soc_component *component)
3278{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003279 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003280 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003281 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003282}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003283
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003284static void snd_soc_component_cleanup(struct snd_soc_component *component)
3285{
3286 snd_soc_unregister_dais(component);
3287 kfree(component->name);
3288}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003289
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003290static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3291{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003292 struct snd_soc_card *card = component->card;
3293
3294 if (card)
3295 snd_soc_unregister_card(card);
3296
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003297 list_del(&component->list);
3298}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003299
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003300#define ENDIANNESS_MAP(name) \
3301 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3302static u64 endianness_format_map[] = {
3303 ENDIANNESS_MAP(S16_),
3304 ENDIANNESS_MAP(U16_),
3305 ENDIANNESS_MAP(S24_),
3306 ENDIANNESS_MAP(U24_),
3307 ENDIANNESS_MAP(S32_),
3308 ENDIANNESS_MAP(U32_),
3309 ENDIANNESS_MAP(S24_3),
3310 ENDIANNESS_MAP(U24_3),
3311 ENDIANNESS_MAP(S20_3),
3312 ENDIANNESS_MAP(U20_3),
3313 ENDIANNESS_MAP(S18_3),
3314 ENDIANNESS_MAP(U18_3),
3315 ENDIANNESS_MAP(FLOAT_),
3316 ENDIANNESS_MAP(FLOAT64_),
3317 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3318};
3319
3320/*
3321 * Fix up the DAI formats for endianness: codecs don't actually see
3322 * the endianness of the data but we're using the CPU format
3323 * definitions which do need to include endianness so we ensure that
3324 * codec DAIs always have both big and little endian variants set.
3325 */
3326static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3327{
3328 int i;
3329
3330 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3331 if (stream->formats & endianness_format_map[i])
3332 stream->formats |= endianness_format_map[i];
3333}
3334
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003335int snd_soc_add_component(struct device *dev,
3336 struct snd_soc_component *component,
3337 const struct snd_soc_component_driver *component_driver,
3338 struct snd_soc_dai_driver *dai_drv,
3339 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003340{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003341 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003342 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003343
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003344 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003345 if (ret)
3346 goto err_free;
3347
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003348 component->ignore_pmdown_time = true;
3349 component->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003350
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003351 if (component_driver->endianness) {
3352 for (i = 0; i < num_dai; i++) {
3353 convert_endianness_formats(&dai_drv[i].playback);
3354 convert_endianness_formats(&dai_drv[i].capture);
3355 }
3356 }
3357
Kuninori Morimoto69941bab2017-10-11 01:38:51 +00003358 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3359 !component_driver->non_legacy_dai_naming);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003360 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003361 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003362 goto err_cleanup;
3363 }
3364
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003365 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003366
3367 return 0;
3368
3369err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003370 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003371err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003372 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003373}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003374EXPORT_SYMBOL_GPL(snd_soc_add_component);
3375
3376int snd_soc_register_component(struct device *dev,
3377 const struct snd_soc_component_driver *component_driver,
3378 struct snd_soc_dai_driver *dai_drv,
3379 int num_dai)
3380{
3381 struct snd_soc_component *component;
3382
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003383 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003384 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003385 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003386
3387 return snd_soc_add_component(dev, component, component_driver,
3388 dai_drv, num_dai);
3389}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003390EXPORT_SYMBOL_GPL(snd_soc_register_component);
3391
3392/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003393 * snd_soc_unregister_component - Unregister all related component
3394 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003395 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003396 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003397 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003398static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003399{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003400 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003401 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003402
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003403 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003404 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003405 if (dev != component->dev ||
3406 !component->registered_as_component)
3407 continue;
3408
3409 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3410 snd_soc_component_del_unlocked(component);
3411 found = 1;
3412 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003413 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003414 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003415
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003416 if (found) {
3417 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003418 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003419
3420 return found;
3421}
3422
3423void snd_soc_unregister_component(struct device *dev)
3424{
3425 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003426}
3427EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3428
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003429struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3430 const char *driver_name)
3431{
3432 struct snd_soc_component *component;
3433 struct snd_soc_component *ret;
3434
3435 ret = NULL;
3436 mutex_lock(&client_mutex);
3437 list_for_each_entry(component, &component_list, list) {
3438 if (dev != component->dev)
3439 continue;
3440
3441 if (driver_name &&
3442 (driver_name != component->driver->name) &&
3443 (strcmp(component->driver->name, driver_name) != 0))
3444 continue;
3445
3446 ret = component;
3447 break;
3448 }
3449 mutex_unlock(&client_mutex);
3450
3451 return ret;
3452}
3453EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3454
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003455static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3456{
3457 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3458
3459 return codec->driver->probe(codec);
3460}
3461
3462static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3463{
3464 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3465
3466 codec->driver->remove(codec);
3467}
3468
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003469static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3470{
3471 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3472
3473 return codec->driver->suspend(codec);
3474}
3475
3476static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3477{
3478 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3479
3480 return codec->driver->resume(codec);
3481}
3482
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003483static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3484 unsigned int reg, unsigned int val)
3485{
3486 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3487
3488 return codec->driver->write(codec, reg, val);
3489}
3490
3491static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3492 unsigned int reg, unsigned int *val)
3493{
3494 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3495
3496 *val = codec->driver->read(codec, reg);
3497
3498 return 0;
3499}
3500
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003501static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3502 int clk_id, int source, unsigned int freq, int dir)
3503{
3504 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3505
3506 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3507}
3508
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003509static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3510 int pll_id, int source, unsigned int freq_in,
3511 unsigned int freq_out)
3512{
3513 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3514
3515 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3516}
3517
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003518static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3519 struct snd_soc_jack *jack, void *data)
3520{
3521 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3522
3523 return snd_soc_codec_set_jack(codec, jack, data);
3524}
3525
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003526static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3527 enum snd_soc_bias_level level)
3528{
3529 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3530
3531 return codec->driver->set_bias_level(codec, level);
3532}
3533
Mark Brown0d0cf002008-12-10 14:32:45 +00003534/**
3535 * snd_soc_register_codec - Register a codec with the ASoC core
3536 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003537 * @dev: The parent device for this codec
3538 * @codec_drv: Codec driver
3539 * @dai_drv: The associated DAI driver
3540 * @num_dai: Number of DAIs
Mark Brown0d0cf002008-12-10 14:32:45 +00003541 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003542int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003543 const struct snd_soc_codec_driver *codec_drv,
3544 struct snd_soc_dai_driver *dai_drv,
3545 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003546{
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003547 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003549 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003550 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003554 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3555 if (codec == NULL)
3556 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003557
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003558 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003559
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003560 ret = snd_soc_component_initialize(&codec->component,
3561 &codec_drv->component_driver, dev);
3562 if (ret)
3563 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003564
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003565 if (codec_drv->probe)
3566 codec->component.probe = snd_soc_codec_drv_probe;
3567 if (codec_drv->remove)
3568 codec->component.remove = snd_soc_codec_drv_remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003569 if (codec_drv->suspend)
3570 codec->component.suspend = snd_soc_codec_drv_suspend;
3571 if (codec_drv->resume)
3572 codec->component.resume = snd_soc_codec_drv_resume;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003573 if (codec_drv->write)
3574 codec->component.write = snd_soc_codec_drv_write;
3575 if (codec_drv->read)
3576 codec->component.read = snd_soc_codec_drv_read;
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003577 if (codec_drv->set_sysclk)
3578 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003579 if (codec_drv->set_pll)
3580 codec->component.set_pll = snd_soc_codec_set_pll_;
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003581 if (codec_drv->set_jack)
3582 codec->component.set_jack = snd_soc_codec_set_jack_;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003583 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003584
3585 dapm = snd_soc_codec_get_dapm(codec);
3586 dapm->idle_bias_off = codec_drv->idle_bias_off;
3587 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003588 if (codec_drv->seq_notifier)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003589 dapm->seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003590 if (codec_drv->set_bias_level)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003591 dapm->set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003592 codec->dev = dev;
3593 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003594 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003595
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003596#ifdef CONFIG_DEBUG_FS
3597 codec->component.init_debugfs = soc_init_codec_debugfs;
3598 codec->component.debugfs_prefix = "codec";
3599#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003600
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003601 if (codec_drv->get_regmap)
3602 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003604 for (i = 0; i < num_dai; i++) {
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003605 convert_endianness_formats(&dai_drv[i].playback);
3606 convert_endianness_formats(&dai_drv[i].capture);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003607 }
3608
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003609 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3610 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003611 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003612 goto err_cleanup;
3613 }
3614
3615 list_for_each_entry(dai, &codec->component.dai_list, list)
3616 dai->codec = codec;
3617
Mark Brown054880f2012-04-09 17:29:19 +01003618 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003619 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003620 list_add(&codec->list, &codec_list);
3621 mutex_unlock(&client_mutex);
3622
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003623 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3624 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003625 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003626
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003627err_cleanup:
3628 snd_soc_component_cleanup(&codec->component);
3629err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003630 kfree(codec);
3631 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003632}
3633EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3634
3635/**
3636 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3637 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003638 * @dev: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003639 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003640void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003641{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003642 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003643
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003644 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003645 list_for_each_entry(codec, &codec_list, list) {
3646 if (dev == codec->dev)
3647 goto found;
3648 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003649 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003650 return;
3651
3652found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003653 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003654 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003655 mutex_unlock(&client_mutex);
3656
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003657 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3658 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003660 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003661 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003662 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003663}
3664EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3665
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003666/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003667int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3668 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003669{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003670 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003671 int ret;
3672
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303673 if (!card->dev) {
3674 pr_err("card->dev is not set before calling %s\n", __func__);
3675 return -EINVAL;
3676 }
3677
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003678 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303679
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003680 ret = of_property_read_string_index(np, propname, 0, &card->name);
3681 /*
3682 * EINVAL means the property does not exist. This is fine providing
3683 * card->name was previously set, which is checked later in
3684 * snd_soc_register_card.
3685 */
3686 if (ret < 0 && ret != -EINVAL) {
3687 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003688 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003689 propname, ret);
3690 return ret;
3691 }
3692
3693 return 0;
3694}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003695EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003696
Xiubo Li9a6d4862014-02-08 15:59:52 +08003697static const struct snd_soc_dapm_widget simple_widgets[] = {
3698 SND_SOC_DAPM_MIC("Microphone", NULL),
3699 SND_SOC_DAPM_LINE("Line", NULL),
3700 SND_SOC_DAPM_HP("Headphone", NULL),
3701 SND_SOC_DAPM_SPK("Speaker", NULL),
3702};
3703
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003704int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003705 const char *propname)
3706{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003707 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003708 struct snd_soc_dapm_widget *widgets;
3709 const char *template, *wname;
3710 int i, j, num_widgets, ret;
3711
3712 num_widgets = of_property_count_strings(np, propname);
3713 if (num_widgets < 0) {
3714 dev_err(card->dev,
3715 "ASoC: Property '%s' does not exist\n", propname);
3716 return -EINVAL;
3717 }
3718 if (num_widgets & 1) {
3719 dev_err(card->dev,
3720 "ASoC: Property '%s' length is not even\n", propname);
3721 return -EINVAL;
3722 }
3723
3724 num_widgets /= 2;
3725 if (!num_widgets) {
3726 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3727 propname);
3728 return -EINVAL;
3729 }
3730
3731 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3732 GFP_KERNEL);
3733 if (!widgets) {
3734 dev_err(card->dev,
3735 "ASoC: Could not allocate memory for widgets\n");
3736 return -ENOMEM;
3737 }
3738
3739 for (i = 0; i < num_widgets; i++) {
3740 ret = of_property_read_string_index(np, propname,
3741 2 * i, &template);
3742 if (ret) {
3743 dev_err(card->dev,
3744 "ASoC: Property '%s' index %d read error:%d\n",
3745 propname, 2 * i, ret);
3746 return -EINVAL;
3747 }
3748
3749 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3750 if (!strncmp(template, simple_widgets[j].name,
3751 strlen(simple_widgets[j].name))) {
3752 widgets[i] = simple_widgets[j];
3753 break;
3754 }
3755 }
3756
3757 if (j >= ARRAY_SIZE(simple_widgets)) {
3758 dev_err(card->dev,
3759 "ASoC: DAPM widget '%s' is not supported\n",
3760 template);
3761 return -EINVAL;
3762 }
3763
3764 ret = of_property_read_string_index(np, propname,
3765 (2 * i) + 1,
3766 &wname);
3767 if (ret) {
3768 dev_err(card->dev,
3769 "ASoC: Property '%s' index %d read error:%d\n",
3770 propname, (2 * i) + 1, ret);
3771 return -EINVAL;
3772 }
3773
3774 widgets[i].name = wname;
3775 }
3776
Nicolin Chenf23e8602015-02-14 17:22:49 -08003777 card->of_dapm_widgets = widgets;
3778 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003779
3780 return 0;
3781}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003782EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08003783
Jyri Sarha61310842015-09-09 21:27:43 +03003784static int snd_soc_of_get_slot_mask(struct device_node *np,
3785 const char *prop_name,
3786 unsigned int *mask)
3787{
3788 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03003789 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03003790 int i;
3791
3792 if (!of_slot_mask)
3793 return 0;
3794 val /= sizeof(u32);
3795 for (i = 0; i < val; i++)
3796 if (be32_to_cpup(&of_slot_mask[i]))
3797 *mask |= (1 << i);
3798
3799 return val;
3800}
3801
Xiubo Li89c67852014-02-14 09:34:35 +08003802int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03003803 unsigned int *tx_mask,
3804 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08003805 unsigned int *slots,
3806 unsigned int *slot_width)
3807{
3808 u32 val;
3809 int ret;
3810
Jyri Sarha61310842015-09-09 21:27:43 +03003811 if (tx_mask)
3812 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3813 if (rx_mask)
3814 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3815
Xiubo Li89c67852014-02-14 09:34:35 +08003816 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3817 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3818 if (ret)
3819 return ret;
3820
3821 if (slots)
3822 *slots = val;
3823 }
3824
3825 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3826 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3827 if (ret)
3828 return ret;
3829
3830 if (slot_width)
3831 *slot_width = val;
3832 }
3833
3834 return 0;
3835}
3836EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3837
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003838void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003839 struct snd_soc_codec_conf *codec_conf,
3840 struct device_node *of_node,
3841 const char *propname)
3842{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003843 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003844 const char *str;
3845 int ret;
3846
3847 ret = of_property_read_string(np, propname, &str);
3848 if (ret < 0) {
3849 /* no prefix is not error */
3850 return;
3851 }
3852
3853 codec_conf->of_node = of_node;
3854 codec_conf->name_prefix = str;
3855}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00003856EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00003857
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003858int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003859 const char *propname)
3860{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003861 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003862 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003863 struct snd_soc_dapm_route *routes;
3864 int i, ret;
3865
3866 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003867 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003868 dev_err(card->dev,
3869 "ASoC: Property '%s' does not exist or its length is not even\n",
3870 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003871 return -EINVAL;
3872 }
3873 num_routes /= 2;
3874 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003875 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003876 propname);
3877 return -EINVAL;
3878 }
3879
Mark Browne3b1e6a2014-12-18 11:46:38 +00003880 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003881 GFP_KERNEL);
3882 if (!routes) {
3883 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003884 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003885 return -EINVAL;
3886 }
3887
3888 for (i = 0; i < num_routes; i++) {
3889 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003890 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003891 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003892 dev_err(card->dev,
3893 "ASoC: Property '%s' index %d could not be read: %d\n",
3894 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003895 return -EINVAL;
3896 }
3897 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003898 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003899 if (ret) {
3900 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003901 "ASoC: Property '%s' index %d could not be read: %d\n",
3902 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003903 return -EINVAL;
3904 }
3905 }
3906
Nicolin Chenf23e8602015-02-14 17:22:49 -08003907 card->num_of_dapm_routes = num_routes;
3908 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003909
3910 return 0;
3911}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00003912EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003913
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003914unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003915 const char *prefix,
3916 struct device_node **bitclkmaster,
3917 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003918{
3919 int ret, i;
3920 char prop[128];
3921 unsigned int format = 0;
3922 int bit, frame;
3923 const char *str;
3924 struct {
3925 char *name;
3926 unsigned int val;
3927 } of_fmt_table[] = {
3928 { "i2s", SND_SOC_DAIFMT_I2S },
3929 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3930 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3931 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3932 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3933 { "ac97", SND_SOC_DAIFMT_AC97 },
3934 { "pdm", SND_SOC_DAIFMT_PDM},
3935 { "msb", SND_SOC_DAIFMT_MSB },
3936 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003937 };
3938
3939 if (!prefix)
3940 prefix = "";
3941
3942 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003943 * check "dai-format = xxx"
3944 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003945 * SND_SOC_DAIFMT_FORMAT_MASK area
3946 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00003947 ret = of_property_read_string(np, "dai-format", &str);
3948 if (ret < 0) {
3949 snprintf(prop, sizeof(prop), "%sformat", prefix);
3950 ret = of_property_read_string(np, prop, &str);
3951 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003952 if (ret == 0) {
3953 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3954 if (strcmp(str, of_fmt_table[i].name) == 0) {
3955 format |= of_fmt_table[i].val;
3956 break;
3957 }
3958 }
3959 }
3960
3961 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003962 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003963 * SND_SOC_DAIFMT_CLOCK_MASK area
3964 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003965 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02003966 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003967 format |= SND_SOC_DAIFMT_CONT;
3968 else
3969 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003970
3971 /*
3972 * check "[prefix]bitclock-inversion"
3973 * check "[prefix]frame-inversion"
3974 * SND_SOC_DAIFMT_INV_MASK area
3975 */
3976 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3977 bit = !!of_get_property(np, prop, NULL);
3978
3979 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3980 frame = !!of_get_property(np, prop, NULL);
3981
3982 switch ((bit << 4) + frame) {
3983 case 0x11:
3984 format |= SND_SOC_DAIFMT_IB_IF;
3985 break;
3986 case 0x10:
3987 format |= SND_SOC_DAIFMT_IB_NF;
3988 break;
3989 case 0x01:
3990 format |= SND_SOC_DAIFMT_NB_IF;
3991 break;
3992 default:
3993 /* SND_SOC_DAIFMT_NB_NF is default */
3994 break;
3995 }
3996
3997 /*
3998 * check "[prefix]bitclock-master"
3999 * check "[prefix]frame-master"
4000 * SND_SOC_DAIFMT_MASTER_MASK area
4001 */
4002 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4003 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004004 if (bit && bitclkmaster)
4005 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004006
4007 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4008 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004009 if (frame && framemaster)
4010 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004011
4012 switch ((bit << 4) + frame) {
4013 case 0x11:
4014 format |= SND_SOC_DAIFMT_CBM_CFM;
4015 break;
4016 case 0x10:
4017 format |= SND_SOC_DAIFMT_CBM_CFS;
4018 break;
4019 case 0x01:
4020 format |= SND_SOC_DAIFMT_CBS_CFM;
4021 break;
4022 default:
4023 format |= SND_SOC_DAIFMT_CBS_CFS;
4024 break;
4025 }
4026
4027 return format;
4028}
4029EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4030
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00004031int snd_soc_get_dai_id(struct device_node *ep)
4032{
4033 struct snd_soc_component *pos;
4034 struct device_node *node;
4035 int ret;
4036
4037 node = of_graph_get_port_parent(ep);
4038
4039 /*
4040 * For example HDMI case, HDMI has video/sound port,
4041 * but ALSA SoC needs sound port number only.
4042 * Thus counting HDMI DT port/endpoint doesn't work.
4043 * Then, it should have .of_xlate_dai_id
4044 */
4045 ret = -ENOTSUPP;
4046 mutex_lock(&client_mutex);
4047 list_for_each_entry(pos, &component_list, list) {
4048 struct device_node *component_of_node = pos->dev->of_node;
4049
4050 if (!component_of_node && pos->dev->parent)
4051 component_of_node = pos->dev->parent->of_node;
4052
4053 if (component_of_node != node)
4054 continue;
4055
4056 if (pos->driver->of_xlate_dai_id)
4057 ret = pos->driver->of_xlate_dai_id(pos, ep);
4058
4059 break;
4060 }
4061 mutex_unlock(&client_mutex);
4062
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07004063 of_node_put(node);
4064
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00004065 return ret;
4066}
4067EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4068
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004069int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004070 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004071{
4072 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004073 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004074 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004075
4076 mutex_lock(&client_mutex);
4077 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004078 component_of_node = pos->dev->of_node;
4079 if (!component_of_node && pos->dev->parent)
4080 component_of_node = pos->dev->parent->of_node;
4081
4082 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004083 continue;
4084
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004085 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004086 ret = pos->driver->of_xlate_dai_name(pos,
4087 args,
4088 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004089 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00004090 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004091 int id = -1;
4092
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004093 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004094 case 0:
4095 id = 0; /* same as dai_drv[0] */
4096 break;
4097 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004098 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004099 break;
4100 default:
4101 /* not supported */
4102 break;
4103 }
4104
4105 if (id < 0 || id >= pos->num_dai) {
4106 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004107 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004108 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004109
4110 ret = 0;
4111
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00004112 /* find target DAI */
4113 list_for_each_entry(dai, &pos->dai_list, list) {
4114 if (id == 0)
4115 break;
4116 id--;
4117 }
4118
4119 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08004120 if (!*dai_name)
4121 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004122 }
4123
Kuninori Morimotocb470082013-09-10 17:39:56 -07004124 break;
4125 }
4126 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004127 return ret;
4128}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004129EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004130
4131int snd_soc_of_get_dai_name(struct device_node *of_node,
4132 const char **dai_name)
4133{
4134 struct of_phandle_args args;
4135 int ret;
4136
4137 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4138 "#sound-dai-cells", 0, &args);
4139 if (ret)
4140 return ret;
4141
4142 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07004143
4144 of_node_put(args.np);
4145
4146 return ret;
4147}
4148EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4149
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004150/*
Sylwester Nawrocki94685762018-03-09 18:48:54 +01004151 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
4152 * @dai_link: DAI link
4153 *
4154 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
4155 */
4156void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
4157{
4158 struct snd_soc_dai_link_component *component = dai_link->codecs;
4159 int index;
4160
4161 for (index = 0; index < dai_link->num_codecs; index++, component++) {
4162 if (!component->of_node)
4163 break;
4164 of_node_put(component->of_node);
4165 component->of_node = NULL;
4166 }
4167}
4168EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
4169
4170/*
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004171 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4172 * @dev: Card device
4173 * @of_node: Device node
4174 * @dai_link: DAI link
4175 *
4176 * Builds an array of CODEC DAI components from the DAI link property
4177 * 'sound-dai'.
4178 * The array is set in the DAI link and the number of DAIs is set accordingly.
Sylwester Nawrocki94685762018-03-09 18:48:54 +01004179 * The device nodes in the array (of_node) must be dereferenced by calling
4180 * snd_soc_of_put_dai_link_codecs() on @dai_link.
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004181 *
4182 * Returns 0 for success
4183 */
4184int snd_soc_of_get_dai_link_codecs(struct device *dev,
4185 struct device_node *of_node,
4186 struct snd_soc_dai_link *dai_link)
4187{
4188 struct of_phandle_args args;
4189 struct snd_soc_dai_link_component *component;
4190 char *name;
4191 int index, num_codecs, ret;
4192
4193 /* Count the number of CODECs */
4194 name = "sound-dai";
4195 num_codecs = of_count_phandle_with_args(of_node, name,
4196 "#sound-dai-cells");
4197 if (num_codecs <= 0) {
4198 if (num_codecs == -ENOENT)
4199 dev_err(dev, "No 'sound-dai' property\n");
4200 else
4201 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4202 return num_codecs;
4203 }
4204 component = devm_kzalloc(dev,
4205 sizeof *component * num_codecs,
4206 GFP_KERNEL);
4207 if (!component)
4208 return -ENOMEM;
4209 dai_link->codecs = component;
4210 dai_link->num_codecs = num_codecs;
4211
4212 /* Parse the list */
4213 for (index = 0, component = dai_link->codecs;
4214 index < dai_link->num_codecs;
4215 index++, component++) {
4216 ret = of_parse_phandle_with_args(of_node, name,
4217 "#sound-dai-cells",
4218 index, &args);
4219 if (ret)
4220 goto err;
4221 component->of_node = args.np;
4222 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4223 if (ret < 0)
4224 goto err;
4225 }
4226 return 0;
4227err:
Sylwester Nawrocki94685762018-03-09 18:48:54 +01004228 snd_soc_of_put_dai_link_codecs(dai_link);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004229 dai_link->codecs = NULL;
4230 dai_link->num_codecs = 0;
4231 return ret;
4232}
4233EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4234
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004235static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004236{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004237 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01004238 snd_soc_util_init();
4239
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004240 return platform_driver_register(&soc_driver);
4241}
Mark Brown4abe8e12010-10-12 17:41:03 +01004242module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004243
Mark Brown7d8c16a2008-11-30 22:11:24 +00004244static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004245{
Mark Brownfb257892011-04-28 10:57:54 +01004246 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004247 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01004248
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004249 platform_driver_unregister(&soc_driver);
4250}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004251module_exit(snd_soc_exit);
4252
4253/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004254MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004255MODULE_DESCRIPTION("ALSA SoC Core");
4256MODULE_LICENSE("GPL");
4257MODULE_ALIAS("platform:soc-audio");