blob: 8fafdca5cdf56320453d1879983b04f245f9b0f4 [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>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010043#include <sound/soc-topology.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020044#include <sound/initval.h>
45
Mark Browna8b1d342010-11-03 18:05:58 -040046#define CREATE_TRACE_POINTS
47#include <trace/events/asoc.h>
48
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000049#define NAME_SIZE 32
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000057static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000058static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070059static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000070/* returns the minimum number of bytes needed to represent
71 * a particular given value */
72static int min_bytes_needed(unsigned long val)
73{
74 int c = 0;
75 int i;
76
77 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
78 if (val & (1UL << i))
79 break;
80 c = (sizeof val * 8) - c;
81 if (!c || (c % 8))
82 c = (c + 8) / 8;
83 else
84 c /= 8;
85 return c;
86}
87
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000088/* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90static int format_register_str(struct snd_soc_codec *codec,
91 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000092{
Stephen Warren00b317a2011-04-01 14:50:44 -060093 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
94 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000095 int ret;
96 char tmpbuf[len + 1];
97 char regbuf[regsize + 1];
98
99 /* since tmpbuf is allocated on the stack, warn the callers if they
100 * try to abuse this function */
101 WARN_ON(len > 63);
102
103 /* +2 for ': ' and + 1 for '\n' */
104 if (wordsize + regsize + 2 + 1 != len)
105 return -EINVAL;
106
Mark Brown25032c12011-08-01 13:52:48 +0900107 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000108 if (ret < 0) {
109 memset(regbuf, 'X', regsize);
110 regbuf[regsize] = '\0';
111 } else {
112 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
113 }
114
115 /* prepare the buffer */
116 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
117 /* copy it back to the caller without the '\0' */
118 memcpy(buf, tmpbuf, len);
119
120 return 0;
121}
122
123/* codec register dump */
124static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
125 size_t count, loff_t pos)
126{
127 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000128 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000129 int len;
130 size_t total = 0;
131 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000132
Stephen Warren00b317a2011-04-01 14:50:44 -0600133 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
134 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000135
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000136 len = wordsize + regsize + 2 + 1;
137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000138 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000139 return 0;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 if (codec->driver->reg_cache_step)
142 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100145 /* only support larger than PAGE_SIZE bytes debugfs
146 * entries for the default case */
147 if (p >= pos) {
148 if (total + len >= count - 1)
149 break;
150 format_register_str(codec, i, buf + total, len);
151 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100152 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100153 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000154 }
155
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000156 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000157
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000158 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000159}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000160
Mark Brown2624d5f2009-11-03 21:56:13 +0000161static ssize_t codec_reg_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
Mark Brown36ae1a92012-01-06 17:12:45 -0800164 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
168
169static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
170
Mark Browndbe21402010-02-12 11:37:24 +0000171static ssize_t pmdown_time_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
Mark Brown36ae1a92012-01-06 17:12:45 -0800174 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000175
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000176 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000177}
178
179static ssize_t pmdown_time_set(struct device *dev,
180 struct device_attribute *attr,
181 const char *buf, size_t count)
182{
Mark Brown36ae1a92012-01-06 17:12:45 -0800183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700184 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000185
Jingoo Hanb785a492013-07-19 16:24:59 +0900186 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700187 if (ret)
188 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000189
190 return count;
191}
192
193static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
194
Takashi Iwaid29697d2015-01-30 20:16:37 +0100195static struct attribute *soc_dev_attrs[] = {
196 &dev_attr_codec_reg.attr,
197 &dev_attr_pmdown_time.attr,
198 NULL
199};
200
201static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
202 struct attribute *attr, int idx)
203{
204 struct device *dev = kobj_to_dev(kobj);
205 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
206
207 if (attr == &dev_attr_pmdown_time.attr)
208 return attr->mode; /* always visible */
209 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
210}
211
212static const struct attribute_group soc_dapm_dev_group = {
213 .attrs = soc_dapm_dev_attrs,
214 .is_visible = soc_dev_attr_is_visible,
215};
216
217static const struct attribute_group soc_dev_roup = {
218 .attrs = soc_dev_attrs,
219 .is_visible = soc_dev_attr_is_visible,
220};
221
222static const struct attribute_group *soc_dev_attr_groups[] = {
223 &soc_dapm_dev_group,
224 &soc_dev_roup,
225 NULL
226};
227
Mark Brown2624d5f2009-11-03 21:56:13 +0000228#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000229static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000230 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000231{
232 ssize_t ret;
233 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000234 char *buf;
235
236 if (*ppos < 0 || !count)
237 return -EINVAL;
238
239 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 if (!buf)
241 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000242
243 ret = soc_codec_reg_show(codec, buf, count, *ppos);
244 if (ret >= 0) {
245 if (copy_to_user(user_buf, buf, ret)) {
246 kfree(buf);
247 return -EFAULT;
248 }
249 *ppos += ret;
250 }
251
Mark Brown2624d5f2009-11-03 21:56:13 +0000252 kfree(buf);
253 return ret;
254}
255
256static ssize_t codec_reg_write_file(struct file *file,
257 const char __user *user_buf, size_t count, loff_t *ppos)
258{
259 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700260 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 char *start = buf;
262 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900264 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000265
266 buf_size = min(count, (sizeof(buf)-1));
267 if (copy_from_user(buf, user_buf, buf_size))
268 return -EFAULT;
269 buf[buf_size] = 0;
270
Mark Brown2624d5f2009-11-03 21:56:13 +0000271 while (*start == ' ')
272 start++;
273 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000274 while (*start == ' ')
275 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900276 ret = kstrtoul(start, 16, &value);
277 if (ret)
278 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000279
280 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030281 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000282
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000283 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000284 return buf_size;
285}
286
287static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700288 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 .read = codec_reg_read_file,
290 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200291 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000292};
293
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200294static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100295{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200296 if (!component->card->debugfs_card_root)
297 return;
298
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200299 if (component->debugfs_prefix) {
300 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100301
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200302 name = kasprintf(GFP_KERNEL, "%s:%s",
303 component->debugfs_prefix, component->name);
304 if (name) {
305 component->debugfs_root = debugfs_create_dir(name,
306 component->card->debugfs_card_root);
307 kfree(name);
308 }
309 } else {
310 component->debugfs_root = debugfs_create_dir(component->name,
311 component->card->debugfs_card_root);
312 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100313
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 if (!component->debugfs_root) {
315 dev_warn(component->dev,
316 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 return;
318 }
319
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200320 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
321 component->debugfs_root);
322
323 if (component->init_debugfs)
324 component->init_debugfs(component);
325}
326
327static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
328{
329 debugfs_remove_recursive(component->debugfs_root);
330}
331
332static void soc_init_codec_debugfs(struct snd_soc_component *component)
333{
334 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
335
Mark Brown2624d5f2009-11-03 21:56:13 +0000336 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200337 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000338 codec, &codec_reg_fops);
339 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200340 dev_warn(codec->dev,
341 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000342}
343
Mark Brownc3c5a192010-09-15 18:15:14 +0100344static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100349 struct snd_soc_codec *codec;
350
351 if (!buf)
352 return -ENOMEM;
353
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100354 mutex_lock(&client_mutex);
355
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 list_for_each_entry(codec, &codec_list, list) {
357 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200358 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100359 if (len >= 0)
360 ret += len;
361 if (ret > PAGE_SIZE) {
362 ret = PAGE_SIZE;
363 break;
364 }
365 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100366
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100367 mutex_unlock(&client_mutex);
368
Mark Brownc3c5a192010-09-15 18:15:14 +0100369 if (ret >= 0)
370 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
371
372 kfree(buf);
373
374 return ret;
375}
376
377static const struct file_operations codec_list_fops = {
378 .read = codec_list_read_file,
379 .llseek = default_llseek,/* read accesses f_pos */
380};
381
Mark Brownf3208782010-09-15 18:19:07 +0100382static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
383 size_t count, loff_t *ppos)
384{
385 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100386 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100387 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100388 struct snd_soc_dai *dai;
389
390 if (!buf)
391 return -ENOMEM;
392
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100393 mutex_lock(&client_mutex);
394
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100395 list_for_each_entry(component, &component_list, list) {
396 list_for_each_entry(dai, &component->dai_list, list) {
397 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
398 dai->name);
399 if (len >= 0)
400 ret += len;
401 if (ret > PAGE_SIZE) {
402 ret = PAGE_SIZE;
403 break;
404 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100405 }
406 }
Mark Brownf3208782010-09-15 18:19:07 +0100407
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100408 mutex_unlock(&client_mutex);
409
Mark Brown2b194f9d2010-10-13 10:52:16 +0100410 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100411
412 kfree(buf);
413
414 return ret;
415}
416
417static const struct file_operations dai_list_fops = {
418 .read = dai_list_read_file,
419 .llseek = default_llseek,/* read accesses f_pos */
420};
421
Mark Brown19c7ac22010-09-15 18:22:40 +0100422static ssize_t platform_list_read_file(struct file *file,
423 char __user *user_buf,
424 size_t count, loff_t *ppos)
425{
426 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100427 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100428 struct snd_soc_platform *platform;
429
430 if (!buf)
431 return -ENOMEM;
432
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100433 mutex_lock(&client_mutex);
434
Mark Brown2b194f9d2010-10-13 10:52:16 +0100435 list_for_each_entry(platform, &platform_list, list) {
436 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200437 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100438 if (len >= 0)
439 ret += len;
440 if (ret > PAGE_SIZE) {
441 ret = PAGE_SIZE;
442 break;
443 }
444 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100445
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100446 mutex_unlock(&client_mutex);
447
Mark Brown2b194f9d2010-10-13 10:52:16 +0100448 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100449
450 kfree(buf);
451
452 return ret;
453}
454
455static const struct file_operations platform_list_fops = {
456 .read = platform_list_read_file,
457 .llseek = default_llseek,/* read accesses f_pos */
458};
459
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200460static void soc_init_card_debugfs(struct snd_soc_card *card)
461{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200462 if (!snd_soc_debugfs_root)
463 return;
464
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200465 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000466 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200467 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200468 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100469 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200470 return;
471 }
472
473 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
474 card->debugfs_card_root,
475 &card->pop_time);
476 if (!card->debugfs_pop_time)
477 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000478 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200479}
480
481static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
482{
483 debugfs_remove_recursive(card->debugfs_card_root);
484}
485
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200486
487static void snd_soc_debugfs_init(void)
488{
489 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
490 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
491 pr_warn("ASoC: Failed to create debugfs directory\n");
492 snd_soc_debugfs_root = NULL;
493 return;
494 }
495
496 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
497 &codec_list_fops))
498 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
499
500 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
501 &dai_list_fops))
502 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
503
504 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
505 &platform_list_fops))
506 pr_warn("ASoC: Failed to create platform list debugfs file\n");
507}
508
509static void snd_soc_debugfs_exit(void)
510{
511 debugfs_remove_recursive(snd_soc_debugfs_root);
512}
513
Mark Brown2624d5f2009-11-03 21:56:13 +0000514#else
515
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200516#define soc_init_codec_debugfs NULL
517
518static inline void soc_init_component_debugfs(
519 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000520{
521}
522
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200523static inline void soc_cleanup_component_debugfs(
524 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000525{
526}
527
Axel Linb95fccb2010-11-09 17:06:44 +0800528static inline void soc_init_card_debugfs(struct snd_soc_card *card)
529{
530}
531
532static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
533{
534}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200535
536static inline void snd_soc_debugfs_init(void)
537{
538}
539
540static inline void snd_soc_debugfs_exit(void)
541{
542}
543
Mark Brown2624d5f2009-11-03 21:56:13 +0000544#endif
545
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100546struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
547 const char *dai_link, int stream)
548{
549 int i;
550
551 for (i = 0; i < card->num_links; i++) {
552 if (card->rtd[i].dai_link->no_pcm &&
553 !strcmp(card->rtd[i].dai_link->name, dai_link))
554 return card->rtd[i].pcm->streams[stream].substream;
555 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000556 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100557 return NULL;
558}
559EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
560
561struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
562 const char *dai_link)
563{
564 int i;
565
566 for (i = 0; i < card->num_links; i++) {
567 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
568 return &card->rtd[i];
569 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000570 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100571 return NULL;
572}
573EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
574
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100575static void codec2codec_close_delayed_work(struct work_struct *work)
576{
577 /* Currently nothing to do for c2c links
578 * Since c2c links are internal nodes in the DAPM graph and
579 * don't interface with the outside world or application layer
580 * we don't have to do any special handling on close.
581 */
582}
583
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000584#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000586int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000588 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200589 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200590 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200592 /* If the card is not initialized yet there is nothing to do */
593 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200594 return 0;
595
Andy Green6ed25972008-06-13 16:24:05 +0100596 /* Due to the resume being scheduled into a workqueue we could
597 * suspend before that's finished - wait for it to complete.
598 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 snd_power_lock(card->snd_card);
600 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
601 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100602
603 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100605
Mark Browna00f90f2010-12-02 16:24:24 +0000606 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100610 continue;
611
Benoit Cousson88bd8702014-07-08 23:19:34 +0200612 for (j = 0; j < card->rtd[i].num_codecs; j++) {
613 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
614 struct snd_soc_dai_driver *drv = dai->driver;
615
616 if (drv->ops->digital_mute && dai->playback_active)
617 drv->ops->digital_mute(dai, 1);
618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619 }
620
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100621 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
623 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100624 continue;
625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100627 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100628
Mark Brown87506542008-11-18 20:50:34 +0000629 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000630 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 for (i = 0; i < card->num_rtd; i++) {
633 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100636 continue;
637
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100638 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100640 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200642 /* close any waiting streams */
643 for (i = 0; i < card->num_rtd; i++)
Tejun Heo43829732012-08-20 14:51:24 -0700644 flush_delayed_work(&card->rtd[i].delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100645
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647
648 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100649 continue;
650
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800651 snd_soc_dapm_stream_event(&card->rtd[i],
652 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800653 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800655 snd_soc_dapm_stream_event(&card->rtd[i],
656 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800657 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 }
659
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200660 /* Recheck all endpoints too, their state is affected by suspend */
661 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700662 snd_soc_dapm_sync(&card->dapm);
663
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200665 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 /* If there are paths active then the CODEC will be held with
667 * bias _ON and should not be suspended. */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200668 if (!codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200669 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000671 /*
672 * If the CODEC is capable of idle
673 * bias off then being in STANDBY
674 * means it's doing something,
675 * otherwise fall through.
676 */
677 if (codec->dapm.idle_bias_off) {
678 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200679 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000680 break;
681 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200684 if (codec->driver->suspend)
685 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 codec->suspended = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200687 if (codec->component.regmap)
688 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800689 /* deactivate pins to sleep state */
690 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 break;
692 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200693 dev_dbg(codec->dev,
694 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 break;
696 }
697 }
698 }
699
700 for (i = 0; i < card->num_rtd; i++) {
701 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
702
703 if (card->rtd[i].dai_link->ignore_suspend)
704 continue;
705
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100706 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800708
709 /* deactivate pins to sleep state */
710 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200711 }
712
Mark Brown87506542008-11-18 20:50:34 +0000713 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000714 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715
716 return 0;
717}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000718EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719
Andy Green6ed25972008-06-13 16:24:05 +0100720/* deferred resume work, so resume can complete before we finished
721 * setting our codec back up, which can be very slow on I2C
722 */
723static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 struct snd_soc_card *card =
726 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200727 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200728 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729
Andy Green6ed25972008-06-13 16:24:05 +0100730 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
731 * so userspace apps are blocked from touching us
732 */
733
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000734 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100735
Mark Brown99497882010-05-07 20:24:05 +0100736 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100738
Mark Brown87506542008-11-18 20:50:34 +0000739 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000740 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100742 /* resume control bus DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 for (i = 0; i < card->num_rtd; i++) {
744 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100747 continue;
748
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100749 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751 }
752
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200753 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200754 if (codec->suspended) {
Lars-Peter Clausenb8faaba2015-05-15 12:41:30 +0200755 if (codec->driver->resume)
756 codec->driver->resume(codec);
757 codec->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100758 }
759 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100764 continue;
765
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800766 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000767 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800768 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800770 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000771 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800772 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773 }
774
Mark Brown3ff3f642008-05-19 12:32:25 +0200775 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100779 continue;
780
Benoit Cousson88bd8702014-07-08 23:19:34 +0200781 for (j = 0; j < card->rtd[i].num_codecs; j++) {
782 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
783 struct snd_soc_dai_driver *drv = dai->driver;
784
785 if (drv->ops->digital_mute && dai->playback_active)
786 drv->ops->digital_mute(dai, 0);
787 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788 }
789
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 for (i = 0; i < card->num_rtd; i++) {
791 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100794 continue;
795
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100796 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200798 }
799
Mark Brown87506542008-11-18 20:50:34 +0000800 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000801 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000803 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100804
805 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700807
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200808 /* Recheck all endpoints too, their state is affected by suspend */
809 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700810 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100811}
812
813/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000814int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100815{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000816 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100817 bool bus_control = false;
818 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200819
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200820 /* If the card is not initialized yet there is nothing to do */
821 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800822 return 0;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 /* activate pins from sleep state */
825 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
827 struct snd_soc_dai **codec_dais = rtd->codec_dais;
828 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
829 int j;
830
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800831 if (cpu_dai->active)
832 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200833
834 for (j = 0; j < rtd->num_codecs; j++) {
835 struct snd_soc_dai *codec_dai = codec_dais[j];
836 if (codec_dai->active)
837 pinctrl_pm_select_default_state(codec_dai->dev);
838 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800839 }
840
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100841 /*
842 * DAIs that also act as the control bus master might have other drivers
843 * hanging off them so need to resume immediately. Other drivers don't
844 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100845 * due to I/O costs and anti-pop so handle them out of line.
846 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 for (i = 0; i < card->num_rtd; i++) {
848 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100849 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600850 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100851 if (bus_control) {
852 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600853 soc_resume_deferred(&card->deferred_resume_work);
854 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000855 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600856 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000857 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100858 }
Andy Green6ed25972008-06-13 16:24:05 +0100859
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860 return 0;
861}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000862EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000864#define snd_soc_suspend NULL
865#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866#endif
867
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100868static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800869};
870
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200871static struct snd_soc_component *soc_find_component(
872 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100873{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200874 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100875
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100876 lockdep_assert_held(&client_mutex);
877
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200878 list_for_each_entry(component, &component_list, list) {
879 if (of_node) {
880 if (component->dev->of_node == of_node)
881 return component;
882 } else if (strcmp(component->name, name) == 0) {
883 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100884 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100885 }
886
887 return NULL;
888}
889
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200890static struct snd_soc_dai *snd_soc_find_dai(
891 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200893 struct snd_soc_component *component;
894 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100895
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100896 lockdep_assert_held(&client_mutex);
897
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200898 /* Find CPU DAI from registered DAIs*/
899 list_for_each_entry(component, &component_list, list) {
900 if (dlc->of_node && component->dev->of_node != dlc->of_node)
901 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100902 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200903 continue;
904 list_for_each_entry(dai, &component->dai_list, list) {
905 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100907
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200908 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100909 }
910 }
911
912 return NULL;
913}
914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200919 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200920 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200921 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000922 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100923 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200924 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200925
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000926 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000927
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200928 cpu_dai_component.name = dai_link->cpu_name;
929 cpu_dai_component.of_node = dai_link->cpu_of_node;
930 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
931 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000932 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000933 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000935 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000936 }
937
Benoit Cousson88bd8702014-07-08 23:19:34 +0200938 rtd->num_codecs = dai_link->num_codecs;
939
940 /* Find CODEC from registered CODECs */
941 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200942 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200943 if (!codec_dais[i]) {
944 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
945 codecs[i].dai_name);
946 return -EPROBE_DEFER;
947 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000948 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100949
Benoit Cousson88bd8702014-07-08 23:19:34 +0200950 /* Single codec links expect codec and codec_dai in runtime data */
951 rtd->codec_dai = codec_dais[0];
952 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100953
Mark Brown848dd8b2011-04-27 18:16:32 +0100954 /* if there's no platform we match on the empty platform */
955 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700956 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100957 platform_name = "snd-soc-dummy";
958
Mark Brownb19e6e72012-03-14 21:18:39 +0000959 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700961 if (dai_link->platform_of_node) {
962 if (platform->dev->of_node !=
963 dai_link->platform_of_node)
964 continue;
965 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200966 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700967 continue;
968 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700969
970 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000972 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000973 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000975 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000977
978 card->num_rtd++;
979
980 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981}
982
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200983static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600984{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200985 if (!component->probed)
986 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600987
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200988 /* This is a HACK and will be removed soon */
989 if (component->codec)
990 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600991
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200992 if (component->remove)
993 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600994
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200995 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600996
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200997 soc_cleanup_component_debugfs(component);
998 component->probed = 0;
999 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -06001000}
1001
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001002static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001003{
1004 int err;
1005
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001006 if (dai && dai->probed &&
1007 dai->driver->remove_order == order) {
1008 if (dai->driver->remove) {
1009 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001010 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001011 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001012 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001013 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001014 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001015 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001016 }
1017}
1018
Stephen Warren62ae68f2012-06-08 12:34:23 -06001019static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020{
1021 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001022 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023
1024 /* unregister the rtd device */
1025 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001026 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001027 rtd->dev_registered = 0;
1028 }
1029
1030 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001031 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001032 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001033
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001034 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035}
1036
Stephen Warren62ae68f2012-06-08 12:34:23 -06001037static void soc_remove_link_components(struct snd_soc_card *card, int num,
1038 int order)
1039{
1040 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001042 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001043 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001044 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045
1046 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001047 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001048 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001049
1050 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001051 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001052 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001053 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001054 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001055 }
1056
1057 /* remove any CPU-side CODEC */
1058 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001059 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001060 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001061 }
1062}
1063
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001064static void soc_remove_dai_links(struct snd_soc_card *card)
1065{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001066 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001067
Liam Girdwood0168bf02011-06-07 16:08:05 +01001068 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1069 order++) {
1070 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001071 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001072 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001073
1074 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1075 order++) {
1076 for (dai = 0; dai < card->num_rtd; dai++)
1077 soc_remove_link_components(card, dai, order);
1078 }
1079
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001080 card->num_rtd = 0;
1081}
1082
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001083static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001084 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001085{
1086 int i;
1087
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001088 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001089 return;
1090
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001091 for (i = 0; i < card->num_configs; i++) {
1092 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001093 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001094 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001095 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001096 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001097 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001098 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001099 }
1100}
1101
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001102static int soc_probe_component(struct snd_soc_card *card,
1103 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001104{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001105 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001106 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001107 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001108
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001109 if (component->probed)
1110 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001111
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001112 component->card = card;
1113 dapm->card = card;
1114 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001115
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001116 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001117 return -ENODEV;
1118
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001119 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001120
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001121 if (component->dapm_widgets) {
1122 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1123 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001124
1125 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001126 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001127 "Failed to create new controls %d\n", ret);
1128 goto err_probe;
1129 }
1130 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001131
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001132 list_for_each_entry(dai, &component->dai_list, list) {
1133 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001134 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001135 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001136 "Failed to create DAI widgets %d\n", ret);
1137 goto err_probe;
1138 }
1139 }
Mark Brown888df392012-02-16 19:37:51 -08001140
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001141 if (component->probe) {
1142 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001143 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001144 dev_err(component->dev,
1145 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001146 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001147 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001148
1149 WARN(dapm->idle_bias_off &&
1150 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001151 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001152 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001153 }
1154
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001155 if (component->controls)
1156 snd_soc_add_component_controls(component, component->controls,
1157 component->num_controls);
1158 if (component->dapm_routes)
1159 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1160 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001161
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001162 component->probed = 1;
1163 list_add(&dapm->list, &card->dapm_list);
1164
1165 /* This is a HACK and will be removed soon */
1166 if (component->codec)
1167 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001168
Jarkko Nikula70d29332011-01-27 16:24:22 +02001169 return 0;
1170
1171err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001172 soc_cleanup_component_debugfs(component);
1173 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001174
1175 return ret;
1176}
1177
Mark Brown36ae1a92012-01-06 17:12:45 -08001178static void rtd_release(struct device *dev)
1179{
1180 kfree(dev);
1181}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001182
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001183static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1184 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001185{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001186 int ret = 0;
1187
Jarkko Nikula589c3562010-12-06 16:27:07 +02001188 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001189 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1190 if (!rtd->dev)
1191 return -ENOMEM;
1192 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001193 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001194 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001195 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001196 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001197 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001198 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001199 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1200 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1201 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1202 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001203 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001204 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001205 /* calling put_device() here to free the rtd->dev */
1206 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001207 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001208 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001209 return ret;
1210 }
1211 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001212 return 0;
1213}
1214
Stephen Warren62ae68f2012-06-08 12:34:23 -06001215static int soc_probe_link_components(struct snd_soc_card *card, int num,
1216 int order)
1217{
1218 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001219 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001220 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001221 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001222
1223 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001224 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001225 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001226 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001227 if (ret < 0)
1228 return ret;
1229 }
1230
Benoit Cousson88bd8702014-07-08 23:19:34 +02001231 /* probe the CODEC-side components */
1232 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001233 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001234 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001235 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001236 if (ret < 0)
1237 return ret;
1238 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001239 }
1240
1241 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001242 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001243 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001244 if (ret < 0)
1245 return ret;
1246 }
1247
1248 return 0;
1249}
1250
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001251static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001252{
1253 int ret;
1254
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001255 if (!dai->probed && dai->driver->probe_order == order) {
1256 if (dai->driver->probe) {
1257 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001258 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001259 dev_err(dai->dev,
1260 "ASoC: failed to probe DAI %s: %d\n",
1261 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001262 return ret;
1263 }
1264 }
1265
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001266 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001267 }
1268
1269 return 0;
1270}
1271
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001272static int soc_link_dai_widgets(struct snd_soc_card *card,
1273 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001274 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001275{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001276 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1277 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001278 struct snd_soc_dapm_widget *play_w, *capture_w;
1279 int ret;
1280
Benoit Cousson88bd8702014-07-08 23:19:34 +02001281 if (rtd->num_codecs > 1)
1282 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1283
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001284 /* link the DAI widgets */
1285 play_w = codec_dai->playback_widget;
1286 capture_w = cpu_dai->capture_widget;
1287 if (play_w && capture_w) {
1288 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00001289 dai_link->num_params, capture_w,
1290 play_w);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001291 if (ret != 0) {
1292 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1293 play_w->name, capture_w->name, ret);
1294 return ret;
1295 }
1296 }
1297
1298 play_w = cpu_dai->playback_widget;
1299 capture_w = codec_dai->capture_widget;
1300 if (play_w && capture_w) {
1301 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00001302 dai_link->num_params, capture_w,
1303 play_w);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001304 if (ret != 0) {
1305 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1306 play_w->name, capture_w->name, ret);
1307 return ret;
1308 }
1309 }
1310
1311 return 0;
1312}
1313
Stephen Warren62ae68f2012-06-08 12:34:23 -06001314static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315{
1316 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1317 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownc74184e2012-04-04 22:12:09 +01001318 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001319 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001320
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001321 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001322 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001323
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001324 /* set default power off timeout */
1325 rtd->pmdown_time = pmdown_time;
1326
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001327 ret = soc_probe_dai(cpu_dai, order);
1328 if (ret)
1329 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001331 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001332 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001333 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001334 if (ret)
1335 return ret;
1336 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001337
Liam Girdwood0168bf02011-06-07 16:08:05 +01001338 /* complete DAI probe during last probe */
1339 if (order != SND_SOC_COMP_ORDER_LAST)
1340 return 0;
1341
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001342 /* do machine specific initialization */
1343 if (dai_link->init) {
1344 ret = dai_link->init(rtd);
1345 if (ret < 0) {
1346 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1347 dai_link->name, ret);
1348 return ret;
1349 }
1350 }
1351
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001352 if (dai_link->dai_fmt)
1353 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1354
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001355 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001356 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001357 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001358
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001359#ifdef CONFIG_DEBUG_FS
1360 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001361 if (dai_link->dynamic)
1362 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001363#endif
1364
Namarta Kohli1245b702012-08-16 17:10:41 +05301365 if (cpu_dai->driver->compress_dai) {
1366 /*create compress_device"*/
1367 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001368 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001369 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301370 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001371 return ret;
1372 }
1373 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301374
1375 if (!dai_link->params) {
1376 /* create the pcm */
1377 ret = soc_new_pcm(rtd, num);
1378 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001379 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301380 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001381 return ret;
1382 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301383 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001384 INIT_DELAYED_WORK(&rtd->delayed_work,
1385 codec2codec_close_delayed_work);
1386
Namarta Kohli1245b702012-08-16 17:10:41 +05301387 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001388 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001389 if (ret)
1390 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001391 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 }
1393
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001394 return 0;
1395}
1396
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001397static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001398{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001399 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001400 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001401 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001402
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001403 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1404 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001405 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001406 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001407
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001408 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001409 return -EPROBE_DEFER;
1410 }
1411
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001412 /*
1413 * Some places still reference rtd->codec, so we have to keep that
1414 * initialized if the component is a CODEC. Once all those references
1415 * have been removed, this code can be removed as well.
1416 */
1417 rtd->codec = rtd->component->codec;
1418
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001419 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001420}
1421
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001422static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1423{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001424 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001425 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001426 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001428 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001429 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001430 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001431
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001432 /* do machine specific initialization */
1433 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001434 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001435 if (ret < 0) {
1436 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1437 aux_dev->name, ret);
1438 return ret;
1439 }
1440 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001441
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001442 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001443}
1444
1445static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1446{
1447 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001448 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001449
1450 /* unregister the rtd device */
1451 if (rtd->dev_registered) {
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001452 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001453 rtd->dev_registered = 0;
1454 }
1455
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001456 if (component && component->probed)
1457 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001458}
1459
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001460static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001461{
1462 int ret;
1463
1464 if (codec->cache_init)
1465 return 0;
1466
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001467 ret = snd_soc_cache_init(codec);
1468 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001469 dev_err(codec->dev,
1470 "ASoC: Failed to set cache compression type: %d\n",
1471 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001472 return ret;
1473 }
1474 codec->cache_init = 1;
1475 return 0;
1476}
1477
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001478/**
1479 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1480 * @rtd: The runtime for which the DAI link format should be changed
1481 * @dai_fmt: The new DAI link format
1482 *
1483 * This function updates the DAI link format for all DAIs connected to the DAI
1484 * link for the specified runtime.
1485 *
1486 * Note: For setups with a static format set the dai_fmt field in the
1487 * corresponding snd_dai_link struct instead of using this function.
1488 *
1489 * Returns 0 on success, otherwise a negative error code.
1490 */
1491int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1492 unsigned int dai_fmt)
1493{
1494 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1496 unsigned int i;
1497 int ret;
1498
1499 for (i = 0; i < rtd->num_codecs; i++) {
1500 struct snd_soc_dai *codec_dai = codec_dais[i];
1501
1502 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1503 if (ret != 0 && ret != -ENOTSUPP) {
1504 dev_warn(codec_dai->dev,
1505 "ASoC: Failed to set DAI format: %d\n", ret);
1506 return ret;
1507 }
1508 }
1509
1510 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1511 if (cpu_dai->codec) {
1512 unsigned int inv_dai_fmt;
1513
1514 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1515 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1516 case SND_SOC_DAIFMT_CBM_CFM:
1517 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1518 break;
1519 case SND_SOC_DAIFMT_CBM_CFS:
1520 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1521 break;
1522 case SND_SOC_DAIFMT_CBS_CFM:
1523 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1524 break;
1525 case SND_SOC_DAIFMT_CBS_CFS:
1526 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1527 break;
1528 }
1529
1530 dai_fmt = inv_dai_fmt;
1531 }
1532
1533 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1534 if (ret != 0 && ret != -ENOTSUPP) {
1535 dev_warn(cpu_dai->dev,
1536 "ASoC: Failed to set DAI format: %d\n", ret);
1537 return ret;
1538 }
1539
1540 return 0;
1541}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001542EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001543
Mark Brownb19e6e72012-03-14 21:18:39 +00001544static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001546 struct snd_soc_codec *codec;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001547 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001548
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001549 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001550 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001551
Mark Brownb19e6e72012-03-14 21:18:39 +00001552 /* bind DAIs */
1553 for (i = 0; i < card->num_links; i++) {
1554 ret = soc_bind_dai_link(card, i);
1555 if (ret != 0)
1556 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001557 }
1558
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001559 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001560 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001561 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001562 if (ret != 0)
1563 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001564 }
1565
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001566 /* initialize the register cache for each available codec */
1567 list_for_each_entry(codec, &codec_list, list) {
1568 if (codec->cache_init)
1569 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001570 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001571 if (ret < 0)
1572 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001573 }
1574
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001576 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 card->owner, 0, &card->snd_card);
1578 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001579 dev_err(card->dev,
1580 "ASoC: can't create sound card for card %s: %d\n",
1581 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001582 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001584
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001585 soc_init_card_debugfs(card);
1586
Mark Browne37a4972011-03-02 18:21:57 +00001587 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1588 card->dapm.dev = card->dev;
1589 card->dapm.card = card;
1590 list_add(&card->dapm.list, &card->dapm_list);
1591
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001592#ifdef CONFIG_DEBUG_FS
1593 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1594#endif
1595
Mark Brown88ee1c62011-02-02 10:43:26 +00001596#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001597 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001598 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001599#endif
Andy Green6ed25972008-06-13 16:24:05 +01001600
Mark Brown9a841eb2011-04-12 17:51:37 -07001601 if (card->dapm_widgets)
1602 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1603 card->num_dapm_widgets);
1604
Nicolin Chenf23e8602015-02-14 17:22:49 -08001605 if (card->of_dapm_widgets)
1606 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1607 card->num_of_dapm_widgets);
1608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 /* initialise the sound card only once */
1610 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001611 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612 if (ret < 0)
1613 goto card_probe_error;
1614 }
1615
Stephen Warren62ae68f2012-06-08 12:34:23 -06001616 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001617 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1618 order++) {
1619 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001620 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001621 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001622 dev_err(card->dev,
1623 "ASoC: failed to instantiate card %d\n",
1624 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001625 goto probe_dai_err;
1626 }
1627 }
1628 }
1629
1630 /* probe all DAI links on this card */
1631 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1632 order++) {
1633 for (i = 0; i < card->num_links; i++) {
1634 ret = soc_probe_link_dais(card, i, order);
1635 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001636 dev_err(card->dev,
1637 "ASoC: failed to instantiate card %d\n",
1638 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001639 goto probe_dai_err;
1640 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641 }
1642 }
1643
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001644 for (i = 0; i < card->num_aux_devs; i++) {
1645 ret = soc_probe_aux_dev(card, i);
1646 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001647 dev_err(card->dev,
1648 "ASoC: failed to add auxiliary devices %d\n",
1649 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001650 goto probe_aux_dev_err;
1651 }
1652 }
1653
Mark Brown888df392012-02-16 19:37:51 -08001654 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001655 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001656
Mark Brownb7af1da2011-04-07 19:18:44 +09001657 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001658 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001659
Mark Brownb8ad29d2011-03-02 18:35:51 +00001660 if (card->dapm_routes)
1661 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1662 card->num_dapm_routes);
1663
Nicolin Chenf23e8602015-02-14 17:22:49 -08001664 if (card->of_dapm_routes)
1665 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1666 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01001667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001670 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1671 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001672 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1673 "%s", card->driver_name ? card->driver_name : card->name);
1674 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1675 switch (card->snd_card->driver[i]) {
1676 case '_':
1677 case '-':
1678 case '\0':
1679 break;
1680 default:
1681 if (!isalnum(card->snd_card->driver[i]))
1682 card->snd_card->driver[i] = '_';
1683 break;
1684 }
1685 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686
Mark Brown28e9ad92011-03-02 18:36:34 +00001687 if (card->late_probe) {
1688 ret = card->late_probe(card);
1689 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001690 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001691 card->name, ret);
1692 goto probe_aux_dev_err;
1693 }
1694 }
1695
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001696 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 ret = snd_card_register(card->snd_card);
1699 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001700 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1701 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001702 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 }
1704
Mark Brown435c5e22008-12-04 15:32:53 +00001705 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001706 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001708 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001709
1710 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001711
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001712probe_aux_dev_err:
1713 for (i = 0; i < card->num_aux_devs; i++)
1714 soc_remove_aux_dev(card, i);
1715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001716probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001717 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001718
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001719card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001720 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001721 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001722
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001723 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724 snd_card_free(card->snd_card);
1725
Mark Brownb19e6e72012-03-14 21:18:39 +00001726base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001727 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001728 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001729
Mark Brownb19e6e72012-03-14 21:18:39 +00001730 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001731}
1732
1733/* probes a new socdev */
1734static int soc_probe(struct platform_device *pdev)
1735{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001736 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001737
Vinod Koul70a7ca32011-01-14 19:22:48 +05301738 /*
1739 * no card, so machine driver should be registering card
1740 * we should not be here in that case so ret error
1741 */
1742 if (!card)
1743 return -EINVAL;
1744
Mark Brownfe4085e2012-03-02 13:07:41 +00001745 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001746 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001747 card->name);
1748
Mark Brown435c5e22008-12-04 15:32:53 +00001749 /* Bodge while we unpick instantiation */
1750 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751
Mark Brown28d528c2012-08-09 18:45:23 +01001752 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753}
1754
Vinod Koulb0e26482011-01-13 22:48:02 +05301755static int soc_cleanup_card_resources(struct snd_soc_card *card)
1756{
Vinod Koulb0e26482011-01-13 22:48:02 +05301757 int i;
1758
1759 /* make sure any delayed work runs */
1760 for (i = 0; i < card->num_rtd; i++) {
1761 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001762 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301763 }
1764
1765 /* remove auxiliary devices */
1766 for (i = 0; i < card->num_aux_devs; i++)
1767 soc_remove_aux_dev(card, i);
1768
1769 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001770 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301771
1772 soc_cleanup_card_debugfs(card);
1773
1774 /* remove the card */
1775 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001776 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301777
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001778 snd_soc_dapm_free(&card->dapm);
1779
Vinod Koulb0e26482011-01-13 22:48:02 +05301780 snd_card_free(card->snd_card);
1781 return 0;
1782
1783}
1784
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001785/* removes a socdev */
1786static int soc_remove(struct platform_device *pdev)
1787{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001788 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789
Mark Brownc5af3a22008-11-28 13:29:45 +00001790 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791 return 0;
1792}
1793
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001794int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001795{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001796 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797 int i;
Mark Brown51737472009-06-22 13:16:51 +01001798
1799 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001800 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001801
1802 /* Flush out pmdown_time work - we actually do want to run it
1803 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804 for (i = 0; i < card->num_rtd; i++) {
1805 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001806 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 }
Mark Brown51737472009-06-22 13:16:51 +01001808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001810
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001811 /* deactivate pins to sleep state */
1812 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001813 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1814 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1815 int j;
1816
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001817 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001818 for (j = 0; j < rtd->num_codecs; j++) {
1819 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1820 pinctrl_pm_select_sleep_state(codec_dai->dev);
1821 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001822 }
1823
Mark Brown416356f2009-06-30 19:05:15 +01001824 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001825}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001826EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001827
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001828const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301829 .suspend = snd_soc_suspend,
1830 .resume = snd_soc_resume,
1831 .freeze = snd_soc_suspend,
1832 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001833 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301834 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001835};
Stephen Warrendeb26072011-04-05 19:35:30 -06001836EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001837
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838/* ASoC platform driver */
1839static struct platform_driver soc_driver = {
1840 .driver = {
1841 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001842 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001843 },
1844 .probe = soc_probe,
1845 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846};
1847
Mark Brown096e49d2009-07-05 15:12:22 +01001848/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849 * snd_soc_cnew - create new control
1850 * @_template: control template
1851 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001852 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001853 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854 *
1855 * Create a new mixer control from a template control.
1856 *
1857 * Returns 0 for success, else error.
1858 */
1859struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08001860 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00001861 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001862{
1863 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001864 struct snd_kcontrol *kcontrol;
1865 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001866
1867 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001868 template.index = 0;
1869
Mark Brownefb7ac32011-03-08 17:23:24 +00001870 if (!long_name)
1871 long_name = template.name;
1872
1873 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02001874 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00001875 if (!name)
1876 return NULL;
1877
Mark Brownefb7ac32011-03-08 17:23:24 +00001878 template.name = name;
1879 } else {
1880 template.name = long_name;
1881 }
1882
1883 kcontrol = snd_ctl_new1(&template, data);
1884
1885 kfree(name);
1886
1887 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001888}
1889EXPORT_SYMBOL_GPL(snd_soc_cnew);
1890
Liam Girdwood022658b2012-02-03 17:43:09 +00001891static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1892 const struct snd_kcontrol_new *controls, int num_controls,
1893 const char *prefix, void *data)
1894{
1895 int err, i;
1896
1897 for (i = 0; i < num_controls; i++) {
1898 const struct snd_kcontrol_new *control = &controls[i];
1899 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1900 control->name, prefix));
1901 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001902 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1903 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00001904 return err;
1905 }
1906 }
1907
1908 return 0;
1909}
1910
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01001911struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1912 const char *name)
1913{
1914 struct snd_card *card = soc_card->snd_card;
1915 struct snd_kcontrol *kctl;
1916
1917 if (unlikely(!name))
1918 return NULL;
1919
1920 list_for_each_entry(kctl, &card->controls, list)
1921 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1922 return kctl;
1923 return NULL;
1924}
1925EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1926
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001928 * snd_soc_add_component_controls - Add an array of controls to a component.
1929 *
1930 * @component: Component to add controls to
1931 * @controls: Array of controls to add
1932 * @num_controls: Number of elements in the array
1933 *
1934 * Return: 0 for success, else error.
1935 */
1936int snd_soc_add_component_controls(struct snd_soc_component *component,
1937 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1938{
1939 struct snd_card *card = component->card->snd_card;
1940
1941 return snd_soc_add_controls(card, component->dev, controls,
1942 num_controls, component->name_prefix, component);
1943}
1944EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1945
1946/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001947 * snd_soc_add_codec_controls - add an array of controls to a codec.
1948 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00001949 * duplicating this code.
1950 *
1951 * @codec: codec to add controls to
1952 * @controls: array of controls to add
1953 * @num_controls: number of elements in the array
1954 *
1955 * Return 0 for success, else error.
1956 */
Liam Girdwood022658b2012-02-03 17:43:09 +00001957int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001958 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00001959{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001960 return snd_soc_add_component_controls(&codec->component, controls,
1961 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001962}
Liam Girdwood022658b2012-02-03 17:43:09 +00001963EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001964
1965/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001966 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00001967 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001968 *
1969 * @platform: platform to add controls to
1970 * @controls: array of controls to add
1971 * @num_controls: number of elements in the array
1972 *
1973 * Return 0 for success, else error.
1974 */
1975int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001976 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001977{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001978 return snd_soc_add_component_controls(&platform->component, controls,
1979 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001980}
1981EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1982
1983/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001984 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1985 * Convenience function to add a list of controls.
1986 *
1987 * @soc_card: SoC card to add controls to
1988 * @controls: array of controls to add
1989 * @num_controls: number of elements in the array
1990 *
1991 * Return 0 for success, else error.
1992 */
1993int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1994 const struct snd_kcontrol_new *controls, int num_controls)
1995{
1996 struct snd_card *card = soc_card->snd_card;
1997
1998 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1999 NULL, soc_card);
2000}
2001EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2002
2003/**
2004 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2005 * Convienience function to add a list of controls.
2006 *
2007 * @dai: DAI to add controls to
2008 * @controls: array of controls to add
2009 * @num_controls: number of elements in the array
2010 *
2011 * Return 0 for success, else error.
2012 */
2013int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2014 const struct snd_kcontrol_new *controls, int num_controls)
2015{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002016 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002017
2018 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2019 NULL, dai);
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2022
2023/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002024 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2025 * @dai: DAI
2026 * @clk_id: DAI specific clock ID
2027 * @freq: new clock frequency in Hz
2028 * @dir: new clock direction - input/output.
2029 *
2030 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2031 */
2032int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2033 unsigned int freq, int dir)
2034{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002035 if (dai->driver && dai->driver->ops->set_sysclk)
2036 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002037 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002038 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002039 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002040 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002041 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002042}
2043EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2044
2045/**
Mark Brownec4ee522011-03-07 20:58:11 +00002046 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2047 * @codec: CODEC
2048 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002049 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002050 * @freq: new clock frequency in Hz
2051 * @dir: new clock direction - input/output.
2052 *
2053 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2054 */
2055int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002056 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002057{
2058 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002059 return codec->driver->set_sysclk(codec, clk_id, source,
2060 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002061 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002062 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002063}
2064EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2065
2066/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002067 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2068 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002069 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002070 * @div: new clock divisor.
2071 *
2072 * Configures the clock dividers. This is used to derive the best DAI bit and
2073 * frame clocks from the system or master clock. It's best to set the DAI bit
2074 * and frame clocks as low as possible to save system power.
2075 */
2076int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2077 int div_id, int div)
2078{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002079 if (dai->driver && dai->driver->ops->set_clkdiv)
2080 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002081 else
2082 return -EINVAL;
2083}
2084EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2085
2086/**
2087 * snd_soc_dai_set_pll - configure DAI PLL.
2088 * @dai: DAI
2089 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002090 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002091 * @freq_in: PLL input clock frequency in Hz
2092 * @freq_out: requested PLL output clock frequency in Hz
2093 *
2094 * Configures and enables PLL to generate output clock based on input clock.
2095 */
Mark Brown85488032009-09-05 18:52:16 +01002096int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2097 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002098{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002099 if (dai->driver && dai->driver->ops->set_pll)
2100 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002101 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002102 else if (dai->codec && dai->codec->driver->set_pll)
2103 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2104 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002105 else
2106 return -EINVAL;
2107}
2108EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2109
Mark Brownec4ee522011-03-07 20:58:11 +00002110/*
2111 * snd_soc_codec_set_pll - configure codec PLL.
2112 * @codec: CODEC
2113 * @pll_id: DAI specific PLL ID
2114 * @source: DAI specific source for the PLL
2115 * @freq_in: PLL input clock frequency in Hz
2116 * @freq_out: requested PLL output clock frequency in Hz
2117 *
2118 * Configures and enables PLL to generate output clock based on input clock.
2119 */
2120int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2121 unsigned int freq_in, unsigned int freq_out)
2122{
2123 if (codec->driver->set_pll)
2124 return codec->driver->set_pll(codec, pll_id, source,
2125 freq_in, freq_out);
2126 else
2127 return -EINVAL;
2128}
2129EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2130
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002131/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002132 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2133 * @dai: DAI
2134 * @ratio Ratio of BCLK to Sample rate.
2135 *
2136 * Configures the DAI for a preset BCLK to sample rate ratio.
2137 */
2138int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2139{
2140 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2141 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2142 else
2143 return -EINVAL;
2144}
2145EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2146
2147/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002148 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2149 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002150 * @fmt: SND_SOC_DAIFMT_ format value.
2151 *
2152 * Configures the DAI hardware format and clocking.
2153 */
2154int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2155{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002156 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002157 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002158 if (dai->driver->ops->set_fmt == NULL)
2159 return -ENOTSUPP;
2160 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002161}
2162EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2163
2164/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002165 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002166 * @slots: Number of slots in use.
2167 * @tx_mask: bitmask representing active TX slots.
2168 * @rx_mask: bitmask representing active RX slots.
2169 *
2170 * Generates the TDM tx and rx slot default masks for DAI.
2171 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002172static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002173 unsigned int *tx_mask,
2174 unsigned int *rx_mask)
2175{
2176 if (*tx_mask || *rx_mask)
2177 return 0;
2178
2179 if (!slots)
2180 return -EINVAL;
2181
2182 *tx_mask = (1 << slots) - 1;
2183 *rx_mask = (1 << slots) - 1;
2184
2185 return 0;
2186}
2187
2188/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002189 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2190 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002191 * @tx_mask: bitmask representing active TX slots.
2192 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002193 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002194 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002195 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002196 * This function configures the specified DAI for TDM operation. @slot contains
2197 * the total number of slots of the TDM stream and @slot_with the width of each
2198 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2199 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2200 * DAI should write to or read from. If a bit is set the corresponding slot is
2201 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2202 * the first slot, bit 1 to the second slot and so on. The first active slot
2203 * maps to the first channel of the DAI, the second active slot to the second
2204 * channel and so on.
2205 *
2206 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2207 * @rx_mask and @slot_width will be ignored.
2208 *
2209 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002210 */
2211int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002212 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002213{
Xiubo Lie5c21512014-03-21 14:17:12 +08002214 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2215 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002216 &tx_mask, &rx_mask);
2217 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002218 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002219
Benoit Cousson88bd8702014-07-08 23:19:34 +02002220 dai->tx_mask = tx_mask;
2221 dai->rx_mask = rx_mask;
2222
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002223 if (dai->driver && dai->driver->ops->set_tdm_slot)
2224 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002225 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002226 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002227 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002228}
2229EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2230
2231/**
Barry Song472df3c2009-09-12 01:16:29 +08002232 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2233 * @dai: DAI
2234 * @tx_num: how many TX channels
2235 * @tx_slot: pointer to an array which imply the TX slot number channel
2236 * 0~num-1 uses
2237 * @rx_num: how many RX channels
2238 * @rx_slot: pointer to an array which imply the RX slot number channel
2239 * 0~num-1 uses
2240 *
2241 * configure the relationship between channel number and TDM slot number.
2242 */
2243int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2244 unsigned int tx_num, unsigned int *tx_slot,
2245 unsigned int rx_num, unsigned int *rx_slot)
2246{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002247 if (dai->driver && dai->driver->ops->set_channel_map)
2248 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002249 rx_num, rx_slot);
2250 else
2251 return -EINVAL;
2252}
2253EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2254
2255/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002256 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2257 * @dai: DAI
2258 * @tristate: tristate enable
2259 *
2260 * Tristates the DAI so that others can use it.
2261 */
2262int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2263{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002264 if (dai->driver && dai->driver->ops->set_tristate)
2265 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002266 else
2267 return -EINVAL;
2268}
2269EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2270
2271/**
2272 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2273 * @dai: DAI
2274 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002275 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002276 *
2277 * Mutes the DAI DAC.
2278 */
Mark Brownda183962013-02-06 15:44:07 +00002279int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2280 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002281{
Mark Brownda183962013-02-06 15:44:07 +00002282 if (!dai->driver)
2283 return -ENOTSUPP;
2284
2285 if (dai->driver->ops->mute_stream)
2286 return dai->driver->ops->mute_stream(dai, mute, direction);
2287 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2288 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002289 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002290 else
Mark Brown04570c62012-04-13 19:16:03 +01002291 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002292}
2293EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2294
Benoit Cousson88bd8702014-07-08 23:19:34 +02002295static int snd_soc_init_multicodec(struct snd_soc_card *card,
2296 struct snd_soc_dai_link *dai_link)
2297{
2298 /* Legacy codec/codec_dai link is a single entry in multicodec */
2299 if (dai_link->codec_name || dai_link->codec_of_node ||
2300 dai_link->codec_dai_name) {
2301 dai_link->num_codecs = 1;
2302
2303 dai_link->codecs = devm_kzalloc(card->dev,
2304 sizeof(struct snd_soc_dai_link_component),
2305 GFP_KERNEL);
2306 if (!dai_link->codecs)
2307 return -ENOMEM;
2308
2309 dai_link->codecs[0].name = dai_link->codec_name;
2310 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2311 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2312 }
2313
2314 if (!dai_link->codecs) {
2315 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2316 return -EINVAL;
2317 }
2318
2319 return 0;
2320}
2321
Mark Brownc5af3a22008-11-28 13:29:45 +00002322/**
2323 * snd_soc_register_card - Register a card with the ASoC core
2324 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002325 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002326 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002327 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302328int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002329{
Benoit Cousson88bd8702014-07-08 23:19:34 +02002330 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002331
Mark Brownc5af3a22008-11-28 13:29:45 +00002332 if (!card->name || !card->dev)
2333 return -EINVAL;
2334
Stephen Warren5a504962011-12-21 10:40:59 -07002335 for (i = 0; i < card->num_links; i++) {
2336 struct snd_soc_dai_link *link = &card->dai_link[i];
2337
Benoit Cousson88bd8702014-07-08 23:19:34 +02002338 ret = snd_soc_init_multicodec(card, link);
2339 if (ret) {
2340 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2341 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002342 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02002343
2344 for (j = 0; j < link->num_codecs; j++) {
2345 /*
2346 * Codec must be specified by 1 of name or OF node,
2347 * not both or neither.
2348 */
2349 if (!!link->codecs[j].name ==
2350 !!link->codecs[j].of_node) {
2351 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2352 link->name);
2353 return -EINVAL;
2354 }
2355 /* Codec DAI name must be specified */
2356 if (!link->codecs[j].dai_name) {
2357 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2358 link->name);
2359 return -EINVAL;
2360 }
Stephen Warrenbc926572012-05-25 18:22:11 -06002361 }
Stephen Warren5a504962011-12-21 10:40:59 -07002362
2363 /*
2364 * Platform may be specified by either name or OF node, but
2365 * can be left unspecified, and a dummy platform will be used.
2366 */
2367 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002368 dev_err(card->dev,
2369 "ASoC: Both platform name/of_node are set for %s\n",
2370 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002371 return -EINVAL;
2372 }
2373
2374 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06002375 * CPU device may be specified by either name or OF node, but
2376 * can be left unspecified, and will be matched based on DAI
2377 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07002378 */
Stephen Warrenbc926572012-05-25 18:22:11 -06002379 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002380 dev_err(card->dev,
2381 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2382 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06002383 return -EINVAL;
2384 }
2385 /*
2386 * At least one of CPU DAI name or CPU device name/node must be
2387 * specified
2388 */
2389 if (!link->cpu_dai_name &&
2390 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002391 dev_err(card->dev,
2392 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2393 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002394 return -EINVAL;
2395 }
2396 }
2397
Mark Browned77cc12011-05-03 18:25:34 +01002398 dev_set_drvdata(card->dev, card);
2399
Stephen Warren111c6412011-01-28 14:26:35 -07002400 snd_soc_initialize_card_lists(card);
2401
Mark Brown181a6892012-03-14 20:18:49 +00002402 card->rtd = devm_kzalloc(card->dev,
2403 sizeof(struct snd_soc_pcm_runtime) *
2404 (card->num_links + card->num_aux_devs),
2405 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002406 if (card->rtd == NULL)
2407 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01002408 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002409 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002410
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002411 for (i = 0; i < card->num_links; i++) {
2412 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002413 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002414 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2415 sizeof(struct snd_soc_dai *) *
2416 (card->rtd[i].dai_link->num_codecs),
2417 GFP_KERNEL);
2418 if (card->rtd[i].codec_dais == NULL)
2419 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002420 }
2421
2422 for (i = 0; i < card->num_aux_devs; i++)
2423 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002424
Mark Browndb432b42011-10-03 21:06:40 +01002425 INIT_LIST_HEAD(&card->dapm_dirty);
Liam Girdwood8a978232015-05-29 19:06:14 +01002426 INIT_LIST_HEAD(&card->dobj_list);
Mark Brownc5af3a22008-11-28 13:29:45 +00002427 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002428 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002429 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002430
Mark Brownb19e6e72012-03-14 21:18:39 +00002431 ret = snd_soc_instantiate_card(card);
2432 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002433 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00002434
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002435 /* deactivate pins to sleep state */
2436 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002437 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2439 int j;
2440
2441 for (j = 0; j < rtd->num_codecs; j++) {
2442 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2443 if (!codec_dai->active)
2444 pinctrl_pm_select_sleep_state(codec_dai->dev);
2445 }
2446
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002447 if (!cpu_dai->active)
2448 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2449 }
2450
Mark Brownb19e6e72012-03-14 21:18:39 +00002451 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00002452}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302453EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002454
2455/**
2456 * snd_soc_unregister_card - Unregister a card with the ASoC core
2457 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002458 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002459 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002460 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302461int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002462{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002463 if (card->instantiated) {
2464 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002465 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302466 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002467 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002468 }
Mark Brownc5af3a22008-11-28 13:29:45 +00002469
2470 return 0;
2471}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302472EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002474/*
2475 * Simplify DAI link configuration by removing ".-1" from device names
2476 * and sanitizing names.
2477 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002478static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002479{
2480 char *found, name[NAME_SIZE];
2481 int id1, id2;
2482
2483 if (dev_name(dev) == NULL)
2484 return NULL;
2485
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002486 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002487
2488 /* are we a "%s.%d" name (platform and SPI components) */
2489 found = strstr(name, dev->driver->name);
2490 if (found) {
2491 /* get ID */
2492 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2493
2494 /* discard ID from name if ID == -1 */
2495 if (*id == -1)
2496 found[strlen(dev->driver->name)] = '\0';
2497 }
2498
2499 } else {
2500 /* I2C component devices are named "bus-addr" */
2501 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2502 char tmp[NAME_SIZE];
2503
2504 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002505 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002506
2507 /* sanitize component name for DAI link creation */
2508 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002509 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002510 } else
2511 *id = 0;
2512 }
2513
2514 return kstrdup(name, GFP_KERNEL);
2515}
2516
2517/*
2518 * Simplify DAI link naming for single devices with multiple DAIs by removing
2519 * any ".-1" and using the DAI name (instead of device name).
2520 */
2521static inline char *fmt_multiple_name(struct device *dev,
2522 struct snd_soc_dai_driver *dai_drv)
2523{
2524 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002525 dev_err(dev,
2526 "ASoC: error - multiple DAI %s registered with no name\n",
2527 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002528 return NULL;
2529 }
2530
2531 return kstrdup(dai_drv->name, GFP_KERNEL);
2532}
2533
Mark Brown91151712008-11-30 23:31:24 +00002534/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002535 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002536 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002537 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002538 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002539static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002540{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002541 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002542
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002543 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002544 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2545 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002546 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002547 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002548 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002549 }
Mark Brown91151712008-11-30 23:31:24 +00002550}
Mark Brown91151712008-11-30 23:31:24 +00002551
2552/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002553 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002554 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002555 * @component: The component the DAIs are registered for
2556 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002557 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002558 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2559 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002560 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002561static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002562 struct snd_soc_dai_driver *dai_drv, size_t count,
2563 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00002564{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002565 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002566 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002567 unsigned int i;
2568 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002569
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002570 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002571
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002572 component->dai_drv = dai_drv;
2573 component->num_dai = count;
2574
Mark Brown91151712008-11-30 23:31:24 +00002575 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002576
2577 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08002578 if (dai == NULL) {
2579 ret = -ENOMEM;
2580 goto err;
2581 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002582
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002583 /*
2584 * Back in the old days when we still had component-less DAIs,
2585 * instead of having a static name, component-less DAIs would
2586 * inherit the name of the parent device so it is possible to
2587 * register multiple instances of the DAI. We still need to keep
2588 * the same naming style even though those DAIs are not
2589 * component-less anymore.
2590 */
2591 if (count == 1 && legacy_dai_naming) {
2592 dai->name = fmt_single_name(dev, &dai->id);
2593 } else {
2594 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2595 if (dai_drv[i].id)
2596 dai->id = dai_drv[i].id;
2597 else
2598 dai->id = i;
2599 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002600 if (dai->name == NULL) {
2601 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002602 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00002603 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002604 }
2605
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002606 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002607 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002608 dai->driver = &dai_drv[i];
2609 if (!dai->driver->ops)
2610 dai->driver->ops = &null_dai_ops;
2611
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01002612 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01002613
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002614 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002615 }
2616
2617 return 0;
2618
2619err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002620 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002621
2622 return ret;
2623}
Mark Brown91151712008-11-30 23:31:24 +00002624
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002625static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2626 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002627{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002628 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002629
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002630 component->driver->seq_notifier(component, type, subseq);
2631}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002632
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002633static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2634 int event)
2635{
2636 struct snd_soc_component *component = dapm->component;
2637
2638 return component->driver->stream_event(component, event);
2639}
2640
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002641static int snd_soc_component_initialize(struct snd_soc_component *component,
2642 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002643{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002644 struct snd_soc_dapm_context *dapm;
2645
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002646 component->name = fmt_single_name(dev, &component->id);
2647 if (!component->name) {
2648 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002649 return -ENOMEM;
2650 }
2651
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002652 component->dev = dev;
2653 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002654 component->probe = component->driver->probe;
2655 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002656
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002657 if (!component->dapm_ptr)
2658 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002659
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002660 dapm = component->dapm_ptr;
2661 dapm->dev = dev;
2662 dapm->component = component;
2663 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02002664 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002665 if (driver->seq_notifier)
2666 dapm->seq_notifier = snd_soc_component_seq_notifier;
2667 if (driver->stream_event)
2668 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002669
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002670 component->controls = driver->controls;
2671 component->num_controls = driver->num_controls;
2672 component->dapm_widgets = driver->dapm_widgets;
2673 component->num_dapm_widgets = driver->num_dapm_widgets;
2674 component->dapm_routes = driver->dapm_routes;
2675 component->num_dapm_routes = driver->num_dapm_routes;
2676
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002677 INIT_LIST_HEAD(&component->dai_list);
2678 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002679
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002680 return 0;
2681}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002682
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002683static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002684{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002685 int val_bytes = regmap_get_val_bytes(component->regmap);
2686
2687 /* Errors are legitimate for non-integer byte multiples */
2688 if (val_bytes > 0)
2689 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002690}
2691
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002692#ifdef CONFIG_REGMAP
2693
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002694/**
2695 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2696 * @component: The component for which to initialize the regmap instance
2697 * @regmap: The regmap instance that should be used by the component
2698 *
2699 * This function allows deferred assignment of the regmap instance that is
2700 * associated with the component. Only use this if the regmap instance is not
2701 * yet ready when the component is registered. The function must also be called
2702 * before the first IO attempt of the component.
2703 */
2704void snd_soc_component_init_regmap(struct snd_soc_component *component,
2705 struct regmap *regmap)
2706{
2707 component->regmap = regmap;
2708 snd_soc_component_setup_regmap(component);
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2711
2712/**
2713 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2714 * @component: The component for which to de-initialize the regmap instance
2715 *
2716 * Calls regmap_exit() on the regmap instance associated to the component and
2717 * removes the regmap instance from the component.
2718 *
2719 * This function should only be used if snd_soc_component_init_regmap() was used
2720 * to initialize the regmap instance.
2721 */
2722void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2723{
2724 regmap_exit(component->regmap);
2725 component->regmap = NULL;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2728
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002729#endif
2730
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002731static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2732{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002733 if (!component->write && !component->read) {
2734 if (!component->regmap)
2735 component->regmap = dev_get_regmap(component->dev, NULL);
2736 if (component->regmap)
2737 snd_soc_component_setup_regmap(component);
2738 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002739
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002740 list_add(&component->list, &component_list);
Liam Girdwood8a978232015-05-29 19:06:14 +01002741 INIT_LIST_HEAD(&component->dobj_list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002742}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002743
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002744static void snd_soc_component_add(struct snd_soc_component *component)
2745{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002746 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002747 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002748 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002749}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002750
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002751static void snd_soc_component_cleanup(struct snd_soc_component *component)
2752{
2753 snd_soc_unregister_dais(component);
2754 kfree(component->name);
2755}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002756
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002757static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2758{
2759 list_del(&component->list);
2760}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002761
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002762int snd_soc_register_component(struct device *dev,
2763 const struct snd_soc_component_driver *cmpnt_drv,
2764 struct snd_soc_dai_driver *dai_drv,
2765 int num_dai)
2766{
2767 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002768 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002769
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002770 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002771 if (!cmpnt) {
2772 dev_err(dev, "ASoC: Failed to allocate memory\n");
2773 return -ENOMEM;
2774 }
2775
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002776 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2777 if (ret)
2778 goto err_free;
2779
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002780 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002781 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002782
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002783 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2784 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002785 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002786 goto err_cleanup;
2787 }
2788
2789 snd_soc_component_add(cmpnt);
2790
2791 return 0;
2792
2793err_cleanup:
2794 snd_soc_component_cleanup(cmpnt);
2795err_free:
2796 kfree(cmpnt);
2797 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002798}
2799EXPORT_SYMBOL_GPL(snd_soc_register_component);
2800
2801/**
2802 * snd_soc_unregister_component - Unregister a component from the ASoC core
2803 *
2804 */
2805void snd_soc_unregister_component(struct device *dev)
2806{
2807 struct snd_soc_component *cmpnt;
2808
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002809 mutex_lock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002810 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002811 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002812 goto found;
2813 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002814 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002815 return;
2816
2817found:
Liam Girdwood8a978232015-05-29 19:06:14 +01002818 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002819 snd_soc_component_del_unlocked(cmpnt);
2820 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002821 snd_soc_component_cleanup(cmpnt);
2822 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002823}
2824EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2825
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002826static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002827{
2828 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2829
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002830 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002831}
2832
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002833static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002834{
2835 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2836
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002837 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002838}
2839
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002840/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002841 * snd_soc_add_platform - Add a platform to the ASoC core
2842 * @dev: The parent device for the platform
2843 * @platform: The platform to add
2844 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00002845 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002846int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01002847 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002848{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002849 int ret;
2850
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002851 ret = snd_soc_component_initialize(&platform->component,
2852 &platform_drv->component_driver, dev);
2853 if (ret)
2854 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002855
2856 platform->dev = dev;
2857 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002858
2859 if (platform_drv->probe)
2860 platform->component.probe = snd_soc_platform_drv_probe;
2861 if (platform_drv->remove)
2862 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002863
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02002864#ifdef CONFIG_DEBUG_FS
2865 platform->component.debugfs_prefix = "platform";
2866#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00002867
2868 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002869 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002870 list_add(&platform->list, &platform_list);
2871 mutex_unlock(&client_mutex);
2872
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002873 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2874 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002875
2876 return 0;
2877}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002878EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2879
2880/**
2881 * snd_soc_register_platform - Register a platform with the ASoC core
2882 *
2883 * @platform: platform to register
2884 */
2885int snd_soc_register_platform(struct device *dev,
2886 const struct snd_soc_platform_driver *platform_drv)
2887{
2888 struct snd_soc_platform *platform;
2889 int ret;
2890
2891 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2892
2893 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2894 if (platform == NULL)
2895 return -ENOMEM;
2896
2897 ret = snd_soc_add_platform(dev, platform, platform_drv);
2898 if (ret)
2899 kfree(platform);
2900
2901 return ret;
2902}
Mark Brown12a48a8c2008-12-03 19:40:30 +00002903EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2904
2905/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002906 * snd_soc_remove_platform - Remove a platform from the ASoC core
2907 * @platform: the platform to remove
2908 */
2909void snd_soc_remove_platform(struct snd_soc_platform *platform)
2910{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002911
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002912 mutex_lock(&client_mutex);
2913 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002914 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002915 mutex_unlock(&client_mutex);
2916
2917 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002918 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02002919
2920 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002921}
2922EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2923
2924struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2925{
2926 struct snd_soc_platform *platform;
2927
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002928 mutex_lock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002929 list_for_each_entry(platform, &platform_list, list) {
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002930 if (dev == platform->dev) {
2931 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002932 return platform;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002933 }
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002934 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002935 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002936
2937 return NULL;
2938}
2939EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2940
2941/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00002942 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2943 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002944 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002945 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002946void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002947{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002948 struct snd_soc_platform *platform;
2949
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002950 platform = snd_soc_lookup_platform(dev);
2951 if (!platform)
2952 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002954 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002955 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002956}
2957EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2958
Mark Brown151ab222009-05-09 16:22:58 +01002959static u64 codec_format_map[] = {
2960 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2961 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2962 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2963 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2964 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2965 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2966 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2967 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2968 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2969 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2970 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2971 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2972 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2973 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2974 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2975 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2976};
2977
2978/* Fix up the DAI formats for endianness: codecs don't actually see
2979 * the endianness of the data but we're using the CPU format
2980 * definitions which do need to include endianness so we ensure that
2981 * codec DAIs always have both big and little endian variants set.
2982 */
2983static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2984{
2985 int i;
2986
2987 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2988 if (stream->formats & codec_format_map[i])
2989 stream->formats |= codec_format_map[i];
2990}
2991
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002992static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2993{
2994 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2995
2996 return codec->driver->probe(codec);
2997}
2998
2999static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3000{
3001 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3002
3003 codec->driver->remove(codec);
3004}
3005
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003006static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3007 unsigned int reg, unsigned int val)
3008{
3009 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3010
3011 return codec->driver->write(codec, reg, val);
3012}
3013
3014static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3015 unsigned int reg, unsigned int *val)
3016{
3017 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3018
3019 *val = codec->driver->read(codec, reg);
3020
3021 return 0;
3022}
3023
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003024static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3025 enum snd_soc_bias_level level)
3026{
3027 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3028
3029 return codec->driver->set_bias_level(codec, level);
3030}
3031
Mark Brown0d0cf002008-12-10 14:32:45 +00003032/**
3033 * snd_soc_register_codec - Register a codec with the ASoC core
3034 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003035 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003036 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003038 const struct snd_soc_codec_driver *codec_drv,
3039 struct snd_soc_dai_driver *dai_drv,
3040 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003041{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003042 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003043 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003044 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003045
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003046 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3049 if (codec == NULL)
3050 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003051
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003052 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003053 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003054
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003055 ret = snd_soc_component_initialize(&codec->component,
3056 &codec_drv->component_driver, dev);
3057 if (ret)
3058 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003059
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003060 if (codec_drv->controls) {
3061 codec->component.controls = codec_drv->controls;
3062 codec->component.num_controls = codec_drv->num_controls;
3063 }
3064 if (codec_drv->dapm_widgets) {
3065 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3066 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3067 }
3068 if (codec_drv->dapm_routes) {
3069 codec->component.dapm_routes = codec_drv->dapm_routes;
3070 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3071 }
3072
3073 if (codec_drv->probe)
3074 codec->component.probe = snd_soc_codec_drv_probe;
3075 if (codec_drv->remove)
3076 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003077 if (codec_drv->write)
3078 codec->component.write = snd_soc_codec_drv_write;
3079 if (codec_drv->read)
3080 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003081 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003082 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02003083 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003084 if (codec_drv->seq_notifier)
3085 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003086 if (codec_drv->set_bias_level)
3087 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088 codec->dev = dev;
3089 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003090 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003092#ifdef CONFIG_DEBUG_FS
3093 codec->component.init_debugfs = soc_init_codec_debugfs;
3094 codec->component.debugfs_prefix = "codec";
3095#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003096
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003097 if (codec_drv->get_regmap)
3098 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003099
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003100 for (i = 0; i < num_dai; i++) {
3101 fixup_codec_formats(&dai_drv[i].playback);
3102 fixup_codec_formats(&dai_drv[i].capture);
3103 }
3104
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003105 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3106 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003107 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003108 goto err_cleanup;
3109 }
3110
3111 list_for_each_entry(dai, &codec->component.dai_list, list)
3112 dai->codec = codec;
3113
Mark Brown054880f2012-04-09 17:29:19 +01003114 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003115 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003116 list_add(&codec->list, &codec_list);
3117 mutex_unlock(&client_mutex);
3118
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003119 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3120 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003121 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003123err_cleanup:
3124 snd_soc_component_cleanup(&codec->component);
3125err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003126 kfree(codec);
3127 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003128}
3129EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3130
3131/**
3132 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3133 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003134 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003135 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003136void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003137{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003138 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003139
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003140 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003141 list_for_each_entry(codec, &codec_list, list) {
3142 if (dev == codec->dev)
3143 goto found;
3144 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003145 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003146 return;
3147
3148found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003149 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003150 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003151 mutex_unlock(&client_mutex);
3152
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003153 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3154 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003156 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003157 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003158 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003159}
3160EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3161
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003162/* Retrieve a card's name from device tree */
3163int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3164 const char *propname)
3165{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303166 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003167 int ret;
3168
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303169 if (!card->dev) {
3170 pr_err("card->dev is not set before calling %s\n", __func__);
3171 return -EINVAL;
3172 }
3173
3174 np = card->dev->of_node;
3175
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003176 ret = of_property_read_string_index(np, propname, 0, &card->name);
3177 /*
3178 * EINVAL means the property does not exist. This is fine providing
3179 * card->name was previously set, which is checked later in
3180 * snd_soc_register_card.
3181 */
3182 if (ret < 0 && ret != -EINVAL) {
3183 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003184 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003185 propname, ret);
3186 return ret;
3187 }
3188
3189 return 0;
3190}
3191EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3192
Xiubo Li9a6d4862014-02-08 15:59:52 +08003193static const struct snd_soc_dapm_widget simple_widgets[] = {
3194 SND_SOC_DAPM_MIC("Microphone", NULL),
3195 SND_SOC_DAPM_LINE("Line", NULL),
3196 SND_SOC_DAPM_HP("Headphone", NULL),
3197 SND_SOC_DAPM_SPK("Speaker", NULL),
3198};
3199
3200int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3201 const char *propname)
3202{
3203 struct device_node *np = card->dev->of_node;
3204 struct snd_soc_dapm_widget *widgets;
3205 const char *template, *wname;
3206 int i, j, num_widgets, ret;
3207
3208 num_widgets = of_property_count_strings(np, propname);
3209 if (num_widgets < 0) {
3210 dev_err(card->dev,
3211 "ASoC: Property '%s' does not exist\n", propname);
3212 return -EINVAL;
3213 }
3214 if (num_widgets & 1) {
3215 dev_err(card->dev,
3216 "ASoC: Property '%s' length is not even\n", propname);
3217 return -EINVAL;
3218 }
3219
3220 num_widgets /= 2;
3221 if (!num_widgets) {
3222 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3223 propname);
3224 return -EINVAL;
3225 }
3226
3227 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3228 GFP_KERNEL);
3229 if (!widgets) {
3230 dev_err(card->dev,
3231 "ASoC: Could not allocate memory for widgets\n");
3232 return -ENOMEM;
3233 }
3234
3235 for (i = 0; i < num_widgets; i++) {
3236 ret = of_property_read_string_index(np, propname,
3237 2 * i, &template);
3238 if (ret) {
3239 dev_err(card->dev,
3240 "ASoC: Property '%s' index %d read error:%d\n",
3241 propname, 2 * i, ret);
3242 return -EINVAL;
3243 }
3244
3245 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3246 if (!strncmp(template, simple_widgets[j].name,
3247 strlen(simple_widgets[j].name))) {
3248 widgets[i] = simple_widgets[j];
3249 break;
3250 }
3251 }
3252
3253 if (j >= ARRAY_SIZE(simple_widgets)) {
3254 dev_err(card->dev,
3255 "ASoC: DAPM widget '%s' is not supported\n",
3256 template);
3257 return -EINVAL;
3258 }
3259
3260 ret = of_property_read_string_index(np, propname,
3261 (2 * i) + 1,
3262 &wname);
3263 if (ret) {
3264 dev_err(card->dev,
3265 "ASoC: Property '%s' index %d read error:%d\n",
3266 propname, (2 * i) + 1, ret);
3267 return -EINVAL;
3268 }
3269
3270 widgets[i].name = wname;
3271 }
3272
Nicolin Chenf23e8602015-02-14 17:22:49 -08003273 card->of_dapm_widgets = widgets;
3274 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003275
3276 return 0;
3277}
3278EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3279
Xiubo Li89c67852014-02-14 09:34:35 +08003280int snd_soc_of_parse_tdm_slot(struct device_node *np,
3281 unsigned int *slots,
3282 unsigned int *slot_width)
3283{
3284 u32 val;
3285 int ret;
3286
3287 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3288 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3289 if (ret)
3290 return ret;
3291
3292 if (slots)
3293 *slots = val;
3294 }
3295
3296 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3297 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3298 if (ret)
3299 return ret;
3300
3301 if (slot_width)
3302 *slot_width = val;
3303 }
3304
3305 return 0;
3306}
3307EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3308
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003309int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3310 const char *propname)
3311{
3312 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003313 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003314 struct snd_soc_dapm_route *routes;
3315 int i, ret;
3316
3317 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003318 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003319 dev_err(card->dev,
3320 "ASoC: Property '%s' does not exist or its length is not even\n",
3321 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003322 return -EINVAL;
3323 }
3324 num_routes /= 2;
3325 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003326 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003327 propname);
3328 return -EINVAL;
3329 }
3330
Mark Browne3b1e6a2014-12-18 11:46:38 +00003331 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003332 GFP_KERNEL);
3333 if (!routes) {
3334 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003335 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003336 return -EINVAL;
3337 }
3338
3339 for (i = 0; i < num_routes; i++) {
3340 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003341 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003342 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003343 dev_err(card->dev,
3344 "ASoC: Property '%s' index %d could not be read: %d\n",
3345 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003346 return -EINVAL;
3347 }
3348 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003349 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003350 if (ret) {
3351 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003352 "ASoC: Property '%s' index %d could not be read: %d\n",
3353 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003354 return -EINVAL;
3355 }
3356 }
3357
Nicolin Chenf23e8602015-02-14 17:22:49 -08003358 card->num_of_dapm_routes = num_routes;
3359 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003360
3361 return 0;
3362}
3363EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3364
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003365unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003366 const char *prefix,
3367 struct device_node **bitclkmaster,
3368 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003369{
3370 int ret, i;
3371 char prop[128];
3372 unsigned int format = 0;
3373 int bit, frame;
3374 const char *str;
3375 struct {
3376 char *name;
3377 unsigned int val;
3378 } of_fmt_table[] = {
3379 { "i2s", SND_SOC_DAIFMT_I2S },
3380 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3381 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3382 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3383 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3384 { "ac97", SND_SOC_DAIFMT_AC97 },
3385 { "pdm", SND_SOC_DAIFMT_PDM},
3386 { "msb", SND_SOC_DAIFMT_MSB },
3387 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003388 };
3389
3390 if (!prefix)
3391 prefix = "";
3392
3393 /*
3394 * check "[prefix]format = xxx"
3395 * SND_SOC_DAIFMT_FORMAT_MASK area
3396 */
3397 snprintf(prop, sizeof(prop), "%sformat", prefix);
3398 ret = of_property_read_string(np, prop, &str);
3399 if (ret == 0) {
3400 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3401 if (strcmp(str, of_fmt_table[i].name) == 0) {
3402 format |= of_fmt_table[i].val;
3403 break;
3404 }
3405 }
3406 }
3407
3408 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003409 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003410 * SND_SOC_DAIFMT_CLOCK_MASK area
3411 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003412 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3413 if (of_get_property(np, prop, NULL))
3414 format |= SND_SOC_DAIFMT_CONT;
3415 else
3416 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003417
3418 /*
3419 * check "[prefix]bitclock-inversion"
3420 * check "[prefix]frame-inversion"
3421 * SND_SOC_DAIFMT_INV_MASK area
3422 */
3423 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3424 bit = !!of_get_property(np, prop, NULL);
3425
3426 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3427 frame = !!of_get_property(np, prop, NULL);
3428
3429 switch ((bit << 4) + frame) {
3430 case 0x11:
3431 format |= SND_SOC_DAIFMT_IB_IF;
3432 break;
3433 case 0x10:
3434 format |= SND_SOC_DAIFMT_IB_NF;
3435 break;
3436 case 0x01:
3437 format |= SND_SOC_DAIFMT_NB_IF;
3438 break;
3439 default:
3440 /* SND_SOC_DAIFMT_NB_NF is default */
3441 break;
3442 }
3443
3444 /*
3445 * check "[prefix]bitclock-master"
3446 * check "[prefix]frame-master"
3447 * SND_SOC_DAIFMT_MASTER_MASK area
3448 */
3449 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3450 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003451 if (bit && bitclkmaster)
3452 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003453
3454 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3455 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003456 if (frame && framemaster)
3457 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003458
3459 switch ((bit << 4) + frame) {
3460 case 0x11:
3461 format |= SND_SOC_DAIFMT_CBM_CFM;
3462 break;
3463 case 0x10:
3464 format |= SND_SOC_DAIFMT_CBM_CFS;
3465 break;
3466 case 0x01:
3467 format |= SND_SOC_DAIFMT_CBS_CFM;
3468 break;
3469 default:
3470 format |= SND_SOC_DAIFMT_CBS_CFS;
3471 break;
3472 }
3473
3474 return format;
3475}
3476EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3477
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003478static int snd_soc_get_dai_name(struct of_phandle_args *args,
3479 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003480{
3481 struct snd_soc_component *pos;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003482 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003483
3484 mutex_lock(&client_mutex);
3485 list_for_each_entry(pos, &component_list, list) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003486 if (pos->dev->of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003487 continue;
3488
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003489 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003490 ret = pos->driver->of_xlate_dai_name(pos,
3491 args,
3492 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003493 } else {
3494 int id = -1;
3495
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003496 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003497 case 0:
3498 id = 0; /* same as dai_drv[0] */
3499 break;
3500 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003501 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003502 break;
3503 default:
3504 /* not supported */
3505 break;
3506 }
3507
3508 if (id < 0 || id >= pos->num_dai) {
3509 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003510 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003511 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003512
3513 ret = 0;
3514
3515 *dai_name = pos->dai_drv[id].name;
3516 if (!*dai_name)
3517 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003518 }
3519
Kuninori Morimotocb470082013-09-10 17:39:56 -07003520 break;
3521 }
3522 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003523 return ret;
3524}
3525
3526int snd_soc_of_get_dai_name(struct device_node *of_node,
3527 const char **dai_name)
3528{
3529 struct of_phandle_args args;
3530 int ret;
3531
3532 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3533 "#sound-dai-cells", 0, &args);
3534 if (ret)
3535 return ret;
3536
3537 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003538
3539 of_node_put(args.np);
3540
3541 return ret;
3542}
3543EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3544
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003545/*
3546 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3547 * @dev: Card device
3548 * @of_node: Device node
3549 * @dai_link: DAI link
3550 *
3551 * Builds an array of CODEC DAI components from the DAI link property
3552 * 'sound-dai'.
3553 * The array is set in the DAI link and the number of DAIs is set accordingly.
3554 * The device nodes in the array (of_node) must be dereferenced by the caller.
3555 *
3556 * Returns 0 for success
3557 */
3558int snd_soc_of_get_dai_link_codecs(struct device *dev,
3559 struct device_node *of_node,
3560 struct snd_soc_dai_link *dai_link)
3561{
3562 struct of_phandle_args args;
3563 struct snd_soc_dai_link_component *component;
3564 char *name;
3565 int index, num_codecs, ret;
3566
3567 /* Count the number of CODECs */
3568 name = "sound-dai";
3569 num_codecs = of_count_phandle_with_args(of_node, name,
3570 "#sound-dai-cells");
3571 if (num_codecs <= 0) {
3572 if (num_codecs == -ENOENT)
3573 dev_err(dev, "No 'sound-dai' property\n");
3574 else
3575 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3576 return num_codecs;
3577 }
3578 component = devm_kzalloc(dev,
3579 sizeof *component * num_codecs,
3580 GFP_KERNEL);
3581 if (!component)
3582 return -ENOMEM;
3583 dai_link->codecs = component;
3584 dai_link->num_codecs = num_codecs;
3585
3586 /* Parse the list */
3587 for (index = 0, component = dai_link->codecs;
3588 index < dai_link->num_codecs;
3589 index++, component++) {
3590 ret = of_parse_phandle_with_args(of_node, name,
3591 "#sound-dai-cells",
3592 index, &args);
3593 if (ret)
3594 goto err;
3595 component->of_node = args.np;
3596 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3597 if (ret < 0)
3598 goto err;
3599 }
3600 return 0;
3601err:
3602 for (index = 0, component = dai_link->codecs;
3603 index < dai_link->num_codecs;
3604 index++, component++) {
3605 if (!component->of_node)
3606 break;
3607 of_node_put(component->of_node);
3608 component->of_node = NULL;
3609 }
3610 dai_link->codecs = NULL;
3611 dai_link->num_codecs = 0;
3612 return ret;
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3615
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003616static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003617{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003618 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003619 snd_soc_util_init();
3620
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003621 return platform_driver_register(&soc_driver);
3622}
Mark Brown4abe8e12010-10-12 17:41:03 +01003623module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003624
Mark Brown7d8c16a2008-11-30 22:11:24 +00003625static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003626{
Mark Brownfb257892011-04-28 10:57:54 +01003627 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003628 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003629
Mark Brown384c89e2008-12-03 17:34:03 +00003630#ifdef CONFIG_DEBUG_FS
Mark Brown384c89e2008-12-03 17:34:03 +00003631#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003632 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003633}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003634module_exit(snd_soc_exit);
3635
3636/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003637MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003638MODULE_DESCRIPTION("ALSA SoC Core");
3639MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003640MODULE_ALIAS("platform:soc-audio");