blob: 1c705c28389ce409da5c893e36ac27eaaa3cc1da [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000315 &codec->cache_only);
316
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200318 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000323}
324
Mark Brownc3c5a192010-09-15 18:15:14 +0100325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200337 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
Mark Brownf3208782010-09-15 18:19:07 +0100359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100364 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 }
381 }
Mark Brownf3208782010-09-15 18:19:07 +0100382
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
Mark Brown19c7ac22010-09-15 18:22:40 +0100395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200408 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
Mark Brown2b194f9d2010-10-13 10:52:16 +0100417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000432 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200433 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100435 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000444 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#else
453
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200454#define soc_init_codec_debugfs NULL
455
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000458{
459}
460
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463{
464}
465
Axel Linb95fccb2010-11-09 17:06:44 +0800466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200522 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 codec->ac97->dev.release = soc_ac97_device_release;
524
Kay Sieversbb072bf2008-11-02 03:50:35 +0100525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 continue;
576
Benoit Cousson88bd8702014-07-08 23:19:34 +0200577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100586 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100589 continue;
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 /* close any waiting streams and save state */
613 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623
624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 }
635
Mark Browne2d32ff2012-08-31 17:38:32 -0700636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200645 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200655 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000656 break;
657 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100659 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900661 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 }
672 }
673 }
674
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
677
678 if (card->rtd[i].dai_link->ignore_suspend)
679 continue;
680
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 }
687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
691 return 0;
692}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000693EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Andy Green6ed25972008-06-13 16:24:05 +0100695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Andy Green6ed25972008-06-13 16:24:05 +0100705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100710
Mark Brown99497882010-05-07 20:24:05 +0100711 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100722 continue;
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 break;
744 }
Mark Brown1547aba2010-05-07 21:11:40 +0100745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100751 continue;
752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Mark Brown3ff3f642008-05-19 12:32:25 +0200762 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100766 continue;
767
Benoit Cousson88bd8702014-07-08 23:19:34 +0200768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 }
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100782 continue;
783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000793 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000795 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100796
797 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100803}
804
805/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100807{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200810
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800832 }
833
Mark Brown64ab9ba2009-03-31 11:27:03 +0100834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600848 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100850 }
Andy Green6ed25972008-06-13 16:24:05 +0100851
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 return 0;
853}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000854EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858#endif
859
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100860static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800861};
862
Benoit Cousson88bd8702014-07-08 23:19:34 +0200863static struct snd_soc_codec *soc_find_codec(
864 const struct device_node *codec_of_node,
865 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100866{
867 struct snd_soc_codec *codec;
868
869 list_for_each_entry(codec, &codec_list, list) {
870 if (codec_of_node) {
871 if (codec->dev->of_node != codec_of_node)
872 continue;
873 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200874 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100875 continue;
876 }
877
878 return codec;
879 }
880
881 return NULL;
882}
883
884static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
885 const char *codec_dai_name)
886{
887 struct snd_soc_dai *codec_dai;
888
889 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
890 if (!strcmp(codec_dai->name, codec_dai_name)) {
891 return codec_dai;
892 }
893 }
894
895 return NULL;
896}
897
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
901 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100902 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
904 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000905 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100906 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100907 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200908 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200909
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000910 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000911
Mark Brownb19e6e72012-03-14 21:18:39 +0000912 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100913 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600914 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100915 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600916 continue;
917 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100918 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600919 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100920 list_for_each_entry(cpu_dai, &component->dai_list, list) {
921 if (dai_link->cpu_dai_name &&
922 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
923 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700924
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100925 rtd->cpu_dai = cpu_dai;
926 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000928
929 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000930 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000932 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000933 }
934
Benoit Cousson88bd8702014-07-08 23:19:34 +0200935 rtd->num_codecs = dai_link->num_codecs;
936
937 /* Find CODEC from registered CODECs */
938 for (i = 0; i < rtd->num_codecs; i++) {
939 struct snd_soc_codec *codec;
940 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
941 if (!codec) {
942 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
943 codecs[i].name);
944 return -EPROBE_DEFER;
945 }
946
947 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
948 if (!codec_dais[i]) {
949 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
950 codecs[i].dai_name);
951 return -EPROBE_DEFER;
952 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000953 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100954
Benoit Cousson88bd8702014-07-08 23:19:34 +0200955 /* Single codec links expect codec and codec_dai in runtime data */
956 rtd->codec_dai = codec_dais[0];
957 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100958
Mark Brown848dd8b2011-04-27 18:16:32 +0100959 /* if there's no platform we match on the empty platform */
960 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700961 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100962 platform_name = "snd-soc-dummy";
963
Mark Brownb19e6e72012-03-14 21:18:39 +0000964 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700966 if (dai_link->platform_of_node) {
967 if (platform->dev->of_node !=
968 dai_link->platform_of_node)
969 continue;
970 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200971 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700972 continue;
973 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700974
975 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000977 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000978 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000980 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000982
983 card->num_rtd++;
984
985 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986}
987
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200988static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600989{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200990 /* This is a HACK and will be removed soon */
991 if (component->codec)
992 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600993
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200994 if (component->remove)
995 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600996
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200997 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600998
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200999 soc_cleanup_component_debugfs(component);
1000 component->probed = 0;
1001 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001002}
1003
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001004static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1005{
1006 int err;
1007
1008 if (codec_dai && codec_dai->probed &&
1009 codec_dai->driver->remove_order == order) {
1010 if (codec_dai->driver->remove) {
1011 err = codec_dai->driver->remove(codec_dai);
1012 if (err < 0)
1013 dev_err(codec_dai->dev,
1014 "ASoC: failed to remove %s: %d\n",
1015 codec_dai->name, err);
1016 }
1017 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001018 }
1019}
1020
Stephen Warren62ae68f2012-06-08 12:34:23 -06001021static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022{
1023 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001024 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1025 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001026
1027 /* unregister the rtd device */
1028 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001029 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1030 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1031 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032 rtd->dev_registered = 0;
1033 }
1034
1035 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001036 for (i = 0; i < rtd->num_codecs; i++)
1037 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001038
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001040 if (cpu_dai && cpu_dai->probed &&
1041 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042 if (cpu_dai->driver->remove) {
1043 err = cpu_dai->driver->remove(cpu_dai);
1044 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001045 dev_err(cpu_dai->dev,
1046 "ASoC: failed to remove %s: %d\n",
1047 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 }
1049 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001050 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001051 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001052 }
1053}
1054
Stephen Warren62ae68f2012-06-08 12:34:23 -06001055static void soc_remove_link_components(struct snd_soc_card *card, int num,
1056 int order)
1057{
1058 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1059 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001060 struct snd_soc_platform *platform = rtd->platform;
1061 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001062 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001063
1064 /* remove the platform */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001065 if (platform && platform->component.probed &&
1066 platform->component.driver->remove_order == order)
1067 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001068
1069 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001070 for (i = 0; i < rtd->num_codecs; i++) {
1071 codec = rtd->codec_dais[i]->codec;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001072 if (codec && codec->component.probed &&
1073 codec->component.driver->remove_order == order)
1074 soc_remove_component(&codec->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001075 }
1076
1077 /* remove any CPU-side CODEC */
1078 if (cpu_dai) {
1079 codec = cpu_dai->codec;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001080 if (codec && codec->component.probed &&
1081 codec->component.driver->remove_order == order)
1082 soc_remove_component(&codec->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001083 }
1084}
1085
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001086static void soc_remove_dai_links(struct snd_soc_card *card)
1087{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001088 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001089
Liam Girdwood0168bf02011-06-07 16:08:05 +01001090 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1091 order++) {
1092 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001093 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001094 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001095
1096 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1097 order++) {
1098 for (dai = 0; dai < card->num_rtd; dai++)
1099 soc_remove_link_components(card, dai, order);
1100 }
1101
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001102 card->num_rtd = 0;
1103}
1104
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001105static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001106 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001107{
1108 int i;
1109
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001110 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001111 return;
1112
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001113 for (i = 0; i < card->num_configs; i++) {
1114 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001115 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001116 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001117 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001118 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001119 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001120 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001121 }
1122}
1123
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001124static int soc_probe_component(struct snd_soc_card *card,
1125 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001126{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001127 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1128 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001129 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001130 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001132 component->card = card;
1133 dapm->card = card;
1134 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001135
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001136 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001137 return -ENODEV;
1138
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001139 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001140
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001141 if (component->dapm_widgets) {
1142 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1143 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001144
1145 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001147 "Failed to create new controls %d\n", ret);
1148 goto err_probe;
1149 }
1150 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001151
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001152 /*
1153 * This is rather ugly, but certain platforms expect that the DAPM
1154 * widgets for the DAIs for components with the same parent device are
1155 * created in the platforms DAPM context. Until that is fixed we need to
1156 * keep this.
1157 */
1158 if (component->steal_sibling_dai_widgets) {
1159 dai_component = NULL;
1160 list_for_each_entry(component2, &component_list, list) {
1161 if (component == component2)
1162 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001163
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001164 if (component2->dev == component->dev &&
1165 !list_empty(&component2->dai_list)) {
1166 dai_component = component2;
1167 break;
1168 }
1169 }
1170 } else {
1171 dai_component = component;
1172 list_for_each_entry(component2, &component_list, list) {
1173 if (component2->dev == component->dev &&
1174 component2->steal_sibling_dai_widgets) {
1175 dai_component = NULL;
1176 break;
1177 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001178 }
1179 }
Mark Brown888df392012-02-16 19:37:51 -08001180
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001181 if (dai_component) {
1182 list_for_each_entry(dai, &dai_component->dai_list, list) {
1183 snd_soc_dapm_new_dai_widgets(dapm, dai);
1184 if (ret != 0) {
1185 dev_err(component->dev,
1186 "Failed to create DAI widgets %d\n",
1187 ret);
1188 goto err_probe;
1189 }
1190 }
1191 }
Mark Brown33c5f962011-08-22 18:40:30 +01001192
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001193 if (component->probe) {
1194 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001195 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001196 dev_err(component->dev,
1197 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001198 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001199 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001200
1201 WARN(dapm->idle_bias_off &&
1202 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001203 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001204 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205 }
1206
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001207 if (component->controls)
1208 snd_soc_add_component_controls(component, component->controls,
1209 component->num_controls);
1210 if (component->dapm_routes)
1211 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1212 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001213
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001214 component->probed = 1;
1215 list_add(&dapm->list, &card->dapm_list);
1216
1217 /* This is a HACK and will be removed soon */
1218 if (component->codec)
1219 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001220
Jarkko Nikula70d29332011-01-27 16:24:22 +02001221 return 0;
1222
1223err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001224 soc_cleanup_component_debugfs(component);
1225 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001226
1227 return ret;
1228}
1229
Mark Brown36ae1a92012-01-06 17:12:45 -08001230static void rtd_release(struct device *dev)
1231{
1232 kfree(dev);
1233}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001234
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001235static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1236 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001237{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001238 int ret = 0;
1239
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001241 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1242 if (!rtd->dev)
1243 return -ENOMEM;
1244 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001245 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001246 rtd->dev->release = rtd_release;
1247 rtd->dev->init_name = name;
1248 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001249 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001250 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1251 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1252 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1253 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001254 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001255 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001256 /* calling put_device() here to free the rtd->dev */
1257 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001258 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001259 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001260 return ret;
1261 }
1262 rtd->dev_registered = 1;
1263
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001264 if (rtd->codec) {
1265 /* add DAPM sysfs entries for this codec */
1266 ret = snd_soc_dapm_sys_add(rtd->dev);
1267 if (ret < 0)
1268 dev_err(rtd->dev,
1269 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1270 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001271
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001272 /* add codec sysfs entries */
1273 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1274 if (ret < 0)
1275 dev_err(rtd->dev,
1276 "ASoC: failed to add codec sysfs files: %d\n",
1277 ret);
1278 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279
1280 return 0;
1281}
1282
Stephen Warren62ae68f2012-06-08 12:34:23 -06001283static int soc_probe_link_components(struct snd_soc_card *card, int num,
1284 int order)
1285{
1286 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001287 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001288 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001289 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001290
1291 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001292 if (rtd->cpu_dai->codec) {
1293 component = &rtd->cpu_dai->codec->component;
1294 if (!component->probed &&
1295 component->driver->probe_order == order) {
1296 ret = soc_probe_component(card, component);
1297 if (ret < 0)
1298 return ret;
1299 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001300 }
1301
Benoit Cousson88bd8702014-07-08 23:19:34 +02001302 /* probe the CODEC-side components */
1303 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001304 component = &rtd->codec_dais[i]->codec->component;
1305 if (!component->probed &&
1306 component->driver->probe_order == order) {
1307 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001308 if (ret < 0)
1309 return ret;
1310 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001311 }
1312
1313 /* probe the platform */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001314 if (!platform->component.probed &&
1315 platform->component.driver->probe_order == order) {
1316 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) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001411 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001412 if (!try_module_get(cpu_dai->dev->driver->owner))
1413 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001414 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001415
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001416 if (cpu_dai->driver->probe) {
1417 ret = cpu_dai->driver->probe(cpu_dai);
1418 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001419 dev_err(cpu_dai->dev,
1420 "ASoC: failed to probe CPU DAI %s: %d\n",
1421 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001422 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 return ret;
1424 }
1425 }
1426 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001427 }
1428
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001429 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001430 for (i = 0; i < rtd->num_codecs; i++) {
1431 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1432 if (ret)
1433 return ret;
1434 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001435
Liam Girdwood0168bf02011-06-07 16:08:05 +01001436 /* complete DAI probe during last probe */
1437 if (order != SND_SOC_COMP_ORDER_LAST)
1438 return 0;
1439
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001440 /* do machine specific initialization */
1441 if (dai_link->init) {
1442 ret = dai_link->init(rtd);
1443 if (ret < 0) {
1444 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1445 dai_link->name, ret);
1446 return ret;
1447 }
1448 }
1449
1450 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001451 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001452 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001453
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001454#ifdef CONFIG_DEBUG_FS
1455 /* add DPCM sysfs entries */
1456 if (dai_link->dynamic) {
1457 ret = soc_dpcm_debugfs_add(rtd);
1458 if (ret < 0) {
1459 dev_err(rtd->dev,
1460 "ASoC: failed to add dpcm sysfs entries: %d\n",
1461 ret);
1462 return ret;
1463 }
1464 }
1465#endif
1466
Mark Brown36ae1a92012-01-06 17:12:45 -08001467 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001469 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1470 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471
Namarta Kohli1245b702012-08-16 17:10:41 +05301472 if (cpu_dai->driver->compress_dai) {
1473 /*create compress_device"*/
1474 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001475 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001476 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301477 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001478 return ret;
1479 }
1480 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301481
1482 if (!dai_link->params) {
1483 /* create the pcm */
1484 ret = soc_new_pcm(rtd, num);
1485 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001486 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301487 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001488 return ret;
1489 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301490 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001491 INIT_DELAYED_WORK(&rtd->delayed_work,
1492 codec2codec_close_delayed_work);
1493
Namarta Kohli1245b702012-08-16 17:10:41 +05301494 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001495 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001496 if (ret)
1497 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001498 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 }
1500
1501 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001502 for (i = 0; i < rtd->num_codecs; i++) {
1503 if (rtd->codec_dais[i]->driver->ac97_control)
1504 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1505 rtd->cpu_dai->ac97_pdata);
1506 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001507
1508 return 0;
1509}
1510
1511#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001512static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1513 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514{
1515 int ret;
1516
1517 /* Only instantiate AC97 if not already done by the adaptor
1518 * for the generic AC97 subsystem.
1519 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001520 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001521 /*
1522 * It is possible that the AC97 device is already registered to
1523 * the device subsystem. This happens when the device is created
1524 * via snd_ac97_mixer(). Currently only SoC codec that does so
1525 * is the generic AC97 glue but others migh emerge.
1526 *
1527 * In those cases we don't try to register the device again.
1528 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001529 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001530 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001531
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001532 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001533 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001534 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001535 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001536 return ret;
1537 }
1538
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001539 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001540 }
1541 return 0;
1542}
1543
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001544static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545{
1546 if (codec->ac97_registered) {
1547 soc_ac97_dev_unregister(codec);
1548 codec->ac97_registered = 0;
1549 }
1550}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001551
Benoit Cousson88bd8702014-07-08 23:19:34 +02001552static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1553{
1554 int i, ret;
1555
1556 for (i = 0; i < rtd->num_codecs; i++) {
1557 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1558
1559 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1560 if (ret) {
1561 while (--i >= 0)
1562 soc_unregister_ac97_codec(codec_dai->codec);
1563 return ret;
1564 }
1565 }
1566
1567 return 0;
1568}
1569
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001570static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1571{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001572 int i;
1573
1574 for (i = 0; i < rtd->num_codecs; i++)
1575 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001576}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577#endif
1578
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001579static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001580{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001581 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001582 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1583 const char *codecname = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001584
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001585 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1586 if (!rtd->codec) {
1587 if (aux_dev->codec_of_node)
1588 codecname = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001589
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001590 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1591 return -EPROBE_DEFER;
1592 }
1593
1594 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001595}
1596
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001597static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1598{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001599 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001600 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001601 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001602
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001603 if (rtd->codec->component.probed) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001604 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001605 return -EBUSY;
1606 }
1607
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001608 ret = soc_probe_component(card, &rtd->codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001609 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001610 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001611
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001612 /* do machine specific initialization */
1613 if (aux_dev->init) {
1614 ret = aux_dev->init(&rtd->codec->dapm);
1615 if (ret < 0) {
1616 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1617 aux_dev->name, ret);
1618 return ret;
1619 }
1620 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001621
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001622 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001623}
1624
1625static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1626{
1627 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1628 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001629
1630 /* unregister the rtd device */
1631 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001632 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001633 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001634 rtd->dev_registered = 0;
1635 }
1636
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001637 if (codec && codec->component.probed)
1638 soc_remove_component(&codec->component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001639}
1640
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001641static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001642{
1643 int ret;
1644
1645 if (codec->cache_init)
1646 return 0;
1647
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001648 ret = snd_soc_cache_init(codec);
1649 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001650 dev_err(codec->dev,
1651 "ASoC: Failed to set cache compression type: %d\n",
1652 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001653 return ret;
1654 }
1655 codec->cache_init = 1;
1656 return 0;
1657}
1658
Mark Brownb19e6e72012-03-14 21:18:39 +00001659static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001661 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001662 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001663 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001664
Liam Girdwood01b9d992012-03-07 10:38:25 +00001665 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666
Mark Brownb19e6e72012-03-14 21:18:39 +00001667 /* bind DAIs */
1668 for (i = 0; i < card->num_links; i++) {
1669 ret = soc_bind_dai_link(card, i);
1670 if (ret != 0)
1671 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001672 }
1673
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001674 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001675 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001676 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001677 if (ret != 0)
1678 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001679 }
1680
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001681 /* initialize the register cache for each available codec */
1682 list_for_each_entry(codec, &codec_list, list) {
1683 if (codec->cache_init)
1684 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001685 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001686 if (ret < 0)
1687 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001688 }
1689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001690 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001691 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001692 card->owner, 0, &card->snd_card);
1693 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001694 dev_err(card->dev,
1695 "ASoC: can't create sound card for card %s: %d\n",
1696 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001697 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001699
Mark Browne37a4972011-03-02 18:21:57 +00001700 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1701 card->dapm.dev = card->dev;
1702 card->dapm.card = card;
1703 list_add(&card->dapm.list, &card->dapm_list);
1704
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001705#ifdef CONFIG_DEBUG_FS
1706 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1707#endif
1708
Mark Brown88ee1c62011-02-02 10:43:26 +00001709#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001710 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001711 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001712#endif
Andy Green6ed25972008-06-13 16:24:05 +01001713
Mark Brown9a841eb2011-04-12 17:51:37 -07001714 if (card->dapm_widgets)
1715 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1716 card->num_dapm_widgets);
1717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718 /* initialise the sound card only once */
1719 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001720 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721 if (ret < 0)
1722 goto card_probe_error;
1723 }
1724
Stephen Warren62ae68f2012-06-08 12:34:23 -06001725 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001726 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1727 order++) {
1728 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001729 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001730 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001731 dev_err(card->dev,
1732 "ASoC: failed to instantiate card %d\n",
1733 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001734 goto probe_dai_err;
1735 }
1736 }
1737 }
1738
1739 /* probe all DAI links on this card */
1740 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1741 order++) {
1742 for (i = 0; i < card->num_links; i++) {
1743 ret = soc_probe_link_dais(card, i, order);
1744 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001745 dev_err(card->dev,
1746 "ASoC: failed to instantiate card %d\n",
1747 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001748 goto probe_dai_err;
1749 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001750 }
1751 }
1752
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001753 for (i = 0; i < card->num_aux_devs; i++) {
1754 ret = soc_probe_aux_dev(card, i);
1755 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001756 dev_err(card->dev,
1757 "ASoC: failed to add auxiliary devices %d\n",
1758 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001759 goto probe_aux_dev_err;
1760 }
1761 }
1762
Mark Brown888df392012-02-16 19:37:51 -08001763 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001764 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001765
Mark Brownb7af1da2011-04-07 19:18:44 +09001766 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001767 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001768
Mark Brownb8ad29d2011-03-02 18:35:51 +00001769 if (card->dapm_routes)
1770 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1771 card->num_dapm_routes);
1772
Mark Brown75d9ac42011-09-27 16:41:01 +01001773 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001774 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001775 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001776 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001777
Mark Brownf04209a2012-04-09 16:29:21 +01001778 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001779 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1780 int j;
1781
1782 for (j = 0; j < rtd->num_codecs; j++) {
1783 struct snd_soc_dai *codec_dai = codec_dais[j];
1784
1785 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1786 if (ret != 0 && ret != -ENOTSUPP)
1787 dev_warn(codec_dai->dev,
1788 "ASoC: Failed to set DAI format: %d\n",
1789 ret);
1790 }
Mark Brownf04209a2012-04-09 16:29:21 +01001791 }
1792
1793 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001794 if (dai_fmt &&
1795 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001796 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1797 dai_fmt);
1798 if (ret != 0 && ret != -ENOTSUPP)
1799 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001800 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001801 ret);
1802 } else if (dai_fmt) {
1803 /* Flip the polarity for the "CPU" end */
1804 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1805 switch (dai_link->dai_fmt &
1806 SND_SOC_DAIFMT_MASTER_MASK) {
1807 case SND_SOC_DAIFMT_CBM_CFM:
1808 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1809 break;
1810 case SND_SOC_DAIFMT_CBM_CFS:
1811 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1812 break;
1813 case SND_SOC_DAIFMT_CBS_CFM:
1814 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1815 break;
1816 case SND_SOC_DAIFMT_CBS_CFS:
1817 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1818 break;
1819 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001820
1821 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001822 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001823 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001824 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001825 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001826 ret);
1827 }
1828 }
1829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001832 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1833 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001834 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1835 "%s", card->driver_name ? card->driver_name : card->name);
1836 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1837 switch (card->snd_card->driver[i]) {
1838 case '_':
1839 case '-':
1840 case '\0':
1841 break;
1842 default:
1843 if (!isalnum(card->snd_card->driver[i]))
1844 card->snd_card->driver[i] = '_';
1845 break;
1846 }
1847 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001848
Mark Brown28e9ad92011-03-02 18:36:34 +00001849 if (card->late_probe) {
1850 ret = card->late_probe(card);
1851 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001852 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001853 card->name, ret);
1854 goto probe_aux_dev_err;
1855 }
1856 }
1857
Stephen Warren16332812011-11-23 12:42:04 -07001858 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001859 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001860
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001861 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001863 ret = snd_card_register(card->snd_card);
1864 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001865 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1866 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001867 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001868 }
1869
1870#ifdef CONFIG_SND_SOC_AC97_BUS
1871 /* register any AC97 codecs */
1872 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001873 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1874 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001875 dev_err(card->dev,
1876 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001877 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001878 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001879 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001880 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001881 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001882#endif
1883
Mark Brown435c5e22008-12-04 15:32:53 +00001884 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001885 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001886 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001887
1888 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001890probe_aux_dev_err:
1891 for (i = 0; i < card->num_aux_devs; i++)
1892 soc_remove_aux_dev(card, i);
1893
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001894probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001895 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001897card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001898 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001899 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900
1901 snd_card_free(card->snd_card);
1902
Mark Brownb19e6e72012-03-14 21:18:39 +00001903base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001904 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905
Mark Brownb19e6e72012-03-14 21:18:39 +00001906 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001907}
1908
1909/* probes a new socdev */
1910static int soc_probe(struct platform_device *pdev)
1911{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001912 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001913
Vinod Koul70a7ca32011-01-14 19:22:48 +05301914 /*
1915 * no card, so machine driver should be registering card
1916 * we should not be here in that case so ret error
1917 */
1918 if (!card)
1919 return -EINVAL;
1920
Mark Brownfe4085e2012-03-02 13:07:41 +00001921 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001922 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001923 card->name);
1924
Mark Brown435c5e22008-12-04 15:32:53 +00001925 /* Bodge while we unpick instantiation */
1926 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001927
Mark Brown28d528c2012-08-09 18:45:23 +01001928 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929}
1930
Vinod Koulb0e26482011-01-13 22:48:02 +05301931static int soc_cleanup_card_resources(struct snd_soc_card *card)
1932{
Vinod Koulb0e26482011-01-13 22:48:02 +05301933 int i;
1934
1935 /* make sure any delayed work runs */
1936 for (i = 0; i < card->num_rtd; i++) {
1937 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001938 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301939 }
1940
1941 /* remove auxiliary devices */
1942 for (i = 0; i < card->num_aux_devs; i++)
1943 soc_remove_aux_dev(card, i);
1944
1945 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001946 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301947
1948 soc_cleanup_card_debugfs(card);
1949
1950 /* remove the card */
1951 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001952 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301953
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001954 snd_soc_dapm_free(&card->dapm);
1955
Vinod Koulb0e26482011-01-13 22:48:02 +05301956 snd_card_free(card->snd_card);
1957 return 0;
1958
1959}
1960
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961/* removes a socdev */
1962static int soc_remove(struct platform_device *pdev)
1963{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001964 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965
Mark Brownc5af3a22008-11-28 13:29:45 +00001966 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001967 return 0;
1968}
1969
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001970int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001971{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001972 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973 int i;
Mark Brown51737472009-06-22 13:16:51 +01001974
1975 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001976 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001977
1978 /* Flush out pmdown_time work - we actually do want to run it
1979 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001980 for (i = 0; i < card->num_rtd; i++) {
1981 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001982 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983 }
Mark Brown51737472009-06-22 13:16:51 +01001984
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001986
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001987 /* deactivate pins to sleep state */
1988 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001989 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1990 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1991 int j;
1992
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001993 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001994 for (j = 0; j < rtd->num_codecs; j++) {
1995 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1996 pinctrl_pm_select_sleep_state(codec_dai->dev);
1997 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001998 }
1999
Mark Brown416356f2009-06-30 19:05:15 +01002000 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002001}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002002EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002003
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002004const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302005 .suspend = snd_soc_suspend,
2006 .resume = snd_soc_resume,
2007 .freeze = snd_soc_suspend,
2008 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002009 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302010 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002011};
Stephen Warrendeb26072011-04-05 19:35:30 -06002012EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002013
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014/* ASoC platform driver */
2015static struct platform_driver soc_driver = {
2016 .driver = {
2017 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002018 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002019 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002020 },
2021 .probe = soc_probe,
2022 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002023};
2024
Mark Brown096e49d2009-07-05 15:12:22 +01002025/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 * snd_soc_new_ac97_codec - initailise AC97 device
2027 * @codec: audio codec
2028 * @ops: AC97 bus operations
2029 * @num: AC97 codec number
2030 *
2031 * Initialises AC97 codec resources for use by ad-hoc devices only.
2032 */
2033int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2034 struct snd_ac97_bus_ops *ops, int num)
2035{
2036 mutex_lock(&codec->mutex);
2037
2038 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2039 if (codec->ac97 == NULL) {
2040 mutex_unlock(&codec->mutex);
2041 return -ENOMEM;
2042 }
2043
2044 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2045 if (codec->ac97->bus == NULL) {
2046 kfree(codec->ac97);
2047 codec->ac97 = NULL;
2048 mutex_unlock(&codec->mutex);
2049 return -ENOMEM;
2050 }
2051
2052 codec->ac97->bus->ops = ops;
2053 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002054
2055 /*
2056 * Mark the AC97 device to be created by us. This way we ensure that the
2057 * device will be registered with the device subsystem later on.
2058 */
2059 codec->ac97_created = 1;
2060
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 mutex_unlock(&codec->mutex);
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2065
Markus Pargmann741a5092013-08-19 17:05:55 +02002066static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2067
2068static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2069{
2070 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2071
2072 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2073
2074 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2075
2076 udelay(10);
2077
2078 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2079
2080 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2081 msleep(2);
2082}
2083
2084static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2085{
2086 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2087
2088 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2089
2090 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2091 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2092 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2093
2094 udelay(10);
2095
2096 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2097
2098 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2099 msleep(2);
2100}
2101
2102static int snd_soc_ac97_parse_pinctl(struct device *dev,
2103 struct snd_ac97_reset_cfg *cfg)
2104{
2105 struct pinctrl *p;
2106 struct pinctrl_state *state;
2107 int gpio;
2108 int ret;
2109
2110 p = devm_pinctrl_get(dev);
2111 if (IS_ERR(p)) {
2112 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002113 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002114 }
2115 cfg->pctl = p;
2116
2117 state = pinctrl_lookup_state(p, "ac97-reset");
2118 if (IS_ERR(state)) {
2119 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002120 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002121 }
2122 cfg->pstate_reset = state;
2123
2124 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2125 if (IS_ERR(state)) {
2126 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002127 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002128 }
2129 cfg->pstate_warm_reset = state;
2130
2131 state = pinctrl_lookup_state(p, "ac97-running");
2132 if (IS_ERR(state)) {
2133 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002134 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002135 }
2136 cfg->pstate_run = state;
2137
2138 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2139 if (gpio < 0) {
2140 dev_err(dev, "Can't find ac97-sync gpio\n");
2141 return gpio;
2142 }
2143 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2144 if (ret) {
2145 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2146 return ret;
2147 }
2148 cfg->gpio_sync = gpio;
2149
2150 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2151 if (gpio < 0) {
2152 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2153 return gpio;
2154 }
2155 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2156 if (ret) {
2157 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2158 return ret;
2159 }
2160 cfg->gpio_sdata = gpio;
2161
2162 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2163 if (gpio < 0) {
2164 dev_err(dev, "Can't find ac97-reset gpio\n");
2165 return gpio;
2166 }
2167 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2168 if (ret) {
2169 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2170 return ret;
2171 }
2172 cfg->gpio_reset = gpio;
2173
2174 return 0;
2175}
2176
Mark Brownb047e1c2013-06-26 12:45:59 +01002177struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002178EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002179
2180int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2181{
2182 if (ops == soc_ac97_ops)
2183 return 0;
2184
2185 if (soc_ac97_ops && ops)
2186 return -EBUSY;
2187
2188 soc_ac97_ops = ops;
2189
2190 return 0;
2191}
2192EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2193
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002195 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2196 *
2197 * This function sets the reset and warm_reset properties of ops and parses
2198 * the device node of pdev to get pinctrl states and gpio numbers to use.
2199 */
2200int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2201 struct platform_device *pdev)
2202{
2203 struct device *dev = &pdev->dev;
2204 struct snd_ac97_reset_cfg cfg;
2205 int ret;
2206
2207 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2208 if (ret)
2209 return ret;
2210
2211 ret = snd_soc_set_ac97_ops(ops);
2212 if (ret)
2213 return ret;
2214
2215 ops->warm_reset = snd_soc_ac97_warm_reset;
2216 ops->reset = snd_soc_ac97_reset;
2217
2218 snd_ac97_rst_cfg = cfg;
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2222
2223/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002224 * snd_soc_free_ac97_codec - free AC97 codec device
2225 * @codec: audio codec
2226 *
2227 * Frees AC97 codec device resources.
2228 */
2229void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2230{
2231 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002232#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002233 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002234#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 kfree(codec->ac97->bus);
2236 kfree(codec->ac97);
2237 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002238 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239 mutex_unlock(&codec->mutex);
2240}
2241EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2242
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002243/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244 * snd_soc_cnew - create new control
2245 * @_template: control template
2246 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002247 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002248 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249 *
2250 * Create a new mixer control from a template control.
2251 *
2252 * Returns 0 for success, else error.
2253 */
2254struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002255 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002256 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257{
2258 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002259 struct snd_kcontrol *kcontrol;
2260 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261
2262 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263 template.index = 0;
2264
Mark Brownefb7ac32011-03-08 17:23:24 +00002265 if (!long_name)
2266 long_name = template.name;
2267
2268 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002269 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002270 if (!name)
2271 return NULL;
2272
Mark Brownefb7ac32011-03-08 17:23:24 +00002273 template.name = name;
2274 } else {
2275 template.name = long_name;
2276 }
2277
2278 kcontrol = snd_ctl_new1(&template, data);
2279
2280 kfree(name);
2281
2282 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002283}
2284EXPORT_SYMBOL_GPL(snd_soc_cnew);
2285
Liam Girdwood022658b2012-02-03 17:43:09 +00002286static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2287 const struct snd_kcontrol_new *controls, int num_controls,
2288 const char *prefix, void *data)
2289{
2290 int err, i;
2291
2292 for (i = 0; i < num_controls; i++) {
2293 const struct snd_kcontrol_new *control = &controls[i];
2294 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2295 control->name, prefix));
2296 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002297 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2298 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002299 return err;
2300 }
2301 }
2302
2303 return 0;
2304}
2305
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002306struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2307 const char *name)
2308{
2309 struct snd_card *card = soc_card->snd_card;
2310 struct snd_kcontrol *kctl;
2311
2312 if (unlikely(!name))
2313 return NULL;
2314
2315 list_for_each_entry(kctl, &card->controls, list)
2316 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2317 return kctl;
2318 return NULL;
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2321
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002323 * snd_soc_add_component_controls - Add an array of controls to a component.
2324 *
2325 * @component: Component to add controls to
2326 * @controls: Array of controls to add
2327 * @num_controls: Number of elements in the array
2328 *
2329 * Return: 0 for success, else error.
2330 */
2331int snd_soc_add_component_controls(struct snd_soc_component *component,
2332 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2333{
2334 struct snd_card *card = component->card->snd_card;
2335
2336 return snd_soc_add_controls(card, component->dev, controls,
2337 num_controls, component->name_prefix, component);
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2340
2341/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002342 * snd_soc_add_codec_controls - add an array of controls to a codec.
2343 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002344 * duplicating this code.
2345 *
2346 * @codec: codec to add controls to
2347 * @controls: array of controls to add
2348 * @num_controls: number of elements in the array
2349 *
2350 * Return 0 for success, else error.
2351 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002352int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002353 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002354{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002355 return snd_soc_add_component_controls(&codec->component, controls,
2356 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002357}
Liam Girdwood022658b2012-02-03 17:43:09 +00002358EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002359
2360/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002361 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002362 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002363 *
2364 * @platform: platform to add controls to
2365 * @controls: array of controls to add
2366 * @num_controls: number of elements in the array
2367 *
2368 * Return 0 for success, else error.
2369 */
2370int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002371 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002372{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002373 return snd_soc_add_component_controls(&platform->component, controls,
2374 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002375}
2376EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2377
2378/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002379 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2380 * Convenience function to add a list of controls.
2381 *
2382 * @soc_card: SoC card to add controls to
2383 * @controls: array of controls to add
2384 * @num_controls: number of elements in the array
2385 *
2386 * Return 0 for success, else error.
2387 */
2388int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2389 const struct snd_kcontrol_new *controls, int num_controls)
2390{
2391 struct snd_card *card = soc_card->snd_card;
2392
2393 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2394 NULL, soc_card);
2395}
2396EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2397
2398/**
2399 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2400 * Convienience function to add a list of controls.
2401 *
2402 * @dai: DAI to add controls to
2403 * @controls: array of controls to add
2404 * @num_controls: number of elements in the array
2405 *
2406 * Return 0 for success, else error.
2407 */
2408int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2409 const struct snd_kcontrol_new *controls, int num_controls)
2410{
2411 struct snd_card *card = dai->card->snd_card;
2412
2413 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2414 NULL, dai);
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2417
2418/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419 * snd_soc_info_enum_double - enumerated double mixer info callback
2420 * @kcontrol: mixer control
2421 * @uinfo: control element information
2422 *
2423 * Callback to provide information about a double enumerated
2424 * mixer control.
2425 *
2426 * Returns 0 for success.
2427 */
2428int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_info *uinfo)
2430{
2431 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2432
2433 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2434 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002435 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002437 if (uinfo->value.enumerated.item >= e->items)
2438 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002439 strlcpy(uinfo->value.enumerated.name,
2440 e->texts[uinfo->value.enumerated.item],
2441 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442 return 0;
2443}
2444EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2445
2446/**
2447 * snd_soc_get_enum_double - enumerated double mixer get callback
2448 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002449 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450 *
2451 * Callback to get the value of a double enumerated mixer.
2452 *
2453 * Returns 0 for success.
2454 */
2455int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002458 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002460 unsigned int val, item;
2461 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002462 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002464 ret = snd_soc_component_read(component, e->reg, &reg_val);
2465 if (ret)
2466 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002467 val = (reg_val >> e->shift_l) & e->mask;
2468 item = snd_soc_enum_val_to_item(e, val);
2469 ucontrol->value.enumerated.item[0] = item;
2470 if (e->shift_l != e->shift_r) {
2471 val = (reg_val >> e->shift_l) & e->mask;
2472 item = snd_soc_enum_val_to_item(e, val);
2473 ucontrol->value.enumerated.item[1] = item;
2474 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475
2476 return 0;
2477}
2478EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2479
2480/**
2481 * snd_soc_put_enum_double - enumerated double mixer put callback
2482 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002483 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002484 *
2485 * Callback to set the value of a double enumerated mixer.
2486 *
2487 * Returns 0 for success.
2488 */
2489int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2490 struct snd_ctl_elem_value *ucontrol)
2491{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002492 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002494 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002495 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002496 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002498 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002499 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002500 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002501 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002503 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002505 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002506 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507 }
2508
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002509 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510}
2511EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2512
2513/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002514 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002515 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002516 * @reg: Register to read
2517 * @mask: Mask to use after shifting the register value
2518 * @shift: Right shift of register value
2519 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002520 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002521 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002522 * This functions reads a codec register. The register value is shifted right
2523 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2524 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002525 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002526 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002527 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002528static int snd_soc_read_signed(struct snd_soc_component *component,
2529 unsigned int reg, unsigned int mask, unsigned int shift,
2530 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002531{
Markus Pargmannf227b882014-01-16 16:02:10 +01002532 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002533 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002534
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002535 ret = snd_soc_component_read(component, reg, &val);
2536 if (ret < 0)
2537 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002538
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002539 val = (val >> shift) & mask;
2540
2541 if (!sign_bit) {
2542 *signed_val = val;
2543 return 0;
2544 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002545
2546 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002547 if (!(val & BIT(sign_bit))) {
2548 *signed_val = val;
2549 return 0;
2550 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002551
2552 ret = val;
2553
2554 /*
2555 * The register most probably does not contain a full-sized int.
2556 * Instead we have an arbitrary number of bits in a signed
2557 * representation which has to be translated into a full-sized int.
2558 * This is done by filling up all bits above the sign-bit.
2559 */
2560 ret |= ~((int)(BIT(sign_bit) - 1));
2561
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002562 *signed_val = ret;
2563
2564 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002565}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002566
2567/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002568 * snd_soc_info_volsw - single mixer info callback
2569 * @kcontrol: mixer control
2570 * @uinfo: control element information
2571 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002572 * Callback to provide information about a single mixer control, or a double
2573 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002574 *
2575 * Returns 0 for success.
2576 */
2577int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2578 struct snd_ctl_elem_info *uinfo)
2579{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002580 struct soc_mixer_control *mc =
2581 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002582 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002583
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002584 if (!mc->platform_max)
2585 mc->platform_max = mc->max;
2586 platform_max = mc->platform_max;
2587
2588 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002589 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2590 else
2591 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2592
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002593 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002594 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002595 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002596 return 0;
2597}
2598EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2599
2600/**
2601 * snd_soc_get_volsw - single mixer get callback
2602 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002603 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002605 * Callback to get the value of a single mixer control, or a double mixer
2606 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002607 *
2608 * Returns 0 for success.
2609 */
2610int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2612{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002613 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002614 struct soc_mixer_control *mc =
2615 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002616 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002617 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002618 unsigned int shift = mc->shift;
2619 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002620 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002621 int min = mc->min;
2622 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002623 unsigned int mask = (1 << fls(max)) - 1;
2624 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002625 int val;
2626 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627
Markus Pargmannf227b882014-01-16 16:02:10 +01002628 if (sign_bit)
2629 mask = BIT(sign_bit + 1) - 1;
2630
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002631 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2632 if (ret)
2633 return ret;
2634
2635 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002636 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002638 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002639
2640 if (snd_soc_volsw_is_stereo(mc)) {
2641 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002642 ret = snd_soc_read_signed(component, reg, mask, rshift,
2643 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002644 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002645 ret = snd_soc_read_signed(component, reg2, mask, shift,
2646 sign_bit, &val);
2647 if (ret)
2648 return ret;
2649
2650 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002651 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002653 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654 }
2655
2656 return 0;
2657}
2658EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2659
2660/**
2661 * snd_soc_put_volsw - single mixer put callback
2662 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002663 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002664 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002665 * Callback to set the value of a single mixer control, or a double mixer
2666 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002667 *
2668 * Returns 0 for success.
2669 */
2670int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2672{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002673 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002674 struct soc_mixer_control *mc =
2675 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002676 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002677 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002678 unsigned int shift = mc->shift;
2679 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002680 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002681 int min = mc->min;
2682 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002683 unsigned int mask = (1 << fls(max)) - 1;
2684 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002685 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002686 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002687 unsigned int val2 = 0;
2688 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002689
Markus Pargmannf227b882014-01-16 16:02:10 +01002690 if (sign_bit)
2691 mask = BIT(sign_bit + 1) - 1;
2692
2693 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002694 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002695 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 val_mask = mask << shift;
2697 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002698 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002699 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002700 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002701 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002702 if (reg == reg2) {
2703 val_mask |= mask << rshift;
2704 val |= val2 << rshift;
2705 } else {
2706 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002707 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002709 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002710 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002711 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002712 return err;
2713
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002715 err = snd_soc_component_update_bits(component, reg2, val_mask,
2716 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002717
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718 return err;
2719}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002720EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721
Mark Browne13ac2e2008-05-28 17:58:05 +01002722/**
Brian Austin1d99f242012-03-30 10:43:55 -05002723 * snd_soc_get_volsw_sx - single mixer get callback
2724 * @kcontrol: mixer control
2725 * @ucontrol: control element information
2726 *
2727 * Callback to get the value of a single mixer control, or a double mixer
2728 * control that spans 2 registers.
2729 *
2730 * Returns 0 for success.
2731 */
2732int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2734{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002735 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002736 struct soc_mixer_control *mc =
2737 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002738 unsigned int reg = mc->reg;
2739 unsigned int reg2 = mc->rreg;
2740 unsigned int shift = mc->shift;
2741 unsigned int rshift = mc->rshift;
2742 int max = mc->max;
2743 int min = mc->min;
2744 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002745 unsigned int val;
2746 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002747
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002748 ret = snd_soc_component_read(component, reg, &val);
2749 if (ret < 0)
2750 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002751
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002752 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2753
2754 if (snd_soc_volsw_is_stereo(mc)) {
2755 ret = snd_soc_component_read(component, reg2, &val);
2756 if (ret < 0)
2757 return ret;
2758
2759 val = ((val >> rshift) - min) & mask;
2760 ucontrol->value.integer.value[1] = val;
2761 }
Brian Austin1d99f242012-03-30 10:43:55 -05002762
2763 return 0;
2764}
2765EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2766
2767/**
2768 * snd_soc_put_volsw_sx - double mixer set callback
2769 * @kcontrol: mixer control
2770 * @uinfo: control element information
2771 *
2772 * Callback to set the value of a double mixer control that spans 2 registers.
2773 *
2774 * Returns 0 for success.
2775 */
2776int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
2778{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002779 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002780 struct soc_mixer_control *mc =
2781 (struct soc_mixer_control *)kcontrol->private_value;
2782
2783 unsigned int reg = mc->reg;
2784 unsigned int reg2 = mc->rreg;
2785 unsigned int shift = mc->shift;
2786 unsigned int rshift = mc->rshift;
2787 int max = mc->max;
2788 int min = mc->min;
2789 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002790 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002791 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002792
2793 val_mask = mask << shift;
2794 val = (ucontrol->value.integer.value[0] + min) & mask;
2795 val = val << shift;
2796
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002797 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302798 if (err < 0)
2799 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002800
2801 if (snd_soc_volsw_is_stereo(mc)) {
2802 val_mask = mask << rshift;
2803 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2804 val2 = val2 << rshift;
2805
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002806 err = snd_soc_component_update_bits(component, reg2, val_mask,
2807 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002808 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002809 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002810}
2811EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2812
2813/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002814 * snd_soc_info_volsw_s8 - signed mixer info callback
2815 * @kcontrol: mixer control
2816 * @uinfo: control element information
2817 *
2818 * Callback to provide information about a signed mixer control.
2819 *
2820 * Returns 0 for success.
2821 */
2822int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2823 struct snd_ctl_elem_info *uinfo)
2824{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002825 struct soc_mixer_control *mc =
2826 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002827 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002828 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002829
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002830 if (!mc->platform_max)
2831 mc->platform_max = mc->max;
2832 platform_max = mc->platform_max;
2833
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2835 uinfo->count = 2;
2836 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002837 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002838 return 0;
2839}
2840EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2841
2842/**
2843 * snd_soc_get_volsw_s8 - signed mixer get callback
2844 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002845 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002846 *
2847 * Callback to get the value of a signed mixer control.
2848 *
2849 * Returns 0 for success.
2850 */
2851int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
2853{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002854 struct soc_mixer_control *mc =
2855 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002857 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002858 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002859 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002860 int ret;
2861
2862 ret = snd_soc_component_read(component, reg, &val);
2863 if (ret)
2864 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002865
2866 ucontrol->value.integer.value[0] =
2867 ((signed char)(val & 0xff))-min;
2868 ucontrol->value.integer.value[1] =
2869 ((signed char)((val >> 8) & 0xff))-min;
2870 return 0;
2871}
2872EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2873
2874/**
2875 * snd_soc_put_volsw_sgn - signed mixer put callback
2876 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002877 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002878 *
2879 * Callback to set the value of a signed mixer control.
2880 *
2881 * Returns 0 for success.
2882 */
2883int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2884 struct snd_ctl_elem_value *ucontrol)
2885{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002886 struct soc_mixer_control *mc =
2887 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002888 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002889 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002890 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002891 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002892
2893 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2894 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2895
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002896 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002897}
2898EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2899
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002900/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002901 * snd_soc_info_volsw_range - single mixer info callback with range.
2902 * @kcontrol: mixer control
2903 * @uinfo: control element information
2904 *
2905 * Callback to provide information, within a range, about a single
2906 * mixer control.
2907 *
2908 * returns 0 for success.
2909 */
2910int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_info *uinfo)
2912{
2913 struct soc_mixer_control *mc =
2914 (struct soc_mixer_control *)kcontrol->private_value;
2915 int platform_max;
2916 int min = mc->min;
2917
2918 if (!mc->platform_max)
2919 mc->platform_max = mc->max;
2920 platform_max = mc->platform_max;
2921
2922 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002923 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002924 uinfo->value.integer.min = 0;
2925 uinfo->value.integer.max = platform_max - min;
2926
2927 return 0;
2928}
2929EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2930
2931/**
2932 * snd_soc_put_volsw_range - single mixer put value callback with range.
2933 * @kcontrol: mixer control
2934 * @ucontrol: control element information
2935 *
2936 * Callback to set the value, within a range, for a single mixer control.
2937 *
2938 * Returns 0 for success.
2939 */
2940int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2941 struct snd_ctl_elem_value *ucontrol)
2942{
2943 struct soc_mixer_control *mc =
2944 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002945 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002946 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002947 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002948 unsigned int shift = mc->shift;
2949 int min = mc->min;
2950 int max = mc->max;
2951 unsigned int mask = (1 << fls(max)) - 1;
2952 unsigned int invert = mc->invert;
2953 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002954 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002955
2956 val = ((ucontrol->value.integer.value[0] + min) & mask);
2957 if (invert)
2958 val = max - val;
2959 val_mask = mask << shift;
2960 val = val << shift;
2961
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002962 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002963 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002964 return ret;
2965
2966 if (snd_soc_volsw_is_stereo(mc)) {
2967 val = ((ucontrol->value.integer.value[1] + min) & mask);
2968 if (invert)
2969 val = max - val;
2970 val_mask = mask << shift;
2971 val = val << shift;
2972
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002973 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2974 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 }
2976
2977 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002978}
2979EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2980
2981/**
2982 * snd_soc_get_volsw_range - single mixer get callback with range
2983 * @kcontrol: mixer control
2984 * @ucontrol: control element information
2985 *
2986 * Callback to get the value, within a range, of a single mixer control.
2987 *
2988 * Returns 0 for success.
2989 */
2990int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2992{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002993 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994 struct soc_mixer_control *mc =
2995 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002996 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002997 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002998 unsigned int shift = mc->shift;
2999 int min = mc->min;
3000 int max = mc->max;
3001 unsigned int mask = (1 << fls(max)) - 1;
3002 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003003 unsigned int val;
3004 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003005
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003006 ret = snd_soc_component_read(component, reg, &val);
3007 if (ret)
3008 return ret;
3009
3010 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003011 if (invert)
3012 ucontrol->value.integer.value[0] =
3013 max - ucontrol->value.integer.value[0];
3014 ucontrol->value.integer.value[0] =
3015 ucontrol->value.integer.value[0] - min;
3016
Mark Brown9bde4f02012-12-19 16:05:00 +00003017 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003018 ret = snd_soc_component_read(component, rreg, &val);
3019 if (ret)
3020 return ret;
3021
3022 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003023 if (invert)
3024 ucontrol->value.integer.value[1] =
3025 max - ucontrol->value.integer.value[1];
3026 ucontrol->value.integer.value[1] =
3027 ucontrol->value.integer.value[1] - min;
3028 }
3029
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3033
3034/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003035 * snd_soc_limit_volume - Set new limit to an existing volume control.
3036 *
3037 * @codec: where to look for the control
3038 * @name: Name of the control
3039 * @max: new maximum limit
3040 *
3041 * Return 0 for success, else error.
3042 */
3043int snd_soc_limit_volume(struct snd_soc_codec *codec,
3044 const char *name, int max)
3045{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003046 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003047 struct snd_kcontrol *kctl;
3048 struct soc_mixer_control *mc;
3049 int found = 0;
3050 int ret = -EINVAL;
3051
3052 /* Sanity check for name and max */
3053 if (unlikely(!name || max <= 0))
3054 return -EINVAL;
3055
3056 list_for_each_entry(kctl, &card->controls, list) {
3057 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3058 found = 1;
3059 break;
3060 }
3061 }
3062 if (found) {
3063 mc = (struct soc_mixer_control *)kctl->private_value;
3064 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003065 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003066 ret = 0;
3067 }
3068 }
3069 return ret;
3070}
3071EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3072
Mark Brown71d08512011-10-10 18:31:26 +01003073int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3074 struct snd_ctl_elem_info *uinfo)
3075{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003076 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003077 struct soc_bytes *params = (void *)kcontrol->private_value;
3078
3079 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003080 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003081
3082 return 0;
3083}
3084EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3085
3086int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3087 struct snd_ctl_elem_value *ucontrol)
3088{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003089 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003090 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003091 int ret;
3092
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003093 if (component->regmap)
3094 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003095 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003096 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003097 else
3098 ret = -EINVAL;
3099
Mark Brownf831b052012-02-17 16:20:33 -08003100 /* Hide any masked bytes to ensure consistent data reporting */
3101 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003102 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003103 case 1:
3104 ucontrol->value.bytes.data[0] &= ~params->mask;
3105 break;
3106 case 2:
3107 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003108 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003109 break;
3110 case 4:
3111 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003112 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003113 break;
3114 default:
3115 return -EINVAL;
3116 }
3117 }
3118
Mark Brown71d08512011-10-10 18:31:26 +01003119 return ret;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3122
3123int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3124 struct snd_ctl_elem_value *ucontrol)
3125{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003126 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003127 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003128 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003129 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003130 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003131
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003132 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003133 return -EINVAL;
3134
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003135 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003136
Mark Brownb5a8fe42013-01-20 21:42:22 +09003137 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3138 if (!data)
3139 return -ENOMEM;
3140
Mark Brownf831b052012-02-17 16:20:33 -08003141 /*
3142 * If we've got a mask then we need to preserve the register
3143 * bits. We shouldn't modify the incoming data so take a
3144 * copy.
3145 */
3146 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003147 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003148 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003149 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003150
3151 val &= params->mask;
3152
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003153 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003154 case 1:
3155 ((u8 *)data)[0] &= ~params->mask;
3156 ((u8 *)data)[0] |= val;
3157 break;
3158 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003159 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003160 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003161 &mask, &mask);
3162 if (ret != 0)
3163 goto out;
3164
3165 ((u16 *)data)[0] &= mask;
3166
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003167 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003168 &val, &val);
3169 if (ret != 0)
3170 goto out;
3171
3172 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003173 break;
3174 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003175 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003176 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003177 &mask, &mask);
3178 if (ret != 0)
3179 goto out;
3180
3181 ((u32 *)data)[0] &= mask;
3182
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003183 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003184 &val, &val);
3185 if (ret != 0)
3186 goto out;
3187
3188 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003189 break;
3190 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003191 ret = -EINVAL;
3192 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003193 }
3194 }
3195
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003196 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003197 data, len);
3198
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003199out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003200 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003201
3202 return ret;
3203}
3204EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3205
Vinod Kould9881202014-05-02 10:58:11 +05303206int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_info *ucontrol)
3208{
3209 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3210
3211 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3212 ucontrol->count = params->max;
3213
3214 return 0;
3215}
3216EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3217
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303218int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3219 unsigned int size, unsigned int __user *tlv)
3220{
3221 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3222 unsigned int count = size < params->max ? size : params->max;
3223 int ret = -ENXIO;
3224
3225 switch (op_flag) {
3226 case SNDRV_CTL_TLV_OP_READ:
3227 if (params->get)
3228 ret = params->get(tlv, count);
3229 break;
3230 case SNDRV_CTL_TLV_OP_WRITE:
3231 if (params->put)
3232 ret = params->put(tlv, count);
3233 break;
3234 }
3235 return ret;
3236}
3237EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3238
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003239/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003240 * snd_soc_info_xr_sx - signed multi register info callback
3241 * @kcontrol: mreg control
3242 * @uinfo: control element information
3243 *
3244 * Callback to provide information of a control that can
3245 * span multiple codec registers which together
3246 * forms a single signed value in a MSB/LSB manner.
3247 *
3248 * Returns 0 for success.
3249 */
3250int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_info *uinfo)
3252{
3253 struct soc_mreg_control *mc =
3254 (struct soc_mreg_control *)kcontrol->private_value;
3255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3256 uinfo->count = 1;
3257 uinfo->value.integer.min = mc->min;
3258 uinfo->value.integer.max = mc->max;
3259
3260 return 0;
3261}
3262EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3263
3264/**
3265 * snd_soc_get_xr_sx - signed multi register get callback
3266 * @kcontrol: mreg control
3267 * @ucontrol: control element information
3268 *
3269 * Callback to get the value of a control that can span
3270 * multiple codec registers which together forms a single
3271 * signed value in a MSB/LSB manner. The control supports
3272 * specifying total no of bits used to allow for bitfields
3273 * across the multiple codec registers.
3274 *
3275 * Returns 0 for success.
3276 */
3277int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3278 struct snd_ctl_elem_value *ucontrol)
3279{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003280 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003281 struct soc_mreg_control *mc =
3282 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003283 unsigned int regbase = mc->regbase;
3284 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003285 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003286 unsigned int regwmask = (1<<regwshift)-1;
3287 unsigned int invert = mc->invert;
3288 unsigned long mask = (1UL<<mc->nbits)-1;
3289 long min = mc->min;
3290 long max = mc->max;
3291 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003292 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003293 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003294 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003295
3296 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003297 ret = snd_soc_component_read(component, regbase+i, &regval);
3298 if (ret)
3299 return ret;
3300 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003301 }
3302 val &= mask;
3303 if (min < 0 && val > max)
3304 val |= ~mask;
3305 if (invert)
3306 val = max - val;
3307 ucontrol->value.integer.value[0] = val;
3308
3309 return 0;
3310}
3311EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3312
3313/**
3314 * snd_soc_put_xr_sx - signed multi register get callback
3315 * @kcontrol: mreg control
3316 * @ucontrol: control element information
3317 *
3318 * Callback to set the value of a control that can span
3319 * multiple codec registers which together forms a single
3320 * signed value in a MSB/LSB manner. The control supports
3321 * specifying total no of bits used to allow for bitfields
3322 * across the multiple codec registers.
3323 *
3324 * Returns 0 for success.
3325 */
3326int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_value *ucontrol)
3328{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003329 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003330 struct soc_mreg_control *mc =
3331 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003332 unsigned int regbase = mc->regbase;
3333 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003334 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003335 unsigned int regwmask = (1<<regwshift)-1;
3336 unsigned int invert = mc->invert;
3337 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003338 long max = mc->max;
3339 long val = ucontrol->value.integer.value[0];
3340 unsigned int i, regval, regmask;
3341 int err;
3342
3343 if (invert)
3344 val = max - val;
3345 val &= mask;
3346 for (i = 0; i < regcount; i++) {
3347 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3348 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003349 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003350 regmask, regval);
3351 if (err < 0)
3352 return err;
3353 }
3354
3355 return 0;
3356}
3357EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3358
3359/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003360 * snd_soc_get_strobe - strobe get callback
3361 * @kcontrol: mixer control
3362 * @ucontrol: control element information
3363 *
3364 * Callback get the value of a strobe mixer control.
3365 *
3366 * Returns 0 for success.
3367 */
3368int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3369 struct snd_ctl_elem_value *ucontrol)
3370{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003371 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003372 struct soc_mixer_control *mc =
3373 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003374 unsigned int reg = mc->reg;
3375 unsigned int shift = mc->shift;
3376 unsigned int mask = 1 << shift;
3377 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003378 unsigned int val;
3379 int ret;
3380
3381 ret = snd_soc_component_read(component, reg, &val);
3382 if (ret)
3383 return ret;
3384
3385 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003386
3387 if (shift != 0 && val != 0)
3388 val = val >> shift;
3389 ucontrol->value.enumerated.item[0] = val ^ invert;
3390
3391 return 0;
3392}
3393EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3394
3395/**
3396 * snd_soc_put_strobe - strobe put callback
3397 * @kcontrol: mixer control
3398 * @ucontrol: control element information
3399 *
3400 * Callback strobe a register bit to high then low (or the inverse)
3401 * in one pass of a single mixer enum control.
3402 *
3403 * Returns 1 for success.
3404 */
3405int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
3407{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003408 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003409 struct soc_mixer_control *mc =
3410 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003411 unsigned int reg = mc->reg;
3412 unsigned int shift = mc->shift;
3413 unsigned int mask = 1 << shift;
3414 unsigned int invert = mc->invert != 0;
3415 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3416 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3417 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3418 int err;
3419
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003420 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003421 if (err < 0)
3422 return err;
3423
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003424 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003425}
3426EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3427
3428/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003429 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3430 * @dai: DAI
3431 * @clk_id: DAI specific clock ID
3432 * @freq: new clock frequency in Hz
3433 * @dir: new clock direction - input/output.
3434 *
3435 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3436 */
3437int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3438 unsigned int freq, int dir)
3439{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003440 if (dai->driver && dai->driver->ops->set_sysclk)
3441 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003442 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003443 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003444 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003445 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003446 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003447}
3448EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3449
3450/**
Mark Brownec4ee522011-03-07 20:58:11 +00003451 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3452 * @codec: CODEC
3453 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003454 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003455 * @freq: new clock frequency in Hz
3456 * @dir: new clock direction - input/output.
3457 *
3458 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3459 */
3460int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003461 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003462{
3463 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003464 return codec->driver->set_sysclk(codec, clk_id, source,
3465 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003466 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003467 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003468}
3469EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3470
3471/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3473 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003474 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003475 * @div: new clock divisor.
3476 *
3477 * Configures the clock dividers. This is used to derive the best DAI bit and
3478 * frame clocks from the system or master clock. It's best to set the DAI bit
3479 * and frame clocks as low as possible to save system power.
3480 */
3481int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3482 int div_id, int div)
3483{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003484 if (dai->driver && dai->driver->ops->set_clkdiv)
3485 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003486 else
3487 return -EINVAL;
3488}
3489EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3490
3491/**
3492 * snd_soc_dai_set_pll - configure DAI PLL.
3493 * @dai: DAI
3494 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003495 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496 * @freq_in: PLL input clock frequency in Hz
3497 * @freq_out: requested PLL output clock frequency in Hz
3498 *
3499 * Configures and enables PLL to generate output clock based on input clock.
3500 */
Mark Brown85488032009-09-05 18:52:16 +01003501int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3502 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003503{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003504 if (dai->driver && dai->driver->ops->set_pll)
3505 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003506 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003507 else if (dai->codec && dai->codec->driver->set_pll)
3508 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3509 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003510 else
3511 return -EINVAL;
3512}
3513EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3514
Mark Brownec4ee522011-03-07 20:58:11 +00003515/*
3516 * snd_soc_codec_set_pll - configure codec PLL.
3517 * @codec: CODEC
3518 * @pll_id: DAI specific PLL ID
3519 * @source: DAI specific source for the PLL
3520 * @freq_in: PLL input clock frequency in Hz
3521 * @freq_out: requested PLL output clock frequency in Hz
3522 *
3523 * Configures and enables PLL to generate output clock based on input clock.
3524 */
3525int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3526 unsigned int freq_in, unsigned int freq_out)
3527{
3528 if (codec->driver->set_pll)
3529 return codec->driver->set_pll(codec, pll_id, source,
3530 freq_in, freq_out);
3531 else
3532 return -EINVAL;
3533}
3534EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3535
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003536/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003537 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3538 * @dai: DAI
3539 * @ratio Ratio of BCLK to Sample rate.
3540 *
3541 * Configures the DAI for a preset BCLK to sample rate ratio.
3542 */
3543int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3544{
3545 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3546 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3547 else
3548 return -EINVAL;
3549}
3550EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3551
3552/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003553 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3554 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003555 * @fmt: SND_SOC_DAIFMT_ format value.
3556 *
3557 * Configures the DAI hardware format and clocking.
3558 */
3559int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3560{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003561 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003563 if (dai->driver->ops->set_fmt == NULL)
3564 return -ENOTSUPP;
3565 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003566}
3567EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3568
3569/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003570 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003571 * @slots: Number of slots in use.
3572 * @tx_mask: bitmask representing active TX slots.
3573 * @rx_mask: bitmask representing active RX slots.
3574 *
3575 * Generates the TDM tx and rx slot default masks for DAI.
3576 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003577static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003578 unsigned int *tx_mask,
3579 unsigned int *rx_mask)
3580{
3581 if (*tx_mask || *rx_mask)
3582 return 0;
3583
3584 if (!slots)
3585 return -EINVAL;
3586
3587 *tx_mask = (1 << slots) - 1;
3588 *rx_mask = (1 << slots) - 1;
3589
3590 return 0;
3591}
3592
3593/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003594 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3595 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003596 * @tx_mask: bitmask representing active TX slots.
3597 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003598 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003599 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003600 *
3601 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3602 * specific.
3603 */
3604int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003605 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003606{
Xiubo Lie5c21512014-03-21 14:17:12 +08003607 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3608 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003609 &tx_mask, &rx_mask);
3610 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003611 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003612
Benoit Cousson88bd8702014-07-08 23:19:34 +02003613 dai->tx_mask = tx_mask;
3614 dai->rx_mask = rx_mask;
3615
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003616 if (dai->driver && dai->driver->ops->set_tdm_slot)
3617 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003618 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003619 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003620 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003621}
3622EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3623
3624/**
Barry Song472df3c2009-09-12 01:16:29 +08003625 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3626 * @dai: DAI
3627 * @tx_num: how many TX channels
3628 * @tx_slot: pointer to an array which imply the TX slot number channel
3629 * 0~num-1 uses
3630 * @rx_num: how many RX channels
3631 * @rx_slot: pointer to an array which imply the RX slot number channel
3632 * 0~num-1 uses
3633 *
3634 * configure the relationship between channel number and TDM slot number.
3635 */
3636int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3637 unsigned int tx_num, unsigned int *tx_slot,
3638 unsigned int rx_num, unsigned int *rx_slot)
3639{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003640 if (dai->driver && dai->driver->ops->set_channel_map)
3641 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003642 rx_num, rx_slot);
3643 else
3644 return -EINVAL;
3645}
3646EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3647
3648/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003649 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3650 * @dai: DAI
3651 * @tristate: tristate enable
3652 *
3653 * Tristates the DAI so that others can use it.
3654 */
3655int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3656{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003657 if (dai->driver && dai->driver->ops->set_tristate)
3658 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003659 else
3660 return -EINVAL;
3661}
3662EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3663
3664/**
3665 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3666 * @dai: DAI
3667 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003668 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003669 *
3670 * Mutes the DAI DAC.
3671 */
Mark Brownda183962013-02-06 15:44:07 +00003672int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3673 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003674{
Mark Brownda183962013-02-06 15:44:07 +00003675 if (!dai->driver)
3676 return -ENOTSUPP;
3677
3678 if (dai->driver->ops->mute_stream)
3679 return dai->driver->ops->mute_stream(dai, mute, direction);
3680 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3681 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003683 else
Mark Brown04570c62012-04-13 19:16:03 +01003684 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003685}
3686EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3687
Benoit Cousson88bd8702014-07-08 23:19:34 +02003688static int snd_soc_init_multicodec(struct snd_soc_card *card,
3689 struct snd_soc_dai_link *dai_link)
3690{
3691 /* Legacy codec/codec_dai link is a single entry in multicodec */
3692 if (dai_link->codec_name || dai_link->codec_of_node ||
3693 dai_link->codec_dai_name) {
3694 dai_link->num_codecs = 1;
3695
3696 dai_link->codecs = devm_kzalloc(card->dev,
3697 sizeof(struct snd_soc_dai_link_component),
3698 GFP_KERNEL);
3699 if (!dai_link->codecs)
3700 return -ENOMEM;
3701
3702 dai_link->codecs[0].name = dai_link->codec_name;
3703 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3704 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3705 }
3706
3707 if (!dai_link->codecs) {
3708 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3709 return -EINVAL;
3710 }
3711
3712 return 0;
3713}
3714
Mark Brownc5af3a22008-11-28 13:29:45 +00003715/**
3716 * snd_soc_register_card - Register a card with the ASoC core
3717 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003718 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003719 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003720 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303721int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003722{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003723 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003724
Mark Brownc5af3a22008-11-28 13:29:45 +00003725 if (!card->name || !card->dev)
3726 return -EINVAL;
3727
Stephen Warren5a504962011-12-21 10:40:59 -07003728 for (i = 0; i < card->num_links; i++) {
3729 struct snd_soc_dai_link *link = &card->dai_link[i];
3730
Benoit Cousson88bd8702014-07-08 23:19:34 +02003731 ret = snd_soc_init_multicodec(card, link);
3732 if (ret) {
3733 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3734 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003735 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003736
3737 for (j = 0; j < link->num_codecs; j++) {
3738 /*
3739 * Codec must be specified by 1 of name or OF node,
3740 * not both or neither.
3741 */
3742 if (!!link->codecs[j].name ==
3743 !!link->codecs[j].of_node) {
3744 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3745 link->name);
3746 return -EINVAL;
3747 }
3748 /* Codec DAI name must be specified */
3749 if (!link->codecs[j].dai_name) {
3750 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3751 link->name);
3752 return -EINVAL;
3753 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003754 }
Stephen Warren5a504962011-12-21 10:40:59 -07003755
3756 /*
3757 * Platform may be specified by either name or OF node, but
3758 * can be left unspecified, and a dummy platform will be used.
3759 */
3760 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003761 dev_err(card->dev,
3762 "ASoC: Both platform name/of_node are set for %s\n",
3763 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003764 return -EINVAL;
3765 }
3766
3767 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003768 * CPU device may be specified by either name or OF node, but
3769 * can be left unspecified, and will be matched based on DAI
3770 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003771 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003772 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003773 dev_err(card->dev,
3774 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3775 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003776 return -EINVAL;
3777 }
3778 /*
3779 * At least one of CPU DAI name or CPU device name/node must be
3780 * specified
3781 */
3782 if (!link->cpu_dai_name &&
3783 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003784 dev_err(card->dev,
3785 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3786 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003787 return -EINVAL;
3788 }
3789 }
3790
Mark Browned77cc12011-05-03 18:25:34 +01003791 dev_set_drvdata(card->dev, card);
3792
Stephen Warren111c6412011-01-28 14:26:35 -07003793 snd_soc_initialize_card_lists(card);
3794
Vinod Koul150dd2f2011-01-13 22:48:32 +05303795 soc_init_card_debugfs(card);
3796
Mark Brown181a6892012-03-14 20:18:49 +00003797 card->rtd = devm_kzalloc(card->dev,
3798 sizeof(struct snd_soc_pcm_runtime) *
3799 (card->num_links + card->num_aux_devs),
3800 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003801 if (card->rtd == NULL)
3802 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003803 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003804 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003805
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003806 for (i = 0; i < card->num_links; i++) {
3807 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003808 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003809 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3810 sizeof(struct snd_soc_dai *) *
3811 (card->rtd[i].dai_link->num_codecs),
3812 GFP_KERNEL);
3813 if (card->rtd[i].codec_dais == NULL)
3814 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003815 }
3816
3817 for (i = 0; i < card->num_aux_devs; i++)
3818 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003819
Mark Browndb432b42011-10-03 21:06:40 +01003820 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003821 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003823 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003824
Mark Brownb19e6e72012-03-14 21:18:39 +00003825 ret = snd_soc_instantiate_card(card);
3826 if (ret != 0)
3827 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003828
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003829 /* deactivate pins to sleep state */
3830 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003831 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3832 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3833 int j;
3834
3835 for (j = 0; j < rtd->num_codecs; j++) {
3836 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3837 if (!codec_dai->active)
3838 pinctrl_pm_select_sleep_state(codec_dai->dev);
3839 }
3840
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003841 if (!cpu_dai->active)
3842 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3843 }
3844
Mark Brownb19e6e72012-03-14 21:18:39 +00003845 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003846}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303847EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003848
3849/**
3850 * snd_soc_unregister_card - Unregister a card with the ASoC core
3851 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003852 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003853 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003854 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303855int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003856{
Vinod Koulb0e26482011-01-13 22:48:02 +05303857 if (card->instantiated)
3858 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003859 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003860
3861 return 0;
3862}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303863EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003864
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003865/*
3866 * Simplify DAI link configuration by removing ".-1" from device names
3867 * and sanitizing names.
3868 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003869static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003870{
3871 char *found, name[NAME_SIZE];
3872 int id1, id2;
3873
3874 if (dev_name(dev) == NULL)
3875 return NULL;
3876
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003877 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003878
3879 /* are we a "%s.%d" name (platform and SPI components) */
3880 found = strstr(name, dev->driver->name);
3881 if (found) {
3882 /* get ID */
3883 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3884
3885 /* discard ID from name if ID == -1 */
3886 if (*id == -1)
3887 found[strlen(dev->driver->name)] = '\0';
3888 }
3889
3890 } else {
3891 /* I2C component devices are named "bus-addr" */
3892 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3893 char tmp[NAME_SIZE];
3894
3895 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003896 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897
3898 /* sanitize component name for DAI link creation */
3899 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003900 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003901 } else
3902 *id = 0;
3903 }
3904
3905 return kstrdup(name, GFP_KERNEL);
3906}
3907
3908/*
3909 * Simplify DAI link naming for single devices with multiple DAIs by removing
3910 * any ".-1" and using the DAI name (instead of device name).
3911 */
3912static inline char *fmt_multiple_name(struct device *dev,
3913 struct snd_soc_dai_driver *dai_drv)
3914{
3915 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003916 dev_err(dev,
3917 "ASoC: error - multiple DAI %s registered with no name\n",
3918 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003919 return NULL;
3920 }
3921
3922 return kstrdup(dai_drv->name, GFP_KERNEL);
3923}
3924
Mark Brown91151712008-11-30 23:31:24 +00003925/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003926 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003927 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003928 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003929 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003930static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003931{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003932 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003933
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003934 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003935 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3936 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003937 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003938 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003939 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003940 }
Mark Brown91151712008-11-30 23:31:24 +00003941}
Mark Brown91151712008-11-30 23:31:24 +00003942
3943/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003944 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003945 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003946 * @component: The component the DAIs are registered for
3947 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003948 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003949 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3950 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003951 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003952static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003953 struct snd_soc_dai_driver *dai_drv, size_t count,
3954 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003955{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003956 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003958 unsigned int i;
3959 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003961 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003962
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003963 component->dai_drv = dai_drv;
3964 component->num_dai = count;
3965
Mark Brown91151712008-11-30 23:31:24 +00003966 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003967
3968 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003969 if (dai == NULL) {
3970 ret = -ENOMEM;
3971 goto err;
3972 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003973
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003974 /*
3975 * Back in the old days when we still had component-less DAIs,
3976 * instead of having a static name, component-less DAIs would
3977 * inherit the name of the parent device so it is possible to
3978 * register multiple instances of the DAI. We still need to keep
3979 * the same naming style even though those DAIs are not
3980 * component-less anymore.
3981 */
3982 if (count == 1 && legacy_dai_naming) {
3983 dai->name = fmt_single_name(dev, &dai->id);
3984 } else {
3985 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3986 if (dai_drv[i].id)
3987 dai->id = dai_drv[i].id;
3988 else
3989 dai->id = i;
3990 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003991 if (dai->name == NULL) {
3992 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003993 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003994 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003995 }
3996
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003997 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003998 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003999 dai->driver = &dai_drv[i];
4000 if (!dai->driver->ops)
4001 dai->driver->ops = &null_dai_ops;
4002
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004003 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004004
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004005 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004006 }
4007
4008 return 0;
4009
4010err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004011 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004012
4013 return ret;
4014}
Mark Brown91151712008-11-30 23:31:24 +00004015
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004016static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4017 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004018{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004019 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004020
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004021 component->driver->seq_notifier(component, type, subseq);
4022}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004023
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004024static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4025 int event)
4026{
4027 struct snd_soc_component *component = dapm->component;
4028
4029 return component->driver->stream_event(component, event);
4030}
4031
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004032static int snd_soc_component_initialize(struct snd_soc_component *component,
4033 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004034{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004035 struct snd_soc_dapm_context *dapm;
4036
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004037 component->name = fmt_single_name(dev, &component->id);
4038 if (!component->name) {
4039 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004040 return -ENOMEM;
4041 }
4042
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004043 component->dev = dev;
4044 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004045
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004046 if (!component->dapm_ptr)
4047 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004048
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004049 dapm = component->dapm_ptr;
4050 dapm->dev = dev;
4051 dapm->component = component;
4052 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004053 if (driver->seq_notifier)
4054 dapm->seq_notifier = snd_soc_component_seq_notifier;
4055 if (driver->stream_event)
4056 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004057
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004058 INIT_LIST_HEAD(&component->dai_list);
4059 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004060
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004061 return 0;
4062}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004063
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004064static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4065{
4066 list_add(&component->list, &component_list);
4067}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004068
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004069static void snd_soc_component_add(struct snd_soc_component *component)
4070{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004072 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004073 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004074}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004075
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004076static void snd_soc_component_cleanup(struct snd_soc_component *component)
4077{
4078 snd_soc_unregister_dais(component);
4079 kfree(component->name);
4080}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004081
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004082static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4083{
4084 list_del(&component->list);
4085}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004086
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004087static void snd_soc_component_del(struct snd_soc_component *component)
4088{
4089 mutex_lock(&client_mutex);
4090 snd_soc_component_del_unlocked(component);
4091 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004092}
4093
4094int snd_soc_register_component(struct device *dev,
4095 const struct snd_soc_component_driver *cmpnt_drv,
4096 struct snd_soc_dai_driver *dai_drv,
4097 int num_dai)
4098{
4099 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004100 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004101
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004102 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004103 if (!cmpnt) {
4104 dev_err(dev, "ASoC: Failed to allocate memory\n");
4105 return -ENOMEM;
4106 }
4107
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004108 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4109 if (ret)
4110 goto err_free;
4111
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004112 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004113 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004114
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004115 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4116 if (ret < 0) {
4117 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4118 goto err_cleanup;
4119 }
4120
4121 snd_soc_component_add(cmpnt);
4122
4123 return 0;
4124
4125err_cleanup:
4126 snd_soc_component_cleanup(cmpnt);
4127err_free:
4128 kfree(cmpnt);
4129 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004130}
4131EXPORT_SYMBOL_GPL(snd_soc_register_component);
4132
4133/**
4134 * snd_soc_unregister_component - Unregister a component from the ASoC core
4135 *
4136 */
4137void snd_soc_unregister_component(struct device *dev)
4138{
4139 struct snd_soc_component *cmpnt;
4140
4141 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004142 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004143 goto found;
4144 }
4145 return;
4146
4147found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004148 snd_soc_component_del(cmpnt);
4149 snd_soc_component_cleanup(cmpnt);
4150 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004151}
4152EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4153
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004154static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4155{
4156 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4157
4158 return platform->driver->probe(platform);
4159}
4160
4161static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4162{
4163 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4164
4165 platform->driver->remove(platform);
4166}
4167
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004168static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4169 unsigned int reg, unsigned int val)
4170{
4171 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4172
4173 return platform->driver->write(platform, reg, val);
4174}
4175
4176static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4177 unsigned int reg, unsigned int *val)
4178{
4179 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4180
4181 *val = platform->driver->read(platform, reg);
4182
4183 return 0;
4184}
4185
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004186/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004187 * snd_soc_add_platform - Add a platform to the ASoC core
4188 * @dev: The parent device for the platform
4189 * @platform: The platform to add
4190 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004191 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004192int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004193 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004194{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004195 int ret;
4196
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004197 ret = snd_soc_component_initialize(&platform->component,
4198 &platform_drv->component_driver, dev);
4199 if (ret)
4200 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004201
4202 platform->dev = dev;
4203 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004204 if (platform_drv->controls) {
4205 platform->component.controls = platform_drv->controls;
4206 platform->component.num_controls = platform_drv->num_controls;
4207 }
4208 if (platform_drv->dapm_widgets) {
4209 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4210 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4211 platform->component.steal_sibling_dai_widgets = true;
4212 }
4213 if (platform_drv->dapm_routes) {
4214 platform->component.dapm_routes = platform_drv->dapm_routes;
4215 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4216 }
4217
4218 if (platform_drv->probe)
4219 platform->component.probe = snd_soc_platform_drv_probe;
4220 if (platform_drv->remove)
4221 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004222 if (platform_drv->write)
4223 platform->component.write = snd_soc_platform_drv_write;
4224 if (platform_drv->read)
4225 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004226
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004227#ifdef CONFIG_DEBUG_FS
4228 platform->component.debugfs_prefix = "platform";
4229#endif
4230
Mark Brown12a48a8c2008-12-03 19:40:30 +00004231 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004232 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004233 list_add(&platform->list, &platform_list);
4234 mutex_unlock(&client_mutex);
4235
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004236 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4237 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004238
4239 return 0;
4240}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004241EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4242
4243/**
4244 * snd_soc_register_platform - Register a platform with the ASoC core
4245 *
4246 * @platform: platform to register
4247 */
4248int snd_soc_register_platform(struct device *dev,
4249 const struct snd_soc_platform_driver *platform_drv)
4250{
4251 struct snd_soc_platform *platform;
4252 int ret;
4253
4254 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4255
4256 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4257 if (platform == NULL)
4258 return -ENOMEM;
4259
4260 ret = snd_soc_add_platform(dev, platform, platform_drv);
4261 if (ret)
4262 kfree(platform);
4263
4264 return ret;
4265}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004266EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4267
4268/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004269 * snd_soc_remove_platform - Remove a platform from the ASoC core
4270 * @platform: the platform to remove
4271 */
4272void snd_soc_remove_platform(struct snd_soc_platform *platform)
4273{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004274
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004275 mutex_lock(&client_mutex);
4276 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004277 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004278 mutex_unlock(&client_mutex);
4279
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004280 snd_soc_component_cleanup(&platform->component);
4281
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004282 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004283 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004284}
4285EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4286
4287struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4288{
4289 struct snd_soc_platform *platform;
4290
4291 list_for_each_entry(platform, &platform_list, list) {
4292 if (dev == platform->dev)
4293 return platform;
4294 }
4295
4296 return NULL;
4297}
4298EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4299
4300/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004301 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4302 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004303 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004304 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004305void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004306{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004307 struct snd_soc_platform *platform;
4308
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004309 platform = snd_soc_lookup_platform(dev);
4310 if (!platform)
4311 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004312
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004313 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004314 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004315}
4316EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4317
Mark Brown151ab222009-05-09 16:22:58 +01004318static u64 codec_format_map[] = {
4319 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4320 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4321 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4322 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4323 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4324 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4325 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4326 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4327 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4328 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4329 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4330 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4331 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4332 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4333 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4334 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4335};
4336
4337/* Fix up the DAI formats for endianness: codecs don't actually see
4338 * the endianness of the data but we're using the CPU format
4339 * definitions which do need to include endianness so we ensure that
4340 * codec DAIs always have both big and little endian variants set.
4341 */
4342static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4343{
4344 int i;
4345
4346 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4347 if (stream->formats & codec_format_map[i])
4348 stream->formats |= codec_format_map[i];
4349}
4350
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004351static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4352{
4353 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4354
4355 return codec->driver->probe(codec);
4356}
4357
4358static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4359{
4360 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4361
4362 codec->driver->remove(codec);
4363}
4364
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004365static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4366 unsigned int reg, unsigned int val)
4367{
4368 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4369
4370 return codec->driver->write(codec, reg, val);
4371}
4372
4373static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4374 unsigned int reg, unsigned int *val)
4375{
4376 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4377
4378 *val = codec->driver->read(codec, reg);
4379
4380 return 0;
4381}
4382
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004383static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4384 enum snd_soc_bias_level level)
4385{
4386 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4387
4388 return codec->driver->set_bias_level(codec, level);
4389}
4390
Mark Brown0d0cf002008-12-10 14:32:45 +00004391/**
4392 * snd_soc_register_codec - Register a codec with the ASoC core
4393 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004394 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004395 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004396int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004397 const struct snd_soc_codec_driver *codec_drv,
4398 struct snd_soc_dai_driver *dai_drv,
4399 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004400{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004401 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004402 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004403 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004404 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004405
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004406 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004407
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004408 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4409 if (codec == NULL)
4410 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004411
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004412 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004413 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004414
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004415 ret = snd_soc_component_initialize(&codec->component,
4416 &codec_drv->component_driver, dev);
4417 if (ret)
4418 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004419
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004420 if (codec_drv->controls) {
4421 codec->component.controls = codec_drv->controls;
4422 codec->component.num_controls = codec_drv->num_controls;
4423 }
4424 if (codec_drv->dapm_widgets) {
4425 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4426 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4427 }
4428 if (codec_drv->dapm_routes) {
4429 codec->component.dapm_routes = codec_drv->dapm_routes;
4430 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4431 }
4432
4433 if (codec_drv->probe)
4434 codec->component.probe = snd_soc_codec_drv_probe;
4435 if (codec_drv->remove)
4436 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004437 if (codec_drv->write)
4438 codec->component.write = snd_soc_codec_drv_write;
4439 if (codec_drv->read)
4440 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004441 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004442 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004443 if (codec_drv->seq_notifier)
4444 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004445 if (codec_drv->set_bias_level)
4446 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004447 codec->dev = dev;
4448 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004449 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004450 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004451
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004452#ifdef CONFIG_DEBUG_FS
4453 codec->component.init_debugfs = soc_init_codec_debugfs;
4454 codec->component.debugfs_prefix = "codec";
4455#endif
4456
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004457 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004458 if (codec_drv->get_regmap)
4459 regmap = codec_drv->get_regmap(dev);
4460 else
4461 regmap = dev_get_regmap(dev, NULL);
4462
4463 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004464 ret = snd_soc_component_init_io(&codec->component,
4465 regmap);
4466 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004467 dev_err(codec->dev,
4468 "Failed to set cache I/O:%d\n",
4469 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004470 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004471 }
4472 }
4473 }
4474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004475 for (i = 0; i < num_dai; i++) {
4476 fixup_codec_formats(&dai_drv[i].playback);
4477 fixup_codec_formats(&dai_drv[i].capture);
4478 }
4479
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004480 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4481 if (ret < 0) {
4482 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4483 goto err_cleanup;
4484 }
4485
4486 list_for_each_entry(dai, &codec->component.dai_list, list)
4487 dai->codec = codec;
4488
Mark Brown054880f2012-04-09 17:29:19 +01004489 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004490 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004491 list_add(&codec->list, &codec_list);
4492 mutex_unlock(&client_mutex);
4493
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004494 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4495 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004496 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004497
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004498err_cleanup:
4499 snd_soc_component_cleanup(&codec->component);
4500err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004501 kfree(codec);
4502 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004503}
4504EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4505
4506/**
4507 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4508 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004509 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004510 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004511void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004512{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004513 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004514
4515 list_for_each_entry(codec, &codec_list, list) {
4516 if (dev == codec->dev)
4517 goto found;
4518 }
4519 return;
4520
4521found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004522
Mark Brown0d0cf002008-12-10 14:32:45 +00004523 mutex_lock(&client_mutex);
4524 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004525 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004526 mutex_unlock(&client_mutex);
4527
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004528 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4529 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004530
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004531 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004532 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004533 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004534}
4535EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4536
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004537/* Retrieve a card's name from device tree */
4538int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4539 const char *propname)
4540{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304541 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004542 int ret;
4543
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304544 if (!card->dev) {
4545 pr_err("card->dev is not set before calling %s\n", __func__);
4546 return -EINVAL;
4547 }
4548
4549 np = card->dev->of_node;
4550
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004551 ret = of_property_read_string_index(np, propname, 0, &card->name);
4552 /*
4553 * EINVAL means the property does not exist. This is fine providing
4554 * card->name was previously set, which is checked later in
4555 * snd_soc_register_card.
4556 */
4557 if (ret < 0 && ret != -EINVAL) {
4558 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004559 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004560 propname, ret);
4561 return ret;
4562 }
4563
4564 return 0;
4565}
4566EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4567
Xiubo Li9a6d4862014-02-08 15:59:52 +08004568static const struct snd_soc_dapm_widget simple_widgets[] = {
4569 SND_SOC_DAPM_MIC("Microphone", NULL),
4570 SND_SOC_DAPM_LINE("Line", NULL),
4571 SND_SOC_DAPM_HP("Headphone", NULL),
4572 SND_SOC_DAPM_SPK("Speaker", NULL),
4573};
4574
4575int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4576 const char *propname)
4577{
4578 struct device_node *np = card->dev->of_node;
4579 struct snd_soc_dapm_widget *widgets;
4580 const char *template, *wname;
4581 int i, j, num_widgets, ret;
4582
4583 num_widgets = of_property_count_strings(np, propname);
4584 if (num_widgets < 0) {
4585 dev_err(card->dev,
4586 "ASoC: Property '%s' does not exist\n", propname);
4587 return -EINVAL;
4588 }
4589 if (num_widgets & 1) {
4590 dev_err(card->dev,
4591 "ASoC: Property '%s' length is not even\n", propname);
4592 return -EINVAL;
4593 }
4594
4595 num_widgets /= 2;
4596 if (!num_widgets) {
4597 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4598 propname);
4599 return -EINVAL;
4600 }
4601
4602 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4603 GFP_KERNEL);
4604 if (!widgets) {
4605 dev_err(card->dev,
4606 "ASoC: Could not allocate memory for widgets\n");
4607 return -ENOMEM;
4608 }
4609
4610 for (i = 0; i < num_widgets; i++) {
4611 ret = of_property_read_string_index(np, propname,
4612 2 * i, &template);
4613 if (ret) {
4614 dev_err(card->dev,
4615 "ASoC: Property '%s' index %d read error:%d\n",
4616 propname, 2 * i, ret);
4617 return -EINVAL;
4618 }
4619
4620 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4621 if (!strncmp(template, simple_widgets[j].name,
4622 strlen(simple_widgets[j].name))) {
4623 widgets[i] = simple_widgets[j];
4624 break;
4625 }
4626 }
4627
4628 if (j >= ARRAY_SIZE(simple_widgets)) {
4629 dev_err(card->dev,
4630 "ASoC: DAPM widget '%s' is not supported\n",
4631 template);
4632 return -EINVAL;
4633 }
4634
4635 ret = of_property_read_string_index(np, propname,
4636 (2 * i) + 1,
4637 &wname);
4638 if (ret) {
4639 dev_err(card->dev,
4640 "ASoC: Property '%s' index %d read error:%d\n",
4641 propname, (2 * i) + 1, ret);
4642 return -EINVAL;
4643 }
4644
4645 widgets[i].name = wname;
4646 }
4647
4648 card->dapm_widgets = widgets;
4649 card->num_dapm_widgets = num_widgets;
4650
4651 return 0;
4652}
4653EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4654
Xiubo Li89c67852014-02-14 09:34:35 +08004655int snd_soc_of_parse_tdm_slot(struct device_node *np,
4656 unsigned int *slots,
4657 unsigned int *slot_width)
4658{
4659 u32 val;
4660 int ret;
4661
4662 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4663 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4664 if (ret)
4665 return ret;
4666
4667 if (slots)
4668 *slots = val;
4669 }
4670
4671 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4672 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4673 if (ret)
4674 return ret;
4675
4676 if (slot_width)
4677 *slot_width = val;
4678 }
4679
4680 return 0;
4681}
4682EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4683
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004684int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4685 const char *propname)
4686{
4687 struct device_node *np = card->dev->of_node;
4688 int num_routes;
4689 struct snd_soc_dapm_route *routes;
4690 int i, ret;
4691
4692 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004693 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004694 dev_err(card->dev,
4695 "ASoC: Property '%s' does not exist or its length is not even\n",
4696 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004697 return -EINVAL;
4698 }
4699 num_routes /= 2;
4700 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004701 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004702 propname);
4703 return -EINVAL;
4704 }
4705
4706 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4707 GFP_KERNEL);
4708 if (!routes) {
4709 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004710 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004711 return -EINVAL;
4712 }
4713
4714 for (i = 0; i < num_routes; i++) {
4715 ret = of_property_read_string_index(np, propname,
4716 2 * i, &routes[i].sink);
4717 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004718 dev_err(card->dev,
4719 "ASoC: Property '%s' index %d could not be read: %d\n",
4720 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004721 return -EINVAL;
4722 }
4723 ret = of_property_read_string_index(np, propname,
4724 (2 * i) + 1, &routes[i].source);
4725 if (ret) {
4726 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004727 "ASoC: Property '%s' index %d could not be read: %d\n",
4728 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004729 return -EINVAL;
4730 }
4731 }
4732
4733 card->num_dapm_routes = num_routes;
4734 card->dapm_routes = routes;
4735
4736 return 0;
4737}
4738EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4739
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004740unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004741 const char *prefix,
4742 struct device_node **bitclkmaster,
4743 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004744{
4745 int ret, i;
4746 char prop[128];
4747 unsigned int format = 0;
4748 int bit, frame;
4749 const char *str;
4750 struct {
4751 char *name;
4752 unsigned int val;
4753 } of_fmt_table[] = {
4754 { "i2s", SND_SOC_DAIFMT_I2S },
4755 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4756 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4757 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4758 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4759 { "ac97", SND_SOC_DAIFMT_AC97 },
4760 { "pdm", SND_SOC_DAIFMT_PDM},
4761 { "msb", SND_SOC_DAIFMT_MSB },
4762 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004763 };
4764
4765 if (!prefix)
4766 prefix = "";
4767
4768 /*
4769 * check "[prefix]format = xxx"
4770 * SND_SOC_DAIFMT_FORMAT_MASK area
4771 */
4772 snprintf(prop, sizeof(prop), "%sformat", prefix);
4773 ret = of_property_read_string(np, prop, &str);
4774 if (ret == 0) {
4775 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4776 if (strcmp(str, of_fmt_table[i].name) == 0) {
4777 format |= of_fmt_table[i].val;
4778 break;
4779 }
4780 }
4781 }
4782
4783 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004784 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004785 * SND_SOC_DAIFMT_CLOCK_MASK area
4786 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004787 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4788 if (of_get_property(np, prop, NULL))
4789 format |= SND_SOC_DAIFMT_CONT;
4790 else
4791 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004792
4793 /*
4794 * check "[prefix]bitclock-inversion"
4795 * check "[prefix]frame-inversion"
4796 * SND_SOC_DAIFMT_INV_MASK area
4797 */
4798 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4799 bit = !!of_get_property(np, prop, NULL);
4800
4801 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4802 frame = !!of_get_property(np, prop, NULL);
4803
4804 switch ((bit << 4) + frame) {
4805 case 0x11:
4806 format |= SND_SOC_DAIFMT_IB_IF;
4807 break;
4808 case 0x10:
4809 format |= SND_SOC_DAIFMT_IB_NF;
4810 break;
4811 case 0x01:
4812 format |= SND_SOC_DAIFMT_NB_IF;
4813 break;
4814 default:
4815 /* SND_SOC_DAIFMT_NB_NF is default */
4816 break;
4817 }
4818
4819 /*
4820 * check "[prefix]bitclock-master"
4821 * check "[prefix]frame-master"
4822 * SND_SOC_DAIFMT_MASTER_MASK area
4823 */
4824 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4825 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004826 if (bit && bitclkmaster)
4827 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004828
4829 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4830 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004831 if (frame && framemaster)
4832 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004833
4834 switch ((bit << 4) + frame) {
4835 case 0x11:
4836 format |= SND_SOC_DAIFMT_CBM_CFM;
4837 break;
4838 case 0x10:
4839 format |= SND_SOC_DAIFMT_CBM_CFS;
4840 break;
4841 case 0x01:
4842 format |= SND_SOC_DAIFMT_CBS_CFM;
4843 break;
4844 default:
4845 format |= SND_SOC_DAIFMT_CBS_CFS;
4846 break;
4847 }
4848
4849 return format;
4850}
4851EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4852
Kuninori Morimotocb470082013-09-10 17:39:56 -07004853int snd_soc_of_get_dai_name(struct device_node *of_node,
4854 const char **dai_name)
4855{
4856 struct snd_soc_component *pos;
4857 struct of_phandle_args args;
4858 int ret;
4859
4860 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4861 "#sound-dai-cells", 0, &args);
4862 if (ret)
4863 return ret;
4864
4865 ret = -EPROBE_DEFER;
4866
4867 mutex_lock(&client_mutex);
4868 list_for_each_entry(pos, &component_list, list) {
4869 if (pos->dev->of_node != args.np)
4870 continue;
4871
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004872 if (pos->driver->of_xlate_dai_name) {
4873 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4874 } else {
4875 int id = -1;
4876
4877 switch (args.args_count) {
4878 case 0:
4879 id = 0; /* same as dai_drv[0] */
4880 break;
4881 case 1:
4882 id = args.args[0];
4883 break;
4884 default:
4885 /* not supported */
4886 break;
4887 }
4888
4889 if (id < 0 || id >= pos->num_dai) {
4890 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004891 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004892 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004893
4894 ret = 0;
4895
4896 *dai_name = pos->dai_drv[id].name;
4897 if (!*dai_name)
4898 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004899 }
4900
Kuninori Morimotocb470082013-09-10 17:39:56 -07004901 break;
4902 }
4903 mutex_unlock(&client_mutex);
4904
4905 of_node_put(args.np);
4906
4907 return ret;
4908}
4909EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4910
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004911static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004912{
Mark Brown384c89e2008-12-03 17:34:03 +00004913#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004914 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4915 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004916 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004917 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004918 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004919
Mark Brown8a9dab12011-01-10 22:25:21 +00004920 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004921 &codec_list_fops))
4922 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4923
Mark Brown8a9dab12011-01-10 22:25:21 +00004924 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004925 &dai_list_fops))
4926 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004927
Mark Brown8a9dab12011-01-10 22:25:21 +00004928 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004929 &platform_list_fops))
4930 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004931#endif
4932
Mark Brownfb257892011-04-28 10:57:54 +01004933 snd_soc_util_init();
4934
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004935 return platform_driver_register(&soc_driver);
4936}
Mark Brown4abe8e12010-10-12 17:41:03 +01004937module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004938
Mark Brown7d8c16a2008-11-30 22:11:24 +00004939static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004940{
Mark Brownfb257892011-04-28 10:57:54 +01004941 snd_soc_util_exit();
4942
Mark Brown384c89e2008-12-03 17:34:03 +00004943#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004944 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004945#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004946 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004947}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004948module_exit(snd_soc_exit);
4949
4950/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004951MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004952MODULE_DESCRIPTION("ALSA SoC Core");
4953MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004954MODULE_ALIAS("platform:soc-audio");