blob: 2fbfbfca48dc4fcdab4e44b559bf6433a7cd0ad9 [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>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000315 &codec->cache_only);
316
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200318 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000323}
324
Mark Brownc3c5a192010-09-15 18:15:14 +0100325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200337 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
Mark Brownf3208782010-09-15 18:19:07 +0100359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100364 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 }
381 }
Mark Brownf3208782010-09-15 18:19:07 +0100382
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
Mark Brown19c7ac22010-09-15 18:22:40 +0100395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200408 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
Mark Brown2b194f9d2010-10-13 10:52:16 +0100417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000432 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200433 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100435 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000444 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#else
453
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200454#define soc_init_codec_debugfs NULL
455
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000458{
459}
460
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463{
464}
465
Axel Linb95fccb2010-11-09 17:06:44 +0800466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200522 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 codec->ac97->dev.release = soc_ac97_device_release;
524
Kay Sieversbb072bf2008-11-02 03:50:35 +0100525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 continue;
576
Benoit Cousson88bd8702014-07-08 23:19:34 +0200577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100586 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100589 continue;
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 /* close any waiting streams and save state */
613 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623
624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 }
635
Mark Browne2d32ff2012-08-31 17:38:32 -0700636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200645 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200655 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000656 break;
657 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100659 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900661 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 }
672 }
673 }
674
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
677
678 if (card->rtd[i].dai_link->ignore_suspend)
679 continue;
680
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 }
687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
691 return 0;
692}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000693EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Andy Green6ed25972008-06-13 16:24:05 +0100695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Andy Green6ed25972008-06-13 16:24:05 +0100705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100710
Mark Brown99497882010-05-07 20:24:05 +0100711 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100722 continue;
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 break;
744 }
Mark Brown1547aba2010-05-07 21:11:40 +0100745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100751 continue;
752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Mark Brown3ff3f642008-05-19 12:32:25 +0200762 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100766 continue;
767
Benoit Cousson88bd8702014-07-08 23:19:34 +0200768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 }
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100782 continue;
783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000793 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000795 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100796
797 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100803}
804
805/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100807{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200810
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800832 }
833
Mark Brown64ab9ba2009-03-31 11:27:03 +0100834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600848 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100850 }
Andy Green6ed25972008-06-13 16:24:05 +0100851
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 return 0;
853}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000854EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858#endif
859
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100860static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800861};
862
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200863static struct snd_soc_component *soc_find_component(
864 const struct device_node *of_node, const char *name)
865{
866 struct snd_soc_component *component;
867
868 list_for_each_entry(component, &component_list, list) {
869 if (of_node) {
870 if (component->dev->of_node == of_node)
871 return component;
872 } else if (strcmp(component->name, name) == 0) {
873 return component;
874 }
875 }
876
877 return NULL;
878}
879
Benoit Cousson88bd8702014-07-08 23:19:34 +0200880static struct snd_soc_codec *soc_find_codec(
881 const struct device_node *codec_of_node,
882 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883{
884 struct snd_soc_codec *codec;
885
886 list_for_each_entry(codec, &codec_list, list) {
887 if (codec_of_node) {
888 if (codec->dev->of_node != codec_of_node)
889 continue;
890 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200891 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892 continue;
893 }
894
895 return codec;
896 }
897
898 return NULL;
899}
900
901static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
902 const char *codec_dai_name)
903{
904 struct snd_soc_dai *codec_dai;
905
906 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
907 if (!strcmp(codec_dai->name, codec_dai_name)) {
908 return codec_dai;
909 }
910 }
911
912 return NULL;
913}
914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100919 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
921 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000922 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100923 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000927 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000928
Mark Brownb19e6e72012-03-14 21:18:39 +0000929 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100930 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600931 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100932 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600933 continue;
934 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100935 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600936 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100937 list_for_each_entry(cpu_dai, &component->dai_list, list) {
938 if (dai_link->cpu_dai_name &&
939 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
940 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700941
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100942 rtd->cpu_dai = cpu_dai;
943 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000945
946 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000947 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000949 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000950 }
951
Benoit Cousson88bd8702014-07-08 23:19:34 +0200952 rtd->num_codecs = dai_link->num_codecs;
953
954 /* Find CODEC from registered CODECs */
955 for (i = 0; i < rtd->num_codecs; i++) {
956 struct snd_soc_codec *codec;
957 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
958 if (!codec) {
959 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
960 codecs[i].name);
961 return -EPROBE_DEFER;
962 }
963
964 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
965 if (!codec_dais[i]) {
966 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
967 codecs[i].dai_name);
968 return -EPROBE_DEFER;
969 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000970 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100971
Benoit Cousson88bd8702014-07-08 23:19:34 +0200972 /* Single codec links expect codec and codec_dai in runtime data */
973 rtd->codec_dai = codec_dais[0];
974 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100975
Mark Brown848dd8b2011-04-27 18:16:32 +0100976 /* if there's no platform we match on the empty platform */
977 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700978 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100979 platform_name = "snd-soc-dummy";
980
Mark Brownb19e6e72012-03-14 21:18:39 +0000981 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700983 if (dai_link->platform_of_node) {
984 if (platform->dev->of_node !=
985 dai_link->platform_of_node)
986 continue;
987 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200988 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700989 continue;
990 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700991
992 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000993 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000994 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000995 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000997 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000999
1000 card->num_rtd++;
1001
1002 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003}
1004
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001005static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001006{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001007 if (!component->probed)
1008 return;
1009
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001010 /* This is a HACK and will be removed soon */
1011 if (component->codec)
1012 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001013
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001014 if (component->remove)
1015 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001016
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001017 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001018
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001019 soc_cleanup_component_debugfs(component);
1020 component->probed = 0;
1021 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001022}
1023
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001024static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1025{
1026 int err;
1027
1028 if (codec_dai && codec_dai->probed &&
1029 codec_dai->driver->remove_order == order) {
1030 if (codec_dai->driver->remove) {
1031 err = codec_dai->driver->remove(codec_dai);
1032 if (err < 0)
1033 dev_err(codec_dai->dev,
1034 "ASoC: failed to remove %s: %d\n",
1035 codec_dai->name, err);
1036 }
1037 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001038 }
1039}
1040
Stephen Warren62ae68f2012-06-08 12:34:23 -06001041static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042{
1043 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001044 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1045 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001046
1047 /* unregister the rtd device */
1048 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001049 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1050 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1051 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001052 rtd->dev_registered = 0;
1053 }
1054
1055 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001056 for (i = 0; i < rtd->num_codecs; i++)
1057 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001060 if (cpu_dai && cpu_dai->probed &&
1061 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 if (cpu_dai->driver->remove) {
1063 err = cpu_dai->driver->remove(cpu_dai);
1064 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001065 dev_err(cpu_dai->dev,
1066 "ASoC: failed to remove %s: %d\n",
1067 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001068 }
1069 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001070 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001071 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072 }
1073}
1074
Stephen Warren62ae68f2012-06-08 12:34:23 -06001075static void soc_remove_link_components(struct snd_soc_card *card, int num,
1076 int order)
1077{
1078 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1079 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001080 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001081 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001082 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001083
1084 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001085 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001086 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001087
1088 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001089 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001090 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001091 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001092 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001093 }
1094
1095 /* remove any CPU-side CODEC */
1096 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001097 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001098 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001099 }
1100}
1101
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001102static void soc_remove_dai_links(struct snd_soc_card *card)
1103{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001104 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001105
Liam Girdwood0168bf02011-06-07 16:08:05 +01001106 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1107 order++) {
1108 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001109 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001110 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001111
1112 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1113 order++) {
1114 for (dai = 0; dai < card->num_rtd; dai++)
1115 soc_remove_link_components(card, dai, order);
1116 }
1117
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001118 card->num_rtd = 0;
1119}
1120
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001121static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001122 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001123{
1124 int i;
1125
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001126 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001127 return;
1128
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001129 for (i = 0; i < card->num_configs; i++) {
1130 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001131 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001132 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001133 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001134 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001135 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001136 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001137 }
1138}
1139
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001140static int soc_probe_component(struct snd_soc_card *card,
1141 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001143 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1144 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001145 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001147
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001148 if (component->probed)
1149 return 0;
1150
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001151 component->card = card;
1152 dapm->card = card;
1153 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001154
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001155 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001156 return -ENODEV;
1157
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001158 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001159
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001160 if (component->dapm_widgets) {
1161 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1162 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001163
1164 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001165 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001166 "Failed to create new controls %d\n", ret);
1167 goto err_probe;
1168 }
1169 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001170
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001171 /*
1172 * This is rather ugly, but certain platforms expect that the DAPM
1173 * widgets for the DAIs for components with the same parent device are
1174 * created in the platforms DAPM context. Until that is fixed we need to
1175 * keep this.
1176 */
1177 if (component->steal_sibling_dai_widgets) {
1178 dai_component = NULL;
1179 list_for_each_entry(component2, &component_list, list) {
1180 if (component == component2)
1181 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001182
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001183 if (component2->dev == component->dev &&
1184 !list_empty(&component2->dai_list)) {
1185 dai_component = component2;
1186 break;
1187 }
1188 }
1189 } else {
1190 dai_component = component;
1191 list_for_each_entry(component2, &component_list, list) {
1192 if (component2->dev == component->dev &&
1193 component2->steal_sibling_dai_widgets) {
1194 dai_component = NULL;
1195 break;
1196 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001197 }
1198 }
Mark Brown888df392012-02-16 19:37:51 -08001199
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001200 if (dai_component) {
1201 list_for_each_entry(dai, &dai_component->dai_list, list) {
1202 snd_soc_dapm_new_dai_widgets(dapm, dai);
1203 if (ret != 0) {
1204 dev_err(component->dev,
1205 "Failed to create DAI widgets %d\n",
1206 ret);
1207 goto err_probe;
1208 }
1209 }
1210 }
Mark Brown33c5f962011-08-22 18:40:30 +01001211
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001212 if (component->probe) {
1213 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001214 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001215 dev_err(component->dev,
1216 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001217 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001218 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001219
1220 WARN(dapm->idle_bias_off &&
1221 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001222 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001223 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001224 }
1225
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001226 if (component->controls)
1227 snd_soc_add_component_controls(component, component->controls,
1228 component->num_controls);
1229 if (component->dapm_routes)
1230 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1231 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001232
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001233 component->probed = 1;
1234 list_add(&dapm->list, &card->dapm_list);
1235
1236 /* This is a HACK and will be removed soon */
1237 if (component->codec)
1238 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001239
Jarkko Nikula70d29332011-01-27 16:24:22 +02001240 return 0;
1241
1242err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001243 soc_cleanup_component_debugfs(component);
1244 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001245
1246 return ret;
1247}
1248
Mark Brown36ae1a92012-01-06 17:12:45 -08001249static void rtd_release(struct device *dev)
1250{
1251 kfree(dev);
1252}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001253
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001254static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1255 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001256{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001257 int ret = 0;
1258
Jarkko Nikula589c3562010-12-06 16:27:07 +02001259 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001260 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1261 if (!rtd->dev)
1262 return -ENOMEM;
1263 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001264 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001265 rtd->dev->release = rtd_release;
1266 rtd->dev->init_name = name;
1267 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001268 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001269 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1270 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1271 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1272 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001273 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001274 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001275 /* calling put_device() here to free the rtd->dev */
1276 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001277 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001278 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279 return ret;
1280 }
1281 rtd->dev_registered = 1;
1282
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001283 if (rtd->codec) {
1284 /* add DAPM sysfs entries for this codec */
1285 ret = snd_soc_dapm_sys_add(rtd->dev);
1286 if (ret < 0)
1287 dev_err(rtd->dev,
1288 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1289 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001291 /* add codec sysfs entries */
1292 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1293 if (ret < 0)
1294 dev_err(rtd->dev,
1295 "ASoC: failed to add codec sysfs files: %d\n",
1296 ret);
1297 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001298
1299 return 0;
1300}
1301
Stephen Warren62ae68f2012-06-08 12:34:23 -06001302static int soc_probe_link_components(struct snd_soc_card *card, int num,
1303 int order)
1304{
1305 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001306 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001307 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001308 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001309
1310 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001311 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001312 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001313 ret = soc_probe_component(card, component);
1314 if (ret < 0)
1315 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001316 }
1317
Benoit Cousson88bd8702014-07-08 23:19:34 +02001318 /* probe the CODEC-side components */
1319 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001320 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001321 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001322 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001323 if (ret < 0)
1324 return ret;
1325 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001326 }
1327
1328 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001329 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001330 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001331 if (ret < 0)
1332 return ret;
1333 }
1334
1335 return 0;
1336}
1337
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001338static int soc_probe_codec_dai(struct snd_soc_card *card,
1339 struct snd_soc_dai *codec_dai,
1340 int order)
1341{
1342 int ret;
1343
1344 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1345 if (codec_dai->driver->probe) {
1346 ret = codec_dai->driver->probe(codec_dai);
1347 if (ret < 0) {
1348 dev_err(codec_dai->dev,
1349 "ASoC: failed to probe CODEC DAI %s: %d\n",
1350 codec_dai->name, ret);
1351 return ret;
1352 }
1353 }
1354
1355 /* mark codec_dai as probed and add to card dai list */
1356 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001357 }
1358
1359 return 0;
1360}
1361
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001362static int soc_link_dai_widgets(struct snd_soc_card *card,
1363 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001364 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001365{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001366 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1367 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001368 struct snd_soc_dapm_widget *play_w, *capture_w;
1369 int ret;
1370
Benoit Cousson88bd8702014-07-08 23:19:34 +02001371 if (rtd->num_codecs > 1)
1372 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1373
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001374 /* link the DAI widgets */
1375 play_w = codec_dai->playback_widget;
1376 capture_w = cpu_dai->capture_widget;
1377 if (play_w && capture_w) {
1378 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1379 capture_w, play_w);
1380 if (ret != 0) {
1381 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1382 play_w->name, capture_w->name, ret);
1383 return ret;
1384 }
1385 }
1386
1387 play_w = cpu_dai->playback_widget;
1388 capture_w = codec_dai->capture_widget;
1389 if (play_w && capture_w) {
1390 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1391 capture_w, play_w);
1392 if (ret != 0) {
1393 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1394 play_w->name, capture_w->name, ret);
1395 return ret;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
Stephen Warren62ae68f2012-06-08 12:34:23 -06001402static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001403{
1404 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1405 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001407 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001408 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001410 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001411 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412
1413 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001414 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001415 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001416 for (i = 0; i < rtd->num_codecs; i++)
1417 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001418
1419 /* set default power off timeout */
1420 rtd->pmdown_time = pmdown_time;
1421
1422 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001423 if (!cpu_dai->probed &&
1424 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001425 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001426 if (!try_module_get(cpu_dai->dev->driver->owner))
1427 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001428 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001429
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001430 if (cpu_dai->driver->probe) {
1431 ret = cpu_dai->driver->probe(cpu_dai);
1432 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001433 dev_err(cpu_dai->dev,
1434 "ASoC: failed to probe CPU DAI %s: %d\n",
1435 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001436 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437 return ret;
1438 }
1439 }
1440 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001441 }
1442
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001444 for (i = 0; i < rtd->num_codecs; i++) {
1445 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1446 if (ret)
1447 return ret;
1448 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Liam Girdwood0168bf02011-06-07 16:08:05 +01001450 /* complete DAI probe during last probe */
1451 if (order != SND_SOC_COMP_ORDER_LAST)
1452 return 0;
1453
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001454 /* do machine specific initialization */
1455 if (dai_link->init) {
1456 ret = dai_link->init(rtd);
1457 if (ret < 0) {
1458 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1459 dai_link->name, ret);
1460 return ret;
1461 }
1462 }
1463
1464 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001465 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001467
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001468#ifdef CONFIG_DEBUG_FS
1469 /* add DPCM sysfs entries */
1470 if (dai_link->dynamic) {
1471 ret = soc_dpcm_debugfs_add(rtd);
1472 if (ret < 0) {
1473 dev_err(rtd->dev,
1474 "ASoC: failed to add dpcm sysfs entries: %d\n",
1475 ret);
1476 return ret;
1477 }
1478 }
1479#endif
1480
Mark Brown36ae1a92012-01-06 17:12:45 -08001481 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001483 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1484 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
Namarta Kohli1245b702012-08-16 17:10:41 +05301486 if (cpu_dai->driver->compress_dai) {
1487 /*create compress_device"*/
1488 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001489 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001490 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301491 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001492 return ret;
1493 }
1494 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301495
1496 if (!dai_link->params) {
1497 /* create the pcm */
1498 ret = soc_new_pcm(rtd, num);
1499 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001500 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301501 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001502 return ret;
1503 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301504 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001505 INIT_DELAYED_WORK(&rtd->delayed_work,
1506 codec2codec_close_delayed_work);
1507
Namarta Kohli1245b702012-08-16 17:10:41 +05301508 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001509 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001510 if (ret)
1511 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001512 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001513 }
1514
1515 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001516 for (i = 0; i < rtd->num_codecs; i++) {
1517 if (rtd->codec_dais[i]->driver->ac97_control)
1518 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1519 rtd->cpu_dai->ac97_pdata);
1520 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001521
1522 return 0;
1523}
1524
1525#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001526static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1527 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001528{
1529 int ret;
1530
1531 /* Only instantiate AC97 if not already done by the adaptor
1532 * for the generic AC97 subsystem.
1533 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001534 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001535 /*
1536 * It is possible that the AC97 device is already registered to
1537 * the device subsystem. This happens when the device is created
1538 * via snd_ac97_mixer(). Currently only SoC codec that does so
1539 * is the generic AC97 glue but others migh emerge.
1540 *
1541 * In those cases we don't try to register the device again.
1542 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001543 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001544 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001546 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001548 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001549 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001550 return ret;
1551 }
1552
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001553 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554 }
1555 return 0;
1556}
1557
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001558static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001559{
1560 if (codec->ac97_registered) {
1561 soc_ac97_dev_unregister(codec);
1562 codec->ac97_registered = 0;
1563 }
1564}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001565
Benoit Cousson88bd8702014-07-08 23:19:34 +02001566static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1567{
1568 int i, ret;
1569
1570 for (i = 0; i < rtd->num_codecs; i++) {
1571 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1572
1573 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1574 if (ret) {
1575 while (--i >= 0)
1576 soc_unregister_ac97_codec(codec_dai->codec);
1577 return ret;
1578 }
1579 }
1580
1581 return 0;
1582}
1583
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001584static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1585{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001586 int i;
1587
1588 for (i = 0; i < rtd->num_codecs; i++)
1589 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001590}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591#endif
1592
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001593static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001594{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001595 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001596 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001597 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001598
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001599 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1600 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001601 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001602 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001603
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001604 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001605 return -EPROBE_DEFER;
1606 }
1607
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001608 /*
1609 * Some places still reference rtd->codec, so we have to keep that
1610 * initialized if the component is a CODEC. Once all those references
1611 * have been removed, this code can be removed as well.
1612 */
1613 rtd->codec = rtd->component->codec;
1614
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001615 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001616}
1617
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001618static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1619{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001620 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001621 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001622 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001623
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001624 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001625 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001626 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001627
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001628 /* do machine specific initialization */
1629 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001630 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001631 if (ret < 0) {
1632 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1633 aux_dev->name, ret);
1634 return ret;
1635 }
1636 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001637
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001638 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001639}
1640
1641static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1642{
1643 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001644 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001645
1646 /* unregister the rtd device */
1647 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001648 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001649 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001650 rtd->dev_registered = 0;
1651 }
1652
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001653 if (component && component->probed)
1654 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001655}
1656
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001657static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001658{
1659 int ret;
1660
1661 if (codec->cache_init)
1662 return 0;
1663
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001664 ret = snd_soc_cache_init(codec);
1665 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001666 dev_err(codec->dev,
1667 "ASoC: Failed to set cache compression type: %d\n",
1668 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001669 return ret;
1670 }
1671 codec->cache_init = 1;
1672 return 0;
1673}
1674
Mark Brownb19e6e72012-03-14 21:18:39 +00001675static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001677 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001678 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001679 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001680
Liam Girdwood01b9d992012-03-07 10:38:25 +00001681 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001682
Mark Brownb19e6e72012-03-14 21:18:39 +00001683 /* bind DAIs */
1684 for (i = 0; i < card->num_links; i++) {
1685 ret = soc_bind_dai_link(card, i);
1686 if (ret != 0)
1687 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 }
1689
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001690 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001691 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001692 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001693 if (ret != 0)
1694 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001695 }
1696
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001697 /* initialize the register cache for each available codec */
1698 list_for_each_entry(codec, &codec_list, list) {
1699 if (codec->cache_init)
1700 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001701 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001702 if (ret < 0)
1703 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001704 }
1705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001707 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001708 card->owner, 0, &card->snd_card);
1709 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001710 dev_err(card->dev,
1711 "ASoC: can't create sound card for card %s: %d\n",
1712 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001713 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715
Mark Browne37a4972011-03-02 18:21:57 +00001716 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1717 card->dapm.dev = card->dev;
1718 card->dapm.card = card;
1719 list_add(&card->dapm.list, &card->dapm_list);
1720
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001721#ifdef CONFIG_DEBUG_FS
1722 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1723#endif
1724
Mark Brown88ee1c62011-02-02 10:43:26 +00001725#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001726 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001727 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001728#endif
Andy Green6ed25972008-06-13 16:24:05 +01001729
Mark Brown9a841eb2011-04-12 17:51:37 -07001730 if (card->dapm_widgets)
1731 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1732 card->num_dapm_widgets);
1733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001734 /* initialise the sound card only once */
1735 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001736 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737 if (ret < 0)
1738 goto card_probe_error;
1739 }
1740
Stephen Warren62ae68f2012-06-08 12:34:23 -06001741 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001742 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1743 order++) {
1744 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001745 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001746 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001747 dev_err(card->dev,
1748 "ASoC: failed to instantiate card %d\n",
1749 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001750 goto probe_dai_err;
1751 }
1752 }
1753 }
1754
1755 /* probe all DAI links on this card */
1756 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1757 order++) {
1758 for (i = 0; i < card->num_links; i++) {
1759 ret = soc_probe_link_dais(card, i, order);
1760 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001761 dev_err(card->dev,
1762 "ASoC: failed to instantiate card %d\n",
1763 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001764 goto probe_dai_err;
1765 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 }
1767 }
1768
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001769 for (i = 0; i < card->num_aux_devs; i++) {
1770 ret = soc_probe_aux_dev(card, i);
1771 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001772 dev_err(card->dev,
1773 "ASoC: failed to add auxiliary devices %d\n",
1774 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001775 goto probe_aux_dev_err;
1776 }
1777 }
1778
Mark Brown888df392012-02-16 19:37:51 -08001779 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001780 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001781
Mark Brownb7af1da2011-04-07 19:18:44 +09001782 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001783 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001784
Mark Brownb8ad29d2011-03-02 18:35:51 +00001785 if (card->dapm_routes)
1786 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1787 card->num_dapm_routes);
1788
Mark Brown75d9ac42011-09-27 16:41:01 +01001789 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001790 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001791 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001792 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001793
Mark Brownf04209a2012-04-09 16:29:21 +01001794 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001795 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1796 int j;
1797
1798 for (j = 0; j < rtd->num_codecs; j++) {
1799 struct snd_soc_dai *codec_dai = codec_dais[j];
1800
1801 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1802 if (ret != 0 && ret != -ENOTSUPP)
1803 dev_warn(codec_dai->dev,
1804 "ASoC: Failed to set DAI format: %d\n",
1805 ret);
1806 }
Mark Brownf04209a2012-04-09 16:29:21 +01001807 }
1808
1809 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001810 if (dai_fmt &&
1811 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001812 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1813 dai_fmt);
1814 if (ret != 0 && ret != -ENOTSUPP)
1815 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001816 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001817 ret);
1818 } else if (dai_fmt) {
1819 /* Flip the polarity for the "CPU" end */
1820 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1821 switch (dai_link->dai_fmt &
1822 SND_SOC_DAIFMT_MASTER_MASK) {
1823 case SND_SOC_DAIFMT_CBM_CFM:
1824 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1825 break;
1826 case SND_SOC_DAIFMT_CBM_CFS:
1827 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1828 break;
1829 case SND_SOC_DAIFMT_CBS_CFM:
1830 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1831 break;
1832 case SND_SOC_DAIFMT_CBS_CFS:
1833 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1834 break;
1835 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001836
1837 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001838 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001839 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001840 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001841 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001842 ret);
1843 }
1844 }
1845
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001847 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001848 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1849 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001850 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1851 "%s", card->driver_name ? card->driver_name : card->name);
1852 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1853 switch (card->snd_card->driver[i]) {
1854 case '_':
1855 case '-':
1856 case '\0':
1857 break;
1858 default:
1859 if (!isalnum(card->snd_card->driver[i]))
1860 card->snd_card->driver[i] = '_';
1861 break;
1862 }
1863 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001864
Mark Brown28e9ad92011-03-02 18:36:34 +00001865 if (card->late_probe) {
1866 ret = card->late_probe(card);
1867 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001868 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001869 card->name, ret);
1870 goto probe_aux_dev_err;
1871 }
1872 }
1873
Stephen Warren16332812011-11-23 12:42:04 -07001874 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001875 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001876
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001877 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001879 ret = snd_card_register(card->snd_card);
1880 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001881 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1882 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001883 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001884 }
1885
1886#ifdef CONFIG_SND_SOC_AC97_BUS
1887 /* register any AC97 codecs */
1888 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001889 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1890 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001891 dev_err(card->dev,
1892 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001893 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001894 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001895 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001896 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001897 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001898#endif
1899
Mark Brown435c5e22008-12-04 15:32:53 +00001900 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001901 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001902 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001903
1904 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001906probe_aux_dev_err:
1907 for (i = 0; i < card->num_aux_devs; i++)
1908 soc_remove_aux_dev(card, i);
1909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001911 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001912
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001914 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001915 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001916
1917 snd_card_free(card->snd_card);
1918
Mark Brownb19e6e72012-03-14 21:18:39 +00001919base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001920 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001921
Mark Brownb19e6e72012-03-14 21:18:39 +00001922 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001923}
1924
1925/* probes a new socdev */
1926static int soc_probe(struct platform_device *pdev)
1927{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001928 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001929
Vinod Koul70a7ca32011-01-14 19:22:48 +05301930 /*
1931 * no card, so machine driver should be registering card
1932 * we should not be here in that case so ret error
1933 */
1934 if (!card)
1935 return -EINVAL;
1936
Mark Brownfe4085e2012-03-02 13:07:41 +00001937 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001938 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001939 card->name);
1940
Mark Brown435c5e22008-12-04 15:32:53 +00001941 /* Bodge while we unpick instantiation */
1942 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001943
Mark Brown28d528c2012-08-09 18:45:23 +01001944 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945}
1946
Vinod Koulb0e26482011-01-13 22:48:02 +05301947static int soc_cleanup_card_resources(struct snd_soc_card *card)
1948{
Vinod Koulb0e26482011-01-13 22:48:02 +05301949 int i;
1950
1951 /* make sure any delayed work runs */
1952 for (i = 0; i < card->num_rtd; i++) {
1953 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001954 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301955 }
1956
1957 /* remove auxiliary devices */
1958 for (i = 0; i < card->num_aux_devs; i++)
1959 soc_remove_aux_dev(card, i);
1960
1961 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001962 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301963
1964 soc_cleanup_card_debugfs(card);
1965
1966 /* remove the card */
1967 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001968 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301969
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001970 snd_soc_dapm_free(&card->dapm);
1971
Vinod Koulb0e26482011-01-13 22:48:02 +05301972 snd_card_free(card->snd_card);
1973 return 0;
1974
1975}
1976
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001977/* removes a socdev */
1978static int soc_remove(struct platform_device *pdev)
1979{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001980 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981
Mark Brownc5af3a22008-11-28 13:29:45 +00001982 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 return 0;
1984}
1985
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001986int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001987{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001988 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001989 int i;
Mark Brown51737472009-06-22 13:16:51 +01001990
1991 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001992 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001993
1994 /* Flush out pmdown_time work - we actually do want to run it
1995 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996 for (i = 0; i < card->num_rtd; i++) {
1997 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001998 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999 }
Mark Brown51737472009-06-22 13:16:51 +01002000
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002001 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002002
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002003 /* deactivate pins to sleep state */
2004 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002005 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2006 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2007 int j;
2008
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002009 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002010 for (j = 0; j < rtd->num_codecs; j++) {
2011 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2012 pinctrl_pm_select_sleep_state(codec_dai->dev);
2013 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002014 }
2015
Mark Brown416356f2009-06-30 19:05:15 +01002016 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002017}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002018EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002019
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002020const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302021 .suspend = snd_soc_suspend,
2022 .resume = snd_soc_resume,
2023 .freeze = snd_soc_suspend,
2024 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002025 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302026 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002027};
Stephen Warrendeb26072011-04-05 19:35:30 -06002028EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002029
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002030/* ASoC platform driver */
2031static struct platform_driver soc_driver = {
2032 .driver = {
2033 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002034 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002035 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 },
2037 .probe = soc_probe,
2038 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039};
2040
Mark Brown096e49d2009-07-05 15:12:22 +01002041/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002042 * snd_soc_new_ac97_codec - initailise AC97 device
2043 * @codec: audio codec
2044 * @ops: AC97 bus operations
2045 * @num: AC97 codec number
2046 *
2047 * Initialises AC97 codec resources for use by ad-hoc devices only.
2048 */
2049int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2050 struct snd_ac97_bus_ops *ops, int num)
2051{
2052 mutex_lock(&codec->mutex);
2053
2054 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2055 if (codec->ac97 == NULL) {
2056 mutex_unlock(&codec->mutex);
2057 return -ENOMEM;
2058 }
2059
2060 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2061 if (codec->ac97->bus == NULL) {
2062 kfree(codec->ac97);
2063 codec->ac97 = NULL;
2064 mutex_unlock(&codec->mutex);
2065 return -ENOMEM;
2066 }
2067
2068 codec->ac97->bus->ops = ops;
2069 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002070
2071 /*
2072 * Mark the AC97 device to be created by us. This way we ensure that the
2073 * device will be registered with the device subsystem later on.
2074 */
2075 codec->ac97_created = 1;
2076
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002077 mutex_unlock(&codec->mutex);
2078 return 0;
2079}
2080EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2081
Markus Pargmann741a5092013-08-19 17:05:55 +02002082static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2083
2084static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2085{
2086 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2087
2088 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2089
2090 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2091
2092 udelay(10);
2093
2094 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2095
2096 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2097 msleep(2);
2098}
2099
2100static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2101{
2102 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2103
2104 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2105
2106 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2107 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2108 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2109
2110 udelay(10);
2111
2112 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2113
2114 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2115 msleep(2);
2116}
2117
2118static int snd_soc_ac97_parse_pinctl(struct device *dev,
2119 struct snd_ac97_reset_cfg *cfg)
2120{
2121 struct pinctrl *p;
2122 struct pinctrl_state *state;
2123 int gpio;
2124 int ret;
2125
2126 p = devm_pinctrl_get(dev);
2127 if (IS_ERR(p)) {
2128 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002129 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002130 }
2131 cfg->pctl = p;
2132
2133 state = pinctrl_lookup_state(p, "ac97-reset");
2134 if (IS_ERR(state)) {
2135 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002136 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002137 }
2138 cfg->pstate_reset = state;
2139
2140 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2141 if (IS_ERR(state)) {
2142 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002143 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002144 }
2145 cfg->pstate_warm_reset = state;
2146
2147 state = pinctrl_lookup_state(p, "ac97-running");
2148 if (IS_ERR(state)) {
2149 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002150 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002151 }
2152 cfg->pstate_run = state;
2153
2154 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2155 if (gpio < 0) {
2156 dev_err(dev, "Can't find ac97-sync gpio\n");
2157 return gpio;
2158 }
2159 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2160 if (ret) {
2161 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2162 return ret;
2163 }
2164 cfg->gpio_sync = gpio;
2165
2166 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2167 if (gpio < 0) {
2168 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2169 return gpio;
2170 }
2171 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2172 if (ret) {
2173 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2174 return ret;
2175 }
2176 cfg->gpio_sdata = gpio;
2177
2178 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2179 if (gpio < 0) {
2180 dev_err(dev, "Can't find ac97-reset gpio\n");
2181 return gpio;
2182 }
2183 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2184 if (ret) {
2185 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2186 return ret;
2187 }
2188 cfg->gpio_reset = gpio;
2189
2190 return 0;
2191}
2192
Mark Brownb047e1c2013-06-26 12:45:59 +01002193struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002194EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002195
2196int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2197{
2198 if (ops == soc_ac97_ops)
2199 return 0;
2200
2201 if (soc_ac97_ops && ops)
2202 return -EBUSY;
2203
2204 soc_ac97_ops = ops;
2205
2206 return 0;
2207}
2208EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2209
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002210/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002211 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2212 *
2213 * This function sets the reset and warm_reset properties of ops and parses
2214 * the device node of pdev to get pinctrl states and gpio numbers to use.
2215 */
2216int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2217 struct platform_device *pdev)
2218{
2219 struct device *dev = &pdev->dev;
2220 struct snd_ac97_reset_cfg cfg;
2221 int ret;
2222
2223 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2224 if (ret)
2225 return ret;
2226
2227 ret = snd_soc_set_ac97_ops(ops);
2228 if (ret)
2229 return ret;
2230
2231 ops->warm_reset = snd_soc_ac97_warm_reset;
2232 ops->reset = snd_soc_ac97_reset;
2233
2234 snd_ac97_rst_cfg = cfg;
2235 return 0;
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2238
2239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_free_ac97_codec - free AC97 codec device
2241 * @codec: audio codec
2242 *
2243 * Frees AC97 codec device resources.
2244 */
2245void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2246{
2247 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002248#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002249 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002250#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251 kfree(codec->ac97->bus);
2252 kfree(codec->ac97);
2253 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002254 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255 mutex_unlock(&codec->mutex);
2256}
2257EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2258
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260 * snd_soc_cnew - create new control
2261 * @_template: control template
2262 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002263 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002264 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265 *
2266 * Create a new mixer control from a template control.
2267 *
2268 * Returns 0 for success, else error.
2269 */
2270struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002271 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002272 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273{
2274 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002275 struct snd_kcontrol *kcontrol;
2276 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002277
2278 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279 template.index = 0;
2280
Mark Brownefb7ac32011-03-08 17:23:24 +00002281 if (!long_name)
2282 long_name = template.name;
2283
2284 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002285 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002286 if (!name)
2287 return NULL;
2288
Mark Brownefb7ac32011-03-08 17:23:24 +00002289 template.name = name;
2290 } else {
2291 template.name = long_name;
2292 }
2293
2294 kcontrol = snd_ctl_new1(&template, data);
2295
2296 kfree(name);
2297
2298 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299}
2300EXPORT_SYMBOL_GPL(snd_soc_cnew);
2301
Liam Girdwood022658b2012-02-03 17:43:09 +00002302static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2303 const struct snd_kcontrol_new *controls, int num_controls,
2304 const char *prefix, void *data)
2305{
2306 int err, i;
2307
2308 for (i = 0; i < num_controls; i++) {
2309 const struct snd_kcontrol_new *control = &controls[i];
2310 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2311 control->name, prefix));
2312 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002313 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2314 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002315 return err;
2316 }
2317 }
2318
2319 return 0;
2320}
2321
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002322struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2323 const char *name)
2324{
2325 struct snd_card *card = soc_card->snd_card;
2326 struct snd_kcontrol *kctl;
2327
2328 if (unlikely(!name))
2329 return NULL;
2330
2331 list_for_each_entry(kctl, &card->controls, list)
2332 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2333 return kctl;
2334 return NULL;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2337
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002339 * snd_soc_add_component_controls - Add an array of controls to a component.
2340 *
2341 * @component: Component to add controls to
2342 * @controls: Array of controls to add
2343 * @num_controls: Number of elements in the array
2344 *
2345 * Return: 0 for success, else error.
2346 */
2347int snd_soc_add_component_controls(struct snd_soc_component *component,
2348 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2349{
2350 struct snd_card *card = component->card->snd_card;
2351
2352 return snd_soc_add_controls(card, component->dev, controls,
2353 num_controls, component->name_prefix, component);
2354}
2355EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2356
2357/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002358 * snd_soc_add_codec_controls - add an array of controls to a codec.
2359 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002360 * duplicating this code.
2361 *
2362 * @codec: codec to add controls to
2363 * @controls: array of controls to add
2364 * @num_controls: number of elements in the array
2365 *
2366 * Return 0 for success, else error.
2367 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002368int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002369 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002370{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002371 return snd_soc_add_component_controls(&codec->component, controls,
2372 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002373}
Liam Girdwood022658b2012-02-03 17:43:09 +00002374EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002375
2376/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002377 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002378 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002379 *
2380 * @platform: platform to add controls to
2381 * @controls: array of controls to add
2382 * @num_controls: number of elements in the array
2383 *
2384 * Return 0 for success, else error.
2385 */
2386int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002387 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002388{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002389 return snd_soc_add_component_controls(&platform->component, controls,
2390 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002391}
2392EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2393
2394/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002395 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2396 * Convenience function to add a list of controls.
2397 *
2398 * @soc_card: SoC card to add controls to
2399 * @controls: array of controls to add
2400 * @num_controls: number of elements in the array
2401 *
2402 * Return 0 for success, else error.
2403 */
2404int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2405 const struct snd_kcontrol_new *controls, int num_controls)
2406{
2407 struct snd_card *card = soc_card->snd_card;
2408
2409 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2410 NULL, soc_card);
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2413
2414/**
2415 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2416 * Convienience function to add a list of controls.
2417 *
2418 * @dai: DAI to add controls to
2419 * @controls: array of controls to add
2420 * @num_controls: number of elements in the array
2421 *
2422 * Return 0 for success, else error.
2423 */
2424int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2425 const struct snd_kcontrol_new *controls, int num_controls)
2426{
2427 struct snd_card *card = dai->card->snd_card;
2428
2429 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2430 NULL, dai);
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2433
2434/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 * snd_soc_info_enum_double - enumerated double mixer info callback
2436 * @kcontrol: mixer control
2437 * @uinfo: control element information
2438 *
2439 * Callback to provide information about a double enumerated
2440 * mixer control.
2441 *
2442 * Returns 0 for success.
2443 */
2444int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_info *uinfo)
2446{
2447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2448
2449 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2450 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002451 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002453 if (uinfo->value.enumerated.item >= e->items)
2454 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002455 strlcpy(uinfo->value.enumerated.name,
2456 e->texts[uinfo->value.enumerated.item],
2457 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458 return 0;
2459}
2460EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2461
2462/**
2463 * snd_soc_get_enum_double - enumerated double mixer get callback
2464 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002465 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 *
2467 * Callback to get the value of a double enumerated mixer.
2468 *
2469 * Returns 0 for success.
2470 */
2471int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_value *ucontrol)
2473{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002474 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002476 unsigned int val, item;
2477 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002478 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002480 ret = snd_soc_component_read(component, e->reg, &reg_val);
2481 if (ret)
2482 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002483 val = (reg_val >> e->shift_l) & e->mask;
2484 item = snd_soc_enum_val_to_item(e, val);
2485 ucontrol->value.enumerated.item[0] = item;
2486 if (e->shift_l != e->shift_r) {
2487 val = (reg_val >> e->shift_l) & e->mask;
2488 item = snd_soc_enum_val_to_item(e, val);
2489 ucontrol->value.enumerated.item[1] = item;
2490 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002491
2492 return 0;
2493}
2494EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2495
2496/**
2497 * snd_soc_put_enum_double - enumerated double mixer put callback
2498 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002499 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002500 *
2501 * Callback to set the value of a double enumerated mixer.
2502 *
2503 * Returns 0 for success.
2504 */
2505int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2506 struct snd_ctl_elem_value *ucontrol)
2507{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002508 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002509 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002510 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002511 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002512 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002513
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002514 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002516 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002517 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002519 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002520 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002521 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002522 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 }
2524
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002525 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526}
2527EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2528
2529/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002530 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002531 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002532 * @reg: Register to read
2533 * @mask: Mask to use after shifting the register value
2534 * @shift: Right shift of register value
2535 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002536 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002537 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002538 * This functions reads a codec register. The register value is shifted right
2539 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2540 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002541 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002542 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002543 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002544static int snd_soc_read_signed(struct snd_soc_component *component,
2545 unsigned int reg, unsigned int mask, unsigned int shift,
2546 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002547{
Markus Pargmannf227b882014-01-16 16:02:10 +01002548 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002549 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002550
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002551 ret = snd_soc_component_read(component, reg, &val);
2552 if (ret < 0)
2553 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002554
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002555 val = (val >> shift) & mask;
2556
2557 if (!sign_bit) {
2558 *signed_val = val;
2559 return 0;
2560 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002561
2562 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002563 if (!(val & BIT(sign_bit))) {
2564 *signed_val = val;
2565 return 0;
2566 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002567
2568 ret = val;
2569
2570 /*
2571 * The register most probably does not contain a full-sized int.
2572 * Instead we have an arbitrary number of bits in a signed
2573 * representation which has to be translated into a full-sized int.
2574 * This is done by filling up all bits above the sign-bit.
2575 */
2576 ret |= ~((int)(BIT(sign_bit) - 1));
2577
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002578 *signed_val = ret;
2579
2580 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002581}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002582
2583/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002584 * snd_soc_info_volsw - single mixer info callback
2585 * @kcontrol: mixer control
2586 * @uinfo: control element information
2587 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002588 * Callback to provide information about a single mixer control, or a double
2589 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 *
2591 * Returns 0 for success.
2592 */
2593int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_info *uinfo)
2595{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002596 struct soc_mixer_control *mc =
2597 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002598 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002600 if (!mc->platform_max)
2601 mc->platform_max = mc->max;
2602 platform_max = mc->platform_max;
2603
2604 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002605 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2606 else
2607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2608
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002609 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002611 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612 return 0;
2613}
2614EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2615
2616/**
2617 * snd_soc_get_volsw - single mixer get callback
2618 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002619 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002621 * Callback to get the value of a single mixer control, or a double mixer
2622 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002629 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002630 struct soc_mixer_control *mc =
2631 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002632 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002633 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int shift = mc->shift;
2635 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002636 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002637 int min = mc->min;
2638 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002639 unsigned int mask = (1 << fls(max)) - 1;
2640 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002641 int val;
2642 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643
Markus Pargmannf227b882014-01-16 16:02:10 +01002644 if (sign_bit)
2645 mask = BIT(sign_bit + 1) - 1;
2646
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002647 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2648 if (ret)
2649 return ret;
2650
2651 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002652 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002653 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002654 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002655
2656 if (snd_soc_volsw_is_stereo(mc)) {
2657 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002658 ret = snd_soc_read_signed(component, reg, mask, rshift,
2659 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002660 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002661 ret = snd_soc_read_signed(component, reg2, mask, shift,
2662 sign_bit, &val);
2663 if (ret)
2664 return ret;
2665
2666 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002667 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002668 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002669 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002670 }
2671
2672 return 0;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2675
2676/**
2677 * snd_soc_put_volsw - single mixer put callback
2678 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002679 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002680 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002681 * Callback to set the value of a single mixer control, or a double mixer
2682 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683 *
2684 * Returns 0 for success.
2685 */
2686int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2688{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002689 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002690 struct soc_mixer_control *mc =
2691 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002693 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002694 unsigned int shift = mc->shift;
2695 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002696 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002697 int min = mc->min;
2698 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002699 unsigned int mask = (1 << fls(max)) - 1;
2700 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002701 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002702 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002703 unsigned int val2 = 0;
2704 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705
Markus Pargmannf227b882014-01-16 16:02:10 +01002706 if (sign_bit)
2707 mask = BIT(sign_bit + 1) - 1;
2708
2709 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002711 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002712 val_mask = mask << shift;
2713 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002715 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002716 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002717 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002718 if (reg == reg2) {
2719 val_mask |= mask << rshift;
2720 val |= val2 << rshift;
2721 } else {
2722 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002723 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002724 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002726 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002727 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728 return err;
2729
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002730 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 err = snd_soc_component_update_bits(component, reg2, val_mask,
2732 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002733
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734 return err;
2735}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002736EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002737
Mark Browne13ac2e2008-05-28 17:58:05 +01002738/**
Brian Austin1d99f242012-03-30 10:43:55 -05002739 * snd_soc_get_volsw_sx - single mixer get callback
2740 * @kcontrol: mixer control
2741 * @ucontrol: control element information
2742 *
2743 * Callback to get the value of a single mixer control, or a double mixer
2744 * control that spans 2 registers.
2745 *
2746 * Returns 0 for success.
2747 */
2748int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2750{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002751 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002752 struct soc_mixer_control *mc =
2753 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002754 unsigned int reg = mc->reg;
2755 unsigned int reg2 = mc->rreg;
2756 unsigned int shift = mc->shift;
2757 unsigned int rshift = mc->rshift;
2758 int max = mc->max;
2759 int min = mc->min;
2760 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002761 unsigned int val;
2762 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002763
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002764 ret = snd_soc_component_read(component, reg, &val);
2765 if (ret < 0)
2766 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002767
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002768 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2769
2770 if (snd_soc_volsw_is_stereo(mc)) {
2771 ret = snd_soc_component_read(component, reg2, &val);
2772 if (ret < 0)
2773 return ret;
2774
2775 val = ((val >> rshift) - min) & mask;
2776 ucontrol->value.integer.value[1] = val;
2777 }
Brian Austin1d99f242012-03-30 10:43:55 -05002778
2779 return 0;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2782
2783/**
2784 * snd_soc_put_volsw_sx - double mixer set callback
2785 * @kcontrol: mixer control
2786 * @uinfo: control element information
2787 *
2788 * Callback to set the value of a double mixer control that spans 2 registers.
2789 *
2790 * Returns 0 for success.
2791 */
2792int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002795 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002796 struct soc_mixer_control *mc =
2797 (struct soc_mixer_control *)kcontrol->private_value;
2798
2799 unsigned int reg = mc->reg;
2800 unsigned int reg2 = mc->rreg;
2801 unsigned int shift = mc->shift;
2802 unsigned int rshift = mc->rshift;
2803 int max = mc->max;
2804 int min = mc->min;
2805 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002806 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002807 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002808
2809 val_mask = mask << shift;
2810 val = (ucontrol->value.integer.value[0] + min) & mask;
2811 val = val << shift;
2812
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002813 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302814 if (err < 0)
2815 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002816
2817 if (snd_soc_volsw_is_stereo(mc)) {
2818 val_mask = mask << rshift;
2819 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2820 val2 = val2 << rshift;
2821
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002822 err = snd_soc_component_update_bits(component, reg2, val_mask,
2823 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002824 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002825 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002826}
2827EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2828
2829/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002830 * snd_soc_info_volsw_s8 - signed mixer info callback
2831 * @kcontrol: mixer control
2832 * @uinfo: control element information
2833 *
2834 * Callback to provide information about a signed mixer control.
2835 *
2836 * Returns 0 for success.
2837 */
2838int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_info *uinfo)
2840{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 struct soc_mixer_control *mc =
2842 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002843 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002844 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002845
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002846 if (!mc->platform_max)
2847 mc->platform_max = mc->max;
2848 platform_max = mc->platform_max;
2849
Mark Browne13ac2e2008-05-28 17:58:05 +01002850 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2851 uinfo->count = 2;
2852 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002853 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002854 return 0;
2855}
2856EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2857
2858/**
2859 * snd_soc_get_volsw_s8 - signed mixer get callback
2860 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002861 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002862 *
2863 * Callback to get the value of a signed mixer control.
2864 *
2865 * Returns 0 for success.
2866 */
2867int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2869{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002870 struct soc_mixer_control *mc =
2871 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002872 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002873 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002874 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002875 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002876 int ret;
2877
2878 ret = snd_soc_component_read(component, reg, &val);
2879 if (ret)
2880 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002881
2882 ucontrol->value.integer.value[0] =
2883 ((signed char)(val & 0xff))-min;
2884 ucontrol->value.integer.value[1] =
2885 ((signed char)((val >> 8) & 0xff))-min;
2886 return 0;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2889
2890/**
2891 * snd_soc_put_volsw_sgn - signed mixer put callback
2892 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002893 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002894 *
2895 * Callback to set the value of a signed mixer control.
2896 *
2897 * Returns 0 for success.
2898 */
2899int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
2901{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002902 struct soc_mixer_control *mc =
2903 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002904 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002905 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002906 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002907 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002908
2909 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2910 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2911
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002912 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002913}
2914EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2915
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002916/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002917 * snd_soc_info_volsw_range - single mixer info callback with range.
2918 * @kcontrol: mixer control
2919 * @uinfo: control element information
2920 *
2921 * Callback to provide information, within a range, about a single
2922 * mixer control.
2923 *
2924 * returns 0 for success.
2925 */
2926int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2927 struct snd_ctl_elem_info *uinfo)
2928{
2929 struct soc_mixer_control *mc =
2930 (struct soc_mixer_control *)kcontrol->private_value;
2931 int platform_max;
2932 int min = mc->min;
2933
2934 if (!mc->platform_max)
2935 mc->platform_max = mc->max;
2936 platform_max = mc->platform_max;
2937
2938 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002939 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002940 uinfo->value.integer.min = 0;
2941 uinfo->value.integer.max = platform_max - min;
2942
2943 return 0;
2944}
2945EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2946
2947/**
2948 * snd_soc_put_volsw_range - single mixer put value callback with range.
2949 * @kcontrol: mixer control
2950 * @ucontrol: control element information
2951 *
2952 * Callback to set the value, within a range, for a single mixer control.
2953 *
2954 * Returns 0 for success.
2955 */
2956int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2958{
2959 struct soc_mixer_control *mc =
2960 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002961 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002962 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002963 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002964 unsigned int shift = mc->shift;
2965 int min = mc->min;
2966 int max = mc->max;
2967 unsigned int mask = (1 << fls(max)) - 1;
2968 unsigned int invert = mc->invert;
2969 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002970 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002971
2972 val = ((ucontrol->value.integer.value[0] + min) & mask);
2973 if (invert)
2974 val = max - val;
2975 val_mask = mask << shift;
2976 val = val << shift;
2977
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002978 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002979 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002980 return ret;
2981
2982 if (snd_soc_volsw_is_stereo(mc)) {
2983 val = ((ucontrol->value.integer.value[1] + min) & mask);
2984 if (invert)
2985 val = max - val;
2986 val_mask = mask << shift;
2987 val = val << shift;
2988
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002989 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2990 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002991 }
2992
2993 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994}
2995EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2996
2997/**
2998 * snd_soc_get_volsw_range - single mixer get callback with range
2999 * @kcontrol: mixer control
3000 * @ucontrol: control element information
3001 *
3002 * Callback to get the value, within a range, of a single mixer control.
3003 *
3004 * Returns 0 for success.
3005 */
3006int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3007 struct snd_ctl_elem_value *ucontrol)
3008{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003009 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003010 struct soc_mixer_control *mc =
3011 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003012 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003013 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003014 unsigned int shift = mc->shift;
3015 int min = mc->min;
3016 int max = mc->max;
3017 unsigned int mask = (1 << fls(max)) - 1;
3018 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003019 unsigned int val;
3020 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003021
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003022 ret = snd_soc_component_read(component, reg, &val);
3023 if (ret)
3024 return ret;
3025
3026 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003027 if (invert)
3028 ucontrol->value.integer.value[0] =
3029 max - ucontrol->value.integer.value[0];
3030 ucontrol->value.integer.value[0] =
3031 ucontrol->value.integer.value[0] - min;
3032
Mark Brown9bde4f02012-12-19 16:05:00 +00003033 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003034 ret = snd_soc_component_read(component, rreg, &val);
3035 if (ret)
3036 return ret;
3037
3038 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003039 if (invert)
3040 ucontrol->value.integer.value[1] =
3041 max - ucontrol->value.integer.value[1];
3042 ucontrol->value.integer.value[1] =
3043 ucontrol->value.integer.value[1] - min;
3044 }
3045
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003046 return 0;
3047}
3048EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3049
3050/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003051 * snd_soc_limit_volume - Set new limit to an existing volume control.
3052 *
3053 * @codec: where to look for the control
3054 * @name: Name of the control
3055 * @max: new maximum limit
3056 *
3057 * Return 0 for success, else error.
3058 */
3059int snd_soc_limit_volume(struct snd_soc_codec *codec,
3060 const char *name, int max)
3061{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003062 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003063 struct snd_kcontrol *kctl;
3064 struct soc_mixer_control *mc;
3065 int found = 0;
3066 int ret = -EINVAL;
3067
3068 /* Sanity check for name and max */
3069 if (unlikely(!name || max <= 0))
3070 return -EINVAL;
3071
3072 list_for_each_entry(kctl, &card->controls, list) {
3073 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3074 found = 1;
3075 break;
3076 }
3077 }
3078 if (found) {
3079 mc = (struct soc_mixer_control *)kctl->private_value;
3080 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003081 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003082 ret = 0;
3083 }
3084 }
3085 return ret;
3086}
3087EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3088
Mark Brown71d08512011-10-10 18:31:26 +01003089int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3090 struct snd_ctl_elem_info *uinfo)
3091{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003092 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003093 struct soc_bytes *params = (void *)kcontrol->private_value;
3094
3095 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003096 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003097
3098 return 0;
3099}
3100EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3101
3102int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3103 struct snd_ctl_elem_value *ucontrol)
3104{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003105 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003106 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003107 int ret;
3108
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003109 if (component->regmap)
3110 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003111 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003112 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003113 else
3114 ret = -EINVAL;
3115
Mark Brownf831b052012-02-17 16:20:33 -08003116 /* Hide any masked bytes to ensure consistent data reporting */
3117 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003118 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003119 case 1:
3120 ucontrol->value.bytes.data[0] &= ~params->mask;
3121 break;
3122 case 2:
3123 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003124 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003125 break;
3126 case 4:
3127 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003128 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003129 break;
3130 default:
3131 return -EINVAL;
3132 }
3133 }
3134
Mark Brown71d08512011-10-10 18:31:26 +01003135 return ret;
3136}
3137EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3138
3139int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3141{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003142 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003143 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003144 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003145 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003146 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003147
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003148 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003149 return -EINVAL;
3150
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003151 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003152
Mark Brownb5a8fe42013-01-20 21:42:22 +09003153 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3154 if (!data)
3155 return -ENOMEM;
3156
Mark Brownf831b052012-02-17 16:20:33 -08003157 /*
3158 * If we've got a mask then we need to preserve the register
3159 * bits. We shouldn't modify the incoming data so take a
3160 * copy.
3161 */
3162 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003163 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003164 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003165 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003166
3167 val &= params->mask;
3168
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003169 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003170 case 1:
3171 ((u8 *)data)[0] &= ~params->mask;
3172 ((u8 *)data)[0] |= val;
3173 break;
3174 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003175 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003176 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003177 &mask, &mask);
3178 if (ret != 0)
3179 goto out;
3180
3181 ((u16 *)data)[0] &= mask;
3182
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003183 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003184 &val, &val);
3185 if (ret != 0)
3186 goto out;
3187
3188 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003189 break;
3190 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003191 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003192 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003193 &mask, &mask);
3194 if (ret != 0)
3195 goto out;
3196
3197 ((u32 *)data)[0] &= mask;
3198
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003199 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003200 &val, &val);
3201 if (ret != 0)
3202 goto out;
3203
3204 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003205 break;
3206 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003207 ret = -EINVAL;
3208 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003209 }
3210 }
3211
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003212 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003213 data, len);
3214
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003215out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003216 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003217
3218 return ret;
3219}
3220EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3221
Vinod Kould9881202014-05-02 10:58:11 +05303222int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_info *ucontrol)
3224{
3225 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3226
3227 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3228 ucontrol->count = params->max;
3229
3230 return 0;
3231}
3232EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3233
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303234int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3235 unsigned int size, unsigned int __user *tlv)
3236{
3237 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3238 unsigned int count = size < params->max ? size : params->max;
3239 int ret = -ENXIO;
3240
3241 switch (op_flag) {
3242 case SNDRV_CTL_TLV_OP_READ:
3243 if (params->get)
3244 ret = params->get(tlv, count);
3245 break;
3246 case SNDRV_CTL_TLV_OP_WRITE:
3247 if (params->put)
3248 ret = params->put(tlv, count);
3249 break;
3250 }
3251 return ret;
3252}
3253EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3254
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003255/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003256 * snd_soc_info_xr_sx - signed multi register info callback
3257 * @kcontrol: mreg control
3258 * @uinfo: control element information
3259 *
3260 * Callback to provide information of a control that can
3261 * span multiple codec registers which together
3262 * forms a single signed value in a MSB/LSB manner.
3263 *
3264 * Returns 0 for success.
3265 */
3266int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_info *uinfo)
3268{
3269 struct soc_mreg_control *mc =
3270 (struct soc_mreg_control *)kcontrol->private_value;
3271 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3272 uinfo->count = 1;
3273 uinfo->value.integer.min = mc->min;
3274 uinfo->value.integer.max = mc->max;
3275
3276 return 0;
3277}
3278EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3279
3280/**
3281 * snd_soc_get_xr_sx - signed multi register get callback
3282 * @kcontrol: mreg control
3283 * @ucontrol: control element information
3284 *
3285 * Callback to get the value of a control that can span
3286 * multiple codec registers which together forms a single
3287 * signed value in a MSB/LSB manner. The control supports
3288 * specifying total no of bits used to allow for bitfields
3289 * across the multiple codec registers.
3290 *
3291 * Returns 0 for success.
3292 */
3293int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3294 struct snd_ctl_elem_value *ucontrol)
3295{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003296 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003297 struct soc_mreg_control *mc =
3298 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003299 unsigned int regbase = mc->regbase;
3300 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003301 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003302 unsigned int regwmask = (1<<regwshift)-1;
3303 unsigned int invert = mc->invert;
3304 unsigned long mask = (1UL<<mc->nbits)-1;
3305 long min = mc->min;
3306 long max = mc->max;
3307 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003308 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003309 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003310 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003311
3312 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003313 ret = snd_soc_component_read(component, regbase+i, &regval);
3314 if (ret)
3315 return ret;
3316 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003317 }
3318 val &= mask;
3319 if (min < 0 && val > max)
3320 val |= ~mask;
3321 if (invert)
3322 val = max - val;
3323 ucontrol->value.integer.value[0] = val;
3324
3325 return 0;
3326}
3327EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3328
3329/**
3330 * snd_soc_put_xr_sx - signed multi register get callback
3331 * @kcontrol: mreg control
3332 * @ucontrol: control element information
3333 *
3334 * Callback to set the value of a control that can span
3335 * multiple codec registers which together forms a single
3336 * signed value in a MSB/LSB manner. The control supports
3337 * specifying total no of bits used to allow for bitfields
3338 * across the multiple codec registers.
3339 *
3340 * Returns 0 for success.
3341 */
3342int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3343 struct snd_ctl_elem_value *ucontrol)
3344{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003345 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003346 struct soc_mreg_control *mc =
3347 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003348 unsigned int regbase = mc->regbase;
3349 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003350 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003351 unsigned int regwmask = (1<<regwshift)-1;
3352 unsigned int invert = mc->invert;
3353 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003354 long max = mc->max;
3355 long val = ucontrol->value.integer.value[0];
3356 unsigned int i, regval, regmask;
3357 int err;
3358
3359 if (invert)
3360 val = max - val;
3361 val &= mask;
3362 for (i = 0; i < regcount; i++) {
3363 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3364 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003365 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003366 regmask, regval);
3367 if (err < 0)
3368 return err;
3369 }
3370
3371 return 0;
3372}
3373EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3374
3375/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003376 * snd_soc_get_strobe - strobe get callback
3377 * @kcontrol: mixer control
3378 * @ucontrol: control element information
3379 *
3380 * Callback get the value of a strobe mixer control.
3381 *
3382 * Returns 0 for success.
3383 */
3384int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3386{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003387 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003388 struct soc_mixer_control *mc =
3389 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003390 unsigned int reg = mc->reg;
3391 unsigned int shift = mc->shift;
3392 unsigned int mask = 1 << shift;
3393 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003394 unsigned int val;
3395 int ret;
3396
3397 ret = snd_soc_component_read(component, reg, &val);
3398 if (ret)
3399 return ret;
3400
3401 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003402
3403 if (shift != 0 && val != 0)
3404 val = val >> shift;
3405 ucontrol->value.enumerated.item[0] = val ^ invert;
3406
3407 return 0;
3408}
3409EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3410
3411/**
3412 * snd_soc_put_strobe - strobe put callback
3413 * @kcontrol: mixer control
3414 * @ucontrol: control element information
3415 *
3416 * Callback strobe a register bit to high then low (or the inverse)
3417 * in one pass of a single mixer enum control.
3418 *
3419 * Returns 1 for success.
3420 */
3421int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3423{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003424 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003425 struct soc_mixer_control *mc =
3426 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003427 unsigned int reg = mc->reg;
3428 unsigned int shift = mc->shift;
3429 unsigned int mask = 1 << shift;
3430 unsigned int invert = mc->invert != 0;
3431 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3432 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3433 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3434 int err;
3435
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003436 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003437 if (err < 0)
3438 return err;
3439
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003440 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003441}
3442EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3443
3444/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003445 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3446 * @dai: DAI
3447 * @clk_id: DAI specific clock ID
3448 * @freq: new clock frequency in Hz
3449 * @dir: new clock direction - input/output.
3450 *
3451 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3452 */
3453int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3454 unsigned int freq, int dir)
3455{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003456 if (dai->driver && dai->driver->ops->set_sysclk)
3457 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003458 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003459 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003460 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003461 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003462 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003463}
3464EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3465
3466/**
Mark Brownec4ee522011-03-07 20:58:11 +00003467 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3468 * @codec: CODEC
3469 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003470 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003471 * @freq: new clock frequency in Hz
3472 * @dir: new clock direction - input/output.
3473 *
3474 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3475 */
3476int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003477 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003478{
3479 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003480 return codec->driver->set_sysclk(codec, clk_id, source,
3481 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003482 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003483 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003484}
3485EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3486
3487/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003488 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3489 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003490 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003491 * @div: new clock divisor.
3492 *
3493 * Configures the clock dividers. This is used to derive the best DAI bit and
3494 * frame clocks from the system or master clock. It's best to set the DAI bit
3495 * and frame clocks as low as possible to save system power.
3496 */
3497int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3498 int div_id, int div)
3499{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 if (dai->driver && dai->driver->ops->set_clkdiv)
3501 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003502 else
3503 return -EINVAL;
3504}
3505EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3506
3507/**
3508 * snd_soc_dai_set_pll - configure DAI PLL.
3509 * @dai: DAI
3510 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003511 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003512 * @freq_in: PLL input clock frequency in Hz
3513 * @freq_out: requested PLL output clock frequency in Hz
3514 *
3515 * Configures and enables PLL to generate output clock based on input clock.
3516 */
Mark Brown85488032009-09-05 18:52:16 +01003517int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3518 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003519{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003520 if (dai->driver && dai->driver->ops->set_pll)
3521 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003522 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003523 else if (dai->codec && dai->codec->driver->set_pll)
3524 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3525 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003526 else
3527 return -EINVAL;
3528}
3529EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3530
Mark Brownec4ee522011-03-07 20:58:11 +00003531/*
3532 * snd_soc_codec_set_pll - configure codec PLL.
3533 * @codec: CODEC
3534 * @pll_id: DAI specific PLL ID
3535 * @source: DAI specific source for the PLL
3536 * @freq_in: PLL input clock frequency in Hz
3537 * @freq_out: requested PLL output clock frequency in Hz
3538 *
3539 * Configures and enables PLL to generate output clock based on input clock.
3540 */
3541int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3542 unsigned int freq_in, unsigned int freq_out)
3543{
3544 if (codec->driver->set_pll)
3545 return codec->driver->set_pll(codec, pll_id, source,
3546 freq_in, freq_out);
3547 else
3548 return -EINVAL;
3549}
3550EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3551
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003552/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003553 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3554 * @dai: DAI
3555 * @ratio Ratio of BCLK to Sample rate.
3556 *
3557 * Configures the DAI for a preset BCLK to sample rate ratio.
3558 */
3559int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3560{
3561 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3562 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3563 else
3564 return -EINVAL;
3565}
3566EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3567
3568/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003569 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3570 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003571 * @fmt: SND_SOC_DAIFMT_ format value.
3572 *
3573 * Configures the DAI hardware format and clocking.
3574 */
3575int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3576{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003577 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003578 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003579 if (dai->driver->ops->set_fmt == NULL)
3580 return -ENOTSUPP;
3581 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003582}
3583EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3584
3585/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003586 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003587 * @slots: Number of slots in use.
3588 * @tx_mask: bitmask representing active TX slots.
3589 * @rx_mask: bitmask representing active RX slots.
3590 *
3591 * Generates the TDM tx and rx slot default masks for DAI.
3592 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003593static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003594 unsigned int *tx_mask,
3595 unsigned int *rx_mask)
3596{
3597 if (*tx_mask || *rx_mask)
3598 return 0;
3599
3600 if (!slots)
3601 return -EINVAL;
3602
3603 *tx_mask = (1 << slots) - 1;
3604 *rx_mask = (1 << slots) - 1;
3605
3606 return 0;
3607}
3608
3609/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003610 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3611 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003612 * @tx_mask: bitmask representing active TX slots.
3613 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003614 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003615 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003616 *
3617 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3618 * specific.
3619 */
3620int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003621 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003622{
Xiubo Lie5c21512014-03-21 14:17:12 +08003623 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3624 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003625 &tx_mask, &rx_mask);
3626 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003627 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003628
Benoit Cousson88bd8702014-07-08 23:19:34 +02003629 dai->tx_mask = tx_mask;
3630 dai->rx_mask = rx_mask;
3631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003632 if (dai->driver && dai->driver->ops->set_tdm_slot)
3633 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003634 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003635 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003636 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003637}
3638EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3639
3640/**
Barry Song472df3c2009-09-12 01:16:29 +08003641 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3642 * @dai: DAI
3643 * @tx_num: how many TX channels
3644 * @tx_slot: pointer to an array which imply the TX slot number channel
3645 * 0~num-1 uses
3646 * @rx_num: how many RX channels
3647 * @rx_slot: pointer to an array which imply the RX slot number channel
3648 * 0~num-1 uses
3649 *
3650 * configure the relationship between channel number and TDM slot number.
3651 */
3652int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3653 unsigned int tx_num, unsigned int *tx_slot,
3654 unsigned int rx_num, unsigned int *rx_slot)
3655{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003656 if (dai->driver && dai->driver->ops->set_channel_map)
3657 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003658 rx_num, rx_slot);
3659 else
3660 return -EINVAL;
3661}
3662EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3663
3664/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3666 * @dai: DAI
3667 * @tristate: tristate enable
3668 *
3669 * Tristates the DAI so that others can use it.
3670 */
3671int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3672{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003673 if (dai->driver && dai->driver->ops->set_tristate)
3674 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003675 else
3676 return -EINVAL;
3677}
3678EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3679
3680/**
3681 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3682 * @dai: DAI
3683 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003684 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003685 *
3686 * Mutes the DAI DAC.
3687 */
Mark Brownda183962013-02-06 15:44:07 +00003688int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3689 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003690{
Mark Brownda183962013-02-06 15:44:07 +00003691 if (!dai->driver)
3692 return -ENOTSUPP;
3693
3694 if (dai->driver->ops->mute_stream)
3695 return dai->driver->ops->mute_stream(dai, mute, direction);
3696 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3697 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003698 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003699 else
Mark Brown04570c62012-04-13 19:16:03 +01003700 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003701}
3702EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3703
Benoit Cousson88bd8702014-07-08 23:19:34 +02003704static int snd_soc_init_multicodec(struct snd_soc_card *card,
3705 struct snd_soc_dai_link *dai_link)
3706{
3707 /* Legacy codec/codec_dai link is a single entry in multicodec */
3708 if (dai_link->codec_name || dai_link->codec_of_node ||
3709 dai_link->codec_dai_name) {
3710 dai_link->num_codecs = 1;
3711
3712 dai_link->codecs = devm_kzalloc(card->dev,
3713 sizeof(struct snd_soc_dai_link_component),
3714 GFP_KERNEL);
3715 if (!dai_link->codecs)
3716 return -ENOMEM;
3717
3718 dai_link->codecs[0].name = dai_link->codec_name;
3719 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3720 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3721 }
3722
3723 if (!dai_link->codecs) {
3724 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3725 return -EINVAL;
3726 }
3727
3728 return 0;
3729}
3730
Mark Brownc5af3a22008-11-28 13:29:45 +00003731/**
3732 * snd_soc_register_card - Register a card with the ASoC core
3733 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003734 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003735 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003736 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303737int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003738{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003739 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003740
Mark Brownc5af3a22008-11-28 13:29:45 +00003741 if (!card->name || !card->dev)
3742 return -EINVAL;
3743
Stephen Warren5a504962011-12-21 10:40:59 -07003744 for (i = 0; i < card->num_links; i++) {
3745 struct snd_soc_dai_link *link = &card->dai_link[i];
3746
Benoit Cousson88bd8702014-07-08 23:19:34 +02003747 ret = snd_soc_init_multicodec(card, link);
3748 if (ret) {
3749 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3750 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003751 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003752
3753 for (j = 0; j < link->num_codecs; j++) {
3754 /*
3755 * Codec must be specified by 1 of name or OF node,
3756 * not both or neither.
3757 */
3758 if (!!link->codecs[j].name ==
3759 !!link->codecs[j].of_node) {
3760 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3761 link->name);
3762 return -EINVAL;
3763 }
3764 /* Codec DAI name must be specified */
3765 if (!link->codecs[j].dai_name) {
3766 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3767 link->name);
3768 return -EINVAL;
3769 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003770 }
Stephen Warren5a504962011-12-21 10:40:59 -07003771
3772 /*
3773 * Platform may be specified by either name or OF node, but
3774 * can be left unspecified, and a dummy platform will be used.
3775 */
3776 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003777 dev_err(card->dev,
3778 "ASoC: Both platform name/of_node are set for %s\n",
3779 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003780 return -EINVAL;
3781 }
3782
3783 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003784 * CPU device may be specified by either name or OF node, but
3785 * can be left unspecified, and will be matched based on DAI
3786 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003787 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003788 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003789 dev_err(card->dev,
3790 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3791 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003792 return -EINVAL;
3793 }
3794 /*
3795 * At least one of CPU DAI name or CPU device name/node must be
3796 * specified
3797 */
3798 if (!link->cpu_dai_name &&
3799 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003800 dev_err(card->dev,
3801 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3802 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003803 return -EINVAL;
3804 }
3805 }
3806
Mark Browned77cc12011-05-03 18:25:34 +01003807 dev_set_drvdata(card->dev, card);
3808
Stephen Warren111c6412011-01-28 14:26:35 -07003809 snd_soc_initialize_card_lists(card);
3810
Vinod Koul150dd2f2011-01-13 22:48:32 +05303811 soc_init_card_debugfs(card);
3812
Mark Brown181a6892012-03-14 20:18:49 +00003813 card->rtd = devm_kzalloc(card->dev,
3814 sizeof(struct snd_soc_pcm_runtime) *
3815 (card->num_links + card->num_aux_devs),
3816 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003817 if (card->rtd == NULL)
3818 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003819 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003820 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003821
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003822 for (i = 0; i < card->num_links; i++) {
3823 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003824 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003825 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3826 sizeof(struct snd_soc_dai *) *
3827 (card->rtd[i].dai_link->num_codecs),
3828 GFP_KERNEL);
3829 if (card->rtd[i].codec_dais == NULL)
3830 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003831 }
3832
3833 for (i = 0; i < card->num_aux_devs; i++)
3834 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003835
Mark Browndb432b42011-10-03 21:06:40 +01003836 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003837 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003839 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003840
Mark Brownb19e6e72012-03-14 21:18:39 +00003841 ret = snd_soc_instantiate_card(card);
3842 if (ret != 0)
3843 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003844
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003845 /* deactivate pins to sleep state */
3846 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003847 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3848 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3849 int j;
3850
3851 for (j = 0; j < rtd->num_codecs; j++) {
3852 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3853 if (!codec_dai->active)
3854 pinctrl_pm_select_sleep_state(codec_dai->dev);
3855 }
3856
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003857 if (!cpu_dai->active)
3858 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3859 }
3860
Mark Brownb19e6e72012-03-14 21:18:39 +00003861 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003862}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303863EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003864
3865/**
3866 * snd_soc_unregister_card - Unregister a card with the ASoC core
3867 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003868 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003869 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003870 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303871int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003872{
Vinod Koulb0e26482011-01-13 22:48:02 +05303873 if (card->instantiated)
3874 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003875 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003876
3877 return 0;
3878}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303879EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003880
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881/*
3882 * Simplify DAI link configuration by removing ".-1" from device names
3883 * and sanitizing names.
3884 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003885static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886{
3887 char *found, name[NAME_SIZE];
3888 int id1, id2;
3889
3890 if (dev_name(dev) == NULL)
3891 return NULL;
3892
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003893 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003894
3895 /* are we a "%s.%d" name (platform and SPI components) */
3896 found = strstr(name, dev->driver->name);
3897 if (found) {
3898 /* get ID */
3899 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3900
3901 /* discard ID from name if ID == -1 */
3902 if (*id == -1)
3903 found[strlen(dev->driver->name)] = '\0';
3904 }
3905
3906 } else {
3907 /* I2C component devices are named "bus-addr" */
3908 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3909 char tmp[NAME_SIZE];
3910
3911 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003912 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913
3914 /* sanitize component name for DAI link creation */
3915 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003916 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003917 } else
3918 *id = 0;
3919 }
3920
3921 return kstrdup(name, GFP_KERNEL);
3922}
3923
3924/*
3925 * Simplify DAI link naming for single devices with multiple DAIs by removing
3926 * any ".-1" and using the DAI name (instead of device name).
3927 */
3928static inline char *fmt_multiple_name(struct device *dev,
3929 struct snd_soc_dai_driver *dai_drv)
3930{
3931 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003932 dev_err(dev,
3933 "ASoC: error - multiple DAI %s registered with no name\n",
3934 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003935 return NULL;
3936 }
3937
3938 return kstrdup(dai_drv->name, GFP_KERNEL);
3939}
3940
Mark Brown91151712008-11-30 23:31:24 +00003941/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003942 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003943 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003944 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003945 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003946static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003947{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003948 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003949
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003950 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003951 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3952 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003953 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003954 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003955 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956 }
Mark Brown91151712008-11-30 23:31:24 +00003957}
Mark Brown91151712008-11-30 23:31:24 +00003958
3959/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003960 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003961 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003962 * @component: The component the DAIs are registered for
3963 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003964 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003965 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3966 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003967 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003968static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003969 struct snd_soc_dai_driver *dai_drv, size_t count,
3970 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003971{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003972 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003973 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003974 unsigned int i;
3975 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003976
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003977 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003978
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003979 component->dai_drv = dai_drv;
3980 component->num_dai = count;
3981
Mark Brown91151712008-11-30 23:31:24 +00003982 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003983
3984 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003985 if (dai == NULL) {
3986 ret = -ENOMEM;
3987 goto err;
3988 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003989
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003990 /*
3991 * Back in the old days when we still had component-less DAIs,
3992 * instead of having a static name, component-less DAIs would
3993 * inherit the name of the parent device so it is possible to
3994 * register multiple instances of the DAI. We still need to keep
3995 * the same naming style even though those DAIs are not
3996 * component-less anymore.
3997 */
3998 if (count == 1 && legacy_dai_naming) {
3999 dai->name = fmt_single_name(dev, &dai->id);
4000 } else {
4001 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4002 if (dai_drv[i].id)
4003 dai->id = dai_drv[i].id;
4004 else
4005 dai->id = i;
4006 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007 if (dai->name == NULL) {
4008 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004009 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004010 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004011 }
4012
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004013 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004014 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004015 dai->driver = &dai_drv[i];
4016 if (!dai->driver->ops)
4017 dai->driver->ops = &null_dai_ops;
4018
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004019 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004020
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004021 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004022 }
4023
4024 return 0;
4025
4026err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004027 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004028
4029 return ret;
4030}
Mark Brown91151712008-11-30 23:31:24 +00004031
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004032static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4033 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004034{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004035 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004036
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004037 component->driver->seq_notifier(component, type, subseq);
4038}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004039
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004040static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4041 int event)
4042{
4043 struct snd_soc_component *component = dapm->component;
4044
4045 return component->driver->stream_event(component, event);
4046}
4047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004048static int snd_soc_component_initialize(struct snd_soc_component *component,
4049 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004050{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004051 struct snd_soc_dapm_context *dapm;
4052
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004053 component->name = fmt_single_name(dev, &component->id);
4054 if (!component->name) {
4055 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004056 return -ENOMEM;
4057 }
4058
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004059 component->dev = dev;
4060 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004061 component->probe = component->driver->probe;
4062 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004063
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004064 if (!component->dapm_ptr)
4065 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004066
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004067 dapm = component->dapm_ptr;
4068 dapm->dev = dev;
4069 dapm->component = component;
4070 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004071 if (driver->seq_notifier)
4072 dapm->seq_notifier = snd_soc_component_seq_notifier;
4073 if (driver->stream_event)
4074 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004075
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004076 component->controls = driver->controls;
4077 component->num_controls = driver->num_controls;
4078 component->dapm_widgets = driver->dapm_widgets;
4079 component->num_dapm_widgets = driver->num_dapm_widgets;
4080 component->dapm_routes = driver->dapm_routes;
4081 component->num_dapm_routes = driver->num_dapm_routes;
4082
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004083 INIT_LIST_HEAD(&component->dai_list);
4084 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004085
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004086 return 0;
4087}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004088
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004089static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4090{
4091 list_add(&component->list, &component_list);
4092}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004093
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004094static void snd_soc_component_add(struct snd_soc_component *component)
4095{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004096 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004097 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004098 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004099}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004100
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004101static void snd_soc_component_cleanup(struct snd_soc_component *component)
4102{
4103 snd_soc_unregister_dais(component);
4104 kfree(component->name);
4105}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004106
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004107static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4108{
4109 list_del(&component->list);
4110}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004111
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004112static void snd_soc_component_del(struct snd_soc_component *component)
4113{
4114 mutex_lock(&client_mutex);
4115 snd_soc_component_del_unlocked(component);
4116 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004117}
4118
4119int snd_soc_register_component(struct device *dev,
4120 const struct snd_soc_component_driver *cmpnt_drv,
4121 struct snd_soc_dai_driver *dai_drv,
4122 int num_dai)
4123{
4124 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004125 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004126
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004127 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004128 if (!cmpnt) {
4129 dev_err(dev, "ASoC: Failed to allocate memory\n");
4130 return -ENOMEM;
4131 }
4132
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004133 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4134 if (ret)
4135 goto err_free;
4136
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004137 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004138 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004139
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004140 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4141 if (ret < 0) {
4142 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4143 goto err_cleanup;
4144 }
4145
4146 snd_soc_component_add(cmpnt);
4147
4148 return 0;
4149
4150err_cleanup:
4151 snd_soc_component_cleanup(cmpnt);
4152err_free:
4153 kfree(cmpnt);
4154 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004155}
4156EXPORT_SYMBOL_GPL(snd_soc_register_component);
4157
4158/**
4159 * snd_soc_unregister_component - Unregister a component from the ASoC core
4160 *
4161 */
4162void snd_soc_unregister_component(struct device *dev)
4163{
4164 struct snd_soc_component *cmpnt;
4165
4166 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004167 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004168 goto found;
4169 }
4170 return;
4171
4172found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004173 snd_soc_component_del(cmpnt);
4174 snd_soc_component_cleanup(cmpnt);
4175 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004176}
4177EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4178
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004179static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4180{
4181 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4182
4183 return platform->driver->probe(platform);
4184}
4185
4186static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4187{
4188 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4189
4190 platform->driver->remove(platform);
4191}
4192
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004193static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4194 unsigned int reg, unsigned int val)
4195{
4196 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4197
4198 return platform->driver->write(platform, reg, val);
4199}
4200
4201static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4202 unsigned int reg, unsigned int *val)
4203{
4204 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4205
4206 *val = platform->driver->read(platform, reg);
4207
4208 return 0;
4209}
4210
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004211/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004212 * snd_soc_add_platform - Add a platform to the ASoC core
4213 * @dev: The parent device for the platform
4214 * @platform: The platform to add
4215 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004216 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004217int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004218 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004219{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004220 int ret;
4221
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004222 ret = snd_soc_component_initialize(&platform->component,
4223 &platform_drv->component_driver, dev);
4224 if (ret)
4225 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004226
4227 platform->dev = dev;
4228 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004229 if (platform_drv->controls) {
4230 platform->component.controls = platform_drv->controls;
4231 platform->component.num_controls = platform_drv->num_controls;
4232 }
4233 if (platform_drv->dapm_widgets) {
4234 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4235 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4236 platform->component.steal_sibling_dai_widgets = true;
4237 }
4238 if (platform_drv->dapm_routes) {
4239 platform->component.dapm_routes = platform_drv->dapm_routes;
4240 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4241 }
4242
4243 if (platform_drv->probe)
4244 platform->component.probe = snd_soc_platform_drv_probe;
4245 if (platform_drv->remove)
4246 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004247 if (platform_drv->write)
4248 platform->component.write = snd_soc_platform_drv_write;
4249 if (platform_drv->read)
4250 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004251
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004252#ifdef CONFIG_DEBUG_FS
4253 platform->component.debugfs_prefix = "platform";
4254#endif
4255
Mark Brown12a48a8c2008-12-03 19:40:30 +00004256 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004257 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004258 list_add(&platform->list, &platform_list);
4259 mutex_unlock(&client_mutex);
4260
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004261 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4262 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004263
4264 return 0;
4265}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004266EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4267
4268/**
4269 * snd_soc_register_platform - Register a platform with the ASoC core
4270 *
4271 * @platform: platform to register
4272 */
4273int snd_soc_register_platform(struct device *dev,
4274 const struct snd_soc_platform_driver *platform_drv)
4275{
4276 struct snd_soc_platform *platform;
4277 int ret;
4278
4279 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4280
4281 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4282 if (platform == NULL)
4283 return -ENOMEM;
4284
4285 ret = snd_soc_add_platform(dev, platform, platform_drv);
4286 if (ret)
4287 kfree(platform);
4288
4289 return ret;
4290}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004291EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4292
4293/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004294 * snd_soc_remove_platform - Remove a platform from the ASoC core
4295 * @platform: the platform to remove
4296 */
4297void snd_soc_remove_platform(struct snd_soc_platform *platform)
4298{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004299
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004300 mutex_lock(&client_mutex);
4301 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004302 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004303 mutex_unlock(&client_mutex);
4304
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004305 snd_soc_component_cleanup(&platform->component);
4306
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004307 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004308 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004309}
4310EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4311
4312struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4313{
4314 struct snd_soc_platform *platform;
4315
4316 list_for_each_entry(platform, &platform_list, list) {
4317 if (dev == platform->dev)
4318 return platform;
4319 }
4320
4321 return NULL;
4322}
4323EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4324
4325/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004326 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4327 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004328 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004329 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004330void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004331{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004332 struct snd_soc_platform *platform;
4333
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004334 platform = snd_soc_lookup_platform(dev);
4335 if (!platform)
4336 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004337
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004338 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004339 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004340}
4341EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4342
Mark Brown151ab222009-05-09 16:22:58 +01004343static u64 codec_format_map[] = {
4344 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4345 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4346 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4347 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4348 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4349 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4350 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4351 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4352 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4353 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4354 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4355 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4356 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4357 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4358 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4359 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4360};
4361
4362/* Fix up the DAI formats for endianness: codecs don't actually see
4363 * the endianness of the data but we're using the CPU format
4364 * definitions which do need to include endianness so we ensure that
4365 * codec DAIs always have both big and little endian variants set.
4366 */
4367static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4368{
4369 int i;
4370
4371 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4372 if (stream->formats & codec_format_map[i])
4373 stream->formats |= codec_format_map[i];
4374}
4375
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004376static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4377{
4378 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4379
4380 return codec->driver->probe(codec);
4381}
4382
4383static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4384{
4385 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4386
4387 codec->driver->remove(codec);
4388}
4389
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004390static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4391 unsigned int reg, unsigned int val)
4392{
4393 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4394
4395 return codec->driver->write(codec, reg, val);
4396}
4397
4398static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4399 unsigned int reg, unsigned int *val)
4400{
4401 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4402
4403 *val = codec->driver->read(codec, reg);
4404
4405 return 0;
4406}
4407
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004408static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4409 enum snd_soc_bias_level level)
4410{
4411 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4412
4413 return codec->driver->set_bias_level(codec, level);
4414}
4415
Mark Brown0d0cf002008-12-10 14:32:45 +00004416/**
4417 * snd_soc_register_codec - Register a codec with the ASoC core
4418 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004419 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004420 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004421int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004422 const struct snd_soc_codec_driver *codec_drv,
4423 struct snd_soc_dai_driver *dai_drv,
4424 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004425{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004426 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004427 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004428 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004429 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004430
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004431 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004432
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004433 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4434 if (codec == NULL)
4435 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004436
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004437 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004438 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004439
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004440 ret = snd_soc_component_initialize(&codec->component,
4441 &codec_drv->component_driver, dev);
4442 if (ret)
4443 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004444
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004445 if (codec_drv->controls) {
4446 codec->component.controls = codec_drv->controls;
4447 codec->component.num_controls = codec_drv->num_controls;
4448 }
4449 if (codec_drv->dapm_widgets) {
4450 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4451 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4452 }
4453 if (codec_drv->dapm_routes) {
4454 codec->component.dapm_routes = codec_drv->dapm_routes;
4455 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4456 }
4457
4458 if (codec_drv->probe)
4459 codec->component.probe = snd_soc_codec_drv_probe;
4460 if (codec_drv->remove)
4461 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004462 if (codec_drv->write)
4463 codec->component.write = snd_soc_codec_drv_write;
4464 if (codec_drv->read)
4465 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004466 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004467 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004468 if (codec_drv->seq_notifier)
4469 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004470 if (codec_drv->set_bias_level)
4471 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004472 codec->dev = dev;
4473 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004474 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004475 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004476
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004477#ifdef CONFIG_DEBUG_FS
4478 codec->component.init_debugfs = soc_init_codec_debugfs;
4479 codec->component.debugfs_prefix = "codec";
4480#endif
4481
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004482 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004483 if (codec_drv->get_regmap)
4484 regmap = codec_drv->get_regmap(dev);
4485 else
4486 regmap = dev_get_regmap(dev, NULL);
4487
4488 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004489 ret = snd_soc_component_init_io(&codec->component,
4490 regmap);
4491 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004492 dev_err(codec->dev,
4493 "Failed to set cache I/O:%d\n",
4494 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004495 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004496 }
4497 }
4498 }
4499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004500 for (i = 0; i < num_dai; i++) {
4501 fixup_codec_formats(&dai_drv[i].playback);
4502 fixup_codec_formats(&dai_drv[i].capture);
4503 }
4504
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004505 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4506 if (ret < 0) {
4507 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4508 goto err_cleanup;
4509 }
4510
4511 list_for_each_entry(dai, &codec->component.dai_list, list)
4512 dai->codec = codec;
4513
Mark Brown054880f2012-04-09 17:29:19 +01004514 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004515 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004516 list_add(&codec->list, &codec_list);
4517 mutex_unlock(&client_mutex);
4518
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004519 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4520 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004521 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004522
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004523err_cleanup:
4524 snd_soc_component_cleanup(&codec->component);
4525err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004526 kfree(codec);
4527 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004528}
4529EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4530
4531/**
4532 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4533 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004534 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004535 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004536void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004537{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004538 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004539
4540 list_for_each_entry(codec, &codec_list, list) {
4541 if (dev == codec->dev)
4542 goto found;
4543 }
4544 return;
4545
4546found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004547
Mark Brown0d0cf002008-12-10 14:32:45 +00004548 mutex_lock(&client_mutex);
4549 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004550 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004551 mutex_unlock(&client_mutex);
4552
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004553 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4554 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004555
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004556 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004557 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004558 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004559}
4560EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4561
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004562/* Retrieve a card's name from device tree */
4563int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4564 const char *propname)
4565{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304566 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004567 int ret;
4568
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304569 if (!card->dev) {
4570 pr_err("card->dev is not set before calling %s\n", __func__);
4571 return -EINVAL;
4572 }
4573
4574 np = card->dev->of_node;
4575
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004576 ret = of_property_read_string_index(np, propname, 0, &card->name);
4577 /*
4578 * EINVAL means the property does not exist. This is fine providing
4579 * card->name was previously set, which is checked later in
4580 * snd_soc_register_card.
4581 */
4582 if (ret < 0 && ret != -EINVAL) {
4583 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004584 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004585 propname, ret);
4586 return ret;
4587 }
4588
4589 return 0;
4590}
4591EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4592
Xiubo Li9a6d4862014-02-08 15:59:52 +08004593static const struct snd_soc_dapm_widget simple_widgets[] = {
4594 SND_SOC_DAPM_MIC("Microphone", NULL),
4595 SND_SOC_DAPM_LINE("Line", NULL),
4596 SND_SOC_DAPM_HP("Headphone", NULL),
4597 SND_SOC_DAPM_SPK("Speaker", NULL),
4598};
4599
4600int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4601 const char *propname)
4602{
4603 struct device_node *np = card->dev->of_node;
4604 struct snd_soc_dapm_widget *widgets;
4605 const char *template, *wname;
4606 int i, j, num_widgets, ret;
4607
4608 num_widgets = of_property_count_strings(np, propname);
4609 if (num_widgets < 0) {
4610 dev_err(card->dev,
4611 "ASoC: Property '%s' does not exist\n", propname);
4612 return -EINVAL;
4613 }
4614 if (num_widgets & 1) {
4615 dev_err(card->dev,
4616 "ASoC: Property '%s' length is not even\n", propname);
4617 return -EINVAL;
4618 }
4619
4620 num_widgets /= 2;
4621 if (!num_widgets) {
4622 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4623 propname);
4624 return -EINVAL;
4625 }
4626
4627 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4628 GFP_KERNEL);
4629 if (!widgets) {
4630 dev_err(card->dev,
4631 "ASoC: Could not allocate memory for widgets\n");
4632 return -ENOMEM;
4633 }
4634
4635 for (i = 0; i < num_widgets; i++) {
4636 ret = of_property_read_string_index(np, propname,
4637 2 * i, &template);
4638 if (ret) {
4639 dev_err(card->dev,
4640 "ASoC: Property '%s' index %d read error:%d\n",
4641 propname, 2 * i, ret);
4642 return -EINVAL;
4643 }
4644
4645 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4646 if (!strncmp(template, simple_widgets[j].name,
4647 strlen(simple_widgets[j].name))) {
4648 widgets[i] = simple_widgets[j];
4649 break;
4650 }
4651 }
4652
4653 if (j >= ARRAY_SIZE(simple_widgets)) {
4654 dev_err(card->dev,
4655 "ASoC: DAPM widget '%s' is not supported\n",
4656 template);
4657 return -EINVAL;
4658 }
4659
4660 ret = of_property_read_string_index(np, propname,
4661 (2 * i) + 1,
4662 &wname);
4663 if (ret) {
4664 dev_err(card->dev,
4665 "ASoC: Property '%s' index %d read error:%d\n",
4666 propname, (2 * i) + 1, ret);
4667 return -EINVAL;
4668 }
4669
4670 widgets[i].name = wname;
4671 }
4672
4673 card->dapm_widgets = widgets;
4674 card->num_dapm_widgets = num_widgets;
4675
4676 return 0;
4677}
4678EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4679
Xiubo Li89c67852014-02-14 09:34:35 +08004680int snd_soc_of_parse_tdm_slot(struct device_node *np,
4681 unsigned int *slots,
4682 unsigned int *slot_width)
4683{
4684 u32 val;
4685 int ret;
4686
4687 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4688 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4689 if (ret)
4690 return ret;
4691
4692 if (slots)
4693 *slots = val;
4694 }
4695
4696 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4697 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4698 if (ret)
4699 return ret;
4700
4701 if (slot_width)
4702 *slot_width = val;
4703 }
4704
4705 return 0;
4706}
4707EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4708
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004709int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4710 const char *propname)
4711{
4712 struct device_node *np = card->dev->of_node;
4713 int num_routes;
4714 struct snd_soc_dapm_route *routes;
4715 int i, ret;
4716
4717 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004718 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004719 dev_err(card->dev,
4720 "ASoC: Property '%s' does not exist or its length is not even\n",
4721 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004722 return -EINVAL;
4723 }
4724 num_routes /= 2;
4725 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004726 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004727 propname);
4728 return -EINVAL;
4729 }
4730
4731 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4732 GFP_KERNEL);
4733 if (!routes) {
4734 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004735 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004736 return -EINVAL;
4737 }
4738
4739 for (i = 0; i < num_routes; i++) {
4740 ret = of_property_read_string_index(np, propname,
4741 2 * i, &routes[i].sink);
4742 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004743 dev_err(card->dev,
4744 "ASoC: Property '%s' index %d could not be read: %d\n",
4745 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004746 return -EINVAL;
4747 }
4748 ret = of_property_read_string_index(np, propname,
4749 (2 * i) + 1, &routes[i].source);
4750 if (ret) {
4751 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004752 "ASoC: Property '%s' index %d could not be read: %d\n",
4753 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004754 return -EINVAL;
4755 }
4756 }
4757
4758 card->num_dapm_routes = num_routes;
4759 card->dapm_routes = routes;
4760
4761 return 0;
4762}
4763EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4764
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004765unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004766 const char *prefix,
4767 struct device_node **bitclkmaster,
4768 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004769{
4770 int ret, i;
4771 char prop[128];
4772 unsigned int format = 0;
4773 int bit, frame;
4774 const char *str;
4775 struct {
4776 char *name;
4777 unsigned int val;
4778 } of_fmt_table[] = {
4779 { "i2s", SND_SOC_DAIFMT_I2S },
4780 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4781 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4782 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4783 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4784 { "ac97", SND_SOC_DAIFMT_AC97 },
4785 { "pdm", SND_SOC_DAIFMT_PDM},
4786 { "msb", SND_SOC_DAIFMT_MSB },
4787 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004788 };
4789
4790 if (!prefix)
4791 prefix = "";
4792
4793 /*
4794 * check "[prefix]format = xxx"
4795 * SND_SOC_DAIFMT_FORMAT_MASK area
4796 */
4797 snprintf(prop, sizeof(prop), "%sformat", prefix);
4798 ret = of_property_read_string(np, prop, &str);
4799 if (ret == 0) {
4800 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4801 if (strcmp(str, of_fmt_table[i].name) == 0) {
4802 format |= of_fmt_table[i].val;
4803 break;
4804 }
4805 }
4806 }
4807
4808 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004809 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004810 * SND_SOC_DAIFMT_CLOCK_MASK area
4811 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004812 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4813 if (of_get_property(np, prop, NULL))
4814 format |= SND_SOC_DAIFMT_CONT;
4815 else
4816 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004817
4818 /*
4819 * check "[prefix]bitclock-inversion"
4820 * check "[prefix]frame-inversion"
4821 * SND_SOC_DAIFMT_INV_MASK area
4822 */
4823 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4824 bit = !!of_get_property(np, prop, NULL);
4825
4826 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4827 frame = !!of_get_property(np, prop, NULL);
4828
4829 switch ((bit << 4) + frame) {
4830 case 0x11:
4831 format |= SND_SOC_DAIFMT_IB_IF;
4832 break;
4833 case 0x10:
4834 format |= SND_SOC_DAIFMT_IB_NF;
4835 break;
4836 case 0x01:
4837 format |= SND_SOC_DAIFMT_NB_IF;
4838 break;
4839 default:
4840 /* SND_SOC_DAIFMT_NB_NF is default */
4841 break;
4842 }
4843
4844 /*
4845 * check "[prefix]bitclock-master"
4846 * check "[prefix]frame-master"
4847 * SND_SOC_DAIFMT_MASTER_MASK area
4848 */
4849 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4850 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004851 if (bit && bitclkmaster)
4852 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004853
4854 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4855 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004856 if (frame && framemaster)
4857 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004858
4859 switch ((bit << 4) + frame) {
4860 case 0x11:
4861 format |= SND_SOC_DAIFMT_CBM_CFM;
4862 break;
4863 case 0x10:
4864 format |= SND_SOC_DAIFMT_CBM_CFS;
4865 break;
4866 case 0x01:
4867 format |= SND_SOC_DAIFMT_CBS_CFM;
4868 break;
4869 default:
4870 format |= SND_SOC_DAIFMT_CBS_CFS;
4871 break;
4872 }
4873
4874 return format;
4875}
4876EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4877
Kuninori Morimotocb470082013-09-10 17:39:56 -07004878int snd_soc_of_get_dai_name(struct device_node *of_node,
4879 const char **dai_name)
4880{
4881 struct snd_soc_component *pos;
4882 struct of_phandle_args args;
4883 int ret;
4884
4885 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4886 "#sound-dai-cells", 0, &args);
4887 if (ret)
4888 return ret;
4889
4890 ret = -EPROBE_DEFER;
4891
4892 mutex_lock(&client_mutex);
4893 list_for_each_entry(pos, &component_list, list) {
4894 if (pos->dev->of_node != args.np)
4895 continue;
4896
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004897 if (pos->driver->of_xlate_dai_name) {
4898 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4899 } else {
4900 int id = -1;
4901
4902 switch (args.args_count) {
4903 case 0:
4904 id = 0; /* same as dai_drv[0] */
4905 break;
4906 case 1:
4907 id = args.args[0];
4908 break;
4909 default:
4910 /* not supported */
4911 break;
4912 }
4913
4914 if (id < 0 || id >= pos->num_dai) {
4915 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004916 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004917 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004918
4919 ret = 0;
4920
4921 *dai_name = pos->dai_drv[id].name;
4922 if (!*dai_name)
4923 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004924 }
4925
Kuninori Morimotocb470082013-09-10 17:39:56 -07004926 break;
4927 }
4928 mutex_unlock(&client_mutex);
4929
4930 of_node_put(args.np);
4931
4932 return ret;
4933}
4934EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4935
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004936static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004937{
Mark Brown384c89e2008-12-03 17:34:03 +00004938#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004939 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4940 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004941 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004942 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004943 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004944
Mark Brown8a9dab12011-01-10 22:25:21 +00004945 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004946 &codec_list_fops))
4947 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4948
Mark Brown8a9dab12011-01-10 22:25:21 +00004949 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004950 &dai_list_fops))
4951 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004952
Mark Brown8a9dab12011-01-10 22:25:21 +00004953 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004954 &platform_list_fops))
4955 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004956#endif
4957
Mark Brownfb257892011-04-28 10:57:54 +01004958 snd_soc_util_init();
4959
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004960 return platform_driver_register(&soc_driver);
4961}
Mark Brown4abe8e12010-10-12 17:41:03 +01004962module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004963
Mark Brown7d8c16a2008-11-30 22:11:24 +00004964static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004965{
Mark Brownfb257892011-04-28 10:57:54 +01004966 snd_soc_util_exit();
4967
Mark Brown384c89e2008-12-03 17:34:03 +00004968#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004969 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004970#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004971 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004972}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004973module_exit(snd_soc_exit);
4974
4975/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004976MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004977MODULE_DESCRIPTION("ALSA SoC Core");
4978MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004979MODULE_ALIAS("platform:soc-audio");