blob: fba6e28e18d33e863065e1dc0f5eebc2e29af17a [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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
554 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
632 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
633 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 /* resume AC97 DAIs */
669 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
676 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
737 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);
Stephen Warren82e14e82011-05-25 14:06:41 -0600761 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200762
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200763 /* If the card is not initialized yet there is nothing to do */
764 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800765 return 0;
766
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800767 /* activate pins from sleep state */
768 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200769 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
770 struct snd_soc_dai **codec_dais = rtd->codec_dais;
771 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
772 int j;
773
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800774 if (cpu_dai->active)
775 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200776
777 for (j = 0; j < rtd->num_codecs; j++) {
778 struct snd_soc_dai *codec_dai = codec_dais[j];
779 if (codec_dai->active)
780 pinctrl_pm_select_default_state(codec_dai->dev);
781 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800782 }
783
Mark Brown64ab9ba2009-03-31 11:27:03 +0100784 /* AC97 devices might have other drivers hanging off them so
785 * need to resume immediately. Other drivers don't have that
786 * problem and may take a substantial amount of time to resume
787 * due to I/O costs and anti-pop so handle them out of line.
788 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 for (i = 0; i < card->num_rtd; i++) {
790 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600791 ac97_control |= cpu_dai->driver->ac97_control;
792 }
793 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000794 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600795 soc_resume_deferred(&card->deferred_resume_work);
796 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000797 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600798 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000799 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100800 }
Andy Green6ed25972008-06-13 16:24:05 +0100801
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 return 0;
803}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000804EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806#define snd_soc_suspend NULL
807#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200808#endif
809
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100810static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800811};
812
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200813static struct snd_soc_component *soc_find_component(
814 const struct device_node *of_node, const char *name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100815{
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200816 struct snd_soc_component *component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100817
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200818 list_for_each_entry(component, &component_list, list) {
819 if (of_node) {
820 if (component->dev->of_node == of_node)
821 return component;
822 } else if (strcmp(component->name, name) == 0) {
823 return component;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100824 }
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100825 }
826
827 return NULL;
828}
829
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200830static struct snd_soc_dai *snd_soc_find_dai(
831 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100832{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200833 struct snd_soc_component *component;
834 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100835
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200836 /* Find CPU DAI from registered DAIs*/
837 list_for_each_entry(component, &component_list, list) {
838 if (dlc->of_node && component->dev->of_node != dlc->of_node)
839 continue;
840 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
841 continue;
842 list_for_each_entry(dai, &component->dai_list, list) {
843 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100844 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100845
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200846 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100847 }
848 }
849
850 return NULL;
851}
852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
856 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200857 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200858 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200859 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000860 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100861 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200862 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000864 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000865
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200866 cpu_dai_component.name = dai_link->cpu_name;
867 cpu_dai_component.of_node = dai_link->cpu_of_node;
868 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
869 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000870 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000871 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000873 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000874 }
875
Benoit Cousson88bd8702014-07-08 23:19:34 +0200876 rtd->num_codecs = dai_link->num_codecs;
877
878 /* Find CODEC from registered CODECs */
879 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200880 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200881 if (!codec_dais[i]) {
882 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
883 codecs[i].dai_name);
884 return -EPROBE_DEFER;
885 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000886 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100887
Benoit Cousson88bd8702014-07-08 23:19:34 +0200888 /* Single codec links expect codec and codec_dai in runtime data */
889 rtd->codec_dai = codec_dais[0];
890 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891
Mark Brown848dd8b2011-04-27 18:16:32 +0100892 /* if there's no platform we match on the empty platform */
893 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700894 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100895 platform_name = "snd-soc-dummy";
896
Mark Brownb19e6e72012-03-14 21:18:39 +0000897 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700899 if (dai_link->platform_of_node) {
900 if (platform->dev->of_node !=
901 dai_link->platform_of_node)
902 continue;
903 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200904 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700905 continue;
906 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700907
908 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000910 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000911 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000913 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000915
916 card->num_rtd++;
917
918 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919}
920
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200921static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600922{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200923 if (!component->probed)
924 return;
Stephen Warrend12cd192012-06-08 12:34:22 -0600925
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200926 /* This is a HACK and will be removed soon */
927 if (component->codec)
928 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600929
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200930 if (component->remove)
931 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600932
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200933 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600934
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200935 soc_cleanup_component_debugfs(component);
936 component->probed = 0;
937 module_put(component->dev->driver->owner);
Stephen Warrend12cd192012-06-08 12:34:22 -0600938}
939
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200940static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200941{
942 int err;
943
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200944 if (dai && dai->probed &&
945 dai->driver->remove_order == order) {
946 if (dai->driver->remove) {
947 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100948 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200949 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100950 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200951 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100952 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200953 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100954 }
955}
956
Stephen Warren62ae68f2012-06-08 12:34:23 -0600957static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958{
959 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200960 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961
962 /* unregister the rtd device */
963 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800964 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
965 device_remove_file(rtd->dev, &dev_attr_codec_reg);
966 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000967 rtd->dev_registered = 0;
968 }
969
970 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200971 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200972 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200974 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975}
976
Stephen Warren62ae68f2012-06-08 12:34:23 -0600977static void soc_remove_link_components(struct snd_soc_card *card, int num,
978 int order)
979{
980 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
981 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600982 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200983 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200984 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -0600985
986 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200987 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200988 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600989
990 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +0200991 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200992 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200993 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +0200994 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -0600995 }
996
997 /* remove any CPU-side CODEC */
998 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200999 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001000 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001 }
1002}
1003
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001004static void soc_remove_dai_links(struct snd_soc_card *card)
1005{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001006 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001007
Liam Girdwood0168bf02011-06-07 16:08:05 +01001008 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1009 order++) {
1010 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001011 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001012 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001013
1014 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1015 order++) {
1016 for (dai = 0; dai < card->num_rtd; dai++)
1017 soc_remove_link_components(card, dai, order);
1018 }
1019
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001020 card->num_rtd = 0;
1021}
1022
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001023static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001024 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001025{
1026 int i;
1027
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001028 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001029 return;
1030
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001031 for (i = 0; i < card->num_configs; i++) {
1032 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001033 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001034 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001035 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001036 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001037 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001038 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001039 }
1040}
1041
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001042static int soc_probe_component(struct snd_soc_card *card,
1043 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001044{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001045 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001046 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001047 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001048
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001049 if (component->probed)
1050 return 0;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001051
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001052 component->card = card;
1053 dapm->card = card;
1054 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001055
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001056 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001057 return -ENODEV;
1058
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001059 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001060
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001061 if (component->dapm_widgets) {
1062 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1063 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001064
1065 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001066 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001067 "Failed to create new controls %d\n", ret);
1068 goto err_probe;
1069 }
1070 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001071
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001072 list_for_each_entry(dai, &component->dai_list, list) {
1073 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
Nariman Poushin261edc72014-03-31 15:47:12 +01001074 if (ret != 0) {
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001075 dev_err(component->dev,
Nariman Poushin261edc72014-03-31 15:47:12 +01001076 "Failed to create DAI widgets %d\n", ret);
1077 goto err_probe;
1078 }
1079 }
Mark Brown888df392012-02-16 19:37:51 -08001080
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001081 if (component->probe) {
1082 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001083 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001084 dev_err(component->dev,
1085 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001086 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001087 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001088
1089 WARN(dapm->idle_bias_off &&
1090 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001091 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001092 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001093 }
1094
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001095 if (component->controls)
1096 snd_soc_add_component_controls(component, component->controls,
1097 component->num_controls);
1098 if (component->dapm_routes)
1099 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1100 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001101
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001102 component->probed = 1;
1103 list_add(&dapm->list, &card->dapm_list);
1104
1105 /* This is a HACK and will be removed soon */
1106 if (component->codec)
1107 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001108
Jarkko Nikula70d29332011-01-27 16:24:22 +02001109 return 0;
1110
1111err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001112 soc_cleanup_component_debugfs(component);
1113 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001114
1115 return ret;
1116}
1117
Mark Brown36ae1a92012-01-06 17:12:45 -08001118static void rtd_release(struct device *dev)
1119{
1120 kfree(dev);
1121}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001123static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1124 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001125{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001126 int ret = 0;
1127
Jarkko Nikula589c3562010-12-06 16:27:07 +02001128 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001129 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1130 if (!rtd->dev)
1131 return -ENOMEM;
1132 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001133 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001134 rtd->dev->release = rtd_release;
Lars-Peter Clausenf294afe2014-08-17 11:28:35 +02001135 dev_set_name(rtd->dev, "%s", name);
Mark Brown36ae1a92012-01-06 17:12:45 -08001136 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001137 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001138 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1139 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1140 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1141 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001142 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001143 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001144 /* calling put_device() here to free the rtd->dev */
1145 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001146 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001147 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001148 return ret;
1149 }
1150 rtd->dev_registered = 1;
1151
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001152 if (rtd->codec) {
1153 /* add DAPM sysfs entries for this codec */
1154 ret = snd_soc_dapm_sys_add(rtd->dev);
1155 if (ret < 0)
1156 dev_err(rtd->dev,
1157 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1158 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001159
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001160 /* add codec sysfs entries */
1161 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1162 if (ret < 0)
1163 dev_err(rtd->dev,
1164 "ASoC: failed to add codec sysfs files: %d\n",
1165 ret);
1166 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001167
1168 return 0;
1169}
1170
Stephen Warren62ae68f2012-06-08 12:34:23 -06001171static int soc_probe_link_components(struct snd_soc_card *card, int num,
1172 int order)
1173{
1174 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001175 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001176 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001177 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001178
1179 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001180 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001181 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001182 ret = soc_probe_component(card, component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001183 if (ret < 0)
1184 return ret;
1185 }
1186
Benoit Cousson88bd8702014-07-08 23:19:34 +02001187 /* probe the CODEC-side components */
1188 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001189 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001190 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001191 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001192 if (ret < 0)
1193 return ret;
1194 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001195 }
1196
1197 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001198 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001199 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001200 if (ret < 0)
1201 return ret;
1202 }
1203
1204 return 0;
1205}
1206
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001207static int soc_probe_codec_dai(struct snd_soc_card *card,
1208 struct snd_soc_dai *codec_dai,
1209 int order)
1210{
1211 int ret;
1212
1213 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1214 if (codec_dai->driver->probe) {
1215 ret = codec_dai->driver->probe(codec_dai);
1216 if (ret < 0) {
1217 dev_err(codec_dai->dev,
1218 "ASoC: failed to probe CODEC DAI %s: %d\n",
1219 codec_dai->name, ret);
1220 return ret;
1221 }
1222 }
1223
1224 /* mark codec_dai as probed and add to card dai list */
1225 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001226 }
1227
1228 return 0;
1229}
1230
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001231static int soc_link_dai_widgets(struct snd_soc_card *card,
1232 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001233 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001234{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1236 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001237 struct snd_soc_dapm_widget *play_w, *capture_w;
1238 int ret;
1239
Benoit Cousson88bd8702014-07-08 23:19:34 +02001240 if (rtd->num_codecs > 1)
1241 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1242
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001243 /* link the DAI widgets */
1244 play_w = codec_dai->playback_widget;
1245 capture_w = cpu_dai->capture_widget;
1246 if (play_w && capture_w) {
1247 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1248 capture_w, play_w);
1249 if (ret != 0) {
1250 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1251 play_w->name, capture_w->name, ret);
1252 return ret;
1253 }
1254 }
1255
1256 play_w = cpu_dai->playback_widget;
1257 capture_w = codec_dai->capture_widget;
1258 if (play_w && capture_w) {
1259 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1260 capture_w, play_w);
1261 if (ret != 0) {
1262 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1263 play_w->name, capture_w->name, ret);
1264 return ret;
1265 }
1266 }
1267
1268 return 0;
1269}
1270
Stephen Warren62ae68f2012-06-08 12:34:23 -06001271static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001272{
1273 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1274 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001275 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001276 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001277 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001278
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001279 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001280 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001281
1282 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001284 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001285 for (i = 0; i < rtd->num_codecs; i++)
1286 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001287
1288 /* set default power off timeout */
1289 rtd->pmdown_time = pmdown_time;
1290
1291 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001292 if (!cpu_dai->probed &&
1293 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001294 if (cpu_dai->driver->probe) {
1295 ret = cpu_dai->driver->probe(cpu_dai);
1296 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001297 dev_err(cpu_dai->dev,
1298 "ASoC: failed to probe CPU DAI %s: %d\n",
1299 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001300 return ret;
1301 }
1302 }
1303 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001304 }
1305
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001306 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001307 for (i = 0; i < rtd->num_codecs; i++) {
1308 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1309 if (ret)
1310 return ret;
1311 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001312
Liam Girdwood0168bf02011-06-07 16:08:05 +01001313 /* complete DAI probe during last probe */
1314 if (order != SND_SOC_COMP_ORDER_LAST)
1315 return 0;
1316
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001317 /* do machine specific initialization */
1318 if (dai_link->init) {
1319 ret = dai_link->init(rtd);
1320 if (ret < 0) {
1321 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1322 dai_link->name, ret);
1323 return ret;
1324 }
1325 }
1326
1327 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001329 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001330
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001331#ifdef CONFIG_DEBUG_FS
1332 /* add DPCM sysfs entries */
1333 if (dai_link->dynamic) {
1334 ret = soc_dpcm_debugfs_add(rtd);
1335 if (ret < 0) {
1336 dev_err(rtd->dev,
1337 "ASoC: failed to add dpcm sysfs entries: %d\n",
1338 ret);
1339 return ret;
1340 }
1341 }
1342#endif
1343
Mark Brown36ae1a92012-01-06 17:12:45 -08001344 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001345 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001346 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1347 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348
Namarta Kohli1245b702012-08-16 17:10:41 +05301349 if (cpu_dai->driver->compress_dai) {
1350 /*create compress_device"*/
1351 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001352 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001353 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301354 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001355 return ret;
1356 }
1357 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301358
1359 if (!dai_link->params) {
1360 /* create the pcm */
1361 ret = soc_new_pcm(rtd, num);
1362 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001363 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301364 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001365 return ret;
1366 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301367 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001368 INIT_DELAYED_WORK(&rtd->delayed_work,
1369 codec2codec_close_delayed_work);
1370
Namarta Kohli1245b702012-08-16 17:10:41 +05301371 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001372 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001373 if (ret)
1374 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001375 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001376 }
1377
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 return 0;
1379}
1380
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001381static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001382{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001383 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001384 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001385 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001386
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001387 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1388 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001389 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001390 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001391
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001392 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001393 return -EPROBE_DEFER;
1394 }
1395
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001396 /*
1397 * Some places still reference rtd->codec, so we have to keep that
1398 * initialized if the component is a CODEC. Once all those references
1399 * have been removed, this code can be removed as well.
1400 */
1401 rtd->codec = rtd->component->codec;
1402
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001403 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001404}
1405
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001406static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1407{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001408 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001409 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001410 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001411
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001412 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001413 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001414 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001415
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001416 /* do machine specific initialization */
1417 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001418 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001419 if (ret < 0) {
1420 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1421 aux_dev->name, ret);
1422 return ret;
1423 }
1424 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001425
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001426 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427}
1428
1429static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1430{
1431 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001432 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001433
1434 /* unregister the rtd device */
1435 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001436 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001437 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001438 rtd->dev_registered = 0;
1439 }
1440
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001441 if (component && component->probed)
1442 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001443}
1444
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001445static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001446{
1447 int ret;
1448
1449 if (codec->cache_init)
1450 return 0;
1451
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001452 ret = snd_soc_cache_init(codec);
1453 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001454 dev_err(codec->dev,
1455 "ASoC: Failed to set cache compression type: %d\n",
1456 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001457 return ret;
1458 }
1459 codec->cache_init = 1;
1460 return 0;
1461}
1462
Mark Brownb19e6e72012-03-14 21:18:39 +00001463static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001465 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001466 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001467 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468
Liam Girdwood01b9d992012-03-07 10:38:25 +00001469 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470
Mark Brownb19e6e72012-03-14 21:18:39 +00001471 /* bind DAIs */
1472 for (i = 0; i < card->num_links; i++) {
1473 ret = soc_bind_dai_link(card, i);
1474 if (ret != 0)
1475 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 }
1477
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001478 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001479 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001480 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001481 if (ret != 0)
1482 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483 }
1484
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001485 /* initialize the register cache for each available codec */
1486 list_for_each_entry(codec, &codec_list, list) {
1487 if (codec->cache_init)
1488 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001489 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001490 if (ret < 0)
1491 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001492 }
1493
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001494 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001495 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 card->owner, 0, &card->snd_card);
1497 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001498 dev_err(card->dev,
1499 "ASoC: can't create sound card for card %s: %d\n",
1500 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001501 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503
Mark Browne37a4972011-03-02 18:21:57 +00001504 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1505 card->dapm.dev = card->dev;
1506 card->dapm.card = card;
1507 list_add(&card->dapm.list, &card->dapm_list);
1508
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001509#ifdef CONFIG_DEBUG_FS
1510 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1511#endif
1512
Mark Brown88ee1c62011-02-02 10:43:26 +00001513#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001514 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001515 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001516#endif
Andy Green6ed25972008-06-13 16:24:05 +01001517
Mark Brown9a841eb2011-04-12 17:51:37 -07001518 if (card->dapm_widgets)
1519 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1520 card->num_dapm_widgets);
1521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522 /* initialise the sound card only once */
1523 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001524 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525 if (ret < 0)
1526 goto card_probe_error;
1527 }
1528
Stephen Warren62ae68f2012-06-08 12:34:23 -06001529 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001530 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1531 order++) {
1532 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001533 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001534 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001535 dev_err(card->dev,
1536 "ASoC: failed to instantiate card %d\n",
1537 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001538 goto probe_dai_err;
1539 }
1540 }
1541 }
1542
1543 /* probe all DAI links on this card */
1544 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1545 order++) {
1546 for (i = 0; i < card->num_links; i++) {
1547 ret = soc_probe_link_dais(card, i, order);
1548 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001549 dev_err(card->dev,
1550 "ASoC: failed to instantiate card %d\n",
1551 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001552 goto probe_dai_err;
1553 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554 }
1555 }
1556
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001557 for (i = 0; i < card->num_aux_devs; i++) {
1558 ret = soc_probe_aux_dev(card, i);
1559 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001560 dev_err(card->dev,
1561 "ASoC: failed to add auxiliary devices %d\n",
1562 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001563 goto probe_aux_dev_err;
1564 }
1565 }
1566
Mark Brown888df392012-02-16 19:37:51 -08001567 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001568 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001569
Mark Brownb7af1da2011-04-07 19:18:44 +09001570 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001571 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001572
Mark Brownb8ad29d2011-03-02 18:35:51 +00001573 if (card->dapm_routes)
1574 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1575 card->num_dapm_routes);
1576
Mark Brown75d9ac42011-09-27 16:41:01 +01001577 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001578 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001579 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001580 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001581
Mark Brownf04209a2012-04-09 16:29:21 +01001582 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001583 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1584 int j;
1585
1586 for (j = 0; j < rtd->num_codecs; j++) {
1587 struct snd_soc_dai *codec_dai = codec_dais[j];
1588
1589 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1590 if (ret != 0 && ret != -ENOTSUPP)
1591 dev_warn(codec_dai->dev,
1592 "ASoC: Failed to set DAI format: %d\n",
1593 ret);
1594 }
Mark Brownf04209a2012-04-09 16:29:21 +01001595 }
1596
1597 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001598 if (dai_fmt &&
1599 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001600 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1601 dai_fmt);
1602 if (ret != 0 && ret != -ENOTSUPP)
1603 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001604 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001605 ret);
1606 } else if (dai_fmt) {
1607 /* Flip the polarity for the "CPU" end */
1608 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1609 switch (dai_link->dai_fmt &
1610 SND_SOC_DAIFMT_MASTER_MASK) {
1611 case SND_SOC_DAIFMT_CBM_CFM:
1612 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1613 break;
1614 case SND_SOC_DAIFMT_CBM_CFS:
1615 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1616 break;
1617 case SND_SOC_DAIFMT_CBS_CFM:
1618 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1619 break;
1620 case SND_SOC_DAIFMT_CBS_CFS:
1621 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1622 break;
1623 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001624
1625 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001626 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001627 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001628 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001629 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001630 ret);
1631 }
1632 }
1633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001636 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1637 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001638 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1639 "%s", card->driver_name ? card->driver_name : card->name);
1640 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1641 switch (card->snd_card->driver[i]) {
1642 case '_':
1643 case '-':
1644 case '\0':
1645 break;
1646 default:
1647 if (!isalnum(card->snd_card->driver[i]))
1648 card->snd_card->driver[i] = '_';
1649 break;
1650 }
1651 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652
Mark Brown28e9ad92011-03-02 18:36:34 +00001653 if (card->late_probe) {
1654 ret = card->late_probe(card);
1655 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001656 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001657 card->name, ret);
1658 goto probe_aux_dev_err;
1659 }
1660 }
1661
Stephen Warren16332812011-11-23 12:42:04 -07001662 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001663 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001664
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001665 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001666
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667 ret = snd_card_register(card->snd_card);
1668 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001669 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1670 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001671 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001672 }
1673
Mark Brown435c5e22008-12-04 15:32:53 +00001674 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001675 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001677
1678 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001680probe_aux_dev_err:
1681 for (i = 0; i < card->num_aux_devs; i++)
1682 soc_remove_aux_dev(card, i);
1683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001684probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001685 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001687card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001688 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001689 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001690
1691 snd_card_free(card->snd_card);
1692
Mark Brownb19e6e72012-03-14 21:18:39 +00001693base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695
Mark Brownb19e6e72012-03-14 21:18:39 +00001696 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001697}
1698
1699/* probes a new socdev */
1700static int soc_probe(struct platform_device *pdev)
1701{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001703
Vinod Koul70a7ca32011-01-14 19:22:48 +05301704 /*
1705 * no card, so machine driver should be registering card
1706 * we should not be here in that case so ret error
1707 */
1708 if (!card)
1709 return -EINVAL;
1710
Mark Brownfe4085e2012-03-02 13:07:41 +00001711 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001712 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001713 card->name);
1714
Mark Brown435c5e22008-12-04 15:32:53 +00001715 /* Bodge while we unpick instantiation */
1716 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717
Mark Brown28d528c2012-08-09 18:45:23 +01001718 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001719}
1720
Vinod Koulb0e26482011-01-13 22:48:02 +05301721static int soc_cleanup_card_resources(struct snd_soc_card *card)
1722{
Vinod Koulb0e26482011-01-13 22:48:02 +05301723 int i;
1724
1725 /* make sure any delayed work runs */
1726 for (i = 0; i < card->num_rtd; i++) {
1727 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001728 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301729 }
1730
1731 /* remove auxiliary devices */
1732 for (i = 0; i < card->num_aux_devs; i++)
1733 soc_remove_aux_dev(card, i);
1734
1735 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001736 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301737
1738 soc_cleanup_card_debugfs(card);
1739
1740 /* remove the card */
1741 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001742 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301743
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001744 snd_soc_dapm_free(&card->dapm);
1745
Vinod Koulb0e26482011-01-13 22:48:02 +05301746 snd_card_free(card->snd_card);
1747 return 0;
1748
1749}
1750
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001751/* removes a socdev */
1752static int soc_remove(struct platform_device *pdev)
1753{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001754 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755
Mark Brownc5af3a22008-11-28 13:29:45 +00001756 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001757 return 0;
1758}
1759
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001760int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001761{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001762 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 int i;
Mark Brown51737472009-06-22 13:16:51 +01001764
1765 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001766 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001767
1768 /* Flush out pmdown_time work - we actually do want to run it
1769 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770 for (i = 0; i < card->num_rtd; i++) {
1771 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001772 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 }
Mark Brown51737472009-06-22 13:16:51 +01001774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001776
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001777 /* deactivate pins to sleep state */
1778 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001779 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1780 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1781 int j;
1782
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001783 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001784 for (j = 0; j < rtd->num_codecs; j++) {
1785 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1786 pinctrl_pm_select_sleep_state(codec_dai->dev);
1787 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001788 }
1789
Mark Brown416356f2009-06-30 19:05:15 +01001790 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001791}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001792EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001793
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001794const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301795 .suspend = snd_soc_suspend,
1796 .resume = snd_soc_resume,
1797 .freeze = snd_soc_suspend,
1798 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001799 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301800 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001801};
Stephen Warrendeb26072011-04-05 19:35:30 -06001802EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001803
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001804/* ASoC platform driver */
1805static struct platform_driver soc_driver = {
1806 .driver = {
1807 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001808 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001809 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001810 },
1811 .probe = soc_probe,
1812 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001813};
1814
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001815/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816 * snd_soc_cnew - create new control
1817 * @_template: control template
1818 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001819 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001820 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001821 *
1822 * Create a new mixer control from a template control.
1823 *
1824 * Returns 0 for success, else error.
1825 */
1826struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08001827 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00001828 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829{
1830 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001831 struct snd_kcontrol *kcontrol;
1832 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833
1834 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001835 template.index = 0;
1836
Mark Brownefb7ac32011-03-08 17:23:24 +00001837 if (!long_name)
1838 long_name = template.name;
1839
1840 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02001841 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00001842 if (!name)
1843 return NULL;
1844
Mark Brownefb7ac32011-03-08 17:23:24 +00001845 template.name = name;
1846 } else {
1847 template.name = long_name;
1848 }
1849
1850 kcontrol = snd_ctl_new1(&template, data);
1851
1852 kfree(name);
1853
1854 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001855}
1856EXPORT_SYMBOL_GPL(snd_soc_cnew);
1857
Liam Girdwood022658b2012-02-03 17:43:09 +00001858static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
1859 const struct snd_kcontrol_new *controls, int num_controls,
1860 const char *prefix, void *data)
1861{
1862 int err, i;
1863
1864 for (i = 0; i < num_controls; i++) {
1865 const struct snd_kcontrol_new *control = &controls[i];
1866 err = snd_ctl_add(card, snd_soc_cnew(control, data,
1867 control->name, prefix));
1868 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001869 dev_err(dev, "ASoC: Failed to add %s: %d\n",
1870 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00001871 return err;
1872 }
1873 }
1874
1875 return 0;
1876}
1877
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01001878struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
1879 const char *name)
1880{
1881 struct snd_card *card = soc_card->snd_card;
1882 struct snd_kcontrol *kctl;
1883
1884 if (unlikely(!name))
1885 return NULL;
1886
1887 list_for_each_entry(kctl, &card->controls, list)
1888 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
1889 return kctl;
1890 return NULL;
1891}
1892EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
1893
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001894/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001895 * snd_soc_add_component_controls - Add an array of controls to a component.
1896 *
1897 * @component: Component to add controls to
1898 * @controls: Array of controls to add
1899 * @num_controls: Number of elements in the array
1900 *
1901 * Return: 0 for success, else error.
1902 */
1903int snd_soc_add_component_controls(struct snd_soc_component *component,
1904 const struct snd_kcontrol_new *controls, unsigned int num_controls)
1905{
1906 struct snd_card *card = component->card->snd_card;
1907
1908 return snd_soc_add_controls(card, component->dev, controls,
1909 num_controls, component->name_prefix, component);
1910}
1911EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
1912
1913/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001914 * snd_soc_add_codec_controls - add an array of controls to a codec.
1915 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00001916 * duplicating this code.
1917 *
1918 * @codec: codec to add controls to
1919 * @controls: array of controls to add
1920 * @num_controls: number of elements in the array
1921 *
1922 * Return 0 for success, else error.
1923 */
Liam Girdwood022658b2012-02-03 17:43:09 +00001924int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001925 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00001926{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001927 return snd_soc_add_component_controls(&codec->component, controls,
1928 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001929}
Liam Girdwood022658b2012-02-03 17:43:09 +00001930EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00001931
1932/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001933 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00001934 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001935 *
1936 * @platform: platform to add controls to
1937 * @controls: array of controls to add
1938 * @num_controls: number of elements in the array
1939 *
1940 * Return 0 for success, else error.
1941 */
1942int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001943 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001944{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02001945 return snd_soc_add_component_controls(&platform->component, controls,
1946 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01001947}
1948EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
1949
1950/**
Liam Girdwood022658b2012-02-03 17:43:09 +00001951 * snd_soc_add_card_controls - add an array of controls to a SoC card.
1952 * Convenience function to add a list of controls.
1953 *
1954 * @soc_card: SoC card to add controls to
1955 * @controls: array of controls to add
1956 * @num_controls: number of elements in the array
1957 *
1958 * Return 0 for success, else error.
1959 */
1960int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
1961 const struct snd_kcontrol_new *controls, int num_controls)
1962{
1963 struct snd_card *card = soc_card->snd_card;
1964
1965 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
1966 NULL, soc_card);
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
1969
1970/**
1971 * snd_soc_add_dai_controls - add an array of controls to a DAI.
1972 * Convienience function to add a list of controls.
1973 *
1974 * @dai: DAI to add controls to
1975 * @controls: array of controls to add
1976 * @num_controls: number of elements in the array
1977 *
1978 * Return 0 for success, else error.
1979 */
1980int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
1981 const struct snd_kcontrol_new *controls, int num_controls)
1982{
1983 struct snd_card *card = dai->card->snd_card;
1984
1985 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
1986 NULL, dai);
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
1989
1990/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991 * snd_soc_info_enum_double - enumerated double mixer info callback
1992 * @kcontrol: mixer control
1993 * @uinfo: control element information
1994 *
1995 * Callback to provide information about a double enumerated
1996 * mixer control.
1997 *
1998 * Returns 0 for success.
1999 */
2000int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_info *uinfo)
2002{
2003 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2004
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2006 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002007 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002008
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002009 if (uinfo->value.enumerated.item >= e->items)
2010 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002011 strlcpy(uinfo->value.enumerated.name,
2012 e->texts[uinfo->value.enumerated.item],
2013 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2017
2018/**
2019 * snd_soc_get_enum_double - enumerated double mixer get callback
2020 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002021 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 *
2023 * Callback to get the value of a double enumerated mixer.
2024 *
2025 * Returns 0 for success.
2026 */
2027int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_value *ucontrol)
2029{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002030 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002032 unsigned int val, item;
2033 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002034 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002036 ret = snd_soc_component_read(component, e->reg, &reg_val);
2037 if (ret)
2038 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002039 val = (reg_val >> e->shift_l) & e->mask;
2040 item = snd_soc_enum_val_to_item(e, val);
2041 ucontrol->value.enumerated.item[0] = item;
2042 if (e->shift_l != e->shift_r) {
2043 val = (reg_val >> e->shift_l) & e->mask;
2044 item = snd_soc_enum_val_to_item(e, val);
2045 ucontrol->value.enumerated.item[1] = item;
2046 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002047
2048 return 0;
2049}
2050EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2051
2052/**
2053 * snd_soc_put_enum_double - enumerated double mixer put callback
2054 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002055 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002056 *
2057 * Callback to set the value of a double enumerated mixer.
2058 *
2059 * Returns 0 for success.
2060 */
2061int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2062 struct snd_ctl_elem_value *ucontrol)
2063{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002064 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002065 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002066 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002067 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002068 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002069
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002070 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002071 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002072 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002073 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002075 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002077 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002078 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079 }
2080
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002081 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082}
2083EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2084
2085/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002086 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002087 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002088 * @reg: Register to read
2089 * @mask: Mask to use after shifting the register value
2090 * @shift: Right shift of register value
2091 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002092 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002093 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002094 * This functions reads a codec register. The register value is shifted right
2095 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2096 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002097 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002098 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002099 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002100static int snd_soc_read_signed(struct snd_soc_component *component,
2101 unsigned int reg, unsigned int mask, unsigned int shift,
2102 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002103{
Markus Pargmannf227b882014-01-16 16:02:10 +01002104 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002105 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002106
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002107 ret = snd_soc_component_read(component, reg, &val);
2108 if (ret < 0)
2109 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002110
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002111 val = (val >> shift) & mask;
2112
2113 if (!sign_bit) {
2114 *signed_val = val;
2115 return 0;
2116 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002117
2118 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002119 if (!(val & BIT(sign_bit))) {
2120 *signed_val = val;
2121 return 0;
2122 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002123
2124 ret = val;
2125
2126 /*
2127 * The register most probably does not contain a full-sized int.
2128 * Instead we have an arbitrary number of bits in a signed
2129 * representation which has to be translated into a full-sized int.
2130 * This is done by filling up all bits above the sign-bit.
2131 */
2132 ret |= ~((int)(BIT(sign_bit) - 1));
2133
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002134 *signed_val = ret;
2135
2136 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002137}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002138
2139/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140 * snd_soc_info_volsw - single mixer info callback
2141 * @kcontrol: mixer control
2142 * @uinfo: control element information
2143 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002144 * Callback to provide information about a single mixer control, or a double
2145 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146 *
2147 * Returns 0 for success.
2148 */
2149int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_info *uinfo)
2151{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002152 struct soc_mixer_control *mc =
2153 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002154 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002156 if (!mc->platform_max)
2157 mc->platform_max = mc->max;
2158 platform_max = mc->platform_max;
2159
2160 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002161 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2162 else
2163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2164
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002165 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002167 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 return 0;
2169}
2170EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2171
2172/**
2173 * snd_soc_get_volsw - single mixer get callback
2174 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002175 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002177 * Callback to get the value of a single mixer control, or a double mixer
2178 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179 *
2180 * Returns 0 for success.
2181 */
2182int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
2184{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002185 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002186 struct soc_mixer_control *mc =
2187 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002188 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002189 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002190 unsigned int shift = mc->shift;
2191 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002192 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002193 int min = mc->min;
2194 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002195 unsigned int mask = (1 << fls(max)) - 1;
2196 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002197 int val;
2198 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199
Markus Pargmannf227b882014-01-16 16:02:10 +01002200 if (sign_bit)
2201 mask = BIT(sign_bit + 1) - 1;
2202
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002203 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2204 if (ret)
2205 return ret;
2206
2207 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002208 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002210 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002211
2212 if (snd_soc_volsw_is_stereo(mc)) {
2213 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002214 ret = snd_soc_read_signed(component, reg, mask, rshift,
2215 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002216 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002217 ret = snd_soc_read_signed(component, reg2, mask, shift,
2218 sign_bit, &val);
2219 if (ret)
2220 return ret;
2221
2222 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002223 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002224 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002225 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 }
2227
2228 return 0;
2229}
2230EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2231
2232/**
2233 * snd_soc_put_volsw - single mixer put callback
2234 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002235 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002237 * Callback to set the value of a single mixer control, or a double mixer
2238 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239 *
2240 * Returns 0 for success.
2241 */
2242int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2243 struct snd_ctl_elem_value *ucontrol)
2244{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002245 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002246 struct soc_mixer_control *mc =
2247 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002248 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002249 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002250 unsigned int shift = mc->shift;
2251 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002252 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002253 int min = mc->min;
2254 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002255 unsigned int mask = (1 << fls(max)) - 1;
2256 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002257 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002258 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002259 unsigned int val2 = 0;
2260 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261
Markus Pargmannf227b882014-01-16 16:02:10 +01002262 if (sign_bit)
2263 mask = BIT(sign_bit + 1) - 1;
2264
2265 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002266 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002267 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268 val_mask = mask << shift;
2269 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002270 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002271 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002273 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002274 if (reg == reg2) {
2275 val_mask |= mask << rshift;
2276 val |= val2 << rshift;
2277 } else {
2278 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002279 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002280 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002282 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002283 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284 return err;
2285
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002286 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002287 err = snd_soc_component_update_bits(component, reg2, val_mask,
2288 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002289
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290 return err;
2291}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002292EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293
Mark Browne13ac2e2008-05-28 17:58:05 +01002294/**
Brian Austin1d99f242012-03-30 10:43:55 -05002295 * snd_soc_get_volsw_sx - single mixer get callback
2296 * @kcontrol: mixer control
2297 * @ucontrol: control element information
2298 *
2299 * Callback to get the value of a single mixer control, or a double mixer
2300 * control that spans 2 registers.
2301 *
2302 * Returns 0 for success.
2303 */
2304int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_value *ucontrol)
2306{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002307 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002308 struct soc_mixer_control *mc =
2309 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002310 unsigned int reg = mc->reg;
2311 unsigned int reg2 = mc->rreg;
2312 unsigned int shift = mc->shift;
2313 unsigned int rshift = mc->rshift;
2314 int max = mc->max;
2315 int min = mc->min;
2316 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002317 unsigned int val;
2318 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002319
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002320 ret = snd_soc_component_read(component, reg, &val);
2321 if (ret < 0)
2322 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002323
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002324 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2325
2326 if (snd_soc_volsw_is_stereo(mc)) {
2327 ret = snd_soc_component_read(component, reg2, &val);
2328 if (ret < 0)
2329 return ret;
2330
2331 val = ((val >> rshift) - min) & mask;
2332 ucontrol->value.integer.value[1] = val;
2333 }
Brian Austin1d99f242012-03-30 10:43:55 -05002334
2335 return 0;
2336}
2337EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2338
2339/**
2340 * snd_soc_put_volsw_sx - double mixer set callback
2341 * @kcontrol: mixer control
2342 * @uinfo: control element information
2343 *
2344 * Callback to set the value of a double mixer control that spans 2 registers.
2345 *
2346 * Returns 0 for success.
2347 */
2348int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2350{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002351 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002352 struct soc_mixer_control *mc =
2353 (struct soc_mixer_control *)kcontrol->private_value;
2354
2355 unsigned int reg = mc->reg;
2356 unsigned int reg2 = mc->rreg;
2357 unsigned int shift = mc->shift;
2358 unsigned int rshift = mc->rshift;
2359 int max = mc->max;
2360 int min = mc->min;
2361 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002362 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002363 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002364
2365 val_mask = mask << shift;
2366 val = (ucontrol->value.integer.value[0] + min) & mask;
2367 val = val << shift;
2368
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002369 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302370 if (err < 0)
2371 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002372
2373 if (snd_soc_volsw_is_stereo(mc)) {
2374 val_mask = mask << rshift;
2375 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2376 val2 = val2 << rshift;
2377
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002378 err = snd_soc_component_update_bits(component, reg2, val_mask,
2379 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002380 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002381 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002382}
2383EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2384
2385/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002386 * snd_soc_info_volsw_s8 - signed mixer info callback
2387 * @kcontrol: mixer control
2388 * @uinfo: control element information
2389 *
2390 * Callback to provide information about a signed mixer control.
2391 *
2392 * Returns 0 for success.
2393 */
2394int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_info *uinfo)
2396{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002397 struct soc_mixer_control *mc =
2398 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002399 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002400 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002401
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002402 if (!mc->platform_max)
2403 mc->platform_max = mc->max;
2404 platform_max = mc->platform_max;
2405
Mark Browne13ac2e2008-05-28 17:58:05 +01002406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2407 uinfo->count = 2;
2408 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002409 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002410 return 0;
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2413
2414/**
2415 * snd_soc_get_volsw_s8 - signed mixer get callback
2416 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002417 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002418 *
2419 * Callback to get the value of a signed mixer control.
2420 *
2421 * Returns 0 for success.
2422 */
2423int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2425{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002426 struct soc_mixer_control *mc =
2427 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002428 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002429 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002430 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002431 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002432 int ret;
2433
2434 ret = snd_soc_component_read(component, reg, &val);
2435 if (ret)
2436 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002437
2438 ucontrol->value.integer.value[0] =
2439 ((signed char)(val & 0xff))-min;
2440 ucontrol->value.integer.value[1] =
2441 ((signed char)((val >> 8) & 0xff))-min;
2442 return 0;
2443}
2444EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2445
2446/**
2447 * snd_soc_put_volsw_sgn - signed mixer put callback
2448 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002449 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002450 *
2451 * Callback to set the value of a signed mixer control.
2452 *
2453 * Returns 0 for success.
2454 */
2455int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002458 struct soc_mixer_control *mc =
2459 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002460 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002461 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002462 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002463 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002464
2465 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2466 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2467
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002468 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002469}
2470EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2471
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002472/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002473 * snd_soc_info_volsw_range - single mixer info callback with range.
2474 * @kcontrol: mixer control
2475 * @uinfo: control element information
2476 *
2477 * Callback to provide information, within a range, about a single
2478 * mixer control.
2479 *
2480 * returns 0 for success.
2481 */
2482int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2483 struct snd_ctl_elem_info *uinfo)
2484{
2485 struct soc_mixer_control *mc =
2486 (struct soc_mixer_control *)kcontrol->private_value;
2487 int platform_max;
2488 int min = mc->min;
2489
2490 if (!mc->platform_max)
2491 mc->platform_max = mc->max;
2492 platform_max = mc->platform_max;
2493
2494 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002495 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002496 uinfo->value.integer.min = 0;
2497 uinfo->value.integer.max = platform_max - min;
2498
2499 return 0;
2500}
2501EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2502
2503/**
2504 * snd_soc_put_volsw_range - single mixer put value callback with range.
2505 * @kcontrol: mixer control
2506 * @ucontrol: control element information
2507 *
2508 * Callback to set the value, within a range, for a single mixer control.
2509 *
2510 * Returns 0 for success.
2511 */
2512int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_value *ucontrol)
2514{
2515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002517 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002518 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002519 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002520 unsigned int shift = mc->shift;
2521 int min = mc->min;
2522 int max = mc->max;
2523 unsigned int mask = (1 << fls(max)) - 1;
2524 unsigned int invert = mc->invert;
2525 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002526 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002527
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002528 if (invert)
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002529 val = (max - ucontrol->value.integer.value[0]) & mask;
2530 else
2531 val = ((ucontrol->value.integer.value[0] + min) & mask);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002532 val_mask = mask << shift;
2533 val = val << shift;
2534
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002535 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002536 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002537 return ret;
2538
2539 if (snd_soc_volsw_is_stereo(mc)) {
Mark Brown9bde4f02012-12-19 16:05:00 +00002540 if (invert)
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002541 val = (max - ucontrol->value.integer.value[1]) & mask;
2542 else
2543 val = ((ucontrol->value.integer.value[1] + min) & mask);
Mark Brown9bde4f02012-12-19 16:05:00 +00002544 val_mask = mask << shift;
2545 val = val << shift;
2546
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002547 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2548 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002549 }
2550
2551 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002552}
2553EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2554
2555/**
2556 * snd_soc_get_volsw_range - single mixer get callback with range
2557 * @kcontrol: mixer control
2558 * @ucontrol: control element information
2559 *
2560 * Callback to get the value, within a range, of a single mixer control.
2561 *
2562 * Returns 0 for success.
2563 */
2564int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2565 struct snd_ctl_elem_value *ucontrol)
2566{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002567 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002568 struct soc_mixer_control *mc =
2569 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002570 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002571 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002572 unsigned int shift = mc->shift;
2573 int min = mc->min;
2574 int max = mc->max;
2575 unsigned int mask = (1 << fls(max)) - 1;
2576 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002577 unsigned int val;
2578 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002579
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002580 ret = snd_soc_component_read(component, reg, &val);
2581 if (ret)
2582 return ret;
2583
2584 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002585 if (invert)
2586 ucontrol->value.integer.value[0] =
2587 max - ucontrol->value.integer.value[0];
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002588 else
2589 ucontrol->value.integer.value[0] =
2590 ucontrol->value.integer.value[0] - min;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002591
Mark Brown9bde4f02012-12-19 16:05:00 +00002592 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002593 ret = snd_soc_component_read(component, rreg, &val);
2594 if (ret)
2595 return ret;
2596
2597 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002598 if (invert)
2599 ucontrol->value.integer.value[1] =
2600 max - ucontrol->value.integer.value[1];
Howard Mitchell5c7c3432014-09-19 12:50:31 +01002601 else
2602 ucontrol->value.integer.value[1] =
2603 ucontrol->value.integer.value[1] - min;
Mark Brown9bde4f02012-12-19 16:05:00 +00002604 }
2605
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2609
2610/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002611 * snd_soc_limit_volume - Set new limit to an existing volume control.
2612 *
2613 * @codec: where to look for the control
2614 * @name: Name of the control
2615 * @max: new maximum limit
2616 *
2617 * Return 0 for success, else error.
2618 */
2619int snd_soc_limit_volume(struct snd_soc_codec *codec,
2620 const char *name, int max)
2621{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002622 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002623 struct snd_kcontrol *kctl;
2624 struct soc_mixer_control *mc;
2625 int found = 0;
2626 int ret = -EINVAL;
2627
2628 /* Sanity check for name and max */
2629 if (unlikely(!name || max <= 0))
2630 return -EINVAL;
2631
2632 list_for_each_entry(kctl, &card->controls, list) {
2633 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2634 found = 1;
2635 break;
2636 }
2637 }
2638 if (found) {
2639 mc = (struct soc_mixer_control *)kctl->private_value;
2640 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002641 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002642 ret = 0;
2643 }
2644 }
2645 return ret;
2646}
2647EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2648
Mark Brown71d08512011-10-10 18:31:26 +01002649int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_info *uinfo)
2651{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002652 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002653 struct soc_bytes *params = (void *)kcontrol->private_value;
2654
2655 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002656 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01002657
2658 return 0;
2659}
2660EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2661
2662int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_value *ucontrol)
2664{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002665 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002666 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01002667 int ret;
2668
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002669 if (component->regmap)
2670 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01002671 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002672 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01002673 else
2674 ret = -EINVAL;
2675
Mark Brownf831b052012-02-17 16:20:33 -08002676 /* Hide any masked bytes to ensure consistent data reporting */
2677 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002678 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08002679 case 1:
2680 ucontrol->value.bytes.data[0] &= ~params->mask;
2681 break;
2682 case 2:
2683 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00002684 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08002685 break;
2686 case 4:
2687 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00002688 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08002689 break;
2690 default:
2691 return -EINVAL;
2692 }
2693 }
2694
Mark Brown71d08512011-10-10 18:31:26 +01002695 return ret;
2696}
2697EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2698
2699int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2701{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002702 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01002703 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08002704 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002705 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08002706 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002707
Xiubo Li6596aa02014-09-28 17:29:37 +08002708 if (!component->regmap || !params->num_regs)
Mark Brownf831b052012-02-17 16:20:33 -08002709 return -EINVAL;
2710
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002711 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08002712
Mark Brownb5a8fe42013-01-20 21:42:22 +09002713 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
2714 if (!data)
2715 return -ENOMEM;
2716
Mark Brownf831b052012-02-17 16:20:33 -08002717 /*
2718 * If we've got a mask then we need to preserve the register
2719 * bits. We shouldn't modify the incoming data so take a
2720 * copy.
2721 */
2722 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002723 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08002724 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002725 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08002726
2727 val &= params->mask;
2728
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002729 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08002730 case 1:
2731 ((u8 *)data)[0] &= ~params->mask;
2732 ((u8 *)data)[0] |= val;
2733 break;
2734 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002735 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002736 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002737 &mask, &mask);
2738 if (ret != 0)
2739 goto out;
2740
2741 ((u16 *)data)[0] &= mask;
2742
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002743 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002744 &val, &val);
2745 if (ret != 0)
2746 goto out;
2747
2748 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08002749 break;
2750 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002751 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002752 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002753 &mask, &mask);
2754 if (ret != 0)
2755 goto out;
2756
2757 ((u32 *)data)[0] &= mask;
2758
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002759 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08002760 &val, &val);
2761 if (ret != 0)
2762 goto out;
2763
2764 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08002765 break;
2766 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002767 ret = -EINVAL;
2768 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08002769 }
2770 }
2771
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002772 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08002773 data, len);
2774
Wei Yongjune8b18ad2013-03-12 00:35:14 +08002775out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09002776 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002777
2778 return ret;
2779}
2780EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2781
Vinod Kould9881202014-05-02 10:58:11 +05302782int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_info *ucontrol)
2784{
2785 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2786
2787 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2788 ucontrol->count = params->max;
2789
2790 return 0;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
2793
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05302794int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
2795 unsigned int size, unsigned int __user *tlv)
2796{
2797 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
2798 unsigned int count = size < params->max ? size : params->max;
2799 int ret = -ENXIO;
2800
2801 switch (op_flag) {
2802 case SNDRV_CTL_TLV_OP_READ:
2803 if (params->get)
2804 ret = params->get(tlv, count);
2805 break;
2806 case SNDRV_CTL_TLV_OP_WRITE:
2807 if (params->put)
2808 ret = params->put(tlv, count);
2809 break;
2810 }
2811 return ret;
2812}
2813EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
2814
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002815/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002816 * snd_soc_info_xr_sx - signed multi register info callback
2817 * @kcontrol: mreg control
2818 * @uinfo: control element information
2819 *
2820 * Callback to provide information of a control that can
2821 * span multiple codec registers which together
2822 * forms a single signed value in a MSB/LSB manner.
2823 *
2824 * Returns 0 for success.
2825 */
2826int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
2828{
2829 struct soc_mreg_control *mc =
2830 (struct soc_mreg_control *)kcontrol->private_value;
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2832 uinfo->count = 1;
2833 uinfo->value.integer.min = mc->min;
2834 uinfo->value.integer.max = mc->max;
2835
2836 return 0;
2837}
2838EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2839
2840/**
2841 * snd_soc_get_xr_sx - signed multi register get callback
2842 * @kcontrol: mreg control
2843 * @ucontrol: control element information
2844 *
2845 * Callback to get the value of a control that can span
2846 * multiple codec registers which together forms a single
2847 * signed value in a MSB/LSB manner. The control supports
2848 * specifying total no of bits used to allow for bitfields
2849 * across the multiple codec registers.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002857 struct soc_mreg_control *mc =
2858 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002859 unsigned int regbase = mc->regbase;
2860 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002861 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002862 unsigned int regwmask = (1<<regwshift)-1;
2863 unsigned int invert = mc->invert;
2864 unsigned long mask = (1UL<<mc->nbits)-1;
2865 long min = mc->min;
2866 long max = mc->max;
2867 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002868 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002869 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002870 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002871
2872 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002873 ret = snd_soc_component_read(component, regbase+i, &regval);
2874 if (ret)
2875 return ret;
2876 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002877 }
2878 val &= mask;
2879 if (min < 0 && val > max)
2880 val |= ~mask;
2881 if (invert)
2882 val = max - val;
2883 ucontrol->value.integer.value[0] = val;
2884
2885 return 0;
2886}
2887EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2888
2889/**
2890 * snd_soc_put_xr_sx - signed multi register get callback
2891 * @kcontrol: mreg control
2892 * @ucontrol: control element information
2893 *
2894 * Callback to set the value of a control that can span
2895 * multiple codec registers which together forms a single
2896 * signed value in a MSB/LSB manner. The control supports
2897 * specifying total no of bits used to allow for bitfields
2898 * across the multiple codec registers.
2899 *
2900 * Returns 0 for success.
2901 */
2902int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2904{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002905 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002906 struct soc_mreg_control *mc =
2907 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002908 unsigned int regbase = mc->regbase;
2909 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002910 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002911 unsigned int regwmask = (1<<regwshift)-1;
2912 unsigned int invert = mc->invert;
2913 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002914 long max = mc->max;
2915 long val = ucontrol->value.integer.value[0];
2916 unsigned int i, regval, regmask;
2917 int err;
2918
2919 if (invert)
2920 val = max - val;
2921 val &= mask;
2922 for (i = 0; i < regcount; i++) {
2923 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
2924 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002925 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002926 regmask, regval);
2927 if (err < 0)
2928 return err;
2929 }
2930
2931 return 0;
2932}
2933EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
2934
2935/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002936 * snd_soc_get_strobe - strobe get callback
2937 * @kcontrol: mixer control
2938 * @ucontrol: control element information
2939 *
2940 * Callback get the value of a strobe mixer control.
2941 *
2942 * Returns 0 for success.
2943 */
2944int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_value *ucontrol)
2946{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002947 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002948 struct soc_mixer_control *mc =
2949 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002950 unsigned int reg = mc->reg;
2951 unsigned int shift = mc->shift;
2952 unsigned int mask = 1 << shift;
2953 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002954 unsigned int val;
2955 int ret;
2956
2957 ret = snd_soc_component_read(component, reg, &val);
2958 if (ret)
2959 return ret;
2960
2961 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002962
2963 if (shift != 0 && val != 0)
2964 val = val >> shift;
2965 ucontrol->value.enumerated.item[0] = val ^ invert;
2966
2967 return 0;
2968}
2969EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
2970
2971/**
2972 * snd_soc_put_strobe - strobe put callback
2973 * @kcontrol: mixer control
2974 * @ucontrol: control element information
2975 *
2976 * Callback strobe a register bit to high then low (or the inverse)
2977 * in one pass of a single mixer enum control.
2978 *
2979 * Returns 1 for success.
2980 */
2981int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_value *ucontrol)
2983{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002984 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002985 struct soc_mixer_control *mc =
2986 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002987 unsigned int reg = mc->reg;
2988 unsigned int shift = mc->shift;
2989 unsigned int mask = 1 << shift;
2990 unsigned int invert = mc->invert != 0;
2991 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
2992 unsigned int val1 = (strobe ^ invert) ? mask : 0;
2993 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
2994 int err;
2995
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002996 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02002997 if (err < 0)
2998 return err;
2999
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003000 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003001}
3002EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3003
3004/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003005 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3006 * @dai: DAI
3007 * @clk_id: DAI specific clock ID
3008 * @freq: new clock frequency in Hz
3009 * @dir: new clock direction - input/output.
3010 *
3011 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3012 */
3013int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3014 unsigned int freq, int dir)
3015{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003016 if (dai->driver && dai->driver->ops->set_sysclk)
3017 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003018 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003019 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003020 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003021 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003022 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003023}
3024EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3025
3026/**
Mark Brownec4ee522011-03-07 20:58:11 +00003027 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3028 * @codec: CODEC
3029 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003030 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003031 * @freq: new clock frequency in Hz
3032 * @dir: new clock direction - input/output.
3033 *
3034 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3035 */
3036int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003037 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003038{
3039 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003040 return codec->driver->set_sysclk(codec, clk_id, source,
3041 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003042 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003043 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003044}
3045EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3046
3047/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003048 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3049 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003050 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003051 * @div: new clock divisor.
3052 *
3053 * Configures the clock dividers. This is used to derive the best DAI bit and
3054 * frame clocks from the system or master clock. It's best to set the DAI bit
3055 * and frame clocks as low as possible to save system power.
3056 */
3057int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3058 int div_id, int div)
3059{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003060 if (dai->driver && dai->driver->ops->set_clkdiv)
3061 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003062 else
3063 return -EINVAL;
3064}
3065EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3066
3067/**
3068 * snd_soc_dai_set_pll - configure DAI PLL.
3069 * @dai: DAI
3070 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003071 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003072 * @freq_in: PLL input clock frequency in Hz
3073 * @freq_out: requested PLL output clock frequency in Hz
3074 *
3075 * Configures and enables PLL to generate output clock based on input clock.
3076 */
Mark Brown85488032009-09-05 18:52:16 +01003077int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3078 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003079{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003080 if (dai->driver && dai->driver->ops->set_pll)
3081 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003082 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003083 else if (dai->codec && dai->codec->driver->set_pll)
3084 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3085 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003086 else
3087 return -EINVAL;
3088}
3089EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3090
Mark Brownec4ee522011-03-07 20:58:11 +00003091/*
3092 * snd_soc_codec_set_pll - configure codec PLL.
3093 * @codec: CODEC
3094 * @pll_id: DAI specific PLL ID
3095 * @source: DAI specific source for the PLL
3096 * @freq_in: PLL input clock frequency in Hz
3097 * @freq_out: requested PLL output clock frequency in Hz
3098 *
3099 * Configures and enables PLL to generate output clock based on input clock.
3100 */
3101int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3102 unsigned int freq_in, unsigned int freq_out)
3103{
3104 if (codec->driver->set_pll)
3105 return codec->driver->set_pll(codec, pll_id, source,
3106 freq_in, freq_out);
3107 else
3108 return -EINVAL;
3109}
3110EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3111
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003112/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003113 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3114 * @dai: DAI
3115 * @ratio Ratio of BCLK to Sample rate.
3116 *
3117 * Configures the DAI for a preset BCLK to sample rate ratio.
3118 */
3119int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3120{
3121 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3122 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3123 else
3124 return -EINVAL;
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3127
3128/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003129 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3130 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003131 * @fmt: SND_SOC_DAIFMT_ format value.
3132 *
3133 * Configures the DAI hardware format and clocking.
3134 */
3135int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3136{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003137 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003138 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003139 if (dai->driver->ops->set_fmt == NULL)
3140 return -ENOTSUPP;
3141 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003142}
3143EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3144
3145/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003146 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003147 * @slots: Number of slots in use.
3148 * @tx_mask: bitmask representing active TX slots.
3149 * @rx_mask: bitmask representing active RX slots.
3150 *
3151 * Generates the TDM tx and rx slot default masks for DAI.
3152 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003153static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003154 unsigned int *tx_mask,
3155 unsigned int *rx_mask)
3156{
3157 if (*tx_mask || *rx_mask)
3158 return 0;
3159
3160 if (!slots)
3161 return -EINVAL;
3162
3163 *tx_mask = (1 << slots) - 1;
3164 *rx_mask = (1 << slots) - 1;
3165
3166 return 0;
3167}
3168
3169/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003170 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3171 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003172 * @tx_mask: bitmask representing active TX slots.
3173 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003174 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003175 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003176 *
3177 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3178 * specific.
3179 */
3180int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003181 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003182{
Xiubo Lie5c21512014-03-21 14:17:12 +08003183 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3184 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003185 &tx_mask, &rx_mask);
3186 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003187 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003188
Benoit Cousson88bd8702014-07-08 23:19:34 +02003189 dai->tx_mask = tx_mask;
3190 dai->rx_mask = rx_mask;
3191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003192 if (dai->driver && dai->driver->ops->set_tdm_slot)
3193 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003194 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003195 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003196 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003197}
3198EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3199
3200/**
Barry Song472df3c2009-09-12 01:16:29 +08003201 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3202 * @dai: DAI
3203 * @tx_num: how many TX channels
3204 * @tx_slot: pointer to an array which imply the TX slot number channel
3205 * 0~num-1 uses
3206 * @rx_num: how many RX channels
3207 * @rx_slot: pointer to an array which imply the RX slot number channel
3208 * 0~num-1 uses
3209 *
3210 * configure the relationship between channel number and TDM slot number.
3211 */
3212int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3213 unsigned int tx_num, unsigned int *tx_slot,
3214 unsigned int rx_num, unsigned int *rx_slot)
3215{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003216 if (dai->driver && dai->driver->ops->set_channel_map)
3217 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003218 rx_num, rx_slot);
3219 else
3220 return -EINVAL;
3221}
3222EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3223
3224/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003225 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3226 * @dai: DAI
3227 * @tristate: tristate enable
3228 *
3229 * Tristates the DAI so that others can use it.
3230 */
3231int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3232{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003233 if (dai->driver && dai->driver->ops->set_tristate)
3234 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003235 else
3236 return -EINVAL;
3237}
3238EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3239
3240/**
3241 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3242 * @dai: DAI
3243 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003244 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003245 *
3246 * Mutes the DAI DAC.
3247 */
Mark Brownda183962013-02-06 15:44:07 +00003248int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3249 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003250{
Mark Brownda183962013-02-06 15:44:07 +00003251 if (!dai->driver)
3252 return -ENOTSUPP;
3253
3254 if (dai->driver->ops->mute_stream)
3255 return dai->driver->ops->mute_stream(dai, mute, direction);
3256 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3257 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003259 else
Mark Brown04570c62012-04-13 19:16:03 +01003260 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003261}
3262EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3263
Benoit Cousson88bd8702014-07-08 23:19:34 +02003264static int snd_soc_init_multicodec(struct snd_soc_card *card,
3265 struct snd_soc_dai_link *dai_link)
3266{
3267 /* Legacy codec/codec_dai link is a single entry in multicodec */
3268 if (dai_link->codec_name || dai_link->codec_of_node ||
3269 dai_link->codec_dai_name) {
3270 dai_link->num_codecs = 1;
3271
3272 dai_link->codecs = devm_kzalloc(card->dev,
3273 sizeof(struct snd_soc_dai_link_component),
3274 GFP_KERNEL);
3275 if (!dai_link->codecs)
3276 return -ENOMEM;
3277
3278 dai_link->codecs[0].name = dai_link->codec_name;
3279 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3280 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3281 }
3282
3283 if (!dai_link->codecs) {
3284 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3285 return -EINVAL;
3286 }
3287
3288 return 0;
3289}
3290
Mark Brownc5af3a22008-11-28 13:29:45 +00003291/**
3292 * snd_soc_register_card - Register a card with the ASoC core
3293 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003294 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003295 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003296 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303297int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003298{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003299 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003300
Mark Brownc5af3a22008-11-28 13:29:45 +00003301 if (!card->name || !card->dev)
3302 return -EINVAL;
3303
Stephen Warren5a504962011-12-21 10:40:59 -07003304 for (i = 0; i < card->num_links; i++) {
3305 struct snd_soc_dai_link *link = &card->dai_link[i];
3306
Benoit Cousson88bd8702014-07-08 23:19:34 +02003307 ret = snd_soc_init_multicodec(card, link);
3308 if (ret) {
3309 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3310 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003311 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003312
3313 for (j = 0; j < link->num_codecs; j++) {
3314 /*
3315 * Codec must be specified by 1 of name or OF node,
3316 * not both or neither.
3317 */
3318 if (!!link->codecs[j].name ==
3319 !!link->codecs[j].of_node) {
3320 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3321 link->name);
3322 return -EINVAL;
3323 }
3324 /* Codec DAI name must be specified */
3325 if (!link->codecs[j].dai_name) {
3326 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3327 link->name);
3328 return -EINVAL;
3329 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003330 }
Stephen Warren5a504962011-12-21 10:40:59 -07003331
3332 /*
3333 * Platform may be specified by either name or OF node, but
3334 * can be left unspecified, and a dummy platform will be used.
3335 */
3336 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003337 dev_err(card->dev,
3338 "ASoC: Both platform name/of_node are set for %s\n",
3339 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003340 return -EINVAL;
3341 }
3342
3343 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003344 * CPU device may be specified by either name or OF node, but
3345 * can be left unspecified, and will be matched based on DAI
3346 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003347 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003348 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003349 dev_err(card->dev,
3350 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3351 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003352 return -EINVAL;
3353 }
3354 /*
3355 * At least one of CPU DAI name or CPU device name/node must be
3356 * specified
3357 */
3358 if (!link->cpu_dai_name &&
3359 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003360 dev_err(card->dev,
3361 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3362 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003363 return -EINVAL;
3364 }
3365 }
3366
Mark Browned77cc12011-05-03 18:25:34 +01003367 dev_set_drvdata(card->dev, card);
3368
Stephen Warren111c6412011-01-28 14:26:35 -07003369 snd_soc_initialize_card_lists(card);
3370
Vinod Koul150dd2f2011-01-13 22:48:32 +05303371 soc_init_card_debugfs(card);
3372
Mark Brown181a6892012-03-14 20:18:49 +00003373 card->rtd = devm_kzalloc(card->dev,
3374 sizeof(struct snd_soc_pcm_runtime) *
3375 (card->num_links + card->num_aux_devs),
3376 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003377 if (card->rtd == NULL)
3378 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003379 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003380 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003381
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003382 for (i = 0; i < card->num_links; i++) {
3383 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003384 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003385 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3386 sizeof(struct snd_soc_dai *) *
3387 (card->rtd[i].dai_link->num_codecs),
3388 GFP_KERNEL);
3389 if (card->rtd[i].codec_dais == NULL)
3390 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003391 }
3392
3393 for (i = 0; i < card->num_aux_devs; i++)
3394 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003395
Mark Browndb432b42011-10-03 21:06:40 +01003396 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003397 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003399 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003400
Mark Brownb19e6e72012-03-14 21:18:39 +00003401 ret = snd_soc_instantiate_card(card);
3402 if (ret != 0)
3403 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003404
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003405 /* deactivate pins to sleep state */
3406 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003407 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3408 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3409 int j;
3410
3411 for (j = 0; j < rtd->num_codecs; j++) {
3412 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3413 if (!codec_dai->active)
3414 pinctrl_pm_select_sleep_state(codec_dai->dev);
3415 }
3416
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003417 if (!cpu_dai->active)
3418 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3419 }
3420
Mark Brownb19e6e72012-03-14 21:18:39 +00003421 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003422}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303423EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003424
3425/**
3426 * snd_soc_unregister_card - Unregister a card with the ASoC core
3427 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003428 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003429 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003430 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303431int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003432{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003433 if (card->instantiated) {
3434 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02003435 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05303436 soc_cleanup_card_resources(card);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003437 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003438 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003439
3440 return 0;
3441}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303442EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003443
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003444/*
3445 * Simplify DAI link configuration by removing ".-1" from device names
3446 * and sanitizing names.
3447 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003448static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003449{
3450 char *found, name[NAME_SIZE];
3451 int id1, id2;
3452
3453 if (dev_name(dev) == NULL)
3454 return NULL;
3455
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003456 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003457
3458 /* are we a "%s.%d" name (platform and SPI components) */
3459 found = strstr(name, dev->driver->name);
3460 if (found) {
3461 /* get ID */
3462 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3463
3464 /* discard ID from name if ID == -1 */
3465 if (*id == -1)
3466 found[strlen(dev->driver->name)] = '\0';
3467 }
3468
3469 } else {
3470 /* I2C component devices are named "bus-addr" */
3471 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3472 char tmp[NAME_SIZE];
3473
3474 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003475 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003476
3477 /* sanitize component name for DAI link creation */
3478 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003479 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480 } else
3481 *id = 0;
3482 }
3483
3484 return kstrdup(name, GFP_KERNEL);
3485}
3486
3487/*
3488 * Simplify DAI link naming for single devices with multiple DAIs by removing
3489 * any ".-1" and using the DAI name (instead of device name).
3490 */
3491static inline char *fmt_multiple_name(struct device *dev,
3492 struct snd_soc_dai_driver *dai_drv)
3493{
3494 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003495 dev_err(dev,
3496 "ASoC: error - multiple DAI %s registered with no name\n",
3497 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003498 return NULL;
3499 }
3500
3501 return kstrdup(dai_drv->name, GFP_KERNEL);
3502}
3503
Mark Brown91151712008-11-30 23:31:24 +00003504/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003505 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003506 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003507 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003508 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003509static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003510{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003511 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003512
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003513 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003514 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3515 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003516 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003517 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003518 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003519 }
Mark Brown91151712008-11-30 23:31:24 +00003520}
Mark Brown91151712008-11-30 23:31:24 +00003521
3522/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003523 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003524 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003525 * @component: The component the DAIs are registered for
3526 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003527 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003528 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3529 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003530 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003531static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003532 struct snd_soc_dai_driver *dai_drv, size_t count,
3533 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003534{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003535 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003536 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003537 unsigned int i;
3538 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003539
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003540 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003541
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003542 component->dai_drv = dai_drv;
3543 component->num_dai = count;
3544
Mark Brown91151712008-11-30 23:31:24 +00003545 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003546
3547 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003548 if (dai == NULL) {
3549 ret = -ENOMEM;
3550 goto err;
3551 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003553 /*
3554 * Back in the old days when we still had component-less DAIs,
3555 * instead of having a static name, component-less DAIs would
3556 * inherit the name of the parent device so it is possible to
3557 * register multiple instances of the DAI. We still need to keep
3558 * the same naming style even though those DAIs are not
3559 * component-less anymore.
3560 */
3561 if (count == 1 && legacy_dai_naming) {
3562 dai->name = fmt_single_name(dev, &dai->id);
3563 } else {
3564 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3565 if (dai_drv[i].id)
3566 dai->id = dai_drv[i].id;
3567 else
3568 dai->id = i;
3569 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570 if (dai->name == NULL) {
3571 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003572 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003573 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003574 }
3575
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003576 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003577 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578 dai->driver = &dai_drv[i];
3579 if (!dai->driver->ops)
3580 dai->driver->ops = &null_dai_ops;
3581
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003582 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003583
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003584 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003585 }
3586
3587 return 0;
3588
3589err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003590 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003591
3592 return ret;
3593}
Mark Brown91151712008-11-30 23:31:24 +00003594
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003595static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3596 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003597{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003598 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003599
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003600 component->driver->seq_notifier(component, type, subseq);
3601}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003602
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003603static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3604 int event)
3605{
3606 struct snd_soc_component *component = dapm->component;
3607
3608 return component->driver->stream_event(component, event);
3609}
3610
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003611static int snd_soc_component_initialize(struct snd_soc_component *component,
3612 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003613{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003614 struct snd_soc_dapm_context *dapm;
3615
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003616 component->name = fmt_single_name(dev, &component->id);
3617 if (!component->name) {
3618 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003619 return -ENOMEM;
3620 }
3621
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003622 component->dev = dev;
3623 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003624 component->probe = component->driver->probe;
3625 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003626
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003627 if (!component->dapm_ptr)
3628 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003629
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003630 dapm = component->dapm_ptr;
3631 dapm->dev = dev;
3632 dapm->component = component;
3633 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003634 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003635 if (driver->seq_notifier)
3636 dapm->seq_notifier = snd_soc_component_seq_notifier;
3637 if (driver->stream_event)
3638 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003639
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003640 component->controls = driver->controls;
3641 component->num_controls = driver->num_controls;
3642 component->dapm_widgets = driver->dapm_widgets;
3643 component->num_dapm_widgets = driver->num_dapm_widgets;
3644 component->dapm_routes = driver->dapm_routes;
3645 component->num_dapm_routes = driver->num_dapm_routes;
3646
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003647 INIT_LIST_HEAD(&component->dai_list);
3648 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003649
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003650 return 0;
3651}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003652
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003653static void snd_soc_component_init_regmap(struct snd_soc_component *component)
3654{
3655 if (!component->regmap)
3656 component->regmap = dev_get_regmap(component->dev, NULL);
3657 if (component->regmap) {
3658 int val_bytes = regmap_get_val_bytes(component->regmap);
3659 /* Errors are legitimate for non-integer byte multiples */
3660 if (val_bytes > 0)
3661 component->val_bytes = val_bytes;
3662 }
3663}
3664
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003665static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3666{
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003667 if (!component->write && !component->read)
3668 snd_soc_component_init_regmap(component);
3669
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003670 list_add(&component->list, &component_list);
3671}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003672
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003673static void snd_soc_component_add(struct snd_soc_component *component)
3674{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003675 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003676 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003677 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003678}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003679
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003680static void snd_soc_component_cleanup(struct snd_soc_component *component)
3681{
3682 snd_soc_unregister_dais(component);
3683 kfree(component->name);
3684}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003685
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003686static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3687{
3688 list_del(&component->list);
3689}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003690
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003691static void snd_soc_component_del(struct snd_soc_component *component)
3692{
3693 mutex_lock(&client_mutex);
3694 snd_soc_component_del_unlocked(component);
3695 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003696}
3697
3698int snd_soc_register_component(struct device *dev,
3699 const struct snd_soc_component_driver *cmpnt_drv,
3700 struct snd_soc_dai_driver *dai_drv,
3701 int num_dai)
3702{
3703 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003704 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003705
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003706 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003707 if (!cmpnt) {
3708 dev_err(dev, "ASoC: Failed to allocate memory\n");
3709 return -ENOMEM;
3710 }
3711
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003712 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3713 if (ret)
3714 goto err_free;
3715
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003716 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003717 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003718
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003719 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3720 if (ret < 0) {
3721 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3722 goto err_cleanup;
3723 }
3724
3725 snd_soc_component_add(cmpnt);
3726
3727 return 0;
3728
3729err_cleanup:
3730 snd_soc_component_cleanup(cmpnt);
3731err_free:
3732 kfree(cmpnt);
3733 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003734}
3735EXPORT_SYMBOL_GPL(snd_soc_register_component);
3736
3737/**
3738 * snd_soc_unregister_component - Unregister a component from the ASoC core
3739 *
3740 */
3741void snd_soc_unregister_component(struct device *dev)
3742{
3743 struct snd_soc_component *cmpnt;
3744
3745 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003746 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003747 goto found;
3748 }
3749 return;
3750
3751found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003752 snd_soc_component_del(cmpnt);
3753 snd_soc_component_cleanup(cmpnt);
3754 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003755}
3756EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3757
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003758static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003759{
3760 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3761
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003762 return platform->driver->probe(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003763}
3764
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003765static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003766{
3767 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3768
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003769 platform->driver->remove(platform);
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003770}
3771
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003772/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003773 * snd_soc_add_platform - Add a platform to the ASoC core
3774 * @dev: The parent device for the platform
3775 * @platform: The platform to add
3776 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003777 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003778int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01003779 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003780{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003781 int ret;
3782
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003783 ret = snd_soc_component_initialize(&platform->component,
3784 &platform_drv->component_driver, dev);
3785 if (ret)
3786 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003787
3788 platform->dev = dev;
3789 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003790
3791 if (platform_drv->probe)
3792 platform->component.probe = snd_soc_platform_drv_probe;
3793 if (platform_drv->remove)
3794 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003795
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02003796#ifdef CONFIG_DEBUG_FS
3797 platform->component.debugfs_prefix = "platform";
3798#endif
Mark Brown12a48a8c2008-12-03 19:40:30 +00003799
3800 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003801 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003802 list_add(&platform->list, &platform_list);
3803 mutex_unlock(&client_mutex);
3804
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003805 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3806 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003807
3808 return 0;
3809}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003810EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3811
3812/**
3813 * snd_soc_register_platform - Register a platform with the ASoC core
3814 *
3815 * @platform: platform to register
3816 */
3817int snd_soc_register_platform(struct device *dev,
3818 const struct snd_soc_platform_driver *platform_drv)
3819{
3820 struct snd_soc_platform *platform;
3821 int ret;
3822
3823 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3824
3825 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3826 if (platform == NULL)
3827 return -ENOMEM;
3828
3829 ret = snd_soc_add_platform(dev, platform, platform_drv);
3830 if (ret)
3831 kfree(platform);
3832
3833 return ret;
3834}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003835EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3836
3837/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003838 * snd_soc_remove_platform - Remove a platform from the ASoC core
3839 * @platform: the platform to remove
3840 */
3841void snd_soc_remove_platform(struct snd_soc_platform *platform)
3842{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003843
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003844 mutex_lock(&client_mutex);
3845 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003846 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003847 mutex_unlock(&client_mutex);
3848
3849 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02003850 platform->component.name);
Daniel Mackdecc27b2014-10-07 13:41:23 +02003851
3852 snd_soc_component_cleanup(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003853}
3854EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3855
3856struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3857{
3858 struct snd_soc_platform *platform;
3859
3860 list_for_each_entry(platform, &platform_list, list) {
3861 if (dev == platform->dev)
3862 return platform;
3863 }
3864
3865 return NULL;
3866}
3867EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3868
3869/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003870 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3871 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003872 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003873 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003874void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003875{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876 struct snd_soc_platform *platform;
3877
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003878 platform = snd_soc_lookup_platform(dev);
3879 if (!platform)
3880 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003882 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003884}
3885EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3886
Mark Brown151ab222009-05-09 16:22:58 +01003887static u64 codec_format_map[] = {
3888 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3889 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3890 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3891 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3892 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3893 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3894 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3895 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3896 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3897 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3898 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3899 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3900 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3901 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3902 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3903 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3904};
3905
3906/* Fix up the DAI formats for endianness: codecs don't actually see
3907 * the endianness of the data but we're using the CPU format
3908 * definitions which do need to include endianness so we ensure that
3909 * codec DAIs always have both big and little endian variants set.
3910 */
3911static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3912{
3913 int i;
3914
3915 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3916 if (stream->formats & codec_format_map[i])
3917 stream->formats |= codec_format_map[i];
3918}
3919
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003920static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3921{
3922 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3923
3924 return codec->driver->probe(codec);
3925}
3926
3927static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3928{
3929 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3930
3931 codec->driver->remove(codec);
3932}
3933
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003934static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3935 unsigned int reg, unsigned int val)
3936{
3937 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3938
3939 return codec->driver->write(codec, reg, val);
3940}
3941
3942static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3943 unsigned int reg, unsigned int *val)
3944{
3945 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3946
3947 *val = codec->driver->read(codec, reg);
3948
3949 return 0;
3950}
3951
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02003952static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3953 enum snd_soc_bias_level level)
3954{
3955 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3956
3957 return codec->driver->set_bias_level(codec, level);
3958}
3959
Mark Brown0d0cf002008-12-10 14:32:45 +00003960/**
3961 * snd_soc_register_codec - Register a codec with the ASoC core
3962 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003963 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003964 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003965int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003966 const struct snd_soc_codec_driver *codec_drv,
3967 struct snd_soc_dai_driver *dai_drv,
3968 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003969{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003970 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003971 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003972 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003973
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003974 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003975
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003976 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3977 if (codec == NULL)
3978 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003979
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003980 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003981 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003982
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003983 ret = snd_soc_component_initialize(&codec->component,
3984 &codec_drv->component_driver, dev);
3985 if (ret)
3986 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01003987
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02003988 if (codec_drv->controls) {
3989 codec->component.controls = codec_drv->controls;
3990 codec->component.num_controls = codec_drv->num_controls;
3991 }
3992 if (codec_drv->dapm_widgets) {
3993 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3994 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3995 }
3996 if (codec_drv->dapm_routes) {
3997 codec->component.dapm_routes = codec_drv->dapm_routes;
3998 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3999 }
4000
4001 if (codec_drv->probe)
4002 codec->component.probe = snd_soc_codec_drv_probe;
4003 if (codec_drv->remove)
4004 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004005 if (codec_drv->write)
4006 codec->component.write = snd_soc_codec_drv_write;
4007 if (codec_drv->read)
4008 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004009 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004010 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02004011 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004012 if (codec_drv->seq_notifier)
4013 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004014 if (codec_drv->set_bias_level)
4015 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004016 codec->dev = dev;
4017 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004018 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004019 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004020
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004021#ifdef CONFIG_DEBUG_FS
4022 codec->component.init_debugfs = soc_init_codec_debugfs;
4023 codec->component.debugfs_prefix = "codec";
4024#endif
Xiubo Lia39f75f2014-03-26 13:40:23 +08004025
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004026 if (codec_drv->get_regmap)
4027 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08004028
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004029 for (i = 0; i < num_dai; i++) {
4030 fixup_codec_formats(&dai_drv[i].playback);
4031 fixup_codec_formats(&dai_drv[i].capture);
4032 }
4033
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004034 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4035 if (ret < 0) {
4036 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4037 goto err_cleanup;
4038 }
4039
4040 list_for_each_entry(dai, &codec->component.dai_list, list)
4041 dai->codec = codec;
4042
Mark Brown054880f2012-04-09 17:29:19 +01004043 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004044 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004045 list_add(&codec->list, &codec_list);
4046 mutex_unlock(&client_mutex);
4047
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004048 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4049 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004050 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004051
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004052err_cleanup:
4053 snd_soc_component_cleanup(&codec->component);
4054err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055 kfree(codec);
4056 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004057}
4058EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4059
4060/**
4061 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4062 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004063 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004064 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004066{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004067 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004068
4069 list_for_each_entry(codec, &codec_list, list) {
4070 if (dev == codec->dev)
4071 goto found;
4072 }
4073 return;
4074
4075found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004076
Mark Brown0d0cf002008-12-10 14:32:45 +00004077 mutex_lock(&client_mutex);
4078 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004079 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004080 mutex_unlock(&client_mutex);
4081
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004082 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4083 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004084
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004085 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004086 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004087 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004088}
4089EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4090
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004091/* Retrieve a card's name from device tree */
4092int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4093 const char *propname)
4094{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304095 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004096 int ret;
4097
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304098 if (!card->dev) {
4099 pr_err("card->dev is not set before calling %s\n", __func__);
4100 return -EINVAL;
4101 }
4102
4103 np = card->dev->of_node;
4104
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004105 ret = of_property_read_string_index(np, propname, 0, &card->name);
4106 /*
4107 * EINVAL means the property does not exist. This is fine providing
4108 * card->name was previously set, which is checked later in
4109 * snd_soc_register_card.
4110 */
4111 if (ret < 0 && ret != -EINVAL) {
4112 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004113 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004114 propname, ret);
4115 return ret;
4116 }
4117
4118 return 0;
4119}
4120EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4121
Xiubo Li9a6d4862014-02-08 15:59:52 +08004122static const struct snd_soc_dapm_widget simple_widgets[] = {
4123 SND_SOC_DAPM_MIC("Microphone", NULL),
4124 SND_SOC_DAPM_LINE("Line", NULL),
4125 SND_SOC_DAPM_HP("Headphone", NULL),
4126 SND_SOC_DAPM_SPK("Speaker", NULL),
4127};
4128
4129int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4130 const char *propname)
4131{
4132 struct device_node *np = card->dev->of_node;
4133 struct snd_soc_dapm_widget *widgets;
4134 const char *template, *wname;
4135 int i, j, num_widgets, ret;
4136
4137 num_widgets = of_property_count_strings(np, propname);
4138 if (num_widgets < 0) {
4139 dev_err(card->dev,
4140 "ASoC: Property '%s' does not exist\n", propname);
4141 return -EINVAL;
4142 }
4143 if (num_widgets & 1) {
4144 dev_err(card->dev,
4145 "ASoC: Property '%s' length is not even\n", propname);
4146 return -EINVAL;
4147 }
4148
4149 num_widgets /= 2;
4150 if (!num_widgets) {
4151 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4152 propname);
4153 return -EINVAL;
4154 }
4155
4156 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4157 GFP_KERNEL);
4158 if (!widgets) {
4159 dev_err(card->dev,
4160 "ASoC: Could not allocate memory for widgets\n");
4161 return -ENOMEM;
4162 }
4163
4164 for (i = 0; i < num_widgets; i++) {
4165 ret = of_property_read_string_index(np, propname,
4166 2 * i, &template);
4167 if (ret) {
4168 dev_err(card->dev,
4169 "ASoC: Property '%s' index %d read error:%d\n",
4170 propname, 2 * i, ret);
4171 return -EINVAL;
4172 }
4173
4174 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4175 if (!strncmp(template, simple_widgets[j].name,
4176 strlen(simple_widgets[j].name))) {
4177 widgets[i] = simple_widgets[j];
4178 break;
4179 }
4180 }
4181
4182 if (j >= ARRAY_SIZE(simple_widgets)) {
4183 dev_err(card->dev,
4184 "ASoC: DAPM widget '%s' is not supported\n",
4185 template);
4186 return -EINVAL;
4187 }
4188
4189 ret = of_property_read_string_index(np, propname,
4190 (2 * i) + 1,
4191 &wname);
4192 if (ret) {
4193 dev_err(card->dev,
4194 "ASoC: Property '%s' index %d read error:%d\n",
4195 propname, (2 * i) + 1, ret);
4196 return -EINVAL;
4197 }
4198
4199 widgets[i].name = wname;
4200 }
4201
4202 card->dapm_widgets = widgets;
4203 card->num_dapm_widgets = num_widgets;
4204
4205 return 0;
4206}
4207EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4208
Xiubo Li89c67852014-02-14 09:34:35 +08004209int snd_soc_of_parse_tdm_slot(struct device_node *np,
4210 unsigned int *slots,
4211 unsigned int *slot_width)
4212{
4213 u32 val;
4214 int ret;
4215
4216 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4217 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4218 if (ret)
4219 return ret;
4220
4221 if (slots)
4222 *slots = val;
4223 }
4224
4225 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4226 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4227 if (ret)
4228 return ret;
4229
4230 if (slot_width)
4231 *slot_width = val;
4232 }
4233
4234 return 0;
4235}
4236EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4237
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004238int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4239 const char *propname)
4240{
4241 struct device_node *np = card->dev->of_node;
4242 int num_routes;
4243 struct snd_soc_dapm_route *routes;
4244 int i, ret;
4245
4246 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004247 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004248 dev_err(card->dev,
4249 "ASoC: Property '%s' does not exist or its length is not even\n",
4250 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004251 return -EINVAL;
4252 }
4253 num_routes /= 2;
4254 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004255 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004256 propname);
4257 return -EINVAL;
4258 }
4259
4260 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4261 GFP_KERNEL);
4262 if (!routes) {
4263 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004264 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004265 return -EINVAL;
4266 }
4267
4268 for (i = 0; i < num_routes; i++) {
4269 ret = of_property_read_string_index(np, propname,
4270 2 * i, &routes[i].sink);
4271 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004272 dev_err(card->dev,
4273 "ASoC: Property '%s' index %d could not be read: %d\n",
4274 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004275 return -EINVAL;
4276 }
4277 ret = of_property_read_string_index(np, propname,
4278 (2 * i) + 1, &routes[i].source);
4279 if (ret) {
4280 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004281 "ASoC: Property '%s' index %d could not be read: %d\n",
4282 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004283 return -EINVAL;
4284 }
4285 }
4286
4287 card->num_dapm_routes = num_routes;
4288 card->dapm_routes = routes;
4289
4290 return 0;
4291}
4292EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4293
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004294unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004295 const char *prefix,
4296 struct device_node **bitclkmaster,
4297 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004298{
4299 int ret, i;
4300 char prop[128];
4301 unsigned int format = 0;
4302 int bit, frame;
4303 const char *str;
4304 struct {
4305 char *name;
4306 unsigned int val;
4307 } of_fmt_table[] = {
4308 { "i2s", SND_SOC_DAIFMT_I2S },
4309 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4310 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4311 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4312 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4313 { "ac97", SND_SOC_DAIFMT_AC97 },
4314 { "pdm", SND_SOC_DAIFMT_PDM},
4315 { "msb", SND_SOC_DAIFMT_MSB },
4316 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004317 };
4318
4319 if (!prefix)
4320 prefix = "";
4321
4322 /*
4323 * check "[prefix]format = xxx"
4324 * SND_SOC_DAIFMT_FORMAT_MASK area
4325 */
4326 snprintf(prop, sizeof(prop), "%sformat", prefix);
4327 ret = of_property_read_string(np, prop, &str);
4328 if (ret == 0) {
4329 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4330 if (strcmp(str, of_fmt_table[i].name) == 0) {
4331 format |= of_fmt_table[i].val;
4332 break;
4333 }
4334 }
4335 }
4336
4337 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004338 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004339 * SND_SOC_DAIFMT_CLOCK_MASK area
4340 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004341 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4342 if (of_get_property(np, prop, NULL))
4343 format |= SND_SOC_DAIFMT_CONT;
4344 else
4345 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004346
4347 /*
4348 * check "[prefix]bitclock-inversion"
4349 * check "[prefix]frame-inversion"
4350 * SND_SOC_DAIFMT_INV_MASK area
4351 */
4352 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4353 bit = !!of_get_property(np, prop, NULL);
4354
4355 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4356 frame = !!of_get_property(np, prop, NULL);
4357
4358 switch ((bit << 4) + frame) {
4359 case 0x11:
4360 format |= SND_SOC_DAIFMT_IB_IF;
4361 break;
4362 case 0x10:
4363 format |= SND_SOC_DAIFMT_IB_NF;
4364 break;
4365 case 0x01:
4366 format |= SND_SOC_DAIFMT_NB_IF;
4367 break;
4368 default:
4369 /* SND_SOC_DAIFMT_NB_NF is default */
4370 break;
4371 }
4372
4373 /*
4374 * check "[prefix]bitclock-master"
4375 * check "[prefix]frame-master"
4376 * SND_SOC_DAIFMT_MASTER_MASK area
4377 */
4378 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4379 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004380 if (bit && bitclkmaster)
4381 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004382
4383 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4384 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004385 if (frame && framemaster)
4386 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004387
4388 switch ((bit << 4) + frame) {
4389 case 0x11:
4390 format |= SND_SOC_DAIFMT_CBM_CFM;
4391 break;
4392 case 0x10:
4393 format |= SND_SOC_DAIFMT_CBM_CFS;
4394 break;
4395 case 0x01:
4396 format |= SND_SOC_DAIFMT_CBS_CFM;
4397 break;
4398 default:
4399 format |= SND_SOC_DAIFMT_CBS_CFS;
4400 break;
4401 }
4402
4403 return format;
4404}
4405EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4406
Kuninori Morimotocb470082013-09-10 17:39:56 -07004407int snd_soc_of_get_dai_name(struct device_node *of_node,
4408 const char **dai_name)
4409{
4410 struct snd_soc_component *pos;
4411 struct of_phandle_args args;
4412 int ret;
4413
4414 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4415 "#sound-dai-cells", 0, &args);
4416 if (ret)
4417 return ret;
4418
4419 ret = -EPROBE_DEFER;
4420
4421 mutex_lock(&client_mutex);
4422 list_for_each_entry(pos, &component_list, list) {
4423 if (pos->dev->of_node != args.np)
4424 continue;
4425
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004426 if (pos->driver->of_xlate_dai_name) {
4427 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4428 } else {
4429 int id = -1;
4430
4431 switch (args.args_count) {
4432 case 0:
4433 id = 0; /* same as dai_drv[0] */
4434 break;
4435 case 1:
4436 id = args.args[0];
4437 break;
4438 default:
4439 /* not supported */
4440 break;
4441 }
4442
4443 if (id < 0 || id >= pos->num_dai) {
4444 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004445 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004446 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004447
4448 ret = 0;
4449
4450 *dai_name = pos->dai_drv[id].name;
4451 if (!*dai_name)
4452 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004453 }
4454
Kuninori Morimotocb470082013-09-10 17:39:56 -07004455 break;
4456 }
4457 mutex_unlock(&client_mutex);
4458
4459 of_node_put(args.np);
4460
4461 return ret;
4462}
4463EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4464
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004465static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004466{
Mark Brown384c89e2008-12-03 17:34:03 +00004467#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004468 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4469 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004470 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004471 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004472 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004473
Mark Brown8a9dab12011-01-10 22:25:21 +00004474 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004475 &codec_list_fops))
4476 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4477
Mark Brown8a9dab12011-01-10 22:25:21 +00004478 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004479 &dai_list_fops))
4480 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004481
Mark Brown8a9dab12011-01-10 22:25:21 +00004482 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004483 &platform_list_fops))
4484 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004485#endif
4486
Mark Brownfb257892011-04-28 10:57:54 +01004487 snd_soc_util_init();
4488
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004489 return platform_driver_register(&soc_driver);
4490}
Mark Brown4abe8e12010-10-12 17:41:03 +01004491module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004492
Mark Brown7d8c16a2008-11-30 22:11:24 +00004493static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004494{
Mark Brownfb257892011-04-28 10:57:54 +01004495 snd_soc_util_exit();
4496
Mark Brown384c89e2008-12-03 17:34:03 +00004497#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004498 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004499#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004500 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004501}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004502module_exit(snd_soc_exit);
4503
4504/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004505MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004506MODULE_DESCRIPTION("ALSA SoC Core");
4507MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004508MODULE_ALIAS("platform:soc-audio");