blob: 1edc519d62868be8bf34f951a8089c72d27021c0 [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
Mark Brown2624d5f2009-11-03 21:56:13 +0000194#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000195static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000196 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000197{
198 ssize_t ret;
199 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000200 char *buf;
201
202 if (*ppos < 0 || !count)
203 return -EINVAL;
204
205 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000206 if (!buf)
207 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000208
209 ret = soc_codec_reg_show(codec, buf, count, *ppos);
210 if (ret >= 0) {
211 if (copy_to_user(user_buf, buf, ret)) {
212 kfree(buf);
213 return -EFAULT;
214 }
215 *ppos += ret;
216 }
217
Mark Brown2624d5f2009-11-03 21:56:13 +0000218 kfree(buf);
219 return ret;
220}
221
222static ssize_t codec_reg_write_file(struct file *file,
223 const char __user *user_buf, size_t count, loff_t *ppos)
224{
225 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700226 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 char *start = buf;
228 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000229 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900230 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000231
232 buf_size = min(count, (sizeof(buf)-1));
233 if (copy_from_user(buf, user_buf, buf_size))
234 return -EFAULT;
235 buf[buf_size] = 0;
236
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 while (*start == ' ')
238 start++;
239 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 while (*start == ' ')
241 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900242 ret = kstrtoul(start, 16, &value);
243 if (ret)
244 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000245
246 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030247 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000248
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000249 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 return buf_size;
251}
252
253static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700254 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000255 .read = codec_reg_read_file,
256 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200257 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000258};
259
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200260static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100261{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200262 if (component->debugfs_prefix) {
263 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100264
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200265 name = kasprintf(GFP_KERNEL, "%s:%s",
266 component->debugfs_prefix, component->name);
267 if (name) {
268 component->debugfs_root = debugfs_create_dir(name,
269 component->card->debugfs_card_root);
270 kfree(name);
271 }
272 } else {
273 component->debugfs_root = debugfs_create_dir(component->name,
274 component->card->debugfs_card_root);
275 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100276
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200277 if (!component->debugfs_root) {
278 dev_warn(component->dev,
279 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000280 return;
281 }
282
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200283 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
284 component->debugfs_root);
285
286 if (component->init_debugfs)
287 component->init_debugfs(component);
288}
289
290static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
291{
292 debugfs_remove_recursive(component->debugfs_root);
293}
294
295static void soc_init_codec_debugfs(struct snd_soc_component *component)
296{
297 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
298
299 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000300 &codec->cache_sync);
Mark Brownaaee8ef2011-01-26 20:53:50 +0000301
Mark Brown2624d5f2009-11-03 21:56:13 +0000302 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200303 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000304 codec, &codec_reg_fops);
305 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200306 dev_warn(codec->dev,
307 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000308}
309
Mark Brownc3c5a192010-09-15 18:15:14 +0100310static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
312{
313 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100314 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100315 struct snd_soc_codec *codec;
316
317 if (!buf)
318 return -ENOMEM;
319
Mark Brown2b194f9d2010-10-13 10:52:16 +0100320 list_for_each_entry(codec, &codec_list, list) {
321 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200322 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100323 if (len >= 0)
324 ret += len;
325 if (ret > PAGE_SIZE) {
326 ret = PAGE_SIZE;
327 break;
328 }
329 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100330
331 if (ret >= 0)
332 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
333
334 kfree(buf);
335
336 return ret;
337}
338
339static const struct file_operations codec_list_fops = {
340 .read = codec_list_read_file,
341 .llseek = default_llseek,/* read accesses f_pos */
342};
343
Mark Brownf3208782010-09-15 18:19:07 +0100344static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100349 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100350 struct snd_soc_dai *dai;
351
352 if (!buf)
353 return -ENOMEM;
354
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100355 list_for_each_entry(component, &component_list, list) {
356 list_for_each_entry(dai, &component->dai_list, list) {
357 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
358 dai->name);
359 if (len >= 0)
360 ret += len;
361 if (ret > PAGE_SIZE) {
362 ret = PAGE_SIZE;
363 break;
364 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100365 }
366 }
Mark Brownf3208782010-09-15 18:19:07 +0100367
Mark Brown2b194f9d2010-10-13 10:52:16 +0100368 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100369
370 kfree(buf);
371
372 return ret;
373}
374
375static const struct file_operations dai_list_fops = {
376 .read = dai_list_read_file,
377 .llseek = default_llseek,/* read accesses f_pos */
378};
379
Mark Brown19c7ac22010-09-15 18:22:40 +0100380static ssize_t platform_list_read_file(struct file *file,
381 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;
Mark Brown19c7ac22010-09-15 18:22:40 +0100386 struct snd_soc_platform *platform;
387
388 if (!buf)
389 return -ENOMEM;
390
Mark Brown2b194f9d2010-10-13 10:52:16 +0100391 list_for_each_entry(platform, &platform_list, list) {
392 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200393 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100394 if (len >= 0)
395 ret += len;
396 if (ret > PAGE_SIZE) {
397 ret = PAGE_SIZE;
398 break;
399 }
400 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100401
Mark Brown2b194f9d2010-10-13 10:52:16 +0100402 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100403
404 kfree(buf);
405
406 return ret;
407}
408
409static const struct file_operations platform_list_fops = {
410 .read = platform_list_read_file,
411 .llseek = default_llseek,/* read accesses f_pos */
412};
413
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200414static void soc_init_card_debugfs(struct snd_soc_card *card)
415{
416 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000417 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200418 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200419 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100420 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200421 return;
422 }
423
424 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
425 card->debugfs_card_root,
426 &card->pop_time);
427 if (!card->debugfs_pop_time)
428 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000429 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200430}
431
432static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
433{
434 debugfs_remove_recursive(card->debugfs_card_root);
435}
436
Mark Brown2624d5f2009-11-03 21:56:13 +0000437#else
438
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200439#define soc_init_codec_debugfs NULL
440
441static inline void soc_init_component_debugfs(
442 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000443{
444}
445
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200446static inline void soc_cleanup_component_debugfs(
447 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000448{
449}
450
Axel Linb95fccb2010-11-09 17:06:44 +0800451static inline void soc_init_card_debugfs(struct snd_soc_card *card)
452{
453}
454
455static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
456{
457}
Mark Brown2624d5f2009-11-03 21:56:13 +0000458#endif
459
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100460struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
461 const char *dai_link, int stream)
462{
463 int i;
464
465 for (i = 0; i < card->num_links; i++) {
466 if (card->rtd[i].dai_link->no_pcm &&
467 !strcmp(card->rtd[i].dai_link->name, dai_link))
468 return card->rtd[i].pcm->streams[stream].substream;
469 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000470 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100471 return NULL;
472}
473EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
474
475struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
476 const char *dai_link)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
482 return &card->rtd[i];
483 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000484 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100485 return NULL;
486}
487EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
488
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100489static void codec2codec_close_delayed_work(struct work_struct *work)
490{
491 /* Currently nothing to do for c2c links
492 * Since c2c links are internal nodes in the DAPM graph and
493 * don't interface with the outside world or application layer
494 * we don't have to do any special handling on close.
495 */
496}
497
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000498#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000500int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000502 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200503 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200504 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200506 /* If the card is not initialized yet there is nothing to do */
507 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200508 return 0;
509
Andy Green6ed25972008-06-13 16:24:05 +0100510 /* Due to the resume being scheduled into a workqueue we could
511 * suspend before that's finished - wait for it to complete.
512 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 snd_power_lock(card->snd_card);
514 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
515 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100516
517 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100519
Mark Browna00f90f2010-12-02 16:24:24 +0000520 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100522
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100524 continue;
525
Benoit Cousson88bd8702014-07-08 23:19:34 +0200526 for (j = 0; j < card->rtd[i].num_codecs; j++) {
527 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
528 struct snd_soc_dai_driver *drv = dai->driver;
529
530 if (drv->ops->digital_mute && dai->playback_active)
531 drv->ops->digital_mute(dai, 1);
532 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533 }
534
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100535 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 for (i = 0; i < card->num_rtd; i++) {
537 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100538 continue;
539
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100541 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100542
Mark Brown87506542008-11-18 20:50:34 +0000543 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000544 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 for (i = 0; i < card->num_rtd; i++) {
547 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
548 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100551 continue;
552
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100553 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 cpu_dai->driver->suspend(cpu_dai);
555 if (platform->driver->suspend && !platform->suspended) {
556 platform->driver->suspend(cpu_dai);
557 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100558 }
559 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 /* close any waiting streams and save state */
562 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200563 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700564 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200565 for (j = 0; j < card->rtd[i].num_codecs; j++) {
566 codec_dais[j]->codec->dapm.suspend_bias_level =
567 codec_dais[j]->codec->dapm.bias_level;
568 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572
573 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100574 continue;
575
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800576 snd_soc_dapm_stream_event(&card->rtd[i],
577 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800578 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800580 snd_soc_dapm_stream_event(&card->rtd[i],
581 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800582 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 }
584
Mark Browne2d32ff2012-08-31 17:38:32 -0700585 /* Recheck all analogue paths too */
586 dapm_mark_io_dirty(&card->dapm);
587 snd_soc_dapm_sync(&card->dapm);
588
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200590 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 /* If there are paths active then the CODEC will be held with
592 * bias _ON and should not be suspended. */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200593 if (!codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200594 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000596 /*
597 * If the CODEC is capable of idle
598 * bias off then being in STANDBY
599 * means it's doing something,
600 * otherwise fall through.
601 */
602 if (codec->dapm.idle_bias_off) {
603 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200604 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000605 break;
606 }
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200609 if (codec->driver->suspend)
610 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900612 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200613 if (codec->component.regmap)
614 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800615 /* deactivate pins to sleep state */
616 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 break;
618 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200619 dev_dbg(codec->dev,
620 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 break;
622 }
623 }
624 }
625
626 for (i = 0; i < card->num_rtd; i++) {
627 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
628
629 if (card->rtd[i].dai_link->ignore_suspend)
630 continue;
631
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100632 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800634
635 /* deactivate pins to sleep state */
636 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 }
638
Mark Brown87506542008-11-18 20:50:34 +0000639 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000640 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641
642 return 0;
643}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000644EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645
Andy Green6ed25972008-06-13 16:24:05 +0100646/* deferred resume work, so resume can complete before we finished
647 * setting our codec back up, which can be very slow on I2C
648 */
649static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 struct snd_soc_card *card =
652 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200653 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200654 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
Andy Green6ed25972008-06-13 16:24:05 +0100656 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
657 * so userspace apps are blocked from touching us
658 */
659
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000660 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100661
Mark Brown99497882010-05-07 20:24:05 +0100662 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100664
Mark Brown87506542008-11-18 20:50:34 +0000665 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000666 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100668 /* resume control bus DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 for (i = 0; i < card->num_rtd; i++) {
670 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100671
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100673 continue;
674
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100675 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677 }
678
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200679 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 /* If the CODEC was idle over suspend then it will have been
681 * left with bias OFF or STANDBY and suspended so we must now
682 * resume. Otherwise the suspend was suppressed.
683 */
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200684 if (codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200685 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 case SND_SOC_BIAS_STANDBY:
687 case SND_SOC_BIAS_OFF:
Lars-Peter Clausena8093292014-09-04 19:44:07 +0200688 if (codec->driver->resume)
689 codec->driver->resume(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 codec->suspended = 0;
691 break;
692 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200693 dev_dbg(codec->dev,
694 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 break;
696 }
Mark Brown1547aba2010-05-07 21:11:40 +0100697 }
698 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100703 continue;
704
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800705 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000706 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800707 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800709 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000710 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800711 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 }
713
Mark Brown3ff3f642008-05-19 12:32:25 +0200714 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100718 continue;
719
Benoit Cousson88bd8702014-07-08 23:19:34 +0200720 for (j = 0; j < card->rtd[i].num_codecs; j++) {
721 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
722 struct snd_soc_dai_driver *drv = dai->driver;
723
724 if (drv->ops->digital_mute && dai->playback_active)
725 drv->ops->digital_mute(dai, 0);
726 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200727 }
728
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 for (i = 0; i < card->num_rtd; i++) {
730 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
731 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100734 continue;
735
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100736 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 cpu_dai->driver->resume(cpu_dai);
738 if (platform->driver->resume && platform->suspended) {
739 platform->driver->resume(cpu_dai);
740 platform->suspended = 0;
741 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 }
743
Mark Brown87506542008-11-18 20:50:34 +0000744 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000745 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000747 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100748
749 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700751
752 /* Recheck all analogue paths too */
753 dapm_mark_io_dirty(&card->dapm);
754 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100755}
756
757/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000758int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100759{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000760 struct snd_soc_card *card = dev_get_drvdata(dev);
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100761 bool bus_control = false;
762 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200763
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200764 /* If the card is not initialized yet there is nothing to do */
765 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800766 return 0;
767
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800768 /* activate pins from sleep state */
769 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200770 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
771 struct snd_soc_dai **codec_dais = rtd->codec_dais;
772 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
773 int j;
774
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800775 if (cpu_dai->active)
776 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200777
778 for (j = 0; j < rtd->num_codecs; j++) {
779 struct snd_soc_dai *codec_dai = codec_dais[j];
780 if (codec_dai->active)
781 pinctrl_pm_select_default_state(codec_dai->dev);
782 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800783 }
784
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100785 /*
786 * DAIs that also act as the control bus master might have other drivers
787 * hanging off them so need to resume immediately. Other drivers don't
788 * have that problem and may take a substantial amount of time to resume
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 * due to I/O costs and anti-pop so handle them out of line.
790 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000791 for (i = 0; i < card->num_rtd; i++) {
792 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100793 bus_control |= cpu_dai->driver->bus_control;
Stephen Warren82e14e82011-05-25 14:06:41 -0600794 }
Lars-Peter Clausenbc263212014-11-10 22:41:52 +0100795 if (bus_control) {
796 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600797 soc_resume_deferred(&card->deferred_resume_work);
798 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000799 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600800 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000801 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100802 }
Andy Green6ed25972008-06-13 16:24:05 +0100803
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200804 return 0;
805}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808#define snd_soc_suspend NULL
809#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810#endif
811
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100812static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800813};
814
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200815static struct snd_soc_component *soc_find_component(
816 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100817{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200818 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100819
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200820 list_for_each_entry(component, &component_list, list) {
821 if (of_node) {
822 if (component->dev->of_node == of_node)
823 return component;
824 } else if (strcmp(component->name, name) == 0) {
825 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100826 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100827 }
828
829 return NULL;
830}
831
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200832static struct snd_soc_dai *snd_soc_find_dai(
833 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100834{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200835 struct snd_soc_component *component;
836 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100837
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200838 /* Find CPU DAI from registered DAIs*/
839 list_for_each_entry(component, &component_list, list) {
840 if (dlc->of_node && component->dev->of_node != dlc->of_node)
841 continue;
842 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
843 continue;
844 list_for_each_entry(dai, &component->dai_list, list) {
845 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100846 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100847
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200848 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100849 }
850 }
851
852 return NULL;
853}
854
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
858 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200859 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200860 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200861 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000862 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100863 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200864 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200865
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000866 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000867
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200868 cpu_dai_component.name = dai_link->cpu_name;
869 cpu_dai_component.of_node = dai_link->cpu_of_node;
870 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
871 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000872 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000873 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000875 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000876 }
877
Benoit Cousson88bd8702014-07-08 23:19:34 +0200878 rtd->num_codecs = dai_link->num_codecs;
879
880 /* Find CODEC from registered CODECs */
881 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200882 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200883 if (!codec_dais[i]) {
884 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
885 codecs[i].dai_name);
886 return -EPROBE_DEFER;
887 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000888 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100889
Benoit Cousson88bd8702014-07-08 23:19:34 +0200890 /* Single codec links expect codec and codec_dai in runtime data */
891 rtd->codec_dai = codec_dais[0];
892 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100893
Mark Brown848dd8b2011-04-27 18:16:32 +0100894 /* if there's no platform we match on the empty platform */
895 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700896 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100897 platform_name = "snd-soc-dummy";
898
Mark Brownb19e6e72012-03-14 21:18:39 +0000899 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700901 if (dai_link->platform_of_node) {
902 if (platform->dev->of_node !=
903 dai_link->platform_of_node)
904 continue;
905 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200906 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700907 continue;
908 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700909
910 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000912 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000913 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000917
918 card->num_rtd++;
919
920 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921}
922
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200923static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600924{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200925 if (!component->probed)
926 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600927
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200928 /* This is a HACK and will be removed soon */
929 if (component->codec)
930 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600931
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200932 if (component->remove)
933 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600934
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200935 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600936
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200937 soc_cleanup_component_debugfs(component);
938 component->probed = 0;
939 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600940}
941
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200942static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200943{
944 int err;
945
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200946 if (dai && dai->probed &&
947 dai->driver->remove_order == order) {
948 if (dai->driver->remove) {
949 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100950 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200951 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100952 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200953 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100954 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200955 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100956 }
957}
958
Stephen Warren62ae68f2012-06-08 12:34:23 -0600959static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960{
961 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200962 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800966 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
967 device_remove_file(rtd->dev, &dev_attr_codec_reg);
968 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 rtd->dev_registered = 0;
970 }
971
972 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200973 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200974 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200976 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977}
978
Stephen Warren62ae68f2012-06-08 12:34:23 -0600979static void soc_remove_link_components(struct snd_soc_card *card, int num,
980 int order)
981{
982 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
983 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600984 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200985 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200986 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600987
988 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200989 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200990 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600991
992 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200993 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200994 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200995 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200996 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600997 }
998
999 /* remove any CPU-side CODEC */
1000 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001001 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001002 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001003 }
1004}
1005
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001006static void soc_remove_dai_links(struct snd_soc_card *card)
1007{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001008 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001009
Liam Girdwood0168bf02011-06-07 16:08:05 +01001010 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1011 order++) {
1012 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001013 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001014 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001015
1016 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1017 order++) {
1018 for (dai = 0; dai < card->num_rtd; dai++)
1019 soc_remove_link_components(card, dai, order);
1020 }
1021
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001022 card->num_rtd = 0;
1023}
1024
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001025static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001026 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001027{
1028 int i;
1029
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001030 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001031 return;
1032
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001033 for (i = 0; i < card->num_configs; i++) {
1034 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001035 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001036 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001037 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001038 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001039 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001040 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001041 }
1042}
1043
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001044static int soc_probe_component(struct snd_soc_card *card,
1045 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001046{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001047 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001048 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001049 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001050
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001051 if (component->probed)
1052 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001053
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001054 component->card = card;
1055 dapm->card = card;
1056 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001057
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001058 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001059 return -ENODEV;
1060
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001061 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001062
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001063 if (component->dapm_widgets) {
1064 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1065 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001066
1067 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001068 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001069 "Failed to create new controls %d\n", ret);
1070 goto err_probe;
1071 }
1072 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001073
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001074 list_for_each_entry(dai, &component->dai_list, list) {
1075 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001076 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001077 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001078 "Failed to create DAI widgets %d\n", ret);
1079 goto err_probe;
1080 }
1081 }
Mark Brown888df392012-02-16 19:37:51 -08001082
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001083 if (component->probe) {
1084 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001085 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001086 dev_err(component->dev,
1087 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001088 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001089 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001090
1091 WARN(dapm->idle_bias_off &&
1092 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001093 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001094 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001095 }
1096
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001097 if (component->controls)
1098 snd_soc_add_component_controls(component, component->controls,
1099 component->num_controls);
1100 if (component->dapm_routes)
1101 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1102 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001103
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001104 component->probed = 1;
1105 list_add(&dapm->list, &card->dapm_list);
1106
1107 /* This is a HACK and will be removed soon */
1108 if (component->codec)
1109 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001110
Jarkko Nikula70d29332011-01-27 16:24:22 +02001111 return 0;
1112
1113err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001114 soc_cleanup_component_debugfs(component);
1115 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001116
1117 return ret;
1118}
1119
Mark Brown36ae1a92012-01-06 17:12:45 -08001120static void rtd_release(struct device *dev)
1121{
1122 kfree(dev);
1123}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001125static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1126 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001127{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001128 int ret = 0;
1129
Jarkko Nikula589c3562010-12-06 16:27:07 +02001130 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001131 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1132 if (!rtd->dev)
1133 return -ENOMEM;
1134 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001135 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001136 rtd->dev->release = rtd_release;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001137 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001138 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001139 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001140 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1141 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1142 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1143 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001144 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001145 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001146 /* calling put_device() here to free the rtd->dev */
1147 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001148 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001149 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001150 return ret;
1151 }
1152 rtd->dev_registered = 1;
1153
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001154 if (rtd->codec) {
1155 /* add DAPM sysfs entries for this codec */
1156 ret = snd_soc_dapm_sys_add(rtd->dev);
1157 if (ret < 0)
1158 dev_err(rtd->dev,
1159 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1160 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001161
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001162 /* add codec sysfs entries */
1163 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1164 if (ret < 0)
1165 dev_err(rtd->dev,
1166 "ASoC: failed to add codec sysfs files: %d\n",
1167 ret);
1168 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169
1170 return 0;
1171}
1172
Stephen Warren62ae68f2012-06-08 12:34:23 -06001173static int soc_probe_link_components(struct snd_soc_card *card, int num,
1174 int order)
1175{
1176 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001177 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001178 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001179 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001180
1181 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001182 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001183 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001184 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001185 if (ret < 0)
1186 return ret;
1187 }
1188
Benoit Cousson88bd8702014-07-08 23:19:34 +02001189 /* probe the CODEC-side components */
1190 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001191 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001192 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001193 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001194 if (ret < 0)
1195 return ret;
1196 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001197 }
1198
1199 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001200 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001201 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001202 if (ret < 0)
1203 return ret;
1204 }
1205
1206 return 0;
1207}
1208
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001209static int soc_probe_codec_dai(struct snd_soc_card *card,
1210 struct snd_soc_dai *codec_dai,
1211 int order)
1212{
1213 int ret;
1214
1215 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1216 if (codec_dai->driver->probe) {
1217 ret = codec_dai->driver->probe(codec_dai);
1218 if (ret < 0) {
1219 dev_err(codec_dai->dev,
1220 "ASoC: failed to probe CODEC DAI %s: %d\n",
1221 codec_dai->name, ret);
1222 return ret;
1223 }
1224 }
1225
1226 /* mark codec_dai as probed and add to card dai list */
1227 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001228 }
1229
1230 return 0;
1231}
1232
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001233static int soc_link_dai_widgets(struct snd_soc_card *card,
1234 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001235 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001236{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1238 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001239 struct snd_soc_dapm_widget *play_w, *capture_w;
1240 int ret;
1241
Benoit Cousson88bd8702014-07-08 23:19:34 +02001242 if (rtd->num_codecs > 1)
1243 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1244
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001245 /* link the DAI widgets */
1246 play_w = codec_dai->playback_widget;
1247 capture_w = cpu_dai->capture_widget;
1248 if (play_w && capture_w) {
1249 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1250 capture_w, play_w);
1251 if (ret != 0) {
1252 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1253 play_w->name, capture_w->name, ret);
1254 return ret;
1255 }
1256 }
1257
1258 play_w = cpu_dai->playback_widget;
1259 capture_w = codec_dai->capture_widget;
1260 if (play_w && capture_w) {
1261 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1262 capture_w, play_w);
1263 if (ret != 0) {
1264 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1265 play_w->name, capture_w->name, ret);
1266 return ret;
1267 }
1268 }
1269
1270 return 0;
1271}
1272
Stephen Warren62ae68f2012-06-08 12:34:23 -06001273static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274{
1275 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1276 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001279 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001281 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001282 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283
1284 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001285 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001287 for (i = 0; i < rtd->num_codecs; i++)
1288 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001289
1290 /* set default power off timeout */
1291 rtd->pmdown_time = pmdown_time;
1292
1293 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001294 if (!cpu_dai->probed &&
1295 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001296 if (cpu_dai->driver->probe) {
1297 ret = cpu_dai->driver->probe(cpu_dai);
1298 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001299 dev_err(cpu_dai->dev,
1300 "ASoC: failed to probe CPU DAI %s: %d\n",
1301 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001302 return ret;
1303 }
1304 }
1305 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001306 }
1307
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001308 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001309 for (i = 0; i < rtd->num_codecs; i++) {
1310 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1311 if (ret)
1312 return ret;
1313 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001314
Liam Girdwood0168bf02011-06-07 16:08:05 +01001315 /* complete DAI probe during last probe */
1316 if (order != SND_SOC_COMP_ORDER_LAST)
1317 return 0;
1318
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001319 /* do machine specific initialization */
1320 if (dai_link->init) {
1321 ret = dai_link->init(rtd);
1322 if (ret < 0) {
1323 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1324 dai_link->name, ret);
1325 return ret;
1326 }
1327 }
1328
1329 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001330 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001331 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001333#ifdef CONFIG_DEBUG_FS
1334 /* add DPCM sysfs entries */
1335 if (dai_link->dynamic) {
1336 ret = soc_dpcm_debugfs_add(rtd);
1337 if (ret < 0) {
1338 dev_err(rtd->dev,
1339 "ASoC: failed to add dpcm sysfs entries: %d\n",
1340 ret);
1341 return ret;
1342 }
1343 }
1344#endif
1345
Mark Brown36ae1a92012-01-06 17:12:45 -08001346 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001348 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1349 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001350
Namarta Kohli1245b702012-08-16 17:10:41 +05301351 if (cpu_dai->driver->compress_dai) {
1352 /*create compress_device"*/
1353 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001354 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001355 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301356 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001357 return ret;
1358 }
1359 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301360
1361 if (!dai_link->params) {
1362 /* create the pcm */
1363 ret = soc_new_pcm(rtd, num);
1364 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001365 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301366 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001367 return ret;
1368 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301369 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001370 INIT_DELAYED_WORK(&rtd->delayed_work,
1371 codec2codec_close_delayed_work);
1372
Namarta Kohli1245b702012-08-16 17:10:41 +05301373 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001374 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001375 if (ret)
1376 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001377 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 }
1379
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001380 return 0;
1381}
1382
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001383static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001384{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001385 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001386 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001387 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001388
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001389 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1390 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001391 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001392 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001393
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001394 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001395 return -EPROBE_DEFER;
1396 }
1397
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001398 /*
1399 * Some places still reference rtd->codec, so we have to keep that
1400 * initialized if the component is a CODEC. Once all those references
1401 * have been removed, this code can be removed as well.
1402 */
1403 rtd->codec = rtd->component->codec;
1404
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001405 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001406}
1407
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001408static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1409{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001410 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001411 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001412 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001413
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001414 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001415 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001416 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001417
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001418 /* do machine specific initialization */
1419 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001420 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001421 if (ret < 0) {
1422 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1423 aux_dev->name, ret);
1424 return ret;
1425 }
1426 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001428 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001429}
1430
1431static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1432{
1433 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001434 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001435
1436 /* unregister the rtd device */
1437 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001438 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001439 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001440 rtd->dev_registered = 0;
1441 }
1442
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001443 if (component && component->probed)
1444 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001445}
1446
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001447static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001448{
1449 int ret;
1450
1451 if (codec->cache_init)
1452 return 0;
1453
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001454 ret = snd_soc_cache_init(codec);
1455 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001456 dev_err(codec->dev,
1457 "ASoC: Failed to set cache compression type: %d\n",
1458 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001459 return ret;
1460 }
1461 codec->cache_init = 1;
1462 return 0;
1463}
1464
Mark Brownb19e6e72012-03-14 21:18:39 +00001465static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001467 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001468 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001469 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470
Liam Girdwood01b9d992012-03-07 10:38:25 +00001471 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001472
Mark Brownb19e6e72012-03-14 21:18:39 +00001473 /* bind DAIs */
1474 for (i = 0; i < card->num_links; i++) {
1475 ret = soc_bind_dai_link(card, i);
1476 if (ret != 0)
1477 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478 }
1479
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001480 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001481 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001482 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001483 if (ret != 0)
1484 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485 }
1486
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001487 /* initialize the register cache for each available codec */
1488 list_for_each_entry(codec, &codec_list, list) {
1489 if (codec->cache_init)
1490 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001491 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001492 if (ret < 0)
1493 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001494 }
1495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001497 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001498 card->owner, 0, &card->snd_card);
1499 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001500 dev_err(card->dev,
1501 "ASoC: can't create sound card for card %s: %d\n",
1502 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001503 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001505
Mark Browne37a4972011-03-02 18:21:57 +00001506 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1507 card->dapm.dev = card->dev;
1508 card->dapm.card = card;
1509 list_add(&card->dapm.list, &card->dapm_list);
1510
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001511#ifdef CONFIG_DEBUG_FS
1512 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1513#endif
1514
Mark Brown88ee1c62011-02-02 10:43:26 +00001515#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001516 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001517 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001518#endif
Andy Green6ed25972008-06-13 16:24:05 +01001519
Mark Brown9a841eb2011-04-12 17:51:37 -07001520 if (card->dapm_widgets)
1521 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1522 card->num_dapm_widgets);
1523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524 /* initialise the sound card only once */
1525 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001526 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 if (ret < 0)
1528 goto card_probe_error;
1529 }
1530
Stephen Warren62ae68f2012-06-08 12:34:23 -06001531 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001532 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1533 order++) {
1534 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001535 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001536 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001537 dev_err(card->dev,
1538 "ASoC: failed to instantiate card %d\n",
1539 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001540 goto probe_dai_err;
1541 }
1542 }
1543 }
1544
1545 /* probe all DAI links on this card */
1546 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1547 order++) {
1548 for (i = 0; i < card->num_links; i++) {
1549 ret = soc_probe_link_dais(card, i, order);
1550 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001551 dev_err(card->dev,
1552 "ASoC: failed to instantiate card %d\n",
1553 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001554 goto probe_dai_err;
1555 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001556 }
1557 }
1558
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001559 for (i = 0; i < card->num_aux_devs; i++) {
1560 ret = soc_probe_aux_dev(card, i);
1561 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001562 dev_err(card->dev,
1563 "ASoC: failed to add auxiliary devices %d\n",
1564 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001565 goto probe_aux_dev_err;
1566 }
1567 }
1568
Mark Brown888df392012-02-16 19:37:51 -08001569 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001570 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001571
Mark Brownb7af1da2011-04-07 19:18:44 +09001572 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001573 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001574
Mark Brownb8ad29d2011-03-02 18:35:51 +00001575 if (card->dapm_routes)
1576 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1577 card->num_dapm_routes);
1578
Mark Brown75d9ac42011-09-27 16:41:01 +01001579 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001580 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001581 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001582 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001583
Mark Brownf04209a2012-04-09 16:29:21 +01001584 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001585 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1586 int j;
1587
1588 for (j = 0; j < rtd->num_codecs; j++) {
1589 struct snd_soc_dai *codec_dai = codec_dais[j];
1590
1591 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1592 if (ret != 0 && ret != -ENOTSUPP)
1593 dev_warn(codec_dai->dev,
1594 "ASoC: Failed to set DAI format: %d\n",
1595 ret);
1596 }
Mark Brownf04209a2012-04-09 16:29:21 +01001597 }
1598
1599 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001600 if (dai_fmt &&
1601 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001602 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1603 dai_fmt);
1604 if (ret != 0 && ret != -ENOTSUPP)
1605 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001606 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001607 ret);
1608 } else if (dai_fmt) {
1609 /* Flip the polarity for the "CPU" end */
1610 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1611 switch (dai_link->dai_fmt &
1612 SND_SOC_DAIFMT_MASTER_MASK) {
1613 case SND_SOC_DAIFMT_CBM_CFM:
1614 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1615 break;
1616 case SND_SOC_DAIFMT_CBM_CFS:
1617 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1618 break;
1619 case SND_SOC_DAIFMT_CBS_CFM:
1620 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1621 break;
1622 case SND_SOC_DAIFMT_CBS_CFS:
1623 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1624 break;
1625 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001626
1627 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001628 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001629 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001630 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001631 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001632 ret);
1633 }
1634 }
1635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001636 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001638 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1639 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001640 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1641 "%s", card->driver_name ? card->driver_name : card->name);
1642 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1643 switch (card->snd_card->driver[i]) {
1644 case '_':
1645 case '-':
1646 case '\0':
1647 break;
1648 default:
1649 if (!isalnum(card->snd_card->driver[i]))
1650 card->snd_card->driver[i] = '_';
1651 break;
1652 }
1653 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654
Mark Brown28e9ad92011-03-02 18:36:34 +00001655 if (card->late_probe) {
1656 ret = card->late_probe(card);
1657 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001658 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001659 card->name, ret);
1660 goto probe_aux_dev_err;
1661 }
1662 }
1663
Stephen Warren16332812011-11-23 12:42:04 -07001664 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001665 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001666
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001667 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001668
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 ret = snd_card_register(card->snd_card);
1670 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001671 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1672 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001673 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 }
1675
Mark Brown435c5e22008-12-04 15:32:53 +00001676 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001677 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001679
1680 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001681
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001682probe_aux_dev_err:
1683 for (i = 0; i < card->num_aux_devs; i++)
1684 soc_remove_aux_dev(card, i);
1685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001687 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001689card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001690 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001691 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001692
1693 snd_card_free(card->snd_card);
1694
Mark Brownb19e6e72012-03-14 21:18:39 +00001695base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001697
Mark Brownb19e6e72012-03-14 21:18:39 +00001698 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001699}
1700
1701/* probes a new socdev */
1702static int soc_probe(struct platform_device *pdev)
1703{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001704 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001705
Vinod Koul70a7ca32011-01-14 19:22:48 +05301706 /*
1707 * no card, so machine driver should be registering card
1708 * we should not be here in that case so ret error
1709 */
1710 if (!card)
1711 return -EINVAL;
1712
Mark Brownfe4085e2012-03-02 13:07:41 +00001713 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001714 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001715 card->name);
1716
Mark Brown435c5e22008-12-04 15:32:53 +00001717 /* Bodge while we unpick instantiation */
1718 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001719
Mark Brown28d528c2012-08-09 18:45:23 +01001720 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001721}
1722
Vinod Koulb0e26482011-01-13 22:48:02 +05301723static int soc_cleanup_card_resources(struct snd_soc_card *card)
1724{
Vinod Koulb0e26482011-01-13 22:48:02 +05301725 int i;
1726
1727 /* make sure any delayed work runs */
1728 for (i = 0; i < card->num_rtd; i++) {
1729 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001730 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301731 }
1732
1733 /* remove auxiliary devices */
1734 for (i = 0; i < card->num_aux_devs; i++)
1735 soc_remove_aux_dev(card, i);
1736
1737 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001738 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301739
1740 soc_cleanup_card_debugfs(card);
1741
1742 /* remove the card */
1743 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001744 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301745
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001746 snd_soc_dapm_free(&card->dapm);
1747
Vinod Koulb0e26482011-01-13 22:48:02 +05301748 snd_card_free(card->snd_card);
1749 return 0;
1750
1751}
1752
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753/* removes a socdev */
1754static int soc_remove(struct platform_device *pdev)
1755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001756 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001757
Mark Brownc5af3a22008-11-28 13:29:45 +00001758 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001759 return 0;
1760}
1761
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001762int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001763{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001764 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 int i;
Mark Brown51737472009-06-22 13:16:51 +01001766
1767 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001768 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001769
1770 /* Flush out pmdown_time work - we actually do want to run it
1771 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 for (i = 0; i < card->num_rtd; i++) {
1773 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001774 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 }
Mark Brown51737472009-06-22 13:16:51 +01001776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001777 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001778
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001779 /* deactivate pins to sleep state */
1780 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001781 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1782 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1783 int j;
1784
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001785 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001786 for (j = 0; j < rtd->num_codecs; j++) {
1787 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1788 pinctrl_pm_select_sleep_state(codec_dai->dev);
1789 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001790 }
1791
Mark Brown416356f2009-06-30 19:05:15 +01001792 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001793}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001794EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001795
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001796const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301797 .suspend = snd_soc_suspend,
1798 .resume = snd_soc_resume,
1799 .freeze = snd_soc_suspend,
1800 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001801 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301802 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001803};
Stephen Warrendeb26072011-04-05 19:35:30 -06001804EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001805
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001806/* ASoC platform driver */
1807static struct platform_driver soc_driver = {
1808 .driver = {
1809 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001810 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001811 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001812 },
1813 .probe = soc_probe,
1814 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001815};
1816
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001817/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001818 * snd_soc_cnew - create new control
1819 * @_template: control template
1820 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001821 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001822 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001823 *
1824 * Create a new mixer control from a template control.
1825 *
1826 * Returns 0 for success, else error.
1827 */
1828struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08001829 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00001830 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001831{
1832 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001833 struct snd_kcontrol *kcontrol;
1834 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001835
1836 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837 template.index = 0;
1838
Mark Brownefb7ac32011-03-08 17:23:24 +00001839 if (!long_name)
1840 long_name = template.name;
1841
1842 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02001843 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00001844 if (!name)
1845 return NULL;
1846
Mark Brownefb7ac32011-03-08 17:23:24 +00001847 template.name = name;
1848 } else {
1849 template.name = long_name;
1850 }
1851
1852 kcontrol = snd_ctl_new1(&template, data);
1853
1854 kfree(name);
1855
1856 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001857}
1858EXPORT_SYMBOL_GPL(snd_soc_cnew);
1859
Liam Girdwood022658b2012-02-03 17:43:09 +00001860static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1861 const struct snd_kcontrol_new *controls, int num_controls,
1862 const char *prefix, void *data)
1863{
1864 int err, i;
1865
1866 for (i = 0; i < num_controls; i++) {
1867 const struct snd_kcontrol_new *control = &controls[i];
1868 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1869 control->name, prefix));
1870 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001871 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1872 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00001873 return err;
1874 }
1875 }
1876
1877 return 0;
1878}
1879
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01001880struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1881 const char *name)
1882{
1883 struct snd_card *card = soc_card->snd_card;
1884 struct snd_kcontrol *kctl;
1885
1886 if (unlikely(!name))
1887 return NULL;
1888
1889 list_for_each_entry(kctl, &card->controls, list)
1890 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1891 return kctl;
1892 return NULL;
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1895
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001897 * snd_soc_add_component_controls - Add an array of controls to a component.
1898 *
1899 * @component: Component to add controls to
1900 * @controls: Array of controls to add
1901 * @num_controls: Number of elements in the array
1902 *
1903 * Return: 0 for success, else error.
1904 */
1905int snd_soc_add_component_controls(struct snd_soc_component *component,
1906 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1907{
1908 struct snd_card *card = component->card->snd_card;
1909
1910 return snd_soc_add_controls(card, component->dev, controls,
1911 num_controls, component->name_prefix, component);
1912}
1913EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1914
1915/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001916 * snd_soc_add_codec_controls - add an array of controls to a codec.
1917 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00001918 * duplicating this code.
1919 *
1920 * @codec: codec to add controls to
1921 * @controls: array of controls to add
1922 * @num_controls: number of elements in the array
1923 *
1924 * Return 0 for success, else error.
1925 */
Liam Girdwood022658b2012-02-03 17:43:09 +00001926int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001927 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00001928{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001929 return snd_soc_add_component_controls(&codec->component, controls,
1930 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001931}
Liam Girdwood022658b2012-02-03 17:43:09 +00001932EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001933
1934/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001935 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00001936 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001937 *
1938 * @platform: platform to add controls to
1939 * @controls: array of controls to add
1940 * @num_controls: number of elements in the array
1941 *
1942 * Return 0 for success, else error.
1943 */
1944int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001945 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001946{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001947 return snd_soc_add_component_controls(&platform->component, controls,
1948 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001949}
1950EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1951
1952/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001953 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1954 * Convenience function to add a list of controls.
1955 *
1956 * @soc_card: SoC card to add controls to
1957 * @controls: array of controls to add
1958 * @num_controls: number of elements in the array
1959 *
1960 * Return 0 for success, else error.
1961 */
1962int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1963 const struct snd_kcontrol_new *controls, int num_controls)
1964{
1965 struct snd_card *card = soc_card->snd_card;
1966
1967 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1968 NULL, soc_card);
1969}
1970EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1971
1972/**
1973 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1974 * Convienience function to add a list of controls.
1975 *
1976 * @dai: DAI to add controls to
1977 * @controls: array of controls to add
1978 * @num_controls: number of elements in the array
1979 *
1980 * Return 0 for success, else error.
1981 */
1982int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1983 const struct snd_kcontrol_new *controls, int num_controls)
1984{
1985 struct snd_card *card = dai->card->snd_card;
1986
1987 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1988 NULL, dai);
1989}
1990EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1991
1992/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001993 * snd_soc_info_enum_double - enumerated double mixer info callback
1994 * @kcontrol: mixer control
1995 * @uinfo: control element information
1996 *
1997 * Callback to provide information about a double enumerated
1998 * mixer control.
1999 *
2000 * Returns 0 for success.
2001 */
2002int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2004{
2005 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2006
2007 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2008 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002009 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002011 if (uinfo->value.enumerated.item >= e->items)
2012 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002013 strlcpy(uinfo->value.enumerated.name,
2014 e->texts[uinfo->value.enumerated.item],
2015 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2019
2020/**
2021 * snd_soc_get_enum_double - enumerated double mixer get callback
2022 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002023 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002024 *
2025 * Callback to get the value of a double enumerated mixer.
2026 *
2027 * Returns 0 for success.
2028 */
2029int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_value *ucontrol)
2031{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002032 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002033 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002034 unsigned int val, item;
2035 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002036 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002038 ret = snd_soc_component_read(component, e->reg, &reg_val);
2039 if (ret)
2040 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002041 val = (reg_val >> e->shift_l) & e->mask;
2042 item = snd_soc_enum_val_to_item(e, val);
2043 ucontrol->value.enumerated.item[0] = item;
2044 if (e->shift_l != e->shift_r) {
2045 val = (reg_val >> e->shift_l) & e->mask;
2046 item = snd_soc_enum_val_to_item(e, val);
2047 ucontrol->value.enumerated.item[1] = item;
2048 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049
2050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2053
2054/**
2055 * snd_soc_put_enum_double - enumerated double mixer put callback
2056 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002057 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002058 *
2059 * Callback to set the value of a double enumerated mixer.
2060 *
2061 * Returns 0 for success.
2062 */
2063int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2065{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002066 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002067 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002068 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002069 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002070 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002071
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002072 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002073 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002074 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002075 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002077 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002078 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002079 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002080 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002081 }
2082
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002083 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084}
2085EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2086
2087/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002088 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002089 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002090 * @reg: Register to read
2091 * @mask: Mask to use after shifting the register value
2092 * @shift: Right shift of register value
2093 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002094 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002095 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002096 * This functions reads a codec register. The register value is shifted right
2097 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2098 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002099 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002100 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002101 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002102static int snd_soc_read_signed(struct snd_soc_component *component,
2103 unsigned int reg, unsigned int mask, unsigned int shift,
2104 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002105{
Markus Pargmannf227b882014-01-16 16:02:10 +01002106 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002107 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002108
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002109 ret = snd_soc_component_read(component, reg, &val);
2110 if (ret < 0)
2111 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002112
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002113 val = (val >> shift) & mask;
2114
2115 if (!sign_bit) {
2116 *signed_val = val;
2117 return 0;
2118 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002119
2120 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002121 if (!(val & BIT(sign_bit))) {
2122 *signed_val = val;
2123 return 0;
2124 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002125
2126 ret = val;
2127
2128 /*
2129 * The register most probably does not contain a full-sized int.
2130 * Instead we have an arbitrary number of bits in a signed
2131 * representation which has to be translated into a full-sized int.
2132 * This is done by filling up all bits above the sign-bit.
2133 */
2134 ret |= ~((int)(BIT(sign_bit) - 1));
2135
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002136 *signed_val = ret;
2137
2138 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002139}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002140
2141/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002142 * snd_soc_info_volsw - single mixer info callback
2143 * @kcontrol: mixer control
2144 * @uinfo: control element information
2145 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002146 * Callback to provide information about a single mixer control, or a double
2147 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148 *
2149 * Returns 0 for success.
2150 */
2151int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_info *uinfo)
2153{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002154 struct soc_mixer_control *mc =
2155 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002156 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002158 if (!mc->platform_max)
2159 mc->platform_max = mc->max;
2160 platform_max = mc->platform_max;
2161
2162 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002163 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2164 else
2165 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2166
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002167 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002169 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2173
2174/**
2175 * snd_soc_get_volsw - single mixer get callback
2176 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002177 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002179 * Callback to get the value of a single mixer control, or a double mixer
2180 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002181 *
2182 * Returns 0 for success.
2183 */
2184int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002187 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002188 struct soc_mixer_control *mc =
2189 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002190 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002191 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002192 unsigned int shift = mc->shift;
2193 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002194 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002195 int min = mc->min;
2196 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002197 unsigned int mask = (1 << fls(max)) - 1;
2198 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002199 int val;
2200 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201
Markus Pargmannf227b882014-01-16 16:02:10 +01002202 if (sign_bit)
2203 mask = BIT(sign_bit + 1) - 1;
2204
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002205 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2206 if (ret)
2207 return ret;
2208
2209 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002210 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002212 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002213
2214 if (snd_soc_volsw_is_stereo(mc)) {
2215 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002216 ret = snd_soc_read_signed(component, reg, mask, rshift,
2217 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002218 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002219 ret = snd_soc_read_signed(component, reg2, mask, shift,
2220 sign_bit, &val);
2221 if (ret)
2222 return ret;
2223
2224 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002225 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002227 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002228 }
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2233
2234/**
2235 * snd_soc_put_volsw - single mixer put callback
2236 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002237 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002239 * Callback to set the value of a single mixer control, or a double mixer
2240 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241 *
2242 * Returns 0 for success.
2243 */
2244int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2246{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002247 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002248 struct soc_mixer_control *mc =
2249 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002250 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002251 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002252 unsigned int shift = mc->shift;
2253 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002254 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002255 int min = mc->min;
2256 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002257 unsigned int mask = (1 << fls(max)) - 1;
2258 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002259 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002260 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002261 unsigned int val2 = 0;
2262 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263
Markus Pargmannf227b882014-01-16 16:02:10 +01002264 if (sign_bit)
2265 mask = BIT(sign_bit + 1) - 1;
2266
2267 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002269 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002270 val_mask = mask << shift;
2271 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002272 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002273 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002275 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002276 if (reg == reg2) {
2277 val_mask |= mask << rshift;
2278 val |= val2 << rshift;
2279 } else {
2280 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002281 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002282 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002283 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002284 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002285 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 return err;
2287
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002288 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002289 err = snd_soc_component_update_bits(component, reg2, val_mask,
2290 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002291
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002292 return err;
2293}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002294EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295
Mark Browne13ac2e2008-05-28 17:58:05 +01002296/**
Brian Austin1d99f242012-03-30 10:43:55 -05002297 * snd_soc_get_volsw_sx - single mixer get callback
2298 * @kcontrol: mixer control
2299 * @ucontrol: control element information
2300 *
2301 * Callback to get the value of a single mixer control, or a double mixer
2302 * control that spans 2 registers.
2303 *
2304 * Returns 0 for success.
2305 */
2306int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *ucontrol)
2308{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002309 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002310 struct soc_mixer_control *mc =
2311 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002312 unsigned int reg = mc->reg;
2313 unsigned int reg2 = mc->rreg;
2314 unsigned int shift = mc->shift;
2315 unsigned int rshift = mc->rshift;
2316 int max = mc->max;
2317 int min = mc->min;
2318 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002319 unsigned int val;
2320 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002321
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002322 ret = snd_soc_component_read(component, reg, &val);
2323 if (ret < 0)
2324 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002325
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002326 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2327
2328 if (snd_soc_volsw_is_stereo(mc)) {
2329 ret = snd_soc_component_read(component, reg2, &val);
2330 if (ret < 0)
2331 return ret;
2332
2333 val = ((val >> rshift) - min) & mask;
2334 ucontrol->value.integer.value[1] = val;
2335 }
Brian Austin1d99f242012-03-30 10:43:55 -05002336
2337 return 0;
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2340
2341/**
2342 * snd_soc_put_volsw_sx - double mixer set callback
2343 * @kcontrol: mixer control
2344 * @uinfo: control element information
2345 *
2346 * Callback to set the value of a double mixer control that spans 2 registers.
2347 *
2348 * Returns 0 for success.
2349 */
2350int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2351 struct snd_ctl_elem_value *ucontrol)
2352{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002353 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002354 struct soc_mixer_control *mc =
2355 (struct soc_mixer_control *)kcontrol->private_value;
2356
2357 unsigned int reg = mc->reg;
2358 unsigned int reg2 = mc->rreg;
2359 unsigned int shift = mc->shift;
2360 unsigned int rshift = mc->rshift;
2361 int max = mc->max;
2362 int min = mc->min;
2363 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002364 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002365 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002366
2367 val_mask = mask << shift;
2368 val = (ucontrol->value.integer.value[0] + min) & mask;
2369 val = val << shift;
2370
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002371 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302372 if (err < 0)
2373 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002374
2375 if (snd_soc_volsw_is_stereo(mc)) {
2376 val_mask = mask << rshift;
2377 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2378 val2 = val2 << rshift;
2379
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002380 err = snd_soc_component_update_bits(component, reg2, val_mask,
2381 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002382 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002383 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002384}
2385EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2386
2387/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002388 * snd_soc_info_volsw_s8 - signed mixer info callback
2389 * @kcontrol: mixer control
2390 * @uinfo: control element information
2391 *
2392 * Callback to provide information about a signed mixer control.
2393 *
2394 * Returns 0 for success.
2395 */
2396int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_info *uinfo)
2398{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002399 struct soc_mixer_control *mc =
2400 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002401 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002402 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002403
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002404 if (!mc->platform_max)
2405 mc->platform_max = mc->max;
2406 platform_max = mc->platform_max;
2407
Mark Browne13ac2e2008-05-28 17:58:05 +01002408 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2409 uinfo->count = 2;
2410 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002411 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002412 return 0;
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2415
2416/**
2417 * snd_soc_get_volsw_s8 - signed mixer get callback
2418 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002419 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002420 *
2421 * Callback to get the value of a signed mixer control.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002430 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002431 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002432 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002433 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002434 int ret;
2435
2436 ret = snd_soc_component_read(component, reg, &val);
2437 if (ret)
2438 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002439
2440 ucontrol->value.integer.value[0] =
2441 ((signed char)(val & 0xff))-min;
2442 ucontrol->value.integer.value[1] =
2443 ((signed char)((val >> 8) & 0xff))-min;
2444 return 0;
2445}
2446EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2447
2448/**
2449 * snd_soc_put_volsw_sgn - signed mixer put callback
2450 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002451 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002452 *
2453 * Callback to set the value of a signed mixer control.
2454 *
2455 * Returns 0 for success.
2456 */
2457int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2459{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002460 struct soc_mixer_control *mc =
2461 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002462 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002463 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002464 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002465 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002466
2467 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2468 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2469
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002470 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002471}
2472EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2473
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002474/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002475 * snd_soc_info_volsw_range - single mixer info callback with range.
2476 * @kcontrol: mixer control
2477 * @uinfo: control element information
2478 *
2479 * Callback to provide information, within a range, about a single
2480 * mixer control.
2481 *
2482 * returns 0 for success.
2483 */
2484int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_info *uinfo)
2486{
2487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
2489 int platform_max;
2490 int min = mc->min;
2491
2492 if (!mc->platform_max)
2493 mc->platform_max = mc->max;
2494 platform_max = mc->platform_max;
2495
2496 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002497 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002498 uinfo->value.integer.min = 0;
2499 uinfo->value.integer.max = platform_max - min;
2500
2501 return 0;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2504
2505/**
2506 * snd_soc_put_volsw_range - single mixer put value callback with range.
2507 * @kcontrol: mixer control
2508 * @ucontrol: control element information
2509 *
2510 * Callback to set the value, within a range, for a single mixer control.
2511 *
2512 * Returns 0 for success.
2513 */
2514int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2516{
2517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002519 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002520 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002521 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002522 unsigned int shift = mc->shift;
2523 int min = mc->min;
2524 int max = mc->max;
2525 unsigned int mask = (1 << fls(max)) - 1;
2526 unsigned int invert = mc->invert;
2527 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002528 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002529
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002530 if (invert)
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002531 val = (max - ucontrol->value.integer.value[0]) & mask;
2532 else
2533 val = ((ucontrol->value.integer.value[0] + min) & mask);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002534 val_mask = mask << shift;
2535 val = val << shift;
2536
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002537 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002538 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002539 return ret;
2540
2541 if (snd_soc_volsw_is_stereo(mc)) {
Mark Brown9bde4f02012-12-19 16:05:00 +00002542 if (invert)
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002543 val = (max - ucontrol->value.integer.value[1]) & mask;
2544 else
2545 val = ((ucontrol->value.integer.value[1] + min) & mask);
Mark Brown9bde4f02012-12-19 16:05:00 +00002546 val_mask = mask << shift;
2547 val = val << shift;
2548
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002549 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2550 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002551 }
2552
2553 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002554}
2555EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2556
2557/**
2558 * snd_soc_get_volsw_range - single mixer get callback with range
2559 * @kcontrol: mixer control
2560 * @ucontrol: control element information
2561 *
2562 * Callback to get the value, within a range, of a single mixer control.
2563 *
2564 * Returns 0 for success.
2565 */
2566int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2568{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002569 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002570 struct soc_mixer_control *mc =
2571 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002572 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002573 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002574 unsigned int shift = mc->shift;
2575 int min = mc->min;
2576 int max = mc->max;
2577 unsigned int mask = (1 << fls(max)) - 1;
2578 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002579 unsigned int val;
2580 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002581
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002582 ret = snd_soc_component_read(component, reg, &val);
2583 if (ret)
2584 return ret;
2585
2586 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002587 if (invert)
2588 ucontrol->value.integer.value[0] =
2589 max - ucontrol->value.integer.value[0];
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002590 else
2591 ucontrol->value.integer.value[0] =
2592 ucontrol->value.integer.value[0] - min;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002593
Mark Brown9bde4f02012-12-19 16:05:00 +00002594 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002595 ret = snd_soc_component_read(component, rreg, &val);
2596 if (ret)
2597 return ret;
2598
2599 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002600 if (invert)
2601 ucontrol->value.integer.value[1] =
2602 max - ucontrol->value.integer.value[1];
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002603 else
2604 ucontrol->value.integer.value[1] =
2605 ucontrol->value.integer.value[1] - min;
Mark Brown9bde4f02012-12-19 16:05:00 +00002606 }
2607
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002608 return 0;
2609}
2610EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2611
2612/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2614 *
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2618 *
2619 * Return 0 for success, else error.
2620 */
2621int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2623{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002624 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2627 int found = 0;
2628 int ret = -EINVAL;
2629
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2632 return -EINVAL;
2633
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2636 found = 1;
2637 break;
2638 }
2639 }
2640 if (found) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002643 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002644 ret = 0;
2645 }
2646 }
2647 return ret;
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2650
Mark Brown71d08512011-10-10 18:31:26 +01002651int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_info *uinfo)
2653{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002654 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002655 struct soc_bytes *params = (void *)kcontrol->private_value;
2656
2657 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002658 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01002659
2660 return 0;
2661}
2662EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2663
2664int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_value *ucontrol)
2666{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002667 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002668 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01002669 int ret;
2670
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002671 if (component->regmap)
2672 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01002673 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002674 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01002675 else
2676 ret = -EINVAL;
2677
Mark Brownf831b052012-02-17 16:20:33 -08002678 /* Hide any masked bytes to ensure consistent data reporting */
2679 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002680 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08002681 case 1:
2682 ucontrol->value.bytes.data[0] &= ~params->mask;
2683 break;
2684 case 2:
2685 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00002686 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08002687 break;
2688 case 4:
2689 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00002690 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08002691 break;
2692 default:
2693 return -EINVAL;
2694 }
2695 }
2696
Mark Brown71d08512011-10-10 18:31:26 +01002697 return ret;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2700
2701int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2703{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002704 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002705 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08002706 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002707 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08002708 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002709
Xiubo Li6596aa02014-09-28 17:29:37 +08002710 if (!component->regmap || !params->num_regs)
Mark Brownf831b052012-02-17 16:20:33 -08002711 return -EINVAL;
2712
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002713 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08002714
Mark Brownb5a8fe42013-01-20 21:42:22 +09002715 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
2716 if (!data)
2717 return -ENOMEM;
2718
Mark Brownf831b052012-02-17 16:20:33 -08002719 /*
2720 * If we've got a mask then we need to preserve the register
2721 * bits. We shouldn't modify the incoming data so take a
2722 * copy.
2723 */
2724 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002725 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08002726 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002727 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08002728
2729 val &= params->mask;
2730
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08002732 case 1:
2733 ((u8 *)data)[0] &= ~params->mask;
2734 ((u8 *)data)[0] |= val;
2735 break;
2736 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002737 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002738 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002739 &mask, &mask);
2740 if (ret != 0)
2741 goto out;
2742
2743 ((u16 *)data)[0] &= mask;
2744
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002745 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002746 &val, &val);
2747 if (ret != 0)
2748 goto out;
2749
2750 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08002751 break;
2752 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002753 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002754 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002755 &mask, &mask);
2756 if (ret != 0)
2757 goto out;
2758
2759 ((u32 *)data)[0] &= mask;
2760
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002761 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002762 &val, &val);
2763 if (ret != 0)
2764 goto out;
2765
2766 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08002767 break;
2768 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002769 ret = -EINVAL;
2770 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08002771 }
2772 }
2773
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002774 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08002775 data, len);
2776
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002777out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09002778 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002779
2780 return ret;
2781}
2782EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2783
Vinod Kould9881202014-05-02 10:58:11 +05302784int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_info *ucontrol)
2786{
2787 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2788
2789 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2790 ucontrol->count = params->max;
2791
2792 return 0;
2793}
2794EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
2795
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05302796int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
2797 unsigned int size, unsigned int __user *tlv)
2798{
2799 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2800 unsigned int count = size < params->max ? size : params->max;
2801 int ret = -ENXIO;
2802
2803 switch (op_flag) {
2804 case SNDRV_CTL_TLV_OP_READ:
2805 if (params->get)
2806 ret = params->get(tlv, count);
2807 break;
2808 case SNDRV_CTL_TLV_OP_WRITE:
2809 if (params->put)
2810 ret = params->put(tlv, count);
2811 break;
2812 }
2813 return ret;
2814}
2815EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
2816
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002817/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002818 * snd_soc_info_xr_sx - signed multi register info callback
2819 * @kcontrol: mreg control
2820 * @uinfo: control element information
2821 *
2822 * Callback to provide information of a control that can
2823 * span multiple codec registers which together
2824 * forms a single signed value in a MSB/LSB manner.
2825 *
2826 * Returns 0 for success.
2827 */
2828int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_info *uinfo)
2830{
2831 struct soc_mreg_control *mc =
2832 (struct soc_mreg_control *)kcontrol->private_value;
2833 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2834 uinfo->count = 1;
2835 uinfo->value.integer.min = mc->min;
2836 uinfo->value.integer.max = mc->max;
2837
2838 return 0;
2839}
2840EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2841
2842/**
2843 * snd_soc_get_xr_sx - signed multi register get callback
2844 * @kcontrol: mreg control
2845 * @ucontrol: control element information
2846 *
2847 * Callback to get the value of a control that can span
2848 * multiple codec registers which together forms a single
2849 * signed value in a MSB/LSB manner. The control supports
2850 * specifying total no of bits used to allow for bitfields
2851 * across the multiple codec registers.
2852 *
2853 * Returns 0 for success.
2854 */
2855int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2856 struct snd_ctl_elem_value *ucontrol)
2857{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002858 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002859 struct soc_mreg_control *mc =
2860 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002861 unsigned int regbase = mc->regbase;
2862 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002863 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002864 unsigned int regwmask = (1<<regwshift)-1;
2865 unsigned int invert = mc->invert;
2866 unsigned long mask = (1UL<<mc->nbits)-1;
2867 long min = mc->min;
2868 long max = mc->max;
2869 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002870 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002871 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002872 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002873
2874 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002875 ret = snd_soc_component_read(component, regbase+i, &regval);
2876 if (ret)
2877 return ret;
2878 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002879 }
2880 val &= mask;
2881 if (min < 0 && val > max)
2882 val |= ~mask;
2883 if (invert)
2884 val = max - val;
2885 ucontrol->value.integer.value[0] = val;
2886
2887 return 0;
2888}
2889EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2890
2891/**
2892 * snd_soc_put_xr_sx - signed multi register get callback
2893 * @kcontrol: mreg control
2894 * @ucontrol: control element information
2895 *
2896 * Callback to set the value of a control that can span
2897 * multiple codec registers which together forms a single
2898 * signed value in a MSB/LSB manner. The control supports
2899 * specifying total no of bits used to allow for bitfields
2900 * across the multiple codec registers.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002907 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002908 struct soc_mreg_control *mc =
2909 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002910 unsigned int regbase = mc->regbase;
2911 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002912 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002913 unsigned int regwmask = (1<<regwshift)-1;
2914 unsigned int invert = mc->invert;
2915 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002916 long max = mc->max;
2917 long val = ucontrol->value.integer.value[0];
2918 unsigned int i, regval, regmask;
2919 int err;
2920
2921 if (invert)
2922 val = max - val;
2923 val &= mask;
2924 for (i = 0; i < regcount; i++) {
2925 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
2926 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002927 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002928 regmask, regval);
2929 if (err < 0)
2930 return err;
2931 }
2932
2933 return 0;
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
2936
2937/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002938 * snd_soc_get_strobe - strobe get callback
2939 * @kcontrol: mixer control
2940 * @ucontrol: control element information
2941 *
2942 * Callback get the value of a strobe mixer control.
2943 *
2944 * Returns 0 for success.
2945 */
2946int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002949 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002950 struct soc_mixer_control *mc =
2951 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002952 unsigned int reg = mc->reg;
2953 unsigned int shift = mc->shift;
2954 unsigned int mask = 1 << shift;
2955 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002956 unsigned int val;
2957 int ret;
2958
2959 ret = snd_soc_component_read(component, reg, &val);
2960 if (ret)
2961 return ret;
2962
2963 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002964
2965 if (shift != 0 && val != 0)
2966 val = val >> shift;
2967 ucontrol->value.enumerated.item[0] = val ^ invert;
2968
2969 return 0;
2970}
2971EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
2972
2973/**
2974 * snd_soc_put_strobe - strobe put callback
2975 * @kcontrol: mixer control
2976 * @ucontrol: control element information
2977 *
2978 * Callback strobe a register bit to high then low (or the inverse)
2979 * in one pass of a single mixer enum control.
2980 *
2981 * Returns 1 for success.
2982 */
2983int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
2985{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002986 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002987 struct soc_mixer_control *mc =
2988 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002989 unsigned int reg = mc->reg;
2990 unsigned int shift = mc->shift;
2991 unsigned int mask = 1 << shift;
2992 unsigned int invert = mc->invert != 0;
2993 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
2994 unsigned int val1 = (strobe ^ invert) ? mask : 0;
2995 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
2996 int err;
2997
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002998 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002999 if (err < 0)
3000 return err;
3001
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003002 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003003}
3004EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3005
3006/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003007 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3008 * @dai: DAI
3009 * @clk_id: DAI specific clock ID
3010 * @freq: new clock frequency in Hz
3011 * @dir: new clock direction - input/output.
3012 *
3013 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3014 */
3015int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3016 unsigned int freq, int dir)
3017{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018 if (dai->driver && dai->driver->ops->set_sysclk)
3019 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003020 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003021 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003022 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003023 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003024 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003025}
3026EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3027
3028/**
Mark Brownec4ee522011-03-07 20:58:11 +00003029 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3030 * @codec: CODEC
3031 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003032 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003033 * @freq: new clock frequency in Hz
3034 * @dir: new clock direction - input/output.
3035 *
3036 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3037 */
3038int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003039 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003040{
3041 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003042 return codec->driver->set_sysclk(codec, clk_id, source,
3043 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003044 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003045 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003046}
3047EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3048
3049/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003050 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3051 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003052 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003053 * @div: new clock divisor.
3054 *
3055 * Configures the clock dividers. This is used to derive the best DAI bit and
3056 * frame clocks from the system or master clock. It's best to set the DAI bit
3057 * and frame clocks as low as possible to save system power.
3058 */
3059int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3060 int div_id, int div)
3061{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003062 if (dai->driver && dai->driver->ops->set_clkdiv)
3063 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003064 else
3065 return -EINVAL;
3066}
3067EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3068
3069/**
3070 * snd_soc_dai_set_pll - configure DAI PLL.
3071 * @dai: DAI
3072 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003073 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003074 * @freq_in: PLL input clock frequency in Hz
3075 * @freq_out: requested PLL output clock frequency in Hz
3076 *
3077 * Configures and enables PLL to generate output clock based on input clock.
3078 */
Mark Brown85488032009-09-05 18:52:16 +01003079int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3080 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003081{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003082 if (dai->driver && dai->driver->ops->set_pll)
3083 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003084 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003085 else if (dai->codec && dai->codec->driver->set_pll)
3086 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3087 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003088 else
3089 return -EINVAL;
3090}
3091EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3092
Mark Brownec4ee522011-03-07 20:58:11 +00003093/*
3094 * snd_soc_codec_set_pll - configure codec PLL.
3095 * @codec: CODEC
3096 * @pll_id: DAI specific PLL ID
3097 * @source: DAI specific source for the PLL
3098 * @freq_in: PLL input clock frequency in Hz
3099 * @freq_out: requested PLL output clock frequency in Hz
3100 *
3101 * Configures and enables PLL to generate output clock based on input clock.
3102 */
3103int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3104 unsigned int freq_in, unsigned int freq_out)
3105{
3106 if (codec->driver->set_pll)
3107 return codec->driver->set_pll(codec, pll_id, source,
3108 freq_in, freq_out);
3109 else
3110 return -EINVAL;
3111}
3112EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3113
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003114/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003115 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3116 * @dai: DAI
3117 * @ratio Ratio of BCLK to Sample rate.
3118 *
3119 * Configures the DAI for a preset BCLK to sample rate ratio.
3120 */
3121int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3122{
3123 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3124 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3125 else
3126 return -EINVAL;
3127}
3128EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3129
3130/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003131 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3132 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003133 * @fmt: SND_SOC_DAIFMT_ format value.
3134 *
3135 * Configures the DAI hardware format and clocking.
3136 */
3137int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3138{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003139 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003140 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003141 if (dai->driver->ops->set_fmt == NULL)
3142 return -ENOTSUPP;
3143 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003144}
3145EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3146
3147/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003148 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003149 * @slots: Number of slots in use.
3150 * @tx_mask: bitmask representing active TX slots.
3151 * @rx_mask: bitmask representing active RX slots.
3152 *
3153 * Generates the TDM tx and rx slot default masks for DAI.
3154 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003155static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003156 unsigned int *tx_mask,
3157 unsigned int *rx_mask)
3158{
3159 if (*tx_mask || *rx_mask)
3160 return 0;
3161
3162 if (!slots)
3163 return -EINVAL;
3164
3165 *tx_mask = (1 << slots) - 1;
3166 *rx_mask = (1 << slots) - 1;
3167
3168 return 0;
3169}
3170
3171/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003172 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3173 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003174 * @tx_mask: bitmask representing active TX slots.
3175 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003176 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003177 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003178 *
3179 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3180 * specific.
3181 */
3182int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003183 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003184{
Xiubo Lie5c21512014-03-21 14:17:12 +08003185 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3186 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003187 &tx_mask, &rx_mask);
3188 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003189 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003190
Benoit Cousson88bd8702014-07-08 23:19:34 +02003191 dai->tx_mask = tx_mask;
3192 dai->rx_mask = rx_mask;
3193
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003194 if (dai->driver && dai->driver->ops->set_tdm_slot)
3195 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003196 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003197 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003198 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003199}
3200EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3201
3202/**
Barry Song472df3c2009-09-12 01:16:29 +08003203 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3204 * @dai: DAI
3205 * @tx_num: how many TX channels
3206 * @tx_slot: pointer to an array which imply the TX slot number channel
3207 * 0~num-1 uses
3208 * @rx_num: how many RX channels
3209 * @rx_slot: pointer to an array which imply the RX slot number channel
3210 * 0~num-1 uses
3211 *
3212 * configure the relationship between channel number and TDM slot number.
3213 */
3214int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3215 unsigned int tx_num, unsigned int *tx_slot,
3216 unsigned int rx_num, unsigned int *rx_slot)
3217{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003218 if (dai->driver && dai->driver->ops->set_channel_map)
3219 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003220 rx_num, rx_slot);
3221 else
3222 return -EINVAL;
3223}
3224EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3225
3226/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003227 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3228 * @dai: DAI
3229 * @tristate: tristate enable
3230 *
3231 * Tristates the DAI so that others can use it.
3232 */
3233int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3234{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003235 if (dai->driver && dai->driver->ops->set_tristate)
3236 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003237 else
3238 return -EINVAL;
3239}
3240EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3241
3242/**
3243 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3244 * @dai: DAI
3245 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003246 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003247 *
3248 * Mutes the DAI DAC.
3249 */
Mark Brownda183962013-02-06 15:44:07 +00003250int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3251 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003252{
Mark Brownda183962013-02-06 15:44:07 +00003253 if (!dai->driver)
3254 return -ENOTSUPP;
3255
3256 if (dai->driver->ops->mute_stream)
3257 return dai->driver->ops->mute_stream(dai, mute, direction);
3258 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3259 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003261 else
Mark Brown04570c62012-04-13 19:16:03 +01003262 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003263}
3264EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3265
Benoit Cousson88bd8702014-07-08 23:19:34 +02003266static int snd_soc_init_multicodec(struct snd_soc_card *card,
3267 struct snd_soc_dai_link *dai_link)
3268{
3269 /* Legacy codec/codec_dai link is a single entry in multicodec */
3270 if (dai_link->codec_name || dai_link->codec_of_node ||
3271 dai_link->codec_dai_name) {
3272 dai_link->num_codecs = 1;
3273
3274 dai_link->codecs = devm_kzalloc(card->dev,
3275 sizeof(struct snd_soc_dai_link_component),
3276 GFP_KERNEL);
3277 if (!dai_link->codecs)
3278 return -ENOMEM;
3279
3280 dai_link->codecs[0].name = dai_link->codec_name;
3281 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3282 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3283 }
3284
3285 if (!dai_link->codecs) {
3286 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3287 return -EINVAL;
3288 }
3289
3290 return 0;
3291}
3292
Mark Brownc5af3a22008-11-28 13:29:45 +00003293/**
3294 * snd_soc_register_card - Register a card with the ASoC core
3295 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003296 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003297 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003298 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303299int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003300{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003301 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003302
Mark Brownc5af3a22008-11-28 13:29:45 +00003303 if (!card->name || !card->dev)
3304 return -EINVAL;
3305
Stephen Warren5a504962011-12-21 10:40:59 -07003306 for (i = 0; i < card->num_links; i++) {
3307 struct snd_soc_dai_link *link = &card->dai_link[i];
3308
Benoit Cousson88bd8702014-07-08 23:19:34 +02003309 ret = snd_soc_init_multicodec(card, link);
3310 if (ret) {
3311 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3312 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003313 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003314
3315 for (j = 0; j < link->num_codecs; j++) {
3316 /*
3317 * Codec must be specified by 1 of name or OF node,
3318 * not both or neither.
3319 */
3320 if (!!link->codecs[j].name ==
3321 !!link->codecs[j].of_node) {
3322 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3323 link->name);
3324 return -EINVAL;
3325 }
3326 /* Codec DAI name must be specified */
3327 if (!link->codecs[j].dai_name) {
3328 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3329 link->name);
3330 return -EINVAL;
3331 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003332 }
Stephen Warren5a504962011-12-21 10:40:59 -07003333
3334 /*
3335 * Platform may be specified by either name or OF node, but
3336 * can be left unspecified, and a dummy platform will be used.
3337 */
3338 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003339 dev_err(card->dev,
3340 "ASoC: Both platform name/of_node are set for %s\n",
3341 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003342 return -EINVAL;
3343 }
3344
3345 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003346 * CPU device may be specified by either name or OF node, but
3347 * can be left unspecified, and will be matched based on DAI
3348 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003349 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003350 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003351 dev_err(card->dev,
3352 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3353 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003354 return -EINVAL;
3355 }
3356 /*
3357 * At least one of CPU DAI name or CPU device name/node must be
3358 * specified
3359 */
3360 if (!link->cpu_dai_name &&
3361 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003362 dev_err(card->dev,
3363 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3364 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003365 return -EINVAL;
3366 }
3367 }
3368
Mark Browned77cc12011-05-03 18:25:34 +01003369 dev_set_drvdata(card->dev, card);
3370
Stephen Warren111c6412011-01-28 14:26:35 -07003371 snd_soc_initialize_card_lists(card);
3372
Vinod Koul150dd2f2011-01-13 22:48:32 +05303373 soc_init_card_debugfs(card);
3374
Mark Brown181a6892012-03-14 20:18:49 +00003375 card->rtd = devm_kzalloc(card->dev,
3376 sizeof(struct snd_soc_pcm_runtime) *
3377 (card->num_links + card->num_aux_devs),
3378 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003379 if (card->rtd == NULL)
3380 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003381 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003382 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003384 for (i = 0; i < card->num_links; i++) {
3385 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003386 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003387 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3388 sizeof(struct snd_soc_dai *) *
3389 (card->rtd[i].dai_link->num_codecs),
3390 GFP_KERNEL);
3391 if (card->rtd[i].codec_dais == NULL)
3392 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003393 }
3394
3395 for (i = 0; i < card->num_aux_devs; i++)
3396 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003397
Mark Browndb432b42011-10-03 21:06:40 +01003398 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003399 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003401 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003402
Mark Brownb19e6e72012-03-14 21:18:39 +00003403 ret = snd_soc_instantiate_card(card);
3404 if (ret != 0)
3405 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003406
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003407 /* deactivate pins to sleep state */
3408 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003409 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3410 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3411 int j;
3412
3413 for (j = 0; j < rtd->num_codecs; j++) {
3414 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3415 if (!codec_dai->active)
3416 pinctrl_pm_select_sleep_state(codec_dai->dev);
3417 }
3418
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003419 if (!cpu_dai->active)
3420 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3421 }
3422
Mark Brownb19e6e72012-03-14 21:18:39 +00003423 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003424}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303425EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003426
3427/**
3428 * snd_soc_unregister_card - Unregister a card with the ASoC core
3429 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003430 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003431 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003432 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303433int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003434{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003435 if (card->instantiated) {
3436 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02003437 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05303438 soc_cleanup_card_resources(card);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003439 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003440 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003441
3442 return 0;
3443}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303444EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003445
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003446/*
3447 * Simplify DAI link configuration by removing ".-1" from device names
3448 * and sanitizing names.
3449 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003450static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003451{
3452 char *found, name[NAME_SIZE];
3453 int id1, id2;
3454
3455 if (dev_name(dev) == NULL)
3456 return NULL;
3457
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003458 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003459
3460 /* are we a "%s.%d" name (platform and SPI components) */
3461 found = strstr(name, dev->driver->name);
3462 if (found) {
3463 /* get ID */
3464 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3465
3466 /* discard ID from name if ID == -1 */
3467 if (*id == -1)
3468 found[strlen(dev->driver->name)] = '\0';
3469 }
3470
3471 } else {
3472 /* I2C component devices are named "bus-addr" */
3473 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3474 char tmp[NAME_SIZE];
3475
3476 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003477 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003478
3479 /* sanitize component name for DAI link creation */
3480 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003481 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003482 } else
3483 *id = 0;
3484 }
3485
3486 return kstrdup(name, GFP_KERNEL);
3487}
3488
3489/*
3490 * Simplify DAI link naming for single devices with multiple DAIs by removing
3491 * any ".-1" and using the DAI name (instead of device name).
3492 */
3493static inline char *fmt_multiple_name(struct device *dev,
3494 struct snd_soc_dai_driver *dai_drv)
3495{
3496 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003497 dev_err(dev,
3498 "ASoC: error - multiple DAI %s registered with no name\n",
3499 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 return NULL;
3501 }
3502
3503 return kstrdup(dai_drv->name, GFP_KERNEL);
3504}
3505
Mark Brown91151712008-11-30 23:31:24 +00003506/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003507 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003508 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003509 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003510 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003511static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003512{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003513 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003514
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003515 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003516 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3517 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003518 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003519 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003520 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003521 }
Mark Brown91151712008-11-30 23:31:24 +00003522}
Mark Brown91151712008-11-30 23:31:24 +00003523
3524/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003525 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003526 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003527 * @component: The component the DAIs are registered for
3528 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003529 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003530 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3531 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003532 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003533static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003534 struct snd_soc_dai_driver *dai_drv, size_t count,
3535 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003536{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003537 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003538 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003539 unsigned int i;
3540 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003542 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003543
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003544 component->dai_drv = dai_drv;
3545 component->num_dai = count;
3546
Mark Brown91151712008-11-30 23:31:24 +00003547 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548
3549 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003550 if (dai == NULL) {
3551 ret = -ENOMEM;
3552 goto err;
3553 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003554
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003555 /*
3556 * Back in the old days when we still had component-less DAIs,
3557 * instead of having a static name, component-less DAIs would
3558 * inherit the name of the parent device so it is possible to
3559 * register multiple instances of the DAI. We still need to keep
3560 * the same naming style even though those DAIs are not
3561 * component-less anymore.
3562 */
3563 if (count == 1 && legacy_dai_naming) {
3564 dai->name = fmt_single_name(dev, &dai->id);
3565 } else {
3566 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3567 if (dai_drv[i].id)
3568 dai->id = dai_drv[i].id;
3569 else
3570 dai->id = i;
3571 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003572 if (dai->name == NULL) {
3573 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003574 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003575 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003576 }
3577
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003578 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003579 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003580 dai->driver = &dai_drv[i];
3581 if (!dai->driver->ops)
3582 dai->driver->ops = &null_dai_ops;
3583
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003584 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003585
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003586 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003587 }
3588
3589 return 0;
3590
3591err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003592 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003593
3594 return ret;
3595}
Mark Brown91151712008-11-30 23:31:24 +00003596
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003597static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3598 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003599{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003600 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003601
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003602 component->driver->seq_notifier(component, type, subseq);
3603}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003604
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003605static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3606 int event)
3607{
3608 struct snd_soc_component *component = dapm->component;
3609
3610 return component->driver->stream_event(component, event);
3611}
3612
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003613static int snd_soc_component_initialize(struct snd_soc_component *component,
3614 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003615{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003616 struct snd_soc_dapm_context *dapm;
3617
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003618 component->name = fmt_single_name(dev, &component->id);
3619 if (!component->name) {
3620 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003621 return -ENOMEM;
3622 }
3623
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003624 component->dev = dev;
3625 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003626 component->probe = component->driver->probe;
3627 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003628
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003629 if (!component->dapm_ptr)
3630 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003631
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003632 dapm = component->dapm_ptr;
3633 dapm->dev = dev;
3634 dapm->component = component;
3635 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003636 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003637 if (driver->seq_notifier)
3638 dapm->seq_notifier = snd_soc_component_seq_notifier;
3639 if (driver->stream_event)
3640 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003641
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003642 component->controls = driver->controls;
3643 component->num_controls = driver->num_controls;
3644 component->dapm_widgets = driver->dapm_widgets;
3645 component->num_dapm_widgets = driver->num_dapm_widgets;
3646 component->dapm_routes = driver->dapm_routes;
3647 component->num_dapm_routes = driver->num_dapm_routes;
3648
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003649 INIT_LIST_HEAD(&component->dai_list);
3650 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003651
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003652 return 0;
3653}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003654
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003655static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003656{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003657 int val_bytes = regmap_get_val_bytes(component->regmap);
3658
3659 /* Errors are legitimate for non-integer byte multiples */
3660 if (val_bytes > 0)
3661 component->val_bytes = val_bytes;
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003662}
3663
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003664#ifdef CONFIG_REGMAP
3665
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003666/**
3667 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3668 * @component: The component for which to initialize the regmap instance
3669 * @regmap: The regmap instance that should be used by the component
3670 *
3671 * This function allows deferred assignment of the regmap instance that is
3672 * associated with the component. Only use this if the regmap instance is not
3673 * yet ready when the component is registered. The function must also be called
3674 * before the first IO attempt of the component.
3675 */
3676void snd_soc_component_init_regmap(struct snd_soc_component *component,
3677 struct regmap *regmap)
3678{
3679 component->regmap = regmap;
3680 snd_soc_component_setup_regmap(component);
3681}
3682EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3683
3684/**
3685 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3686 * @component: The component for which to de-initialize the regmap instance
3687 *
3688 * Calls regmap_exit() on the regmap instance associated to the component and
3689 * removes the regmap instance from the component.
3690 *
3691 * This function should only be used if snd_soc_component_init_regmap() was used
3692 * to initialize the regmap instance.
3693 */
3694void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3695{
3696 regmap_exit(component->regmap);
3697 component->regmap = NULL;
3698}
3699EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
3700
Lars-Peter Clausene874bf52014-11-25 21:41:03 +01003701#endif
3702
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003703static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3704{
Lars-Peter Clausen20feb882014-11-18 19:45:52 +01003705 if (!component->write && !component->read) {
3706 if (!component->regmap)
3707 component->regmap = dev_get_regmap(component->dev, NULL);
3708 if (component->regmap)
3709 snd_soc_component_setup_regmap(component);
3710 }
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003711
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003712 list_add(&component->list, &component_list);
3713}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003714
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003715static void snd_soc_component_add(struct snd_soc_component *component)
3716{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003717 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003718 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003719 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003720}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003721
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003722static void snd_soc_component_cleanup(struct snd_soc_component *component)
3723{
3724 snd_soc_unregister_dais(component);
3725 kfree(component->name);
3726}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003727
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003728static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3729{
3730 list_del(&component->list);
3731}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003732
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003733static void snd_soc_component_del(struct snd_soc_component *component)
3734{
3735 mutex_lock(&client_mutex);
3736 snd_soc_component_del_unlocked(component);
3737 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003738}
3739
3740int snd_soc_register_component(struct device *dev,
3741 const struct snd_soc_component_driver *cmpnt_drv,
3742 struct snd_soc_dai_driver *dai_drv,
3743 int num_dai)
3744{
3745 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003746 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003747
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003748 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003749 if (!cmpnt) {
3750 dev_err(dev, "ASoC: Failed to allocate memory\n");
3751 return -ENOMEM;
3752 }
3753
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003754 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3755 if (ret)
3756 goto err_free;
3757
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003758 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003759 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003760
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003761 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3762 if (ret < 0) {
3763 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3764 goto err_cleanup;
3765 }
3766
3767 snd_soc_component_add(cmpnt);
3768
3769 return 0;
3770
3771err_cleanup:
3772 snd_soc_component_cleanup(cmpnt);
3773err_free:
3774 kfree(cmpnt);
3775 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003776}
3777EXPORT_SYMBOL_GPL(snd_soc_register_component);
3778
3779/**
3780 * snd_soc_unregister_component - Unregister a component from the ASoC core
3781 *
3782 */
3783void snd_soc_unregister_component(struct device *dev)
3784{
3785 struct snd_soc_component *cmpnt;
3786
3787 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003788 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003789 goto found;
3790 }
3791 return;
3792
3793found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003794 snd_soc_component_del(cmpnt);
3795 snd_soc_component_cleanup(cmpnt);
3796 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003797}
3798EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3799
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003800static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003801{
3802 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3803
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003804 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003805}
3806
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003807static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003808{
3809 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3810
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003811 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003812}
3813
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003814/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003815 * snd_soc_add_platform - Add a platform to the ASoC core
3816 * @dev: The parent device for the platform
3817 * @platform: The platform to add
3818 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003819 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003820int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003821 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003822{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003823 int ret;
3824
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003825 ret = snd_soc_component_initialize(&platform->component,
3826 &platform_drv->component_driver, dev);
3827 if (ret)
3828 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003829
3830 platform->dev = dev;
3831 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003832
3833 if (platform_drv->probe)
3834 platform->component.probe = snd_soc_platform_drv_probe;
3835 if (platform_drv->remove)
3836 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003837
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003838#ifdef CONFIG_DEBUG_FS
3839 platform->component.debugfs_prefix = "platform";
3840#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00003841
3842 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003843 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003844 list_add(&platform->list, &platform_list);
3845 mutex_unlock(&client_mutex);
3846
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003847 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3848 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003849
3850 return 0;
3851}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003852EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3853
3854/**
3855 * snd_soc_register_platform - Register a platform with the ASoC core
3856 *
3857 * @platform: platform to register
3858 */
3859int snd_soc_register_platform(struct device *dev,
3860 const struct snd_soc_platform_driver *platform_drv)
3861{
3862 struct snd_soc_platform *platform;
3863 int ret;
3864
3865 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3866
3867 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3868 if (platform == NULL)
3869 return -ENOMEM;
3870
3871 ret = snd_soc_add_platform(dev, platform, platform_drv);
3872 if (ret)
3873 kfree(platform);
3874
3875 return ret;
3876}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003877EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3878
3879/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003880 * snd_soc_remove_platform - Remove a platform from the ASoC core
3881 * @platform: the platform to remove
3882 */
3883void snd_soc_remove_platform(struct snd_soc_platform *platform)
3884{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003885
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003886 mutex_lock(&client_mutex);
3887 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003888 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003889 mutex_unlock(&client_mutex);
3890
3891 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003892 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02003893
3894 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003895}
3896EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3897
3898struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3899{
3900 struct snd_soc_platform *platform;
3901
3902 list_for_each_entry(platform, &platform_list, list) {
3903 if (dev == platform->dev)
3904 return platform;
3905 }
3906
3907 return NULL;
3908}
3909EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3910
3911/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003912 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3913 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003914 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003915 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003916void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003917{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003918 struct snd_soc_platform *platform;
3919
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003920 platform = snd_soc_lookup_platform(dev);
3921 if (!platform)
3922 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003923
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003924 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003926}
3927EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3928
Mark Brown151ab222009-05-09 16:22:58 +01003929static u64 codec_format_map[] = {
3930 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3931 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3932 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3933 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3934 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3935 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3936 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3937 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3938 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3939 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3940 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3941 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3942 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3943 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3944 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3945 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3946};
3947
3948/* Fix up the DAI formats for endianness: codecs don't actually see
3949 * the endianness of the data but we're using the CPU format
3950 * definitions which do need to include endianness so we ensure that
3951 * codec DAIs always have both big and little endian variants set.
3952 */
3953static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3954{
3955 int i;
3956
3957 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3958 if (stream->formats & codec_format_map[i])
3959 stream->formats |= codec_format_map[i];
3960}
3961
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003962static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3963{
3964 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3965
3966 return codec->driver->probe(codec);
3967}
3968
3969static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3970{
3971 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3972
3973 codec->driver->remove(codec);
3974}
3975
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003976static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3977 unsigned int reg, unsigned int val)
3978{
3979 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3980
3981 return codec->driver->write(codec, reg, val);
3982}
3983
3984static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3985 unsigned int reg, unsigned int *val)
3986{
3987 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3988
3989 *val = codec->driver->read(codec, reg);
3990
3991 return 0;
3992}
3993
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003994static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3995 enum snd_soc_bias_level level)
3996{
3997 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3998
3999 return codec->driver->set_bias_level(codec, level);
4000}
4001
Mark Brown0d0cf002008-12-10 14:32:45 +00004002/**
4003 * snd_soc_register_codec - Register a codec with the ASoC core
4004 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004005 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004006 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004008 const struct snd_soc_codec_driver *codec_drv,
4009 struct snd_soc_dai_driver *dai_drv,
4010 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004011{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004012 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004013 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004014 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004016 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004017
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004018 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4019 if (codec == NULL)
4020 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004021
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004022 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004023 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004024
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004025 ret = snd_soc_component_initialize(&codec->component,
4026 &codec_drv->component_driver, dev);
4027 if (ret)
4028 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004029
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004030 if (codec_drv->controls) {
4031 codec->component.controls = codec_drv->controls;
4032 codec->component.num_controls = codec_drv->num_controls;
4033 }
4034 if (codec_drv->dapm_widgets) {
4035 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4036 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4037 }
4038 if (codec_drv->dapm_routes) {
4039 codec->component.dapm_routes = codec_drv->dapm_routes;
4040 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4041 }
4042
4043 if (codec_drv->probe)
4044 codec->component.probe = snd_soc_codec_drv_probe;
4045 if (codec_drv->remove)
4046 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004047 if (codec_drv->write)
4048 codec->component.write = snd_soc_codec_drv_write;
4049 if (codec_drv->read)
4050 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004051 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004052 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02004053 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004054 if (codec_drv->seq_notifier)
4055 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004056 if (codec_drv->set_bias_level)
4057 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004058 codec->dev = dev;
4059 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004060 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004061 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004062
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004063#ifdef CONFIG_DEBUG_FS
4064 codec->component.init_debugfs = soc_init_codec_debugfs;
4065 codec->component.debugfs_prefix = "codec";
4066#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08004067
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004068 if (codec_drv->get_regmap)
4069 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08004070
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004071 for (i = 0; i < num_dai; i++) {
4072 fixup_codec_formats(&dai_drv[i].playback);
4073 fixup_codec_formats(&dai_drv[i].capture);
4074 }
4075
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004076 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4077 if (ret < 0) {
4078 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4079 goto err_cleanup;
4080 }
4081
4082 list_for_each_entry(dai, &codec->component.dai_list, list)
4083 dai->codec = codec;
4084
Mark Brown054880f2012-04-09 17:29:19 +01004085 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004086 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004087 list_add(&codec->list, &codec_list);
4088 mutex_unlock(&client_mutex);
4089
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004090 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4091 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004092 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004093
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004094err_cleanup:
4095 snd_soc_component_cleanup(&codec->component);
4096err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004097 kfree(codec);
4098 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004099}
4100EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4101
4102/**
4103 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4104 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004105 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004106 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004107void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004108{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004109 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004110
4111 list_for_each_entry(codec, &codec_list, list) {
4112 if (dev == codec->dev)
4113 goto found;
4114 }
4115 return;
4116
4117found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004118
Mark Brown0d0cf002008-12-10 14:32:45 +00004119 mutex_lock(&client_mutex);
4120 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004121 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004122 mutex_unlock(&client_mutex);
4123
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004124 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4125 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004126
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004127 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004128 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004129 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004130}
4131EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4132
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004133/* Retrieve a card's name from device tree */
4134int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4135 const char *propname)
4136{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304137 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004138 int ret;
4139
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304140 if (!card->dev) {
4141 pr_err("card->dev is not set before calling %s\n", __func__);
4142 return -EINVAL;
4143 }
4144
4145 np = card->dev->of_node;
4146
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004147 ret = of_property_read_string_index(np, propname, 0, &card->name);
4148 /*
4149 * EINVAL means the property does not exist. This is fine providing
4150 * card->name was previously set, which is checked later in
4151 * snd_soc_register_card.
4152 */
4153 if (ret < 0 && ret != -EINVAL) {
4154 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004155 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004156 propname, ret);
4157 return ret;
4158 }
4159
4160 return 0;
4161}
4162EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4163
Xiubo Li9a6d4862014-02-08 15:59:52 +08004164static const struct snd_soc_dapm_widget simple_widgets[] = {
4165 SND_SOC_DAPM_MIC("Microphone", NULL),
4166 SND_SOC_DAPM_LINE("Line", NULL),
4167 SND_SOC_DAPM_HP("Headphone", NULL),
4168 SND_SOC_DAPM_SPK("Speaker", NULL),
4169};
4170
4171int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4172 const char *propname)
4173{
4174 struct device_node *np = card->dev->of_node;
4175 struct snd_soc_dapm_widget *widgets;
4176 const char *template, *wname;
4177 int i, j, num_widgets, ret;
4178
4179 num_widgets = of_property_count_strings(np, propname);
4180 if (num_widgets < 0) {
4181 dev_err(card->dev,
4182 "ASoC: Property '%s' does not exist\n", propname);
4183 return -EINVAL;
4184 }
4185 if (num_widgets & 1) {
4186 dev_err(card->dev,
4187 "ASoC: Property '%s' length is not even\n", propname);
4188 return -EINVAL;
4189 }
4190
4191 num_widgets /= 2;
4192 if (!num_widgets) {
4193 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4194 propname);
4195 return -EINVAL;
4196 }
4197
4198 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4199 GFP_KERNEL);
4200 if (!widgets) {
4201 dev_err(card->dev,
4202 "ASoC: Could not allocate memory for widgets\n");
4203 return -ENOMEM;
4204 }
4205
4206 for (i = 0; i < num_widgets; i++) {
4207 ret = of_property_read_string_index(np, propname,
4208 2 * i, &template);
4209 if (ret) {
4210 dev_err(card->dev,
4211 "ASoC: Property '%s' index %d read error:%d\n",
4212 propname, 2 * i, ret);
4213 return -EINVAL;
4214 }
4215
4216 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4217 if (!strncmp(template, simple_widgets[j].name,
4218 strlen(simple_widgets[j].name))) {
4219 widgets[i] = simple_widgets[j];
4220 break;
4221 }
4222 }
4223
4224 if (j >= ARRAY_SIZE(simple_widgets)) {
4225 dev_err(card->dev,
4226 "ASoC: DAPM widget '%s' is not supported\n",
4227 template);
4228 return -EINVAL;
4229 }
4230
4231 ret = of_property_read_string_index(np, propname,
4232 (2 * i) + 1,
4233 &wname);
4234 if (ret) {
4235 dev_err(card->dev,
4236 "ASoC: Property '%s' index %d read error:%d\n",
4237 propname, (2 * i) + 1, ret);
4238 return -EINVAL;
4239 }
4240
4241 widgets[i].name = wname;
4242 }
4243
4244 card->dapm_widgets = widgets;
4245 card->num_dapm_widgets = num_widgets;
4246
4247 return 0;
4248}
4249EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4250
Xiubo Li89c67852014-02-14 09:34:35 +08004251int snd_soc_of_parse_tdm_slot(struct device_node *np,
4252 unsigned int *slots,
4253 unsigned int *slot_width)
4254{
4255 u32 val;
4256 int ret;
4257
4258 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4259 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4260 if (ret)
4261 return ret;
4262
4263 if (slots)
4264 *slots = val;
4265 }
4266
4267 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4268 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4269 if (ret)
4270 return ret;
4271
4272 if (slot_width)
4273 *slot_width = val;
4274 }
4275
4276 return 0;
4277}
4278EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4279
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004280int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4281 const char *propname)
4282{
4283 struct device_node *np = card->dev->of_node;
4284 int num_routes;
4285 struct snd_soc_dapm_route *routes;
4286 int i, ret;
4287
4288 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004289 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004290 dev_err(card->dev,
4291 "ASoC: Property '%s' does not exist or its length is not even\n",
4292 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004293 return -EINVAL;
4294 }
4295 num_routes /= 2;
4296 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004297 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004298 propname);
4299 return -EINVAL;
4300 }
4301
4302 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4303 GFP_KERNEL);
4304 if (!routes) {
4305 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004306 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004307 return -EINVAL;
4308 }
4309
4310 for (i = 0; i < num_routes; i++) {
4311 ret = of_property_read_string_index(np, propname,
4312 2 * i, &routes[i].sink);
4313 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004314 dev_err(card->dev,
4315 "ASoC: Property '%s' index %d could not be read: %d\n",
4316 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004317 return -EINVAL;
4318 }
4319 ret = of_property_read_string_index(np, propname,
4320 (2 * i) + 1, &routes[i].source);
4321 if (ret) {
4322 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004323 "ASoC: Property '%s' index %d could not be read: %d\n",
4324 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004325 return -EINVAL;
4326 }
4327 }
4328
4329 card->num_dapm_routes = num_routes;
4330 card->dapm_routes = routes;
4331
4332 return 0;
4333}
4334EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4335
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004336unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004337 const char *prefix,
4338 struct device_node **bitclkmaster,
4339 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004340{
4341 int ret, i;
4342 char prop[128];
4343 unsigned int format = 0;
4344 int bit, frame;
4345 const char *str;
4346 struct {
4347 char *name;
4348 unsigned int val;
4349 } of_fmt_table[] = {
4350 { "i2s", SND_SOC_DAIFMT_I2S },
4351 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4352 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4353 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4354 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4355 { "ac97", SND_SOC_DAIFMT_AC97 },
4356 { "pdm", SND_SOC_DAIFMT_PDM},
4357 { "msb", SND_SOC_DAIFMT_MSB },
4358 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004359 };
4360
4361 if (!prefix)
4362 prefix = "";
4363
4364 /*
4365 * check "[prefix]format = xxx"
4366 * SND_SOC_DAIFMT_FORMAT_MASK area
4367 */
4368 snprintf(prop, sizeof(prop), "%sformat", prefix);
4369 ret = of_property_read_string(np, prop, &str);
4370 if (ret == 0) {
4371 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4372 if (strcmp(str, of_fmt_table[i].name) == 0) {
4373 format |= of_fmt_table[i].val;
4374 break;
4375 }
4376 }
4377 }
4378
4379 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004380 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004381 * SND_SOC_DAIFMT_CLOCK_MASK area
4382 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004383 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4384 if (of_get_property(np, prop, NULL))
4385 format |= SND_SOC_DAIFMT_CONT;
4386 else
4387 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004388
4389 /*
4390 * check "[prefix]bitclock-inversion"
4391 * check "[prefix]frame-inversion"
4392 * SND_SOC_DAIFMT_INV_MASK area
4393 */
4394 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4395 bit = !!of_get_property(np, prop, NULL);
4396
4397 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4398 frame = !!of_get_property(np, prop, NULL);
4399
4400 switch ((bit << 4) + frame) {
4401 case 0x11:
4402 format |= SND_SOC_DAIFMT_IB_IF;
4403 break;
4404 case 0x10:
4405 format |= SND_SOC_DAIFMT_IB_NF;
4406 break;
4407 case 0x01:
4408 format |= SND_SOC_DAIFMT_NB_IF;
4409 break;
4410 default:
4411 /* SND_SOC_DAIFMT_NB_NF is default */
4412 break;
4413 }
4414
4415 /*
4416 * check "[prefix]bitclock-master"
4417 * check "[prefix]frame-master"
4418 * SND_SOC_DAIFMT_MASTER_MASK area
4419 */
4420 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4421 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004422 if (bit && bitclkmaster)
4423 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004424
4425 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4426 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004427 if (frame && framemaster)
4428 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004429
4430 switch ((bit << 4) + frame) {
4431 case 0x11:
4432 format |= SND_SOC_DAIFMT_CBM_CFM;
4433 break;
4434 case 0x10:
4435 format |= SND_SOC_DAIFMT_CBM_CFS;
4436 break;
4437 case 0x01:
4438 format |= SND_SOC_DAIFMT_CBS_CFM;
4439 break;
4440 default:
4441 format |= SND_SOC_DAIFMT_CBS_CFS;
4442 break;
4443 }
4444
4445 return format;
4446}
4447EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4448
Kuninori Morimotocb470082013-09-10 17:39:56 -07004449int snd_soc_of_get_dai_name(struct device_node *of_node,
4450 const char **dai_name)
4451{
4452 struct snd_soc_component *pos;
4453 struct of_phandle_args args;
4454 int ret;
4455
4456 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4457 "#sound-dai-cells", 0, &args);
4458 if (ret)
4459 return ret;
4460
4461 ret = -EPROBE_DEFER;
4462
4463 mutex_lock(&client_mutex);
4464 list_for_each_entry(pos, &component_list, list) {
4465 if (pos->dev->of_node != args.np)
4466 continue;
4467
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004468 if (pos->driver->of_xlate_dai_name) {
4469 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4470 } else {
4471 int id = -1;
4472
4473 switch (args.args_count) {
4474 case 0:
4475 id = 0; /* same as dai_drv[0] */
4476 break;
4477 case 1:
4478 id = args.args[0];
4479 break;
4480 default:
4481 /* not supported */
4482 break;
4483 }
4484
4485 if (id < 0 || id >= pos->num_dai) {
4486 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004487 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004488 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004489
4490 ret = 0;
4491
4492 *dai_name = pos->dai_drv[id].name;
4493 if (!*dai_name)
4494 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004495 }
4496
Kuninori Morimotocb470082013-09-10 17:39:56 -07004497 break;
4498 }
4499 mutex_unlock(&client_mutex);
4500
4501 of_node_put(args.np);
4502
4503 return ret;
4504}
4505EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4506
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004507static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004508{
Mark Brown384c89e2008-12-03 17:34:03 +00004509#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004510 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4511 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004512 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004513 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004514 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004515
Mark Brown8a9dab12011-01-10 22:25:21 +00004516 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004517 &codec_list_fops))
4518 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4519
Mark Brown8a9dab12011-01-10 22:25:21 +00004520 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004521 &dai_list_fops))
4522 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004523
Mark Brown8a9dab12011-01-10 22:25:21 +00004524 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004525 &platform_list_fops))
4526 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004527#endif
4528
Mark Brownfb257892011-04-28 10:57:54 +01004529 snd_soc_util_init();
4530
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004531 return platform_driver_register(&soc_driver);
4532}
Mark Brown4abe8e12010-10-12 17:41:03 +01004533module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004534
Mark Brown7d8c16a2008-11-30 22:11:24 +00004535static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004536{
Mark Brownfb257892011-04-28 10:57:54 +01004537 snd_soc_util_exit();
4538
Mark Brown384c89e2008-12-03 17:34:03 +00004539#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004540 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004541#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004542 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004543}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004544module_exit(snd_soc_exit);
4545
4546/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004547MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004548MODULE_DESCRIPTION("ALSA SoC Core");
4549MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004550MODULE_ALIAS("platform:soc-audio");