blob: 95b5f034d864ecf9e128f8c99792b20b5e84256c [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>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000056static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000057static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070058static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000059
Frank Mandarinodb2a4162006-10-06 18:31:09 +020060/*
61 * This is a timeout to do a DAPM powerdown after a stream is closed().
62 * It can be used to eliminate pops between different playback streams, e.g.
63 * between two audio tracks.
64 */
65static int pmdown_time = 5000;
66module_param(pmdown_time, int, 0);
67MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
68
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000069/* returns the minimum number of bytes needed to represent
70 * a particular given value */
71static int min_bytes_needed(unsigned long val)
72{
73 int c = 0;
74 int i;
75
76 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
77 if (val & (1UL << i))
78 break;
79 c = (sizeof val * 8) - c;
80 if (!c || (c % 8))
81 c = (c + 8) / 8;
82 else
83 c /= 8;
84 return c;
85}
86
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000087/* fill buf which is 'len' bytes with a formatted
88 * string of the form 'reg: value\n' */
89static int format_register_str(struct snd_soc_codec *codec,
90 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000091{
Stephen Warren00b317a2011-04-01 14:50:44 -060092 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
93 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000094 int ret;
95 char tmpbuf[len + 1];
96 char regbuf[regsize + 1];
97
98 /* since tmpbuf is allocated on the stack, warn the callers if they
99 * try to abuse this function */
100 WARN_ON(len > 63);
101
102 /* +2 for ': ' and + 1 for '\n' */
103 if (wordsize + regsize + 2 + 1 != len)
104 return -EINVAL;
105
Mark Brown25032c12011-08-01 13:52:48 +0900106 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 if (ret < 0) {
108 memset(regbuf, 'X', regsize);
109 regbuf[regsize] = '\0';
110 } else {
111 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
112 }
113
114 /* prepare the buffer */
115 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
116 /* copy it back to the caller without the '\0' */
117 memcpy(buf, tmpbuf, len);
118
119 return 0;
120}
121
122/* codec register dump */
123static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
124 size_t count, loff_t pos)
125{
126 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000127 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000128 int len;
129 size_t total = 0;
130 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000131
Stephen Warren00b317a2011-04-01 14:50:44 -0600132 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
133 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000134
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000135 len = wordsize + regsize + 2 + 1;
136
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000137 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000138 return 0;
139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 if (codec->driver->reg_cache_step)
141 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100144 /* only support larger than PAGE_SIZE bytes debugfs
145 * entries for the default case */
146 if (p >= pos) {
147 if (total + len >= count - 1)
148 break;
149 format_register_str(codec, i, buf + total, len);
150 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100151 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100152 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000153 }
154
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000155 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000156
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000157 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000158}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000159
Mark Brown2624d5f2009-11-03 21:56:13 +0000160static ssize_t codec_reg_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
Mark Brown36ae1a92012-01-06 17:12:45 -0800163 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000166}
167
168static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
169
Mark Browndbe21402010-02-12 11:37:24 +0000170static ssize_t pmdown_time_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
Mark Brown36ae1a92012-01-06 17:12:45 -0800173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000176}
177
178static ssize_t pmdown_time_set(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700183 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000184
Jingoo Hanb785a492013-07-19 16:24:59 +0900185 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700186 if (ret)
187 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000188
189 return count;
190}
191
192static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
193
Takashi Iwaid29697d2015-01-30 20:16:37 +0100194static struct attribute *soc_dev_attrs[] = {
195 &dev_attr_codec_reg.attr,
196 &dev_attr_pmdown_time.attr,
197 NULL
198};
199
200static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
201 struct attribute *attr, int idx)
202{
203 struct device *dev = kobj_to_dev(kobj);
204 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
205
206 if (attr == &dev_attr_pmdown_time.attr)
207 return attr->mode; /* always visible */
208 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
209}
210
211static const struct attribute_group soc_dapm_dev_group = {
212 .attrs = soc_dapm_dev_attrs,
213 .is_visible = soc_dev_attr_is_visible,
214};
215
216static const struct attribute_group soc_dev_roup = {
217 .attrs = soc_dev_attrs,
218 .is_visible = soc_dev_attr_is_visible,
219};
220
221static const struct attribute_group *soc_dev_attr_groups[] = {
222 &soc_dapm_dev_group,
223 &soc_dev_roup,
224 NULL
225};
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000228static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000229 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000230{
231 ssize_t ret;
232 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000233 char *buf;
234
235 if (*ppos < 0 || !count)
236 return -EINVAL;
237
238 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000239 if (!buf)
240 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000241
242 ret = soc_codec_reg_show(codec, buf, count, *ppos);
243 if (ret >= 0) {
244 if (copy_to_user(user_buf, buf, ret)) {
245 kfree(buf);
246 return -EFAULT;
247 }
248 *ppos += ret;
249 }
250
Mark Brown2624d5f2009-11-03 21:56:13 +0000251 kfree(buf);
252 return ret;
253}
254
255static ssize_t codec_reg_write_file(struct file *file,
256 const char __user *user_buf, size_t count, loff_t *ppos)
257{
258 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700259 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000260 char *start = buf;
261 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900263 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000264
265 buf_size = min(count, (sizeof(buf)-1));
266 if (copy_from_user(buf, user_buf, buf_size))
267 return -EFAULT;
268 buf[buf_size] = 0;
269
Mark Brown2624d5f2009-11-03 21:56:13 +0000270 while (*start == ' ')
271 start++;
272 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 while (*start == ' ')
274 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900275 ret = kstrtoul(start, 16, &value);
276 if (ret)
277 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000278
279 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030280 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000281
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000282 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 return buf_size;
284}
285
286static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700287 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000288 .read = codec_reg_read_file,
289 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200290 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000291};
292
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200293static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100294{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200295 if (!component->card->debugfs_card_root)
296 return;
297
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200298 if (component->debugfs_prefix) {
299 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100300
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200301 name = kasprintf(GFP_KERNEL, "%s:%s",
302 component->debugfs_prefix, component->name);
303 if (name) {
304 component->debugfs_root = debugfs_create_dir(name,
305 component->card->debugfs_card_root);
306 kfree(name);
307 }
308 } else {
309 component->debugfs_root = debugfs_create_dir(component->name,
310 component->card->debugfs_card_root);
311 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100312
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200313 if (!component->debugfs_root) {
314 dev_warn(component->dev,
315 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000316 return;
317 }
318
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200319 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
320 component->debugfs_root);
321
322 if (component->init_debugfs)
323 component->init_debugfs(component);
324}
325
326static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
327{
328 debugfs_remove_recursive(component->debugfs_root);
329}
330
331static void soc_init_codec_debugfs(struct snd_soc_component *component)
332{
333 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
334
Mark Brown2624d5f2009-11-03 21:56:13 +0000335 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200336 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000337 codec, &codec_reg_fops);
338 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200339 dev_warn(codec->dev,
340 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000341}
342
Mark Brownc3c5a192010-09-15 18:15:14 +0100343static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
344 size_t count, loff_t *ppos)
345{
346 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100347 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100348 struct snd_soc_codec *codec;
349
350 if (!buf)
351 return -ENOMEM;
352
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100353 mutex_lock(&client_mutex);
354
Mark Brown2b194f9d2010-10-13 10:52:16 +0100355 list_for_each_entry(codec, &codec_list, list) {
356 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200357 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100358 if (len >= 0)
359 ret += len;
360 if (ret > PAGE_SIZE) {
361 ret = PAGE_SIZE;
362 break;
363 }
364 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100365
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100366 mutex_unlock(&client_mutex);
367
Mark Brownc3c5a192010-09-15 18:15:14 +0100368 if (ret >= 0)
369 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
370
371 kfree(buf);
372
373 return ret;
374}
375
376static const struct file_operations codec_list_fops = {
377 .read = codec_list_read_file,
378 .llseek = default_llseek,/* read accesses f_pos */
379};
380
Mark Brownf3208782010-09-15 18:19:07 +0100381static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
382 size_t count, loff_t *ppos)
383{
384 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100385 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100386 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100387 struct snd_soc_dai *dai;
388
389 if (!buf)
390 return -ENOMEM;
391
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100392 mutex_lock(&client_mutex);
393
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100394 list_for_each_entry(component, &component_list, list) {
395 list_for_each_entry(dai, &component->dai_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 dai->name);
398 if (len >= 0)
399 ret += len;
400 if (ret > PAGE_SIZE) {
401 ret = PAGE_SIZE;
402 break;
403 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100404 }
405 }
Mark Brownf3208782010-09-15 18:19:07 +0100406
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100407 mutex_unlock(&client_mutex);
408
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100410
411 kfree(buf);
412
413 return ret;
414}
415
416static const struct file_operations dai_list_fops = {
417 .read = dai_list_read_file,
418 .llseek = default_llseek,/* read accesses f_pos */
419};
420
Mark Brown19c7ac22010-09-15 18:22:40 +0100421static ssize_t platform_list_read_file(struct file *file,
422 char __user *user_buf,
423 size_t count, loff_t *ppos)
424{
425 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100426 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100427 struct snd_soc_platform *platform;
428
429 if (!buf)
430 return -ENOMEM;
431
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100432 mutex_lock(&client_mutex);
433
Mark Brown2b194f9d2010-10-13 10:52:16 +0100434 list_for_each_entry(platform, &platform_list, list) {
435 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200436 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100437 if (len >= 0)
438 ret += len;
439 if (ret > PAGE_SIZE) {
440 ret = PAGE_SIZE;
441 break;
442 }
443 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100444
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100445 mutex_unlock(&client_mutex);
446
Mark Brown2b194f9d2010-10-13 10:52:16 +0100447 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100448
449 kfree(buf);
450
451 return ret;
452}
453
454static const struct file_operations platform_list_fops = {
455 .read = platform_list_read_file,
456 .llseek = default_llseek,/* read accesses f_pos */
457};
458
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200459static void soc_init_card_debugfs(struct snd_soc_card *card)
460{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200461 if (!snd_soc_debugfs_root)
462 return;
463
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200464 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000465 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200466 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200467 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100468 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200469 return;
470 }
471
472 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
473 card->debugfs_card_root,
474 &card->pop_time);
475 if (!card->debugfs_pop_time)
476 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000477 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200478}
479
480static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
481{
482 debugfs_remove_recursive(card->debugfs_card_root);
483}
484
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200485
486static void snd_soc_debugfs_init(void)
487{
488 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
489 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
490 pr_warn("ASoC: Failed to create debugfs directory\n");
491 snd_soc_debugfs_root = NULL;
492 return;
493 }
494
495 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
496 &codec_list_fops))
497 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
498
499 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
500 &dai_list_fops))
501 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
502
503 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
504 &platform_list_fops))
505 pr_warn("ASoC: Failed to create platform list debugfs file\n");
506}
507
508static void snd_soc_debugfs_exit(void)
509{
510 debugfs_remove_recursive(snd_soc_debugfs_root);
511}
512
Mark Brown2624d5f2009-11-03 21:56:13 +0000513#else
514
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200515#define soc_init_codec_debugfs NULL
516
517static inline void soc_init_component_debugfs(
518 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000519{
520}
521
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200522static inline void soc_cleanup_component_debugfs(
523 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000524{
525}
526
Axel Linb95fccb2010-11-09 17:06:44 +0800527static inline void soc_init_card_debugfs(struct snd_soc_card *card)
528{
529}
530
531static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
532{
533}
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +0200534
535static inline void snd_soc_debugfs_init(void)
536{
537}
538
539static inline void snd_soc_debugfs_exit(void)
540{
541}
542
Mark Brown2624d5f2009-11-03 21:56:13 +0000543#endif
544
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100545struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
546 const char *dai_link, int stream)
547{
548 int i;
549
550 for (i = 0; i < card->num_links; i++) {
551 if (card->rtd[i].dai_link->no_pcm &&
552 !strcmp(card->rtd[i].dai_link->name, dai_link))
553 return card->rtd[i].pcm->streams[stream].substream;
554 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000555 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100556 return NULL;
557}
558EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
559
560struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
561 const char *dai_link)
562{
563 int i;
564
565 for (i = 0; i < card->num_links; i++) {
566 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
567 return &card->rtd[i];
568 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000569 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100570 return NULL;
571}
572EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
573
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100574static void codec2codec_close_delayed_work(struct work_struct *work)
575{
576 /* Currently nothing to do for c2c links
577 * Since c2c links are internal nodes in the DAPM graph and
578 * don't interface with the outside world or application layer
579 * we don't have to do any special handling on close.
580 */
581}
582
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000583#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000585int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000587 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200588 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200589 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200591 /* If the card is not initialized yet there is nothing to do */
592 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200593 return 0;
594
Andy Green6ed25972008-06-13 16:24:05 +0100595 /* Due to the resume being scheduled into a workqueue we could
596 * suspend before that's finished - wait for it to complete.
597 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 snd_power_lock(card->snd_card);
599 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
600 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100601
602 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100604
Mark Browna00f90f2010-12-02 16:24:24 +0000605 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100609 continue;
610
Benoit Cousson88bd8702014-07-08 23:19:34 +0200611 for (j = 0; j < card->rtd[i].num_codecs; j++) {
612 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
613 struct snd_soc_dai_driver *drv = dai->driver;
614
615 if (drv->ops->digital_mute && dai->playback_active)
616 drv->ops->digital_mute(dai, 1);
617 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200618 }
619
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100620 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 for (i = 0; i < card->num_rtd; i++) {
622 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100623 continue;
624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100626 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100627
Mark Brown87506542008-11-18 20:50:34 +0000628 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000629 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 for (i = 0; i < card->num_rtd; i++) {
632 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100635 continue;
636
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100637 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 cpu_dai->driver->suspend(cpu_dai);
Mark Brown1547aba2010-05-07 21:11:40 +0100639 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640
Lars-Peter Clausen37660b62015-03-30 21:04:50 +0200641 /* close any waiting streams */
642 for (i = 0; i < card->num_rtd; i++)
Tejun Heo43829732012-08-20 14:51:24 -0700643 flush_delayed_work(&card->rtd[i].delayed_work);
Mark Brown3efab7d2010-05-09 13:25:43 +0100644
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646
647 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100648 continue;
649
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800650 snd_soc_dapm_stream_event(&card->rtd[i],
651 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800652 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800654 snd_soc_dapm_stream_event(&card->rtd[i],
655 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800656 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 }
658
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200659 /* Recheck all endpoints too, their state is affected by suspend */
660 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700661 snd_soc_dapm_sync(&card->dapm);
662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200664 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 /* If there are paths active then the CODEC will be held with
666 * bias _ON and should not be suspended. */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200667 if (!codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200668 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000670 /*
671 * If the CODEC is capable of idle
672 * bias off then being in STANDBY
673 * means it's doing something,
674 * otherwise fall through.
675 */
676 if (codec->dapm.idle_bias_off) {
677 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200678 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000679 break;
680 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200683 if (codec->driver->suspend)
684 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 codec->suspended = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200686 if (codec->component.regmap)
687 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800688 /* deactivate pins to sleep state */
689 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 break;
691 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200692 dev_dbg(codec->dev,
693 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 break;
695 }
696 }
697 }
698
699 for (i = 0; i < card->num_rtd; i++) {
700 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
701
702 if (card->rtd[i].dai_link->ignore_suspend)
703 continue;
704
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100705 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800707
708 /* deactivate pins to sleep state */
709 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 }
711
Mark Brown87506542008-11-18 20:50:34 +0000712 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000713 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200714
715 return 0;
716}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000717EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718
Andy Green6ed25972008-06-13 16:24:05 +0100719/* deferred resume work, so resume can complete before we finished
720 * setting our codec back up, which can be very slow on I2C
721 */
722static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 struct snd_soc_card *card =
725 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200726 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200727 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728
Andy Green6ed25972008-06-13 16:24:05 +0100729 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
730 * so userspace apps are blocked from touching us
731 */
732
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000733 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100734
Mark Brown99497882010-05-07 20:24:05 +0100735 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100737
Mark Brown87506542008-11-18 20:50:34 +0000738 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000739 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100741 /* resume control bus DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 for (i = 0; i < card->num_rtd; i++) {
743 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100746 continue;
747
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100748 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200752 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200753 if (codec->suspended) {
Lars-Peter Clausenb8faaba2015-05-15 12:41:30 +0200754 if (codec->driver->resume)
755 codec->driver->resume(codec);
756 codec->suspended = 0;
Mark Brown1547aba2010-05-07 21:11:40 +0100757 }
758 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200759
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100763 continue;
764
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800765 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000766 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800767 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000768
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800769 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000770 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800771 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772 }
773
Mark Brown3ff3f642008-05-19 12:32:25 +0200774 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100778 continue;
779
Benoit Cousson88bd8702014-07-08 23:19:34 +0200780 for (j = 0; j < card->rtd[i].num_codecs; j++) {
781 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
782 struct snd_soc_dai_driver *drv = dai->driver;
783
784 if (drv->ops->digital_mute && dai->playback_active)
785 drv->ops->digital_mute(dai, 0);
786 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 }
788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 for (i = 0; i < card->num_rtd; i++) {
790 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100793 continue;
794
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100795 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797 }
798
Mark Brown87506542008-11-18 20:50:34 +0000799 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000800 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000802 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100803
804 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000805 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700806
Lars-Peter Clausen8be4da22014-10-25 17:42:01 +0200807 /* Recheck all endpoints too, their state is affected by suspend */
808 dapm_mark_endpoints_dirty(card);
Mark Browne2d32ff2012-08-31 17:38:32 -0700809 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100810}
811
812/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000813int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100814{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000815 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100816 bool bus_control = false;
817 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200818
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200819 /* If the card is not initialized yet there is nothing to do */
820 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800821 return 0;
822
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800823 /* activate pins from sleep state */
824 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200825 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
826 struct snd_soc_dai **codec_dais = rtd->codec_dais;
827 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
828 int j;
829
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800830 if (cpu_dai->active)
831 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200832
833 for (j = 0; j < rtd->num_codecs; j++) {
834 struct snd_soc_dai *codec_dai = codec_dais[j];
835 if (codec_dai->active)
836 pinctrl_pm_select_default_state(codec_dai->dev);
837 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800838 }
839
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100840 /*
841 * DAIs that also act as the control bus master might have other drivers
842 * hanging off them so need to resume immediately. Other drivers don't
843 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100844 * due to I/O costs and anti-pop so handle them out of line.
845 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 for (i = 0; i < card->num_rtd; i++) {
847 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100848 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600849 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100850 if (bus_control) {
851 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600852 soc_resume_deferred(&card->deferred_resume_work);
853 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000854 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600855 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000856 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100857 }
Andy Green6ed25972008-06-13 16:24:05 +0100858
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859 return 0;
860}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000861EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200862#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000863#define snd_soc_suspend NULL
864#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200865#endif
866
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100867static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800868};
869
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200870static struct snd_soc_component *soc_find_component(
871 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100872{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200873 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100874
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100875 lockdep_assert_held(&client_mutex);
876
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200877 list_for_each_entry(component, &component_list, list) {
878 if (of_node) {
879 if (component->dev->of_node == of_node)
880 return component;
881 } else if (strcmp(component->name, name) == 0) {
882 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100884 }
885
886 return NULL;
887}
888
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200889static struct snd_soc_dai *snd_soc_find_dai(
890 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200892 struct snd_soc_component *component;
893 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100894
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +0100895 lockdep_assert_held(&client_mutex);
896
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200897 /* Find CPU DAI from registered DAIs*/
898 list_for_each_entry(component, &component_list, list) {
899 if (dlc->of_node && component->dev->of_node != dlc->of_node)
900 continue;
Lars-Peter Clausen1ffae362014-10-29 21:46:30 +0100901 if (dlc->name && strcmp(component->name, dlc->name))
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200902 continue;
903 list_for_each_entry(dai, &component->dai_list, list) {
904 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100905 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200907 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100908 }
909 }
910
911 return NULL;
912}
913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
917 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200918 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200919 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000921 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100922 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200923 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200924
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000925 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000926
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200927 cpu_dai_component.name = dai_link->cpu_name;
928 cpu_dai_component.of_node = dai_link->cpu_of_node;
929 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
930 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000931 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000932 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000934 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000935 }
936
Benoit Cousson88bd8702014-07-08 23:19:34 +0200937 rtd->num_codecs = dai_link->num_codecs;
938
939 /* Find CODEC from registered CODECs */
940 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200941 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200942 if (!codec_dais[i]) {
943 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
944 codecs[i].dai_name);
945 return -EPROBE_DEFER;
946 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000947 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100948
Benoit Cousson88bd8702014-07-08 23:19:34 +0200949 /* Single codec links expect codec and codec_dai in runtime data */
950 rtd->codec_dai = codec_dais[0];
951 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100952
Mark Brown848dd8b2011-04-27 18:16:32 +0100953 /* if there's no platform we match on the empty platform */
954 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700955 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100956 platform_name = "snd-soc-dummy";
957
Mark Brownb19e6e72012-03-14 21:18:39 +0000958 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700960 if (dai_link->platform_of_node) {
961 if (platform->dev->of_node !=
962 dai_link->platform_of_node)
963 continue;
964 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200965 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700966 continue;
967 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700968
969 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000971 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000972 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000974 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000976
977 card->num_rtd++;
978
979 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980}
981
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200982static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600983{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200984 if (!component->probed)
985 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600986
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200987 /* This is a HACK and will be removed soon */
988 if (component->codec)
989 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600990
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200991 if (component->remove)
992 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600993
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200994 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600995
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200996 soc_cleanup_component_debugfs(component);
997 component->probed = 0;
998 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600999}
1000
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001001static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001002{
1003 int err;
1004
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001005 if (dai && dai->probed &&
1006 dai->driver->remove_order == order) {
1007 if (dai->driver->remove) {
1008 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001009 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001010 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001011 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001012 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001013 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001014 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001015 }
1016}
1017
Stephen Warren62ae68f2012-06-08 12:34:23 -06001018static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019{
1020 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001021 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022
1023 /* unregister the rtd device */
1024 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001025 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001026 rtd->dev_registered = 0;
1027 }
1028
1029 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001030 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001031 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001033 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034}
1035
Stephen Warren62ae68f2012-06-08 12:34:23 -06001036static void soc_remove_link_components(struct snd_soc_card *card, int num,
1037 int order)
1038{
1039 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1040 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001041 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001042 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001043 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001044
1045 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001046 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001047 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001048
1049 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001050 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001051 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001052 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001053 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001054 }
1055
1056 /* remove any CPU-side CODEC */
1057 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001058 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001059 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001060 }
1061}
1062
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001063static void soc_remove_dai_links(struct snd_soc_card *card)
1064{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001065 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001066
Liam Girdwood0168bf02011-06-07 16:08:05 +01001067 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1068 order++) {
1069 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001070 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001071 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001072
1073 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1074 order++) {
1075 for (dai = 0; dai < card->num_rtd; dai++)
1076 soc_remove_link_components(card, dai, order);
1077 }
1078
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001079 card->num_rtd = 0;
1080}
1081
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001082static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001083 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001084{
1085 int i;
1086
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001087 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001088 return;
1089
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001090 for (i = 0; i < card->num_configs; i++) {
1091 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001092 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001093 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001094 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001095 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001096 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001097 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001098 }
1099}
1100
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001101static int soc_probe_component(struct snd_soc_card *card,
1102 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001103{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001104 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001105 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001106 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001107
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001108 if (component->probed)
1109 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001110
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001111 component->card = card;
1112 dapm->card = card;
1113 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001115 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001116 return -ENODEV;
1117
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001118 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001119
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001120 if (component->dapm_widgets) {
1121 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1122 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001123
1124 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001125 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001126 "Failed to create new controls %d\n", ret);
1127 goto err_probe;
1128 }
1129 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001130
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001131 list_for_each_entry(dai, &component->dai_list, list) {
1132 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001133 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001134 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001135 "Failed to create DAI widgets %d\n", ret);
1136 goto err_probe;
1137 }
1138 }
Mark Brown888df392012-02-16 19:37:51 -08001139
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001140 if (component->probe) {
1141 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001143 dev_err(component->dev,
1144 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001145 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001146 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001147
1148 WARN(dapm->idle_bias_off &&
1149 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001150 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001151 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001152 }
1153
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001154 if (component->controls)
1155 snd_soc_add_component_controls(component, component->controls,
1156 component->num_controls);
1157 if (component->dapm_routes)
1158 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1159 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001160
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001161 component->probed = 1;
1162 list_add(&dapm->list, &card->dapm_list);
1163
1164 /* This is a HACK and will be removed soon */
1165 if (component->codec)
1166 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001167
Jarkko Nikula70d29332011-01-27 16:24:22 +02001168 return 0;
1169
1170err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001171 soc_cleanup_component_debugfs(component);
1172 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001173
1174 return ret;
1175}
1176
Mark Brown36ae1a92012-01-06 17:12:45 -08001177static void rtd_release(struct device *dev)
1178{
1179 kfree(dev);
1180}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001181
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001182static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1183 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001184{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001185 int ret = 0;
1186
Jarkko Nikula589c3562010-12-06 16:27:07 +02001187 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001188 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1189 if (!rtd->dev)
1190 return -ENOMEM;
1191 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001192 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001193 rtd->dev->release = rtd_release;
Takashi Iwaid29697d2015-01-30 20:16:37 +01001194 rtd->dev->groups = soc_dev_attr_groups;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001195 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001196 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001197 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001198 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1199 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1200 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1201 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001202 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001204 /* calling put_device() here to free the rtd->dev */
1205 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001206 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001207 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001208 return ret;
1209 }
1210 rtd->dev_registered = 1;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001211 return 0;
1212}
1213
Stephen Warren62ae68f2012-06-08 12:34:23 -06001214static int soc_probe_link_components(struct snd_soc_card *card, int num,
1215 int order)
1216{
1217 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001218 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001219 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001220 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001221
1222 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001223 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001224 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001225 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001226 if (ret < 0)
1227 return ret;
1228 }
1229
Benoit Cousson88bd8702014-07-08 23:19:34 +02001230 /* probe the CODEC-side components */
1231 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001232 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001233 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001234 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001235 if (ret < 0)
1236 return ret;
1237 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001238 }
1239
1240 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001241 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001242 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001243 if (ret < 0)
1244 return ret;
1245 }
1246
1247 return 0;
1248}
1249
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001250static int soc_probe_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001251{
1252 int ret;
1253
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001254 if (!dai->probed && dai->driver->probe_order == order) {
1255 if (dai->driver->probe) {
1256 ret = dai->driver->probe(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001257 if (ret < 0) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001258 dev_err(dai->dev,
1259 "ASoC: failed to probe DAI %s: %d\n",
1260 dai->name, ret);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001261 return ret;
1262 }
1263 }
1264
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001265 dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001266 }
1267
1268 return 0;
1269}
1270
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001271static int soc_link_dai_widgets(struct snd_soc_card *card,
1272 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001273 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001274{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001275 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1276 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001277 struct snd_soc_dapm_widget *play_w, *capture_w;
1278 int ret;
1279
Benoit Cousson88bd8702014-07-08 23:19:34 +02001280 if (rtd->num_codecs > 1)
1281 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1282
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001283 /* link the DAI widgets */
1284 play_w = codec_dai->playback_widget;
1285 capture_w = cpu_dai->capture_widget;
1286 if (play_w && capture_w) {
1287 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00001288 dai_link->num_params, capture_w,
1289 play_w);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001290 if (ret != 0) {
1291 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1292 play_w->name, capture_w->name, ret);
1293 return ret;
1294 }
1295 }
1296
1297 play_w = cpu_dai->playback_widget;
1298 capture_w = codec_dai->capture_widget;
1299 if (play_w && capture_w) {
1300 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Nikesh Oswalc6615082015-02-02 17:06:44 +00001301 dai_link->num_params, capture_w,
1302 play_w);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001303 if (ret != 0) {
1304 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1305 play_w->name, capture_w->name, ret);
1306 return ret;
1307 }
1308 }
1309
1310 return 0;
1311}
1312
Stephen Warren62ae68f2012-06-08 12:34:23 -06001313static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001314{
1315 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1316 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownc74184e2012-04-04 22:12:09 +01001317 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001318 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001319
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001320 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001321 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001322
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001323 /* set default power off timeout */
1324 rtd->pmdown_time = pmdown_time;
1325
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001326 ret = soc_probe_dai(cpu_dai, order);
1327 if (ret)
1328 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001331 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen8e2be562014-11-04 11:30:59 +01001332 ret = soc_probe_dai(rtd->codec_dais[i], order);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001333 if (ret)
1334 return ret;
1335 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001336
Liam Girdwood0168bf02011-06-07 16:08:05 +01001337 /* complete DAI probe during last probe */
1338 if (order != SND_SOC_COMP_ORDER_LAST)
1339 return 0;
1340
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001341 /* do machine specific initialization */
1342 if (dai_link->init) {
1343 ret = dai_link->init(rtd);
1344 if (ret < 0) {
1345 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1346 dai_link->name, ret);
1347 return ret;
1348 }
1349 }
1350
Kuninori Morimotoa5053a82015-04-10 09:47:00 +00001351 if (dai_link->dai_fmt)
1352 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1353
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001354 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001356 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001357
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001358#ifdef CONFIG_DEBUG_FS
1359 /* add DPCM sysfs entries */
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02001360 if (dai_link->dynamic)
1361 soc_dpcm_debugfs_add(rtd);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001362#endif
1363
Namarta Kohli1245b702012-08-16 17:10:41 +05301364 if (cpu_dai->driver->compress_dai) {
1365 /*create compress_device"*/
1366 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001367 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001368 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301369 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001370 return ret;
1371 }
1372 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301373
1374 if (!dai_link->params) {
1375 /* create the pcm */
1376 ret = soc_new_pcm(rtd, num);
1377 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001378 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301379 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001380 return ret;
1381 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301382 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001383 INIT_DELAYED_WORK(&rtd->delayed_work,
1384 codec2codec_close_delayed_work);
1385
Namarta Kohli1245b702012-08-16 17:10:41 +05301386 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001387 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001388 if (ret)
1389 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001390 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001391 }
1392
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001393 return 0;
1394}
1395
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001396static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001397{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001398 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001399 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001400 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001401
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001402 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1403 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001404 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001405 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001406
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001407 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001408 return -EPROBE_DEFER;
1409 }
1410
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001411 /*
1412 * Some places still reference rtd->codec, so we have to keep that
1413 * initialized if the component is a CODEC. Once all those references
1414 * have been removed, this code can be removed as well.
1415 */
1416 rtd->codec = rtd->component->codec;
1417
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001418 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001419}
1420
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001421static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1422{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001423 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001424 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001425 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001426
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001427 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001428 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001429 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001430
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001431 /* do machine specific initialization */
1432 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001433 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001434 if (ret < 0) {
1435 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1436 aux_dev->name, ret);
1437 return ret;
1438 }
1439 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001440
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001441 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001442}
1443
1444static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1445{
1446 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001447 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001448
1449 /* unregister the rtd device */
1450 if (rtd->dev_registered) {
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001451 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001452 rtd->dev_registered = 0;
1453 }
1454
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001455 if (component && component->probed)
1456 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001457}
1458
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001459static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001460{
1461 int ret;
1462
1463 if (codec->cache_init)
1464 return 0;
1465
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001466 ret = snd_soc_cache_init(codec);
1467 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001468 dev_err(codec->dev,
1469 "ASoC: Failed to set cache compression type: %d\n",
1470 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001471 return ret;
1472 }
1473 codec->cache_init = 1;
1474 return 0;
1475}
1476
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001477/**
1478 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1479 * @rtd: The runtime for which the DAI link format should be changed
1480 * @dai_fmt: The new DAI link format
1481 *
1482 * This function updates the DAI link format for all DAIs connected to the DAI
1483 * link for the specified runtime.
1484 *
1485 * Note: For setups with a static format set the dai_fmt field in the
1486 * corresponding snd_dai_link struct instead of using this function.
1487 *
1488 * Returns 0 on success, otherwise a negative error code.
1489 */
1490int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1491 unsigned int dai_fmt)
1492{
1493 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1494 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1495 unsigned int i;
1496 int ret;
1497
1498 for (i = 0; i < rtd->num_codecs; i++) {
1499 struct snd_soc_dai *codec_dai = codec_dais[i];
1500
1501 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1502 if (ret != 0 && ret != -ENOTSUPP) {
1503 dev_warn(codec_dai->dev,
1504 "ASoC: Failed to set DAI format: %d\n", ret);
1505 return ret;
1506 }
1507 }
1508
1509 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1510 if (cpu_dai->codec) {
1511 unsigned int inv_dai_fmt;
1512
1513 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1514 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1515 case SND_SOC_DAIFMT_CBM_CFM:
1516 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1517 break;
1518 case SND_SOC_DAIFMT_CBM_CFS:
1519 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1520 break;
1521 case SND_SOC_DAIFMT_CBS_CFM:
1522 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1523 break;
1524 case SND_SOC_DAIFMT_CBS_CFS:
1525 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1526 break;
1527 }
1528
1529 dai_fmt = inv_dai_fmt;
1530 }
1531
1532 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1533 if (ret != 0 && ret != -ENOTSUPP) {
1534 dev_warn(cpu_dai->dev,
1535 "ASoC: Failed to set DAI format: %d\n", ret);
1536 return ret;
1537 }
1538
1539 return 0;
1540}
Lars-Peter Clausenddaca252015-01-08 12:23:05 +01001541EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001542
Mark Brownb19e6e72012-03-14 21:18:39 +00001543static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001544{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001545 struct snd_soc_codec *codec;
Lars-Peter Clausence64c8b2015-01-06 15:17:20 +01001546 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001548 mutex_lock(&client_mutex);
Liam Girdwood01b9d992012-03-07 10:38:25 +00001549 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001550
Mark Brownb19e6e72012-03-14 21:18:39 +00001551 /* bind DAIs */
1552 for (i = 0; i < card->num_links; i++) {
1553 ret = soc_bind_dai_link(card, i);
1554 if (ret != 0)
1555 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001556 }
1557
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001558 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001559 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001560 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001561 if (ret != 0)
1562 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 }
1564
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001565 /* initialize the register cache for each available codec */
1566 list_for_each_entry(codec, &codec_list, list) {
1567 if (codec->cache_init)
1568 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001569 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001570 if (ret < 0)
1571 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001572 }
1573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001574 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001575 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001576 card->owner, 0, &card->snd_card);
1577 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001578 dev_err(card->dev,
1579 "ASoC: can't create sound card for card %s: %d\n",
1580 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001581 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001584 soc_init_card_debugfs(card);
1585
Mark Browne37a4972011-03-02 18:21:57 +00001586 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1587 card->dapm.dev = card->dev;
1588 card->dapm.card = card;
1589 list_add(&card->dapm.list, &card->dapm_list);
1590
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001591#ifdef CONFIG_DEBUG_FS
1592 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1593#endif
1594
Mark Brown88ee1c62011-02-02 10:43:26 +00001595#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001596 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001597 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001598#endif
Andy Green6ed25972008-06-13 16:24:05 +01001599
Mark Brown9a841eb2011-04-12 17:51:37 -07001600 if (card->dapm_widgets)
1601 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1602 card->num_dapm_widgets);
1603
Nicolin Chenf23e8602015-02-14 17:22:49 -08001604 if (card->of_dapm_widgets)
1605 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
1606 card->num_of_dapm_widgets);
1607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608 /* initialise the sound card only once */
1609 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001610 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611 if (ret < 0)
1612 goto card_probe_error;
1613 }
1614
Stephen Warren62ae68f2012-06-08 12:34:23 -06001615 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001616 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1617 order++) {
1618 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001619 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001620 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001621 dev_err(card->dev,
1622 "ASoC: failed to instantiate card %d\n",
1623 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001624 goto probe_dai_err;
1625 }
1626 }
1627 }
1628
1629 /* probe all DAI links on this card */
1630 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1631 order++) {
1632 for (i = 0; i < card->num_links; i++) {
1633 ret = soc_probe_link_dais(card, i, order);
1634 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001635 dev_err(card->dev,
1636 "ASoC: failed to instantiate card %d\n",
1637 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001638 goto probe_dai_err;
1639 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 }
1641 }
1642
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001643 for (i = 0; i < card->num_aux_devs; i++) {
1644 ret = soc_probe_aux_dev(card, i);
1645 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001646 dev_err(card->dev,
1647 "ASoC: failed to add auxiliary devices %d\n",
1648 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001649 goto probe_aux_dev_err;
1650 }
1651 }
1652
Mark Brown888df392012-02-16 19:37:51 -08001653 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001654 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001655
Mark Brownb7af1da2011-04-07 19:18:44 +09001656 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001657 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001658
Mark Brownb8ad29d2011-03-02 18:35:51 +00001659 if (card->dapm_routes)
1660 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1661 card->num_dapm_routes);
1662
Nicolin Chenf23e8602015-02-14 17:22:49 -08001663 if (card->of_dapm_routes)
1664 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
1665 card->num_of_dapm_routes);
Mark Brown75d9ac42011-09-27 16:41:01 +01001666
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001669 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1670 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001671 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1672 "%s", card->driver_name ? card->driver_name : card->name);
1673 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1674 switch (card->snd_card->driver[i]) {
1675 case '_':
1676 case '-':
1677 case '\0':
1678 break;
1679 default:
1680 if (!isalnum(card->snd_card->driver[i]))
1681 card->snd_card->driver[i] = '_';
1682 break;
1683 }
1684 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685
Mark Brown28e9ad92011-03-02 18:36:34 +00001686 if (card->late_probe) {
1687 ret = card->late_probe(card);
1688 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001689 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001690 card->name, ret);
1691 goto probe_aux_dev_err;
1692 }
1693 }
1694
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001695 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 ret = snd_card_register(card->snd_card);
1698 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001699 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1700 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001701 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702 }
1703
Mark Brown435c5e22008-12-04 15:32:53 +00001704 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001705 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001707 mutex_unlock(&client_mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001708
1709 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001711probe_aux_dev_err:
1712 for (i = 0; i < card->num_aux_devs; i++)
1713 soc_remove_aux_dev(card, i);
1714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001716 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001719 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001720 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721
Lars-Peter Clausen0757d832015-04-09 10:52:36 +02001722 soc_cleanup_card_debugfs(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001723 snd_card_free(card->snd_card);
1724
Mark Brownb19e6e72012-03-14 21:18:39 +00001725base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 mutex_unlock(&card->mutex);
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01001727 mutex_unlock(&client_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001728
Mark Brownb19e6e72012-03-14 21:18:39 +00001729 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001730}
1731
1732/* probes a new socdev */
1733static int soc_probe(struct platform_device *pdev)
1734{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001735 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001736
Vinod Koul70a7ca32011-01-14 19:22:48 +05301737 /*
1738 * no card, so machine driver should be registering card
1739 * we should not be here in that case so ret error
1740 */
1741 if (!card)
1742 return -EINVAL;
1743
Mark Brownfe4085e2012-03-02 13:07:41 +00001744 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001745 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001746 card->name);
1747
Mark Brown435c5e22008-12-04 15:32:53 +00001748 /* Bodge while we unpick instantiation */
1749 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001750
Mark Brown28d528c2012-08-09 18:45:23 +01001751 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752}
1753
Vinod Koulb0e26482011-01-13 22:48:02 +05301754static int soc_cleanup_card_resources(struct snd_soc_card *card)
1755{
Vinod Koulb0e26482011-01-13 22:48:02 +05301756 int i;
1757
1758 /* make sure any delayed work runs */
1759 for (i = 0; i < card->num_rtd; i++) {
1760 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001761 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301762 }
1763
1764 /* remove auxiliary devices */
1765 for (i = 0; i < card->num_aux_devs; i++)
1766 soc_remove_aux_dev(card, i);
1767
1768 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001769 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301770
1771 soc_cleanup_card_debugfs(card);
1772
1773 /* remove the card */
1774 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001775 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301776
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001777 snd_soc_dapm_free(&card->dapm);
1778
Vinod Koulb0e26482011-01-13 22:48:02 +05301779 snd_card_free(card->snd_card);
1780 return 0;
1781
1782}
1783
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001784/* removes a socdev */
1785static int soc_remove(struct platform_device *pdev)
1786{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001787 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788
Mark Brownc5af3a22008-11-28 13:29:45 +00001789 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001790 return 0;
1791}
1792
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001793int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001794{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001795 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796 int i;
Mark Brown51737472009-06-22 13:16:51 +01001797
1798 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001799 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001800
1801 /* Flush out pmdown_time work - we actually do want to run it
1802 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001803 for (i = 0; i < card->num_rtd; i++) {
1804 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001805 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001806 }
Mark Brown51737472009-06-22 13:16:51 +01001807
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001808 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001809
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001810 /* deactivate pins to sleep state */
1811 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001812 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1813 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1814 int j;
1815
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001816 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001817 for (j = 0; j < rtd->num_codecs; j++) {
1818 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1819 pinctrl_pm_select_sleep_state(codec_dai->dev);
1820 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001821 }
1822
Mark Brown416356f2009-06-30 19:05:15 +01001823 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001824}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001825EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001826
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001827const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301828 .suspend = snd_soc_suspend,
1829 .resume = snd_soc_resume,
1830 .freeze = snd_soc_suspend,
1831 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001832 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301833 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001834};
Stephen Warrendeb26072011-04-05 19:35:30 -06001835EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001836
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837/* ASoC platform driver */
1838static struct platform_driver soc_driver = {
1839 .driver = {
1840 .name = "soc-audio",
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001841 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001842 },
1843 .probe = soc_probe,
1844 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001845};
1846
Mark Brown096e49d2009-07-05 15:12:22 +01001847/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001848 * snd_soc_cnew - create new control
1849 * @_template: control template
1850 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001851 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001852 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001853 *
1854 * Create a new mixer control from a template control.
1855 *
1856 * Returns 0 for success, else error.
1857 */
1858struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08001859 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00001860 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861{
1862 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001863 struct snd_kcontrol *kcontrol;
1864 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001865
1866 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001867 template.index = 0;
1868
Mark Brownefb7ac32011-03-08 17:23:24 +00001869 if (!long_name)
1870 long_name = template.name;
1871
1872 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02001873 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00001874 if (!name)
1875 return NULL;
1876
Mark Brownefb7ac32011-03-08 17:23:24 +00001877 template.name = name;
1878 } else {
1879 template.name = long_name;
1880 }
1881
1882 kcontrol = snd_ctl_new1(&template, data);
1883
1884 kfree(name);
1885
1886 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001887}
1888EXPORT_SYMBOL_GPL(snd_soc_cnew);
1889
Liam Girdwood022658b2012-02-03 17:43:09 +00001890static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1891 const struct snd_kcontrol_new *controls, int num_controls,
1892 const char *prefix, void *data)
1893{
1894 int err, i;
1895
1896 for (i = 0; i < num_controls; i++) {
1897 const struct snd_kcontrol_new *control = &controls[i];
1898 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1899 control->name, prefix));
1900 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001901 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1902 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00001903 return err;
1904 }
1905 }
1906
1907 return 0;
1908}
1909
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01001910struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1911 const char *name)
1912{
1913 struct snd_card *card = soc_card->snd_card;
1914 struct snd_kcontrol *kctl;
1915
1916 if (unlikely(!name))
1917 return NULL;
1918
1919 list_for_each_entry(kctl, &card->controls, list)
1920 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1921 return kctl;
1922 return NULL;
1923}
1924EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1925
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001926/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001927 * snd_soc_add_component_controls - Add an array of controls to a component.
1928 *
1929 * @component: Component to add controls to
1930 * @controls: Array of controls to add
1931 * @num_controls: Number of elements in the array
1932 *
1933 * Return: 0 for success, else error.
1934 */
1935int snd_soc_add_component_controls(struct snd_soc_component *component,
1936 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1937{
1938 struct snd_card *card = component->card->snd_card;
1939
1940 return snd_soc_add_controls(card, component->dev, controls,
1941 num_controls, component->name_prefix, component);
1942}
1943EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1944
1945/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001946 * snd_soc_add_codec_controls - add an array of controls to a codec.
1947 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00001948 * duplicating this code.
1949 *
1950 * @codec: codec to add controls to
1951 * @controls: array of controls to add
1952 * @num_controls: number of elements in the array
1953 *
1954 * Return 0 for success, else error.
1955 */
Liam Girdwood022658b2012-02-03 17:43:09 +00001956int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001957 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00001958{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001959 return snd_soc_add_component_controls(&codec->component, controls,
1960 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001961}
Liam Girdwood022658b2012-02-03 17:43:09 +00001962EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001963
1964/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001965 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00001966 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001967 *
1968 * @platform: platform to add controls to
1969 * @controls: array of controls to add
1970 * @num_controls: number of elements in the array
1971 *
1972 * Return 0 for success, else error.
1973 */
1974int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001975 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001976{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001977 return snd_soc_add_component_controls(&platform->component, controls,
1978 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001979}
1980EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1981
1982/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001983 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1984 * Convenience function to add a list of controls.
1985 *
1986 * @soc_card: SoC card to add controls to
1987 * @controls: array of controls to add
1988 * @num_controls: number of elements in the array
1989 *
1990 * Return 0 for success, else error.
1991 */
1992int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1993 const struct snd_kcontrol_new *controls, int num_controls)
1994{
1995 struct snd_card *card = soc_card->snd_card;
1996
1997 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1998 NULL, soc_card);
1999}
2000EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2001
2002/**
2003 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2004 * Convienience function to add a list of controls.
2005 *
2006 * @dai: DAI to add controls to
2007 * @controls: array of controls to add
2008 * @num_controls: number of elements in the array
2009 *
2010 * Return 0 for success, else error.
2011 */
2012int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2013 const struct snd_kcontrol_new *controls, int num_controls)
2014{
Lars-Peter Clausen313665b2014-11-04 11:30:58 +01002015 struct snd_card *card = dai->component->card->snd_card;
Liam Girdwood022658b2012-02-03 17:43:09 +00002016
2017 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2018 NULL, dai);
2019}
2020EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2021
2022/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002023 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2024 * @dai: DAI
2025 * @clk_id: DAI specific clock ID
2026 * @freq: new clock frequency in Hz
2027 * @dir: new clock direction - input/output.
2028 *
2029 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2030 */
2031int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2032 unsigned int freq, int dir)
2033{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034 if (dai->driver && dai->driver->ops->set_sysclk)
2035 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002036 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002037 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002038 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002039 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002040 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002041}
2042EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2043
2044/**
Mark Brownec4ee522011-03-07 20:58:11 +00002045 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2046 * @codec: CODEC
2047 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002048 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002049 * @freq: new clock frequency in Hz
2050 * @dir: new clock direction - input/output.
2051 *
2052 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2053 */
2054int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002055 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002056{
2057 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002058 return codec->driver->set_sysclk(codec, clk_id, source,
2059 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002060 else
Mark Brown1104a9c2014-01-15 19:04:19 +00002061 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00002062}
2063EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2064
2065/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002066 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2067 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002068 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002069 * @div: new clock divisor.
2070 *
2071 * Configures the clock dividers. This is used to derive the best DAI bit and
2072 * frame clocks from the system or master clock. It's best to set the DAI bit
2073 * and frame clocks as low as possible to save system power.
2074 */
2075int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2076 int div_id, int div)
2077{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002078 if (dai->driver && dai->driver->ops->set_clkdiv)
2079 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002080 else
2081 return -EINVAL;
2082}
2083EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2084
2085/**
2086 * snd_soc_dai_set_pll - configure DAI PLL.
2087 * @dai: DAI
2088 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002089 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002090 * @freq_in: PLL input clock frequency in Hz
2091 * @freq_out: requested PLL output clock frequency in Hz
2092 *
2093 * Configures and enables PLL to generate output clock based on input clock.
2094 */
Mark Brown85488032009-09-05 18:52:16 +01002095int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2096 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002097{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002098 if (dai->driver && dai->driver->ops->set_pll)
2099 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002100 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002101 else if (dai->codec && dai->codec->driver->set_pll)
2102 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2103 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002104 else
2105 return -EINVAL;
2106}
2107EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2108
Mark Brownec4ee522011-03-07 20:58:11 +00002109/*
2110 * snd_soc_codec_set_pll - configure codec PLL.
2111 * @codec: CODEC
2112 * @pll_id: DAI specific PLL ID
2113 * @source: DAI specific source for the PLL
2114 * @freq_in: PLL input clock frequency in Hz
2115 * @freq_out: requested PLL output clock frequency in Hz
2116 *
2117 * Configures and enables PLL to generate output clock based on input clock.
2118 */
2119int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2120 unsigned int freq_in, unsigned int freq_out)
2121{
2122 if (codec->driver->set_pll)
2123 return codec->driver->set_pll(codec, pll_id, source,
2124 freq_in, freq_out);
2125 else
2126 return -EINVAL;
2127}
2128EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2129
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002130/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01002131 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2132 * @dai: DAI
2133 * @ratio Ratio of BCLK to Sample rate.
2134 *
2135 * Configures the DAI for a preset BCLK to sample rate ratio.
2136 */
2137int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2138{
2139 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2140 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2141 else
2142 return -EINVAL;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2145
2146/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002147 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2148 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002149 * @fmt: SND_SOC_DAIFMT_ format value.
2150 *
2151 * Configures the DAI hardware format and clocking.
2152 */
2153int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2154{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002155 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002156 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002157 if (dai->driver->ops->set_fmt == NULL)
2158 return -ENOTSUPP;
2159 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002160}
2161EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2162
2163/**
Xiubo Lie5c21512014-03-21 14:17:12 +08002164 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08002165 * @slots: Number of slots in use.
2166 * @tx_mask: bitmask representing active TX slots.
2167 * @rx_mask: bitmask representing active RX slots.
2168 *
2169 * Generates the TDM tx and rx slot default masks for DAI.
2170 */
Xiubo Lie5c21512014-03-21 14:17:12 +08002171static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002172 unsigned int *tx_mask,
2173 unsigned int *rx_mask)
2174{
2175 if (*tx_mask || *rx_mask)
2176 return 0;
2177
2178 if (!slots)
2179 return -EINVAL;
2180
2181 *tx_mask = (1 << slots) - 1;
2182 *rx_mask = (1 << slots) - 1;
2183
2184 return 0;
2185}
2186
2187/**
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002188 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2189 * @dai: The DAI to configure
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002190 * @tx_mask: bitmask representing active TX slots.
2191 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002192 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002193 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002194 *
Lars-Peter Clausene46c9362015-01-12 10:27:20 +01002195 * This function configures the specified DAI for TDM operation. @slot contains
2196 * the total number of slots of the TDM stream and @slot_with the width of each
2197 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2198 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2199 * DAI should write to or read from. If a bit is set the corresponding slot is
2200 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2201 * the first slot, bit 1 to the second slot and so on. The first active slot
2202 * maps to the first channel of the DAI, the second active slot to the second
2203 * channel and so on.
2204 *
2205 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2206 * @rx_mask and @slot_width will be ignored.
2207 *
2208 * Returns 0 on success, a negative error code otherwise.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002209 */
2210int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002211 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002212{
Xiubo Lie5c21512014-03-21 14:17:12 +08002213 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2214 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08002215 &tx_mask, &rx_mask);
2216 else
Xiubo Lie5c21512014-03-21 14:17:12 +08002217 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08002218
Benoit Cousson88bd8702014-07-08 23:19:34 +02002219 dai->tx_mask = tx_mask;
2220 dai->rx_mask = rx_mask;
2221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002222 if (dai->driver && dai->driver->ops->set_tdm_slot)
2223 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002224 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002225 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08002226 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002227}
2228EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2229
2230/**
Barry Song472df3c2009-09-12 01:16:29 +08002231 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2232 * @dai: DAI
2233 * @tx_num: how many TX channels
2234 * @tx_slot: pointer to an array which imply the TX slot number channel
2235 * 0~num-1 uses
2236 * @rx_num: how many RX channels
2237 * @rx_slot: pointer to an array which imply the RX slot number channel
2238 * 0~num-1 uses
2239 *
2240 * configure the relationship between channel number and TDM slot number.
2241 */
2242int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2243 unsigned int tx_num, unsigned int *tx_slot,
2244 unsigned int rx_num, unsigned int *rx_slot)
2245{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002246 if (dai->driver && dai->driver->ops->set_channel_map)
2247 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002248 rx_num, rx_slot);
2249 else
2250 return -EINVAL;
2251}
2252EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2253
2254/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002255 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2256 * @dai: DAI
2257 * @tristate: tristate enable
2258 *
2259 * Tristates the DAI so that others can use it.
2260 */
2261int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2262{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002263 if (dai->driver && dai->driver->ops->set_tristate)
2264 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002265 else
2266 return -EINVAL;
2267}
2268EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2269
2270/**
2271 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2272 * @dai: DAI
2273 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00002274 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002275 *
2276 * Mutes the DAI DAC.
2277 */
Mark Brownda183962013-02-06 15:44:07 +00002278int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2279 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002280{
Mark Brownda183962013-02-06 15:44:07 +00002281 if (!dai->driver)
2282 return -ENOTSUPP;
2283
2284 if (dai->driver->ops->mute_stream)
2285 return dai->driver->ops->mute_stream(dai, mute, direction);
2286 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2287 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002288 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002289 else
Mark Brown04570c62012-04-13 19:16:03 +01002290 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002291}
2292EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2293
Benoit Cousson88bd8702014-07-08 23:19:34 +02002294static int snd_soc_init_multicodec(struct snd_soc_card *card,
2295 struct snd_soc_dai_link *dai_link)
2296{
2297 /* Legacy codec/codec_dai link is a single entry in multicodec */
2298 if (dai_link->codec_name || dai_link->codec_of_node ||
2299 dai_link->codec_dai_name) {
2300 dai_link->num_codecs = 1;
2301
2302 dai_link->codecs = devm_kzalloc(card->dev,
2303 sizeof(struct snd_soc_dai_link_component),
2304 GFP_KERNEL);
2305 if (!dai_link->codecs)
2306 return -ENOMEM;
2307
2308 dai_link->codecs[0].name = dai_link->codec_name;
2309 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2310 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2311 }
2312
2313 if (!dai_link->codecs) {
2314 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2315 return -EINVAL;
2316 }
2317
2318 return 0;
2319}
2320
Mark Brownc5af3a22008-11-28 13:29:45 +00002321/**
2322 * snd_soc_register_card - Register a card with the ASoC core
2323 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002324 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002325 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002326 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302327int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002328{
Benoit Cousson88bd8702014-07-08 23:19:34 +02002329 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002330
Mark Brownc5af3a22008-11-28 13:29:45 +00002331 if (!card->name || !card->dev)
2332 return -EINVAL;
2333
Stephen Warren5a504962011-12-21 10:40:59 -07002334 for (i = 0; i < card->num_links; i++) {
2335 struct snd_soc_dai_link *link = &card->dai_link[i];
2336
Benoit Cousson88bd8702014-07-08 23:19:34 +02002337 ret = snd_soc_init_multicodec(card, link);
2338 if (ret) {
2339 dev_err(card->dev, "ASoC: failed to init multicodec\n");
2340 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07002341 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02002342
2343 for (j = 0; j < link->num_codecs; j++) {
2344 /*
2345 * Codec must be specified by 1 of name or OF node,
2346 * not both or neither.
2347 */
2348 if (!!link->codecs[j].name ==
2349 !!link->codecs[j].of_node) {
2350 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2351 link->name);
2352 return -EINVAL;
2353 }
2354 /* Codec DAI name must be specified */
2355 if (!link->codecs[j].dai_name) {
2356 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2357 link->name);
2358 return -EINVAL;
2359 }
Stephen Warrenbc926572012-05-25 18:22:11 -06002360 }
Stephen Warren5a504962011-12-21 10:40:59 -07002361
2362 /*
2363 * Platform may be specified by either name or OF node, but
2364 * can be left unspecified, and a dummy platform will be used.
2365 */
2366 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002367 dev_err(card->dev,
2368 "ASoC: Both platform name/of_node are set for %s\n",
2369 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002370 return -EINVAL;
2371 }
2372
2373 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06002374 * CPU device may be specified by either name or OF node, but
2375 * can be left unspecified, and will be matched based on DAI
2376 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07002377 */
Stephen Warrenbc926572012-05-25 18:22:11 -06002378 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002379 dev_err(card->dev,
2380 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2381 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06002382 return -EINVAL;
2383 }
2384 /*
2385 * At least one of CPU DAI name or CPU device name/node must be
2386 * specified
2387 */
2388 if (!link->cpu_dai_name &&
2389 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002390 dev_err(card->dev,
2391 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2392 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002393 return -EINVAL;
2394 }
2395 }
2396
Mark Browned77cc12011-05-03 18:25:34 +01002397 dev_set_drvdata(card->dev, card);
2398
Stephen Warren111c6412011-01-28 14:26:35 -07002399 snd_soc_initialize_card_lists(card);
2400
Mark Brown181a6892012-03-14 20:18:49 +00002401 card->rtd = devm_kzalloc(card->dev,
2402 sizeof(struct snd_soc_pcm_runtime) *
2403 (card->num_links + card->num_aux_devs),
2404 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002405 if (card->rtd == NULL)
2406 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01002407 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002408 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002409
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002410 for (i = 0; i < card->num_links; i++) {
2411 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002412 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02002413 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2414 sizeof(struct snd_soc_dai *) *
2415 (card->rtd[i].dai_link->num_codecs),
2416 GFP_KERNEL);
2417 if (card->rtd[i].codec_dais == NULL)
2418 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02002419 }
2420
2421 for (i = 0; i < card->num_aux_devs; i++)
2422 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002423
Mark Browndb432b42011-10-03 21:06:40 +01002424 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002425 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002426 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002427 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002428
Mark Brownb19e6e72012-03-14 21:18:39 +00002429 ret = snd_soc_instantiate_card(card);
2430 if (ret != 0)
Kuninori Morimoto4e2576b2015-03-24 09:01:00 +00002431 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00002432
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002433 /* deactivate pins to sleep state */
2434 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002435 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2436 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2437 int j;
2438
2439 for (j = 0; j < rtd->num_codecs; j++) {
2440 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2441 if (!codec_dai->active)
2442 pinctrl_pm_select_sleep_state(codec_dai->dev);
2443 }
2444
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002445 if (!cpu_dai->active)
2446 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2447 }
2448
Mark Brownb19e6e72012-03-14 21:18:39 +00002449 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00002450}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302451EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002452
2453/**
2454 * snd_soc_unregister_card - Unregister a card with the ASoC core
2455 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002456 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002457 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002458 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302459int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002460{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002461 if (card->instantiated) {
2462 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02002463 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302464 soc_cleanup_card_resources(card);
Kuninori Morimoto8f6f9b22015-02-05 05:34:10 +00002465 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02002466 }
Mark Brownc5af3a22008-11-28 13:29:45 +00002467
2468 return 0;
2469}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302470EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002472/*
2473 * Simplify DAI link configuration by removing ".-1" from device names
2474 * and sanitizing names.
2475 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002476static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002477{
2478 char *found, name[NAME_SIZE];
2479 int id1, id2;
2480
2481 if (dev_name(dev) == NULL)
2482 return NULL;
2483
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002484 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002485
2486 /* are we a "%s.%d" name (platform and SPI components) */
2487 found = strstr(name, dev->driver->name);
2488 if (found) {
2489 /* get ID */
2490 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2491
2492 /* discard ID from name if ID == -1 */
2493 if (*id == -1)
2494 found[strlen(dev->driver->name)] = '\0';
2495 }
2496
2497 } else {
2498 /* I2C component devices are named "bus-addr" */
2499 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2500 char tmp[NAME_SIZE];
2501
2502 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002503 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002504
2505 /* sanitize component name for DAI link creation */
2506 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002507 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002508 } else
2509 *id = 0;
2510 }
2511
2512 return kstrdup(name, GFP_KERNEL);
2513}
2514
2515/*
2516 * Simplify DAI link naming for single devices with multiple DAIs by removing
2517 * any ".-1" and using the DAI name (instead of device name).
2518 */
2519static inline char *fmt_multiple_name(struct device *dev,
2520 struct snd_soc_dai_driver *dai_drv)
2521{
2522 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02002523 dev_err(dev,
2524 "ASoC: error - multiple DAI %s registered with no name\n",
2525 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002526 return NULL;
2527 }
2528
2529 return kstrdup(dai_drv->name, GFP_KERNEL);
2530}
2531
Mark Brown91151712008-11-30 23:31:24 +00002532/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002533 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002534 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002535 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00002536 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002537static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00002538{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002539 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00002540
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002541 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002542 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2543 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01002544 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002545 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002546 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002547 }
Mark Brown91151712008-11-30 23:31:24 +00002548}
Mark Brown91151712008-11-30 23:31:24 +00002549
2550/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002551 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00002552 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002553 * @component: The component the DAIs are registered for
2554 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00002555 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002556 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2557 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00002558 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002559static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002560 struct snd_soc_dai_driver *dai_drv, size_t count,
2561 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00002562{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002563 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002564 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002565 unsigned int i;
2566 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002567
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002568 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00002569
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002570 component->dai_drv = dai_drv;
2571 component->num_dai = count;
2572
Mark Brown91151712008-11-30 23:31:24 +00002573 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002574
2575 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08002576 if (dai == NULL) {
2577 ret = -ENOMEM;
2578 goto err;
2579 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002580
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002581 /*
2582 * Back in the old days when we still had component-less DAIs,
2583 * instead of having a static name, component-less DAIs would
2584 * inherit the name of the parent device so it is possible to
2585 * register multiple instances of the DAI. We still need to keep
2586 * the same naming style even though those DAIs are not
2587 * component-less anymore.
2588 */
2589 if (count == 1 && legacy_dai_naming) {
2590 dai->name = fmt_single_name(dev, &dai->id);
2591 } else {
2592 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2593 if (dai_drv[i].id)
2594 dai->id = dai_drv[i].id;
2595 else
2596 dai->id = i;
2597 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002598 if (dai->name == NULL) {
2599 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002600 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00002601 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002602 }
2603
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01002604 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002605 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002606 dai->driver = &dai_drv[i];
2607 if (!dai->driver->ops)
2608 dai->driver->ops = &null_dai_ops;
2609
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01002610 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01002611
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002612 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00002613 }
2614
2615 return 0;
2616
2617err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01002618 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00002619
2620 return ret;
2621}
Mark Brown91151712008-11-30 23:31:24 +00002622
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002623static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2624 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002625{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002626 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002627
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002628 component->driver->seq_notifier(component, type, subseq);
2629}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002630
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002631static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2632 int event)
2633{
2634 struct snd_soc_component *component = dapm->component;
2635
2636 return component->driver->stream_event(component, event);
2637}
2638
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002639static int snd_soc_component_initialize(struct snd_soc_component *component,
2640 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002641{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002642 struct snd_soc_dapm_context *dapm;
2643
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002644 component->name = fmt_single_name(dev, &component->id);
2645 if (!component->name) {
2646 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002647 return -ENOMEM;
2648 }
2649
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002650 component->dev = dev;
2651 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002652 component->probe = component->driver->probe;
2653 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002654
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002655 if (!component->dapm_ptr)
2656 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002657
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02002658 dapm = component->dapm_ptr;
2659 dapm->dev = dev;
2660 dapm->component = component;
2661 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02002662 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02002663 if (driver->seq_notifier)
2664 dapm->seq_notifier = snd_soc_component_seq_notifier;
2665 if (driver->stream_event)
2666 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002667
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02002668 component->controls = driver->controls;
2669 component->num_controls = driver->num_controls;
2670 component->dapm_widgets = driver->dapm_widgets;
2671 component->num_dapm_widgets = driver->num_dapm_widgets;
2672 component->dapm_routes = driver->dapm_routes;
2673 component->num_dapm_routes = driver->num_dapm_routes;
2674
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002675 INIT_LIST_HEAD(&component->dai_list);
2676 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002677
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002678 return 0;
2679}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002680
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002681static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002682{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002683 int val_bytes = regmap_get_val_bytes(component->regmap);
2684
2685 /* Errors are legitimate for non-integer byte multiples */
2686 if (val_bytes > 0)
2687 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002688}
2689
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002690#ifdef CONFIG_REGMAP
2691
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002692/**
2693 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
2694 * @component: The component for which to initialize the regmap instance
2695 * @regmap: The regmap instance that should be used by the component
2696 *
2697 * This function allows deferred assignment of the regmap instance that is
2698 * associated with the component. Only use this if the regmap instance is not
2699 * yet ready when the component is registered. The function must also be called
2700 * before the first IO attempt of the component.
2701 */
2702void snd_soc_component_init_regmap(struct snd_soc_component *component,
2703 struct regmap *regmap)
2704{
2705 component->regmap = regmap;
2706 snd_soc_component_setup_regmap(component);
2707}
2708EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
2709
2710/**
2711 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
2712 * @component: The component for which to de-initialize the regmap instance
2713 *
2714 * Calls regmap_exit() on the regmap instance associated to the component and
2715 * removes the regmap instance from the component.
2716 *
2717 * This function should only be used if snd_soc_component_init_regmap() was used
2718 * to initialize the regmap instance.
2719 */
2720void snd_soc_component_exit_regmap(struct snd_soc_component *component)
2721{
2722 regmap_exit(component->regmap);
2723 component->regmap = NULL;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
2726
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01002727#endif
2728
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002729static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2730{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01002731 if (!component->write && !component->read) {
2732 if (!component->regmap)
2733 component->regmap = dev_get_regmap(component->dev, NULL);
2734 if (component->regmap)
2735 snd_soc_component_setup_regmap(component);
2736 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02002737
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002738 list_add(&component->list, &component_list);
2739}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002740
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002741static void snd_soc_component_add(struct snd_soc_component *component)
2742{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002743 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002744 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002745 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002746}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002747
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002748static void snd_soc_component_cleanup(struct snd_soc_component *component)
2749{
2750 snd_soc_unregister_dais(component);
2751 kfree(component->name);
2752}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002753
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002754static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2755{
2756 list_del(&component->list);
2757}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002758
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002759int snd_soc_register_component(struct device *dev,
2760 const struct snd_soc_component_driver *cmpnt_drv,
2761 struct snd_soc_dai_driver *dai_drv,
2762 int num_dai)
2763{
2764 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002765 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002766
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002767 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002768 if (!cmpnt) {
2769 dev_err(dev, "ASoC: Failed to allocate memory\n");
2770 return -ENOMEM;
2771 }
2772
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002773 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
2774 if (ret)
2775 goto err_free;
2776
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002777 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002778 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01002779
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002780 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
2781 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002782 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002783 goto err_cleanup;
2784 }
2785
2786 snd_soc_component_add(cmpnt);
2787
2788 return 0;
2789
2790err_cleanup:
2791 snd_soc_component_cleanup(cmpnt);
2792err_free:
2793 kfree(cmpnt);
2794 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002795}
2796EXPORT_SYMBOL_GPL(snd_soc_register_component);
2797
2798/**
2799 * snd_soc_unregister_component - Unregister a component from the ASoC core
2800 *
2801 */
2802void snd_soc_unregister_component(struct device *dev)
2803{
2804 struct snd_soc_component *cmpnt;
2805
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002806 mutex_lock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002807 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01002808 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002809 goto found;
2810 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002811 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002812 return;
2813
2814found:
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002815 snd_soc_component_del_unlocked(cmpnt);
2816 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002817 snd_soc_component_cleanup(cmpnt);
2818 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002819}
2820EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
2821
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002822static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002823{
2824 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2825
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002826 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002827}
2828
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002829static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002830{
2831 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
2832
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002833 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02002834}
2835
Kuninori Morimotod191bd82013-09-04 19:39:03 -07002836/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002837 * snd_soc_add_platform - Add a platform to the ASoC core
2838 * @dev: The parent device for the platform
2839 * @platform: The platform to add
2840 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00002841 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002842int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01002843 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002844{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002845 int ret;
2846
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002847 ret = snd_soc_component_initialize(&platform->component,
2848 &platform_drv->component_driver, dev);
2849 if (ret)
2850 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002851
2852 platform->dev = dev;
2853 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002854
2855 if (platform_drv->probe)
2856 platform->component.probe = snd_soc_platform_drv_probe;
2857 if (platform_drv->remove)
2858 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00002859
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02002860#ifdef CONFIG_DEBUG_FS
2861 platform->component.debugfs_prefix = "platform";
2862#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00002863
2864 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002865 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002866 list_add(&platform->list, &platform_list);
2867 mutex_unlock(&client_mutex);
2868
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002869 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
2870 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002871
2872 return 0;
2873}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002874EXPORT_SYMBOL_GPL(snd_soc_add_platform);
2875
2876/**
2877 * snd_soc_register_platform - Register a platform with the ASoC core
2878 *
2879 * @platform: platform to register
2880 */
2881int snd_soc_register_platform(struct device *dev,
2882 const struct snd_soc_platform_driver *platform_drv)
2883{
2884 struct snd_soc_platform *platform;
2885 int ret;
2886
2887 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
2888
2889 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2890 if (platform == NULL)
2891 return -ENOMEM;
2892
2893 ret = snd_soc_add_platform(dev, platform, platform_drv);
2894 if (ret)
2895 kfree(platform);
2896
2897 return ret;
2898}
Mark Brown12a48a8c2008-12-03 19:40:30 +00002899EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2900
2901/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002902 * snd_soc_remove_platform - Remove a platform from the ASoC core
2903 * @platform: the platform to remove
2904 */
2905void snd_soc_remove_platform(struct snd_soc_platform *platform)
2906{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01002907
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002908 mutex_lock(&client_mutex);
2909 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02002910 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002911 mutex_unlock(&client_mutex);
2912
2913 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02002914 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02002915
2916 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002917}
2918EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
2919
2920struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
2921{
2922 struct snd_soc_platform *platform;
2923
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002924 mutex_lock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002925 list_for_each_entry(platform, &platform_list, list) {
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002926 if (dev == platform->dev) {
2927 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002928 return platform;
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002929 }
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002930 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01002931 mutex_unlock(&client_mutex);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002932
2933 return NULL;
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
2936
2937/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00002938 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2939 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002940 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002941 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002942void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00002943{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002944 struct snd_soc_platform *platform;
2945
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002946 platform = snd_soc_lookup_platform(dev);
2947 if (!platform)
2948 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02002950 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002951 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00002952}
2953EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2954
Mark Brown151ab222009-05-09 16:22:58 +01002955static u64 codec_format_map[] = {
2956 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2957 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2958 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2959 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2960 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2961 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2962 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2963 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2964 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2965 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2966 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2967 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2968 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2969 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2970 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2971 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2972};
2973
2974/* Fix up the DAI formats for endianness: codecs don't actually see
2975 * the endianness of the data but we're using the CPU format
2976 * definitions which do need to include endianness so we ensure that
2977 * codec DAIs always have both big and little endian variants set.
2978 */
2979static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2980{
2981 int i;
2982
2983 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2984 if (stream->formats & codec_format_map[i])
2985 stream->formats |= codec_format_map[i];
2986}
2987
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02002988static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
2989{
2990 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2991
2992 return codec->driver->probe(codec);
2993}
2994
2995static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
2996{
2997 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2998
2999 codec->driver->remove(codec);
3000}
3001
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003002static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3003 unsigned int reg, unsigned int val)
3004{
3005 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3006
3007 return codec->driver->write(codec, reg, val);
3008}
3009
3010static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3011 unsigned int reg, unsigned int *val)
3012{
3013 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3014
3015 *val = codec->driver->read(codec, reg);
3016
3017 return 0;
3018}
3019
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003020static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3021 enum snd_soc_bias_level level)
3022{
3023 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3024
3025 return codec->driver->set_bias_level(codec, level);
3026}
3027
Mark Brown0d0cf002008-12-10 14:32:45 +00003028/**
3029 * snd_soc_register_codec - Register a codec with the ASoC core
3030 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003031 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003032 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003033int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003034 const struct snd_soc_codec_driver *codec_drv,
3035 struct snd_soc_dai_driver *dai_drv,
3036 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003037{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003038 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003039 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003041
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003042 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003043
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003044 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3045 if (codec == NULL)
3046 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003047
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003048 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003049 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003050
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003051 ret = snd_soc_component_initialize(&codec->component,
3052 &codec_drv->component_driver, dev);
3053 if (ret)
3054 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003055
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003056 if (codec_drv->controls) {
3057 codec->component.controls = codec_drv->controls;
3058 codec->component.num_controls = codec_drv->num_controls;
3059 }
3060 if (codec_drv->dapm_widgets) {
3061 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3062 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3063 }
3064 if (codec_drv->dapm_routes) {
3065 codec->component.dapm_routes = codec_drv->dapm_routes;
3066 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3067 }
3068
3069 if (codec_drv->probe)
3070 codec->component.probe = snd_soc_codec_drv_probe;
3071 if (codec_drv->remove)
3072 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003073 if (codec_drv->write)
3074 codec->component.write = snd_soc_codec_drv_write;
3075 if (codec_drv->read)
3076 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003077 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003078 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02003079 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003080 if (codec_drv->seq_notifier)
3081 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003082 if (codec_drv->set_bias_level)
3083 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084 codec->dev = dev;
3085 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003086 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003088#ifdef CONFIG_DEBUG_FS
3089 codec->component.init_debugfs = soc_init_codec_debugfs;
3090 codec->component.debugfs_prefix = "codec";
3091#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08003092
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003093 if (codec_drv->get_regmap)
3094 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08003095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003096 for (i = 0; i < num_dai; i++) {
3097 fixup_codec_formats(&dai_drv[i].playback);
3098 fixup_codec_formats(&dai_drv[i].capture);
3099 }
3100
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003101 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3102 if (ret < 0) {
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09003103 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003104 goto err_cleanup;
3105 }
3106
3107 list_for_each_entry(dai, &codec->component.dai_list, list)
3108 dai->codec = codec;
3109
Mark Brown054880f2012-04-09 17:29:19 +01003110 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003111 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01003112 list_add(&codec->list, &codec_list);
3113 mutex_unlock(&client_mutex);
3114
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003115 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3116 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003117 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003118
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003119err_cleanup:
3120 snd_soc_component_cleanup(&codec->component);
3121err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122 kfree(codec);
3123 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003124}
3125EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3126
3127/**
3128 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3129 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003130 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003131 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003132void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003133{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003134 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003135
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003136 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003137 list_for_each_entry(codec, &codec_list, list) {
3138 if (dev == codec->dev)
3139 goto found;
3140 }
Lars-Peter Clausen34e81ab2015-03-07 19:34:03 +01003141 mutex_unlock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003142 return;
3143
3144found:
Mark Brown0d0cf002008-12-10 14:32:45 +00003145 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003146 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00003147 mutex_unlock(&client_mutex);
3148
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003149 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3150 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003151
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003152 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003153 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003154 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003155}
3156EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3157
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003158/* Retrieve a card's name from device tree */
3159int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3160 const char *propname)
3161{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303162 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003163 int ret;
3164
Tushar Behera7e07e7c2014-07-04 14:23:00 +05303165 if (!card->dev) {
3166 pr_err("card->dev is not set before calling %s\n", __func__);
3167 return -EINVAL;
3168 }
3169
3170 np = card->dev->of_node;
3171
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003172 ret = of_property_read_string_index(np, propname, 0, &card->name);
3173 /*
3174 * EINVAL means the property does not exist. This is fine providing
3175 * card->name was previously set, which is checked later in
3176 * snd_soc_register_card.
3177 */
3178 if (ret < 0 && ret != -EINVAL) {
3179 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003180 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003181 propname, ret);
3182 return ret;
3183 }
3184
3185 return 0;
3186}
3187EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3188
Xiubo Li9a6d4862014-02-08 15:59:52 +08003189static const struct snd_soc_dapm_widget simple_widgets[] = {
3190 SND_SOC_DAPM_MIC("Microphone", NULL),
3191 SND_SOC_DAPM_LINE("Line", NULL),
3192 SND_SOC_DAPM_HP("Headphone", NULL),
3193 SND_SOC_DAPM_SPK("Speaker", NULL),
3194};
3195
3196int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3197 const char *propname)
3198{
3199 struct device_node *np = card->dev->of_node;
3200 struct snd_soc_dapm_widget *widgets;
3201 const char *template, *wname;
3202 int i, j, num_widgets, ret;
3203
3204 num_widgets = of_property_count_strings(np, propname);
3205 if (num_widgets < 0) {
3206 dev_err(card->dev,
3207 "ASoC: Property '%s' does not exist\n", propname);
3208 return -EINVAL;
3209 }
3210 if (num_widgets & 1) {
3211 dev_err(card->dev,
3212 "ASoC: Property '%s' length is not even\n", propname);
3213 return -EINVAL;
3214 }
3215
3216 num_widgets /= 2;
3217 if (!num_widgets) {
3218 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3219 propname);
3220 return -EINVAL;
3221 }
3222
3223 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3224 GFP_KERNEL);
3225 if (!widgets) {
3226 dev_err(card->dev,
3227 "ASoC: Could not allocate memory for widgets\n");
3228 return -ENOMEM;
3229 }
3230
3231 for (i = 0; i < num_widgets; i++) {
3232 ret = of_property_read_string_index(np, propname,
3233 2 * i, &template);
3234 if (ret) {
3235 dev_err(card->dev,
3236 "ASoC: Property '%s' index %d read error:%d\n",
3237 propname, 2 * i, ret);
3238 return -EINVAL;
3239 }
3240
3241 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3242 if (!strncmp(template, simple_widgets[j].name,
3243 strlen(simple_widgets[j].name))) {
3244 widgets[i] = simple_widgets[j];
3245 break;
3246 }
3247 }
3248
3249 if (j >= ARRAY_SIZE(simple_widgets)) {
3250 dev_err(card->dev,
3251 "ASoC: DAPM widget '%s' is not supported\n",
3252 template);
3253 return -EINVAL;
3254 }
3255
3256 ret = of_property_read_string_index(np, propname,
3257 (2 * i) + 1,
3258 &wname);
3259 if (ret) {
3260 dev_err(card->dev,
3261 "ASoC: Property '%s' index %d read error:%d\n",
3262 propname, (2 * i) + 1, ret);
3263 return -EINVAL;
3264 }
3265
3266 widgets[i].name = wname;
3267 }
3268
Nicolin Chenf23e8602015-02-14 17:22:49 -08003269 card->of_dapm_widgets = widgets;
3270 card->num_of_dapm_widgets = num_widgets;
Xiubo Li9a6d4862014-02-08 15:59:52 +08003271
3272 return 0;
3273}
3274EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3275
Xiubo Li89c67852014-02-14 09:34:35 +08003276int snd_soc_of_parse_tdm_slot(struct device_node *np,
3277 unsigned int *slots,
3278 unsigned int *slot_width)
3279{
3280 u32 val;
3281 int ret;
3282
3283 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3284 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3285 if (ret)
3286 return ret;
3287
3288 if (slots)
3289 *slots = val;
3290 }
3291
3292 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3293 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3294 if (ret)
3295 return ret;
3296
3297 if (slot_width)
3298 *slot_width = val;
3299 }
3300
3301 return 0;
3302}
3303EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3304
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003305int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3306 const char *propname)
3307{
3308 struct device_node *np = card->dev->of_node;
Mark Browne3b1e6a2014-12-18 11:46:38 +00003309 int num_routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003310 struct snd_soc_dapm_route *routes;
3311 int i, ret;
3312
3313 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003314 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003315 dev_err(card->dev,
3316 "ASoC: Property '%s' does not exist or its length is not even\n",
3317 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003318 return -EINVAL;
3319 }
3320 num_routes /= 2;
3321 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003322 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003323 propname);
3324 return -EINVAL;
3325 }
3326
Mark Browne3b1e6a2014-12-18 11:46:38 +00003327 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003328 GFP_KERNEL);
3329 if (!routes) {
3330 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003331 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003332 return -EINVAL;
3333 }
3334
3335 for (i = 0; i < num_routes; i++) {
3336 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003337 2 * i, &routes[i].sink);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003338 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09003339 dev_err(card->dev,
3340 "ASoC: Property '%s' index %d could not be read: %d\n",
3341 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003342 return -EINVAL;
3343 }
3344 ret = of_property_read_string_index(np, propname,
Mark Browne3b1e6a2014-12-18 11:46:38 +00003345 (2 * i) + 1, &routes[i].source);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003346 if (ret) {
3347 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09003348 "ASoC: Property '%s' index %d could not be read: %d\n",
3349 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003350 return -EINVAL;
3351 }
3352 }
3353
Nicolin Chenf23e8602015-02-14 17:22:49 -08003354 card->num_of_dapm_routes = num_routes;
3355 card->of_dapm_routes = routes;
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003356
3357 return 0;
3358}
3359EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3360
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003361unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02003362 const char *prefix,
3363 struct device_node **bitclkmaster,
3364 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003365{
3366 int ret, i;
3367 char prop[128];
3368 unsigned int format = 0;
3369 int bit, frame;
3370 const char *str;
3371 struct {
3372 char *name;
3373 unsigned int val;
3374 } of_fmt_table[] = {
3375 { "i2s", SND_SOC_DAIFMT_I2S },
3376 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3377 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3378 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3379 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3380 { "ac97", SND_SOC_DAIFMT_AC97 },
3381 { "pdm", SND_SOC_DAIFMT_PDM},
3382 { "msb", SND_SOC_DAIFMT_MSB },
3383 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003384 };
3385
3386 if (!prefix)
3387 prefix = "";
3388
3389 /*
3390 * check "[prefix]format = xxx"
3391 * SND_SOC_DAIFMT_FORMAT_MASK area
3392 */
3393 snprintf(prop, sizeof(prop), "%sformat", prefix);
3394 ret = of_property_read_string(np, prop, &str);
3395 if (ret == 0) {
3396 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3397 if (strcmp(str, of_fmt_table[i].name) == 0) {
3398 format |= of_fmt_table[i].val;
3399 break;
3400 }
3401 }
3402 }
3403
3404 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003405 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003406 * SND_SOC_DAIFMT_CLOCK_MASK area
3407 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08003408 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3409 if (of_get_property(np, prop, NULL))
3410 format |= SND_SOC_DAIFMT_CONT;
3411 else
3412 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003413
3414 /*
3415 * check "[prefix]bitclock-inversion"
3416 * check "[prefix]frame-inversion"
3417 * SND_SOC_DAIFMT_INV_MASK area
3418 */
3419 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3420 bit = !!of_get_property(np, prop, NULL);
3421
3422 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3423 frame = !!of_get_property(np, prop, NULL);
3424
3425 switch ((bit << 4) + frame) {
3426 case 0x11:
3427 format |= SND_SOC_DAIFMT_IB_IF;
3428 break;
3429 case 0x10:
3430 format |= SND_SOC_DAIFMT_IB_NF;
3431 break;
3432 case 0x01:
3433 format |= SND_SOC_DAIFMT_NB_IF;
3434 break;
3435 default:
3436 /* SND_SOC_DAIFMT_NB_NF is default */
3437 break;
3438 }
3439
3440 /*
3441 * check "[prefix]bitclock-master"
3442 * check "[prefix]frame-master"
3443 * SND_SOC_DAIFMT_MASTER_MASK area
3444 */
3445 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3446 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003447 if (bit && bitclkmaster)
3448 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003449
3450 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3451 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02003452 if (frame && framemaster)
3453 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08003454
3455 switch ((bit << 4) + frame) {
3456 case 0x11:
3457 format |= SND_SOC_DAIFMT_CBM_CFM;
3458 break;
3459 case 0x10:
3460 format |= SND_SOC_DAIFMT_CBM_CFS;
3461 break;
3462 case 0x01:
3463 format |= SND_SOC_DAIFMT_CBS_CFM;
3464 break;
3465 default:
3466 format |= SND_SOC_DAIFMT_CBS_CFS;
3467 break;
3468 }
3469
3470 return format;
3471}
3472EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3473
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003474static int snd_soc_get_dai_name(struct of_phandle_args *args,
3475 const char **dai_name)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003476{
3477 struct snd_soc_component *pos;
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003478 int ret = -EPROBE_DEFER;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003479
3480 mutex_lock(&client_mutex);
3481 list_for_each_entry(pos, &component_list, list) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003482 if (pos->dev->of_node != args->np)
Kuninori Morimotocb470082013-09-10 17:39:56 -07003483 continue;
3484
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003485 if (pos->driver->of_xlate_dai_name) {
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003486 ret = pos->driver->of_xlate_dai_name(pos,
3487 args,
3488 dai_name);
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003489 } else {
3490 int id = -1;
3491
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003492 switch (args->args_count) {
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003493 case 0:
3494 id = 0; /* same as dai_drv[0] */
3495 break;
3496 case 1:
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003497 id = args->args[0];
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003498 break;
3499 default:
3500 /* not supported */
3501 break;
3502 }
3503
3504 if (id < 0 || id >= pos->num_dai) {
3505 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08003506 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003507 }
Xiubo Lie41975e2013-12-20 14:39:51 +08003508
3509 ret = 0;
3510
3511 *dai_name = pos->dai_drv[id].name;
3512 if (!*dai_name)
3513 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07003514 }
3515
Kuninori Morimotocb470082013-09-10 17:39:56 -07003516 break;
3517 }
3518 mutex_unlock(&client_mutex);
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003519 return ret;
3520}
3521
3522int snd_soc_of_get_dai_name(struct device_node *of_node,
3523 const char **dai_name)
3524{
3525 struct of_phandle_args args;
3526 int ret;
3527
3528 ret = of_parse_phandle_with_args(of_node, "sound-dai",
3529 "#sound-dai-cells", 0, &args);
3530 if (ret)
3531 return ret;
3532
3533 ret = snd_soc_get_dai_name(&args, dai_name);
Kuninori Morimotocb470082013-09-10 17:39:56 -07003534
3535 of_node_put(args.np);
3536
3537 return ret;
3538}
3539EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3540
Jean-Francois Moine93b0f3e2014-11-25 13:16:12 +01003541/*
3542 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3543 * @dev: Card device
3544 * @of_node: Device node
3545 * @dai_link: DAI link
3546 *
3547 * Builds an array of CODEC DAI components from the DAI link property
3548 * 'sound-dai'.
3549 * The array is set in the DAI link and the number of DAIs is set accordingly.
3550 * The device nodes in the array (of_node) must be dereferenced by the caller.
3551 *
3552 * Returns 0 for success
3553 */
3554int snd_soc_of_get_dai_link_codecs(struct device *dev,
3555 struct device_node *of_node,
3556 struct snd_soc_dai_link *dai_link)
3557{
3558 struct of_phandle_args args;
3559 struct snd_soc_dai_link_component *component;
3560 char *name;
3561 int index, num_codecs, ret;
3562
3563 /* Count the number of CODECs */
3564 name = "sound-dai";
3565 num_codecs = of_count_phandle_with_args(of_node, name,
3566 "#sound-dai-cells");
3567 if (num_codecs <= 0) {
3568 if (num_codecs == -ENOENT)
3569 dev_err(dev, "No 'sound-dai' property\n");
3570 else
3571 dev_err(dev, "Bad phandle in 'sound-dai'\n");
3572 return num_codecs;
3573 }
3574 component = devm_kzalloc(dev,
3575 sizeof *component * num_codecs,
3576 GFP_KERNEL);
3577 if (!component)
3578 return -ENOMEM;
3579 dai_link->codecs = component;
3580 dai_link->num_codecs = num_codecs;
3581
3582 /* Parse the list */
3583 for (index = 0, component = dai_link->codecs;
3584 index < dai_link->num_codecs;
3585 index++, component++) {
3586 ret = of_parse_phandle_with_args(of_node, name,
3587 "#sound-dai-cells",
3588 index, &args);
3589 if (ret)
3590 goto err;
3591 component->of_node = args.np;
3592 ret = snd_soc_get_dai_name(&args, &component->dai_name);
3593 if (ret < 0)
3594 goto err;
3595 }
3596 return 0;
3597err:
3598 for (index = 0, component = dai_link->codecs;
3599 index < dai_link->num_codecs;
3600 index++, component++) {
3601 if (!component->of_node)
3602 break;
3603 of_node_put(component->of_node);
3604 component->of_node = NULL;
3605 }
3606 dai_link->codecs = NULL;
3607 dai_link->num_codecs = 0;
3608 return ret;
3609}
3610EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3611
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003612static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003613{
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003614 snd_soc_debugfs_init();
Mark Brownfb257892011-04-28 10:57:54 +01003615 snd_soc_util_init();
3616
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003617 return platform_driver_register(&soc_driver);
3618}
Mark Brown4abe8e12010-10-12 17:41:03 +01003619module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003620
Mark Brown7d8c16a2008-11-30 22:11:24 +00003621static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003622{
Mark Brownfb257892011-04-28 10:57:54 +01003623 snd_soc_util_exit();
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003624 snd_soc_debugfs_exit();
Mark Brownfb257892011-04-28 10:57:54 +01003625
Mark Brown384c89e2008-12-03 17:34:03 +00003626#ifdef CONFIG_DEBUG_FS
Mark Brown384c89e2008-12-03 17:34:03 +00003627#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003628 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003629}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003630module_exit(snd_soc_exit);
3631
3632/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003633MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003634MODULE_DESCRIPTION("ALSA SoC Core");
3635MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003636MODULE_ALIAS("platform:soc-audio");