blob: 8b1ef90f7b576577b98781f86f2f7d4b9fc83d80 [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 Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Mengdong Lin98faf432017-06-28 15:01:39 +080072/* If a DMI filed contain strings in this blacklist (e.g.
73 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
74 * as invalid and dropped when setting the card long name from DMI info.
75 */
76static const char * const dmi_blacklist[] = {
77 "To be filled by OEM",
78 "TBD by OEM",
79 "Default String",
80 "Board Manufacturer",
81 "Board Vendor Name",
82 "Board Product Name",
83 NULL, /* terminator */
84};
85
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000086/* returns the minimum number of bytes needed to represent
87 * a particular given value */
88static int min_bytes_needed(unsigned long val)
89{
90 int c = 0;
91 int i;
92
93 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
94 if (val & (1UL << i))
95 break;
96 c = (sizeof val * 8) - c;
97 if (!c || (c % 8))
98 c = (c + 8) / 8;
99 else
100 c /= 8;
101 return c;
102}
103
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000104/* fill buf which is 'len' bytes with a formatted
105 * string of the form 'reg: value\n' */
106static int format_register_str(struct snd_soc_codec *codec,
107 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000108{
Stephen Warren00b317a2011-04-01 14:50:44 -0600109 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
110 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000111 int ret;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000112
113 /* +2 for ': ' and + 1 for '\n' */
114 if (wordsize + regsize + 2 + 1 != len)
115 return -EINVAL;
116
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200117 sprintf(buf, "%.*x: ", wordsize, reg);
118 buf += wordsize + 2;
119
Mark Brown25032c12011-08-01 13:52:48 +0900120 ret = snd_soc_read(codec, reg);
Takashi Iwaid6b6c2c2015-05-26 14:40:14 +0200121 if (ret < 0)
122 memset(buf, 'X', regsize);
123 else
124 sprintf(buf, "%.*x", regsize, ret);
125 buf[regsize] = '\n';
126 /* no NUL-termination needed */
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000127 return 0;
128}
129
130/* codec register dump */
131static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
132 size_t count, loff_t pos)
133{
134 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000135 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000136 int len;
137 size_t total = 0;
138 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000139
Stephen Warren00b317a2011-04-01 14:50:44 -0600140 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
141 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000142
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000143 len = wordsize + regsize + 2 + 1;
144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000146 return 0;
147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->reg_cache_step)
149 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000150
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000151 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100159 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100160 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 }
162
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000167
Mark Brown2624d5f2009-11-03 21:56:13 +0000168static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
Mark Brown36ae1a92012-01-06 17:12:45 -0800171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000174}
175
Joe Perchesc828a892017-12-19 10:15:08 -0800176static DEVICE_ATTR_RO(codec_reg);
Mark Brown2624d5f2009-11-03 21:56:13 +0000177
Mark Browndbe21402010-02-12 11:37:24 +0000178static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
Mark Brown36ae1a92012-01-06 17:12:45 -0800181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000184}
185
186static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
189{
Mark Brown36ae1a92012-01-06 17:12:45 -0800190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700191 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000192
Jingoo Hanb785a492013-07-19 16:24:59 +0900193 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700194 if (ret)
195 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000196
197 return count;
198}
199
200static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201
Takashi Iwaid29697d2015-01-30 20:16:37 +0100202static struct attribute *soc_dev_attrs[] = {
203 &dev_attr_codec_reg.attr,
204 &dev_attr_pmdown_time.attr,
205 NULL
206};
207
208static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
209 struct attribute *attr, int idx)
210{
211 struct device *dev = kobj_to_dev(kobj);
212 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
213
214 if (attr == &dev_attr_pmdown_time.attr)
215 return attr->mode; /* always visible */
Kuninori Morimoto3b6eed8d2017-12-05 04:20:42 +0000216 return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
Takashi Iwaid29697d2015-01-30 20:16:37 +0100217}
218
219static const struct attribute_group soc_dapm_dev_group = {
220 .attrs = soc_dapm_dev_attrs,
221 .is_visible = soc_dev_attr_is_visible,
222};
223
224static const struct attribute_group soc_dev_roup = {
225 .attrs = soc_dev_attrs,
226 .is_visible = soc_dev_attr_is_visible,
227};
228
229static const struct attribute_group *soc_dev_attr_groups[] = {
230 &soc_dapm_dev_group,
231 &soc_dev_roup,
232 NULL
233};
234
Mark Brown2624d5f2009-11-03 21:56:13 +0000235#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000236static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000237 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000238{
239 ssize_t ret;
240 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000241 char *buf;
242
243 if (*ppos < 0 || !count)
244 return -EINVAL;
245
246 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 if (!buf)
248 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000249
250 ret = soc_codec_reg_show(codec, buf, count, *ppos);
251 if (ret >= 0) {
252 if (copy_to_user(user_buf, buf, ret)) {
253 kfree(buf);
254 return -EFAULT;
255 }
256 *ppos += ret;
257 }
258
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 kfree(buf);
260 return ret;
261}
262
263static ssize_t codec_reg_write_file(struct file *file,
264 const char __user *user_buf, size_t count, loff_t *ppos)
265{
266 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700267 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 char *start = buf;
269 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000270 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900271 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000272
273 buf_size = min(count, (sizeof(buf)-1));
274 if (copy_from_user(buf, user_buf, buf_size))
275 return -EFAULT;
276 buf[buf_size] = 0;
277
Mark Brown2624d5f2009-11-03 21:56:13 +0000278 while (*start == ' ')
279 start++;
280 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000281 while (*start == ' ')
282 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900283 ret = kstrtoul(start, 16, &value);
284 if (ret)
285 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000286
287 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030288 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000289
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000290 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000291 return buf_size;
292}
293
294static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700295 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000296 .read = codec_reg_read_file,
297 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200298 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000299};
300
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200301static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100302{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200303 if (!component->card->debugfs_card_root)
304 return;
305
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200306 if (component->debugfs_prefix) {
307 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100308
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200309 name = kasprintf(GFP_KERNEL, "%s:%s",
310 component->debugfs_prefix, component->name);
311 if (name) {
312 component->debugfs_root = debugfs_create_dir(name,
313 component->card->debugfs_card_root);
314 kfree(name);
315 }
316 } else {
317 component->debugfs_root = debugfs_create_dir(component->name,
318 component->card->debugfs_card_root);
319 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100320
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200321 if (!component->debugfs_root) {
322 dev_warn(component->dev,
323 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000324 return;
325 }
326
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200327 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
328 component->debugfs_root);
329
330 if (component->init_debugfs)
331 component->init_debugfs(component);
332}
333
334static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
335{
336 debugfs_remove_recursive(component->debugfs_root);
337}
338
339static void soc_init_codec_debugfs(struct snd_soc_component *component)
340{
341 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
Fabio Estevam353c64d2017-08-07 09:08:52 -0300342 struct dentry *debugfs_reg;
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200343
Fabio Estevam353c64d2017-08-07 09:08:52 -0300344 debugfs_reg = debugfs_create_file("codec_reg", 0644,
345 codec->component.debugfs_root,
346 codec, &codec_reg_fops);
347 if (!debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200348 dev_warn(codec->dev,
349 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000350}
351
Donglin Peng700c17c2018-01-18 13:31:26 +0800352static int codec_list_seq_show(struct seq_file *m, void *v)
Mark Brownc3c5a192010-09-15 18:15:14 +0100353{
Mark Brownc3c5a192010-09-15 18:15:14 +0100354 struct snd_soc_codec *codec;
355
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100356 mutex_lock(&client_mutex);
357
Donglin Peng700c17c2018-01-18 13:31:26 +0800358 list_for_each_entry(codec, &codec_list, list)
359 seq_printf(m, "%s\n", codec->component.name);
Mark Brownc3c5a192010-09-15 18:15:14 +0100360
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100361 mutex_unlock(&client_mutex);
362
Donglin Peng700c17c2018-01-18 13:31:26 +0800363 return 0;
364}
Mark Brownc3c5a192010-09-15 18:15:14 +0100365
Donglin Peng700c17c2018-01-18 13:31:26 +0800366static int codec_list_seq_open(struct inode *inode, struct file *file)
367{
368 return single_open(file, codec_list_seq_show, NULL);
Mark Brownc3c5a192010-09-15 18:15:14 +0100369}
370
371static const struct file_operations codec_list_fops = {
Donglin Peng700c17c2018-01-18 13:31:26 +0800372 .open = codec_list_seq_open,
373 .read = seq_read,
374 .llseek = seq_lseek,
375 .release = single_release,
Mark Brownc3c5a192010-09-15 18:15:14 +0100376};
377
Donglin Peng700c17c2018-01-18 13:31:26 +0800378static int dai_list_seq_show(struct seq_file *m, void *v)
Mark Brownf3208782010-09-15 18:19:07 +0100379{
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100380 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100381 struct snd_soc_dai *dai;
382
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100383 mutex_lock(&client_mutex);
384
Donglin Peng700c17c2018-01-18 13:31:26 +0800385 list_for_each_entry(component, &component_list, list)
386 list_for_each_entry(dai, &component->dai_list, list)
387 seq_printf(m, "%s\n", dai->name);
Mark Brownf3208782010-09-15 18:19:07 +0100388
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100389 mutex_unlock(&client_mutex);
390
Donglin Peng700c17c2018-01-18 13:31:26 +0800391 return 0;
392}
Mark Brownf3208782010-09-15 18:19:07 +0100393
Donglin Peng700c17c2018-01-18 13:31:26 +0800394static int dai_list_seq_open(struct inode *inode, struct file *file)
395{
396 return single_open(file, dai_list_seq_show, NULL);
Mark Brownf3208782010-09-15 18:19:07 +0100397}
398
399static const struct file_operations dai_list_fops = {
Donglin Peng700c17c2018-01-18 13:31:26 +0800400 .open = dai_list_seq_open,
401 .read = seq_read,
402 .llseek = seq_lseek,
403 .release = single_release,
Mark Brownf3208782010-09-15 18:19:07 +0100404};
405
Donglin Peng700c17c2018-01-18 13:31:26 +0800406static int platform_list_seq_show(struct seq_file *m, void *v)
Mark Brown19c7ac22010-09-15 18:22:40 +0100407{
Mark Brown19c7ac22010-09-15 18:22:40 +0100408 struct snd_soc_platform *platform;
409
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100410 mutex_lock(&client_mutex);
411
Donglin Peng700c17c2018-01-18 13:31:26 +0800412 list_for_each_entry(platform, &platform_list, list)
413 seq_printf(m, "%s\n", platform->component.name);
Mark Brown19c7ac22010-09-15 18:22:40 +0100414
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100415 mutex_unlock(&client_mutex);
416
Donglin Peng700c17c2018-01-18 13:31:26 +0800417 return 0;
418}
Mark Brown19c7ac22010-09-15 18:22:40 +0100419
Donglin Peng700c17c2018-01-18 13:31:26 +0800420static int platform_list_seq_open(struct inode *inode, struct file *file)
421{
422 return single_open(file, platform_list_seq_show, NULL);
Mark Brown19c7ac22010-09-15 18:22:40 +0100423}
424
425static const struct file_operations platform_list_fops = {
Donglin Peng700c17c2018-01-18 13:31:26 +0800426 .open = platform_list_seq_open,
427 .read = seq_read,
428 .llseek = seq_lseek,
429 .release = single_release,
Mark Brown19c7ac22010-09-15 18:22:40 +0100430};
431
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200432static void soc_init_card_debugfs(struct snd_soc_card *card)
433{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200434 if (!snd_soc_debugfs_root)
435 return;
436
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200437 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000438 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200439 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200440 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100441 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200442 return;
443 }
444
445 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
446 card->debugfs_card_root,
447 &card->pop_time);
448 if (!card->debugfs_pop_time)
449 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000450 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200451}
452
453static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
454{
455 debugfs_remove_recursive(card->debugfs_card_root);
456}
457
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200458static void snd_soc_debugfs_init(void)
459{
460 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
Fabio Estevamd9a02c52017-08-07 09:08:53 -0300461 if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) {
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200462 pr_warn("ASoC: Failed to create debugfs directory\n");
463 snd_soc_debugfs_root = NULL;
464 return;
465 }
466
467 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
468 &codec_list_fops))
469 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
470
471 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
472 &dai_list_fops))
473 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
474
475 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
476 &platform_list_fops))
477 pr_warn("ASoC: Failed to create platform list debugfs file\n");
478}
479
480static void snd_soc_debugfs_exit(void)
481{
482 debugfs_remove_recursive(snd_soc_debugfs_root);
483}
484
Mark Brown2624d5f2009-11-03 21:56:13 +0000485#else
486
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200487#define soc_init_codec_debugfs NULL
488
489static inline void soc_init_component_debugfs(
490 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000491{
492}
493
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200494static inline void soc_cleanup_component_debugfs(
495 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000496{
497}
498
Axel Linb95fccb2010-11-09 17:06:44 +0800499static inline void soc_init_card_debugfs(struct snd_soc_card *card)
500{
501}
502
503static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
504{
505}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200506
507static inline void snd_soc_debugfs_init(void)
508{
509}
510
511static inline void snd_soc_debugfs_exit(void)
512{
513}
514
Mark Brown2624d5f2009-11-03 21:56:13 +0000515#endif
516
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000517static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
518 struct snd_soc_component *component)
519{
520 struct snd_soc_rtdcom_list *rtdcom;
521 struct snd_soc_rtdcom_list *new_rtdcom;
522
523 for_each_rtdcom(rtd, rtdcom) {
524 /* already connected */
525 if (rtdcom->component == component)
526 return 0;
527 }
528
529 new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL);
530 if (!new_rtdcom)
531 return -ENOMEM;
532
533 new_rtdcom->component = component;
534 INIT_LIST_HEAD(&new_rtdcom->list);
535
536 list_add_tail(&new_rtdcom->list, &rtd->component_list);
537
538 return 0;
539}
540
541static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd)
542{
543 struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2;
544
545 for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2)
546 kfree(rtdcom1);
547
548 INIT_LIST_HEAD(&rtd->component_list);
549}
550
551struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
552 const char *driver_name)
553{
554 struct snd_soc_rtdcom_list *rtdcom;
555
Kuninori Morimoto971da242018-01-23 00:41:24 +0000556 if (!driver_name)
557 return NULL;
558
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000559 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimoto971da242018-01-23 00:41:24 +0000560 const char *component_name = rtdcom->component->driver->name;
561
562 if (!component_name)
563 continue;
564
565 if ((component_name == driver_name) ||
566 strcmp(component_name, driver_name) == 0)
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000567 return rtdcom->component;
568 }
569
570 return NULL;
571}
Kuninori Morimoto031734b2018-01-18 01:13:54 +0000572EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000573
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100574struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
575 const char *dai_link, int stream)
576{
Mengdong Lin1a497982015-11-18 02:34:11 -0500577 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100578
Mengdong Lin1a497982015-11-18 02:34:11 -0500579 list_for_each_entry(rtd, &card->rtd_list, list) {
580 if (rtd->dai_link->no_pcm &&
581 !strcmp(rtd->dai_link->name, dai_link))
582 return rtd->pcm->streams[stream].substream;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100583 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000584 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100585 return NULL;
586}
587EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
588
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000589static const struct snd_soc_ops null_snd_soc_ops;
590
Mengdong Lin1a497982015-11-18 02:34:11 -0500591static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
592 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
593{
594 struct snd_soc_pcm_runtime *rtd;
595
596 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
597 if (!rtd)
598 return NULL;
599
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000600 INIT_LIST_HEAD(&rtd->component_list);
Mengdong Lin1a497982015-11-18 02:34:11 -0500601 rtd->card = card;
602 rtd->dai_link = dai_link;
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000603 if (!rtd->dai_link->ops)
604 rtd->dai_link->ops = &null_snd_soc_ops;
605
Mengdong Lin1a497982015-11-18 02:34:11 -0500606 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
607 dai_link->num_codecs,
608 GFP_KERNEL);
609 if (!rtd->codec_dais) {
610 kfree(rtd);
611 return NULL;
612 }
613
614 return rtd;
615}
616
617static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
618{
Kuninori Morimotodb1721f2017-09-25 01:38:13 +0000619 kfree(rtd->codec_dais);
Kuninori Morimotoa0ac4412017-08-08 06:17:47 +0000620 snd_soc_rtdcom_del_all(rtd);
Mengdong Lin1a497982015-11-18 02:34:11 -0500621 kfree(rtd);
622}
623
624static void soc_add_pcm_runtime(struct snd_soc_card *card,
625 struct snd_soc_pcm_runtime *rtd)
626{
627 list_add_tail(&rtd->list, &card->rtd_list);
628 rtd->num = card->num_rtd;
629 card->num_rtd++;
630}
631
632static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
633{
634 struct snd_soc_pcm_runtime *rtd, *_rtd;
635
636 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
637 list_del(&rtd->list);
638 soc_free_pcm_runtime(rtd);
639 }
640
641 card->num_rtd = 0;
642}
643
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100644struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
645 const char *dai_link)
646{
Mengdong Lin1a497982015-11-18 02:34:11 -0500647 struct snd_soc_pcm_runtime *rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100648
Mengdong Lin1a497982015-11-18 02:34:11 -0500649 list_for_each_entry(rtd, &card->rtd_list, list) {
650 if (!strcmp(rtd->dai_link->name, dai_link))
651 return rtd;
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100652 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000653 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100654 return NULL;
655}
656EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
657
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100658static void codec2codec_close_delayed_work(struct work_struct *work)
659{
660 /* Currently nothing to do for c2c links
661 * Since c2c links are internal nodes in the DAPM graph and
662 * don't interface with the outside world or application layer
663 * we don't have to do any special handling on close.
664 */
665}
666
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000667#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000669int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000671 struct snd_soc_card *card = dev_get_drvdata(dev);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000672 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500673 struct snd_soc_pcm_runtime *rtd;
674 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200676 /* If the card is not initialized yet there is nothing to do */
677 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200678 return 0;
679
Andy Green6ed25972008-06-13 16:24:05 +0100680 /* Due to the resume being scheduled into a workqueue we could
681 * suspend before that's finished - wait for it to complete.
682 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100684
685 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100687
Mark Browna00f90f2010-12-02 16:24:24 +0000688 /* mute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500689 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100690
Mengdong Lin1a497982015-11-18 02:34:11 -0500691 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100692 continue;
693
Mengdong Lin1a497982015-11-18 02:34:11 -0500694 for (i = 0; i < rtd->num_codecs; i++) {
695 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200696 struct snd_soc_dai_driver *drv = dai->driver;
697
698 if (drv->ops->digital_mute && dai->playback_active)
699 drv->ops->digital_mute(dai, 1);
700 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 }
702
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100703 /* suspend all pcms */
Mengdong Lin1a497982015-11-18 02:34:11 -0500704 list_for_each_entry(rtd, &card->rtd_list, list) {
705 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100706 continue;
707
Mengdong Lin1a497982015-11-18 02:34:11 -0500708 snd_pcm_suspend_all(rtd->pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100709 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100710
Mark Brown87506542008-11-18 20:50:34 +0000711 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000712 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713
Mengdong Lin1a497982015-11-18 02:34:11 -0500714 list_for_each_entry(rtd, &card->rtd_list, list) {
715 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100716
Mengdong Lin1a497982015-11-18 02:34:11 -0500717 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100718 continue;
719
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100720 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100722 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200724 /* close any waiting streams */
Mengdong Lin1a497982015-11-18 02:34:11 -0500725 list_for_each_entry(rtd, &card->rtd_list, list)
726 flush_delayed_work(&rtd->delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100727
Mengdong Lin1a497982015-11-18 02:34:11 -0500728 list_for_each_entry(rtd, &card->rtd_list, list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729
Mengdong Lin1a497982015-11-18 02:34:11 -0500730 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100731 continue;
732
Mengdong Lin1a497982015-11-18 02:34:11 -0500733 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800734 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800735 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736
Mengdong Lin1a497982015-11-18 02:34:11 -0500737 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800738 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800739 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 }
741
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200742 /* Recheck all endpoints too, their state is affected by suspend */
743 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700744 snd_soc_dapm_sync(&card->dapm);
745
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000746 /* suspend all COMPONENTs */
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000747 list_for_each_entry(component, &card->component_dev_list, card_list) {
748 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000749
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000750 /* If there are paths active then the COMPONENT will be held with
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 * bias _ON and should not be suspended. */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000752 if (!component->suspended) {
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200753 switch (snd_soc_dapm_get_bias_level(dapm)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000755 /*
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000756 * If the COMPONENT is capable of idle
Mark Brown125a25d2012-01-31 15:49:10 +0000757 * bias off then being in STANDBY
758 * means it's doing something,
759 * otherwise fall through.
760 */
Lars-Peter Clausen48901402015-07-06 15:38:11 +0200761 if (dapm->idle_bias_off) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000762 dev_dbg(component->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200763 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000764 break;
765 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 case SND_SOC_BIAS_OFF:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000768 if (component->suspend)
769 component->suspend(component);
770 component->suspended = 1;
771 if (component->regmap)
772 regcache_mark_dirty(component->regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800773 /* deactivate pins to sleep state */
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000774 pinctrl_pm_select_sleep_state(component->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 break;
776 default:
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000777 dev_dbg(component->dev,
778 "ASoC: COMPONENT is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 break;
780 }
781 }
782 }
783
Mengdong Lin1a497982015-11-18 02:34:11 -0500784 list_for_each_entry(rtd, &card->rtd_list, list) {
785 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786
Mengdong Lin1a497982015-11-18 02:34:11 -0500787 if (rtd->dai_link->ignore_suspend)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788 continue;
789
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100790 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000791 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800792
793 /* deactivate pins to sleep state */
794 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795 }
796
Mark Brown87506542008-11-18 20:50:34 +0000797 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000798 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799
800 return 0;
801}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000802EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803
Andy Green6ed25972008-06-13 16:24:05 +0100804/* deferred resume work, so resume can complete before we finished
805 * setting our codec back up, which can be very slow on I2C
806 */
807static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200808{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 struct snd_soc_card *card =
810 container_of(work, struct snd_soc_card, deferred_resume_work);
Mengdong Lin1a497982015-11-18 02:34:11 -0500811 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000812 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -0500813 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814
Andy Green6ed25972008-06-13 16:24:05 +0100815 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
816 * so userspace apps are blocked from touching us
817 */
818
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000819 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100820
Mark Brown9949788b2010-05-07 20:24:05 +0100821 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100823
Mark Brown87506542008-11-18 20:50:34 +0000824 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000825 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200826
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100827 /* resume control bus DAIs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500828 list_for_each_entry(rtd, &card->rtd_list, list) {
829 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100830
Mengdong Lin1a497982015-11-18 02:34:11 -0500831 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100832 continue;
833
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100834 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200836 }
837
Kuninori Morimotod9fc4062016-11-30 06:22:36 +0000838 list_for_each_entry(component, &card->component_dev_list, card_list) {
Kuninori Morimoto9178feb2016-11-30 06:23:13 +0000839 if (component->suspended) {
840 if (component->resume)
841 component->resume(component);
842 component->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100843 }
844 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845
Mengdong Lin1a497982015-11-18 02:34:11 -0500846 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100847
Mengdong Lin1a497982015-11-18 02:34:11 -0500848 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100849 continue;
850
Mengdong Lin1a497982015-11-18 02:34:11 -0500851 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000852 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800853 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000854
Mengdong Lin1a497982015-11-18 02:34:11 -0500855 snd_soc_dapm_stream_event(rtd,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000856 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800857 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858 }
859
Mark Brown3ff3f642008-05-19 12:32:25 +0200860 /* unmute any active DACs */
Mengdong Lin1a497982015-11-18 02:34:11 -0500861 list_for_each_entry(rtd, &card->rtd_list, list) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100862
Mengdong Lin1a497982015-11-18 02:34:11 -0500863 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100864 continue;
865
Mengdong Lin1a497982015-11-18 02:34:11 -0500866 for (i = 0; i < rtd->num_codecs; i++) {
867 struct snd_soc_dai *dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200868 struct snd_soc_dai_driver *drv = dai->driver;
869
870 if (drv->ops->digital_mute && dai->playback_active)
871 drv->ops->digital_mute(dai, 0);
872 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200873 }
874
Mengdong Lin1a497982015-11-18 02:34:11 -0500875 list_for_each_entry(rtd, &card->rtd_list, list) {
876 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100877
Mengdong Lin1a497982015-11-18 02:34:11 -0500878 if (rtd->dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100879 continue;
880
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100881 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200883 }
884
Mark Brown87506542008-11-18 20:50:34 +0000885 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000886 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000888 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100889
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200890 /* Recheck all endpoints too, their state is affected by suspend */
891 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700892 snd_soc_dapm_sync(&card->dapm);
Jeeja KP1a7aaa52015-11-23 21:22:31 +0530893
894 /* userspace can access us now we are back as we were before */
895 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100896}
897
898/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000899int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100900{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000901 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100902 bool bus_control = false;
Mengdong Lin1a497982015-11-18 02:34:11 -0500903 struct snd_soc_pcm_runtime *rtd;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200904
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200905 /* If the card is not initialized yet there is nothing to do */
906 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800907 return 0;
908
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800909 /* activate pins from sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -0500910 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200911 struct snd_soc_dai **codec_dais = rtd->codec_dais;
912 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
913 int j;
914
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800915 if (cpu_dai->active)
916 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200917
918 for (j = 0; j < rtd->num_codecs; j++) {
919 struct snd_soc_dai *codec_dai = codec_dais[j];
920 if (codec_dai->active)
921 pinctrl_pm_select_default_state(codec_dai->dev);
922 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800923 }
924
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100925 /*
926 * DAIs that also act as the control bus master might have other drivers
927 * hanging off them so need to resume immediately. Other drivers don't
928 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100929 * due to I/O costs and anti-pop so handle them out of line.
930 */
Mengdong Lin1a497982015-11-18 02:34:11 -0500931 list_for_each_entry(rtd, &card->rtd_list, list) {
932 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100933 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600934 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100935 if (bus_control) {
936 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600937 soc_resume_deferred(&card->deferred_resume_work);
938 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000939 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600940 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000941 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100942 }
Andy Green6ed25972008-06-13 16:24:05 +0100943
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200944 return 0;
945}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000946EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200947#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000948#define snd_soc_suspend NULL
949#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950#endif
951
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100952static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800953};
954
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200955static struct snd_soc_component *soc_find_component(
956 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100957{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200958 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100959
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100960 lockdep_assert_held(&client_mutex);
961
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200962 list_for_each_entry(component, &component_list, list) {
963 if (of_node) {
964 if (component->dev->of_node == of_node)
965 return component;
966 } else if (strcmp(component->name, name) == 0) {
967 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100968 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100969 }
970
971 return NULL;
972}
973
Mengdong Linfbb88b52016-04-22 12:25:33 +0800974/**
975 * snd_soc_find_dai - Find a registered DAI
976 *
Jeffy Chen49584712017-08-22 15:57:21 +0800977 * @dlc: name of the DAI or the DAI driver and optional component info to match
Mengdong Linfbb88b52016-04-22 12:25:33 +0800978 *
Stephen Boydad61dd32017-05-08 15:57:50 -0700979 * This function will search all registered components and their DAIs to
Mengdong Linfbb88b52016-04-22 12:25:33 +0800980 * find the DAI of the same name. The component's of_node and name
981 * should also match if being specified.
982 *
983 * Return: pointer of DAI, or NULL if not found.
984 */
Mengdong Lin305e9022016-04-19 13:12:25 +0800985struct snd_soc_dai *snd_soc_find_dai(
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200986 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100987{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200988 struct snd_soc_component *component;
989 struct snd_soc_dai *dai;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300990 struct device_node *component_of_node;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100991
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100992 lockdep_assert_held(&client_mutex);
993
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200994 /* Find CPU DAI from registered DAIs*/
995 list_for_each_entry(component, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +0300996 component_of_node = component->dev->of_node;
997 if (!component_of_node && component->dev->parent)
998 component_of_node = component->dev->parent->of_node;
999
1000 if (dlc->of_node && component_of_node != dlc->of_node)
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001001 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +01001002 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001003 continue;
1004 list_for_each_entry(dai, &component->dai_list, list) {
Jeffy Chen49584712017-08-22 15:57:21 +08001005 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
Jeffy Chen6a6dafd2017-08-24 12:40:17 +08001006 && (!dai->driver->name
1007 || strcmp(dai->driver->name, dlc->dai_name)))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001008 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001009
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001010 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001011 }
1012 }
1013
1014 return NULL;
1015}
Mengdong Lin305e9022016-04-19 13:12:25 +08001016EXPORT_SYMBOL_GPL(snd_soc_find_dai);
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001017
Mengdong Lin17fb17552016-11-03 01:04:12 +08001018
1019/**
1020 * snd_soc_find_dai_link - Find a DAI link
1021 *
1022 * @card: soc card
1023 * @id: DAI link ID to match
1024 * @name: DAI link name to match, optional
Charles Keepax8abab352017-01-12 11:38:15 +00001025 * @stream_name: DAI link stream name to match, optional
Mengdong Lin17fb17552016-11-03 01:04:12 +08001026 *
1027 * This function will search all existing DAI links of the soc card to
1028 * find the link of the same ID. Since DAI links may not have their
1029 * unique ID, so name and stream name should also match if being
1030 * specified.
1031 *
1032 * Return: pointer of DAI link, or NULL if not found.
1033 */
1034struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1035 int id, const char *name,
1036 const char *stream_name)
1037{
1038 struct snd_soc_dai_link *link, *_link;
1039
1040 lockdep_assert_held(&client_mutex);
1041
1042 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1043 if (link->id != id)
1044 continue;
1045
1046 if (name && (!link->name || strcmp(name, link->name)))
1047 continue;
1048
1049 if (stream_name && (!link->stream_name
1050 || strcmp(stream_name, link->stream_name)))
1051 continue;
1052
1053 return link;
1054 }
1055
1056 return NULL;
1057}
1058EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1059
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001060static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1061 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001062{
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001063 struct snd_soc_pcm_runtime *rtd;
1064
1065 list_for_each_entry(rtd, &card->rtd_list, list) {
1066 if (rtd->dai_link == dai_link)
1067 return true;
1068 }
1069
1070 return false;
1071}
1072
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001073static int soc_bind_dai_link(struct snd_soc_card *card,
1074 struct snd_soc_dai_link *dai_link)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001075{
Mengdong Lin1a497982015-11-18 02:34:11 -05001076 struct snd_soc_pcm_runtime *rtd;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001077 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001078 struct snd_soc_dai_link_component cpu_dai_component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001079 struct snd_soc_component *component;
Mengdong Lin1a497982015-11-18 02:34:11 -05001080 struct snd_soc_dai **codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +00001081 struct snd_soc_platform *platform;
Charles Keepax06859fc2016-11-07 13:03:17 +00001082 struct device_node *platform_of_node;
Mark Brown848dd8b2011-04-27 18:16:32 +01001083 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001084 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001085
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05001086 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
Mark Brown63084192008-12-02 15:08:03 +00001087
Mengdong Lin49a5ba12015-12-02 14:11:40 +08001088 if (soc_is_dai_link_bound(card, dai_link)) {
1089 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1090 dai_link->name);
1091 return 0;
1092 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093
Sudip Mukherjee513cb3112016-02-22 14:14:31 +05301094 rtd = soc_new_pcm_runtime(card, dai_link);
1095 if (!rtd)
1096 return -ENOMEM;
1097
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001098 cpu_dai_component.name = dai_link->cpu_name;
1099 cpu_dai_component.of_node = dai_link->cpu_of_node;
1100 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1101 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001102 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001103 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001104 dai_link->cpu_dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001105 goto _err_defer;
Mark Brownc5af3a22008-11-28 13:29:45 +00001106 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001107 snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component);
Mark Brownc5af3a22008-11-28 13:29:45 +00001108
Benoit Cousson88bd8702014-07-08 23:19:34 +02001109 rtd->num_codecs = dai_link->num_codecs;
1110
1111 /* Find CODEC from registered CODECs */
Mengdong Lin1a497982015-11-18 02:34:11 -05001112 codec_dais = rtd->codec_dais;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001113 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +02001114 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001115 if (!codec_dais[i]) {
1116 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1117 codecs[i].dai_name);
Mengdong Lin1a497982015-11-18 02:34:11 -05001118 goto _err_defer;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001119 }
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001120 snd_soc_rtdcom_add(rtd, codec_dais[i]->component);
Mark Brownb19e6e72012-03-14 21:18:39 +00001121 }
Mark Brown848dd8b2011-04-27 18:16:32 +01001122
Benoit Cousson88bd8702014-07-08 23:19:34 +02001123 /* Single codec links expect codec and codec_dai in runtime data */
1124 rtd->codec_dai = codec_dais[0];
1125 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +01001126
Mark Brown848dd8b2011-04-27 18:16:32 +01001127 /* if there's no platform we match on the empty platform */
1128 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -07001129 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +01001130 platform_name = "snd-soc-dummy";
1131
Mark Brownb19e6e72012-03-14 21:18:39 +00001132 /* find one from the set of registered platforms */
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001133 list_for_each_entry(component, &component_list, list) {
1134 platform_of_node = component->dev->of_node;
1135 if (!platform_of_node && component->dev->parent->of_node)
1136 platform_of_node = component->dev->parent->of_node;
1137
1138 if (dai_link->platform_of_node) {
1139 if (platform_of_node != dai_link->platform_of_node)
1140 continue;
1141 } else {
1142 if (strcmp(component->name, platform_name))
1143 continue;
1144 }
1145
1146 snd_soc_rtdcom_add(rtd, component);
1147 }
1148
1149 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001150 list_for_each_entry(platform, &platform_list, list) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001151 platform_of_node = platform->dev->of_node;
1152 if (!platform_of_node && platform->dev->parent->of_node)
1153 platform_of_node = platform->dev->parent->of_node;
1154
Stephen Warren5a504962011-12-21 10:40:59 -07001155 if (dai_link->platform_of_node) {
Charles Keepax06859fc2016-11-07 13:03:17 +00001156 if (platform_of_node != dai_link->platform_of_node)
Stephen Warren5a504962011-12-21 10:40:59 -07001157 continue;
1158 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001159 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -07001160 continue;
1161 }
Stephen Warren2610ab72011-12-07 13:58:27 -07001162
1163 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001164 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001165
Mengdong Lin1a497982015-11-18 02:34:11 -05001166 soc_add_pcm_runtime(card, rtd);
Mark Brownb19e6e72012-03-14 21:18:39 +00001167 return 0;
Mengdong Lin1a497982015-11-18 02:34:11 -05001168
1169_err_defer:
1170 soc_free_pcm_runtime(rtd);
1171 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172}
1173
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001174static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001175{
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001176 if (!component->card)
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001177 return;
Stephen Warrend12cd192012-06-08 12:34:22 -06001178
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001179 list_del(&component->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001180
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001181 if (component->remove)
1182 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001183
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001184 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001185
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001186 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001187 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001188 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -06001189}
1190
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001191static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001192{
1193 int err;
1194
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001195 if (dai && dai->probed &&
1196 dai->driver->remove_order == order) {
1197 if (dai->driver->remove) {
1198 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001199 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001200 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001201 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001202 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001203 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001204 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001205 }
1206}
1207
Mengdong Lin1a497982015-11-18 02:34:11 -05001208static void soc_remove_link_dais(struct snd_soc_card *card,
1209 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001210{
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001211 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212
1213 /* unregister the rtd device */
1214 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001215 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001216 rtd->dev_registered = 0;
1217 }
1218
1219 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001220 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001221 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001223 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224}
1225
Mengdong Lin1a497982015-11-18 02:34:11 -05001226static void soc_remove_link_components(struct snd_soc_card *card,
1227 struct snd_soc_pcm_runtime *rtd, int order)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001228{
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001229 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001230 struct snd_soc_rtdcom_list *rtdcom;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001231
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001232 for_each_rtdcom(rtd, rtdcom) {
1233 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001234
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001235 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001236 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001237 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001238}
1239
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001240static void soc_remove_dai_links(struct snd_soc_card *card)
1241{
Mengdong Lin1a497982015-11-18 02:34:11 -05001242 int order;
1243 struct snd_soc_pcm_runtime *rtd;
Mengdong Linf8f80362015-12-02 14:11:22 +08001244 struct snd_soc_dai_link *link, *_link;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001245
Liam Girdwood0168bf02011-06-07 16:08:05 +01001246 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1247 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001248 list_for_each_entry(rtd, &card->rtd_list, list)
1249 soc_remove_link_dais(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001250 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001251
1252 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1253 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001254 list_for_each_entry(rtd, &card->rtd_list, list)
1255 soc_remove_link_components(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001256 }
1257
Mengdong Linf8f80362015-12-02 14:11:22 +08001258 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1259 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1260 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1261 link->name);
1262
1263 list_del(&link->list);
1264 card->num_dai_links--;
1265 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001266}
1267
Mengdong Lin923c5e612015-11-23 11:01:49 -05001268static int snd_soc_init_multicodec(struct snd_soc_card *card,
1269 struct snd_soc_dai_link *dai_link)
1270{
1271 /* Legacy codec/codec_dai link is a single entry in multicodec */
1272 if (dai_link->codec_name || dai_link->codec_of_node ||
1273 dai_link->codec_dai_name) {
1274 dai_link->num_codecs = 1;
1275
1276 dai_link->codecs = devm_kzalloc(card->dev,
1277 sizeof(struct snd_soc_dai_link_component),
1278 GFP_KERNEL);
1279 if (!dai_link->codecs)
1280 return -ENOMEM;
1281
1282 dai_link->codecs[0].name = dai_link->codec_name;
1283 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1284 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1285 }
1286
1287 if (!dai_link->codecs) {
1288 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1289 return -EINVAL;
1290 }
1291
1292 return 0;
1293}
1294
1295static int soc_init_dai_link(struct snd_soc_card *card,
1296 struct snd_soc_dai_link *link)
1297{
1298 int i, ret;
1299
1300 ret = snd_soc_init_multicodec(card, link);
1301 if (ret) {
1302 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1303 return ret;
1304 }
1305
1306 for (i = 0; i < link->num_codecs; i++) {
1307 /*
1308 * Codec must be specified by 1 of name or OF node,
1309 * not both or neither.
1310 */
1311 if (!!link->codecs[i].name ==
1312 !!link->codecs[i].of_node) {
1313 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1314 link->name);
1315 return -EINVAL;
1316 }
1317 /* Codec DAI name must be specified */
1318 if (!link->codecs[i].dai_name) {
1319 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1320 link->name);
1321 return -EINVAL;
1322 }
1323 }
1324
1325 /*
1326 * Platform may be specified by either name or OF node, but
1327 * can be left unspecified, and a dummy platform will be used.
1328 */
1329 if (link->platform_name && link->platform_of_node) {
1330 dev_err(card->dev,
1331 "ASoC: Both platform name/of_node are set for %s\n",
1332 link->name);
1333 return -EINVAL;
1334 }
1335
1336 /*
1337 * CPU device may be specified by either name or OF node, but
1338 * can be left unspecified, and will be matched based on DAI
1339 * name alone..
1340 */
1341 if (link->cpu_name && link->cpu_of_node) {
1342 dev_err(card->dev,
1343 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1344 link->name);
1345 return -EINVAL;
1346 }
1347 /*
1348 * At least one of CPU DAI name or CPU device name/node must be
1349 * specified
1350 */
1351 if (!link->cpu_dai_name &&
1352 !(link->cpu_name || link->cpu_of_node)) {
1353 dev_err(card->dev,
1354 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1355 link->name);
1356 return -EINVAL;
1357 }
1358
1359 return 0;
1360}
1361
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001362void snd_soc_disconnect_sync(struct device *dev)
1363{
1364 struct snd_soc_component *component = snd_soc_lookup_component(dev, NULL);
1365
1366 if (!component || !component->card)
1367 return;
1368
1369 snd_card_disconnect_sync(component->card->snd_card);
1370}
Kuninori Morimotodf532182017-11-29 02:38:27 +00001371EXPORT_SYMBOL_GPL(snd_soc_disconnect_sync);
Kuninori Morimotoef2e8172017-11-28 06:27:09 +00001372
Mengdong Linf8f80362015-12-02 14:11:22 +08001373/**
1374 * snd_soc_add_dai_link - Add a DAI link dynamically
1375 * @card: The ASoC card to which the DAI link is added
1376 * @dai_link: The new DAI link to add
1377 *
1378 * This function adds a DAI link to the ASoC card's link list.
1379 *
1380 * Note: Topology can use this API to add DAI links when probing the
1381 * topology component. And machine drivers can still define static
1382 * DAI links in dai_link array.
1383 */
1384int snd_soc_add_dai_link(struct snd_soc_card *card,
1385 struct snd_soc_dai_link *dai_link)
1386{
1387 if (dai_link->dobj.type
1388 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1389 dev_err(card->dev, "Invalid dai link type %d\n",
1390 dai_link->dobj.type);
1391 return -EINVAL;
1392 }
1393
1394 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001395 /* Notify the machine driver for extra initialization
1396 * on the link created by topology.
1397 */
1398 if (dai_link->dobj.type && card->add_dai_link)
1399 card->add_dai_link(card, dai_link);
1400
Mengdong Linf8f80362015-12-02 14:11:22 +08001401 list_add_tail(&dai_link->list, &card->dai_link_list);
1402 card->num_dai_links++;
1403
1404 return 0;
1405}
1406EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1407
1408/**
1409 * snd_soc_remove_dai_link - Remove a DAI link from the list
1410 * @card: The ASoC card that owns the link
1411 * @dai_link: The DAI link to remove
1412 *
1413 * This function removes a DAI link from the ASoC card's link list.
1414 *
1415 * For DAI links previously added by topology, topology should
1416 * remove them by using the dobj embedded in the link.
1417 */
1418void snd_soc_remove_dai_link(struct snd_soc_card *card,
1419 struct snd_soc_dai_link *dai_link)
1420{
1421 struct snd_soc_dai_link *link, *_link;
1422
1423 if (dai_link->dobj.type
1424 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1425 dev_err(card->dev, "Invalid dai link type %d\n",
1426 dai_link->dobj.type);
1427 return;
1428 }
1429
1430 lockdep_assert_held(&client_mutex);
Mengdong Lind6f220e2015-12-02 14:11:32 +08001431 /* Notify the machine driver for extra destruction
1432 * on the link created by topology.
1433 */
1434 if (dai_link->dobj.type && card->remove_dai_link)
1435 card->remove_dai_link(card, dai_link);
1436
Mengdong Linf8f80362015-12-02 14:11:22 +08001437 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1438 if (link == dai_link) {
1439 list_del(&link->list);
1440 card->num_dai_links--;
1441 return;
1442 }
1443 }
1444}
1445EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1446
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001447static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001448 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001449{
1450 int i;
1451
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001452 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001453 return;
1454
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001455 for (i = 0; i < card->num_configs; i++) {
1456 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001457 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001458 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001459 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001460 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001461 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001462 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001463 }
1464}
1465
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001466static int soc_probe_component(struct snd_soc_card *card,
1467 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001468{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001469 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001470 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001471 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001472
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001473 if (!strcmp(component->name, "snd-soc-dummy"))
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001474 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001475
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001476 if (component->card) {
Lars-Peter Clausen1b7c1232015-07-08 20:47:43 +02001477 if (component->card != card) {
1478 dev_err(component->dev,
1479 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1480 card->name, component->card->name);
1481 return -ENODEV;
1482 }
1483 return 0;
1484 }
1485
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001486 if (!try_module_get(component->dev->driver->owner))
1487 return -ENODEV;
1488
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001489 component->card = card;
1490 dapm->card = card;
1491 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001492
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001493 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001494
Kuninori Morimoto688d0eb2017-08-25 00:29:20 +00001495 if (component->driver->dapm_widgets) {
1496 ret = snd_soc_dapm_new_controls(dapm,
1497 component->driver->dapm_widgets,
1498 component->driver->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001499
1500 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001501 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001502 "Failed to create new controls %d\n", ret);
1503 goto err_probe;
1504 }
1505 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001506
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001507 list_for_each_entry(dai, &component->dai_list, list) {
1508 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001509 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001510 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001511 "Failed to create DAI widgets %d\n", ret);
1512 goto err_probe;
1513 }
1514 }
Mark Brown888df392012-02-16 19:37:51 -08001515
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001516 if (component->probe) {
1517 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001518 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001519 dev_err(component->dev,
1520 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001521 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001522 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001523
1524 WARN(dapm->idle_bias_off &&
1525 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001526 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001527 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001528 }
1529
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001530 /* machine specific init */
1531 if (component->init) {
1532 ret = component->init(component);
1533 if (ret < 0) {
1534 dev_err(component->dev,
1535 "Failed to do machine specific init %d\n", ret);
1536 goto err_probe;
1537 }
1538 }
1539
Kuninori Morimotob8972bf2017-08-25 00:29:01 +00001540 if (component->driver->controls)
1541 snd_soc_add_component_controls(component,
1542 component->driver->controls,
1543 component->driver->num_controls);
Kuninori Morimoto6969b2b2017-08-25 00:29:41 +00001544 if (component->driver->dapm_routes)
1545 snd_soc_dapm_add_routes(dapm,
1546 component->driver->dapm_routes,
1547 component->driver->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001548
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001549 list_add(&dapm->list, &card->dapm_list);
Kuninori Morimotod9fc4062016-11-30 06:22:36 +00001550 list_add(&component->card_list, &card->component_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001551
Jarkko Nikula70d293312011-01-27 16:24:22 +02001552 return 0;
1553
1554err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001555 soc_cleanup_component_debugfs(component);
Lars-Peter Clausenabd31b32015-07-08 20:47:44 +02001556 component->card = NULL;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001557 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001558
1559 return ret;
1560}
1561
Mark Brown36ae1a92012-01-06 17:12:45 -08001562static void rtd_release(struct device *dev)
1563{
1564 kfree(dev);
1565}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001567static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1568 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001569{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001570 int ret = 0;
1571
Jarkko Nikula589c3562010-12-06 16:27:07 +02001572 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001573 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1574 if (!rtd->dev)
1575 return -ENOMEM;
1576 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001577 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001578 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001579 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001580 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001581 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001582 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001583 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1584 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1585 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1586 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001587 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001588 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001589 /* calling put_device() here to free the rtd->dev */
1590 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001591 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001592 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001593 return ret;
1594 }
1595 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001596 return 0;
1597}
1598
Mengdong Lin1a497982015-11-18 02:34:11 -05001599static int soc_probe_link_components(struct snd_soc_card *card,
1600 struct snd_soc_pcm_runtime *rtd,
Stephen Warren62ae68f2012-06-08 12:34:23 -06001601 int order)
1602{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001603 struct snd_soc_component *component;
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001604 struct snd_soc_rtdcom_list *rtdcom;
1605 int ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001606
Kuninori Morimoto90be7112017-08-08 06:18:10 +00001607 for_each_rtdcom(rtd, rtdcom) {
1608 component = rtdcom->component;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001609
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001610 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001611 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001612 if (ret < 0)
1613 return ret;
1614 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001615 }
1616
Stephen Warren62ae68f2012-06-08 12:34:23 -06001617 return 0;
1618}
1619
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001620static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001621{
1622 int ret;
1623
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001624 if (!dai->probed && dai->driver->probe_order == order) {
1625 if (dai->driver->probe) {
1626 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001627 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001628 dev_err(dai->dev,
1629 "ASoC: failed to probe DAI %s: %d\n",
1630 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001631 return ret;
1632 }
1633 }
1634
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001635 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001636 }
1637
1638 return 0;
1639}
1640
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001641static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1642 struct snd_soc_pcm_runtime *rtd)
1643{
1644 int i, ret = 0;
1645
1646 for (i = 0; i < num_dais; ++i) {
1647 struct snd_soc_dai_driver *drv = dais[i]->driver;
1648
1649 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1650 ret = drv->pcm_new(rtd, dais[i]);
1651 if (ret < 0) {
1652 dev_err(dais[i]->dev,
1653 "ASoC: Failed to bind %s with pcm device\n",
1654 dais[i]->name);
1655 return ret;
1656 }
1657 }
1658
1659 return 0;
1660}
1661
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001662static int soc_link_dai_widgets(struct snd_soc_card *card,
1663 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001664 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001665{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1667 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Vinod Koul3de7c422015-11-09 23:19:58 +05301668 struct snd_soc_dapm_widget *sink, *source;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001669 int ret;
1670
Benoit Cousson88bd8702014-07-08 23:19:34 +02001671 if (rtd->num_codecs > 1)
1672 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1673
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001674 /* link the DAI widgets */
Vinod Koul3de7c422015-11-09 23:19:58 +05301675 sink = codec_dai->playback_widget;
1676 source = cpu_dai->capture_widget;
1677 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001678 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301679 dai_link->num_params,
1680 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001681 if (ret != 0) {
1682 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301683 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001684 return ret;
1685 }
1686 }
1687
Vinod Koul3de7c422015-11-09 23:19:58 +05301688 sink = cpu_dai->playback_widget;
1689 source = codec_dai->capture_widget;
1690 if (sink && source) {
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001691 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Vinod Koul3de7c422015-11-09 23:19:58 +05301692 dai_link->num_params,
1693 source, sink);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001694 if (ret != 0) {
1695 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Vinod Koul3de7c422015-11-09 23:19:58 +05301696 sink->name, source->name, ret);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001697 return ret;
1698 }
1699 }
1700
1701 return 0;
1702}
1703
Mengdong Lin1a497982015-11-18 02:34:11 -05001704static int soc_probe_link_dais(struct snd_soc_card *card,
1705 struct snd_soc_pcm_runtime *rtd, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706{
Mengdong Lin1a497982015-11-18 02:34:11 -05001707 struct snd_soc_dai_link *dai_link = rtd->dai_link;
Mark Brownc74184e2012-04-04 22:12:09 +01001708 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001709 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001711 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Mengdong Lin1a497982015-11-18 02:34:11 -05001712 card->name, rtd->num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 /* set default power off timeout */
1715 rtd->pmdown_time = pmdown_time;
1716
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001717 ret = soc_probe_dai(cpu_dai, order);
1718 if (ret)
1719 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001722 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001723 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001724 if (ret)
1725 return ret;
1726 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001727
Liam Girdwood0168bf02011-06-07 16:08:05 +01001728 /* complete DAI probe during last probe */
1729 if (order != SND_SOC_COMP_ORDER_LAST)
1730 return 0;
1731
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001732 /* do machine specific initialization */
1733 if (dai_link->init) {
1734 ret = dai_link->init(rtd);
1735 if (ret < 0) {
1736 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1737 dai_link->name, ret);
1738 return ret;
1739 }
1740 }
1741
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001742 if (dai_link->dai_fmt)
1743 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1744
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001745 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001746 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001749#ifdef CONFIG_DEBUG_FS
1750 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001751 if (dai_link->dynamic)
1752 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001753#endif
1754
Jie Yang6f0c4222015-10-13 23:41:00 +08001755 if (cpu_dai->driver->compress_new) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301756 /*create compress_device"*/
Mengdong Lin1a497982015-11-18 02:34:11 -05001757 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
Mark Brownc74184e2012-04-04 22:12:09 +01001758 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001759 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301760 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001761 return ret;
1762 }
1763 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301764
1765 if (!dai_link->params) {
1766 /* create the pcm */
Mengdong Lin1a497982015-11-18 02:34:11 -05001767 ret = soc_new_pcm(rtd, rtd->num);
Namarta Kohli1245b702012-08-16 17:10:41 +05301768 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001769 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301770 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001771 return ret;
1772 }
Arnaud Pouliquen25f7b702017-01-03 16:52:51 +01001773 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1774 if (ret < 0)
1775 return ret;
1776 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1777 rtd->num_codecs, rtd);
1778 if (ret < 0)
1779 return ret;
Namarta Kohli1245b702012-08-16 17:10:41 +05301780 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001781 INIT_DELAYED_WORK(&rtd->delayed_work,
1782 codec2codec_close_delayed_work);
1783
Namarta Kohli1245b702012-08-16 17:10:41 +05301784 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001785 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001786 if (ret)
1787 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001788 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789 }
1790
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791 return 0;
1792}
1793
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001794static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001795{
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001796 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001797 struct snd_soc_component *component;
1798 const char *name;
1799 struct device_node *codec_of_node;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001800
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001801 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1802 /* codecs, usually analog devices */
1803 name = aux_dev->codec_name;
1804 codec_of_node = aux_dev->codec_of_node;
1805 component = soc_find_component(codec_of_node, name);
1806 if (!component) {
1807 if (codec_of_node)
1808 name = of_node_full_name(codec_of_node);
1809 goto err_defer;
1810 }
1811 } else if (aux_dev->name) {
1812 /* generic components */
1813 name = aux_dev->name;
1814 component = soc_find_component(NULL, name);
1815 if (!component)
1816 goto err_defer;
1817 } else {
1818 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1819 return -EINVAL;
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001820 }
1821
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001822 component->init = aux_dev->init;
Sylwester Nawrockid2e3a132016-12-29 14:11:21 +01001823 list_add(&component->card_aux_list, &card->aux_comp_list);
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001824
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001825 return 0;
1826
1827err_defer:
1828 dev_err(card->dev, "ASoC: %s not registered\n", name);
1829 return -EPROBE_DEFER;
1830}
1831
1832static int soc_probe_aux_devices(struct snd_soc_card *card)
1833{
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001834 struct snd_soc_component *comp;
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001835 int order;
1836 int ret;
1837
1838 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1839 order++) {
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001840 list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) {
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001841 if (comp->driver->probe_order == order) {
1842 ret = soc_probe_component(card, comp);
1843 if (ret < 0) {
1844 dev_err(card->dev,
1845 "ASoC: failed to probe aux component %s %d\n",
1846 comp->name, ret);
1847 return ret;
1848 }
1849 }
1850 }
1851 }
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001852
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001853 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001854}
1855
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001856static void soc_remove_aux_devices(struct snd_soc_card *card)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001857{
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001858 struct snd_soc_component *comp, *_comp;
1859 int order;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001860
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001861 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1862 order++) {
1863 list_for_each_entry_safe(comp, _comp,
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001864 &card->aux_comp_list, card_aux_list) {
Kuninori Morimoto1a653aa2016-11-30 06:22:55 +00001865
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001866 if (comp->driver->remove_order == order) {
1867 soc_remove_component(comp);
Kuninori Morimoto991454e2017-03-24 00:13:00 +00001868 /* remove it from the card's aux_comp_list */
1869 list_del(&comp->card_aux_list);
Mengdong Linf2ed6b02016-01-06 13:29:31 +08001870 }
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001871 }
1872 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001873}
1874
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001875static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001876{
1877 int ret;
1878
1879 if (codec->cache_init)
1880 return 0;
1881
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001882 ret = snd_soc_cache_init(codec);
1883 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001884 dev_err(codec->dev,
1885 "ASoC: Failed to set cache compression type: %d\n",
1886 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001887 return ret;
1888 }
1889 codec->cache_init = 1;
1890 return 0;
1891}
1892
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001893/**
1894 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1895 * @rtd: The runtime for which the DAI link format should be changed
1896 * @dai_fmt: The new DAI link format
1897 *
1898 * This function updates the DAI link format for all DAIs connected to the DAI
1899 * link for the specified runtime.
1900 *
1901 * Note: For setups with a static format set the dai_fmt field in the
1902 * corresponding snd_dai_link struct instead of using this function.
1903 *
1904 * Returns 0 on success, otherwise a negative error code.
1905 */
1906int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1907 unsigned int dai_fmt)
1908{
1909 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1910 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1911 unsigned int i;
1912 int ret;
1913
1914 for (i = 0; i < rtd->num_codecs; i++) {
1915 struct snd_soc_dai *codec_dai = codec_dais[i];
1916
1917 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1918 if (ret != 0 && ret != -ENOTSUPP) {
1919 dev_warn(codec_dai->dev,
1920 "ASoC: Failed to set DAI format: %d\n", ret);
1921 return ret;
1922 }
1923 }
1924
1925 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
Kuninori Morimotocb2cf0d2017-12-15 05:10:47 +00001926 /* the component which has non_legacy_dai_naming is Codec */
1927 if (cpu_dai->codec ||
1928 cpu_dai->component->driver->non_legacy_dai_naming) {
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001929 unsigned int inv_dai_fmt;
1930
1931 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1932 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1933 case SND_SOC_DAIFMT_CBM_CFM:
1934 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1935 break;
1936 case SND_SOC_DAIFMT_CBM_CFS:
1937 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1938 break;
1939 case SND_SOC_DAIFMT_CBS_CFM:
1940 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1941 break;
1942 case SND_SOC_DAIFMT_CBS_CFS:
1943 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1944 break;
1945 }
1946
1947 dai_fmt = inv_dai_fmt;
1948 }
1949
1950 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1951 if (ret != 0 && ret != -ENOTSUPP) {
1952 dev_warn(cpu_dai->dev,
1953 "ASoC: Failed to set DAI format: %d\n", ret);
1954 return ret;
1955 }
1956
1957 return 0;
1958}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001959EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001960
Liam Girdwood345233d2017-01-14 16:13:02 +08001961
Takashi Iwai1f5a4532017-04-24 08:54:41 +02001962#ifdef CONFIG_DMI
Liam Girdwood345233d2017-01-14 16:13:02 +08001963/* Trim special characters, and replace '-' with '_' since '-' is used to
1964 * separate different DMI fields in the card long name. Only number and
1965 * alphabet characters and a few separator characters are kept.
1966 */
1967static void cleanup_dmi_name(char *name)
1968{
1969 int i, j = 0;
1970
1971 for (i = 0; name[i]; i++) {
1972 if (isalnum(name[i]) || (name[i] == '.')
1973 || (name[i] == '_'))
1974 name[j++] = name[i];
1975 else if (name[i] == '-')
1976 name[j++] = '_';
1977 }
1978
1979 name[j] = '\0';
1980}
1981
Mengdong Lin98faf432017-06-28 15:01:39 +08001982/* Check if a DMI field is valid, i.e. not containing any string
1983 * in the black list.
1984 */
1985static int is_dmi_valid(const char *field)
1986{
1987 int i = 0;
1988
1989 while (dmi_blacklist[i]) {
1990 if (strstr(field, dmi_blacklist[i]))
1991 return 0;
1992 i++;
Wu Fengguang46b5a4d2017-06-30 00:27:13 +08001993 }
Mengdong Lin98faf432017-06-28 15:01:39 +08001994
1995 return 1;
1996}
1997
Liam Girdwood345233d2017-01-14 16:13:02 +08001998/**
1999 * snd_soc_set_dmi_name() - Register DMI names to card
2000 * @card: The card to register DMI names
2001 * @flavour: The flavour "differentiator" for the card amongst its peers.
2002 *
2003 * An Intel machine driver may be used by many different devices but are
2004 * difficult for userspace to differentiate, since machine drivers ususally
2005 * use their own name as the card short name and leave the card long name
2006 * blank. To differentiate such devices and fix bugs due to lack of
2007 * device-specific configurations, this function allows DMI info to be used
2008 * as the sound card long name, in the format of
2009 * "vendor-product-version-board"
2010 * (Character '-' is used to separate different DMI fields here).
2011 * This will help the user space to load the device-specific Use Case Manager
2012 * (UCM) configurations for the card.
2013 *
2014 * Possible card long names may be:
2015 * DellInc.-XPS139343-01-0310JH
2016 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
2017 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
2018 *
2019 * This function also supports flavoring the card longname to provide
2020 * the extra differentiation, like "vendor-product-version-board-flavor".
2021 *
2022 * We only keep number and alphabet characters and a few separator characters
2023 * in the card long name since UCM in the user space uses the card long names
2024 * as card configuration directory names and AudoConf cannot support special
2025 * charactors like SPACE.
2026 *
2027 * Returns 0 on success, otherwise a negative error code.
2028 */
2029int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
2030{
2031 const char *vendor, *product, *product_version, *board;
2032 size_t longname_buf_size = sizeof(card->snd_card->longname);
2033 size_t len;
2034
2035 if (card->long_name)
2036 return 0; /* long name already set by driver or from DMI */
2037
2038 /* make up dmi long name as: vendor.product.version.board */
2039 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Mengdong Lin98faf432017-06-28 15:01:39 +08002040 if (!vendor || !is_dmi_valid(vendor)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08002041 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
2042 return 0;
2043 }
2044
Mengdong Lin98faf432017-06-28 15:01:39 +08002045
Liam Girdwood345233d2017-01-14 16:13:02 +08002046 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
2047 "%s", vendor);
2048 cleanup_dmi_name(card->dmi_longname);
2049
2050 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08002051 if (product && is_dmi_valid(product)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08002052 len = strlen(card->dmi_longname);
2053 snprintf(card->dmi_longname + len,
2054 longname_buf_size - len,
2055 "-%s", product);
2056
2057 len++; /* skip the separator "-" */
2058 if (len < longname_buf_size)
2059 cleanup_dmi_name(card->dmi_longname + len);
2060
2061 /* some vendors like Lenovo may only put a self-explanatory
2062 * name in the product version field
2063 */
2064 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
Mengdong Lin98faf432017-06-28 15:01:39 +08002065 if (product_version && is_dmi_valid(product_version)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08002066 len = strlen(card->dmi_longname);
2067 snprintf(card->dmi_longname + len,
2068 longname_buf_size - len,
2069 "-%s", product_version);
2070
2071 len++;
2072 if (len < longname_buf_size)
2073 cleanup_dmi_name(card->dmi_longname + len);
2074 }
2075 }
2076
2077 board = dmi_get_system_info(DMI_BOARD_NAME);
Mengdong Lin98faf432017-06-28 15:01:39 +08002078 if (board && is_dmi_valid(board)) {
Liam Girdwood345233d2017-01-14 16:13:02 +08002079 len = strlen(card->dmi_longname);
2080 snprintf(card->dmi_longname + len,
2081 longname_buf_size - len,
2082 "-%s", board);
2083
2084 len++;
2085 if (len < longname_buf_size)
2086 cleanup_dmi_name(card->dmi_longname + len);
2087 } else if (!product) {
2088 /* fall back to using legacy name */
2089 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2090 return 0;
2091 }
2092
2093 /* Add flavour to dmi long name */
2094 if (flavour) {
2095 len = strlen(card->dmi_longname);
2096 snprintf(card->dmi_longname + len,
2097 longname_buf_size - len,
2098 "-%s", flavour);
2099
2100 len++;
2101 if (len < longname_buf_size)
2102 cleanup_dmi_name(card->dmi_longname + len);
2103 }
2104
2105 /* set the card long name */
2106 card->long_name = card->dmi_longname;
2107
2108 return 0;
2109}
2110EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
Takashi Iwai1f5a4532017-04-24 08:54:41 +02002111#endif /* CONFIG_DMI */
Liam Girdwood345233d2017-01-14 16:13:02 +08002112
Mark Brownb19e6e72012-03-14 21:18:39 +00002113static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002115 struct snd_soc_codec *codec;
Mengdong Lin1a497982015-11-18 02:34:11 -05002116 struct snd_soc_pcm_runtime *rtd;
Mengdong Lin61b00882015-12-02 14:11:48 +08002117 struct snd_soc_dai_link *dai_link;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01002118 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002119
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002120 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00002121 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002122
Mark Brownb19e6e72012-03-14 21:18:39 +00002123 /* bind DAIs */
2124 for (i = 0; i < card->num_links; i++) {
Mengdong Lin6f2f1ff2015-11-23 11:03:52 -05002125 ret = soc_bind_dai_link(card, &card->dai_link[i]);
Mark Brownb19e6e72012-03-14 21:18:39 +00002126 if (ret != 0)
2127 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002128 }
2129
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002130 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00002131 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02002132 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00002133 if (ret != 0)
2134 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002135 }
2136
Mengdong Linf8f80362015-12-02 14:11:22 +08002137 /* add predefined DAI links to the list */
2138 for (i = 0; i < card->num_links; i++)
2139 snd_soc_add_dai_link(card, card->dai_link+i);
2140
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002141 /* initialize the register cache for each available codec */
2142 list_for_each_entry(codec, &codec_list, list) {
2143 if (codec->cache_init)
2144 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02002145 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00002146 if (ret < 0)
2147 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002148 }
2149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002150 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01002151 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002152 card->owner, 0, &card->snd_card);
2153 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002154 dev_err(card->dev,
2155 "ASoC: can't create sound card for card %s: %d\n",
2156 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00002157 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002158 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002159
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002160 soc_init_card_debugfs(card);
2161
Mark Browne37a4972011-03-02 18:21:57 +00002162 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2163 card->dapm.dev = card->dev;
2164 card->dapm.card = card;
2165 list_add(&card->dapm.list, &card->dapm_list);
2166
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002167#ifdef CONFIG_DEBUG_FS
2168 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2169#endif
2170
Mark Brown88ee1c62011-02-02 10:43:26 +00002171#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002172 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002173 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002174#endif
Andy Green6ed25972008-06-13 16:24:05 +01002175
Mark Brown9a841eb2011-04-12 17:51:37 -07002176 if (card->dapm_widgets)
2177 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2178 card->num_dapm_widgets);
2179
Nicolin Chenf23e8602015-02-14 17:22:49 -08002180 if (card->of_dapm_widgets)
2181 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2182 card->num_of_dapm_widgets);
2183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002184 /* initialise the sound card only once */
2185 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002186 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002187 if (ret < 0)
2188 goto card_probe_error;
2189 }
2190
Stephen Warren62ae68f2012-06-08 12:34:23 -06002191 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01002192 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2193 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002194 list_for_each_entry(rtd, &card->rtd_list, list) {
2195 ret = soc_probe_link_components(card, rtd, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002196 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002197 dev_err(card->dev,
2198 "ASoC: failed to instantiate card %d\n",
2199 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002200 goto probe_dai_err;
2201 }
2202 }
2203 }
2204
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002205 /* probe auxiliary components */
2206 ret = soc_probe_aux_devices(card);
2207 if (ret < 0)
2208 goto probe_dai_err;
2209
Mengdong Lin61b00882015-12-02 14:11:48 +08002210 /* Find new DAI links added during probing components and bind them.
2211 * Components with topology may bring new DAIs and DAI links.
2212 */
2213 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2214 if (soc_is_dai_link_bound(card, dai_link))
2215 continue;
2216
2217 ret = soc_init_dai_link(card, dai_link);
2218 if (ret)
2219 goto probe_dai_err;
2220 ret = soc_bind_dai_link(card, dai_link);
2221 if (ret)
2222 goto probe_dai_err;
2223 }
2224
Stephen Warren62ae68f2012-06-08 12:34:23 -06002225 /* probe all DAI links on this card */
2226 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2227 order++) {
Mengdong Lin1a497982015-11-18 02:34:11 -05002228 list_for_each_entry(rtd, &card->rtd_list, list) {
2229 ret = soc_probe_link_dais(card, rtd, order);
Stephen Warren62ae68f2012-06-08 12:34:23 -06002230 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002231 dev_err(card->dev,
2232 "ASoC: failed to instantiate card %d\n",
2233 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01002234 goto probe_dai_err;
2235 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002236 }
2237 }
2238
Mark Brown888df392012-02-16 19:37:51 -08002239 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00002240 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08002241
Mark Brownb7af1da2011-04-07 19:18:44 +09002242 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00002243 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09002244
Mark Brownb8ad29d2011-03-02 18:35:51 +00002245 if (card->dapm_routes)
2246 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2247 card->num_dapm_routes);
2248
Nicolin Chenf23e8602015-02-14 17:22:49 -08002249 if (card->of_dapm_routes)
2250 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2251 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01002252
Takashi Iwai861886d2017-04-24 08:54:42 +02002253 /* try to set some sane longname if DMI is available */
2254 snd_soc_set_dmi_name(card, NULL);
2255
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002256 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002257 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002258 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2259 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01002260 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2261 "%s", card->driver_name ? card->driver_name : card->name);
2262 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2263 switch (card->snd_card->driver[i]) {
2264 case '_':
2265 case '-':
2266 case '\0':
2267 break;
2268 default:
2269 if (!isalnum(card->snd_card->driver[i]))
2270 card->snd_card->driver[i] = '_';
2271 break;
2272 }
2273 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002274
Mark Brown28e9ad92011-03-02 18:36:34 +00002275 if (card->late_probe) {
2276 ret = card->late_probe(card);
2277 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002278 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00002279 card->name, ret);
2280 goto probe_aux_dev_err;
2281 }
2282 }
2283
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02002284 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02002285
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002286 ret = snd_card_register(card->snd_card);
2287 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002288 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2289 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08002290 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002291 }
2292
Mark Brown435c5e22008-12-04 15:32:53 +00002293 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01002294 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002295 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002296 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00002297
2298 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002300probe_aux_dev_err:
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002301 soc_remove_aux_devices(card);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002302
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002303probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002304 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002305
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002306card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002307 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002308 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002309
Lars-Peter Clausen22104382015-07-08 22:14:48 +02002310 snd_soc_dapm_free(&card->dapm);
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02002311 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002312 snd_card_free(card->snd_card);
2313
Mark Brownb19e6e72012-03-14 21:18:39 +00002314base_error:
Mengdong Lin1a497982015-11-18 02:34:11 -05002315 soc_remove_pcm_runtimes(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002316 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002317 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318
Mark Brownb19e6e72012-03-14 21:18:39 +00002319 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00002320}
2321
2322/* probes a new socdev */
2323static int soc_probe(struct platform_device *pdev)
2324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002325 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002326
Vinod Koul70a7ca32011-01-14 19:22:48 +05302327 /*
2328 * no card, so machine driver should be registering card
2329 * we should not be here in that case so ret error
2330 */
2331 if (!card)
2332 return -EINVAL;
2333
Mark Brownfe4085e2012-03-02 13:07:41 +00002334 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002335 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002336 card->name);
2337
Mark Brown435c5e22008-12-04 15:32:53 +00002338 /* Bodge while we unpick instantiation */
2339 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002340
Mark Brown28d528c2012-08-09 18:45:23 +01002341 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342}
2343
Vinod Koulb0e26482011-01-13 22:48:02 +05302344static int soc_cleanup_card_resources(struct snd_soc_card *card)
2345{
Mengdong Lin1a497982015-11-18 02:34:11 -05002346 struct snd_soc_pcm_runtime *rtd;
Vinod Koulb0e26482011-01-13 22:48:02 +05302347
2348 /* make sure any delayed work runs */
Mengdong Lin1a497982015-11-18 02:34:11 -05002349 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002350 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302351
Takashi Iwai4efda5f22017-05-24 10:19:45 +02002352 /* free the ALSA card at first; this syncs with pending operations */
2353 snd_card_free(card->snd_card);
2354
Vinod Koulb0e26482011-01-13 22:48:02 +05302355 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002356 soc_remove_dai_links(card);
Mengdong Lin1a497982015-11-18 02:34:11 -05002357 soc_remove_pcm_runtimes(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302358
Mengdong Linf2ed6b02016-01-06 13:29:31 +08002359 /* remove auxiliary devices */
2360 soc_remove_aux_devices(card);
2361
Mark Brownd1e81422016-08-18 19:32:59 +01002362 snd_soc_dapm_free(&card->dapm);
Vinod Koulb0e26482011-01-13 22:48:02 +05302363 soc_cleanup_card_debugfs(card);
2364
2365 /* remove the card */
2366 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002367 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302368
Vinod Koulb0e26482011-01-13 22:48:02 +05302369 return 0;
Vinod Koulb0e26482011-01-13 22:48:02 +05302370}
2371
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372/* removes a socdev */
2373static int soc_remove(struct platform_device *pdev)
2374{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002375 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376
Mark Brownc5af3a22008-11-28 13:29:45 +00002377 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 return 0;
2379}
2380
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002381int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002382{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002383 struct snd_soc_card *card = dev_get_drvdata(dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002384 struct snd_soc_pcm_runtime *rtd;
Mark Brown51737472009-06-22 13:16:51 +01002385
2386 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002387 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002388
2389 /* Flush out pmdown_time work - we actually do want to run it
2390 * now, we're shutting down so no imminent restart. */
Mengdong Lin1a497982015-11-18 02:34:11 -05002391 list_for_each_entry(rtd, &card->rtd_list, list)
Tejun Heo43829732012-08-20 14:51:24 -07002392 flush_delayed_work(&rtd->delayed_work);
Mark Brown51737472009-06-22 13:16:51 +01002393
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002394 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002395
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002396 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002397 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002398 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05002399 int i;
Benoit Cousson88bd8702014-07-08 23:19:34 +02002400
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002401 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mengdong Lin1a497982015-11-18 02:34:11 -05002402 for (i = 0; i < rtd->num_codecs; i++) {
2403 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002404 pinctrl_pm_select_sleep_state(codec_dai->dev);
2405 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002406 }
2407
Mark Brown416356f2009-06-30 19:05:15 +01002408 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002409}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002410EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002411
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002412const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302413 .suspend = snd_soc_suspend,
2414 .resume = snd_soc_resume,
2415 .freeze = snd_soc_suspend,
2416 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002417 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302418 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002419};
Stephen Warrendeb26072011-04-05 19:35:30 -06002420EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002421
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422/* ASoC platform driver */
2423static struct platform_driver soc_driver = {
2424 .driver = {
2425 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002426 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427 },
2428 .probe = soc_probe,
2429 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430};
2431
Mark Brown096e49d2009-07-05 15:12:22 +01002432/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433 * snd_soc_cnew - create new control
2434 * @_template: control template
2435 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002436 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002437 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002438 *
2439 * Create a new mixer control from a template control.
2440 *
2441 * Returns 0 for success, else error.
2442 */
2443struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002444 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002445 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446{
2447 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002448 struct snd_kcontrol *kcontrol;
2449 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450
2451 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452 template.index = 0;
2453
Mark Brownefb7ac32011-03-08 17:23:24 +00002454 if (!long_name)
2455 long_name = template.name;
2456
2457 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002458 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002459 if (!name)
2460 return NULL;
2461
Mark Brownefb7ac32011-03-08 17:23:24 +00002462 template.name = name;
2463 } else {
2464 template.name = long_name;
2465 }
2466
2467 kcontrol = snd_ctl_new1(&template, data);
2468
2469 kfree(name);
2470
2471 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472}
2473EXPORT_SYMBOL_GPL(snd_soc_cnew);
2474
Liam Girdwood022658b2012-02-03 17:43:09 +00002475static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2476 const struct snd_kcontrol_new *controls, int num_controls,
2477 const char *prefix, void *data)
2478{
2479 int err, i;
2480
2481 for (i = 0; i < num_controls; i++) {
2482 const struct snd_kcontrol_new *control = &controls[i];
2483 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2484 control->name, prefix));
2485 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002486 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2487 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002488 return err;
2489 }
2490 }
2491
2492 return 0;
2493}
2494
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002495struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2496 const char *name)
2497{
2498 struct snd_card *card = soc_card->snd_card;
2499 struct snd_kcontrol *kctl;
2500
2501 if (unlikely(!name))
2502 return NULL;
2503
2504 list_for_each_entry(kctl, &card->controls, list)
2505 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2506 return kctl;
2507 return NULL;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2510
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002511/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002512 * snd_soc_add_component_controls - Add an array of controls to a component.
2513 *
2514 * @component: Component to add controls to
2515 * @controls: Array of controls to add
2516 * @num_controls: Number of elements in the array
2517 *
2518 * Return: 0 for success, else error.
2519 */
2520int snd_soc_add_component_controls(struct snd_soc_component *component,
2521 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2522{
2523 struct snd_card *card = component->card->snd_card;
2524
2525 return snd_soc_add_controls(card, component->dev, controls,
2526 num_controls, component->name_prefix, component);
2527}
2528EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2529
2530/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002531 * snd_soc_add_codec_controls - add an array of controls to a codec.
2532 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002533 * duplicating this code.
2534 *
2535 * @codec: codec to add controls to
2536 * @controls: array of controls to add
2537 * @num_controls: number of elements in the array
2538 *
2539 * Return 0 for success, else error.
2540 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002541int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002542 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002543{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002544 return snd_soc_add_component_controls(&codec->component, controls,
2545 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002546}
Liam Girdwood022658b2012-02-03 17:43:09 +00002547EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002548
2549/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002550 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002551 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002552 *
2553 * @platform: platform to add controls to
2554 * @controls: array of controls to add
2555 * @num_controls: number of elements in the array
2556 *
2557 * Return 0 for success, else error.
2558 */
2559int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002560 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002561{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002562 return snd_soc_add_component_controls(&platform->component, controls,
2563 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002564}
2565EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2566
2567/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002568 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2569 * Convenience function to add a list of controls.
2570 *
2571 * @soc_card: SoC card to add controls to
2572 * @controls: array of controls to add
2573 * @num_controls: number of elements in the array
2574 *
2575 * Return 0 for success, else error.
2576 */
2577int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2578 const struct snd_kcontrol_new *controls, int num_controls)
2579{
2580 struct snd_card *card = soc_card->snd_card;
2581
2582 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2583 NULL, soc_card);
2584}
2585EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2586
2587/**
2588 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2589 * Convienience function to add a list of controls.
2590 *
2591 * @dai: DAI to add controls to
2592 * @controls: array of controls to add
2593 * @num_controls: number of elements in the array
2594 *
2595 * Return 0 for success, else error.
2596 */
2597int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2598 const struct snd_kcontrol_new *controls, int num_controls)
2599{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002600 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002601
2602 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2603 NULL, dai);
2604}
2605EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2606
2607/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002608 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2609 * @dai: DAI
2610 * @clk_id: DAI specific clock ID
2611 * @freq: new clock frequency in Hz
2612 * @dir: new clock direction - input/output.
2613 *
2614 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2615 */
2616int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2617 unsigned int freq, int dir)
2618{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002619 if (dai->driver->ops->set_sysclk)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002620 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002621
2622 return snd_soc_component_set_sysclk(dai->component, clk_id, 0,
2623 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002624}
2625EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2626
2627/**
Mark Brownec4ee522011-03-07 20:58:11 +00002628 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2629 * @codec: CODEC
2630 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002631 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002632 * @freq: new clock frequency in Hz
2633 * @dir: new clock direction - input/output.
2634 *
2635 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2636 */
2637int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002638 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002639{
2640 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002641 return codec->driver->set_sysclk(codec, clk_id, source,
2642 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002643 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002644 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002645}
2646EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2647
2648/**
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00002649 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
2650 * @component: COMPONENT
2651 * @clk_id: DAI specific clock ID
2652 * @source: Source for the clock
2653 * @freq: new clock frequency in Hz
2654 * @dir: new clock direction - input/output.
2655 *
2656 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2657 */
2658int snd_soc_component_set_sysclk(struct snd_soc_component *component, int clk_id,
2659 int source, unsigned int freq, int dir)
2660{
2661 /* will be removed */
2662 if (component->set_sysclk)
2663 return component->set_sysclk(component, clk_id, source,
2664 freq, dir);
2665
2666 if (component->driver->set_sysclk)
2667 return component->driver->set_sysclk(component, clk_id, source,
2668 freq, dir);
2669
2670 return -ENOTSUPP;
2671}
2672EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
2673
2674/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002675 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2676 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002677 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002678 * @div: new clock divisor.
2679 *
2680 * Configures the clock dividers. This is used to derive the best DAI bit and
2681 * frame clocks from the system or master clock. It's best to set the DAI bit
2682 * and frame clocks as low as possible to save system power.
2683 */
2684int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2685 int div_id, int div)
2686{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002687 if (dai->driver->ops->set_clkdiv)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002688 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002689 else
2690 return -EINVAL;
2691}
2692EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2693
2694/**
2695 * snd_soc_dai_set_pll - configure DAI PLL.
2696 * @dai: DAI
2697 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002698 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002699 * @freq_in: PLL input clock frequency in Hz
2700 * @freq_out: requested PLL output clock frequency in Hz
2701 *
2702 * Configures and enables PLL to generate output clock based on input clock.
2703 */
Mark Brown85488032009-09-05 18:52:16 +01002704int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2705 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002706{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002707 if (dai->driver->ops->set_pll)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002708 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002709 freq_in, freq_out);
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002710
2711 return snd_soc_component_set_pll(dai->component, pll_id, source,
2712 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002713}
2714EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2715
Mark Brownec4ee522011-03-07 20:58:11 +00002716/*
2717 * snd_soc_codec_set_pll - configure codec PLL.
2718 * @codec: CODEC
2719 * @pll_id: DAI specific PLL ID
2720 * @source: DAI specific source for the PLL
2721 * @freq_in: PLL input clock frequency in Hz
2722 * @freq_out: requested PLL output clock frequency in Hz
2723 *
2724 * Configures and enables PLL to generate output clock based on input clock.
2725 */
2726int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2727 unsigned int freq_in, unsigned int freq_out)
2728{
2729 if (codec->driver->set_pll)
2730 return codec->driver->set_pll(codec, pll_id, source,
2731 freq_in, freq_out);
2732 else
2733 return -EINVAL;
2734}
2735EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2736
Kuninori Morimotoef641e52017-08-24 00:57:51 +00002737/*
2738 * snd_soc_component_set_pll - configure component PLL.
2739 * @component: COMPONENT
2740 * @pll_id: DAI specific PLL ID
2741 * @source: DAI specific source for the PLL
2742 * @freq_in: PLL input clock frequency in Hz
2743 * @freq_out: requested PLL output clock frequency in Hz
2744 *
2745 * Configures and enables PLL to generate output clock based on input clock.
2746 */
2747int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
2748 int source, unsigned int freq_in,
2749 unsigned int freq_out)
2750{
2751 /* will be removed */
2752 if (component->set_pll)
2753 return component->set_pll(component, pll_id, source,
2754 freq_in, freq_out);
2755
2756 if (component->driver->set_pll)
2757 return component->driver->set_pll(component, pll_id, source,
2758 freq_in, freq_out);
2759
2760 return -EINVAL;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
2763
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002764/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002765 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2766 * @dai: DAI
Masanari Iida231b86b2015-07-15 23:02:38 +09002767 * @ratio: Ratio of BCLK to Sample rate.
Liam Girdwoode54cf762013-09-16 13:01:46 +01002768 *
2769 * Configures the DAI for a preset BCLK to sample rate ratio.
2770 */
2771int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2772{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002773 if (dai->driver->ops->set_bclk_ratio)
Liam Girdwoode54cf762013-09-16 13:01:46 +01002774 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2775 else
2776 return -EINVAL;
2777}
2778EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2779
2780/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002781 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2782 * @dai: DAI
Randy Dunlapbb19ba22017-10-29 17:10:34 -07002783 * @fmt: SND_SOC_DAIFMT_* format value.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002784 *
2785 * Configures the DAI hardware format and clocking.
2786 */
2787int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2788{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002789 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002790 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002791 if (dai->driver->ops->set_fmt == NULL)
2792 return -ENOTSUPP;
2793 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002794}
2795EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2796
2797/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002798 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002799 * @slots: Number of slots in use.
2800 * @tx_mask: bitmask representing active TX slots.
2801 * @rx_mask: bitmask representing active RX slots.
2802 *
2803 * Generates the TDM tx and rx slot default masks for DAI.
2804 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002805static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002806 unsigned int *tx_mask,
2807 unsigned int *rx_mask)
2808{
2809 if (*tx_mask || *rx_mask)
2810 return 0;
2811
2812 if (!slots)
2813 return -EINVAL;
2814
2815 *tx_mask = (1 << slots) - 1;
2816 *rx_mask = (1 << slots) - 1;
2817
2818 return 0;
2819}
2820
2821/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002822 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2823 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002824 * @tx_mask: bitmask representing active TX slots.
2825 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002826 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002827 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002828 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002829 * This function configures the specified DAI for TDM operation. @slot contains
2830 * the total number of slots of the TDM stream and @slot_with the width of each
2831 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2832 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2833 * DAI should write to or read from. If a bit is set the corresponding slot is
2834 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2835 * the first slot, bit 1 to the second slot and so on. The first active slot
2836 * maps to the first channel of the DAI, the second active slot to the second
2837 * channel and so on.
2838 *
2839 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2840 * @rx_mask and @slot_width will be ignored.
2841 *
2842 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002843 */
2844int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002845 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002846{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002847 if (dai->driver->ops->xlate_tdm_slot_mask)
Xiubo Lie5c21512014-03-21 14:17:12 +08002848 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002849 &tx_mask, &rx_mask);
2850 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002851 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002852
Benoit Cousson88bd8702014-07-08 23:19:34 +02002853 dai->tx_mask = tx_mask;
2854 dai->rx_mask = rx_mask;
2855
Kuninori Morimoto46471922017-09-25 01:38:34 +00002856 if (dai->driver->ops->set_tdm_slot)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002857 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002858 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002859 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002860 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002861}
2862EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2863
2864/**
Barry Song472df3c2009-09-12 01:16:29 +08002865 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2866 * @dai: DAI
2867 * @tx_num: how many TX channels
2868 * @tx_slot: pointer to an array which imply the TX slot number channel
2869 * 0~num-1 uses
2870 * @rx_num: how many RX channels
2871 * @rx_slot: pointer to an array which imply the RX slot number channel
2872 * 0~num-1 uses
2873 *
2874 * configure the relationship between channel number and TDM slot number.
2875 */
2876int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2877 unsigned int tx_num, unsigned int *tx_slot,
2878 unsigned int rx_num, unsigned int *rx_slot)
2879{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002880 if (dai->driver->ops->set_channel_map)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002881 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002882 rx_num, rx_slot);
2883 else
2884 return -EINVAL;
2885}
2886EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2887
2888/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002889 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2890 * @dai: DAI
2891 * @tristate: tristate enable
2892 *
2893 * Tristates the DAI so that others can use it.
2894 */
2895int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2896{
Kuninori Morimoto46471922017-09-25 01:38:34 +00002897 if (dai->driver->ops->set_tristate)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002898 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002899 else
2900 return -EINVAL;
2901}
2902EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2903
2904/**
2905 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2906 * @dai: DAI
2907 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002908 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002909 *
2910 * Mutes the DAI DAC.
2911 */
Mark Brownda183962013-02-06 15:44:07 +00002912int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2913 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002914{
Mark Brownda183962013-02-06 15:44:07 +00002915 if (!dai->driver)
2916 return -ENOTSUPP;
2917
2918 if (dai->driver->ops->mute_stream)
2919 return dai->driver->ops->mute_stream(dai, mute, direction);
2920 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2921 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002922 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002923 else
Mark Brown04570c62012-04-13 19:16:03 +01002924 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002925}
2926EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2927
Mark Brownc5af3a22008-11-28 13:29:45 +00002928/**
2929 * snd_soc_register_card - Register a card with the ASoC core
2930 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002931 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002932 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002933 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302934int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002935{
Mengdong Lin923c5e612015-11-23 11:01:49 -05002936 int i, ret;
Mengdong Lin1a497982015-11-18 02:34:11 -05002937 struct snd_soc_pcm_runtime *rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002938
Mark Brownc5af3a22008-11-28 13:29:45 +00002939 if (!card->name || !card->dev)
2940 return -EINVAL;
2941
Stephen Warren5a504962011-12-21 10:40:59 -07002942 for (i = 0; i < card->num_links; i++) {
2943 struct snd_soc_dai_link *link = &card->dai_link[i];
2944
Mengdong Lin923c5e612015-11-23 11:01:49 -05002945 ret = soc_init_dai_link(card, link);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002946 if (ret) {
Mengdong Lin923c5e612015-11-23 11:01:49 -05002947 dev_err(card->dev, "ASoC: failed to init link %s\n",
2948 link->name);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002949 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002950 }
Stephen Warren5a504962011-12-21 10:40:59 -07002951 }
2952
Mark Browned77cc12011-05-03 18:25:34 +01002953 dev_set_drvdata(card->dev, card);
2954
Stephen Warren111c6412011-01-28 14:26:35 -07002955 snd_soc_initialize_card_lists(card);
2956
Mengdong Linf8f80362015-12-02 14:11:22 +08002957 INIT_LIST_HEAD(&card->dai_link_list);
2958 card->num_dai_links = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002959
Mengdong Lin1a497982015-11-18 02:34:11 -05002960 INIT_LIST_HEAD(&card->rtd_list);
Mark Brown91151712008-11-30 23:31:24 +00002961 card->num_rtd = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002962
Mark Browndb432b42011-10-03 21:06:40 +01002963 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002964 INIT_LIST_HEAD(&card->dobj_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002965 card->instantiated = 0;
2966 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002967 mutex_init(&card->dapm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002968
Mark Brownb19e6e72012-03-14 21:18:39 +00002969 ret = snd_soc_instantiate_card(card);
2970 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002971 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002972
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002973 /* deactivate pins to sleep state */
Mengdong Lin1a497982015-11-18 02:34:11 -05002974 list_for_each_entry(rtd, &card->rtd_list, list) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002975 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2976 int j;
2977
2978 for (j = 0; j < rtd->num_codecs; j++) {
2979 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2980 if (!codec_dai->active)
2981 pinctrl_pm_select_sleep_state(codec_dai->dev);
2982 }
2983
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002984 if (!cpu_dai->active)
2985 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2986 }
2987
Mark Brownb19e6e72012-03-14 21:18:39 +00002988 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002989}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302990EXPORT_SYMBOL_GPL(snd_soc_register_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002991
2992/**
2993 * snd_soc_unregister_card - Unregister a card with the ASoC core
2994 *
2995 * @card: Card to unregister
2996 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002997 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302998int snd_soc_unregister_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002999{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003000 if (card->instantiated) {
3001 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02003002 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05303003 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00003004 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003005 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003006
3007 return 0;
3008}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303009EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003010
3011/*
3012 * Simplify DAI link configuration by removing ".-1" from device names
3013 * and sanitizing names.
3014 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003015static char *fmt_single_name(struct device *dev, int *id)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003016{
3017 char *found, name[NAME_SIZE];
3018 int id1, id2;
3019
3020 if (dev_name(dev) == NULL)
3021 return NULL;
3022
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003023 strlcpy(name, dev_name(dev), NAME_SIZE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003024
3025 /* are we a "%s.%d" name (platform and SPI components) */
Mark Brownc5af3a22008-11-28 13:29:45 +00003026 found = strstr(name, dev->driver->name);
3027 if (found) {
3028 /* get ID */
3029 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3030
3031 /* discard ID from name if ID == -1 */
3032 if (*id == -1)
3033 found[strlen(dev->driver->name)] = '\0';
3034 }
3035
3036 } else {
3037 /* I2C component devices are named "bus-addr" */
3038 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3039 char tmp[NAME_SIZE];
3040
3041 /* create unique ID number from I2C addr and bus */
3042 *id = ((id1 & 0xffff) << 16) + id2;
3043
3044 /* sanitize component name for DAI link creation */
3045 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003046 strlcpy(name, tmp, NAME_SIZE);
Mark Brownc5af3a22008-11-28 13:29:45 +00003047 } else
3048 *id = 0;
3049 }
3050
3051 return kstrdup(name, GFP_KERNEL);
3052}
3053
3054/*
3055 * Simplify DAI link naming for single devices with multiple DAIs by removing
3056 * any ".-1" and using the DAI name (instead of device name).
3057 */
3058static inline char *fmt_multiple_name(struct device *dev,
3059 struct snd_soc_dai_driver *dai_drv)
3060{
3061 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003062 dev_err(dev,
3063 "ASoC: error - multiple DAI %s registered with no name\n",
3064 dev_name(dev));
Mark Brownc5af3a22008-11-28 13:29:45 +00003065 return NULL;
3066 }
3067
3068 return kstrdup(dai_drv->name, GFP_KERNEL);
3069}
3070
3071/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003072 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003073 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003074 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003075 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003076static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003077{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003078 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003079
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003080 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003081 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3082 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003083 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003084 kfree(dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003085 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003086 }
Mark Brown91151712008-11-30 23:31:24 +00003087}
Mark Brown91151712008-11-30 23:31:24 +00003088
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003089/* Create a DAI and add it to the component's DAI list */
3090static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
3091 struct snd_soc_dai_driver *dai_drv,
3092 bool legacy_dai_naming)
3093{
3094 struct device *dev = component->dev;
3095 struct snd_soc_dai *dai;
3096
3097 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
3098
3099 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3100 if (dai == NULL)
3101 return NULL;
3102
3103 /*
3104 * Back in the old days when we still had component-less DAIs,
3105 * instead of having a static name, component-less DAIs would
3106 * inherit the name of the parent device so it is possible to
3107 * register multiple instances of the DAI. We still need to keep
3108 * the same naming style even though those DAIs are not
3109 * component-less anymore.
3110 */
3111 if (legacy_dai_naming &&
3112 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3113 dai->name = fmt_single_name(dev, &dai->id);
3114 } else {
3115 dai->name = fmt_multiple_name(dev, dai_drv);
3116 if (dai_drv->id)
3117 dai->id = dai_drv->id;
3118 else
3119 dai->id = component->num_dai;
3120 }
3121 if (dai->name == NULL) {
3122 kfree(dai);
3123 return NULL;
3124 }
3125
3126 dai->component = component;
3127 dai->dev = dev;
3128 dai->driver = dai_drv;
3129 if (!dai->driver->ops)
3130 dai->driver->ops = &null_dai_ops;
3131
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00003132 list_add_tail(&dai->list, &component->dai_list);
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003133 component->num_dai++;
3134
3135 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3136 return dai;
3137}
3138
Mark Brown91151712008-11-30 23:31:24 +00003139/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003140 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003141 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003142 * @component: The component the DAIs are registered for
3143 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003144 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003145 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3146 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003147 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003148static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003149 struct snd_soc_dai_driver *dai_drv, size_t count,
3150 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003151{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003152 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003153 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003154 unsigned int i;
3155 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003156
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003157 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003158
3159 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003160
Mengdong Lin5e4fb372015-12-31 16:40:20 +08003161 dai = soc_add_dai(component, dai_drv + i,
3162 count == 1 && legacy_dai_naming);
Axel Linc46e0072010-11-03 15:04:45 +08003163 if (dai == NULL) {
3164 ret = -ENOMEM;
3165 goto err;
3166 }
Mark Brown91151712008-11-30 23:31:24 +00003167 }
3168
3169 return 0;
3170
3171err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003172 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003173
3174 return ret;
3175}
Mark Brown91151712008-11-30 23:31:24 +00003176
Mengdong Lin68003e62015-12-31 16:40:43 +08003177/**
3178 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3179 *
3180 * @component: The component the DAIs are registered for
3181 * @dai_drv: DAI driver to use for the DAI
3182 *
3183 * Topology can use this API to register DAIs when probing a component.
3184 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3185 * will be freed in the component cleanup.
3186 */
3187int snd_soc_register_dai(struct snd_soc_component *component,
3188 struct snd_soc_dai_driver *dai_drv)
3189{
3190 struct snd_soc_dapm_context *dapm =
3191 snd_soc_component_get_dapm(component);
3192 struct snd_soc_dai *dai;
3193 int ret;
3194
3195 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3196 dev_err(component->dev, "Invalid dai type %d\n",
3197 dai_drv->dobj.type);
3198 return -EINVAL;
3199 }
3200
3201 lockdep_assert_held(&client_mutex);
3202 dai = soc_add_dai(component, dai_drv, false);
3203 if (!dai)
3204 return -ENOMEM;
3205
3206 /* Create the DAI widgets here. After adding DAIs, topology may
3207 * also add routes that need these widgets as source or sink.
3208 */
3209 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3210 if (ret != 0) {
3211 dev_err(component->dev,
3212 "Failed to create DAI widgets %d\n", ret);
3213 }
3214
3215 return ret;
3216}
3217EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3218
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003219static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3220 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003221{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003222 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003223
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003224 component->driver->seq_notifier(component, type, subseq);
3225}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003226
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003227static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3228 int event)
3229{
3230 struct snd_soc_component *component = dapm->component;
3231
3232 return component->driver->stream_event(component, event);
3233}
3234
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003235static int snd_soc_component_drv_pcm_new(struct snd_soc_component *component,
3236 struct snd_soc_pcm_runtime *rtd)
3237{
3238 if (component->driver->pcm_new)
3239 return component->driver->pcm_new(rtd);
3240
3241 return 0;
3242}
3243
3244static void snd_soc_component_drv_pcm_free(struct snd_soc_component *component,
3245 struct snd_pcm *pcm)
3246{
3247 if (component->driver->pcm_free)
3248 component->driver->pcm_free(pcm);
3249}
3250
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003251static int snd_soc_component_set_bias_level(struct snd_soc_dapm_context *dapm,
3252 enum snd_soc_bias_level level)
3253{
3254 struct snd_soc_component *component = dapm->component;
3255
3256 return component->driver->set_bias_level(component, level);
3257}
3258
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003259static int snd_soc_component_initialize(struct snd_soc_component *component,
3260 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003261{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003262 struct snd_soc_dapm_context *dapm;
3263
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003264 component->name = fmt_single_name(dev, &component->id);
3265 if (!component->name) {
3266 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003267 return -ENOMEM;
3268 }
3269
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003270 component->dev = dev;
3271 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003272 component->probe = component->driver->probe;
3273 component->remove = component->driver->remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003274 component->suspend = component->driver->suspend;
3275 component->resume = component->driver->resume;
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003276 component->set_sysclk = component->driver->set_sysclk;
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003277 component->set_pll = component->driver->set_pll;
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003278 component->set_jack = component->driver->set_jack;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003279 component->pcm_new = snd_soc_component_drv_pcm_new;
3280 component->pcm_free = snd_soc_component_drv_pcm_free;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003281
Kuninori Morimoto88c27462017-08-25 01:06:04 +00003282 dapm = snd_soc_component_get_dapm(component);
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003283 dapm->dev = dev;
3284 dapm->component = component;
3285 dapm->bias_level = SND_SOC_BIAS_OFF;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003286 dapm->idle_bias_off = !driver->idle_bias_on;
3287 dapm->suspend_bias_off = driver->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003288 if (driver->seq_notifier)
3289 dapm->seq_notifier = snd_soc_component_seq_notifier;
3290 if (driver->stream_event)
3291 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimoto7ba236c2017-09-26 01:01:10 +00003292 if (driver->set_bias_level)
3293 dapm->set_bias_level = snd_soc_component_set_bias_level;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003294
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003295 INIT_LIST_HEAD(&component->dai_list);
3296 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003297
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003298 return 0;
3299}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003300
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003301static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003302{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003303 int val_bytes = regmap_get_val_bytes(component->regmap);
3304
3305 /* Errors are legitimate for non-integer byte multiples */
3306 if (val_bytes > 0)
3307 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003308}
3309
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003310#ifdef CONFIG_REGMAP
3311
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003312/**
3313 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3314 * @component: The component for which to initialize the regmap instance
3315 * @regmap: The regmap instance that should be used by the component
3316 *
3317 * This function allows deferred assignment of the regmap instance that is
3318 * associated with the component. Only use this if the regmap instance is not
3319 * yet ready when the component is registered. The function must also be called
3320 * before the first IO attempt of the component.
3321 */
3322void snd_soc_component_init_regmap(struct snd_soc_component *component,
3323 struct regmap *regmap)
3324{
3325 component->regmap = regmap;
3326 snd_soc_component_setup_regmap(component);
3327}
3328EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3329
3330/**
3331 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3332 * @component: The component for which to de-initialize the regmap instance
3333 *
3334 * Calls regmap_exit() on the regmap instance associated to the component and
3335 * removes the regmap instance from the component.
3336 *
3337 * This function should only be used if snd_soc_component_init_regmap() was used
3338 * to initialize the regmap instance.
3339 */
3340void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3341{
3342 regmap_exit(component->regmap);
3343 component->regmap = NULL;
3344}
3345EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3346
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003347#endif
3348
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003349static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3350{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003351 if (!component->write && !component->read) {
3352 if (!component->regmap)
3353 component->regmap = dev_get_regmap(component->dev, NULL);
3354 if (component->regmap)
3355 snd_soc_component_setup_regmap(component);
3356 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003357
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003358 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01003359 INIT_LIST_HEAD(&component->dobj_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003360}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003361
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003362static void snd_soc_component_add(struct snd_soc_component *component)
3363{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003364 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003365 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003366 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003367}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003368
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003369static void snd_soc_component_cleanup(struct snd_soc_component *component)
3370{
3371 snd_soc_unregister_dais(component);
3372 kfree(component->name);
3373}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003374
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003375static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3376{
Kuninori Morimotoc12c1aa2017-04-03 06:31:22 +00003377 struct snd_soc_card *card = component->card;
3378
3379 if (card)
3380 snd_soc_unregister_card(card);
3381
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003382 list_del(&component->list);
3383}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003384
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003385#define ENDIANNESS_MAP(name) \
3386 (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
3387static u64 endianness_format_map[] = {
3388 ENDIANNESS_MAP(S16_),
3389 ENDIANNESS_MAP(U16_),
3390 ENDIANNESS_MAP(S24_),
3391 ENDIANNESS_MAP(U24_),
3392 ENDIANNESS_MAP(S32_),
3393 ENDIANNESS_MAP(U32_),
3394 ENDIANNESS_MAP(S24_3),
3395 ENDIANNESS_MAP(U24_3),
3396 ENDIANNESS_MAP(S20_3),
3397 ENDIANNESS_MAP(U20_3),
3398 ENDIANNESS_MAP(S18_3),
3399 ENDIANNESS_MAP(U18_3),
3400 ENDIANNESS_MAP(FLOAT_),
3401 ENDIANNESS_MAP(FLOAT64_),
3402 ENDIANNESS_MAP(IEC958_SUBFRAME_),
3403};
3404
3405/*
3406 * Fix up the DAI formats for endianness: codecs don't actually see
3407 * the endianness of the data but we're using the CPU format
3408 * definitions which do need to include endianness so we ensure that
3409 * codec DAIs always have both big and little endian variants set.
3410 */
3411static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
3412{
3413 int i;
3414
3415 for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
3416 if (stream->formats & endianness_format_map[i])
3417 stream->formats |= endianness_format_map[i];
3418}
3419
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003420int snd_soc_add_component(struct device *dev,
3421 struct snd_soc_component *component,
3422 const struct snd_soc_component_driver *component_driver,
3423 struct snd_soc_dai_driver *dai_drv,
3424 int num_dai)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003425{
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003426 int ret;
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003427 int i;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003428
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003429 ret = snd_soc_component_initialize(component, component_driver, dev);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003430 if (ret)
3431 goto err_free;
3432
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003433 component->ignore_pmdown_time = true;
3434 component->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003435
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003436 if (component_driver->endianness) {
3437 for (i = 0; i < num_dai; i++) {
3438 convert_endianness_formats(&dai_drv[i].playback);
3439 convert_endianness_formats(&dai_drv[i].capture);
3440 }
3441 }
3442
Kuninori Morimoto69941bab2017-10-11 01:38:51 +00003443 ret = snd_soc_register_dais(component, dai_drv, num_dai,
3444 !component_driver->non_legacy_dai_naming);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003445 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003446 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003447 goto err_cleanup;
3448 }
3449
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003450 snd_soc_component_add(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003451
3452 return 0;
3453
3454err_cleanup:
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003455 snd_soc_component_cleanup(component);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003456err_free:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003457 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003458}
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003459EXPORT_SYMBOL_GPL(snd_soc_add_component);
3460
3461int snd_soc_register_component(struct device *dev,
3462 const struct snd_soc_component_driver *component_driver,
3463 struct snd_soc_dai_driver *dai_drv,
3464 int num_dai)
3465{
3466 struct snd_soc_component *component;
3467
Kuninori Morimoto7ecbd6a2018-03-19 07:27:17 +00003468 component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
Kuninori Morimoto08e61d02017-10-02 05:10:33 +00003469 if (!component)
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003470 return -ENOMEM;
Kuninori Morimotoe0dac412017-10-02 05:10:17 +00003471
3472 return snd_soc_add_component(dev, component, component_driver,
3473 dai_drv, num_dai);
3474}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003475EXPORT_SYMBOL_GPL(snd_soc_register_component);
3476
3477/**
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003478 * snd_soc_unregister_component - Unregister all related component
3479 * from the ASoC core
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003480 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003481 * @dev: The device to unregister
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003482 */
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003483static int __snd_soc_unregister_component(struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003484{
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003485 struct snd_soc_component *component;
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003486 int found = 0;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003487
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003488 mutex_lock(&client_mutex);
Kuninori Morimotocf9e8292017-08-07 02:06:23 +00003489 list_for_each_entry(component, &component_list, list) {
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003490 if (dev != component->dev ||
3491 !component->registered_as_component)
3492 continue;
3493
3494 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3495 snd_soc_component_del_unlocked(component);
3496 found = 1;
3497 break;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003498 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003499 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003500
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003501 if (found) {
3502 snd_soc_component_cleanup(component);
Kuninori Morimoto21a035282017-08-07 02:06:40 +00003503 }
Kuninori Morimoto2eccea82017-08-07 02:06:55 +00003504
3505 return found;
3506}
3507
3508void snd_soc_unregister_component(struct device *dev)
3509{
3510 while (__snd_soc_unregister_component(dev));
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003511}
3512EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3513
Kuninori Morimoto7dd5d0d2017-10-02 05:09:52 +00003514struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
3515 const char *driver_name)
3516{
3517 struct snd_soc_component *component;
3518 struct snd_soc_component *ret;
3519
3520 ret = NULL;
3521 mutex_lock(&client_mutex);
3522 list_for_each_entry(component, &component_list, list) {
3523 if (dev != component->dev)
3524 continue;
3525
3526 if (driver_name &&
3527 (driver_name != component->driver->name) &&
3528 (strcmp(component->driver->name, driver_name) != 0))
3529 continue;
3530
3531 ret = component;
3532 break;
3533 }
3534 mutex_unlock(&client_mutex);
3535
3536 return ret;
3537}
3538EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
3539
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003540static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003541{
3542 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3543
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003544 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003545}
3546
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003547static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003548{
3549 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3550
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003551 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003552}
3553
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003554static int snd_soc_platform_drv_pcm_new(struct snd_soc_component *component,
3555 struct snd_soc_pcm_runtime *rtd)
3556{
3557 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3558
3559 if (platform->driver->pcm_new)
3560 return platform->driver->pcm_new(rtd);
3561
3562 return 0;
3563}
3564
3565static void snd_soc_platform_drv_pcm_free(struct snd_soc_component *component,
3566 struct snd_pcm *pcm)
3567{
3568 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3569
3570 if (platform->driver->pcm_free)
3571 platform->driver->pcm_free(pcm);
3572}
3573
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003574/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003575 * snd_soc_add_platform - Add a platform to the ASoC core
3576 * @dev: The parent device for the platform
3577 * @platform: The platform to add
Masanari Iida7d1442b2015-07-15 23:02:39 +09003578 * @platform_drv: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003579 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003580int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003581 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003582{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003583 int ret;
3584
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003585 ret = snd_soc_component_initialize(&platform->component,
3586 &platform_drv->component_driver, dev);
3587 if (ret)
3588 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003589
3590 platform->dev = dev;
3591 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003592
3593 if (platform_drv->probe)
3594 platform->component.probe = snd_soc_platform_drv_probe;
3595 if (platform_drv->remove)
3596 platform->component.remove = snd_soc_platform_drv_remove;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003597 if (platform_drv->pcm_new)
3598 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3599 if (platform_drv->pcm_free)
3600 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003601
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003602#ifdef CONFIG_DEBUG_FS
3603 platform->component.debugfs_prefix = "platform";
3604#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00003605
3606 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003607 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003608 list_add(&platform->list, &platform_list);
3609 mutex_unlock(&client_mutex);
3610
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003611 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3612 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003613
3614 return 0;
3615}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003616EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3617
3618/**
3619 * snd_soc_register_platform - Register a platform with the ASoC core
3620 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003621 * @dev: The device for the platform
3622 * @platform_drv: The driver for the platform
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003623 */
3624int snd_soc_register_platform(struct device *dev,
3625 const struct snd_soc_platform_driver *platform_drv)
3626{
3627 struct snd_soc_platform *platform;
3628 int ret;
3629
3630 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3631
3632 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3633 if (platform == NULL)
3634 return -ENOMEM;
3635
3636 ret = snd_soc_add_platform(dev, platform, platform_drv);
3637 if (ret)
3638 kfree(platform);
3639
3640 return ret;
3641}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003642EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3643
3644/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003645 * snd_soc_remove_platform - Remove a platform from the ASoC core
3646 * @platform: the platform to remove
3647 */
3648void snd_soc_remove_platform(struct snd_soc_platform *platform)
3649{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003650
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003651 mutex_lock(&client_mutex);
3652 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003653 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003654 mutex_unlock(&client_mutex);
3655
3656 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003657 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02003658
3659 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003660}
3661EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3662
3663struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3664{
3665 struct snd_soc_platform *platform;
3666
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003667 mutex_lock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003668 list_for_each_entry(platform, &platform_list, list) {
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003669 if (dev == platform->dev) {
3670 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003671 return platform;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003672 }
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003673 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003674 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003675
3676 return NULL;
3677}
3678EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3679
3680/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003681 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3682 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003683 * @dev: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003684 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003685void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003686{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003687 struct snd_soc_platform *platform;
3688
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003689 platform = snd_soc_lookup_platform(dev);
3690 if (!platform)
3691 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003692
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003693 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003694 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003695}
3696EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3697
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003698static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3699{
3700 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3701
3702 return codec->driver->probe(codec);
3703}
3704
3705static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3706{
3707 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3708
3709 codec->driver->remove(codec);
3710}
3711
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003712static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3713{
3714 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3715
3716 return codec->driver->suspend(codec);
3717}
3718
3719static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3720{
3721 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3722
3723 return codec->driver->resume(codec);
3724}
3725
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003726static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3727 unsigned int reg, unsigned int val)
3728{
3729 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3730
3731 return codec->driver->write(codec, reg, val);
3732}
3733
3734static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3735 unsigned int reg, unsigned int *val)
3736{
3737 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3738
3739 *val = codec->driver->read(codec, reg);
3740
3741 return 0;
3742}
3743
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003744static int snd_soc_codec_set_sysclk_(struct snd_soc_component *component,
3745 int clk_id, int source, unsigned int freq, int dir)
3746{
3747 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3748
3749 return snd_soc_codec_set_sysclk(codec, clk_id, source, freq, dir);
3750}
3751
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003752static int snd_soc_codec_set_pll_(struct snd_soc_component *component,
3753 int pll_id, int source, unsigned int freq_in,
3754 unsigned int freq_out)
3755{
3756 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3757
3758 return snd_soc_codec_set_pll(codec, pll_id, source, freq_in, freq_out);
3759}
3760
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003761static int snd_soc_codec_set_jack_(struct snd_soc_component *component,
3762 struct snd_soc_jack *jack, void *data)
3763{
3764 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3765
3766 return snd_soc_codec_set_jack(codec, jack, data);
3767}
3768
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003769static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3770 enum snd_soc_bias_level level)
3771{
3772 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3773
3774 return codec->driver->set_bias_level(codec, level);
3775}
3776
Mark Brown0d0cf002008-12-10 14:32:45 +00003777/**
3778 * snd_soc_register_codec - Register a codec with the ASoC core
3779 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003780 * @dev: The parent device for this codec
3781 * @codec_drv: Codec driver
3782 * @dai_drv: The associated DAI driver
3783 * @num_dai: Number of DAIs
Mark Brown0d0cf002008-12-10 14:32:45 +00003784 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003785int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003786 const struct snd_soc_codec_driver *codec_drv,
3787 struct snd_soc_dai_driver *dai_drv,
3788 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003789{
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003790 struct snd_soc_dapm_context *dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003791 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003792 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003794
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3798 if (codec == NULL)
3799 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003800
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003801 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003802
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003803 ret = snd_soc_component_initialize(&codec->component,
3804 &codec_drv->component_driver, dev);
3805 if (ret)
3806 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003807
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003808 if (codec_drv->probe)
3809 codec->component.probe = snd_soc_codec_drv_probe;
3810 if (codec_drv->remove)
3811 codec->component.remove = snd_soc_codec_drv_remove;
Kuninori Morimoto9178feb2016-11-30 06:23:13 +00003812 if (codec_drv->suspend)
3813 codec->component.suspend = snd_soc_codec_drv_suspend;
3814 if (codec_drv->resume)
3815 codec->component.resume = snd_soc_codec_drv_resume;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003816 if (codec_drv->write)
3817 codec->component.write = snd_soc_codec_drv_write;
3818 if (codec_drv->read)
3819 codec->component.read = snd_soc_codec_drv_read;
Kuninori Morimoto71ccef02017-08-24 00:57:35 +00003820 if (codec_drv->set_sysclk)
3821 codec->component.set_sysclk = snd_soc_codec_set_sysclk_;
Kuninori Morimotoef641e52017-08-24 00:57:51 +00003822 if (codec_drv->set_pll)
3823 codec->component.set_pll = snd_soc_codec_set_pll_;
Kuninori Morimoto44c07362017-08-24 00:58:07 +00003824 if (codec_drv->set_jack)
3825 codec->component.set_jack = snd_soc_codec_set_jack_;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003826 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003827
3828 dapm = snd_soc_codec_get_dapm(codec);
3829 dapm->idle_bias_off = codec_drv->idle_bias_off;
3830 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003831 if (codec_drv->seq_notifier)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003832 dapm->seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003833 if (codec_drv->set_bias_level)
Lars-Peter Clausen48901402015-07-06 15:38:11 +02003834 dapm->set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003835 codec->dev = dev;
3836 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003837 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003839#ifdef CONFIG_DEBUG_FS
3840 codec->component.init_debugfs = soc_init_codec_debugfs;
3841 codec->component.debugfs_prefix = "codec";
3842#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003843
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003844 if (codec_drv->get_regmap)
3845 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003846
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003847 for (i = 0; i < num_dai; i++) {
Kuninori Morimoto273d7782017-10-11 01:38:29 +00003848 convert_endianness_formats(&dai_drv[i].playback);
3849 convert_endianness_formats(&dai_drv[i].capture);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003850 }
3851
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003852 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3853 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003854 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003855 goto err_cleanup;
3856 }
3857
3858 list_for_each_entry(dai, &codec->component.dai_list, list)
3859 dai->codec = codec;
3860
Mark Brown054880f2012-04-09 17:29:19 +01003861 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003862 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003863 list_add(&codec->list, &codec_list);
3864 mutex_unlock(&client_mutex);
3865
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003866 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3867 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003868 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003869
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003870err_cleanup:
3871 snd_soc_component_cleanup(&codec->component);
3872err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003873 kfree(codec);
3874 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003875}
3876EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3877
3878/**
3879 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3880 *
Jonathan Corbet628536e2015-08-25 01:14:48 -06003881 * @dev: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003882 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003884{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003885 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003887 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003888 list_for_each_entry(codec, &codec_list, list) {
3889 if (dev == codec->dev)
3890 goto found;
3891 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003892 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003893 return;
3894
3895found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003896 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003897 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003898 mutex_unlock(&client_mutex);
3899
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003900 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3901 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003903 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003904 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003905 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003906}
3907EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3908
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003909/* Retrieve a card's name from device tree */
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003910int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3911 const char *propname)
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003912{
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003913 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003914 int ret;
3915
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303916 if (!card->dev) {
3917 pr_err("card->dev is not set before calling %s\n", __func__);
3918 return -EINVAL;
3919 }
3920
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003921 np = card->dev->of_node;
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303922
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003923 ret = of_property_read_string_index(np, propname, 0, &card->name);
3924 /*
3925 * EINVAL means the property does not exist. This is fine providing
3926 * card->name was previously set, which is checked later in
3927 * snd_soc_register_card.
3928 */
3929 if (ret < 0 && ret != -EINVAL) {
3930 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003931 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003932 propname, ret);
3933 return ret;
3934 }
3935
3936 return 0;
3937}
Kuninori Morimotob07609ce2017-01-27 06:37:51 +00003938EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003939
Xiubo Li9a6d4862014-02-08 15:59:52 +08003940static const struct snd_soc_dapm_widget simple_widgets[] = {
3941 SND_SOC_DAPM_MIC("Microphone", NULL),
3942 SND_SOC_DAPM_LINE("Line", NULL),
3943 SND_SOC_DAPM_HP("Headphone", NULL),
3944 SND_SOC_DAPM_SPK("Speaker", NULL),
3945};
3946
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003947int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
Xiubo Li9a6d4862014-02-08 15:59:52 +08003948 const char *propname)
3949{
Kuninori Morimoto21efde52017-01-27 06:37:34 +00003950 struct device_node *np = card->dev->of_node;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003951 struct snd_soc_dapm_widget *widgets;
3952 const char *template, *wname;
3953 int i, j, num_widgets, ret;
3954
3955 num_widgets = of_property_count_strings(np, propname);
3956 if (num_widgets < 0) {
3957 dev_err(card->dev,
3958 "ASoC: Property '%s' does not exist\n", propname);
3959 return -EINVAL;
3960 }
3961 if (num_widgets & 1) {
3962 dev_err(card->dev,
3963 "ASoC: Property '%s' length is not even\n", propname);
3964 return -EINVAL;
3965 }
3966
3967 num_widgets /= 2;
3968 if (!num_widgets) {
3969 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3970 propname);
3971 return -EINVAL;
3972 }
3973
3974 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3975 GFP_KERNEL);
3976 if (!widgets) {
3977 dev_err(card->dev,
3978 "ASoC: Could not allocate memory for widgets\n");
3979 return -ENOMEM;
3980 }
3981
3982 for (i = 0; i < num_widgets; i++) {
3983 ret = of_property_read_string_index(np, propname,
3984 2 * i, &template);
3985 if (ret) {
3986 dev_err(card->dev,
3987 "ASoC: Property '%s' index %d read error:%d\n",
3988 propname, 2 * i, ret);
3989 return -EINVAL;
3990 }
3991
3992 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3993 if (!strncmp(template, simple_widgets[j].name,
3994 strlen(simple_widgets[j].name))) {
3995 widgets[i] = simple_widgets[j];
3996 break;
3997 }
3998 }
3999
4000 if (j >= ARRAY_SIZE(simple_widgets)) {
4001 dev_err(card->dev,
4002 "ASoC: DAPM widget '%s' is not supported\n",
4003 template);
4004 return -EINVAL;
4005 }
4006
4007 ret = of_property_read_string_index(np, propname,
4008 (2 * i) + 1,
4009 &wname);
4010 if (ret) {
4011 dev_err(card->dev,
4012 "ASoC: Property '%s' index %d read error:%d\n",
4013 propname, (2 * i) + 1, ret);
4014 return -EINVAL;
4015 }
4016
4017 widgets[i].name = wname;
4018 }
4019
Nicolin Chenf23e8602015-02-14 17:22:49 -08004020 card->of_dapm_widgets = widgets;
4021 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08004022
4023 return 0;
4024}
Kuninori Morimoto21efde52017-01-27 06:37:34 +00004025EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
Xiubo Li9a6d4862014-02-08 15:59:52 +08004026
Jyri Sarha61310842015-09-09 21:27:43 +03004027static int snd_soc_of_get_slot_mask(struct device_node *np,
4028 const char *prop_name,
4029 unsigned int *mask)
4030{
4031 u32 val;
Jyri Sarha6c84e5912015-09-17 13:13:38 +03004032 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
Jyri Sarha61310842015-09-09 21:27:43 +03004033 int i;
4034
4035 if (!of_slot_mask)
4036 return 0;
4037 val /= sizeof(u32);
4038 for (i = 0; i < val; i++)
4039 if (be32_to_cpup(&of_slot_mask[i]))
4040 *mask |= (1 << i);
4041
4042 return val;
4043}
4044
Xiubo Li89c67852014-02-14 09:34:35 +08004045int snd_soc_of_parse_tdm_slot(struct device_node *np,
Jyri Sarha61310842015-09-09 21:27:43 +03004046 unsigned int *tx_mask,
4047 unsigned int *rx_mask,
Xiubo Li89c67852014-02-14 09:34:35 +08004048 unsigned int *slots,
4049 unsigned int *slot_width)
4050{
4051 u32 val;
4052 int ret;
4053
Jyri Sarha61310842015-09-09 21:27:43 +03004054 if (tx_mask)
4055 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
4056 if (rx_mask)
4057 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
4058
Xiubo Li89c67852014-02-14 09:34:35 +08004059 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4060 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4061 if (ret)
4062 return ret;
4063
4064 if (slots)
4065 *slots = val;
4066 }
4067
4068 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4069 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4070 if (ret)
4071 return ret;
4072
4073 if (slot_width)
4074 *slot_width = val;
4075 }
4076
4077 return 0;
4078}
4079EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4080
Kuninori Morimoto440a3002017-01-27 06:37:16 +00004081void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00004082 struct snd_soc_codec_conf *codec_conf,
4083 struct device_node *of_node,
4084 const char *propname)
4085{
Kuninori Morimoto440a3002017-01-27 06:37:16 +00004086 struct device_node *np = card->dev->of_node;
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00004087 const char *str;
4088 int ret;
4089
4090 ret = of_property_read_string(np, propname, &str);
4091 if (ret < 0) {
4092 /* no prefix is not error */
4093 return;
4094 }
4095
4096 codec_conf->of_node = of_node;
4097 codec_conf->name_prefix = str;
4098}
Kuninori Morimoto440a3002017-01-27 06:37:16 +00004099EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
Kuninori Morimoto5e3cdaa2015-07-15 07:07:42 +00004100
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00004101int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004102 const char *propname)
4103{
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00004104 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00004105 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004106 struct snd_soc_dapm_route *routes;
4107 int i, ret;
4108
4109 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004110 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004111 dev_err(card->dev,
4112 "ASoC: Property '%s' does not exist or its length is not even\n",
4113 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004114 return -EINVAL;
4115 }
4116 num_routes /= 2;
4117 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004118 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004119 propname);
4120 return -EINVAL;
4121 }
4122
Mark Browne3b1e6a2014-12-18 11:46:38 +00004123 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004124 GFP_KERNEL);
4125 if (!routes) {
4126 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004127 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004128 return -EINVAL;
4129 }
4130
4131 for (i = 0; i < num_routes; i++) {
4132 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00004133 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004134 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004135 dev_err(card->dev,
4136 "ASoC: Property '%s' index %d could not be read: %d\n",
4137 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004138 return -EINVAL;
4139 }
4140 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00004141 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004142 if (ret) {
4143 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004144 "ASoC: Property '%s' index %d could not be read: %d\n",
4145 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004146 return -EINVAL;
4147 }
4148 }
4149
Nicolin Chenf23e8602015-02-14 17:22:49 -08004150 card->num_of_dapm_routes = num_routes;
4151 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004152
4153 return 0;
4154}
Kuninori Morimoto2bc644a2017-01-27 06:36:50 +00004155EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004156
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004157unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004158 const char *prefix,
4159 struct device_node **bitclkmaster,
4160 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004161{
4162 int ret, i;
4163 char prop[128];
4164 unsigned int format = 0;
4165 int bit, frame;
4166 const char *str;
4167 struct {
4168 char *name;
4169 unsigned int val;
4170 } of_fmt_table[] = {
4171 { "i2s", SND_SOC_DAIFMT_I2S },
4172 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4173 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4174 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4175 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4176 { "ac97", SND_SOC_DAIFMT_AC97 },
4177 { "pdm", SND_SOC_DAIFMT_PDM},
4178 { "msb", SND_SOC_DAIFMT_MSB },
4179 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004180 };
4181
4182 if (!prefix)
4183 prefix = "";
4184
4185 /*
Kuninori Morimoto5711c972017-04-20 01:33:24 +00004186 * check "dai-format = xxx"
4187 * or "[prefix]format = xxx"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004188 * SND_SOC_DAIFMT_FORMAT_MASK area
4189 */
Kuninori Morimoto5711c972017-04-20 01:33:24 +00004190 ret = of_property_read_string(np, "dai-format", &str);
4191 if (ret < 0) {
4192 snprintf(prop, sizeof(prop), "%sformat", prefix);
4193 ret = of_property_read_string(np, prop, &str);
4194 }
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004195 if (ret == 0) {
4196 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4197 if (strcmp(str, of_fmt_table[i].name) == 0) {
4198 format |= of_fmt_table[i].val;
4199 break;
4200 }
4201 }
4202 }
4203
4204 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004205 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004206 * SND_SOC_DAIFMT_CLOCK_MASK area
4207 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004208 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
Julia Lawall51930292016-08-05 10:56:51 +02004209 if (of_property_read_bool(np, prop))
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004210 format |= SND_SOC_DAIFMT_CONT;
4211 else
4212 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004213
4214 /*
4215 * check "[prefix]bitclock-inversion"
4216 * check "[prefix]frame-inversion"
4217 * SND_SOC_DAIFMT_INV_MASK area
4218 */
4219 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4220 bit = !!of_get_property(np, prop, NULL);
4221
4222 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4223 frame = !!of_get_property(np, prop, NULL);
4224
4225 switch ((bit << 4) + frame) {
4226 case 0x11:
4227 format |= SND_SOC_DAIFMT_IB_IF;
4228 break;
4229 case 0x10:
4230 format |= SND_SOC_DAIFMT_IB_NF;
4231 break;
4232 case 0x01:
4233 format |= SND_SOC_DAIFMT_NB_IF;
4234 break;
4235 default:
4236 /* SND_SOC_DAIFMT_NB_NF is default */
4237 break;
4238 }
4239
4240 /*
4241 * check "[prefix]bitclock-master"
4242 * check "[prefix]frame-master"
4243 * SND_SOC_DAIFMT_MASTER_MASK area
4244 */
4245 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4246 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004247 if (bit && bitclkmaster)
4248 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004249
4250 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4251 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004252 if (frame && framemaster)
4253 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004254
4255 switch ((bit << 4) + frame) {
4256 case 0x11:
4257 format |= SND_SOC_DAIFMT_CBM_CFM;
4258 break;
4259 case 0x10:
4260 format |= SND_SOC_DAIFMT_CBM_CFS;
4261 break;
4262 case 0x01:
4263 format |= SND_SOC_DAIFMT_CBS_CFM;
4264 break;
4265 default:
4266 format |= SND_SOC_DAIFMT_CBS_CFS;
4267 break;
4268 }
4269
4270 return format;
4271}
4272EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4273
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00004274int snd_soc_get_dai_id(struct device_node *ep)
4275{
4276 struct snd_soc_component *pos;
4277 struct device_node *node;
4278 int ret;
4279
4280 node = of_graph_get_port_parent(ep);
4281
4282 /*
4283 * For example HDMI case, HDMI has video/sound port,
4284 * but ALSA SoC needs sound port number only.
4285 * Thus counting HDMI DT port/endpoint doesn't work.
4286 * Then, it should have .of_xlate_dai_id
4287 */
4288 ret = -ENOTSUPP;
4289 mutex_lock(&client_mutex);
4290 list_for_each_entry(pos, &component_list, list) {
4291 struct device_node *component_of_node = pos->dev->of_node;
4292
4293 if (!component_of_node && pos->dev->parent)
4294 component_of_node = pos->dev->parent->of_node;
4295
4296 if (component_of_node != node)
4297 continue;
4298
4299 if (pos->driver->of_xlate_dai_id)
4300 ret = pos->driver->of_xlate_dai_id(pos, ep);
4301
4302 break;
4303 }
4304 mutex_unlock(&client_mutex);
4305
Tony Lindgrenc0a480d2017-07-28 01:23:15 -07004306 of_node_put(node);
4307
Kuninori Morimotoa180e8b2017-05-18 01:39:25 +00004308 return ret;
4309}
4310EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
4311
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004312int snd_soc_get_dai_name(struct of_phandle_args *args,
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004313 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004314{
4315 struct snd_soc_component *pos;
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004316 struct device_node *component_of_node;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004317 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004318
4319 mutex_lock(&client_mutex);
4320 list_for_each_entry(pos, &component_list, list) {
Jyri Sarha3e0aa8d2015-05-26 21:59:05 +03004321 component_of_node = pos->dev->of_node;
4322 if (!component_of_node && pos->dev->parent)
4323 component_of_node = pos->dev->parent->of_node;
4324
4325 if (component_of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07004326 continue;
4327
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004328 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004329 ret = pos->driver->of_xlate_dai_name(pos,
4330 args,
4331 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004332 } else {
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00004333 struct snd_soc_dai *dai;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004334 int id = -1;
4335
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004336 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004337 case 0:
4338 id = 0; /* same as dai_drv[0] */
4339 break;
4340 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004341 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004342 break;
4343 default:
4344 /* not supported */
4345 break;
4346 }
4347
4348 if (id < 0 || id >= pos->num_dai) {
4349 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004350 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004351 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004352
4353 ret = 0;
4354
Kuninori Morimoto58bf4172017-12-20 01:48:29 +00004355 /* find target DAI */
4356 list_for_each_entry(dai, &pos->dai_list, list) {
4357 if (id == 0)
4358 break;
4359 id--;
4360 }
4361
4362 *dai_name = dai->driver->name;
Xiubo Lie41975e2013-12-20 14:39:51 +08004363 if (!*dai_name)
4364 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004365 }
4366
Kuninori Morimotocb470082013-09-10 17:39:56 -07004367 break;
4368 }
4369 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004370 return ret;
4371}
Kuninori Morimoto1ad8ec52016-11-11 01:19:28 +00004372EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004373
4374int snd_soc_of_get_dai_name(struct device_node *of_node,
4375 const char **dai_name)
4376{
4377 struct of_phandle_args args;
4378 int ret;
4379
4380 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4381 "#sound-dai-cells", 0, &args);
4382 if (ret)
4383 return ret;
4384
4385 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07004386
4387 of_node_put(args.np);
4388
4389 return ret;
4390}
4391EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4392
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01004393/*
4394 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4395 * @dev: Card device
4396 * @of_node: Device node
4397 * @dai_link: DAI link
4398 *
4399 * Builds an array of CODEC DAI components from the DAI link property
4400 * 'sound-dai'.
4401 * The array is set in the DAI link and the number of DAIs is set accordingly.
4402 * The device nodes in the array (of_node) must be dereferenced by the caller.
4403 *
4404 * Returns 0 for success
4405 */
4406int snd_soc_of_get_dai_link_codecs(struct device *dev,
4407 struct device_node *of_node,
4408 struct snd_soc_dai_link *dai_link)
4409{
4410 struct of_phandle_args args;
4411 struct snd_soc_dai_link_component *component;
4412 char *name;
4413 int index, num_codecs, ret;
4414
4415 /* Count the number of CODECs */
4416 name = "sound-dai";
4417 num_codecs = of_count_phandle_with_args(of_node, name,
4418 "#sound-dai-cells");
4419 if (num_codecs <= 0) {
4420 if (num_codecs == -ENOENT)
4421 dev_err(dev, "No 'sound-dai' property\n");
4422 else
4423 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4424 return num_codecs;
4425 }
4426 component = devm_kzalloc(dev,
4427 sizeof *component * num_codecs,
4428 GFP_KERNEL);
4429 if (!component)
4430 return -ENOMEM;
4431 dai_link->codecs = component;
4432 dai_link->num_codecs = num_codecs;
4433
4434 /* Parse the list */
4435 for (index = 0, component = dai_link->codecs;
4436 index < dai_link->num_codecs;
4437 index++, component++) {
4438 ret = of_parse_phandle_with_args(of_node, name,
4439 "#sound-dai-cells",
4440 index, &args);
4441 if (ret)
4442 goto err;
4443 component->of_node = args.np;
4444 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4445 if (ret < 0)
4446 goto err;
4447 }
4448 return 0;
4449err:
4450 for (index = 0, component = dai_link->codecs;
4451 index < dai_link->num_codecs;
4452 index++, component++) {
4453 if (!component->of_node)
4454 break;
4455 of_node_put(component->of_node);
4456 component->of_node = NULL;
4457 }
4458 dai_link->codecs = NULL;
4459 dai_link->num_codecs = 0;
4460 return ret;
4461}
4462EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4463
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004464static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004465{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004466 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01004467 snd_soc_util_init();
4468
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004469 return platform_driver_register(&soc_driver);
4470}
Mark Brown4abe8e12010-10-12 17:41:03 +01004471module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004472
Mark Brown7d8c16a2008-11-30 22:11:24 +00004473static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004474{
Mark Brownfb257892011-04-28 10:57:54 +01004475 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02004476 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01004477
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004478 platform_driver_unregister(&soc_driver);
4479}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004480module_exit(snd_soc_exit);
4481
4482/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004483MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004484MODULE_DESCRIPTION("ALSA SoC Core");
4485MODULE_LICENSE("GPL");
4486MODULE_ALIAS("platform:soc-audio");