blob: 5f6f97874ca254321b96ceb21a580d03f9a44c16 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000315 &codec->cache_only);
316
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200318 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000323}
324
Mark Brownc3c5a192010-09-15 18:15:14 +0100325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200337 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
Mark Brownf3208782010-09-15 18:19:07 +0100359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100364 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 }
381 }
Mark Brownf3208782010-09-15 18:19:07 +0100382
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
Mark Brown19c7ac22010-09-15 18:22:40 +0100395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200408 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
Mark Brown2b194f9d2010-10-13 10:52:16 +0100417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000432 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200433 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100435 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000444 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#else
453
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200454#define soc_init_codec_debugfs NULL
455
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000458{
459}
460
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463{
464}
465
Axel Linb95fccb2010-11-09 17:06:44 +0800466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200522 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 codec->ac97->dev.release = soc_ac97_device_release;
524
Kay Sieversbb072bf2008-11-02 03:50:35 +0100525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 continue;
576
Benoit Cousson88bd8702014-07-08 23:19:34 +0200577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100586 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100589 continue;
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 /* close any waiting streams and save state */
613 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623
624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 }
635
Mark Browne2d32ff2012-08-31 17:38:32 -0700636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200645 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200655 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000656 break;
657 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100659 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900661 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 }
672 }
673 }
674
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
677
678 if (card->rtd[i].dai_link->ignore_suspend)
679 continue;
680
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 }
687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
691 return 0;
692}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000693EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Andy Green6ed25972008-06-13 16:24:05 +0100695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Andy Green6ed25972008-06-13 16:24:05 +0100705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100710
Mark Brown99497882010-05-07 20:24:05 +0100711 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100722 continue;
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 break;
744 }
Mark Brown1547aba2010-05-07 21:11:40 +0100745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100751 continue;
752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Mark Brown3ff3f642008-05-19 12:32:25 +0200762 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100766 continue;
767
Benoit Cousson88bd8702014-07-08 23:19:34 +0200768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 }
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100782 continue;
783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000793 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000795 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100796
797 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100803}
804
805/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100807{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200810
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800832 }
833
Mark Brown64ab9ba2009-03-31 11:27:03 +0100834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600848 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100850 }
Andy Green6ed25972008-06-13 16:24:05 +0100851
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 return 0;
853}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000854EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858#endif
859
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100860static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800861};
862
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200863static struct snd_soc_component *soc_find_component(
864 const struct device_node *of_node, const char *name)
865{
866 struct snd_soc_component *component;
867
868 list_for_each_entry(component, &component_list, list) {
869 if (of_node) {
870 if (component->dev->of_node == of_node)
871 return component;
872 } else if (strcmp(component->name, name) == 0) {
873 return component;
874 }
875 }
876
877 return NULL;
878}
879
Benoit Cousson88bd8702014-07-08 23:19:34 +0200880static struct snd_soc_codec *soc_find_codec(
881 const struct device_node *codec_of_node,
882 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883{
884 struct snd_soc_codec *codec;
885
886 list_for_each_entry(codec, &codec_list, list) {
887 if (codec_of_node) {
888 if (codec->dev->of_node != codec_of_node)
889 continue;
890 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200891 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892 continue;
893 }
894
895 return codec;
896 }
897
898 return NULL;
899}
900
901static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
902 const char *codec_dai_name)
903{
904 struct snd_soc_dai *codec_dai;
905
906 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
907 if (!strcmp(codec_dai->name, codec_dai_name)) {
908 return codec_dai;
909 }
910 }
911
912 return NULL;
913}
914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100919 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
921 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000922 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100923 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000927 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000928
Mark Brownb19e6e72012-03-14 21:18:39 +0000929 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100930 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600931 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100932 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600933 continue;
934 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100935 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600936 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100937 list_for_each_entry(cpu_dai, &component->dai_list, list) {
938 if (dai_link->cpu_dai_name &&
939 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
940 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700941
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100942 rtd->cpu_dai = cpu_dai;
943 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000945
946 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000947 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000949 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000950 }
951
Benoit Cousson88bd8702014-07-08 23:19:34 +0200952 rtd->num_codecs = dai_link->num_codecs;
953
954 /* Find CODEC from registered CODECs */
955 for (i = 0; i < rtd->num_codecs; i++) {
956 struct snd_soc_codec *codec;
957 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
958 if (!codec) {
959 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
960 codecs[i].name);
961 return -EPROBE_DEFER;
962 }
963
964 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
965 if (!codec_dais[i]) {
966 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
967 codecs[i].dai_name);
968 return -EPROBE_DEFER;
969 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000970 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100971
Benoit Cousson88bd8702014-07-08 23:19:34 +0200972 /* Single codec links expect codec and codec_dai in runtime data */
973 rtd->codec_dai = codec_dais[0];
974 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100975
Mark Brown848dd8b2011-04-27 18:16:32 +0100976 /* if there's no platform we match on the empty platform */
977 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700978 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100979 platform_name = "snd-soc-dummy";
980
Mark Brownb19e6e72012-03-14 21:18:39 +0000981 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700983 if (dai_link->platform_of_node) {
984 if (platform->dev->of_node !=
985 dai_link->platform_of_node)
986 continue;
987 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200988 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700989 continue;
990 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700991
992 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000993 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000994 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000995 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000997 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000999
1000 card->num_rtd++;
1001
1002 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003}
1004
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001005static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001006{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001007 if (!component->probed)
1008 return;
1009
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001010 /* This is a HACK and will be removed soon */
1011 if (component->codec)
1012 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001013
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001014 if (component->remove)
1015 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001016
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001017 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001018
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001019 soc_cleanup_component_debugfs(component);
1020 component->probed = 0;
1021 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001022}
1023
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001024static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001025{
1026 int err;
1027
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001028 if (dai && dai->probed &&
1029 dai->driver->remove_order == order) {
1030 if (dai->driver->remove) {
1031 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001032 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001033 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001034 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001035 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001036 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001037 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001038 }
1039}
1040
Stephen Warren62ae68f2012-06-08 12:34:23 -06001041static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042{
1043 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001044 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045
1046 /* unregister the rtd device */
1047 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001048 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1049 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1050 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 rtd->dev_registered = 0;
1052 }
1053
1054 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001055 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001056 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001057
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001058 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059}
1060
Stephen Warren62ae68f2012-06-08 12:34:23 -06001061static void soc_remove_link_components(struct snd_soc_card *card, int num,
1062 int order)
1063{
1064 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1065 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001066 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001067 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001068 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001069
1070 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001071 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001072 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001073
1074 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001075 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001076 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001077 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001078 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001079 }
1080
1081 /* remove any CPU-side CODEC */
1082 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001083 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001084 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001085 }
1086}
1087
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001088static void soc_remove_dai_links(struct snd_soc_card *card)
1089{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001090 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001091
Liam Girdwood0168bf02011-06-07 16:08:05 +01001092 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1093 order++) {
1094 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001095 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001096 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001097
1098 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1099 order++) {
1100 for (dai = 0; dai < card->num_rtd; dai++)
1101 soc_remove_link_components(card, dai, order);
1102 }
1103
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001104 card->num_rtd = 0;
1105}
1106
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001107static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001108 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001109{
1110 int i;
1111
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001112 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001113 return;
1114
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001115 for (i = 0; i < card->num_configs; i++) {
1116 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001117 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001118 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001119 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001120 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001121 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001122 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001123 }
1124}
1125
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001126static int soc_probe_component(struct snd_soc_card *card,
1127 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001128{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001129 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1130 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001131 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001132 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001133
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001134 if (component->probed)
1135 return 0;
1136
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001137 component->card = card;
1138 dapm->card = card;
1139 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001140
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001141 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001142 return -ENODEV;
1143
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001144 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001145
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 if (component->dapm_widgets) {
1147 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1148 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001149
1150 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001151 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001152 "Failed to create new controls %d\n", ret);
1153 goto err_probe;
1154 }
1155 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001156
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001157 /*
1158 * This is rather ugly, but certain platforms expect that the DAPM
1159 * widgets for the DAIs for components with the same parent device are
1160 * created in the platforms DAPM context. Until that is fixed we need to
1161 * keep this.
1162 */
1163 if (component->steal_sibling_dai_widgets) {
1164 dai_component = NULL;
1165 list_for_each_entry(component2, &component_list, list) {
1166 if (component == component2)
1167 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001168
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001169 if (component2->dev == component->dev &&
1170 !list_empty(&component2->dai_list)) {
1171 dai_component = component2;
1172 break;
1173 }
1174 }
1175 } else {
1176 dai_component = component;
1177 list_for_each_entry(component2, &component_list, list) {
1178 if (component2->dev == component->dev &&
1179 component2->steal_sibling_dai_widgets) {
1180 dai_component = NULL;
1181 break;
1182 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001183 }
1184 }
Mark Brown888df392012-02-16 19:37:51 -08001185
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001186 if (dai_component) {
1187 list_for_each_entry(dai, &dai_component->dai_list, list) {
1188 snd_soc_dapm_new_dai_widgets(dapm, dai);
1189 if (ret != 0) {
1190 dev_err(component->dev,
1191 "Failed to create DAI widgets %d\n",
1192 ret);
1193 goto err_probe;
1194 }
1195 }
1196 }
Mark Brown33c5f962011-08-22 18:40:30 +01001197
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001198 if (component->probe) {
1199 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001200 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001201 dev_err(component->dev,
1202 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001203 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001204 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001205
1206 WARN(dapm->idle_bias_off &&
1207 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001208 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001209 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001210 }
1211
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001212 if (component->controls)
1213 snd_soc_add_component_controls(component, component->controls,
1214 component->num_controls);
1215 if (component->dapm_routes)
1216 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1217 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001218
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001219 component->probed = 1;
1220 list_add(&dapm->list, &card->dapm_list);
1221
1222 /* This is a HACK and will be removed soon */
1223 if (component->codec)
1224 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225
Jarkko Nikula70d29332011-01-27 16:24:22 +02001226 return 0;
1227
1228err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001229 soc_cleanup_component_debugfs(component);
1230 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001231
1232 return ret;
1233}
1234
Mark Brown36ae1a92012-01-06 17:12:45 -08001235static void rtd_release(struct device *dev)
1236{
1237 kfree(dev);
1238}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001240static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1241 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001242{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001243 int ret = 0;
1244
Jarkko Nikula589c3562010-12-06 16:27:07 +02001245 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001246 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1247 if (!rtd->dev)
1248 return -ENOMEM;
1249 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001250 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001251 rtd->dev->release = rtd_release;
1252 rtd->dev->init_name = name;
1253 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001254 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001255 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1256 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001259 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001260 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001261 /* calling put_device() here to free the rtd->dev */
1262 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001263 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001264 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001265 return ret;
1266 }
1267 rtd->dev_registered = 1;
1268
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001269 if (rtd->codec) {
1270 /* add DAPM sysfs entries for this codec */
1271 ret = snd_soc_dapm_sys_add(rtd->dev);
1272 if (ret < 0)
1273 dev_err(rtd->dev,
1274 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1275 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001277 /* add codec sysfs entries */
1278 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1279 if (ret < 0)
1280 dev_err(rtd->dev,
1281 "ASoC: failed to add codec sysfs files: %d\n",
1282 ret);
1283 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001284
1285 return 0;
1286}
1287
Stephen Warren62ae68f2012-06-08 12:34:23 -06001288static int soc_probe_link_components(struct snd_soc_card *card, int num,
1289 int order)
1290{
1291 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001292 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001293 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001294 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001295
1296 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001297 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001298 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001299 ret = soc_probe_component(card, component);
1300 if (ret < 0)
1301 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001302 }
1303
Benoit Cousson88bd8702014-07-08 23:19:34 +02001304 /* probe the CODEC-side components */
1305 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001306 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001307 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001308 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001309 if (ret < 0)
1310 return ret;
1311 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001312 }
1313
1314 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001315 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001316 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001317 if (ret < 0)
1318 return ret;
1319 }
1320
1321 return 0;
1322}
1323
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001324static int soc_probe_codec_dai(struct snd_soc_card *card,
1325 struct snd_soc_dai *codec_dai,
1326 int order)
1327{
1328 int ret;
1329
1330 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1331 if (codec_dai->driver->probe) {
1332 ret = codec_dai->driver->probe(codec_dai);
1333 if (ret < 0) {
1334 dev_err(codec_dai->dev,
1335 "ASoC: failed to probe CODEC DAI %s: %d\n",
1336 codec_dai->name, ret);
1337 return ret;
1338 }
1339 }
1340
1341 /* mark codec_dai as probed and add to card dai list */
1342 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001343 }
1344
1345 return 0;
1346}
1347
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001348static int soc_link_dai_widgets(struct snd_soc_card *card,
1349 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001350 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001351{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001352 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1353 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001354 struct snd_soc_dapm_widget *play_w, *capture_w;
1355 int ret;
1356
Benoit Cousson88bd8702014-07-08 23:19:34 +02001357 if (rtd->num_codecs > 1)
1358 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1359
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001360 /* link the DAI widgets */
1361 play_w = codec_dai->playback_widget;
1362 capture_w = cpu_dai->capture_widget;
1363 if (play_w && capture_w) {
1364 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1365 capture_w, play_w);
1366 if (ret != 0) {
1367 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1368 play_w->name, capture_w->name, ret);
1369 return ret;
1370 }
1371 }
1372
1373 play_w = cpu_dai->playback_widget;
1374 capture_w = codec_dai->capture_widget;
1375 if (play_w && capture_w) {
1376 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1377 capture_w, play_w);
1378 if (ret != 0) {
1379 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1380 play_w->name, capture_w->name, ret);
1381 return ret;
1382 }
1383 }
1384
1385 return 0;
1386}
1387
Stephen Warren62ae68f2012-06-08 12:34:23 -06001388static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389{
1390 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1391 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001393 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001394 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001396 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001397 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001398
1399 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001400 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001401 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001402 for (i = 0; i < rtd->num_codecs; i++)
1403 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001404
1405 /* set default power off timeout */
1406 rtd->pmdown_time = pmdown_time;
1407
1408 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001409 if (!cpu_dai->probed &&
1410 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001411 if (cpu_dai->driver->probe) {
1412 ret = cpu_dai->driver->probe(cpu_dai);
1413 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001414 dev_err(cpu_dai->dev,
1415 "ASoC: failed to probe CPU DAI %s: %d\n",
1416 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001417 return ret;
1418 }
1419 }
1420 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001421 }
1422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001424 for (i = 0; i < rtd->num_codecs; i++) {
1425 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1426 if (ret)
1427 return ret;
1428 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001429
Liam Girdwood0168bf02011-06-07 16:08:05 +01001430 /* complete DAI probe during last probe */
1431 if (order != SND_SOC_COMP_ORDER_LAST)
1432 return 0;
1433
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001434 /* do machine specific initialization */
1435 if (dai_link->init) {
1436 ret = dai_link->init(rtd);
1437 if (ret < 0) {
1438 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1439 dai_link->name, ret);
1440 return ret;
1441 }
1442 }
1443
1444 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001445 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001446 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001447
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001448#ifdef CONFIG_DEBUG_FS
1449 /* add DPCM sysfs entries */
1450 if (dai_link->dynamic) {
1451 ret = soc_dpcm_debugfs_add(rtd);
1452 if (ret < 0) {
1453 dev_err(rtd->dev,
1454 "ASoC: failed to add dpcm sysfs entries: %d\n",
1455 ret);
1456 return ret;
1457 }
1458 }
1459#endif
1460
Mark Brown36ae1a92012-01-06 17:12:45 -08001461 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001462 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001463 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1464 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001465
Namarta Kohli1245b702012-08-16 17:10:41 +05301466 if (cpu_dai->driver->compress_dai) {
1467 /*create compress_device"*/
1468 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001469 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001470 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301471 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001472 return ret;
1473 }
1474 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301475
1476 if (!dai_link->params) {
1477 /* create the pcm */
1478 ret = soc_new_pcm(rtd, num);
1479 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001480 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301481 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001482 return ret;
1483 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301484 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001485 INIT_DELAYED_WORK(&rtd->delayed_work,
1486 codec2codec_close_delayed_work);
1487
Namarta Kohli1245b702012-08-16 17:10:41 +05301488 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001489 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001490 if (ret)
1491 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001492 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493 }
1494
1495 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001496 for (i = 0; i < rtd->num_codecs; i++) {
1497 if (rtd->codec_dais[i]->driver->ac97_control)
1498 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1499 rtd->cpu_dai->ac97_pdata);
1500 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501
1502 return 0;
1503}
1504
1505#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001506static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1507 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001508{
1509 int ret;
1510
1511 /* Only instantiate AC97 if not already done by the adaptor
1512 * for the generic AC97 subsystem.
1513 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001514 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001515 /*
1516 * It is possible that the AC97 device is already registered to
1517 * the device subsystem. This happens when the device is created
1518 * via snd_ac97_mixer(). Currently only SoC codec that does so
1519 * is the generic AC97 glue but others migh emerge.
1520 *
1521 * In those cases we don't try to register the device again.
1522 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001523 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001524 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001526 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001528 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001529 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530 return ret;
1531 }
1532
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001533 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001534 }
1535 return 0;
1536}
1537
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001538static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001539{
1540 if (codec->ac97_registered) {
1541 soc_ac97_dev_unregister(codec);
1542 codec->ac97_registered = 0;
1543 }
1544}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001545
Benoit Cousson88bd8702014-07-08 23:19:34 +02001546static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1547{
1548 int i, ret;
1549
1550 for (i = 0; i < rtd->num_codecs; i++) {
1551 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1552
1553 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1554 if (ret) {
1555 while (--i >= 0)
1556 soc_unregister_ac97_codec(codec_dai->codec);
1557 return ret;
1558 }
1559 }
1560
1561 return 0;
1562}
1563
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001564static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1565{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001566 int i;
1567
1568 for (i = 0; i < rtd->num_codecs; i++)
1569 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001570}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001571#endif
1572
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001573static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001574{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001575 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001576 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001577 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001578
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001579 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1580 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001581 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001582 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001583
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001584 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001585 return -EPROBE_DEFER;
1586 }
1587
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001588 /*
1589 * Some places still reference rtd->codec, so we have to keep that
1590 * initialized if the component is a CODEC. Once all those references
1591 * have been removed, this code can be removed as well.
1592 */
1593 rtd->codec = rtd->component->codec;
1594
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001595 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001596}
1597
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001598static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1599{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001600 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001601 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001602 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001603
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001604 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001605 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001606 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001607
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001608 /* do machine specific initialization */
1609 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001610 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001611 if (ret < 0) {
1612 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1613 aux_dev->name, ret);
1614 return ret;
1615 }
1616 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001617
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001618 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001619}
1620
1621static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1622{
1623 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001624 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001625
1626 /* unregister the rtd device */
1627 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001628 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001629 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001630 rtd->dev_registered = 0;
1631 }
1632
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001633 if (component && component->probed)
1634 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001635}
1636
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001637static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001638{
1639 int ret;
1640
1641 if (codec->cache_init)
1642 return 0;
1643
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001644 ret = snd_soc_cache_init(codec);
1645 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001646 dev_err(codec->dev,
1647 "ASoC: Failed to set cache compression type: %d\n",
1648 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001649 return ret;
1650 }
1651 codec->cache_init = 1;
1652 return 0;
1653}
1654
Mark Brownb19e6e72012-03-14 21:18:39 +00001655static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001657 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001658 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001659 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660
Liam Girdwood01b9d992012-03-07 10:38:25 +00001661 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662
Mark Brownb19e6e72012-03-14 21:18:39 +00001663 /* bind DAIs */
1664 for (i = 0; i < card->num_links; i++) {
1665 ret = soc_bind_dai_link(card, i);
1666 if (ret != 0)
1667 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 }
1669
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001670 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001671 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001672 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001673 if (ret != 0)
1674 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675 }
1676
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001677 /* initialize the register cache for each available codec */
1678 list_for_each_entry(codec, &codec_list, list) {
1679 if (codec->cache_init)
1680 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001681 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001682 if (ret < 0)
1683 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001684 }
1685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001687 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 card->owner, 0, &card->snd_card);
1689 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001690 dev_err(card->dev,
1691 "ASoC: can't create sound card for card %s: %d\n",
1692 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001693 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001695
Mark Browne37a4972011-03-02 18:21:57 +00001696 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1697 card->dapm.dev = card->dev;
1698 card->dapm.card = card;
1699 list_add(&card->dapm.list, &card->dapm_list);
1700
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001701#ifdef CONFIG_DEBUG_FS
1702 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1703#endif
1704
Mark Brown88ee1c62011-02-02 10:43:26 +00001705#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001706 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001707 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001708#endif
Andy Green6ed25972008-06-13 16:24:05 +01001709
Mark Brown9a841eb2011-04-12 17:51:37 -07001710 if (card->dapm_widgets)
1711 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1712 card->num_dapm_widgets);
1713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 /* initialise the sound card only once */
1715 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001716 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717 if (ret < 0)
1718 goto card_probe_error;
1719 }
1720
Stephen Warren62ae68f2012-06-08 12:34:23 -06001721 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001722 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1723 order++) {
1724 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001725 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001726 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001727 dev_err(card->dev,
1728 "ASoC: failed to instantiate card %d\n",
1729 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001730 goto probe_dai_err;
1731 }
1732 }
1733 }
1734
1735 /* probe all DAI links on this card */
1736 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1737 order++) {
1738 for (i = 0; i < card->num_links; i++) {
1739 ret = soc_probe_link_dais(card, i, order);
1740 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001741 dev_err(card->dev,
1742 "ASoC: failed to instantiate card %d\n",
1743 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001744 goto probe_dai_err;
1745 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747 }
1748
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001749 for (i = 0; i < card->num_aux_devs; i++) {
1750 ret = soc_probe_aux_dev(card, i);
1751 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001752 dev_err(card->dev,
1753 "ASoC: failed to add auxiliary devices %d\n",
1754 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001755 goto probe_aux_dev_err;
1756 }
1757 }
1758
Mark Brown888df392012-02-16 19:37:51 -08001759 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001760 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001761
Mark Brownb7af1da2011-04-07 19:18:44 +09001762 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001763 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001764
Mark Brownb8ad29d2011-03-02 18:35:51 +00001765 if (card->dapm_routes)
1766 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1767 card->num_dapm_routes);
1768
Mark Brown75d9ac42011-09-27 16:41:01 +01001769 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001770 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001771 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001772 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001773
Mark Brownf04209a2012-04-09 16:29:21 +01001774 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001775 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1776 int j;
1777
1778 for (j = 0; j < rtd->num_codecs; j++) {
1779 struct snd_soc_dai *codec_dai = codec_dais[j];
1780
1781 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1782 if (ret != 0 && ret != -ENOTSUPP)
1783 dev_warn(codec_dai->dev,
1784 "ASoC: Failed to set DAI format: %d\n",
1785 ret);
1786 }
Mark Brownf04209a2012-04-09 16:29:21 +01001787 }
1788
1789 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001790 if (dai_fmt &&
1791 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001792 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1793 dai_fmt);
1794 if (ret != 0 && ret != -ENOTSUPP)
1795 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001796 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001797 ret);
1798 } else if (dai_fmt) {
1799 /* Flip the polarity for the "CPU" end */
1800 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1801 switch (dai_link->dai_fmt &
1802 SND_SOC_DAIFMT_MASTER_MASK) {
1803 case SND_SOC_DAIFMT_CBM_CFM:
1804 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1805 break;
1806 case SND_SOC_DAIFMT_CBM_CFS:
1807 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1808 break;
1809 case SND_SOC_DAIFMT_CBS_CFM:
1810 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1811 break;
1812 case SND_SOC_DAIFMT_CBS_CFS:
1813 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1814 break;
1815 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001816
1817 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001818 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001819 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001820 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001822 ret);
1823 }
1824 }
1825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001826 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001828 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1829 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001830 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1831 "%s", card->driver_name ? card->driver_name : card->name);
1832 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1833 switch (card->snd_card->driver[i]) {
1834 case '_':
1835 case '-':
1836 case '\0':
1837 break;
1838 default:
1839 if (!isalnum(card->snd_card->driver[i]))
1840 card->snd_card->driver[i] = '_';
1841 break;
1842 }
1843 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844
Mark Brown28e9ad92011-03-02 18:36:34 +00001845 if (card->late_probe) {
1846 ret = card->late_probe(card);
1847 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001848 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001849 card->name, ret);
1850 goto probe_aux_dev_err;
1851 }
1852 }
1853
Stephen Warren16332812011-11-23 12:42:04 -07001854 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001855 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001856
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001857 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001858
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859 ret = snd_card_register(card->snd_card);
1860 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001861 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1862 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001863 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001864 }
1865
1866#ifdef CONFIG_SND_SOC_AC97_BUS
1867 /* register any AC97 codecs */
1868 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001869 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1870 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001871 dev_err(card->dev,
1872 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001873 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001874 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001875 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001876 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001877 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001878#endif
1879
Mark Brown435c5e22008-12-04 15:32:53 +00001880 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001881 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001882 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001883
1884 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001886probe_aux_dev_err:
1887 for (i = 0; i < card->num_aux_devs; i++)
1888 soc_remove_aux_dev(card, i);
1889
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001890probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001891 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001892
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001894 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001895 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896
1897 snd_card_free(card->snd_card);
1898
Mark Brownb19e6e72012-03-14 21:18:39 +00001899base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001901
Mark Brownb19e6e72012-03-14 21:18:39 +00001902 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001903}
1904
1905/* probes a new socdev */
1906static int soc_probe(struct platform_device *pdev)
1907{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001908 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001909
Vinod Koul70a7ca32011-01-14 19:22:48 +05301910 /*
1911 * no card, so machine driver should be registering card
1912 * we should not be here in that case so ret error
1913 */
1914 if (!card)
1915 return -EINVAL;
1916
Mark Brownfe4085e2012-03-02 13:07:41 +00001917 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001918 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001919 card->name);
1920
Mark Brown435c5e22008-12-04 15:32:53 +00001921 /* Bodge while we unpick instantiation */
1922 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923
Mark Brown28d528c2012-08-09 18:45:23 +01001924 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925}
1926
Vinod Koulb0e26482011-01-13 22:48:02 +05301927static int soc_cleanup_card_resources(struct snd_soc_card *card)
1928{
Vinod Koulb0e26482011-01-13 22:48:02 +05301929 int i;
1930
1931 /* make sure any delayed work runs */
1932 for (i = 0; i < card->num_rtd; i++) {
1933 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001934 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301935 }
1936
1937 /* remove auxiliary devices */
1938 for (i = 0; i < card->num_aux_devs; i++)
1939 soc_remove_aux_dev(card, i);
1940
1941 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001942 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301943
1944 soc_cleanup_card_debugfs(card);
1945
1946 /* remove the card */
1947 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001948 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301949
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001950 snd_soc_dapm_free(&card->dapm);
1951
Vinod Koulb0e26482011-01-13 22:48:02 +05301952 snd_card_free(card->snd_card);
1953 return 0;
1954
1955}
1956
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957/* removes a socdev */
1958static int soc_remove(struct platform_device *pdev)
1959{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961
Mark Brownc5af3a22008-11-28 13:29:45 +00001962 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963 return 0;
1964}
1965
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001966int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001967{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001968 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969 int i;
Mark Brown51737472009-06-22 13:16:51 +01001970
1971 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001972 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001973
1974 /* Flush out pmdown_time work - we actually do want to run it
1975 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976 for (i = 0; i < card->num_rtd; i++) {
1977 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001978 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 }
Mark Brown51737472009-06-22 13:16:51 +01001980
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001981 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001982
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001983 /* deactivate pins to sleep state */
1984 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001985 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1986 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1987 int j;
1988
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001989 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001990 for (j = 0; j < rtd->num_codecs; j++) {
1991 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1992 pinctrl_pm_select_sleep_state(codec_dai->dev);
1993 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001994 }
1995
Mark Brown416356f2009-06-30 19:05:15 +01001996 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001997}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001998EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001999
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002000const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302001 .suspend = snd_soc_suspend,
2002 .resume = snd_soc_resume,
2003 .freeze = snd_soc_suspend,
2004 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002005 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302006 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002007};
Stephen Warrendeb26072011-04-05 19:35:30 -06002008EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002009
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010/* ASoC platform driver */
2011static struct platform_driver soc_driver = {
2012 .driver = {
2013 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002014 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002015 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016 },
2017 .probe = soc_probe,
2018 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019};
2020
Mark Brown096e49d2009-07-05 15:12:22 +01002021/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 * snd_soc_new_ac97_codec - initailise AC97 device
2023 * @codec: audio codec
2024 * @ops: AC97 bus operations
2025 * @num: AC97 codec number
2026 *
2027 * Initialises AC97 codec resources for use by ad-hoc devices only.
2028 */
2029int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2030 struct snd_ac97_bus_ops *ops, int num)
2031{
2032 mutex_lock(&codec->mutex);
2033
2034 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2035 if (codec->ac97 == NULL) {
2036 mutex_unlock(&codec->mutex);
2037 return -ENOMEM;
2038 }
2039
2040 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2041 if (codec->ac97->bus == NULL) {
2042 kfree(codec->ac97);
2043 codec->ac97 = NULL;
2044 mutex_unlock(&codec->mutex);
2045 return -ENOMEM;
2046 }
2047
2048 codec->ac97->bus->ops = ops;
2049 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002050
2051 /*
2052 * Mark the AC97 device to be created by us. This way we ensure that the
2053 * device will be registered with the device subsystem later on.
2054 */
2055 codec->ac97_created = 1;
2056
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057 mutex_unlock(&codec->mutex);
2058 return 0;
2059}
2060EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2061
Markus Pargmann741a5092013-08-19 17:05:55 +02002062static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2063
2064static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2065{
2066 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2067
2068 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2069
2070 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2071
2072 udelay(10);
2073
2074 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2075
2076 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2077 msleep(2);
2078}
2079
2080static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2081{
2082 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2083
2084 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2085
2086 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2087 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2088 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2089
2090 udelay(10);
2091
2092 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2093
2094 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2095 msleep(2);
2096}
2097
2098static int snd_soc_ac97_parse_pinctl(struct device *dev,
2099 struct snd_ac97_reset_cfg *cfg)
2100{
2101 struct pinctrl *p;
2102 struct pinctrl_state *state;
2103 int gpio;
2104 int ret;
2105
2106 p = devm_pinctrl_get(dev);
2107 if (IS_ERR(p)) {
2108 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002109 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002110 }
2111 cfg->pctl = p;
2112
2113 state = pinctrl_lookup_state(p, "ac97-reset");
2114 if (IS_ERR(state)) {
2115 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002116 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002117 }
2118 cfg->pstate_reset = state;
2119
2120 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2121 if (IS_ERR(state)) {
2122 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002123 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002124 }
2125 cfg->pstate_warm_reset = state;
2126
2127 state = pinctrl_lookup_state(p, "ac97-running");
2128 if (IS_ERR(state)) {
2129 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002130 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002131 }
2132 cfg->pstate_run = state;
2133
2134 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2135 if (gpio < 0) {
2136 dev_err(dev, "Can't find ac97-sync gpio\n");
2137 return gpio;
2138 }
2139 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2140 if (ret) {
2141 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2142 return ret;
2143 }
2144 cfg->gpio_sync = gpio;
2145
2146 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2147 if (gpio < 0) {
2148 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2149 return gpio;
2150 }
2151 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2152 if (ret) {
2153 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2154 return ret;
2155 }
2156 cfg->gpio_sdata = gpio;
2157
2158 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2159 if (gpio < 0) {
2160 dev_err(dev, "Can't find ac97-reset gpio\n");
2161 return gpio;
2162 }
2163 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2164 if (ret) {
2165 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2166 return ret;
2167 }
2168 cfg->gpio_reset = gpio;
2169
2170 return 0;
2171}
2172
Mark Brownb047e1c2013-06-26 12:45:59 +01002173struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002174EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002175
2176int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2177{
2178 if (ops == soc_ac97_ops)
2179 return 0;
2180
2181 if (soc_ac97_ops && ops)
2182 return -EBUSY;
2183
2184 soc_ac97_ops = ops;
2185
2186 return 0;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2189
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002190/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002191 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2192 *
2193 * This function sets the reset and warm_reset properties of ops and parses
2194 * the device node of pdev to get pinctrl states and gpio numbers to use.
2195 */
2196int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2197 struct platform_device *pdev)
2198{
2199 struct device *dev = &pdev->dev;
2200 struct snd_ac97_reset_cfg cfg;
2201 int ret;
2202
2203 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2204 if (ret)
2205 return ret;
2206
2207 ret = snd_soc_set_ac97_ops(ops);
2208 if (ret)
2209 return ret;
2210
2211 ops->warm_reset = snd_soc_ac97_warm_reset;
2212 ops->reset = snd_soc_ac97_reset;
2213
2214 snd_ac97_rst_cfg = cfg;
2215 return 0;
2216}
2217EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2218
2219/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220 * snd_soc_free_ac97_codec - free AC97 codec device
2221 * @codec: audio codec
2222 *
2223 * Frees AC97 codec device resources.
2224 */
2225void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2226{
2227 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002229 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002230#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231 kfree(codec->ac97->bus);
2232 kfree(codec->ac97);
2233 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002234 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 mutex_unlock(&codec->mutex);
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2238
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_cnew - create new control
2241 * @_template: control template
2242 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002243 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002244 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 *
2246 * Create a new mixer control from a template control.
2247 *
2248 * Returns 0 for success, else error.
2249 */
2250struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002251 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002252 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253{
2254 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002255 struct snd_kcontrol *kcontrol;
2256 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257
2258 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 template.index = 0;
2260
Mark Brownefb7ac32011-03-08 17:23:24 +00002261 if (!long_name)
2262 long_name = template.name;
2263
2264 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002265 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!name)
2267 return NULL;
2268
Mark Brownefb7ac32011-03-08 17:23:24 +00002269 template.name = name;
2270 } else {
2271 template.name = long_name;
2272 }
2273
2274 kcontrol = snd_ctl_new1(&template, data);
2275
2276 kfree(name);
2277
2278 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279}
2280EXPORT_SYMBOL_GPL(snd_soc_cnew);
2281
Liam Girdwood022658b2012-02-03 17:43:09 +00002282static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2283 const struct snd_kcontrol_new *controls, int num_controls,
2284 const char *prefix, void *data)
2285{
2286 int err, i;
2287
2288 for (i = 0; i < num_controls; i++) {
2289 const struct snd_kcontrol_new *control = &controls[i];
2290 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2291 control->name, prefix));
2292 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002293 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2294 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002295 return err;
2296 }
2297 }
2298
2299 return 0;
2300}
2301
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002302struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2303 const char *name)
2304{
2305 struct snd_card *card = soc_card->snd_card;
2306 struct snd_kcontrol *kctl;
2307
2308 if (unlikely(!name))
2309 return NULL;
2310
2311 list_for_each_entry(kctl, &card->controls, list)
2312 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2313 return kctl;
2314 return NULL;
2315}
2316EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2317
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002319 * snd_soc_add_component_controls - Add an array of controls to a component.
2320 *
2321 * @component: Component to add controls to
2322 * @controls: Array of controls to add
2323 * @num_controls: Number of elements in the array
2324 *
2325 * Return: 0 for success, else error.
2326 */
2327int snd_soc_add_component_controls(struct snd_soc_component *component,
2328 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2329{
2330 struct snd_card *card = component->card->snd_card;
2331
2332 return snd_soc_add_controls(card, component->dev, controls,
2333 num_controls, component->name_prefix, component);
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2336
2337/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002338 * snd_soc_add_codec_controls - add an array of controls to a codec.
2339 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002340 * duplicating this code.
2341 *
2342 * @codec: codec to add controls to
2343 * @controls: array of controls to add
2344 * @num_controls: number of elements in the array
2345 *
2346 * Return 0 for success, else error.
2347 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002348int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002349 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002350{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002351 return snd_soc_add_component_controls(&codec->component, controls,
2352 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002353}
Liam Girdwood022658b2012-02-03 17:43:09 +00002354EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002355
2356/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002357 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002358 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002359 *
2360 * @platform: platform to add controls to
2361 * @controls: array of controls to add
2362 * @num_controls: number of elements in the array
2363 *
2364 * Return 0 for success, else error.
2365 */
2366int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002367 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002368{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002369 return snd_soc_add_component_controls(&platform->component, controls,
2370 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002371}
2372EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2373
2374/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002375 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2376 * Convenience function to add a list of controls.
2377 *
2378 * @soc_card: SoC card to add controls to
2379 * @controls: array of controls to add
2380 * @num_controls: number of elements in the array
2381 *
2382 * Return 0 for success, else error.
2383 */
2384int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2385 const struct snd_kcontrol_new *controls, int num_controls)
2386{
2387 struct snd_card *card = soc_card->snd_card;
2388
2389 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2390 NULL, soc_card);
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2393
2394/**
2395 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2396 * Convienience function to add a list of controls.
2397 *
2398 * @dai: DAI to add controls to
2399 * @controls: array of controls to add
2400 * @num_controls: number of elements in the array
2401 *
2402 * Return 0 for success, else error.
2403 */
2404int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2405 const struct snd_kcontrol_new *controls, int num_controls)
2406{
2407 struct snd_card *card = dai->card->snd_card;
2408
2409 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2410 NULL, dai);
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2413
2414/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415 * snd_soc_info_enum_double - enumerated double mixer info callback
2416 * @kcontrol: mixer control
2417 * @uinfo: control element information
2418 *
2419 * Callback to provide information about a double enumerated
2420 * mixer control.
2421 *
2422 * Returns 0 for success.
2423 */
2424int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_info *uinfo)
2426{
2427 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2428
2429 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2430 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002431 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002433 if (uinfo->value.enumerated.item >= e->items)
2434 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002435 strlcpy(uinfo->value.enumerated.name,
2436 e->texts[uinfo->value.enumerated.item],
2437 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002438 return 0;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2441
2442/**
2443 * snd_soc_get_enum_double - enumerated double mixer get callback
2444 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002445 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 *
2447 * Callback to get the value of a double enumerated mixer.
2448 *
2449 * Returns 0 for success.
2450 */
2451int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2453{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002454 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002456 unsigned int val, item;
2457 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002458 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002460 ret = snd_soc_component_read(component, e->reg, &reg_val);
2461 if (ret)
2462 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002463 val = (reg_val >> e->shift_l) & e->mask;
2464 item = snd_soc_enum_val_to_item(e, val);
2465 ucontrol->value.enumerated.item[0] = item;
2466 if (e->shift_l != e->shift_r) {
2467 val = (reg_val >> e->shift_l) & e->mask;
2468 item = snd_soc_enum_val_to_item(e, val);
2469 ucontrol->value.enumerated.item[1] = item;
2470 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471
2472 return 0;
2473}
2474EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2475
2476/**
2477 * snd_soc_put_enum_double - enumerated double mixer put callback
2478 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002479 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002480 *
2481 * Callback to set the value of a double enumerated mixer.
2482 *
2483 * Returns 0 for success.
2484 */
2485int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_value *ucontrol)
2487{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002488 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002490 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002491 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002492 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002494 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002495 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002496 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002497 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002499 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002500 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002501 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002502 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 }
2504
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002505 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506}
2507EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2508
2509/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002510 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002511 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002512 * @reg: Register to read
2513 * @mask: Mask to use after shifting the register value
2514 * @shift: Right shift of register value
2515 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002516 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002517 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002518 * This functions reads a codec register. The register value is shifted right
2519 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2520 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002521 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002522 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002523 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002524static int snd_soc_read_signed(struct snd_soc_component *component,
2525 unsigned int reg, unsigned int mask, unsigned int shift,
2526 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002527{
Markus Pargmannf227b882014-01-16 16:02:10 +01002528 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002529 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002530
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002531 ret = snd_soc_component_read(component, reg, &val);
2532 if (ret < 0)
2533 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002534
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002535 val = (val >> shift) & mask;
2536
2537 if (!sign_bit) {
2538 *signed_val = val;
2539 return 0;
2540 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002541
2542 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002543 if (!(val & BIT(sign_bit))) {
2544 *signed_val = val;
2545 return 0;
2546 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002547
2548 ret = val;
2549
2550 /*
2551 * The register most probably does not contain a full-sized int.
2552 * Instead we have an arbitrary number of bits in a signed
2553 * representation which has to be translated into a full-sized int.
2554 * This is done by filling up all bits above the sign-bit.
2555 */
2556 ret |= ~((int)(BIT(sign_bit) - 1));
2557
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002558 *signed_val = ret;
2559
2560 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002561}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002562
2563/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564 * snd_soc_info_volsw - single mixer info callback
2565 * @kcontrol: mixer control
2566 * @uinfo: control element information
2567 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002568 * Callback to provide information about a single mixer control, or a double
2569 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002570 *
2571 * Returns 0 for success.
2572 */
2573int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_info *uinfo)
2575{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002576 struct soc_mixer_control *mc =
2577 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002578 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002580 if (!mc->platform_max)
2581 mc->platform_max = mc->max;
2582 platform_max = mc->platform_max;
2583
2584 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002585 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2586 else
2587 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2588
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002589 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002591 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592 return 0;
2593}
2594EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2595
2596/**
2597 * snd_soc_get_volsw - single mixer get callback
2598 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002599 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002601 * Callback to get the value of a single mixer control, or a double mixer
2602 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 *
2604 * Returns 0 for success.
2605 */
2606int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
2608{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002609 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002610 struct soc_mixer_control *mc =
2611 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002612 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002613 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002614 unsigned int shift = mc->shift;
2615 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002616 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002617 int min = mc->min;
2618 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002619 unsigned int mask = (1 << fls(max)) - 1;
2620 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002621 int val;
2622 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623
Markus Pargmannf227b882014-01-16 16:02:10 +01002624 if (sign_bit)
2625 mask = BIT(sign_bit + 1) - 1;
2626
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002627 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2628 if (ret)
2629 return ret;
2630
2631 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002632 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002634 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002635
2636 if (snd_soc_volsw_is_stereo(mc)) {
2637 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002638 ret = snd_soc_read_signed(component, reg, mask, rshift,
2639 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002640 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002641 ret = snd_soc_read_signed(component, reg2, mask, shift,
2642 sign_bit, &val);
2643 if (ret)
2644 return ret;
2645
2646 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002647 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002649 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650 }
2651
2652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2655
2656/**
2657 * snd_soc_put_volsw - single mixer put callback
2658 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002659 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002660 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002661 * Callback to set the value of a single mixer control, or a double mixer
2662 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 *
2664 * Returns 0 for success.
2665 */
2666int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
2668{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002669 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002670 struct soc_mixer_control *mc =
2671 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002672 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002673 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002674 unsigned int shift = mc->shift;
2675 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002676 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002677 int min = mc->min;
2678 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002679 unsigned int mask = (1 << fls(max)) - 1;
2680 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002681 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002682 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002683 unsigned int val2 = 0;
2684 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685
Markus Pargmannf227b882014-01-16 16:02:10 +01002686 if (sign_bit)
2687 mask = BIT(sign_bit + 1) - 1;
2688
2689 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002690 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002691 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692 val_mask = mask << shift;
2693 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002695 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002697 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002698 if (reg == reg2) {
2699 val_mask |= mask << rshift;
2700 val |= val2 << rshift;
2701 } else {
2702 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002703 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002706 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002707 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708 return err;
2709
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002710 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002711 err = snd_soc_component_update_bits(component, reg2, val_mask,
2712 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002713
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 return err;
2715}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002716EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717
Mark Browne13ac2e2008-05-28 17:58:05 +01002718/**
Brian Austin1d99f242012-03-30 10:43:55 -05002719 * snd_soc_get_volsw_sx - single mixer get callback
2720 * @kcontrol: mixer control
2721 * @ucontrol: control element information
2722 *
2723 * Callback to get the value of a single mixer control, or a double mixer
2724 * control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002732 struct soc_mixer_control *mc =
2733 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002734 unsigned int reg = mc->reg;
2735 unsigned int reg2 = mc->rreg;
2736 unsigned int shift = mc->shift;
2737 unsigned int rshift = mc->rshift;
2738 int max = mc->max;
2739 int min = mc->min;
2740 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002741 unsigned int val;
2742 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002743
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002744 ret = snd_soc_component_read(component, reg, &val);
2745 if (ret < 0)
2746 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002747
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002748 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2749
2750 if (snd_soc_volsw_is_stereo(mc)) {
2751 ret = snd_soc_component_read(component, reg2, &val);
2752 if (ret < 0)
2753 return ret;
2754
2755 val = ((val >> rshift) - min) & mask;
2756 ucontrol->value.integer.value[1] = val;
2757 }
Brian Austin1d99f242012-03-30 10:43:55 -05002758
2759 return 0;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2762
2763/**
2764 * snd_soc_put_volsw_sx - double mixer set callback
2765 * @kcontrol: mixer control
2766 * @uinfo: control element information
2767 *
2768 * Callback to set the value of a double mixer control that spans 2 registers.
2769 *
2770 * Returns 0 for success.
2771 */
2772int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_value *ucontrol)
2774{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002775 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
2778
2779 unsigned int reg = mc->reg;
2780 unsigned int reg2 = mc->rreg;
2781 unsigned int shift = mc->shift;
2782 unsigned int rshift = mc->rshift;
2783 int max = mc->max;
2784 int min = mc->min;
2785 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002786 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002787 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002788
2789 val_mask = mask << shift;
2790 val = (ucontrol->value.integer.value[0] + min) & mask;
2791 val = val << shift;
2792
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002793 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302794 if (err < 0)
2795 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002796
2797 if (snd_soc_volsw_is_stereo(mc)) {
2798 val_mask = mask << rshift;
2799 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2800 val2 = val2 << rshift;
2801
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002802 err = snd_soc_component_update_bits(component, reg2, val_mask,
2803 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002804 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002805 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002806}
2807EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2808
2809/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002810 * snd_soc_info_volsw_s8 - signed mixer info callback
2811 * @kcontrol: mixer control
2812 * @uinfo: control element information
2813 *
2814 * Callback to provide information about a signed mixer control.
2815 *
2816 * Returns 0 for success.
2817 */
2818int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2820{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002821 struct soc_mixer_control *mc =
2822 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002823 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002824 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002825
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002826 if (!mc->platform_max)
2827 mc->platform_max = mc->max;
2828 platform_max = mc->platform_max;
2829
Mark Browne13ac2e2008-05-28 17:58:05 +01002830 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2831 uinfo->count = 2;
2832 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002833 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2837
2838/**
2839 * snd_soc_get_volsw_s8 - signed mixer get callback
2840 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002841 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002842 *
2843 * Callback to get the value of a signed mixer control.
2844 *
2845 * Returns 0 for success.
2846 */
2847int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_value *ucontrol)
2849{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002850 struct soc_mixer_control *mc =
2851 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002852 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002853 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002854 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002855 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 int ret;
2857
2858 ret = snd_soc_component_read(component, reg, &val);
2859 if (ret)
2860 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002861
2862 ucontrol->value.integer.value[0] =
2863 ((signed char)(val & 0xff))-min;
2864 ucontrol->value.integer.value[1] =
2865 ((signed char)((val >> 8) & 0xff))-min;
2866 return 0;
2867}
2868EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2869
2870/**
2871 * snd_soc_put_volsw_sgn - signed mixer put callback
2872 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002873 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002874 *
2875 * Callback to set the value of a signed mixer control.
2876 *
2877 * Returns 0 for success.
2878 */
2879int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002882 struct soc_mixer_control *mc =
2883 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002884 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002885 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002886 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002887 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002888
2889 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2890 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2891
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002892 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002893}
2894EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2895
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002896/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002897 * snd_soc_info_volsw_range - single mixer info callback with range.
2898 * @kcontrol: mixer control
2899 * @uinfo: control element information
2900 *
2901 * Callback to provide information, within a range, about a single
2902 * mixer control.
2903 *
2904 * returns 0 for success.
2905 */
2906int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_info *uinfo)
2908{
2909 struct soc_mixer_control *mc =
2910 (struct soc_mixer_control *)kcontrol->private_value;
2911 int platform_max;
2912 int min = mc->min;
2913
2914 if (!mc->platform_max)
2915 mc->platform_max = mc->max;
2916 platform_max = mc->platform_max;
2917
2918 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002919 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002920 uinfo->value.integer.min = 0;
2921 uinfo->value.integer.max = platform_max - min;
2922
2923 return 0;
2924}
2925EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2926
2927/**
2928 * snd_soc_put_volsw_range - single mixer put value callback with range.
2929 * @kcontrol: mixer control
2930 * @ucontrol: control element information
2931 *
2932 * Callback to set the value, within a range, for a single mixer control.
2933 *
2934 * Returns 0 for success.
2935 */
2936int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
2938{
2939 struct soc_mixer_control *mc =
2940 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002941 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002942 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002943 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002944 unsigned int shift = mc->shift;
2945 int min = mc->min;
2946 int max = mc->max;
2947 unsigned int mask = (1 << fls(max)) - 1;
2948 unsigned int invert = mc->invert;
2949 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002950 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002951
2952 val = ((ucontrol->value.integer.value[0] + min) & mask);
2953 if (invert)
2954 val = max - val;
2955 val_mask = mask << shift;
2956 val = val << shift;
2957
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002958 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002959 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002960 return ret;
2961
2962 if (snd_soc_volsw_is_stereo(mc)) {
2963 val = ((ucontrol->value.integer.value[1] + min) & mask);
2964 if (invert)
2965 val = max - val;
2966 val_mask = mask << shift;
2967 val = val << shift;
2968
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002969 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2970 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002971 }
2972
2973 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002974}
2975EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2976
2977/**
2978 * snd_soc_get_volsw_range - single mixer get callback with range
2979 * @kcontrol: mixer control
2980 * @ucontrol: control element information
2981 *
2982 * Callback to get the value, within a range, of a single mixer control.
2983 *
2984 * Returns 0 for success.
2985 */
2986int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2987 struct snd_ctl_elem_value *ucontrol)
2988{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002989 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002990 struct soc_mixer_control *mc =
2991 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002992 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002993 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994 unsigned int shift = mc->shift;
2995 int min = mc->min;
2996 int max = mc->max;
2997 unsigned int mask = (1 << fls(max)) - 1;
2998 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002999 unsigned int val;
3000 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003001
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003002 ret = snd_soc_component_read(component, reg, &val);
3003 if (ret)
3004 return ret;
3005
3006 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003007 if (invert)
3008 ucontrol->value.integer.value[0] =
3009 max - ucontrol->value.integer.value[0];
3010 ucontrol->value.integer.value[0] =
3011 ucontrol->value.integer.value[0] - min;
3012
Mark Brown9bde4f02012-12-19 16:05:00 +00003013 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003014 ret = snd_soc_component_read(component, rreg, &val);
3015 if (ret)
3016 return ret;
3017
3018 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003019 if (invert)
3020 ucontrol->value.integer.value[1] =
3021 max - ucontrol->value.integer.value[1];
3022 ucontrol->value.integer.value[1] =
3023 ucontrol->value.integer.value[1] - min;
3024 }
3025
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003026 return 0;
3027}
3028EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3029
3030/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003031 * snd_soc_limit_volume - Set new limit to an existing volume control.
3032 *
3033 * @codec: where to look for the control
3034 * @name: Name of the control
3035 * @max: new maximum limit
3036 *
3037 * Return 0 for success, else error.
3038 */
3039int snd_soc_limit_volume(struct snd_soc_codec *codec,
3040 const char *name, int max)
3041{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003042 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003043 struct snd_kcontrol *kctl;
3044 struct soc_mixer_control *mc;
3045 int found = 0;
3046 int ret = -EINVAL;
3047
3048 /* Sanity check for name and max */
3049 if (unlikely(!name || max <= 0))
3050 return -EINVAL;
3051
3052 list_for_each_entry(kctl, &card->controls, list) {
3053 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3054 found = 1;
3055 break;
3056 }
3057 }
3058 if (found) {
3059 mc = (struct soc_mixer_control *)kctl->private_value;
3060 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003061 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003062 ret = 0;
3063 }
3064 }
3065 return ret;
3066}
3067EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3068
Mark Brown71d08512011-10-10 18:31:26 +01003069int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_info *uinfo)
3071{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003072 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003073 struct soc_bytes *params = (void *)kcontrol->private_value;
3074
3075 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003076 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003077
3078 return 0;
3079}
3080EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3081
3082int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3084{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003085 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003086 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003087 int ret;
3088
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003089 if (component->regmap)
3090 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003091 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003092 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003093 else
3094 ret = -EINVAL;
3095
Mark Brownf831b052012-02-17 16:20:33 -08003096 /* Hide any masked bytes to ensure consistent data reporting */
3097 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003098 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003099 case 1:
3100 ucontrol->value.bytes.data[0] &= ~params->mask;
3101 break;
3102 case 2:
3103 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003104 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003105 break;
3106 case 4:
3107 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003108 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003109 break;
3110 default:
3111 return -EINVAL;
3112 }
3113 }
3114
Mark Brown71d08512011-10-10 18:31:26 +01003115 return ret;
3116}
3117EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3118
3119int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003122 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003123 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003124 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003125 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003126 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003127
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003128 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003129 return -EINVAL;
3130
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003131 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003132
Mark Brownb5a8fe42013-01-20 21:42:22 +09003133 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3134 if (!data)
3135 return -ENOMEM;
3136
Mark Brownf831b052012-02-17 16:20:33 -08003137 /*
3138 * If we've got a mask then we need to preserve the register
3139 * bits. We shouldn't modify the incoming data so take a
3140 * copy.
3141 */
3142 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003143 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003144 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003145 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003146
3147 val &= params->mask;
3148
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003149 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003150 case 1:
3151 ((u8 *)data)[0] &= ~params->mask;
3152 ((u8 *)data)[0] |= val;
3153 break;
3154 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003155 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003156 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003157 &mask, &mask);
3158 if (ret != 0)
3159 goto out;
3160
3161 ((u16 *)data)[0] &= mask;
3162
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003163 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003164 &val, &val);
3165 if (ret != 0)
3166 goto out;
3167
3168 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003169 break;
3170 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003171 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003172 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003173 &mask, &mask);
3174 if (ret != 0)
3175 goto out;
3176
3177 ((u32 *)data)[0] &= mask;
3178
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003179 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003180 &val, &val);
3181 if (ret != 0)
3182 goto out;
3183
3184 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003185 break;
3186 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003187 ret = -EINVAL;
3188 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003189 }
3190 }
3191
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003192 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003193 data, len);
3194
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003195out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003196 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003197
3198 return ret;
3199}
3200EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3201
Vinod Kould9881202014-05-02 10:58:11 +05303202int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_info *ucontrol)
3204{
3205 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3206
3207 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3208 ucontrol->count = params->max;
3209
3210 return 0;
3211}
3212EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3213
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303214int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3215 unsigned int size, unsigned int __user *tlv)
3216{
3217 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3218 unsigned int count = size < params->max ? size : params->max;
3219 int ret = -ENXIO;
3220
3221 switch (op_flag) {
3222 case SNDRV_CTL_TLV_OP_READ:
3223 if (params->get)
3224 ret = params->get(tlv, count);
3225 break;
3226 case SNDRV_CTL_TLV_OP_WRITE:
3227 if (params->put)
3228 ret = params->put(tlv, count);
3229 break;
3230 }
3231 return ret;
3232}
3233EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3234
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003235/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003236 * snd_soc_info_xr_sx - signed multi register info callback
3237 * @kcontrol: mreg control
3238 * @uinfo: control element information
3239 *
3240 * Callback to provide information of a control that can
3241 * span multiple codec registers which together
3242 * forms a single signed value in a MSB/LSB manner.
3243 *
3244 * Returns 0 for success.
3245 */
3246int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_info *uinfo)
3248{
3249 struct soc_mreg_control *mc =
3250 (struct soc_mreg_control *)kcontrol->private_value;
3251 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3252 uinfo->count = 1;
3253 uinfo->value.integer.min = mc->min;
3254 uinfo->value.integer.max = mc->max;
3255
3256 return 0;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3259
3260/**
3261 * snd_soc_get_xr_sx - signed multi register get callback
3262 * @kcontrol: mreg control
3263 * @ucontrol: control element information
3264 *
3265 * Callback to get the value of a control that can span
3266 * multiple codec registers which together forms a single
3267 * signed value in a MSB/LSB manner. The control supports
3268 * specifying total no of bits used to allow for bitfields
3269 * across the multiple codec registers.
3270 *
3271 * Returns 0 for success.
3272 */
3273int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3275{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003276 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003277 struct soc_mreg_control *mc =
3278 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003279 unsigned int regbase = mc->regbase;
3280 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003281 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003282 unsigned int regwmask = (1<<regwshift)-1;
3283 unsigned int invert = mc->invert;
3284 unsigned long mask = (1UL<<mc->nbits)-1;
3285 long min = mc->min;
3286 long max = mc->max;
3287 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003288 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003289 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003290 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003291
3292 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003293 ret = snd_soc_component_read(component, regbase+i, &regval);
3294 if (ret)
3295 return ret;
3296 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003297 }
3298 val &= mask;
3299 if (min < 0 && val > max)
3300 val |= ~mask;
3301 if (invert)
3302 val = max - val;
3303 ucontrol->value.integer.value[0] = val;
3304
3305 return 0;
3306}
3307EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3308
3309/**
3310 * snd_soc_put_xr_sx - signed multi register get callback
3311 * @kcontrol: mreg control
3312 * @ucontrol: control element information
3313 *
3314 * Callback to set the value of a control that can span
3315 * multiple codec registers which together forms a single
3316 * signed value in a MSB/LSB manner. The control supports
3317 * specifying total no of bits used to allow for bitfields
3318 * across the multiple codec registers.
3319 *
3320 * Returns 0 for success.
3321 */
3322int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3323 struct snd_ctl_elem_value *ucontrol)
3324{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003325 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003326 struct soc_mreg_control *mc =
3327 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003328 unsigned int regbase = mc->regbase;
3329 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003330 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003331 unsigned int regwmask = (1<<regwshift)-1;
3332 unsigned int invert = mc->invert;
3333 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003334 long max = mc->max;
3335 long val = ucontrol->value.integer.value[0];
3336 unsigned int i, regval, regmask;
3337 int err;
3338
3339 if (invert)
3340 val = max - val;
3341 val &= mask;
3342 for (i = 0; i < regcount; i++) {
3343 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3344 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003345 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003346 regmask, regval);
3347 if (err < 0)
3348 return err;
3349 }
3350
3351 return 0;
3352}
3353EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3354
3355/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003356 * snd_soc_get_strobe - strobe get callback
3357 * @kcontrol: mixer control
3358 * @ucontrol: control element information
3359 *
3360 * Callback get the value of a strobe mixer control.
3361 *
3362 * Returns 0 for success.
3363 */
3364int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3366{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003367 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003368 struct soc_mixer_control *mc =
3369 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003370 unsigned int reg = mc->reg;
3371 unsigned int shift = mc->shift;
3372 unsigned int mask = 1 << shift;
3373 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003374 unsigned int val;
3375 int ret;
3376
3377 ret = snd_soc_component_read(component, reg, &val);
3378 if (ret)
3379 return ret;
3380
3381 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003382
3383 if (shift != 0 && val != 0)
3384 val = val >> shift;
3385 ucontrol->value.enumerated.item[0] = val ^ invert;
3386
3387 return 0;
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3390
3391/**
3392 * snd_soc_put_strobe - strobe put callback
3393 * @kcontrol: mixer control
3394 * @ucontrol: control element information
3395 *
3396 * Callback strobe a register bit to high then low (or the inverse)
3397 * in one pass of a single mixer enum control.
3398 *
3399 * Returns 1 for success.
3400 */
3401int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3403{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003404 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003405 struct soc_mixer_control *mc =
3406 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003407 unsigned int reg = mc->reg;
3408 unsigned int shift = mc->shift;
3409 unsigned int mask = 1 << shift;
3410 unsigned int invert = mc->invert != 0;
3411 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3412 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3413 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3414 int err;
3415
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003416 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003417 if (err < 0)
3418 return err;
3419
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003420 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003421}
3422EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3423
3424/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003425 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3426 * @dai: DAI
3427 * @clk_id: DAI specific clock ID
3428 * @freq: new clock frequency in Hz
3429 * @dir: new clock direction - input/output.
3430 *
3431 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3432 */
3433int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3434 unsigned int freq, int dir)
3435{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003436 if (dai->driver && dai->driver->ops->set_sysclk)
3437 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003438 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003439 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003440 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003441 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003442 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003443}
3444EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3445
3446/**
Mark Brownec4ee522011-03-07 20:58:11 +00003447 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3448 * @codec: CODEC
3449 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003450 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003451 * @freq: new clock frequency in Hz
3452 * @dir: new clock direction - input/output.
3453 *
3454 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3455 */
3456int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003457 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003458{
3459 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003460 return codec->driver->set_sysclk(codec, clk_id, source,
3461 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003462 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003463 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003464}
3465EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3466
3467/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3469 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003470 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003471 * @div: new clock divisor.
3472 *
3473 * Configures the clock dividers. This is used to derive the best DAI bit and
3474 * frame clocks from the system or master clock. It's best to set the DAI bit
3475 * and frame clocks as low as possible to save system power.
3476 */
3477int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3478 int div_id, int div)
3479{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480 if (dai->driver && dai->driver->ops->set_clkdiv)
3481 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 else
3483 return -EINVAL;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3486
3487/**
3488 * snd_soc_dai_set_pll - configure DAI PLL.
3489 * @dai: DAI
3490 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003491 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 * @freq_in: PLL input clock frequency in Hz
3493 * @freq_out: requested PLL output clock frequency in Hz
3494 *
3495 * Configures and enables PLL to generate output clock based on input clock.
3496 */
Mark Brown85488032009-09-05 18:52:16 +01003497int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3498 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003499{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 if (dai->driver && dai->driver->ops->set_pll)
3501 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003502 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003503 else if (dai->codec && dai->codec->driver->set_pll)
3504 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3505 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506 else
3507 return -EINVAL;
3508}
3509EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3510
Mark Brownec4ee522011-03-07 20:58:11 +00003511/*
3512 * snd_soc_codec_set_pll - configure codec PLL.
3513 * @codec: CODEC
3514 * @pll_id: DAI specific PLL ID
3515 * @source: DAI specific source for the PLL
3516 * @freq_in: PLL input clock frequency in Hz
3517 * @freq_out: requested PLL output clock frequency in Hz
3518 *
3519 * Configures and enables PLL to generate output clock based on input clock.
3520 */
3521int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3522 unsigned int freq_in, unsigned int freq_out)
3523{
3524 if (codec->driver->set_pll)
3525 return codec->driver->set_pll(codec, pll_id, source,
3526 freq_in, freq_out);
3527 else
3528 return -EINVAL;
3529}
3530EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3531
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003532/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003533 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3534 * @dai: DAI
3535 * @ratio Ratio of BCLK to Sample rate.
3536 *
3537 * Configures the DAI for a preset BCLK to sample rate ratio.
3538 */
3539int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3540{
3541 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3542 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3543 else
3544 return -EINVAL;
3545}
3546EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3547
3548/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003549 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3550 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003551 * @fmt: SND_SOC_DAIFMT_ format value.
3552 *
3553 * Configures the DAI hardware format and clocking.
3554 */
3555int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3556{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003557 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003558 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003559 if (dai->driver->ops->set_fmt == NULL)
3560 return -ENOTSUPP;
3561 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562}
3563EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3564
3565/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003566 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003567 * @slots: Number of slots in use.
3568 * @tx_mask: bitmask representing active TX slots.
3569 * @rx_mask: bitmask representing active RX slots.
3570 *
3571 * Generates the TDM tx and rx slot default masks for DAI.
3572 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003573static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003574 unsigned int *tx_mask,
3575 unsigned int *rx_mask)
3576{
3577 if (*tx_mask || *rx_mask)
3578 return 0;
3579
3580 if (!slots)
3581 return -EINVAL;
3582
3583 *tx_mask = (1 << slots) - 1;
3584 *rx_mask = (1 << slots) - 1;
3585
3586 return 0;
3587}
3588
3589/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003590 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3591 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003592 * @tx_mask: bitmask representing active TX slots.
3593 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003594 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003595 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003596 *
3597 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3598 * specific.
3599 */
3600int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003601 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003602{
Xiubo Lie5c21512014-03-21 14:17:12 +08003603 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3604 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003605 &tx_mask, &rx_mask);
3606 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003607 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003608
Benoit Cousson88bd8702014-07-08 23:19:34 +02003609 dai->tx_mask = tx_mask;
3610 dai->rx_mask = rx_mask;
3611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003612 if (dai->driver && dai->driver->ops->set_tdm_slot)
3613 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003614 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003615 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003616 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003617}
3618EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3619
3620/**
Barry Song472df3c2009-09-12 01:16:29 +08003621 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3622 * @dai: DAI
3623 * @tx_num: how many TX channels
3624 * @tx_slot: pointer to an array which imply the TX slot number channel
3625 * 0~num-1 uses
3626 * @rx_num: how many RX channels
3627 * @rx_slot: pointer to an array which imply the RX slot number channel
3628 * 0~num-1 uses
3629 *
3630 * configure the relationship between channel number and TDM slot number.
3631 */
3632int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3633 unsigned int tx_num, unsigned int *tx_slot,
3634 unsigned int rx_num, unsigned int *rx_slot)
3635{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003636 if (dai->driver && dai->driver->ops->set_channel_map)
3637 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003638 rx_num, rx_slot);
3639 else
3640 return -EINVAL;
3641}
3642EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3643
3644/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003645 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3646 * @dai: DAI
3647 * @tristate: tristate enable
3648 *
3649 * Tristates the DAI so that others can use it.
3650 */
3651int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3652{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003653 if (dai->driver && dai->driver->ops->set_tristate)
3654 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003655 else
3656 return -EINVAL;
3657}
3658EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3659
3660/**
3661 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3662 * @dai: DAI
3663 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003664 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665 *
3666 * Mutes the DAI DAC.
3667 */
Mark Brownda183962013-02-06 15:44:07 +00003668int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3669 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003670{
Mark Brownda183962013-02-06 15:44:07 +00003671 if (!dai->driver)
3672 return -ENOTSUPP;
3673
3674 if (dai->driver->ops->mute_stream)
3675 return dai->driver->ops->mute_stream(dai, mute, direction);
3676 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3677 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003679 else
Mark Brown04570c62012-04-13 19:16:03 +01003680 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003681}
3682EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3683
Benoit Cousson88bd8702014-07-08 23:19:34 +02003684static int snd_soc_init_multicodec(struct snd_soc_card *card,
3685 struct snd_soc_dai_link *dai_link)
3686{
3687 /* Legacy codec/codec_dai link is a single entry in multicodec */
3688 if (dai_link->codec_name || dai_link->codec_of_node ||
3689 dai_link->codec_dai_name) {
3690 dai_link->num_codecs = 1;
3691
3692 dai_link->codecs = devm_kzalloc(card->dev,
3693 sizeof(struct snd_soc_dai_link_component),
3694 GFP_KERNEL);
3695 if (!dai_link->codecs)
3696 return -ENOMEM;
3697
3698 dai_link->codecs[0].name = dai_link->codec_name;
3699 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3700 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3701 }
3702
3703 if (!dai_link->codecs) {
3704 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3705 return -EINVAL;
3706 }
3707
3708 return 0;
3709}
3710
Mark Brownc5af3a22008-11-28 13:29:45 +00003711/**
3712 * snd_soc_register_card - Register a card with the ASoC core
3713 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003714 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003715 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003716 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303717int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003718{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003719 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003720
Mark Brownc5af3a22008-11-28 13:29:45 +00003721 if (!card->name || !card->dev)
3722 return -EINVAL;
3723
Stephen Warren5a504962011-12-21 10:40:59 -07003724 for (i = 0; i < card->num_links; i++) {
3725 struct snd_soc_dai_link *link = &card->dai_link[i];
3726
Benoit Cousson88bd8702014-07-08 23:19:34 +02003727 ret = snd_soc_init_multicodec(card, link);
3728 if (ret) {
3729 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3730 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003731 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003732
3733 for (j = 0; j < link->num_codecs; j++) {
3734 /*
3735 * Codec must be specified by 1 of name or OF node,
3736 * not both or neither.
3737 */
3738 if (!!link->codecs[j].name ==
3739 !!link->codecs[j].of_node) {
3740 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3741 link->name);
3742 return -EINVAL;
3743 }
3744 /* Codec DAI name must be specified */
3745 if (!link->codecs[j].dai_name) {
3746 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3747 link->name);
3748 return -EINVAL;
3749 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003750 }
Stephen Warren5a504962011-12-21 10:40:59 -07003751
3752 /*
3753 * Platform may be specified by either name or OF node, but
3754 * can be left unspecified, and a dummy platform will be used.
3755 */
3756 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003757 dev_err(card->dev,
3758 "ASoC: Both platform name/of_node are set for %s\n",
3759 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003760 return -EINVAL;
3761 }
3762
3763 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003764 * CPU device may be specified by either name or OF node, but
3765 * can be left unspecified, and will be matched based on DAI
3766 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003767 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003768 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003769 dev_err(card->dev,
3770 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3771 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003772 return -EINVAL;
3773 }
3774 /*
3775 * At least one of CPU DAI name or CPU device name/node must be
3776 * specified
3777 */
3778 if (!link->cpu_dai_name &&
3779 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003780 dev_err(card->dev,
3781 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3782 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003783 return -EINVAL;
3784 }
3785 }
3786
Mark Browned77cc12011-05-03 18:25:34 +01003787 dev_set_drvdata(card->dev, card);
3788
Stephen Warren111c6412011-01-28 14:26:35 -07003789 snd_soc_initialize_card_lists(card);
3790
Vinod Koul150dd2f2011-01-13 22:48:32 +05303791 soc_init_card_debugfs(card);
3792
Mark Brown181a6892012-03-14 20:18:49 +00003793 card->rtd = devm_kzalloc(card->dev,
3794 sizeof(struct snd_soc_pcm_runtime) *
3795 (card->num_links + card->num_aux_devs),
3796 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797 if (card->rtd == NULL)
3798 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003799 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003800 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003801
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003802 for (i = 0; i < card->num_links; i++) {
3803 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003804 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003805 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3806 sizeof(struct snd_soc_dai *) *
3807 (card->rtd[i].dai_link->num_codecs),
3808 GFP_KERNEL);
3809 if (card->rtd[i].codec_dais == NULL)
3810 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003811 }
3812
3813 for (i = 0; i < card->num_aux_devs; i++)
3814 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815
Mark Browndb432b42011-10-03 21:06:40 +01003816 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003817 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003818 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003819 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003820
Mark Brownb19e6e72012-03-14 21:18:39 +00003821 ret = snd_soc_instantiate_card(card);
3822 if (ret != 0)
3823 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003824
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003825 /* deactivate pins to sleep state */
3826 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003827 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3828 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3829 int j;
3830
3831 for (j = 0; j < rtd->num_codecs; j++) {
3832 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3833 if (!codec_dai->active)
3834 pinctrl_pm_select_sleep_state(codec_dai->dev);
3835 }
3836
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003837 if (!cpu_dai->active)
3838 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3839 }
3840
Mark Brownb19e6e72012-03-14 21:18:39 +00003841 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003842}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303843EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003844
3845/**
3846 * snd_soc_unregister_card - Unregister a card with the ASoC core
3847 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003848 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003849 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003850 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303851int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003852{
Vinod Koulb0e26482011-01-13 22:48:02 +05303853 if (card->instantiated)
3854 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003855 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003856
3857 return 0;
3858}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303859EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003860
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003861/*
3862 * Simplify DAI link configuration by removing ".-1" from device names
3863 * and sanitizing names.
3864 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003865static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003866{
3867 char *found, name[NAME_SIZE];
3868 int id1, id2;
3869
3870 if (dev_name(dev) == NULL)
3871 return NULL;
3872
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003873 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003874
3875 /* are we a "%s.%d" name (platform and SPI components) */
3876 found = strstr(name, dev->driver->name);
3877 if (found) {
3878 /* get ID */
3879 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3880
3881 /* discard ID from name if ID == -1 */
3882 if (*id == -1)
3883 found[strlen(dev->driver->name)] = '\0';
3884 }
3885
3886 } else {
3887 /* I2C component devices are named "bus-addr" */
3888 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3889 char tmp[NAME_SIZE];
3890
3891 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003892 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003893
3894 /* sanitize component name for DAI link creation */
3895 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003896 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897 } else
3898 *id = 0;
3899 }
3900
3901 return kstrdup(name, GFP_KERNEL);
3902}
3903
3904/*
3905 * Simplify DAI link naming for single devices with multiple DAIs by removing
3906 * any ".-1" and using the DAI name (instead of device name).
3907 */
3908static inline char *fmt_multiple_name(struct device *dev,
3909 struct snd_soc_dai_driver *dai_drv)
3910{
3911 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003912 dev_err(dev,
3913 "ASoC: error - multiple DAI %s registered with no name\n",
3914 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915 return NULL;
3916 }
3917
3918 return kstrdup(dai_drv->name, GFP_KERNEL);
3919}
3920
Mark Brown91151712008-11-30 23:31:24 +00003921/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003922 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003923 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003924 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003925 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003926static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003927{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003928 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003929
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003930 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003931 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3932 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003933 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003934 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003935 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003936 }
Mark Brown91151712008-11-30 23:31:24 +00003937}
Mark Brown91151712008-11-30 23:31:24 +00003938
3939/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003940 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003941 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003942 * @component: The component the DAIs are registered for
3943 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003944 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003945 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3946 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003947 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003948static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003949 struct snd_soc_dai_driver *dai_drv, size_t count,
3950 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003951{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003952 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003954 unsigned int i;
3955 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003957 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003958
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003959 component->dai_drv = dai_drv;
3960 component->num_dai = count;
3961
Mark Brown91151712008-11-30 23:31:24 +00003962 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003963
3964 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003965 if (dai == NULL) {
3966 ret = -ENOMEM;
3967 goto err;
3968 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003969
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003970 /*
3971 * Back in the old days when we still had component-less DAIs,
3972 * instead of having a static name, component-less DAIs would
3973 * inherit the name of the parent device so it is possible to
3974 * register multiple instances of the DAI. We still need to keep
3975 * the same naming style even though those DAIs are not
3976 * component-less anymore.
3977 */
3978 if (count == 1 && legacy_dai_naming) {
3979 dai->name = fmt_single_name(dev, &dai->id);
3980 } else {
3981 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3982 if (dai_drv[i].id)
3983 dai->id = dai_drv[i].id;
3984 else
3985 dai->id = i;
3986 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003987 if (dai->name == NULL) {
3988 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003989 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003990 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003991 }
3992
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003993 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003995 dai->driver = &dai_drv[i];
3996 if (!dai->driver->ops)
3997 dai->driver->ops = &null_dai_ops;
3998
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003999 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004000
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004001 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004002 }
4003
4004 return 0;
4005
4006err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004007 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004008
4009 return ret;
4010}
Mark Brown91151712008-11-30 23:31:24 +00004011
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004012static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4013 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004014{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004015 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004016
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004017 component->driver->seq_notifier(component, type, subseq);
4018}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004019
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004020static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4021 int event)
4022{
4023 struct snd_soc_component *component = dapm->component;
4024
4025 return component->driver->stream_event(component, event);
4026}
4027
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004028static int snd_soc_component_initialize(struct snd_soc_component *component,
4029 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004030{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004031 struct snd_soc_dapm_context *dapm;
4032
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004033 component->name = fmt_single_name(dev, &component->id);
4034 if (!component->name) {
4035 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004036 return -ENOMEM;
4037 }
4038
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004039 component->dev = dev;
4040 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004041 component->probe = component->driver->probe;
4042 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004043
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004044 if (!component->dapm_ptr)
4045 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004046
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004047 dapm = component->dapm_ptr;
4048 dapm->dev = dev;
4049 dapm->component = component;
4050 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004051 if (driver->seq_notifier)
4052 dapm->seq_notifier = snd_soc_component_seq_notifier;
4053 if (driver->stream_event)
4054 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004055
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004056 component->controls = driver->controls;
4057 component->num_controls = driver->num_controls;
4058 component->dapm_widgets = driver->dapm_widgets;
4059 component->num_dapm_widgets = driver->num_dapm_widgets;
4060 component->dapm_routes = driver->dapm_routes;
4061 component->num_dapm_routes = driver->num_dapm_routes;
4062
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004063 INIT_LIST_HEAD(&component->dai_list);
4064 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004065
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004066 return 0;
4067}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004068
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004069static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4070{
4071 list_add(&component->list, &component_list);
4072}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004073
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004074static void snd_soc_component_add(struct snd_soc_component *component)
4075{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004076 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004077 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004078 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004079}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004080
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004081static void snd_soc_component_cleanup(struct snd_soc_component *component)
4082{
4083 snd_soc_unregister_dais(component);
4084 kfree(component->name);
4085}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004086
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004087static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4088{
4089 list_del(&component->list);
4090}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004091
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004092static void snd_soc_component_del(struct snd_soc_component *component)
4093{
4094 mutex_lock(&client_mutex);
4095 snd_soc_component_del_unlocked(component);
4096 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004097}
4098
4099int snd_soc_register_component(struct device *dev,
4100 const struct snd_soc_component_driver *cmpnt_drv,
4101 struct snd_soc_dai_driver *dai_drv,
4102 int num_dai)
4103{
4104 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004105 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004106
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004107 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004108 if (!cmpnt) {
4109 dev_err(dev, "ASoC: Failed to allocate memory\n");
4110 return -ENOMEM;
4111 }
4112
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004113 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4114 if (ret)
4115 goto err_free;
4116
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004117 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004118 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004119
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004120 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4121 if (ret < 0) {
4122 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4123 goto err_cleanup;
4124 }
4125
4126 snd_soc_component_add(cmpnt);
4127
4128 return 0;
4129
4130err_cleanup:
4131 snd_soc_component_cleanup(cmpnt);
4132err_free:
4133 kfree(cmpnt);
4134 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004135}
4136EXPORT_SYMBOL_GPL(snd_soc_register_component);
4137
4138/**
4139 * snd_soc_unregister_component - Unregister a component from the ASoC core
4140 *
4141 */
4142void snd_soc_unregister_component(struct device *dev)
4143{
4144 struct snd_soc_component *cmpnt;
4145
4146 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004147 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004148 goto found;
4149 }
4150 return;
4151
4152found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004153 snd_soc_component_del(cmpnt);
4154 snd_soc_component_cleanup(cmpnt);
4155 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004156}
4157EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4158
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004159static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4160{
4161 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4162
4163 return platform->driver->probe(platform);
4164}
4165
4166static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4167{
4168 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4169
4170 platform->driver->remove(platform);
4171}
4172
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004173static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4174 unsigned int reg, unsigned int val)
4175{
4176 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4177
4178 return platform->driver->write(platform, reg, val);
4179}
4180
4181static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4182 unsigned int reg, unsigned int *val)
4183{
4184 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4185
4186 *val = platform->driver->read(platform, reg);
4187
4188 return 0;
4189}
4190
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004191/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004192 * snd_soc_add_platform - Add a platform to the ASoC core
4193 * @dev: The parent device for the platform
4194 * @platform: The platform to add
4195 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004196 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004197int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004198 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004199{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004200 int ret;
4201
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004202 ret = snd_soc_component_initialize(&platform->component,
4203 &platform_drv->component_driver, dev);
4204 if (ret)
4205 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004206
4207 platform->dev = dev;
4208 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004209 if (platform_drv->controls) {
4210 platform->component.controls = platform_drv->controls;
4211 platform->component.num_controls = platform_drv->num_controls;
4212 }
4213 if (platform_drv->dapm_widgets) {
4214 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4215 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4216 platform->component.steal_sibling_dai_widgets = true;
4217 }
4218 if (platform_drv->dapm_routes) {
4219 platform->component.dapm_routes = platform_drv->dapm_routes;
4220 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4221 }
4222
4223 if (platform_drv->probe)
4224 platform->component.probe = snd_soc_platform_drv_probe;
4225 if (platform_drv->remove)
4226 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004227 if (platform_drv->write)
4228 platform->component.write = snd_soc_platform_drv_write;
4229 if (platform_drv->read)
4230 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004231
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004232#ifdef CONFIG_DEBUG_FS
4233 platform->component.debugfs_prefix = "platform";
4234#endif
4235
Mark Brown12a48a8c2008-12-03 19:40:30 +00004236 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004237 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004238 list_add(&platform->list, &platform_list);
4239 mutex_unlock(&client_mutex);
4240
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004241 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4242 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004243
4244 return 0;
4245}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004246EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4247
4248/**
4249 * snd_soc_register_platform - Register a platform with the ASoC core
4250 *
4251 * @platform: platform to register
4252 */
4253int snd_soc_register_platform(struct device *dev,
4254 const struct snd_soc_platform_driver *platform_drv)
4255{
4256 struct snd_soc_platform *platform;
4257 int ret;
4258
4259 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4260
4261 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4262 if (platform == NULL)
4263 return -ENOMEM;
4264
4265 ret = snd_soc_add_platform(dev, platform, platform_drv);
4266 if (ret)
4267 kfree(platform);
4268
4269 return ret;
4270}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004271EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4272
4273/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004274 * snd_soc_remove_platform - Remove a platform from the ASoC core
4275 * @platform: the platform to remove
4276 */
4277void snd_soc_remove_platform(struct snd_soc_platform *platform)
4278{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004279
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004280 mutex_lock(&client_mutex);
4281 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004282 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004283 mutex_unlock(&client_mutex);
4284
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004285 snd_soc_component_cleanup(&platform->component);
4286
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004287 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004288 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004289}
4290EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4291
4292struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4293{
4294 struct snd_soc_platform *platform;
4295
4296 list_for_each_entry(platform, &platform_list, list) {
4297 if (dev == platform->dev)
4298 return platform;
4299 }
4300
4301 return NULL;
4302}
4303EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4304
4305/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004306 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4307 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004308 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004309 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004310void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004311{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004312 struct snd_soc_platform *platform;
4313
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004314 platform = snd_soc_lookup_platform(dev);
4315 if (!platform)
4316 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004317
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004318 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004319 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004320}
4321EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4322
Mark Brown151ab222009-05-09 16:22:58 +01004323static u64 codec_format_map[] = {
4324 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4325 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4326 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4327 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4328 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4329 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4330 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4331 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4332 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4333 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4334 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4335 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4336 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4337 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4338 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4339 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4340};
4341
4342/* Fix up the DAI formats for endianness: codecs don't actually see
4343 * the endianness of the data but we're using the CPU format
4344 * definitions which do need to include endianness so we ensure that
4345 * codec DAIs always have both big and little endian variants set.
4346 */
4347static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4348{
4349 int i;
4350
4351 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4352 if (stream->formats & codec_format_map[i])
4353 stream->formats |= codec_format_map[i];
4354}
4355
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004356static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4357{
4358 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4359
4360 return codec->driver->probe(codec);
4361}
4362
4363static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4364{
4365 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4366
4367 codec->driver->remove(codec);
4368}
4369
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004370static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4371 unsigned int reg, unsigned int val)
4372{
4373 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4374
4375 return codec->driver->write(codec, reg, val);
4376}
4377
4378static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4379 unsigned int reg, unsigned int *val)
4380{
4381 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4382
4383 *val = codec->driver->read(codec, reg);
4384
4385 return 0;
4386}
4387
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004388static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4389 enum snd_soc_bias_level level)
4390{
4391 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4392
4393 return codec->driver->set_bias_level(codec, level);
4394}
4395
Mark Brown0d0cf002008-12-10 14:32:45 +00004396/**
4397 * snd_soc_register_codec - Register a codec with the ASoC core
4398 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004399 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004400 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004401int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004402 const struct snd_soc_codec_driver *codec_drv,
4403 struct snd_soc_dai_driver *dai_drv,
4404 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004405{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004406 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004407 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004408 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004409 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004410
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004411 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004412
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004413 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4414 if (codec == NULL)
4415 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004416
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004417 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004418 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004419
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004420 ret = snd_soc_component_initialize(&codec->component,
4421 &codec_drv->component_driver, dev);
4422 if (ret)
4423 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004424
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004425 if (codec_drv->controls) {
4426 codec->component.controls = codec_drv->controls;
4427 codec->component.num_controls = codec_drv->num_controls;
4428 }
4429 if (codec_drv->dapm_widgets) {
4430 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4431 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4432 }
4433 if (codec_drv->dapm_routes) {
4434 codec->component.dapm_routes = codec_drv->dapm_routes;
4435 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4436 }
4437
4438 if (codec_drv->probe)
4439 codec->component.probe = snd_soc_codec_drv_probe;
4440 if (codec_drv->remove)
4441 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004442 if (codec_drv->write)
4443 codec->component.write = snd_soc_codec_drv_write;
4444 if (codec_drv->read)
4445 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004446 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004447 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004448 if (codec_drv->seq_notifier)
4449 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004450 if (codec_drv->set_bias_level)
4451 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004452 codec->dev = dev;
4453 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004454 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004455 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004456
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004457#ifdef CONFIG_DEBUG_FS
4458 codec->component.init_debugfs = soc_init_codec_debugfs;
4459 codec->component.debugfs_prefix = "codec";
4460#endif
4461
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004462 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004463 if (codec_drv->get_regmap)
4464 regmap = codec_drv->get_regmap(dev);
4465 else
4466 regmap = dev_get_regmap(dev, NULL);
4467
4468 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004469 ret = snd_soc_component_init_io(&codec->component,
4470 regmap);
4471 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004472 dev_err(codec->dev,
4473 "Failed to set cache I/O:%d\n",
4474 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004475 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004476 }
4477 }
4478 }
4479
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004480 for (i = 0; i < num_dai; i++) {
4481 fixup_codec_formats(&dai_drv[i].playback);
4482 fixup_codec_formats(&dai_drv[i].capture);
4483 }
4484
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004485 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4486 if (ret < 0) {
4487 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4488 goto err_cleanup;
4489 }
4490
4491 list_for_each_entry(dai, &codec->component.dai_list, list)
4492 dai->codec = codec;
4493
Mark Brown054880f2012-04-09 17:29:19 +01004494 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004495 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004496 list_add(&codec->list, &codec_list);
4497 mutex_unlock(&client_mutex);
4498
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004499 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4500 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004501 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004502
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004503err_cleanup:
4504 snd_soc_component_cleanup(&codec->component);
4505err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004506 kfree(codec);
4507 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004508}
4509EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4510
4511/**
4512 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4513 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004514 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004515 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004516void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004517{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004518 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004519
4520 list_for_each_entry(codec, &codec_list, list) {
4521 if (dev == codec->dev)
4522 goto found;
4523 }
4524 return;
4525
4526found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004527
Mark Brown0d0cf002008-12-10 14:32:45 +00004528 mutex_lock(&client_mutex);
4529 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004530 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004531 mutex_unlock(&client_mutex);
4532
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004533 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4534 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004535
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004536 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004537 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004538 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004539}
4540EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4541
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004542/* Retrieve a card's name from device tree */
4543int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4544 const char *propname)
4545{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304546 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004547 int ret;
4548
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304549 if (!card->dev) {
4550 pr_err("card->dev is not set before calling %s\n", __func__);
4551 return -EINVAL;
4552 }
4553
4554 np = card->dev->of_node;
4555
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004556 ret = of_property_read_string_index(np, propname, 0, &card->name);
4557 /*
4558 * EINVAL means the property does not exist. This is fine providing
4559 * card->name was previously set, which is checked later in
4560 * snd_soc_register_card.
4561 */
4562 if (ret < 0 && ret != -EINVAL) {
4563 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004564 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004565 propname, ret);
4566 return ret;
4567 }
4568
4569 return 0;
4570}
4571EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4572
Xiubo Li9a6d4862014-02-08 15:59:52 +08004573static const struct snd_soc_dapm_widget simple_widgets[] = {
4574 SND_SOC_DAPM_MIC("Microphone", NULL),
4575 SND_SOC_DAPM_LINE("Line", NULL),
4576 SND_SOC_DAPM_HP("Headphone", NULL),
4577 SND_SOC_DAPM_SPK("Speaker", NULL),
4578};
4579
4580int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4581 const char *propname)
4582{
4583 struct device_node *np = card->dev->of_node;
4584 struct snd_soc_dapm_widget *widgets;
4585 const char *template, *wname;
4586 int i, j, num_widgets, ret;
4587
4588 num_widgets = of_property_count_strings(np, propname);
4589 if (num_widgets < 0) {
4590 dev_err(card->dev,
4591 "ASoC: Property '%s' does not exist\n", propname);
4592 return -EINVAL;
4593 }
4594 if (num_widgets & 1) {
4595 dev_err(card->dev,
4596 "ASoC: Property '%s' length is not even\n", propname);
4597 return -EINVAL;
4598 }
4599
4600 num_widgets /= 2;
4601 if (!num_widgets) {
4602 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4603 propname);
4604 return -EINVAL;
4605 }
4606
4607 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4608 GFP_KERNEL);
4609 if (!widgets) {
4610 dev_err(card->dev,
4611 "ASoC: Could not allocate memory for widgets\n");
4612 return -ENOMEM;
4613 }
4614
4615 for (i = 0; i < num_widgets; i++) {
4616 ret = of_property_read_string_index(np, propname,
4617 2 * i, &template);
4618 if (ret) {
4619 dev_err(card->dev,
4620 "ASoC: Property '%s' index %d read error:%d\n",
4621 propname, 2 * i, ret);
4622 return -EINVAL;
4623 }
4624
4625 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4626 if (!strncmp(template, simple_widgets[j].name,
4627 strlen(simple_widgets[j].name))) {
4628 widgets[i] = simple_widgets[j];
4629 break;
4630 }
4631 }
4632
4633 if (j >= ARRAY_SIZE(simple_widgets)) {
4634 dev_err(card->dev,
4635 "ASoC: DAPM widget '%s' is not supported\n",
4636 template);
4637 return -EINVAL;
4638 }
4639
4640 ret = of_property_read_string_index(np, propname,
4641 (2 * i) + 1,
4642 &wname);
4643 if (ret) {
4644 dev_err(card->dev,
4645 "ASoC: Property '%s' index %d read error:%d\n",
4646 propname, (2 * i) + 1, ret);
4647 return -EINVAL;
4648 }
4649
4650 widgets[i].name = wname;
4651 }
4652
4653 card->dapm_widgets = widgets;
4654 card->num_dapm_widgets = num_widgets;
4655
4656 return 0;
4657}
4658EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4659
Xiubo Li89c67852014-02-14 09:34:35 +08004660int snd_soc_of_parse_tdm_slot(struct device_node *np,
4661 unsigned int *slots,
4662 unsigned int *slot_width)
4663{
4664 u32 val;
4665 int ret;
4666
4667 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4668 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4669 if (ret)
4670 return ret;
4671
4672 if (slots)
4673 *slots = val;
4674 }
4675
4676 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4677 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4678 if (ret)
4679 return ret;
4680
4681 if (slot_width)
4682 *slot_width = val;
4683 }
4684
4685 return 0;
4686}
4687EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4688
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004689int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4690 const char *propname)
4691{
4692 struct device_node *np = card->dev->of_node;
4693 int num_routes;
4694 struct snd_soc_dapm_route *routes;
4695 int i, ret;
4696
4697 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004698 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004699 dev_err(card->dev,
4700 "ASoC: Property '%s' does not exist or its length is not even\n",
4701 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004702 return -EINVAL;
4703 }
4704 num_routes /= 2;
4705 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004706 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004707 propname);
4708 return -EINVAL;
4709 }
4710
4711 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4712 GFP_KERNEL);
4713 if (!routes) {
4714 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004715 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004716 return -EINVAL;
4717 }
4718
4719 for (i = 0; i < num_routes; i++) {
4720 ret = of_property_read_string_index(np, propname,
4721 2 * i, &routes[i].sink);
4722 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004723 dev_err(card->dev,
4724 "ASoC: Property '%s' index %d could not be read: %d\n",
4725 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004726 return -EINVAL;
4727 }
4728 ret = of_property_read_string_index(np, propname,
4729 (2 * i) + 1, &routes[i].source);
4730 if (ret) {
4731 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004732 "ASoC: Property '%s' index %d could not be read: %d\n",
4733 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004734 return -EINVAL;
4735 }
4736 }
4737
4738 card->num_dapm_routes = num_routes;
4739 card->dapm_routes = routes;
4740
4741 return 0;
4742}
4743EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4744
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004745unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004746 const char *prefix,
4747 struct device_node **bitclkmaster,
4748 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004749{
4750 int ret, i;
4751 char prop[128];
4752 unsigned int format = 0;
4753 int bit, frame;
4754 const char *str;
4755 struct {
4756 char *name;
4757 unsigned int val;
4758 } of_fmt_table[] = {
4759 { "i2s", SND_SOC_DAIFMT_I2S },
4760 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4761 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4762 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4763 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4764 { "ac97", SND_SOC_DAIFMT_AC97 },
4765 { "pdm", SND_SOC_DAIFMT_PDM},
4766 { "msb", SND_SOC_DAIFMT_MSB },
4767 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004768 };
4769
4770 if (!prefix)
4771 prefix = "";
4772
4773 /*
4774 * check "[prefix]format = xxx"
4775 * SND_SOC_DAIFMT_FORMAT_MASK area
4776 */
4777 snprintf(prop, sizeof(prop), "%sformat", prefix);
4778 ret = of_property_read_string(np, prop, &str);
4779 if (ret == 0) {
4780 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4781 if (strcmp(str, of_fmt_table[i].name) == 0) {
4782 format |= of_fmt_table[i].val;
4783 break;
4784 }
4785 }
4786 }
4787
4788 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004789 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004790 * SND_SOC_DAIFMT_CLOCK_MASK area
4791 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004792 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4793 if (of_get_property(np, prop, NULL))
4794 format |= SND_SOC_DAIFMT_CONT;
4795 else
4796 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004797
4798 /*
4799 * check "[prefix]bitclock-inversion"
4800 * check "[prefix]frame-inversion"
4801 * SND_SOC_DAIFMT_INV_MASK area
4802 */
4803 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4804 bit = !!of_get_property(np, prop, NULL);
4805
4806 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4807 frame = !!of_get_property(np, prop, NULL);
4808
4809 switch ((bit << 4) + frame) {
4810 case 0x11:
4811 format |= SND_SOC_DAIFMT_IB_IF;
4812 break;
4813 case 0x10:
4814 format |= SND_SOC_DAIFMT_IB_NF;
4815 break;
4816 case 0x01:
4817 format |= SND_SOC_DAIFMT_NB_IF;
4818 break;
4819 default:
4820 /* SND_SOC_DAIFMT_NB_NF is default */
4821 break;
4822 }
4823
4824 /*
4825 * check "[prefix]bitclock-master"
4826 * check "[prefix]frame-master"
4827 * SND_SOC_DAIFMT_MASTER_MASK area
4828 */
4829 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4830 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004831 if (bit && bitclkmaster)
4832 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004833
4834 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4835 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004836 if (frame && framemaster)
4837 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004838
4839 switch ((bit << 4) + frame) {
4840 case 0x11:
4841 format |= SND_SOC_DAIFMT_CBM_CFM;
4842 break;
4843 case 0x10:
4844 format |= SND_SOC_DAIFMT_CBM_CFS;
4845 break;
4846 case 0x01:
4847 format |= SND_SOC_DAIFMT_CBS_CFM;
4848 break;
4849 default:
4850 format |= SND_SOC_DAIFMT_CBS_CFS;
4851 break;
4852 }
4853
4854 return format;
4855}
4856EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4857
Kuninori Morimotocb470082013-09-10 17:39:56 -07004858int snd_soc_of_get_dai_name(struct device_node *of_node,
4859 const char **dai_name)
4860{
4861 struct snd_soc_component *pos;
4862 struct of_phandle_args args;
4863 int ret;
4864
4865 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4866 "#sound-dai-cells", 0, &args);
4867 if (ret)
4868 return ret;
4869
4870 ret = -EPROBE_DEFER;
4871
4872 mutex_lock(&client_mutex);
4873 list_for_each_entry(pos, &component_list, list) {
4874 if (pos->dev->of_node != args.np)
4875 continue;
4876
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004877 if (pos->driver->of_xlate_dai_name) {
4878 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4879 } else {
4880 int id = -1;
4881
4882 switch (args.args_count) {
4883 case 0:
4884 id = 0; /* same as dai_drv[0] */
4885 break;
4886 case 1:
4887 id = args.args[0];
4888 break;
4889 default:
4890 /* not supported */
4891 break;
4892 }
4893
4894 if (id < 0 || id >= pos->num_dai) {
4895 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004896 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004897 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004898
4899 ret = 0;
4900
4901 *dai_name = pos->dai_drv[id].name;
4902 if (!*dai_name)
4903 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004904 }
4905
Kuninori Morimotocb470082013-09-10 17:39:56 -07004906 break;
4907 }
4908 mutex_unlock(&client_mutex);
4909
4910 of_node_put(args.np);
4911
4912 return ret;
4913}
4914EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4915
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004916static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004917{
Mark Brown384c89e2008-12-03 17:34:03 +00004918#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004919 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4920 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004921 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004922 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004923 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004924
Mark Brown8a9dab12011-01-10 22:25:21 +00004925 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004926 &codec_list_fops))
4927 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4928
Mark Brown8a9dab12011-01-10 22:25:21 +00004929 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004930 &dai_list_fops))
4931 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004932
Mark Brown8a9dab12011-01-10 22:25:21 +00004933 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004934 &platform_list_fops))
4935 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004936#endif
4937
Mark Brownfb257892011-04-28 10:57:54 +01004938 snd_soc_util_init();
4939
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004940 return platform_driver_register(&soc_driver);
4941}
Mark Brown4abe8e12010-10-12 17:41:03 +01004942module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004943
Mark Brown7d8c16a2008-11-30 22:11:24 +00004944static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004945{
Mark Brownfb257892011-04-28 10:57:54 +01004946 snd_soc_util_exit();
4947
Mark Brown384c89e2008-12-03 17:34:03 +00004948#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004949 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004950#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004951 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004952}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004953module_exit(snd_soc_exit);
4954
4955/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004956MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004957MODULE_DESCRIPTION("ALSA SoC Core");
4958MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004959MODULE_ALIAS("platform:soc-audio");