blob: 79371a77f32497e7f57556aa766412f34ccd925e [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
Benoit Cousson88bd8702014-07-08 23:19:34 +0200863static struct snd_soc_codec *soc_find_codec(
864 const struct device_node *codec_of_node,
865 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100866{
867 struct snd_soc_codec *codec;
868
869 list_for_each_entry(codec, &codec_list, list) {
870 if (codec_of_node) {
871 if (codec->dev->of_node != codec_of_node)
872 continue;
873 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200874 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100875 continue;
876 }
877
878 return codec;
879 }
880
881 return NULL;
882}
883
884static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
885 const char *codec_dai_name)
886{
887 struct snd_soc_dai *codec_dai;
888
889 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
890 if (!strcmp(codec_dai->name, codec_dai_name)) {
891 return codec_dai;
892 }
893 }
894
895 return NULL;
896}
897
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
901 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100902 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
904 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000905 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100907 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200908 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200909
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000910 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000911
Mark Brownb19e6e72012-03-14 21:18:39 +0000912 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100913 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600914 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100915 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600916 continue;
917 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100918 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600919 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100920 list_for_each_entry(cpu_dai, &component->dai_list, list) {
921 if (dai_link->cpu_dai_name &&
922 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
923 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700924
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100925 rtd->cpu_dai = cpu_dai;
926 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000928
929 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000930 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000932 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000933 }
934
Benoit Cousson88bd8702014-07-08 23:19:34 +0200935 rtd->num_codecs = dai_link->num_codecs;
936
937 /* Find CODEC from registered CODECs */
938 for (i = 0; i < rtd->num_codecs; i++) {
939 struct snd_soc_codec *codec;
940 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
941 if (!codec) {
942 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
943 codecs[i].name);
944 return -EPROBE_DEFER;
945 }
946
947 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
948 if (!codec_dais[i]) {
949 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
950 codecs[i].dai_name);
951 return -EPROBE_DEFER;
952 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000953 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100954
Benoit Cousson88bd8702014-07-08 23:19:34 +0200955 /* Single codec links expect codec and codec_dai in runtime data */
956 rtd->codec_dai = codec_dais[0];
957 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100958
Mark Brown848dd8b2011-04-27 18:16:32 +0100959 /* if there's no platform we match on the empty platform */
960 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700961 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100962 platform_name = "snd-soc-dummy";
963
Mark Brownb19e6e72012-03-14 21:18:39 +0000964 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700966 if (dai_link->platform_of_node) {
967 if (platform->dev->of_node !=
968 dai_link->platform_of_node)
969 continue;
970 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200971 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700972 continue;
973 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700974
975 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000977 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000978 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000980 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000982
983 card->num_rtd++;
984
985 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986}
987
Stephen Warrend12cd192012-06-08 12:34:22 -0600988static int soc_remove_platform(struct snd_soc_platform *platform)
989{
990 int ret;
991
992 if (platform->driver->remove) {
993 ret = platform->driver->remove(platform);
994 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000995 dev_err(platform->dev, "ASoC: failed to remove %d\n",
996 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600997 }
998
999 /* Make sure all DAPM widgets are freed */
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001000 snd_soc_dapm_free(&platform->component.dapm);
Stephen Warrend12cd192012-06-08 12:34:22 -06001001
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001002 soc_cleanup_component_debugfs(&platform->component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001003 platform->probed = 0;
Stephen Warrend12cd192012-06-08 12:34:22 -06001004 module_put(platform->dev->driver->owner);
1005
1006 return 0;
1007}
1008
Jarkko Nikula589c3562010-12-06 16:27:07 +02001009static void soc_remove_codec(struct snd_soc_codec *codec)
1010{
1011 int err;
1012
1013 if (codec->driver->remove) {
1014 err = codec->driver->remove(codec);
1015 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001016 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017 }
1018
1019 /* Make sure all DAPM widgets are freed */
1020 snd_soc_dapm_free(&codec->dapm);
1021
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001022 soc_cleanup_component_debugfs(&codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001023 codec->probed = 0;
1024 list_del(&codec->card_list);
1025 module_put(codec->dev->driver->owner);
1026}
1027
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001028static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1029{
1030 int err;
1031
1032 if (codec_dai && codec_dai->probed &&
1033 codec_dai->driver->remove_order == order) {
1034 if (codec_dai->driver->remove) {
1035 err = codec_dai->driver->remove(codec_dai);
1036 if (err < 0)
1037 dev_err(codec_dai->dev,
1038 "ASoC: failed to remove %s: %d\n",
1039 codec_dai->name, err);
1040 }
1041 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001042 }
1043}
1044
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001046{
1047 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001048 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1049 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001050
1051 /* unregister the rtd device */
1052 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001053 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1054 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1055 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 rtd->dev_registered = 0;
1057 }
1058
1059 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001060 for (i = 0; i < rtd->num_codecs; i++)
1061 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001063 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001064 if (cpu_dai && cpu_dai->probed &&
1065 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001066 if (cpu_dai->driver->remove) {
1067 err = cpu_dai->driver->remove(cpu_dai);
1068 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001069 dev_err(cpu_dai->dev,
1070 "ASoC: failed to remove %s: %d\n",
1071 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072 }
1073 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001074 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001075 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 }
1077}
1078
Stephen Warren62ae68f2012-06-08 12:34:23 -06001079static void soc_remove_link_components(struct snd_soc_card *card, int num,
1080 int order)
1081{
1082 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1083 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001084 struct snd_soc_platform *platform = rtd->platform;
1085 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001086 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001087
1088 /* remove the platform */
1089 if (platform && platform->probed &&
1090 platform->driver->remove_order == order) {
1091 soc_remove_platform(platform);
1092 }
1093
1094 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001095 for (i = 0; i < rtd->num_codecs; i++) {
1096 codec = rtd->codec_dais[i]->codec;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001097 if (codec && codec->probed &&
1098 codec->driver->remove_order == order)
1099 soc_remove_codec(codec);
1100 }
1101
1102 /* remove any CPU-side CODEC */
1103 if (cpu_dai) {
1104 codec = cpu_dai->codec;
1105 if (codec && codec->probed &&
1106 codec->driver->remove_order == order)
1107 soc_remove_codec(codec);
1108 }
1109}
1110
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001111static void soc_remove_dai_links(struct snd_soc_card *card)
1112{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001113 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001114
Liam Girdwood0168bf02011-06-07 16:08:05 +01001115 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1116 order++) {
1117 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001118 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001119 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001120
1121 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1122 order++) {
1123 for (dai = 0; dai < card->num_rtd; dai++)
1124 soc_remove_link_components(card, dai, order);
1125 }
1126
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001127 card->num_rtd = 0;
1128}
1129
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001130static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001131 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001132{
1133 int i;
1134
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001135 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001136 return;
1137
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001138 for (i = 0; i < card->num_configs; i++) {
1139 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001140 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001141 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001142 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001143 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001144 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001145 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001146 }
1147}
1148
Jarkko Nikula589c3562010-12-06 16:27:07 +02001149static int soc_probe_codec(struct snd_soc_card *card,
1150 struct snd_soc_codec *codec)
1151{
1152 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001153 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001154 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001155
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001156 codec->component.card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001157 codec->dapm.card = card;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001158 soc_set_name_prefix(card, &codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001159
Jarkko Nikula70d29332011-01-27 16:24:22 +02001160 if (!try_module_get(codec->dev->driver->owner))
1161 return -ENODEV;
1162
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001163 soc_init_component_debugfs(&codec->component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001164
Nariman Poushinb318ad52014-04-01 13:59:33 +01001165 if (driver->dapm_widgets) {
1166 ret = snd_soc_dapm_new_controls(&codec->dapm,
1167 driver->dapm_widgets,
1168 driver->num_dapm_widgets);
1169
1170 if (ret != 0) {
1171 dev_err(codec->dev,
1172 "Failed to create new controls %d\n", ret);
1173 goto err_probe;
1174 }
1175 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001176
Mark Brown888df392012-02-16 19:37:51 -08001177 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001178 list_for_each_entry(dai, &codec->component.dai_list, list) {
1179 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1180
1181 if (ret != 0) {
1182 dev_err(codec->dev,
1183 "Failed to create DAI widgets %d\n", ret);
1184 goto err_probe;
1185 }
1186 }
Mark Brown888df392012-02-16 19:37:51 -08001187
Mark Brown33c5f962011-08-22 18:40:30 +01001188 codec->dapm.idle_bias_off = driver->idle_bias_off;
1189
Mark Brown89b95ac02011-03-07 16:38:44 +00001190 if (driver->probe) {
1191 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001192 if (ret < 0) {
1193 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001194 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001195 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001196 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001197 WARN(codec->dapm.idle_bias_off &&
1198 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001199 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001200 codec->component.name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001201 }
1202
Mark Brownb7af1da2011-04-07 19:18:44 +09001203 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001204 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001205 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001206 if (driver->dapm_routes)
1207 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1208 driver->num_dapm_routes);
1209
Jarkko Nikula589c3562010-12-06 16:27:07 +02001210 /* mark codec as probed and add to card codec list */
1211 codec->probed = 1;
1212 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001213 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001214
Jarkko Nikula70d29332011-01-27 16:24:22 +02001215 return 0;
1216
1217err_probe:
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001218 soc_cleanup_component_debugfs(&codec->component);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001219 module_put(codec->dev->driver->owner);
1220
Jarkko Nikula589c3562010-12-06 16:27:07 +02001221 return ret;
1222}
1223
Liam Girdwood956245e2011-07-01 16:54:08 +01001224static int soc_probe_platform(struct snd_soc_card *card,
1225 struct snd_soc_platform *platform)
1226{
1227 int ret = 0;
1228 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001229 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001230 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001231
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001232 platform->component.card = card;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001233 platform->component.dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001234
1235 if (!try_module_get(platform->dev->driver->owner))
1236 return -ENODEV;
1237
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001238 soc_init_component_debugfs(&platform->component);
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001239
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001240 if (driver->dapm_widgets)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001241 snd_soc_dapm_new_controls(&platform->component.dapm,
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001242 driver->dapm_widgets, driver->num_dapm_widgets);
1243
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001244 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001245 list_for_each_entry(component, &component_list, list) {
1246 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001247 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001248 list_for_each_entry(dai, &component->dai_list, list)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001249 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1250 dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001251 }
1252
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001253 platform->component.dapm.idle_bias_off = 1;
Stephen Warren3fec6b62012-04-05 12:28:01 -06001254
Liam Girdwood956245e2011-07-01 16:54:08 +01001255 if (driver->probe) {
1256 ret = driver->probe(platform);
1257 if (ret < 0) {
1258 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001259 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001260 goto err_probe;
1261 }
1262 }
1263
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001264 if (driver->controls)
1265 snd_soc_add_platform_controls(platform, driver->controls,
1266 driver->num_controls);
1267 if (driver->dapm_routes)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001268 snd_soc_dapm_add_routes(&platform->component.dapm,
1269 driver->dapm_routes, driver->num_dapm_routes);
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001270
Liam Girdwood956245e2011-07-01 16:54:08 +01001271 /* mark platform as probed and add to card platform list */
1272 platform->probed = 1;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001273 list_add(&platform->component.dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001274
1275 return 0;
1276
1277err_probe:
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02001278 soc_cleanup_component_debugfs(&platform->component);
Liam Girdwood956245e2011-07-01 16:54:08 +01001279 module_put(platform->dev->driver->owner);
1280
1281 return ret;
1282}
1283
Mark Brown36ae1a92012-01-06 17:12:45 -08001284static void rtd_release(struct device *dev)
1285{
1286 kfree(dev);
1287}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001289static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1290 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001291{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001292 int ret = 0;
1293
Jarkko Nikula589c3562010-12-06 16:27:07 +02001294 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001295 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1296 if (!rtd->dev)
1297 return -ENOMEM;
1298 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001299 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001300 rtd->dev->release = rtd_release;
1301 rtd->dev->init_name = name;
1302 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001303 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001304 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1305 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1306 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1307 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001308 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001309 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001310 /* calling put_device() here to free the rtd->dev */
1311 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001312 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001313 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001314 return ret;
1315 }
1316 rtd->dev_registered = 1;
1317
1318 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001319 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001320 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001321 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001322 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001323
1324 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001325 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001326 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001327 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001328 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001329
1330 return 0;
1331}
1332
Stephen Warren62ae68f2012-06-08 12:34:23 -06001333static int soc_probe_link_components(struct snd_soc_card *card, int num,
1334 int order)
1335{
1336 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1337 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001338 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001339 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001340
1341 /* probe the CPU-side component, if it is a CODEC */
1342 if (cpu_dai->codec &&
1343 !cpu_dai->codec->probed &&
1344 cpu_dai->codec->driver->probe_order == order) {
1345 ret = soc_probe_codec(card, cpu_dai->codec);
1346 if (ret < 0)
1347 return ret;
1348 }
1349
Benoit Cousson88bd8702014-07-08 23:19:34 +02001350 /* probe the CODEC-side components */
1351 for (i = 0; i < rtd->num_codecs; i++) {
1352 if (!rtd->codec_dais[i]->codec->probed &&
1353 rtd->codec_dais[i]->codec->driver->probe_order == order) {
1354 ret = soc_probe_codec(card, rtd->codec_dais[i]->codec);
1355 if (ret < 0)
1356 return ret;
1357 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001358 }
1359
1360 /* probe the platform */
1361 if (!platform->probed &&
1362 platform->driver->probe_order == order) {
1363 ret = soc_probe_platform(card, platform);
1364 if (ret < 0)
1365 return ret;
1366 }
1367
1368 return 0;
1369}
1370
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001371static int soc_probe_codec_dai(struct snd_soc_card *card,
1372 struct snd_soc_dai *codec_dai,
1373 int order)
1374{
1375 int ret;
1376
1377 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1378 if (codec_dai->driver->probe) {
1379 ret = codec_dai->driver->probe(codec_dai);
1380 if (ret < 0) {
1381 dev_err(codec_dai->dev,
1382 "ASoC: failed to probe CODEC DAI %s: %d\n",
1383 codec_dai->name, ret);
1384 return ret;
1385 }
1386 }
1387
1388 /* mark codec_dai as probed and add to card dai list */
1389 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001390 }
1391
1392 return 0;
1393}
1394
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001395static int soc_link_dai_widgets(struct snd_soc_card *card,
1396 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001397 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001398{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001399 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1400 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001401 struct snd_soc_dapm_widget *play_w, *capture_w;
1402 int ret;
1403
Benoit Cousson88bd8702014-07-08 23:19:34 +02001404 if (rtd->num_codecs > 1)
1405 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1406
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001407 /* link the DAI widgets */
1408 play_w = codec_dai->playback_widget;
1409 capture_w = cpu_dai->capture_widget;
1410 if (play_w && capture_w) {
1411 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1412 capture_w, play_w);
1413 if (ret != 0) {
1414 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1415 play_w->name, capture_w->name, ret);
1416 return ret;
1417 }
1418 }
1419
1420 play_w = cpu_dai->playback_widget;
1421 capture_w = codec_dai->capture_widget;
1422 if (play_w && capture_w) {
1423 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1424 capture_w, play_w);
1425 if (ret != 0) {
1426 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1427 play_w->name, capture_w->name, ret);
1428 return ret;
1429 }
1430 }
1431
1432 return 0;
1433}
1434
Stephen Warren62ae68f2012-06-08 12:34:23 -06001435static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001436{
1437 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1438 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001439 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001440 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001441 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001442
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001443 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001444 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445
1446 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001447 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001449 for (i = 0; i < rtd->num_codecs; i++)
1450 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451
1452 /* set default power off timeout */
1453 rtd->pmdown_time = pmdown_time;
1454
1455 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001456 if (!cpu_dai->probed &&
1457 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001458 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001459 if (!try_module_get(cpu_dai->dev->driver->owner))
1460 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001461 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 if (cpu_dai->driver->probe) {
1464 ret = cpu_dai->driver->probe(cpu_dai);
1465 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001466 dev_err(cpu_dai->dev,
1467 "ASoC: failed to probe CPU DAI %s: %d\n",
1468 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001469 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470 return ret;
1471 }
1472 }
1473 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474 }
1475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001477 for (i = 0; i < rtd->num_codecs; i++) {
1478 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1479 if (ret)
1480 return ret;
1481 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001482
Liam Girdwood0168bf02011-06-07 16:08:05 +01001483 /* complete DAI probe during last probe */
1484 if (order != SND_SOC_COMP_ORDER_LAST)
1485 return 0;
1486
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001487 /* do machine specific initialization */
1488 if (dai_link->init) {
1489 ret = dai_link->init(rtd);
1490 if (ret < 0) {
1491 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1492 dai_link->name, ret);
1493 return ret;
1494 }
1495 }
1496
1497 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001498 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001500
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001501#ifdef CONFIG_DEBUG_FS
1502 /* add DPCM sysfs entries */
1503 if (dai_link->dynamic) {
1504 ret = soc_dpcm_debugfs_add(rtd);
1505 if (ret < 0) {
1506 dev_err(rtd->dev,
1507 "ASoC: failed to add dpcm sysfs entries: %d\n",
1508 ret);
1509 return ret;
1510 }
1511 }
1512#endif
1513
Mark Brown36ae1a92012-01-06 17:12:45 -08001514 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001516 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1517 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518
Namarta Kohli1245b702012-08-16 17:10:41 +05301519 if (cpu_dai->driver->compress_dai) {
1520 /*create compress_device"*/
1521 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001522 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001523 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301524 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001525 return ret;
1526 }
1527 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301528
1529 if (!dai_link->params) {
1530 /* create the pcm */
1531 ret = soc_new_pcm(rtd, num);
1532 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301534 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001535 return ret;
1536 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301537 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001538 INIT_DELAYED_WORK(&rtd->delayed_work,
1539 codec2codec_close_delayed_work);
1540
Namarta Kohli1245b702012-08-16 17:10:41 +05301541 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001542 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001543 if (ret)
1544 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001545 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001546 }
1547
1548 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001549 for (i = 0; i < rtd->num_codecs; i++) {
1550 if (rtd->codec_dais[i]->driver->ac97_control)
1551 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1552 rtd->cpu_dai->ac97_pdata);
1553 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554
1555 return 0;
1556}
1557
1558#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001559static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1560 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001561{
1562 int ret;
1563
1564 /* Only instantiate AC97 if not already done by the adaptor
1565 * for the generic AC97 subsystem.
1566 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001567 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001568 /*
1569 * It is possible that the AC97 device is already registered to
1570 * the device subsystem. This happens when the device is created
1571 * via snd_ac97_mixer(). Currently only SoC codec that does so
1572 * is the generic AC97 glue but others migh emerge.
1573 *
1574 * In those cases we don't try to register the device again.
1575 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001576 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001577 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001579 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001581 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001582 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583 return ret;
1584 }
1585
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001586 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 }
1588 return 0;
1589}
1590
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001591static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001592{
1593 if (codec->ac97_registered) {
1594 soc_ac97_dev_unregister(codec);
1595 codec->ac97_registered = 0;
1596 }
1597}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001598
Benoit Cousson88bd8702014-07-08 23:19:34 +02001599static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1600{
1601 int i, ret;
1602
1603 for (i = 0; i < rtd->num_codecs; i++) {
1604 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1605
1606 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1607 if (ret) {
1608 while (--i >= 0)
1609 soc_unregister_ac97_codec(codec_dai->codec);
1610 return ret;
1611 }
1612 }
1613
1614 return 0;
1615}
1616
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001617static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1618{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001619 int i;
1620
1621 for (i = 0; i < rtd->num_codecs; i++)
1622 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001623}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624#endif
1625
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001626static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001627{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001628 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001629 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1630 const char *codecname = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001631
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001632 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1633 if (!rtd->codec) {
1634 if (aux_dev->codec_of_node)
1635 codecname = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001636
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001637 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1638 return -EPROBE_DEFER;
1639 }
1640
1641 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001642}
1643
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001644static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1645{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001646 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001647 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001648 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001649
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001650 if (rtd->codec->probed) {
1651 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001652 return -EBUSY;
1653 }
1654
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001655 ret = soc_probe_codec(card, rtd->codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001656 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001657 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001658
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001659 /* do machine specific initialization */
1660 if (aux_dev->init) {
1661 ret = aux_dev->init(&rtd->codec->dapm);
1662 if (ret < 0) {
1663 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1664 aux_dev->name, ret);
1665 return ret;
1666 }
1667 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001668
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001669 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001670}
1671
1672static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1673{
1674 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1675 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676
1677 /* unregister the rtd device */
1678 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001679 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001680 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001681 rtd->dev_registered = 0;
1682 }
1683
Jarkko Nikula589c3562010-12-06 16:27:07 +02001684 if (codec && codec->probed)
1685 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001686}
1687
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001688static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001689{
1690 int ret;
1691
1692 if (codec->cache_init)
1693 return 0;
1694
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001695 ret = snd_soc_cache_init(codec);
1696 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001697 dev_err(codec->dev,
1698 "ASoC: Failed to set cache compression type: %d\n",
1699 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001700 return ret;
1701 }
1702 codec->cache_init = 1;
1703 return 0;
1704}
1705
Mark Brownb19e6e72012-03-14 21:18:39 +00001706static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001708 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001709 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001710 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711
Liam Girdwood01b9d992012-03-07 10:38:25 +00001712 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713
Mark Brownb19e6e72012-03-14 21:18:39 +00001714 /* bind DAIs */
1715 for (i = 0; i < card->num_links; i++) {
1716 ret = soc_bind_dai_link(card, i);
1717 if (ret != 0)
1718 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001719 }
1720
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001721 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001722 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001723 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001724 if (ret != 0)
1725 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 }
1727
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001728 /* initialize the register cache for each available codec */
1729 list_for_each_entry(codec, &codec_list, list) {
1730 if (codec->cache_init)
1731 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001732 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001733 if (ret < 0)
1734 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001735 }
1736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001738 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001739 card->owner, 0, &card->snd_card);
1740 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001741 dev_err(card->dev,
1742 "ASoC: can't create sound card for card %s: %d\n",
1743 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001744 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746
Mark Browne37a4972011-03-02 18:21:57 +00001747 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1748 card->dapm.dev = card->dev;
1749 card->dapm.card = card;
1750 list_add(&card->dapm.list, &card->dapm_list);
1751
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001752#ifdef CONFIG_DEBUG_FS
1753 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1754#endif
1755
Mark Brown88ee1c62011-02-02 10:43:26 +00001756#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001757 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001758 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001759#endif
Andy Green6ed25972008-06-13 16:24:05 +01001760
Mark Brown9a841eb2011-04-12 17:51:37 -07001761 if (card->dapm_widgets)
1762 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1763 card->num_dapm_widgets);
1764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 /* initialise the sound card only once */
1766 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001767 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 if (ret < 0)
1769 goto card_probe_error;
1770 }
1771
Stephen Warren62ae68f2012-06-08 12:34:23 -06001772 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001773 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1774 order++) {
1775 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001776 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001777 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001778 dev_err(card->dev,
1779 "ASoC: failed to instantiate card %d\n",
1780 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001781 goto probe_dai_err;
1782 }
1783 }
1784 }
1785
1786 /* probe all DAI links on this card */
1787 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1788 order++) {
1789 for (i = 0; i < card->num_links; i++) {
1790 ret = soc_probe_link_dais(card, i, order);
1791 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001792 dev_err(card->dev,
1793 "ASoC: failed to instantiate card %d\n",
1794 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001795 goto probe_dai_err;
1796 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797 }
1798 }
1799
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001800 for (i = 0; i < card->num_aux_devs; i++) {
1801 ret = soc_probe_aux_dev(card, i);
1802 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001803 dev_err(card->dev,
1804 "ASoC: failed to add auxiliary devices %d\n",
1805 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001806 goto probe_aux_dev_err;
1807 }
1808 }
1809
Mark Brown888df392012-02-16 19:37:51 -08001810 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001811 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001812
Mark Brownb7af1da2011-04-07 19:18:44 +09001813 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001814 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001815
Mark Brownb8ad29d2011-03-02 18:35:51 +00001816 if (card->dapm_routes)
1817 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1818 card->num_dapm_routes);
1819
Mark Brown75d9ac42011-09-27 16:41:01 +01001820 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001821 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001822 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001823 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001824
Mark Brownf04209a2012-04-09 16:29:21 +01001825 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001826 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1827 int j;
1828
1829 for (j = 0; j < rtd->num_codecs; j++) {
1830 struct snd_soc_dai *codec_dai = codec_dais[j];
1831
1832 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1833 if (ret != 0 && ret != -ENOTSUPP)
1834 dev_warn(codec_dai->dev,
1835 "ASoC: Failed to set DAI format: %d\n",
1836 ret);
1837 }
Mark Brownf04209a2012-04-09 16:29:21 +01001838 }
1839
1840 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001841 if (dai_fmt &&
1842 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001843 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1844 dai_fmt);
1845 if (ret != 0 && ret != -ENOTSUPP)
1846 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001847 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001848 ret);
1849 } else if (dai_fmt) {
1850 /* Flip the polarity for the "CPU" end */
1851 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1852 switch (dai_link->dai_fmt &
1853 SND_SOC_DAIFMT_MASTER_MASK) {
1854 case SND_SOC_DAIFMT_CBM_CFM:
1855 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1856 break;
1857 case SND_SOC_DAIFMT_CBM_CFS:
1858 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1859 break;
1860 case SND_SOC_DAIFMT_CBS_CFM:
1861 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1862 break;
1863 case SND_SOC_DAIFMT_CBS_CFS:
1864 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1865 break;
1866 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001867
1868 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001869 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001870 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001871 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001872 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001873 ret);
1874 }
1875 }
1876
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001877 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001878 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001879 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1880 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001881 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1882 "%s", card->driver_name ? card->driver_name : card->name);
1883 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1884 switch (card->snd_card->driver[i]) {
1885 case '_':
1886 case '-':
1887 case '\0':
1888 break;
1889 default:
1890 if (!isalnum(card->snd_card->driver[i]))
1891 card->snd_card->driver[i] = '_';
1892 break;
1893 }
1894 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001895
Mark Brown28e9ad92011-03-02 18:36:34 +00001896 if (card->late_probe) {
1897 ret = card->late_probe(card);
1898 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001899 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001900 card->name, ret);
1901 goto probe_aux_dev_err;
1902 }
1903 }
1904
Stephen Warren16332812011-11-23 12:42:04 -07001905 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001906 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001907
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001908 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910 ret = snd_card_register(card->snd_card);
1911 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001912 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1913 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001914 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001915 }
1916
1917#ifdef CONFIG_SND_SOC_AC97_BUS
1918 /* register any AC97 codecs */
1919 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001920 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1921 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001922 dev_err(card->dev,
1923 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001924 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001925 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001926 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001927 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001928 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001929#endif
1930
Mark Brown435c5e22008-12-04 15:32:53 +00001931 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001932 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001933 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001934
1935 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001937probe_aux_dev_err:
1938 for (i = 0; i < card->num_aux_devs; i++)
1939 soc_remove_aux_dev(card, i);
1940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001942 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001943
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001944card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001945 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001946 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001947
1948 snd_card_free(card->snd_card);
1949
Mark Brownb19e6e72012-03-14 21:18:39 +00001950base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001951 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001952
Mark Brownb19e6e72012-03-14 21:18:39 +00001953 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001954}
1955
1956/* probes a new socdev */
1957static int soc_probe(struct platform_device *pdev)
1958{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001959 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001960
Vinod Koul70a7ca32011-01-14 19:22:48 +05301961 /*
1962 * no card, so machine driver should be registering card
1963 * we should not be here in that case so ret error
1964 */
1965 if (!card)
1966 return -EINVAL;
1967
Mark Brownfe4085e2012-03-02 13:07:41 +00001968 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001969 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001970 card->name);
1971
Mark Brown435c5e22008-12-04 15:32:53 +00001972 /* Bodge while we unpick instantiation */
1973 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001974
Mark Brown28d528c2012-08-09 18:45:23 +01001975 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976}
1977
Vinod Koulb0e26482011-01-13 22:48:02 +05301978static int soc_cleanup_card_resources(struct snd_soc_card *card)
1979{
Vinod Koulb0e26482011-01-13 22:48:02 +05301980 int i;
1981
1982 /* make sure any delayed work runs */
1983 for (i = 0; i < card->num_rtd; i++) {
1984 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001985 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301986 }
1987
1988 /* remove auxiliary devices */
1989 for (i = 0; i < card->num_aux_devs; i++)
1990 soc_remove_aux_dev(card, i);
1991
1992 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001993 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301994
1995 soc_cleanup_card_debugfs(card);
1996
1997 /* remove the card */
1998 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001999 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302000
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002001 snd_soc_dapm_free(&card->dapm);
2002
Vinod Koulb0e26482011-01-13 22:48:02 +05302003 snd_card_free(card->snd_card);
2004 return 0;
2005
2006}
2007
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002008/* removes a socdev */
2009static int soc_remove(struct platform_device *pdev)
2010{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002011 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002012
Mark Brownc5af3a22008-11-28 13:29:45 +00002013 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 return 0;
2015}
2016
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002017int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002018{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002019 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 int i;
Mark Brown51737472009-06-22 13:16:51 +01002021
2022 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002023 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002024
2025 /* Flush out pmdown_time work - we actually do want to run it
2026 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002027 for (i = 0; i < card->num_rtd; i++) {
2028 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002029 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002030 }
Mark Brown51737472009-06-22 13:16:51 +01002031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002032 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002033
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002034 /* deactivate pins to sleep state */
2035 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002036 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2037 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2038 int j;
2039
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002040 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002041 for (j = 0; j < rtd->num_codecs; j++) {
2042 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2043 pinctrl_pm_select_sleep_state(codec_dai->dev);
2044 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002045 }
2046
Mark Brown416356f2009-06-30 19:05:15 +01002047 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002048}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002049EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002050
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002051const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302052 .suspend = snd_soc_suspend,
2053 .resume = snd_soc_resume,
2054 .freeze = snd_soc_suspend,
2055 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002056 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302057 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002058};
Stephen Warrendeb26072011-04-05 19:35:30 -06002059EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002060
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061/* ASoC platform driver */
2062static struct platform_driver soc_driver = {
2063 .driver = {
2064 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002065 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002066 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002067 },
2068 .probe = soc_probe,
2069 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002070};
2071
Mark Brown096e49d2009-07-05 15:12:22 +01002072/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002073 * snd_soc_new_ac97_codec - initailise AC97 device
2074 * @codec: audio codec
2075 * @ops: AC97 bus operations
2076 * @num: AC97 codec number
2077 *
2078 * Initialises AC97 codec resources for use by ad-hoc devices only.
2079 */
2080int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2081 struct snd_ac97_bus_ops *ops, int num)
2082{
2083 mutex_lock(&codec->mutex);
2084
2085 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2086 if (codec->ac97 == NULL) {
2087 mutex_unlock(&codec->mutex);
2088 return -ENOMEM;
2089 }
2090
2091 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2092 if (codec->ac97->bus == NULL) {
2093 kfree(codec->ac97);
2094 codec->ac97 = NULL;
2095 mutex_unlock(&codec->mutex);
2096 return -ENOMEM;
2097 }
2098
2099 codec->ac97->bus->ops = ops;
2100 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002101
2102 /*
2103 * Mark the AC97 device to be created by us. This way we ensure that the
2104 * device will be registered with the device subsystem later on.
2105 */
2106 codec->ac97_created = 1;
2107
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108 mutex_unlock(&codec->mutex);
2109 return 0;
2110}
2111EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2112
Markus Pargmann741a5092013-08-19 17:05:55 +02002113static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2114
2115static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2116{
2117 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2118
2119 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2120
2121 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2122
2123 udelay(10);
2124
2125 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2126
2127 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2128 msleep(2);
2129}
2130
2131static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2132{
2133 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2134
2135 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2136
2137 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2138 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2139 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2140
2141 udelay(10);
2142
2143 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2144
2145 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2146 msleep(2);
2147}
2148
2149static int snd_soc_ac97_parse_pinctl(struct device *dev,
2150 struct snd_ac97_reset_cfg *cfg)
2151{
2152 struct pinctrl *p;
2153 struct pinctrl_state *state;
2154 int gpio;
2155 int ret;
2156
2157 p = devm_pinctrl_get(dev);
2158 if (IS_ERR(p)) {
2159 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002160 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002161 }
2162 cfg->pctl = p;
2163
2164 state = pinctrl_lookup_state(p, "ac97-reset");
2165 if (IS_ERR(state)) {
2166 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002167 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002168 }
2169 cfg->pstate_reset = state;
2170
2171 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2172 if (IS_ERR(state)) {
2173 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002174 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002175 }
2176 cfg->pstate_warm_reset = state;
2177
2178 state = pinctrl_lookup_state(p, "ac97-running");
2179 if (IS_ERR(state)) {
2180 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002181 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002182 }
2183 cfg->pstate_run = state;
2184
2185 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2186 if (gpio < 0) {
2187 dev_err(dev, "Can't find ac97-sync gpio\n");
2188 return gpio;
2189 }
2190 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2191 if (ret) {
2192 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2193 return ret;
2194 }
2195 cfg->gpio_sync = gpio;
2196
2197 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2198 if (gpio < 0) {
2199 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2200 return gpio;
2201 }
2202 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2203 if (ret) {
2204 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2205 return ret;
2206 }
2207 cfg->gpio_sdata = gpio;
2208
2209 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2210 if (gpio < 0) {
2211 dev_err(dev, "Can't find ac97-reset gpio\n");
2212 return gpio;
2213 }
2214 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2215 if (ret) {
2216 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2217 return ret;
2218 }
2219 cfg->gpio_reset = gpio;
2220
2221 return 0;
2222}
2223
Mark Brownb047e1c2013-06-26 12:45:59 +01002224struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002225EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002226
2227int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2228{
2229 if (ops == soc_ac97_ops)
2230 return 0;
2231
2232 if (soc_ac97_ops && ops)
2233 return -EBUSY;
2234
2235 soc_ac97_ops = ops;
2236
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2240
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002242 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2243 *
2244 * This function sets the reset and warm_reset properties of ops and parses
2245 * the device node of pdev to get pinctrl states and gpio numbers to use.
2246 */
2247int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2248 struct platform_device *pdev)
2249{
2250 struct device *dev = &pdev->dev;
2251 struct snd_ac97_reset_cfg cfg;
2252 int ret;
2253
2254 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2255 if (ret)
2256 return ret;
2257
2258 ret = snd_soc_set_ac97_ops(ops);
2259 if (ret)
2260 return ret;
2261
2262 ops->warm_reset = snd_soc_ac97_warm_reset;
2263 ops->reset = snd_soc_ac97_reset;
2264
2265 snd_ac97_rst_cfg = cfg;
2266 return 0;
2267}
2268EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2269
2270/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271 * snd_soc_free_ac97_codec - free AC97 codec device
2272 * @codec: audio codec
2273 *
2274 * Frees AC97 codec device resources.
2275 */
2276void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2277{
2278 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002279#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002280 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002281#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 kfree(codec->ac97->bus);
2283 kfree(codec->ac97);
2284 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002285 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 mutex_unlock(&codec->mutex);
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2289
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002291 * snd_soc_cnew - create new control
2292 * @_template: control template
2293 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002294 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002295 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002296 *
2297 * Create a new mixer control from a template control.
2298 *
2299 * Returns 0 for success, else error.
2300 */
2301struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002302 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002303 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304{
2305 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002306 struct snd_kcontrol *kcontrol;
2307 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308
2309 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 template.index = 0;
2311
Mark Brownefb7ac32011-03-08 17:23:24 +00002312 if (!long_name)
2313 long_name = template.name;
2314
2315 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002316 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002317 if (!name)
2318 return NULL;
2319
Mark Brownefb7ac32011-03-08 17:23:24 +00002320 template.name = name;
2321 } else {
2322 template.name = long_name;
2323 }
2324
2325 kcontrol = snd_ctl_new1(&template, data);
2326
2327 kfree(name);
2328
2329 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330}
2331EXPORT_SYMBOL_GPL(snd_soc_cnew);
2332
Liam Girdwood022658b2012-02-03 17:43:09 +00002333static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2334 const struct snd_kcontrol_new *controls, int num_controls,
2335 const char *prefix, void *data)
2336{
2337 int err, i;
2338
2339 for (i = 0; i < num_controls; i++) {
2340 const struct snd_kcontrol_new *control = &controls[i];
2341 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2342 control->name, prefix));
2343 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002344 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2345 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002346 return err;
2347 }
2348 }
2349
2350 return 0;
2351}
2352
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002353struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2354 const char *name)
2355{
2356 struct snd_card *card = soc_card->snd_card;
2357 struct snd_kcontrol *kctl;
2358
2359 if (unlikely(!name))
2360 return NULL;
2361
2362 list_for_each_entry(kctl, &card->controls, list)
2363 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2364 return kctl;
2365 return NULL;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2368
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002370 * snd_soc_add_component_controls - Add an array of controls to a component.
2371 *
2372 * @component: Component to add controls to
2373 * @controls: Array of controls to add
2374 * @num_controls: Number of elements in the array
2375 *
2376 * Return: 0 for success, else error.
2377 */
2378int snd_soc_add_component_controls(struct snd_soc_component *component,
2379 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2380{
2381 struct snd_card *card = component->card->snd_card;
2382
2383 return snd_soc_add_controls(card, component->dev, controls,
2384 num_controls, component->name_prefix, component);
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2387
2388/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002389 * snd_soc_add_codec_controls - add an array of controls to a codec.
2390 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002391 * duplicating this code.
2392 *
2393 * @codec: codec to add controls to
2394 * @controls: array of controls to add
2395 * @num_controls: number of elements in the array
2396 *
2397 * Return 0 for success, else error.
2398 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002399int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002400 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002401{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002402 return snd_soc_add_component_controls(&codec->component, controls,
2403 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002404}
Liam Girdwood022658b2012-02-03 17:43:09 +00002405EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002406
2407/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002408 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002409 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002410 *
2411 * @platform: platform to add controls to
2412 * @controls: array of controls to add
2413 * @num_controls: number of elements in the array
2414 *
2415 * Return 0 for success, else error.
2416 */
2417int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002418 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002419{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002420 return snd_soc_add_component_controls(&platform->component, controls,
2421 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002422}
2423EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2424
2425/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002426 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2427 * Convenience function to add a list of controls.
2428 *
2429 * @soc_card: SoC card to add controls to
2430 * @controls: array of controls to add
2431 * @num_controls: number of elements in the array
2432 *
2433 * Return 0 for success, else error.
2434 */
2435int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2436 const struct snd_kcontrol_new *controls, int num_controls)
2437{
2438 struct snd_card *card = soc_card->snd_card;
2439
2440 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2441 NULL, soc_card);
2442}
2443EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2444
2445/**
2446 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2447 * Convienience function to add a list of controls.
2448 *
2449 * @dai: DAI to add controls to
2450 * @controls: array of controls to add
2451 * @num_controls: number of elements in the array
2452 *
2453 * Return 0 for success, else error.
2454 */
2455int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2456 const struct snd_kcontrol_new *controls, int num_controls)
2457{
2458 struct snd_card *card = dai->card->snd_card;
2459
2460 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2461 NULL, dai);
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2464
2465/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 * snd_soc_info_enum_double - enumerated double mixer info callback
2467 * @kcontrol: mixer control
2468 * @uinfo: control element information
2469 *
2470 * Callback to provide information about a double enumerated
2471 * mixer control.
2472 *
2473 * Returns 0 for success.
2474 */
2475int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_info *uinfo)
2477{
2478 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2479
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2481 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002482 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002483
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002484 if (uinfo->value.enumerated.item >= e->items)
2485 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002486 strlcpy(uinfo->value.enumerated.name,
2487 e->texts[uinfo->value.enumerated.item],
2488 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 return 0;
2490}
2491EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2492
2493/**
2494 * snd_soc_get_enum_double - enumerated double mixer get callback
2495 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002496 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497 *
2498 * Callback to get the value of a double enumerated mixer.
2499 *
2500 * Returns 0 for success.
2501 */
2502int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_value *ucontrol)
2504{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002505 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002507 unsigned int val, item;
2508 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002509 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002511 ret = snd_soc_component_read(component, e->reg, &reg_val);
2512 if (ret)
2513 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002514 val = (reg_val >> e->shift_l) & e->mask;
2515 item = snd_soc_enum_val_to_item(e, val);
2516 ucontrol->value.enumerated.item[0] = item;
2517 if (e->shift_l != e->shift_r) {
2518 val = (reg_val >> e->shift_l) & e->mask;
2519 item = snd_soc_enum_val_to_item(e, val);
2520 ucontrol->value.enumerated.item[1] = item;
2521 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002522
2523 return 0;
2524}
2525EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2526
2527/**
2528 * snd_soc_put_enum_double - enumerated double mixer put callback
2529 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002530 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531 *
2532 * Callback to set the value of a double enumerated mixer.
2533 *
2534 * Returns 0 for success.
2535 */
2536int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2538{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002539 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002541 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002542 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002543 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002545 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002546 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002547 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002548 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002550 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002552 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002553 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002554 }
2555
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002556 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002557}
2558EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2559
2560/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002561 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002562 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002563 * @reg: Register to read
2564 * @mask: Mask to use after shifting the register value
2565 * @shift: Right shift of register value
2566 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002567 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002568 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002569 * This functions reads a codec register. The register value is shifted right
2570 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2571 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002572 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002573 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002574 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002575static int snd_soc_read_signed(struct snd_soc_component *component,
2576 unsigned int reg, unsigned int mask, unsigned int shift,
2577 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002578{
Markus Pargmannf227b882014-01-16 16:02:10 +01002579 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002580 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002581
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002582 ret = snd_soc_component_read(component, reg, &val);
2583 if (ret < 0)
2584 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002585
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002586 val = (val >> shift) & mask;
2587
2588 if (!sign_bit) {
2589 *signed_val = val;
2590 return 0;
2591 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002592
2593 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002594 if (!(val & BIT(sign_bit))) {
2595 *signed_val = val;
2596 return 0;
2597 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002598
2599 ret = val;
2600
2601 /*
2602 * The register most probably does not contain a full-sized int.
2603 * Instead we have an arbitrary number of bits in a signed
2604 * representation which has to be translated into a full-sized int.
2605 * This is done by filling up all bits above the sign-bit.
2606 */
2607 ret |= ~((int)(BIT(sign_bit) - 1));
2608
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002609 *signed_val = ret;
2610
2611 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002612}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002613
2614/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002615 * snd_soc_info_volsw - single mixer info callback
2616 * @kcontrol: mixer control
2617 * @uinfo: control element information
2618 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002619 * Callback to provide information about a single mixer control, or a double
2620 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 *
2622 * Returns 0 for success.
2623 */
2624int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_info *uinfo)
2626{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002627 struct soc_mixer_control *mc =
2628 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002629 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002630
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002631 if (!mc->platform_max)
2632 mc->platform_max = mc->max;
2633 platform_max = mc->platform_max;
2634
2635 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002636 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2637 else
2638 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2639
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002640 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002642 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2646
2647/**
2648 * snd_soc_get_volsw - single mixer get callback
2649 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002650 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002652 * Callback to get the value of a single mixer control, or a double mixer
2653 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654 *
2655 * Returns 0 for success.
2656 */
2657int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2659{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002660 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002664 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002665 unsigned int shift = mc->shift;
2666 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002667 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002668 int min = mc->min;
2669 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002670 unsigned int mask = (1 << fls(max)) - 1;
2671 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002672 int val;
2673 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674
Markus Pargmannf227b882014-01-16 16:02:10 +01002675 if (sign_bit)
2676 mask = BIT(sign_bit + 1) - 1;
2677
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002678 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2679 if (ret)
2680 return ret;
2681
2682 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002683 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002684 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002685 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002686
2687 if (snd_soc_volsw_is_stereo(mc)) {
2688 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002689 ret = snd_soc_read_signed(component, reg, mask, rshift,
2690 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002691 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002692 ret = snd_soc_read_signed(component, reg2, mask, shift,
2693 sign_bit, &val);
2694 if (ret)
2695 return ret;
2696
2697 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002698 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002699 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002700 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002701 }
2702
2703 return 0;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2706
2707/**
2708 * snd_soc_put_volsw - single mixer put callback
2709 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002710 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002712 * Callback to set the value of a single mixer control, or a double mixer
2713 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2719{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002720 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002721 struct soc_mixer_control *mc =
2722 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002723 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002724 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002725 unsigned int shift = mc->shift;
2726 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002727 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002728 int min = mc->min;
2729 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002730 unsigned int mask = (1 << fls(max)) - 1;
2731 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002732 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002733 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002734 unsigned int val2 = 0;
2735 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736
Markus Pargmannf227b882014-01-16 16:02:10 +01002737 if (sign_bit)
2738 mask = BIT(sign_bit + 1) - 1;
2739
2740 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002741 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002742 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002743 val_mask = mask << shift;
2744 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002745 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002746 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002748 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002749 if (reg == reg2) {
2750 val_mask |= mask << rshift;
2751 val |= val2 << rshift;
2752 } else {
2753 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002754 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002755 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002756 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002757 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002758 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759 return err;
2760
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002761 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002762 err = snd_soc_component_update_bits(component, reg2, val_mask,
2763 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002764
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765 return err;
2766}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002767EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002768
Mark Browne13ac2e2008-05-28 17:58:05 +01002769/**
Brian Austin1d99f242012-03-30 10:43:55 -05002770 * snd_soc_get_volsw_sx - single mixer get callback
2771 * @kcontrol: mixer control
2772 * @ucontrol: control element information
2773 *
2774 * Callback to get the value of a single mixer control, or a double mixer
2775 * control that spans 2 registers.
2776 *
2777 * Returns 0 for success.
2778 */
2779int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_value *ucontrol)
2781{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002782 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002783 struct soc_mixer_control *mc =
2784 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002785 unsigned int reg = mc->reg;
2786 unsigned int reg2 = mc->rreg;
2787 unsigned int shift = mc->shift;
2788 unsigned int rshift = mc->rshift;
2789 int max = mc->max;
2790 int min = mc->min;
2791 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002792 unsigned int val;
2793 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002794
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002795 ret = snd_soc_component_read(component, reg, &val);
2796 if (ret < 0)
2797 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002798
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002799 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2800
2801 if (snd_soc_volsw_is_stereo(mc)) {
2802 ret = snd_soc_component_read(component, reg2, &val);
2803 if (ret < 0)
2804 return ret;
2805
2806 val = ((val >> rshift) - min) & mask;
2807 ucontrol->value.integer.value[1] = val;
2808 }
Brian Austin1d99f242012-03-30 10:43:55 -05002809
2810 return 0;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2813
2814/**
2815 * snd_soc_put_volsw_sx - double mixer set callback
2816 * @kcontrol: mixer control
2817 * @uinfo: control element information
2818 *
2819 * Callback to set the value of a double mixer control that spans 2 registers.
2820 *
2821 * Returns 0 for success.
2822 */
2823int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2825{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002826 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002827 struct soc_mixer_control *mc =
2828 (struct soc_mixer_control *)kcontrol->private_value;
2829
2830 unsigned int reg = mc->reg;
2831 unsigned int reg2 = mc->rreg;
2832 unsigned int shift = mc->shift;
2833 unsigned int rshift = mc->rshift;
2834 int max = mc->max;
2835 int min = mc->min;
2836 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002837 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002838 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002839
2840 val_mask = mask << shift;
2841 val = (ucontrol->value.integer.value[0] + min) & mask;
2842 val = val << shift;
2843
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002844 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302845 if (err < 0)
2846 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002847
2848 if (snd_soc_volsw_is_stereo(mc)) {
2849 val_mask = mask << rshift;
2850 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2851 val2 = val2 << rshift;
2852
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002853 err = snd_soc_component_update_bits(component, reg2, val_mask,
2854 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002855 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002857}
2858EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2859
2860/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002861 * snd_soc_info_volsw_s8 - signed mixer info callback
2862 * @kcontrol: mixer control
2863 * @uinfo: control element information
2864 *
2865 * Callback to provide information about a signed mixer control.
2866 *
2867 * Returns 0 for success.
2868 */
2869int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_info *uinfo)
2871{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002872 struct soc_mixer_control *mc =
2873 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002874 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002875 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002876
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002877 if (!mc->platform_max)
2878 mc->platform_max = mc->max;
2879 platform_max = mc->platform_max;
2880
Mark Browne13ac2e2008-05-28 17:58:05 +01002881 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2882 uinfo->count = 2;
2883 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002884 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002885 return 0;
2886}
2887EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2888
2889/**
2890 * snd_soc_get_volsw_s8 - signed mixer get callback
2891 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002892 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002893 *
2894 * Callback to get the value of a signed mixer control.
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002903 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002904 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002905 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002906 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002907 int ret;
2908
2909 ret = snd_soc_component_read(component, reg, &val);
2910 if (ret)
2911 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002912
2913 ucontrol->value.integer.value[0] =
2914 ((signed char)(val & 0xff))-min;
2915 ucontrol->value.integer.value[1] =
2916 ((signed char)((val >> 8) & 0xff))-min;
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2920
2921/**
2922 * snd_soc_put_volsw_sgn - signed mixer put callback
2923 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002924 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002925 *
2926 * Callback to set the value of a signed mixer control.
2927 *
2928 * Returns 0 for success.
2929 */
2930int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
2932{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002933 struct soc_mixer_control *mc =
2934 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002935 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002936 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002937 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002938 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002939
2940 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2941 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2942
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002943 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002944}
2945EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2946
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002947/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002948 * snd_soc_info_volsw_range - single mixer info callback with range.
2949 * @kcontrol: mixer control
2950 * @uinfo: control element information
2951 *
2952 * Callback to provide information, within a range, about a single
2953 * mixer control.
2954 *
2955 * returns 0 for success.
2956 */
2957int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_info *uinfo)
2959{
2960 struct soc_mixer_control *mc =
2961 (struct soc_mixer_control *)kcontrol->private_value;
2962 int platform_max;
2963 int min = mc->min;
2964
2965 if (!mc->platform_max)
2966 mc->platform_max = mc->max;
2967 platform_max = mc->platform_max;
2968
2969 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002970 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002971 uinfo->value.integer.min = 0;
2972 uinfo->value.integer.max = platform_max - min;
2973
2974 return 0;
2975}
2976EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2977
2978/**
2979 * snd_soc_put_volsw_range - single mixer put value callback with range.
2980 * @kcontrol: mixer control
2981 * @ucontrol: control element information
2982 *
2983 * Callback to set the value, within a range, for a single mixer control.
2984 *
2985 * Returns 0 for success.
2986 */
2987int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_value *ucontrol)
2989{
2990 struct soc_mixer_control *mc =
2991 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002992 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002993 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002994 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002995 unsigned int shift = mc->shift;
2996 int min = mc->min;
2997 int max = mc->max;
2998 unsigned int mask = (1 << fls(max)) - 1;
2999 unsigned int invert = mc->invert;
3000 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003001 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003002
3003 val = ((ucontrol->value.integer.value[0] + min) & mask);
3004 if (invert)
3005 val = max - val;
3006 val_mask = mask << shift;
3007 val = val << shift;
3008
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003009 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003010 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003011 return ret;
3012
3013 if (snd_soc_volsw_is_stereo(mc)) {
3014 val = ((ucontrol->value.integer.value[1] + min) & mask);
3015 if (invert)
3016 val = max - val;
3017 val_mask = mask << shift;
3018 val = val << shift;
3019
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003020 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3021 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003022 }
3023
3024 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003025}
3026EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3027
3028/**
3029 * snd_soc_get_volsw_range - single mixer get callback with range
3030 * @kcontrol: mixer control
3031 * @ucontrol: control element information
3032 *
3033 * Callback to get the value, within a range, of a single mixer control.
3034 *
3035 * Returns 0 for success.
3036 */
3037int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
3039{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003040 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003041 struct soc_mixer_control *mc =
3042 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003043 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003044 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003045 unsigned int shift = mc->shift;
3046 int min = mc->min;
3047 int max = mc->max;
3048 unsigned int mask = (1 << fls(max)) - 1;
3049 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003050 unsigned int val;
3051 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003052
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003053 ret = snd_soc_component_read(component, reg, &val);
3054 if (ret)
3055 return ret;
3056
3057 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003058 if (invert)
3059 ucontrol->value.integer.value[0] =
3060 max - ucontrol->value.integer.value[0];
3061 ucontrol->value.integer.value[0] =
3062 ucontrol->value.integer.value[0] - min;
3063
Mark Brown9bde4f02012-12-19 16:05:00 +00003064 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003065 ret = snd_soc_component_read(component, rreg, &val);
3066 if (ret)
3067 return ret;
3068
3069 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003070 if (invert)
3071 ucontrol->value.integer.value[1] =
3072 max - ucontrol->value.integer.value[1];
3073 ucontrol->value.integer.value[1] =
3074 ucontrol->value.integer.value[1] - min;
3075 }
3076
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003077 return 0;
3078}
3079EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3080
3081/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003082 * snd_soc_limit_volume - Set new limit to an existing volume control.
3083 *
3084 * @codec: where to look for the control
3085 * @name: Name of the control
3086 * @max: new maximum limit
3087 *
3088 * Return 0 for success, else error.
3089 */
3090int snd_soc_limit_volume(struct snd_soc_codec *codec,
3091 const char *name, int max)
3092{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003093 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003094 struct snd_kcontrol *kctl;
3095 struct soc_mixer_control *mc;
3096 int found = 0;
3097 int ret = -EINVAL;
3098
3099 /* Sanity check for name and max */
3100 if (unlikely(!name || max <= 0))
3101 return -EINVAL;
3102
3103 list_for_each_entry(kctl, &card->controls, list) {
3104 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3105 found = 1;
3106 break;
3107 }
3108 }
3109 if (found) {
3110 mc = (struct soc_mixer_control *)kctl->private_value;
3111 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003112 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003113 ret = 0;
3114 }
3115 }
3116 return ret;
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3119
Mark Brown71d08512011-10-10 18:31:26 +01003120int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3121 struct snd_ctl_elem_info *uinfo)
3122{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003123 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003124 struct soc_bytes *params = (void *)kcontrol->private_value;
3125
3126 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003127 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003128
3129 return 0;
3130}
3131EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3132
3133int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol)
3135{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003136 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003137 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003138 int ret;
3139
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003140 if (component->regmap)
3141 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003142 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003143 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003144 else
3145 ret = -EINVAL;
3146
Mark Brownf831b052012-02-17 16:20:33 -08003147 /* Hide any masked bytes to ensure consistent data reporting */
3148 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003149 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003150 case 1:
3151 ucontrol->value.bytes.data[0] &= ~params->mask;
3152 break;
3153 case 2:
3154 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003155 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003156 break;
3157 case 4:
3158 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003159 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003160 break;
3161 default:
3162 return -EINVAL;
3163 }
3164 }
3165
Mark Brown71d08512011-10-10 18:31:26 +01003166 return ret;
3167}
3168EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3169
3170int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
3172{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003173 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003174 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003175 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003176 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003177 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003178
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003179 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003180 return -EINVAL;
3181
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003182 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003183
Mark Brownb5a8fe42013-01-20 21:42:22 +09003184 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3185 if (!data)
3186 return -ENOMEM;
3187
Mark Brownf831b052012-02-17 16:20:33 -08003188 /*
3189 * If we've got a mask then we need to preserve the register
3190 * bits. We shouldn't modify the incoming data so take a
3191 * copy.
3192 */
3193 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003194 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003195 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003196 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003197
3198 val &= params->mask;
3199
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003200 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003201 case 1:
3202 ((u8 *)data)[0] &= ~params->mask;
3203 ((u8 *)data)[0] |= val;
3204 break;
3205 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003206 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003207 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003208 &mask, &mask);
3209 if (ret != 0)
3210 goto out;
3211
3212 ((u16 *)data)[0] &= mask;
3213
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003214 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003215 &val, &val);
3216 if (ret != 0)
3217 goto out;
3218
3219 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003220 break;
3221 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003222 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003223 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003224 &mask, &mask);
3225 if (ret != 0)
3226 goto out;
3227
3228 ((u32 *)data)[0] &= mask;
3229
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003230 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003231 &val, &val);
3232 if (ret != 0)
3233 goto out;
3234
3235 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003236 break;
3237 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003238 ret = -EINVAL;
3239 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003240 }
3241 }
3242
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003243 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003244 data, len);
3245
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003246out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003247 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003248
3249 return ret;
3250}
3251EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3252
Vinod Kould9881202014-05-02 10:58:11 +05303253int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3254 struct snd_ctl_elem_info *ucontrol)
3255{
3256 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3257
3258 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3259 ucontrol->count = params->max;
3260
3261 return 0;
3262}
3263EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3264
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303265int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3266 unsigned int size, unsigned int __user *tlv)
3267{
3268 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3269 unsigned int count = size < params->max ? size : params->max;
3270 int ret = -ENXIO;
3271
3272 switch (op_flag) {
3273 case SNDRV_CTL_TLV_OP_READ:
3274 if (params->get)
3275 ret = params->get(tlv, count);
3276 break;
3277 case SNDRV_CTL_TLV_OP_WRITE:
3278 if (params->put)
3279 ret = params->put(tlv, count);
3280 break;
3281 }
3282 return ret;
3283}
3284EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3285
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003286/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003287 * snd_soc_info_xr_sx - signed multi register info callback
3288 * @kcontrol: mreg control
3289 * @uinfo: control element information
3290 *
3291 * Callback to provide information of a control that can
3292 * span multiple codec registers which together
3293 * forms a single signed value in a MSB/LSB manner.
3294 *
3295 * Returns 0 for success.
3296 */
3297int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_info *uinfo)
3299{
3300 struct soc_mreg_control *mc =
3301 (struct soc_mreg_control *)kcontrol->private_value;
3302 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3303 uinfo->count = 1;
3304 uinfo->value.integer.min = mc->min;
3305 uinfo->value.integer.max = mc->max;
3306
3307 return 0;
3308}
3309EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3310
3311/**
3312 * snd_soc_get_xr_sx - signed multi register get callback
3313 * @kcontrol: mreg control
3314 * @ucontrol: control element information
3315 *
3316 * Callback to get the value of a control that can span
3317 * multiple codec registers which together forms a single
3318 * signed value in a MSB/LSB manner. The control supports
3319 * specifying total no of bits used to allow for bitfields
3320 * across the multiple codec registers.
3321 *
3322 * Returns 0 for success.
3323 */
3324int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
3326{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003327 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003328 struct soc_mreg_control *mc =
3329 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003330 unsigned int regbase = mc->regbase;
3331 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003332 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003333 unsigned int regwmask = (1<<regwshift)-1;
3334 unsigned int invert = mc->invert;
3335 unsigned long mask = (1UL<<mc->nbits)-1;
3336 long min = mc->min;
3337 long max = mc->max;
3338 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003339 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003340 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003341 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003342
3343 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003344 ret = snd_soc_component_read(component, regbase+i, &regval);
3345 if (ret)
3346 return ret;
3347 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003348 }
3349 val &= mask;
3350 if (min < 0 && val > max)
3351 val |= ~mask;
3352 if (invert)
3353 val = max - val;
3354 ucontrol->value.integer.value[0] = val;
3355
3356 return 0;
3357}
3358EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3359
3360/**
3361 * snd_soc_put_xr_sx - signed multi register get callback
3362 * @kcontrol: mreg control
3363 * @ucontrol: control element information
3364 *
3365 * Callback to set the value of a control that can span
3366 * multiple codec registers which together forms a single
3367 * signed value in a MSB/LSB manner. The control supports
3368 * specifying total no of bits used to allow for bitfields
3369 * across the multiple codec registers.
3370 *
3371 * Returns 0 for success.
3372 */
3373int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
3375{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003376 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003377 struct soc_mreg_control *mc =
3378 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003379 unsigned int regbase = mc->regbase;
3380 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003381 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003382 unsigned int regwmask = (1<<regwshift)-1;
3383 unsigned int invert = mc->invert;
3384 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003385 long max = mc->max;
3386 long val = ucontrol->value.integer.value[0];
3387 unsigned int i, regval, regmask;
3388 int err;
3389
3390 if (invert)
3391 val = max - val;
3392 val &= mask;
3393 for (i = 0; i < regcount; i++) {
3394 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3395 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003396 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003397 regmask, regval);
3398 if (err < 0)
3399 return err;
3400 }
3401
3402 return 0;
3403}
3404EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3405
3406/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003407 * snd_soc_get_strobe - strobe get callback
3408 * @kcontrol: mixer control
3409 * @ucontrol: control element information
3410 *
3411 * Callback get the value of a strobe mixer control.
3412 *
3413 * Returns 0 for success.
3414 */
3415int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3416 struct snd_ctl_elem_value *ucontrol)
3417{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003418 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003419 struct soc_mixer_control *mc =
3420 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003421 unsigned int reg = mc->reg;
3422 unsigned int shift = mc->shift;
3423 unsigned int mask = 1 << shift;
3424 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003425 unsigned int val;
3426 int ret;
3427
3428 ret = snd_soc_component_read(component, reg, &val);
3429 if (ret)
3430 return ret;
3431
3432 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003433
3434 if (shift != 0 && val != 0)
3435 val = val >> shift;
3436 ucontrol->value.enumerated.item[0] = val ^ invert;
3437
3438 return 0;
3439}
3440EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3441
3442/**
3443 * snd_soc_put_strobe - strobe put callback
3444 * @kcontrol: mixer control
3445 * @ucontrol: control element information
3446 *
3447 * Callback strobe a register bit to high then low (or the inverse)
3448 * in one pass of a single mixer enum control.
3449 *
3450 * Returns 1 for success.
3451 */
3452int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3453 struct snd_ctl_elem_value *ucontrol)
3454{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003455 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003456 struct soc_mixer_control *mc =
3457 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003458 unsigned int reg = mc->reg;
3459 unsigned int shift = mc->shift;
3460 unsigned int mask = 1 << shift;
3461 unsigned int invert = mc->invert != 0;
3462 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3463 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3464 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3465 int err;
3466
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003467 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003468 if (err < 0)
3469 return err;
3470
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003471 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003472}
3473EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3474
3475/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003476 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3477 * @dai: DAI
3478 * @clk_id: DAI specific clock ID
3479 * @freq: new clock frequency in Hz
3480 * @dir: new clock direction - input/output.
3481 *
3482 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3483 */
3484int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3485 unsigned int freq, int dir)
3486{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003487 if (dai->driver && dai->driver->ops->set_sysclk)
3488 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003489 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003490 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003491 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003493 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003494}
3495EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3496
3497/**
Mark Brownec4ee522011-03-07 20:58:11 +00003498 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3499 * @codec: CODEC
3500 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003501 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003502 * @freq: new clock frequency in Hz
3503 * @dir: new clock direction - input/output.
3504 *
3505 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3506 */
3507int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003508 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003509{
3510 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003511 return codec->driver->set_sysclk(codec, clk_id, source,
3512 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003513 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003514 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003515}
3516EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3517
3518/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003519 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3520 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003521 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003522 * @div: new clock divisor.
3523 *
3524 * Configures the clock dividers. This is used to derive the best DAI bit and
3525 * frame clocks from the system or master clock. It's best to set the DAI bit
3526 * and frame clocks as low as possible to save system power.
3527 */
3528int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3529 int div_id, int div)
3530{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003531 if (dai->driver && dai->driver->ops->set_clkdiv)
3532 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003533 else
3534 return -EINVAL;
3535}
3536EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3537
3538/**
3539 * snd_soc_dai_set_pll - configure DAI PLL.
3540 * @dai: DAI
3541 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003542 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003543 * @freq_in: PLL input clock frequency in Hz
3544 * @freq_out: requested PLL output clock frequency in Hz
3545 *
3546 * Configures and enables PLL to generate output clock based on input clock.
3547 */
Mark Brown85488032009-09-05 18:52:16 +01003548int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3549 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003550{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003551 if (dai->driver && dai->driver->ops->set_pll)
3552 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003553 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003554 else if (dai->codec && dai->codec->driver->set_pll)
3555 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3556 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003557 else
3558 return -EINVAL;
3559}
3560EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3561
Mark Brownec4ee522011-03-07 20:58:11 +00003562/*
3563 * snd_soc_codec_set_pll - configure codec PLL.
3564 * @codec: CODEC
3565 * @pll_id: DAI specific PLL ID
3566 * @source: DAI specific source for the PLL
3567 * @freq_in: PLL input clock frequency in Hz
3568 * @freq_out: requested PLL output clock frequency in Hz
3569 *
3570 * Configures and enables PLL to generate output clock based on input clock.
3571 */
3572int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3573 unsigned int freq_in, unsigned int freq_out)
3574{
3575 if (codec->driver->set_pll)
3576 return codec->driver->set_pll(codec, pll_id, source,
3577 freq_in, freq_out);
3578 else
3579 return -EINVAL;
3580}
3581EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3582
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003583/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003584 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3585 * @dai: DAI
3586 * @ratio Ratio of BCLK to Sample rate.
3587 *
3588 * Configures the DAI for a preset BCLK to sample rate ratio.
3589 */
3590int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3591{
3592 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3593 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3594 else
3595 return -EINVAL;
3596}
3597EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3598
3599/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003600 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3601 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003602 * @fmt: SND_SOC_DAIFMT_ format value.
3603 *
3604 * Configures the DAI hardware format and clocking.
3605 */
3606int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3607{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003608 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003609 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003610 if (dai->driver->ops->set_fmt == NULL)
3611 return -ENOTSUPP;
3612 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003613}
3614EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3615
3616/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003617 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003618 * @slots: Number of slots in use.
3619 * @tx_mask: bitmask representing active TX slots.
3620 * @rx_mask: bitmask representing active RX slots.
3621 *
3622 * Generates the TDM tx and rx slot default masks for DAI.
3623 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003624static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003625 unsigned int *tx_mask,
3626 unsigned int *rx_mask)
3627{
3628 if (*tx_mask || *rx_mask)
3629 return 0;
3630
3631 if (!slots)
3632 return -EINVAL;
3633
3634 *tx_mask = (1 << slots) - 1;
3635 *rx_mask = (1 << slots) - 1;
3636
3637 return 0;
3638}
3639
3640/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003641 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3642 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003643 * @tx_mask: bitmask representing active TX slots.
3644 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003645 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003646 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003647 *
3648 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3649 * specific.
3650 */
3651int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003652 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003653{
Xiubo Lie5c21512014-03-21 14:17:12 +08003654 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3655 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003656 &tx_mask, &rx_mask);
3657 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003658 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003659
Benoit Cousson88bd8702014-07-08 23:19:34 +02003660 dai->tx_mask = tx_mask;
3661 dai->rx_mask = rx_mask;
3662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003663 if (dai->driver && dai->driver->ops->set_tdm_slot)
3664 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003665 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003666 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003667 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003668}
3669EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3670
3671/**
Barry Song472df3c2009-09-12 01:16:29 +08003672 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3673 * @dai: DAI
3674 * @tx_num: how many TX channels
3675 * @tx_slot: pointer to an array which imply the TX slot number channel
3676 * 0~num-1 uses
3677 * @rx_num: how many RX channels
3678 * @rx_slot: pointer to an array which imply the RX slot number channel
3679 * 0~num-1 uses
3680 *
3681 * configure the relationship between channel number and TDM slot number.
3682 */
3683int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3684 unsigned int tx_num, unsigned int *tx_slot,
3685 unsigned int rx_num, unsigned int *rx_slot)
3686{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003687 if (dai->driver && dai->driver->ops->set_channel_map)
3688 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003689 rx_num, rx_slot);
3690 else
3691 return -EINVAL;
3692}
3693EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3694
3695/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003696 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3697 * @dai: DAI
3698 * @tristate: tristate enable
3699 *
3700 * Tristates the DAI so that others can use it.
3701 */
3702int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3703{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003704 if (dai->driver && dai->driver->ops->set_tristate)
3705 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003706 else
3707 return -EINVAL;
3708}
3709EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3710
3711/**
3712 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3713 * @dai: DAI
3714 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003715 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003716 *
3717 * Mutes the DAI DAC.
3718 */
Mark Brownda183962013-02-06 15:44:07 +00003719int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3720 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003721{
Mark Brownda183962013-02-06 15:44:07 +00003722 if (!dai->driver)
3723 return -ENOTSUPP;
3724
3725 if (dai->driver->ops->mute_stream)
3726 return dai->driver->ops->mute_stream(dai, mute, direction);
3727 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3728 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003729 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003730 else
Mark Brown04570c62012-04-13 19:16:03 +01003731 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003732}
3733EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3734
Benoit Cousson88bd8702014-07-08 23:19:34 +02003735static int snd_soc_init_multicodec(struct snd_soc_card *card,
3736 struct snd_soc_dai_link *dai_link)
3737{
3738 /* Legacy codec/codec_dai link is a single entry in multicodec */
3739 if (dai_link->codec_name || dai_link->codec_of_node ||
3740 dai_link->codec_dai_name) {
3741 dai_link->num_codecs = 1;
3742
3743 dai_link->codecs = devm_kzalloc(card->dev,
3744 sizeof(struct snd_soc_dai_link_component),
3745 GFP_KERNEL);
3746 if (!dai_link->codecs)
3747 return -ENOMEM;
3748
3749 dai_link->codecs[0].name = dai_link->codec_name;
3750 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3751 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3752 }
3753
3754 if (!dai_link->codecs) {
3755 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3756 return -EINVAL;
3757 }
3758
3759 return 0;
3760}
3761
Mark Brownc5af3a22008-11-28 13:29:45 +00003762/**
3763 * snd_soc_register_card - Register a card with the ASoC core
3764 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003765 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003766 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003767 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303768int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003769{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003770 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003771
Mark Brownc5af3a22008-11-28 13:29:45 +00003772 if (!card->name || !card->dev)
3773 return -EINVAL;
3774
Stephen Warren5a504962011-12-21 10:40:59 -07003775 for (i = 0; i < card->num_links; i++) {
3776 struct snd_soc_dai_link *link = &card->dai_link[i];
3777
Benoit Cousson88bd8702014-07-08 23:19:34 +02003778 ret = snd_soc_init_multicodec(card, link);
3779 if (ret) {
3780 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3781 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003782 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003783
3784 for (j = 0; j < link->num_codecs; j++) {
3785 /*
3786 * Codec must be specified by 1 of name or OF node,
3787 * not both or neither.
3788 */
3789 if (!!link->codecs[j].name ==
3790 !!link->codecs[j].of_node) {
3791 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3792 link->name);
3793 return -EINVAL;
3794 }
3795 /* Codec DAI name must be specified */
3796 if (!link->codecs[j].dai_name) {
3797 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3798 link->name);
3799 return -EINVAL;
3800 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003801 }
Stephen Warren5a504962011-12-21 10:40:59 -07003802
3803 /*
3804 * Platform may be specified by either name or OF node, but
3805 * can be left unspecified, and a dummy platform will be used.
3806 */
3807 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003808 dev_err(card->dev,
3809 "ASoC: Both platform name/of_node are set for %s\n",
3810 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003811 return -EINVAL;
3812 }
3813
3814 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003815 * CPU device may be specified by either name or OF node, but
3816 * can be left unspecified, and will be matched based on DAI
3817 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003818 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003819 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003820 dev_err(card->dev,
3821 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3822 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003823 return -EINVAL;
3824 }
3825 /*
3826 * At least one of CPU DAI name or CPU device name/node must be
3827 * specified
3828 */
3829 if (!link->cpu_dai_name &&
3830 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003831 dev_err(card->dev,
3832 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3833 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003834 return -EINVAL;
3835 }
3836 }
3837
Mark Browned77cc12011-05-03 18:25:34 +01003838 dev_set_drvdata(card->dev, card);
3839
Stephen Warren111c6412011-01-28 14:26:35 -07003840 snd_soc_initialize_card_lists(card);
3841
Vinod Koul150dd2f2011-01-13 22:48:32 +05303842 soc_init_card_debugfs(card);
3843
Mark Brown181a6892012-03-14 20:18:49 +00003844 card->rtd = devm_kzalloc(card->dev,
3845 sizeof(struct snd_soc_pcm_runtime) *
3846 (card->num_links + card->num_aux_devs),
3847 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003848 if (card->rtd == NULL)
3849 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003850 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003851 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003852
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003853 for (i = 0; i < card->num_links; i++) {
3854 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003855 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003856 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3857 sizeof(struct snd_soc_dai *) *
3858 (card->rtd[i].dai_link->num_codecs),
3859 GFP_KERNEL);
3860 if (card->rtd[i].codec_dais == NULL)
3861 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003862 }
3863
3864 for (i = 0; i < card->num_aux_devs; i++)
3865 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003866
Mark Browndb432b42011-10-03 21:06:40 +01003867 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003868 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003869 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003870 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003871
Mark Brownb19e6e72012-03-14 21:18:39 +00003872 ret = snd_soc_instantiate_card(card);
3873 if (ret != 0)
3874 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003875
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003876 /* deactivate pins to sleep state */
3877 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003878 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3879 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3880 int j;
3881
3882 for (j = 0; j < rtd->num_codecs; j++) {
3883 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3884 if (!codec_dai->active)
3885 pinctrl_pm_select_sleep_state(codec_dai->dev);
3886 }
3887
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003888 if (!cpu_dai->active)
3889 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3890 }
3891
Mark Brownb19e6e72012-03-14 21:18:39 +00003892 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003893}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303894EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003895
3896/**
3897 * snd_soc_unregister_card - Unregister a card with the ASoC core
3898 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003899 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003900 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003901 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303902int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003903{
Vinod Koulb0e26482011-01-13 22:48:02 +05303904 if (card->instantiated)
3905 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003906 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003907
3908 return 0;
3909}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303910EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003911
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003912/*
3913 * Simplify DAI link configuration by removing ".-1" from device names
3914 * and sanitizing names.
3915 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003916static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003917{
3918 char *found, name[NAME_SIZE];
3919 int id1, id2;
3920
3921 if (dev_name(dev) == NULL)
3922 return NULL;
3923
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003924 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925
3926 /* are we a "%s.%d" name (platform and SPI components) */
3927 found = strstr(name, dev->driver->name);
3928 if (found) {
3929 /* get ID */
3930 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3931
3932 /* discard ID from name if ID == -1 */
3933 if (*id == -1)
3934 found[strlen(dev->driver->name)] = '\0';
3935 }
3936
3937 } else {
3938 /* I2C component devices are named "bus-addr" */
3939 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3940 char tmp[NAME_SIZE];
3941
3942 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003943 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003944
3945 /* sanitize component name for DAI link creation */
3946 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003947 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003948 } else
3949 *id = 0;
3950 }
3951
3952 return kstrdup(name, GFP_KERNEL);
3953}
3954
3955/*
3956 * Simplify DAI link naming for single devices with multiple DAIs by removing
3957 * any ".-1" and using the DAI name (instead of device name).
3958 */
3959static inline char *fmt_multiple_name(struct device *dev,
3960 struct snd_soc_dai_driver *dai_drv)
3961{
3962 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003963 dev_err(dev,
3964 "ASoC: error - multiple DAI %s registered with no name\n",
3965 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003966 return NULL;
3967 }
3968
3969 return kstrdup(dai_drv->name, GFP_KERNEL);
3970}
3971
Mark Brown91151712008-11-30 23:31:24 +00003972/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003973 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003974 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003975 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003976 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003977static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003978{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003979 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003980
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003981 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003982 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3983 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003984 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003985 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003986 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003987 }
Mark Brown91151712008-11-30 23:31:24 +00003988}
Mark Brown91151712008-11-30 23:31:24 +00003989
3990/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003991 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003992 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003993 * @component: The component the DAIs are registered for
3994 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003995 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003996 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3997 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003998 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003999static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004000 struct snd_soc_dai_driver *dai_drv, size_t count,
4001 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00004002{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004003 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004005 unsigned int i;
4006 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004008 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00004009
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004010 component->dai_drv = dai_drv;
4011 component->num_dai = count;
4012
Mark Brown91151712008-11-30 23:31:24 +00004013 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004014
4015 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08004016 if (dai == NULL) {
4017 ret = -ENOMEM;
4018 goto err;
4019 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004020
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004021 /*
4022 * Back in the old days when we still had component-less DAIs,
4023 * instead of having a static name, component-less DAIs would
4024 * inherit the name of the parent device so it is possible to
4025 * register multiple instances of the DAI. We still need to keep
4026 * the same naming style even though those DAIs are not
4027 * component-less anymore.
4028 */
4029 if (count == 1 && legacy_dai_naming) {
4030 dai->name = fmt_single_name(dev, &dai->id);
4031 } else {
4032 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4033 if (dai_drv[i].id)
4034 dai->id = dai_drv[i].id;
4035 else
4036 dai->id = i;
4037 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004038 if (dai->name == NULL) {
4039 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004040 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004041 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004042 }
4043
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004044 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004045 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004046 dai->driver = &dai_drv[i];
4047 if (!dai->driver->ops)
4048 dai->driver->ops = &null_dai_ops;
4049
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004050 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004051
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004052 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004053 }
4054
4055 return 0;
4056
4057err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004058 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004059
4060 return ret;
4061}
Mark Brown91151712008-11-30 23:31:24 +00004062
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004063static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4064 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004065{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004066 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004067
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004068 component->driver->seq_notifier(component, type, subseq);
4069}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004070
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004071static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4072 int event)
4073{
4074 struct snd_soc_component *component = dapm->component;
4075
4076 return component->driver->stream_event(component, event);
4077}
4078
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004079static int snd_soc_component_initialize(struct snd_soc_component *component,
4080 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004081{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004082 struct snd_soc_dapm_context *dapm;
4083
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004084 component->name = fmt_single_name(dev, &component->id);
4085 if (!component->name) {
4086 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004087 return -ENOMEM;
4088 }
4089
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004090 component->dev = dev;
4091 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004092
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004093 if (!component->dapm_ptr)
4094 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004095
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004096 dapm = component->dapm_ptr;
4097 dapm->dev = dev;
4098 dapm->component = component;
4099 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004100 if (driver->seq_notifier)
4101 dapm->seq_notifier = snd_soc_component_seq_notifier;
4102 if (driver->stream_event)
4103 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004104
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004105 INIT_LIST_HEAD(&component->dai_list);
4106 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004107
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004108 return 0;
4109}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004110
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004111static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4112{
4113 list_add(&component->list, &component_list);
4114}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004115
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004116static void snd_soc_component_add(struct snd_soc_component *component)
4117{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004118 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004119 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004120 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004121}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004122
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004123static void snd_soc_component_cleanup(struct snd_soc_component *component)
4124{
4125 snd_soc_unregister_dais(component);
4126 kfree(component->name);
4127}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004128
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004129static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4130{
4131 list_del(&component->list);
4132}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004133
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004134static void snd_soc_component_del(struct snd_soc_component *component)
4135{
4136 mutex_lock(&client_mutex);
4137 snd_soc_component_del_unlocked(component);
4138 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004139}
4140
4141int snd_soc_register_component(struct device *dev,
4142 const struct snd_soc_component_driver *cmpnt_drv,
4143 struct snd_soc_dai_driver *dai_drv,
4144 int num_dai)
4145{
4146 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004147 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004148
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004149 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004150 if (!cmpnt) {
4151 dev_err(dev, "ASoC: Failed to allocate memory\n");
4152 return -ENOMEM;
4153 }
4154
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004155 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4156 if (ret)
4157 goto err_free;
4158
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004159 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004160 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004161
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004162 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4163 if (ret < 0) {
4164 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4165 goto err_cleanup;
4166 }
4167
4168 snd_soc_component_add(cmpnt);
4169
4170 return 0;
4171
4172err_cleanup:
4173 snd_soc_component_cleanup(cmpnt);
4174err_free:
4175 kfree(cmpnt);
4176 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004177}
4178EXPORT_SYMBOL_GPL(snd_soc_register_component);
4179
4180/**
4181 * snd_soc_unregister_component - Unregister a component from the ASoC core
4182 *
4183 */
4184void snd_soc_unregister_component(struct device *dev)
4185{
4186 struct snd_soc_component *cmpnt;
4187
4188 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004189 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004190 goto found;
4191 }
4192 return;
4193
4194found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004195 snd_soc_component_del(cmpnt);
4196 snd_soc_component_cleanup(cmpnt);
4197 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004198}
4199EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4200
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004201static int snd_soc_platform_drv_write(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 return platform->driver->write(platform, reg, val);
4207}
4208
4209static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4210 unsigned int reg, unsigned int *val)
4211{
4212 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4213
4214 *val = platform->driver->read(platform, reg);
4215
4216 return 0;
4217}
4218
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004219/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004220 * snd_soc_add_platform - Add a platform to the ASoC core
4221 * @dev: The parent device for the platform
4222 * @platform: The platform to add
4223 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004224 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004225int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004226 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004227{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004228 int ret;
4229
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004230 ret = snd_soc_component_initialize(&platform->component,
4231 &platform_drv->component_driver, dev);
4232 if (ret)
4233 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004234
4235 platform->dev = dev;
4236 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004237 if (platform_drv->write)
4238 platform->component.write = snd_soc_platform_drv_write;
4239 if (platform_drv->read)
4240 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004241
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004242#ifdef CONFIG_DEBUG_FS
4243 platform->component.debugfs_prefix = "platform";
4244#endif
4245
Mark Brown12a48a8c2008-12-03 19:40:30 +00004246 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004247 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004248 list_add(&platform->list, &platform_list);
4249 mutex_unlock(&client_mutex);
4250
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004251 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4252 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004253
4254 return 0;
4255}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004256EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4257
4258/**
4259 * snd_soc_register_platform - Register a platform with the ASoC core
4260 *
4261 * @platform: platform to register
4262 */
4263int snd_soc_register_platform(struct device *dev,
4264 const struct snd_soc_platform_driver *platform_drv)
4265{
4266 struct snd_soc_platform *platform;
4267 int ret;
4268
4269 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4270
4271 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4272 if (platform == NULL)
4273 return -ENOMEM;
4274
4275 ret = snd_soc_add_platform(dev, platform, platform_drv);
4276 if (ret)
4277 kfree(platform);
4278
4279 return ret;
4280}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004281EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4282
4283/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004284 * snd_soc_remove_platform - Remove a platform from the ASoC core
4285 * @platform: the platform to remove
4286 */
4287void snd_soc_remove_platform(struct snd_soc_platform *platform)
4288{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004289
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004290 mutex_lock(&client_mutex);
4291 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004292 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004293 mutex_unlock(&client_mutex);
4294
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004295 snd_soc_component_cleanup(&platform->component);
4296
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004297 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004298 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004299}
4300EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4301
4302struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4303{
4304 struct snd_soc_platform *platform;
4305
4306 list_for_each_entry(platform, &platform_list, list) {
4307 if (dev == platform->dev)
4308 return platform;
4309 }
4310
4311 return NULL;
4312}
4313EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4314
4315/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004316 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4317 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004318 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004319 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004320void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004321{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004322 struct snd_soc_platform *platform;
4323
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004324 platform = snd_soc_lookup_platform(dev);
4325 if (!platform)
4326 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004327
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004328 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004329 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004330}
4331EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4332
Mark Brown151ab222009-05-09 16:22:58 +01004333static u64 codec_format_map[] = {
4334 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4335 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4336 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4337 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4338 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4339 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4340 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4341 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4342 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4343 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4344 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4345 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4346 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4347 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4348 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4349 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4350};
4351
4352/* Fix up the DAI formats for endianness: codecs don't actually see
4353 * the endianness of the data but we're using the CPU format
4354 * definitions which do need to include endianness so we ensure that
4355 * codec DAIs always have both big and little endian variants set.
4356 */
4357static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4358{
4359 int i;
4360
4361 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4362 if (stream->formats & codec_format_map[i])
4363 stream->formats |= codec_format_map[i];
4364}
4365
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004366static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4367 unsigned int reg, unsigned int val)
4368{
4369 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4370
4371 return codec->driver->write(codec, reg, val);
4372}
4373
4374static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4375 unsigned int reg, unsigned int *val)
4376{
4377 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4378
4379 *val = codec->driver->read(codec, reg);
4380
4381 return 0;
4382}
4383
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004384static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4385 enum snd_soc_bias_level level)
4386{
4387 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4388
4389 return codec->driver->set_bias_level(codec, level);
4390}
4391
Mark Brown0d0cf002008-12-10 14:32:45 +00004392/**
4393 * snd_soc_register_codec - Register a codec with the ASoC core
4394 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004395 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004396 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004397int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004398 const struct snd_soc_codec_driver *codec_drv,
4399 struct snd_soc_dai_driver *dai_drv,
4400 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004401{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004402 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004403 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004404 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004405 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004406
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004407 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004408
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004409 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4410 if (codec == NULL)
4411 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004412
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004413 codec->component.dapm_ptr = &codec->dapm;
4414
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004415 ret = snd_soc_component_initialize(&codec->component,
4416 &codec_drv->component_driver, dev);
4417 if (ret)
4418 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004419
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004420 if (codec_drv->write)
4421 codec->component.write = snd_soc_codec_drv_write;
4422 if (codec_drv->read)
4423 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004424 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004425 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004426 if (codec_drv->seq_notifier)
4427 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004428 if (codec_drv->set_bias_level)
4429 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004430 codec->dev = dev;
4431 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004432 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004433 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004434
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004435#ifdef CONFIG_DEBUG_FS
4436 codec->component.init_debugfs = soc_init_codec_debugfs;
4437 codec->component.debugfs_prefix = "codec";
4438#endif
4439
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004440 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004441 if (codec_drv->get_regmap)
4442 regmap = codec_drv->get_regmap(dev);
4443 else
4444 regmap = dev_get_regmap(dev, NULL);
4445
4446 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004447 ret = snd_soc_component_init_io(&codec->component,
4448 regmap);
4449 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004450 dev_err(codec->dev,
4451 "Failed to set cache I/O:%d\n",
4452 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004453 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004454 }
4455 }
4456 }
4457
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004458 for (i = 0; i < num_dai; i++) {
4459 fixup_codec_formats(&dai_drv[i].playback);
4460 fixup_codec_formats(&dai_drv[i].capture);
4461 }
4462
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004463 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4464 if (ret < 0) {
4465 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4466 goto err_cleanup;
4467 }
4468
4469 list_for_each_entry(dai, &codec->component.dai_list, list)
4470 dai->codec = codec;
4471
Mark Brown054880f2012-04-09 17:29:19 +01004472 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004473 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004474 list_add(&codec->list, &codec_list);
4475 mutex_unlock(&client_mutex);
4476
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004477 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4478 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004479 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004480
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004481err_cleanup:
4482 snd_soc_component_cleanup(&codec->component);
4483err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004484 kfree(codec);
4485 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004486}
4487EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4488
4489/**
4490 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4491 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004492 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004493 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004494void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004495{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004496 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004497
4498 list_for_each_entry(codec, &codec_list, list) {
4499 if (dev == codec->dev)
4500 goto found;
4501 }
4502 return;
4503
4504found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004505
Mark Brown0d0cf002008-12-10 14:32:45 +00004506 mutex_lock(&client_mutex);
4507 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004508 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004509 mutex_unlock(&client_mutex);
4510
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004511 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4512 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004513
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004514 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004515 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004516 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004517}
4518EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4519
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004520/* Retrieve a card's name from device tree */
4521int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4522 const char *propname)
4523{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304524 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004525 int ret;
4526
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304527 if (!card->dev) {
4528 pr_err("card->dev is not set before calling %s\n", __func__);
4529 return -EINVAL;
4530 }
4531
4532 np = card->dev->of_node;
4533
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004534 ret = of_property_read_string_index(np, propname, 0, &card->name);
4535 /*
4536 * EINVAL means the property does not exist. This is fine providing
4537 * card->name was previously set, which is checked later in
4538 * snd_soc_register_card.
4539 */
4540 if (ret < 0 && ret != -EINVAL) {
4541 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004542 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004543 propname, ret);
4544 return ret;
4545 }
4546
4547 return 0;
4548}
4549EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4550
Xiubo Li9a6d4862014-02-08 15:59:52 +08004551static const struct snd_soc_dapm_widget simple_widgets[] = {
4552 SND_SOC_DAPM_MIC("Microphone", NULL),
4553 SND_SOC_DAPM_LINE("Line", NULL),
4554 SND_SOC_DAPM_HP("Headphone", NULL),
4555 SND_SOC_DAPM_SPK("Speaker", NULL),
4556};
4557
4558int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4559 const char *propname)
4560{
4561 struct device_node *np = card->dev->of_node;
4562 struct snd_soc_dapm_widget *widgets;
4563 const char *template, *wname;
4564 int i, j, num_widgets, ret;
4565
4566 num_widgets = of_property_count_strings(np, propname);
4567 if (num_widgets < 0) {
4568 dev_err(card->dev,
4569 "ASoC: Property '%s' does not exist\n", propname);
4570 return -EINVAL;
4571 }
4572 if (num_widgets & 1) {
4573 dev_err(card->dev,
4574 "ASoC: Property '%s' length is not even\n", propname);
4575 return -EINVAL;
4576 }
4577
4578 num_widgets /= 2;
4579 if (!num_widgets) {
4580 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4581 propname);
4582 return -EINVAL;
4583 }
4584
4585 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4586 GFP_KERNEL);
4587 if (!widgets) {
4588 dev_err(card->dev,
4589 "ASoC: Could not allocate memory for widgets\n");
4590 return -ENOMEM;
4591 }
4592
4593 for (i = 0; i < num_widgets; i++) {
4594 ret = of_property_read_string_index(np, propname,
4595 2 * i, &template);
4596 if (ret) {
4597 dev_err(card->dev,
4598 "ASoC: Property '%s' index %d read error:%d\n",
4599 propname, 2 * i, ret);
4600 return -EINVAL;
4601 }
4602
4603 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4604 if (!strncmp(template, simple_widgets[j].name,
4605 strlen(simple_widgets[j].name))) {
4606 widgets[i] = simple_widgets[j];
4607 break;
4608 }
4609 }
4610
4611 if (j >= ARRAY_SIZE(simple_widgets)) {
4612 dev_err(card->dev,
4613 "ASoC: DAPM widget '%s' is not supported\n",
4614 template);
4615 return -EINVAL;
4616 }
4617
4618 ret = of_property_read_string_index(np, propname,
4619 (2 * i) + 1,
4620 &wname);
4621 if (ret) {
4622 dev_err(card->dev,
4623 "ASoC: Property '%s' index %d read error:%d\n",
4624 propname, (2 * i) + 1, ret);
4625 return -EINVAL;
4626 }
4627
4628 widgets[i].name = wname;
4629 }
4630
4631 card->dapm_widgets = widgets;
4632 card->num_dapm_widgets = num_widgets;
4633
4634 return 0;
4635}
4636EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4637
Xiubo Li89c67852014-02-14 09:34:35 +08004638int snd_soc_of_parse_tdm_slot(struct device_node *np,
4639 unsigned int *slots,
4640 unsigned int *slot_width)
4641{
4642 u32 val;
4643 int ret;
4644
4645 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4646 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4647 if (ret)
4648 return ret;
4649
4650 if (slots)
4651 *slots = val;
4652 }
4653
4654 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4655 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4656 if (ret)
4657 return ret;
4658
4659 if (slot_width)
4660 *slot_width = val;
4661 }
4662
4663 return 0;
4664}
4665EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4666
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004667int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4668 const char *propname)
4669{
4670 struct device_node *np = card->dev->of_node;
4671 int num_routes;
4672 struct snd_soc_dapm_route *routes;
4673 int i, ret;
4674
4675 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004676 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004677 dev_err(card->dev,
4678 "ASoC: Property '%s' does not exist or its length is not even\n",
4679 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004680 return -EINVAL;
4681 }
4682 num_routes /= 2;
4683 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004684 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004685 propname);
4686 return -EINVAL;
4687 }
4688
4689 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4690 GFP_KERNEL);
4691 if (!routes) {
4692 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004693 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004694 return -EINVAL;
4695 }
4696
4697 for (i = 0; i < num_routes; i++) {
4698 ret = of_property_read_string_index(np, propname,
4699 2 * i, &routes[i].sink);
4700 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004701 dev_err(card->dev,
4702 "ASoC: Property '%s' index %d could not be read: %d\n",
4703 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004704 return -EINVAL;
4705 }
4706 ret = of_property_read_string_index(np, propname,
4707 (2 * i) + 1, &routes[i].source);
4708 if (ret) {
4709 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004710 "ASoC: Property '%s' index %d could not be read: %d\n",
4711 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004712 return -EINVAL;
4713 }
4714 }
4715
4716 card->num_dapm_routes = num_routes;
4717 card->dapm_routes = routes;
4718
4719 return 0;
4720}
4721EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4722
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004723unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004724 const char *prefix,
4725 struct device_node **bitclkmaster,
4726 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004727{
4728 int ret, i;
4729 char prop[128];
4730 unsigned int format = 0;
4731 int bit, frame;
4732 const char *str;
4733 struct {
4734 char *name;
4735 unsigned int val;
4736 } of_fmt_table[] = {
4737 { "i2s", SND_SOC_DAIFMT_I2S },
4738 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4739 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4740 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4741 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4742 { "ac97", SND_SOC_DAIFMT_AC97 },
4743 { "pdm", SND_SOC_DAIFMT_PDM},
4744 { "msb", SND_SOC_DAIFMT_MSB },
4745 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004746 };
4747
4748 if (!prefix)
4749 prefix = "";
4750
4751 /*
4752 * check "[prefix]format = xxx"
4753 * SND_SOC_DAIFMT_FORMAT_MASK area
4754 */
4755 snprintf(prop, sizeof(prop), "%sformat", prefix);
4756 ret = of_property_read_string(np, prop, &str);
4757 if (ret == 0) {
4758 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4759 if (strcmp(str, of_fmt_table[i].name) == 0) {
4760 format |= of_fmt_table[i].val;
4761 break;
4762 }
4763 }
4764 }
4765
4766 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004767 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004768 * SND_SOC_DAIFMT_CLOCK_MASK area
4769 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004770 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4771 if (of_get_property(np, prop, NULL))
4772 format |= SND_SOC_DAIFMT_CONT;
4773 else
4774 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004775
4776 /*
4777 * check "[prefix]bitclock-inversion"
4778 * check "[prefix]frame-inversion"
4779 * SND_SOC_DAIFMT_INV_MASK area
4780 */
4781 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4782 bit = !!of_get_property(np, prop, NULL);
4783
4784 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4785 frame = !!of_get_property(np, prop, NULL);
4786
4787 switch ((bit << 4) + frame) {
4788 case 0x11:
4789 format |= SND_SOC_DAIFMT_IB_IF;
4790 break;
4791 case 0x10:
4792 format |= SND_SOC_DAIFMT_IB_NF;
4793 break;
4794 case 0x01:
4795 format |= SND_SOC_DAIFMT_NB_IF;
4796 break;
4797 default:
4798 /* SND_SOC_DAIFMT_NB_NF is default */
4799 break;
4800 }
4801
4802 /*
4803 * check "[prefix]bitclock-master"
4804 * check "[prefix]frame-master"
4805 * SND_SOC_DAIFMT_MASTER_MASK area
4806 */
4807 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4808 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004809 if (bit && bitclkmaster)
4810 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004811
4812 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4813 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004814 if (frame && framemaster)
4815 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004816
4817 switch ((bit << 4) + frame) {
4818 case 0x11:
4819 format |= SND_SOC_DAIFMT_CBM_CFM;
4820 break;
4821 case 0x10:
4822 format |= SND_SOC_DAIFMT_CBM_CFS;
4823 break;
4824 case 0x01:
4825 format |= SND_SOC_DAIFMT_CBS_CFM;
4826 break;
4827 default:
4828 format |= SND_SOC_DAIFMT_CBS_CFS;
4829 break;
4830 }
4831
4832 return format;
4833}
4834EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4835
Kuninori Morimotocb470082013-09-10 17:39:56 -07004836int snd_soc_of_get_dai_name(struct device_node *of_node,
4837 const char **dai_name)
4838{
4839 struct snd_soc_component *pos;
4840 struct of_phandle_args args;
4841 int ret;
4842
4843 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4844 "#sound-dai-cells", 0, &args);
4845 if (ret)
4846 return ret;
4847
4848 ret = -EPROBE_DEFER;
4849
4850 mutex_lock(&client_mutex);
4851 list_for_each_entry(pos, &component_list, list) {
4852 if (pos->dev->of_node != args.np)
4853 continue;
4854
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004855 if (pos->driver->of_xlate_dai_name) {
4856 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4857 } else {
4858 int id = -1;
4859
4860 switch (args.args_count) {
4861 case 0:
4862 id = 0; /* same as dai_drv[0] */
4863 break;
4864 case 1:
4865 id = args.args[0];
4866 break;
4867 default:
4868 /* not supported */
4869 break;
4870 }
4871
4872 if (id < 0 || id >= pos->num_dai) {
4873 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004874 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004875 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004876
4877 ret = 0;
4878
4879 *dai_name = pos->dai_drv[id].name;
4880 if (!*dai_name)
4881 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004882 }
4883
Kuninori Morimotocb470082013-09-10 17:39:56 -07004884 break;
4885 }
4886 mutex_unlock(&client_mutex);
4887
4888 of_node_put(args.np);
4889
4890 return ret;
4891}
4892EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4893
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004894static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004895{
Mark Brown384c89e2008-12-03 17:34:03 +00004896#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004897 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4898 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004899 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004900 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004901 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004902
Mark Brown8a9dab12011-01-10 22:25:21 +00004903 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004904 &codec_list_fops))
4905 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4906
Mark Brown8a9dab12011-01-10 22:25:21 +00004907 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004908 &dai_list_fops))
4909 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004910
Mark Brown8a9dab12011-01-10 22:25:21 +00004911 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004912 &platform_list_fops))
4913 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004914#endif
4915
Mark Brownfb257892011-04-28 10:57:54 +01004916 snd_soc_util_init();
4917
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004918 return platform_driver_register(&soc_driver);
4919}
Mark Brown4abe8e12010-10-12 17:41:03 +01004920module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004921
Mark Brown7d8c16a2008-11-30 22:11:24 +00004922static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004923{
Mark Brownfb257892011-04-28 10:57:54 +01004924 snd_soc_util_exit();
4925
Mark Brown384c89e2008-12-03 17:34:03 +00004926#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004927 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004928#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004929 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004930}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004931module_exit(snd_soc_exit);
4932
4933/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004934MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004935MODULE_DESCRIPTION("ALSA SoC Core");
4936MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004937MODULE_ALIAS("platform:soc-audio");