blob: b833cc6fd86dbf3bb353456499fa707afbd4c296 [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
1264 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001265 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001267 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001268 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001269
1270 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001271 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001273 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001274 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275
1276 return 0;
1277}
1278
Stephen Warren62ae68f2012-06-08 12:34:23 -06001279static int soc_probe_link_components(struct snd_soc_card *card, int num,
1280 int order)
1281{
1282 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001283 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001284 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001285 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001286
1287 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001288 if (rtd->cpu_dai->codec) {
1289 component = &rtd->cpu_dai->codec->component;
1290 if (!component->probed &&
1291 component->driver->probe_order == order) {
1292 ret = soc_probe_component(card, component);
1293 if (ret < 0)
1294 return ret;
1295 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001296 }
1297
Benoit Cousson88bd8702014-07-08 23:19:34 +02001298 /* probe the CODEC-side components */
1299 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001300 component = &rtd->codec_dais[i]->codec->component;
1301 if (!component->probed &&
1302 component->driver->probe_order == order) {
1303 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001304 if (ret < 0)
1305 return ret;
1306 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001307 }
1308
1309 /* probe the platform */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001310 if (!platform->component.probed &&
1311 platform->component.driver->probe_order == order) {
1312 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001313 if (ret < 0)
1314 return ret;
1315 }
1316
1317 return 0;
1318}
1319
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001320static int soc_probe_codec_dai(struct snd_soc_card *card,
1321 struct snd_soc_dai *codec_dai,
1322 int order)
1323{
1324 int ret;
1325
1326 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1327 if (codec_dai->driver->probe) {
1328 ret = codec_dai->driver->probe(codec_dai);
1329 if (ret < 0) {
1330 dev_err(codec_dai->dev,
1331 "ASoC: failed to probe CODEC DAI %s: %d\n",
1332 codec_dai->name, ret);
1333 return ret;
1334 }
1335 }
1336
1337 /* mark codec_dai as probed and add to card dai list */
1338 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001339 }
1340
1341 return 0;
1342}
1343
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001344static int soc_link_dai_widgets(struct snd_soc_card *card,
1345 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001346 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001347{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001348 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1349 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001350 struct snd_soc_dapm_widget *play_w, *capture_w;
1351 int ret;
1352
Benoit Cousson88bd8702014-07-08 23:19:34 +02001353 if (rtd->num_codecs > 1)
1354 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1355
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001356 /* link the DAI widgets */
1357 play_w = codec_dai->playback_widget;
1358 capture_w = cpu_dai->capture_widget;
1359 if (play_w && capture_w) {
1360 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1361 capture_w, play_w);
1362 if (ret != 0) {
1363 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1364 play_w->name, capture_w->name, ret);
1365 return ret;
1366 }
1367 }
1368
1369 play_w = cpu_dai->playback_widget;
1370 capture_w = codec_dai->capture_widget;
1371 if (play_w && capture_w) {
1372 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1373 capture_w, play_w);
1374 if (ret != 0) {
1375 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1376 play_w->name, capture_w->name, ret);
1377 return ret;
1378 }
1379 }
1380
1381 return 0;
1382}
1383
Stephen Warren62ae68f2012-06-08 12:34:23 -06001384static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385{
1386 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1387 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001389 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001390 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001391
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001392 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001393 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001394
1395 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001396 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001398 for (i = 0; i < rtd->num_codecs; i++)
1399 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001400
1401 /* set default power off timeout */
1402 rtd->pmdown_time = pmdown_time;
1403
1404 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001405 if (!cpu_dai->probed &&
1406 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001407 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001408 if (!try_module_get(cpu_dai->dev->driver->owner))
1409 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001410 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001411
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412 if (cpu_dai->driver->probe) {
1413 ret = cpu_dai->driver->probe(cpu_dai);
1414 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001415 dev_err(cpu_dai->dev,
1416 "ASoC: failed to probe CPU DAI %s: %d\n",
1417 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001418 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001419 return ret;
1420 }
1421 }
1422 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 }
1424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001426 for (i = 0; i < rtd->num_codecs; i++) {
1427 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1428 if (ret)
1429 return ret;
1430 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001431
Liam Girdwood0168bf02011-06-07 16:08:05 +01001432 /* complete DAI probe during last probe */
1433 if (order != SND_SOC_COMP_ORDER_LAST)
1434 return 0;
1435
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001436 /* do machine specific initialization */
1437 if (dai_link->init) {
1438 ret = dai_link->init(rtd);
1439 if (ret < 0) {
1440 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1441 dai_link->name, ret);
1442 return ret;
1443 }
1444 }
1445
1446 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001447 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001450#ifdef CONFIG_DEBUG_FS
1451 /* add DPCM sysfs entries */
1452 if (dai_link->dynamic) {
1453 ret = soc_dpcm_debugfs_add(rtd);
1454 if (ret < 0) {
1455 dev_err(rtd->dev,
1456 "ASoC: failed to add dpcm sysfs entries: %d\n",
1457 ret);
1458 return ret;
1459 }
1460 }
1461#endif
1462
Mark Brown36ae1a92012-01-06 17:12:45 -08001463 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001465 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1466 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001467
Namarta Kohli1245b702012-08-16 17:10:41 +05301468 if (cpu_dai->driver->compress_dai) {
1469 /*create compress_device"*/
1470 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001471 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001472 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301473 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001474 return ret;
1475 }
1476 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301477
1478 if (!dai_link->params) {
1479 /* create the pcm */
1480 ret = soc_new_pcm(rtd, num);
1481 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001482 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301483 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001484 return ret;
1485 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301486 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001487 INIT_DELAYED_WORK(&rtd->delayed_work,
1488 codec2codec_close_delayed_work);
1489
Namarta Kohli1245b702012-08-16 17:10:41 +05301490 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001491 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001492 if (ret)
1493 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001494 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495 }
1496
1497 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001498 for (i = 0; i < rtd->num_codecs; i++) {
1499 if (rtd->codec_dais[i]->driver->ac97_control)
1500 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1501 rtd->cpu_dai->ac97_pdata);
1502 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503
1504 return 0;
1505}
1506
1507#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001508static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1509 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510{
1511 int ret;
1512
1513 /* Only instantiate AC97 if not already done by the adaptor
1514 * for the generic AC97 subsystem.
1515 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001516 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001517 /*
1518 * It is possible that the AC97 device is already registered to
1519 * the device subsystem. This happens when the device is created
1520 * via snd_ac97_mixer(). Currently only SoC codec that does so
1521 * is the generic AC97 glue but others migh emerge.
1522 *
1523 * In those cases we don't try to register the device again.
1524 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001525 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001526 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001528 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001530 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001531 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532 return ret;
1533 }
1534
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001535 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001536 }
1537 return 0;
1538}
1539
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001540static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001541{
1542 if (codec->ac97_registered) {
1543 soc_ac97_dev_unregister(codec);
1544 codec->ac97_registered = 0;
1545 }
1546}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001547
Benoit Cousson88bd8702014-07-08 23:19:34 +02001548static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1549{
1550 int i, ret;
1551
1552 for (i = 0; i < rtd->num_codecs; i++) {
1553 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1554
1555 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1556 if (ret) {
1557 while (--i >= 0)
1558 soc_unregister_ac97_codec(codec_dai->codec);
1559 return ret;
1560 }
1561 }
1562
1563 return 0;
1564}
1565
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001566static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1567{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001568 int i;
1569
1570 for (i = 0; i < rtd->num_codecs; i++)
1571 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001572}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573#endif
1574
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001575static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001576{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001577 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001578 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1579 const char *codecname = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001580
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001581 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1582 if (!rtd->codec) {
1583 if (aux_dev->codec_of_node)
1584 codecname = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001585
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001586 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1587 return -EPROBE_DEFER;
1588 }
1589
1590 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001591}
1592
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001593static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1594{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001595 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001596 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001597 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001598
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001599 if (rtd->codec->component.probed) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001600 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001601 return -EBUSY;
1602 }
1603
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001604 ret = soc_probe_component(card, &rtd->codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001605 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001606 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001607
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001608 /* do machine specific initialization */
1609 if (aux_dev->init) {
1610 ret = aux_dev->init(&rtd->codec->dapm);
1611 if (ret < 0) {
1612 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1613 aux_dev->name, ret);
1614 return ret;
1615 }
1616 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001617
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001618 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001619}
1620
1621static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1622{
1623 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1624 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001625
1626 /* unregister the rtd device */
1627 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001628 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001629 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001630 rtd->dev_registered = 0;
1631 }
1632
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001633 if (codec && codec->component.probed)
1634 soc_remove_component(&codec->component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001635}
1636
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001637static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001638{
1639 int ret;
1640
1641 if (codec->cache_init)
1642 return 0;
1643
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001644 ret = snd_soc_cache_init(codec);
1645 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001646 dev_err(codec->dev,
1647 "ASoC: Failed to set cache compression type: %d\n",
1648 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001649 return ret;
1650 }
1651 codec->cache_init = 1;
1652 return 0;
1653}
1654
Mark Brownb19e6e72012-03-14 21:18:39 +00001655static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001657 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001658 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001659 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660
Liam Girdwood01b9d992012-03-07 10:38:25 +00001661 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662
Mark Brownb19e6e72012-03-14 21:18:39 +00001663 /* bind DAIs */
1664 for (i = 0; i < card->num_links; i++) {
1665 ret = soc_bind_dai_link(card, i);
1666 if (ret != 0)
1667 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 }
1669
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001670 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001671 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001672 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001673 if (ret != 0)
1674 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675 }
1676
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001677 /* initialize the register cache for each available codec */
1678 list_for_each_entry(codec, &codec_list, list) {
1679 if (codec->cache_init)
1680 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001681 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001682 if (ret < 0)
1683 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001684 }
1685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001687 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 card->owner, 0, &card->snd_card);
1689 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001690 dev_err(card->dev,
1691 "ASoC: can't create sound card for card %s: %d\n",
1692 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001693 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001695
Mark Browne37a4972011-03-02 18:21:57 +00001696 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1697 card->dapm.dev = card->dev;
1698 card->dapm.card = card;
1699 list_add(&card->dapm.list, &card->dapm_list);
1700
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001701#ifdef CONFIG_DEBUG_FS
1702 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1703#endif
1704
Mark Brown88ee1c62011-02-02 10:43:26 +00001705#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001706 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001707 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001708#endif
Andy Green6ed25972008-06-13 16:24:05 +01001709
Mark Brown9a841eb2011-04-12 17:51:37 -07001710 if (card->dapm_widgets)
1711 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1712 card->num_dapm_widgets);
1713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 /* initialise the sound card only once */
1715 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001716 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717 if (ret < 0)
1718 goto card_probe_error;
1719 }
1720
Stephen Warren62ae68f2012-06-08 12:34:23 -06001721 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001722 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1723 order++) {
1724 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001725 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001726 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001727 dev_err(card->dev,
1728 "ASoC: failed to instantiate card %d\n",
1729 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001730 goto probe_dai_err;
1731 }
1732 }
1733 }
1734
1735 /* probe all DAI links on this card */
1736 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1737 order++) {
1738 for (i = 0; i < card->num_links; i++) {
1739 ret = soc_probe_link_dais(card, i, order);
1740 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001741 dev_err(card->dev,
1742 "ASoC: failed to instantiate card %d\n",
1743 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001744 goto probe_dai_err;
1745 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747 }
1748
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001749 for (i = 0; i < card->num_aux_devs; i++) {
1750 ret = soc_probe_aux_dev(card, i);
1751 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001752 dev_err(card->dev,
1753 "ASoC: failed to add auxiliary devices %d\n",
1754 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001755 goto probe_aux_dev_err;
1756 }
1757 }
1758
Mark Brown888df392012-02-16 19:37:51 -08001759 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001760 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001761
Mark Brownb7af1da2011-04-07 19:18:44 +09001762 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001763 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001764
Mark Brownb8ad29d2011-03-02 18:35:51 +00001765 if (card->dapm_routes)
1766 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1767 card->num_dapm_routes);
1768
Mark Brown75d9ac42011-09-27 16:41:01 +01001769 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001770 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001771 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001772 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001773
Mark Brownf04209a2012-04-09 16:29:21 +01001774 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001775 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1776 int j;
1777
1778 for (j = 0; j < rtd->num_codecs; j++) {
1779 struct snd_soc_dai *codec_dai = codec_dais[j];
1780
1781 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1782 if (ret != 0 && ret != -ENOTSUPP)
1783 dev_warn(codec_dai->dev,
1784 "ASoC: Failed to set DAI format: %d\n",
1785 ret);
1786 }
Mark Brownf04209a2012-04-09 16:29:21 +01001787 }
1788
1789 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001790 if (dai_fmt &&
1791 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001792 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1793 dai_fmt);
1794 if (ret != 0 && ret != -ENOTSUPP)
1795 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001796 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001797 ret);
1798 } else if (dai_fmt) {
1799 /* Flip the polarity for the "CPU" end */
1800 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1801 switch (dai_link->dai_fmt &
1802 SND_SOC_DAIFMT_MASTER_MASK) {
1803 case SND_SOC_DAIFMT_CBM_CFM:
1804 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1805 break;
1806 case SND_SOC_DAIFMT_CBM_CFS:
1807 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1808 break;
1809 case SND_SOC_DAIFMT_CBS_CFM:
1810 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1811 break;
1812 case SND_SOC_DAIFMT_CBS_CFS:
1813 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1814 break;
1815 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001816
1817 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001818 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001819 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001820 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001822 ret);
1823 }
1824 }
1825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001826 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001828 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1829 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001830 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1831 "%s", card->driver_name ? card->driver_name : card->name);
1832 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1833 switch (card->snd_card->driver[i]) {
1834 case '_':
1835 case '-':
1836 case '\0':
1837 break;
1838 default:
1839 if (!isalnum(card->snd_card->driver[i]))
1840 card->snd_card->driver[i] = '_';
1841 break;
1842 }
1843 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844
Mark Brown28e9ad92011-03-02 18:36:34 +00001845 if (card->late_probe) {
1846 ret = card->late_probe(card);
1847 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001848 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001849 card->name, ret);
1850 goto probe_aux_dev_err;
1851 }
1852 }
1853
Stephen Warren16332812011-11-23 12:42:04 -07001854 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001855 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001856
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001857 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001858
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859 ret = snd_card_register(card->snd_card);
1860 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001861 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1862 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001863 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001864 }
1865
1866#ifdef CONFIG_SND_SOC_AC97_BUS
1867 /* register any AC97 codecs */
1868 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001869 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1870 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001871 dev_err(card->dev,
1872 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001873 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001874 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001875 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001876 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001877 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001878#endif
1879
Mark Brown435c5e22008-12-04 15:32:53 +00001880 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001881 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001882 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001883
1884 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001886probe_aux_dev_err:
1887 for (i = 0; i < card->num_aux_devs; i++)
1888 soc_remove_aux_dev(card, i);
1889
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001890probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001891 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001892
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001894 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001895 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896
1897 snd_card_free(card->snd_card);
1898
Mark Brownb19e6e72012-03-14 21:18:39 +00001899base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001901
Mark Brownb19e6e72012-03-14 21:18:39 +00001902 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001903}
1904
1905/* probes a new socdev */
1906static int soc_probe(struct platform_device *pdev)
1907{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001908 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001909
Vinod Koul70a7ca32011-01-14 19:22:48 +05301910 /*
1911 * no card, so machine driver should be registering card
1912 * we should not be here in that case so ret error
1913 */
1914 if (!card)
1915 return -EINVAL;
1916
Mark Brownfe4085e2012-03-02 13:07:41 +00001917 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001918 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001919 card->name);
1920
Mark Brown435c5e22008-12-04 15:32:53 +00001921 /* Bodge while we unpick instantiation */
1922 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923
Mark Brown28d528c2012-08-09 18:45:23 +01001924 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925}
1926
Vinod Koulb0e26482011-01-13 22:48:02 +05301927static int soc_cleanup_card_resources(struct snd_soc_card *card)
1928{
Vinod Koulb0e26482011-01-13 22:48:02 +05301929 int i;
1930
1931 /* make sure any delayed work runs */
1932 for (i = 0; i < card->num_rtd; i++) {
1933 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001934 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301935 }
1936
1937 /* remove auxiliary devices */
1938 for (i = 0; i < card->num_aux_devs; i++)
1939 soc_remove_aux_dev(card, i);
1940
1941 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001942 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301943
1944 soc_cleanup_card_debugfs(card);
1945
1946 /* remove the card */
1947 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001948 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301949
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001950 snd_soc_dapm_free(&card->dapm);
1951
Vinod Koulb0e26482011-01-13 22:48:02 +05301952 snd_card_free(card->snd_card);
1953 return 0;
1954
1955}
1956
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957/* removes a socdev */
1958static int soc_remove(struct platform_device *pdev)
1959{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961
Mark Brownc5af3a22008-11-28 13:29:45 +00001962 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963 return 0;
1964}
1965
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001966int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001967{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001968 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969 int i;
Mark Brown51737472009-06-22 13:16:51 +01001970
1971 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001972 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001973
1974 /* Flush out pmdown_time work - we actually do want to run it
1975 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976 for (i = 0; i < card->num_rtd; i++) {
1977 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001978 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 }
Mark Brown51737472009-06-22 13:16:51 +01001980
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001981 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001982
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001983 /* deactivate pins to sleep state */
1984 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001985 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1986 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1987 int j;
1988
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001989 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001990 for (j = 0; j < rtd->num_codecs; j++) {
1991 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1992 pinctrl_pm_select_sleep_state(codec_dai->dev);
1993 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001994 }
1995
Mark Brown416356f2009-06-30 19:05:15 +01001996 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001997}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001998EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001999
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002000const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302001 .suspend = snd_soc_suspend,
2002 .resume = snd_soc_resume,
2003 .freeze = snd_soc_suspend,
2004 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002005 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302006 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002007};
Stephen Warrendeb26072011-04-05 19:35:30 -06002008EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002009
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010/* ASoC platform driver */
2011static struct platform_driver soc_driver = {
2012 .driver = {
2013 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002014 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002015 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002016 },
2017 .probe = soc_probe,
2018 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019};
2020
Mark Brown096e49d2009-07-05 15:12:22 +01002021/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 * snd_soc_new_ac97_codec - initailise AC97 device
2023 * @codec: audio codec
2024 * @ops: AC97 bus operations
2025 * @num: AC97 codec number
2026 *
2027 * Initialises AC97 codec resources for use by ad-hoc devices only.
2028 */
2029int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2030 struct snd_ac97_bus_ops *ops, int num)
2031{
2032 mutex_lock(&codec->mutex);
2033
2034 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2035 if (codec->ac97 == NULL) {
2036 mutex_unlock(&codec->mutex);
2037 return -ENOMEM;
2038 }
2039
2040 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2041 if (codec->ac97->bus == NULL) {
2042 kfree(codec->ac97);
2043 codec->ac97 = NULL;
2044 mutex_unlock(&codec->mutex);
2045 return -ENOMEM;
2046 }
2047
2048 codec->ac97->bus->ops = ops;
2049 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002050
2051 /*
2052 * Mark the AC97 device to be created by us. This way we ensure that the
2053 * device will be registered with the device subsystem later on.
2054 */
2055 codec->ac97_created = 1;
2056
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057 mutex_unlock(&codec->mutex);
2058 return 0;
2059}
2060EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2061
Markus Pargmann741a5092013-08-19 17:05:55 +02002062static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2063
2064static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2065{
2066 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2067
2068 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2069
2070 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2071
2072 udelay(10);
2073
2074 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2075
2076 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2077 msleep(2);
2078}
2079
2080static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2081{
2082 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2083
2084 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2085
2086 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2087 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2088 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2089
2090 udelay(10);
2091
2092 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2093
2094 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2095 msleep(2);
2096}
2097
2098static int snd_soc_ac97_parse_pinctl(struct device *dev,
2099 struct snd_ac97_reset_cfg *cfg)
2100{
2101 struct pinctrl *p;
2102 struct pinctrl_state *state;
2103 int gpio;
2104 int ret;
2105
2106 p = devm_pinctrl_get(dev);
2107 if (IS_ERR(p)) {
2108 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002109 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002110 }
2111 cfg->pctl = p;
2112
2113 state = pinctrl_lookup_state(p, "ac97-reset");
2114 if (IS_ERR(state)) {
2115 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002116 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002117 }
2118 cfg->pstate_reset = state;
2119
2120 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2121 if (IS_ERR(state)) {
2122 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002123 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002124 }
2125 cfg->pstate_warm_reset = state;
2126
2127 state = pinctrl_lookup_state(p, "ac97-running");
2128 if (IS_ERR(state)) {
2129 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002130 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002131 }
2132 cfg->pstate_run = state;
2133
2134 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2135 if (gpio < 0) {
2136 dev_err(dev, "Can't find ac97-sync gpio\n");
2137 return gpio;
2138 }
2139 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2140 if (ret) {
2141 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2142 return ret;
2143 }
2144 cfg->gpio_sync = gpio;
2145
2146 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2147 if (gpio < 0) {
2148 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2149 return gpio;
2150 }
2151 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2152 if (ret) {
2153 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2154 return ret;
2155 }
2156 cfg->gpio_sdata = gpio;
2157
2158 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2159 if (gpio < 0) {
2160 dev_err(dev, "Can't find ac97-reset gpio\n");
2161 return gpio;
2162 }
2163 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2164 if (ret) {
2165 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2166 return ret;
2167 }
2168 cfg->gpio_reset = gpio;
2169
2170 return 0;
2171}
2172
Mark Brownb047e1c2013-06-26 12:45:59 +01002173struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002174EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002175
2176int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2177{
2178 if (ops == soc_ac97_ops)
2179 return 0;
2180
2181 if (soc_ac97_ops && ops)
2182 return -EBUSY;
2183
2184 soc_ac97_ops = ops;
2185
2186 return 0;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2189
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002190/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002191 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2192 *
2193 * This function sets the reset and warm_reset properties of ops and parses
2194 * the device node of pdev to get pinctrl states and gpio numbers to use.
2195 */
2196int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2197 struct platform_device *pdev)
2198{
2199 struct device *dev = &pdev->dev;
2200 struct snd_ac97_reset_cfg cfg;
2201 int ret;
2202
2203 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2204 if (ret)
2205 return ret;
2206
2207 ret = snd_soc_set_ac97_ops(ops);
2208 if (ret)
2209 return ret;
2210
2211 ops->warm_reset = snd_soc_ac97_warm_reset;
2212 ops->reset = snd_soc_ac97_reset;
2213
2214 snd_ac97_rst_cfg = cfg;
2215 return 0;
2216}
2217EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2218
2219/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220 * snd_soc_free_ac97_codec - free AC97 codec device
2221 * @codec: audio codec
2222 *
2223 * Frees AC97 codec device resources.
2224 */
2225void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2226{
2227 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002228#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002229 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002230#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231 kfree(codec->ac97->bus);
2232 kfree(codec->ac97);
2233 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002234 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 mutex_unlock(&codec->mutex);
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2238
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_cnew - create new control
2241 * @_template: control template
2242 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002243 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002244 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 *
2246 * Create a new mixer control from a template control.
2247 *
2248 * Returns 0 for success, else error.
2249 */
2250struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002251 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002252 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253{
2254 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002255 struct snd_kcontrol *kcontrol;
2256 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257
2258 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 template.index = 0;
2260
Mark Brownefb7ac32011-03-08 17:23:24 +00002261 if (!long_name)
2262 long_name = template.name;
2263
2264 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002265 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!name)
2267 return NULL;
2268
Mark Brownefb7ac32011-03-08 17:23:24 +00002269 template.name = name;
2270 } else {
2271 template.name = long_name;
2272 }
2273
2274 kcontrol = snd_ctl_new1(&template, data);
2275
2276 kfree(name);
2277
2278 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279}
2280EXPORT_SYMBOL_GPL(snd_soc_cnew);
2281
Liam Girdwood022658b2012-02-03 17:43:09 +00002282static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2283 const struct snd_kcontrol_new *controls, int num_controls,
2284 const char *prefix, void *data)
2285{
2286 int err, i;
2287
2288 for (i = 0; i < num_controls; i++) {
2289 const struct snd_kcontrol_new *control = &controls[i];
2290 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2291 control->name, prefix));
2292 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002293 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2294 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002295 return err;
2296 }
2297 }
2298
2299 return 0;
2300}
2301
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002302struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2303 const char *name)
2304{
2305 struct snd_card *card = soc_card->snd_card;
2306 struct snd_kcontrol *kctl;
2307
2308 if (unlikely(!name))
2309 return NULL;
2310
2311 list_for_each_entry(kctl, &card->controls, list)
2312 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2313 return kctl;
2314 return NULL;
2315}
2316EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2317
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002319 * snd_soc_add_component_controls - Add an array of controls to a component.
2320 *
2321 * @component: Component to add controls to
2322 * @controls: Array of controls to add
2323 * @num_controls: Number of elements in the array
2324 *
2325 * Return: 0 for success, else error.
2326 */
2327int snd_soc_add_component_controls(struct snd_soc_component *component,
2328 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2329{
2330 struct snd_card *card = component->card->snd_card;
2331
2332 return snd_soc_add_controls(card, component->dev, controls,
2333 num_controls, component->name_prefix, component);
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2336
2337/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002338 * snd_soc_add_codec_controls - add an array of controls to a codec.
2339 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002340 * duplicating this code.
2341 *
2342 * @codec: codec to add controls to
2343 * @controls: array of controls to add
2344 * @num_controls: number of elements in the array
2345 *
2346 * Return 0 for success, else error.
2347 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002348int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002349 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002350{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002351 return snd_soc_add_component_controls(&codec->component, controls,
2352 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002353}
Liam Girdwood022658b2012-02-03 17:43:09 +00002354EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002355
2356/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002357 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002358 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002359 *
2360 * @platform: platform to add controls to
2361 * @controls: array of controls to add
2362 * @num_controls: number of elements in the array
2363 *
2364 * Return 0 for success, else error.
2365 */
2366int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002367 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002368{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002369 return snd_soc_add_component_controls(&platform->component, controls,
2370 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002371}
2372EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2373
2374/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002375 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2376 * Convenience function to add a list of controls.
2377 *
2378 * @soc_card: SoC card to add controls to
2379 * @controls: array of controls to add
2380 * @num_controls: number of elements in the array
2381 *
2382 * Return 0 for success, else error.
2383 */
2384int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2385 const struct snd_kcontrol_new *controls, int num_controls)
2386{
2387 struct snd_card *card = soc_card->snd_card;
2388
2389 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2390 NULL, soc_card);
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2393
2394/**
2395 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2396 * Convienience function to add a list of controls.
2397 *
2398 * @dai: DAI to add controls to
2399 * @controls: array of controls to add
2400 * @num_controls: number of elements in the array
2401 *
2402 * Return 0 for success, else error.
2403 */
2404int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2405 const struct snd_kcontrol_new *controls, int num_controls)
2406{
2407 struct snd_card *card = dai->card->snd_card;
2408
2409 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2410 NULL, dai);
2411}
2412EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2413
2414/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415 * snd_soc_info_enum_double - enumerated double mixer info callback
2416 * @kcontrol: mixer control
2417 * @uinfo: control element information
2418 *
2419 * Callback to provide information about a double enumerated
2420 * mixer control.
2421 *
2422 * Returns 0 for success.
2423 */
2424int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_info *uinfo)
2426{
2427 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2428
2429 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2430 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002431 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002433 if (uinfo->value.enumerated.item >= e->items)
2434 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002435 strlcpy(uinfo->value.enumerated.name,
2436 e->texts[uinfo->value.enumerated.item],
2437 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002438 return 0;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2441
2442/**
2443 * snd_soc_get_enum_double - enumerated double mixer get callback
2444 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002445 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 *
2447 * Callback to get the value of a double enumerated mixer.
2448 *
2449 * Returns 0 for success.
2450 */
2451int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2453{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002454 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002456 unsigned int val, item;
2457 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002458 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002460 ret = snd_soc_component_read(component, e->reg, &reg_val);
2461 if (ret)
2462 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002463 val = (reg_val >> e->shift_l) & e->mask;
2464 item = snd_soc_enum_val_to_item(e, val);
2465 ucontrol->value.enumerated.item[0] = item;
2466 if (e->shift_l != e->shift_r) {
2467 val = (reg_val >> e->shift_l) & e->mask;
2468 item = snd_soc_enum_val_to_item(e, val);
2469 ucontrol->value.enumerated.item[1] = item;
2470 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471
2472 return 0;
2473}
2474EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2475
2476/**
2477 * snd_soc_put_enum_double - enumerated double mixer put callback
2478 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002479 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002480 *
2481 * Callback to set the value of a double enumerated mixer.
2482 *
2483 * Returns 0 for success.
2484 */
2485int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_value *ucontrol)
2487{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002488 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002490 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002491 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002492 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002494 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002495 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002496 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002497 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002498 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002499 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002500 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002501 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002502 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 }
2504
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002505 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506}
2507EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2508
2509/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002510 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002511 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002512 * @reg: Register to read
2513 * @mask: Mask to use after shifting the register value
2514 * @shift: Right shift of register value
2515 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002516 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002517 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002518 * This functions reads a codec register. The register value is shifted right
2519 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2520 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002521 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002522 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002523 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002524static int snd_soc_read_signed(struct snd_soc_component *component,
2525 unsigned int reg, unsigned int mask, unsigned int shift,
2526 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002527{
Markus Pargmannf227b882014-01-16 16:02:10 +01002528 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002529 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002530
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002531 ret = snd_soc_component_read(component, reg, &val);
2532 if (ret < 0)
2533 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002534
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002535 val = (val >> shift) & mask;
2536
2537 if (!sign_bit) {
2538 *signed_val = val;
2539 return 0;
2540 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002541
2542 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002543 if (!(val & BIT(sign_bit))) {
2544 *signed_val = val;
2545 return 0;
2546 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002547
2548 ret = val;
2549
2550 /*
2551 * The register most probably does not contain a full-sized int.
2552 * Instead we have an arbitrary number of bits in a signed
2553 * representation which has to be translated into a full-sized int.
2554 * This is done by filling up all bits above the sign-bit.
2555 */
2556 ret |= ~((int)(BIT(sign_bit) - 1));
2557
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002558 *signed_val = ret;
2559
2560 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002561}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002562
2563/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564 * snd_soc_info_volsw - single mixer info callback
2565 * @kcontrol: mixer control
2566 * @uinfo: control element information
2567 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002568 * Callback to provide information about a single mixer control, or a double
2569 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002570 *
2571 * Returns 0 for success.
2572 */
2573int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_info *uinfo)
2575{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002576 struct soc_mixer_control *mc =
2577 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002578 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002580 if (!mc->platform_max)
2581 mc->platform_max = mc->max;
2582 platform_max = mc->platform_max;
2583
2584 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002585 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2586 else
2587 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2588
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002589 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002591 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592 return 0;
2593}
2594EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2595
2596/**
2597 * snd_soc_get_volsw - single mixer get callback
2598 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002599 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002601 * Callback to get the value of a single mixer control, or a double mixer
2602 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 *
2604 * Returns 0 for success.
2605 */
2606int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
2608{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002609 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002610 struct soc_mixer_control *mc =
2611 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002612 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002613 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002614 unsigned int shift = mc->shift;
2615 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002616 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002617 int min = mc->min;
2618 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002619 unsigned int mask = (1 << fls(max)) - 1;
2620 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002621 int val;
2622 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623
Markus Pargmannf227b882014-01-16 16:02:10 +01002624 if (sign_bit)
2625 mask = BIT(sign_bit + 1) - 1;
2626
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002627 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2628 if (ret)
2629 return ret;
2630
2631 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002632 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002634 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002635
2636 if (snd_soc_volsw_is_stereo(mc)) {
2637 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002638 ret = snd_soc_read_signed(component, reg, mask, rshift,
2639 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002640 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002641 ret = snd_soc_read_signed(component, reg2, mask, shift,
2642 sign_bit, &val);
2643 if (ret)
2644 return ret;
2645
2646 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002647 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002649 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650 }
2651
2652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2655
2656/**
2657 * snd_soc_put_volsw - single mixer put callback
2658 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002659 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002660 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002661 * Callback to set the value of a single mixer control, or a double mixer
2662 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 *
2664 * Returns 0 for success.
2665 */
2666int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2667 struct snd_ctl_elem_value *ucontrol)
2668{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002669 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002670 struct soc_mixer_control *mc =
2671 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002672 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002673 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002674 unsigned int shift = mc->shift;
2675 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002676 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002677 int min = mc->min;
2678 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002679 unsigned int mask = (1 << fls(max)) - 1;
2680 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002681 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002682 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002683 unsigned int val2 = 0;
2684 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685
Markus Pargmannf227b882014-01-16 16:02:10 +01002686 if (sign_bit)
2687 mask = BIT(sign_bit + 1) - 1;
2688
2689 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002690 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002691 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692 val_mask = mask << shift;
2693 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002695 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002697 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002698 if (reg == reg2) {
2699 val_mask |= mask << rshift;
2700 val |= val2 << rshift;
2701 } else {
2702 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002703 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002706 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002707 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708 return err;
2709
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002710 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002711 err = snd_soc_component_update_bits(component, reg2, val_mask,
2712 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002713
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 return err;
2715}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002716EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717
Mark Browne13ac2e2008-05-28 17:58:05 +01002718/**
Brian Austin1d99f242012-03-30 10:43:55 -05002719 * snd_soc_get_volsw_sx - single mixer get callback
2720 * @kcontrol: mixer control
2721 * @ucontrol: control element information
2722 *
2723 * Callback to get the value of a single mixer control, or a double mixer
2724 * control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002732 struct soc_mixer_control *mc =
2733 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002734 unsigned int reg = mc->reg;
2735 unsigned int reg2 = mc->rreg;
2736 unsigned int shift = mc->shift;
2737 unsigned int rshift = mc->rshift;
2738 int max = mc->max;
2739 int min = mc->min;
2740 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002741 unsigned int val;
2742 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002743
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002744 ret = snd_soc_component_read(component, reg, &val);
2745 if (ret < 0)
2746 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002747
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002748 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2749
2750 if (snd_soc_volsw_is_stereo(mc)) {
2751 ret = snd_soc_component_read(component, reg2, &val);
2752 if (ret < 0)
2753 return ret;
2754
2755 val = ((val >> rshift) - min) & mask;
2756 ucontrol->value.integer.value[1] = val;
2757 }
Brian Austin1d99f242012-03-30 10:43:55 -05002758
2759 return 0;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2762
2763/**
2764 * snd_soc_put_volsw_sx - double mixer set callback
2765 * @kcontrol: mixer control
2766 * @uinfo: control element information
2767 *
2768 * Callback to set the value of a double mixer control that spans 2 registers.
2769 *
2770 * Returns 0 for success.
2771 */
2772int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_value *ucontrol)
2774{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002775 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
2778
2779 unsigned int reg = mc->reg;
2780 unsigned int reg2 = mc->rreg;
2781 unsigned int shift = mc->shift;
2782 unsigned int rshift = mc->rshift;
2783 int max = mc->max;
2784 int min = mc->min;
2785 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002786 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002787 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002788
2789 val_mask = mask << shift;
2790 val = (ucontrol->value.integer.value[0] + min) & mask;
2791 val = val << shift;
2792
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002793 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302794 if (err < 0)
2795 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002796
2797 if (snd_soc_volsw_is_stereo(mc)) {
2798 val_mask = mask << rshift;
2799 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2800 val2 = val2 << rshift;
2801
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002802 err = snd_soc_component_update_bits(component, reg2, val_mask,
2803 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002804 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002805 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002806}
2807EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2808
2809/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002810 * snd_soc_info_volsw_s8 - signed mixer info callback
2811 * @kcontrol: mixer control
2812 * @uinfo: control element information
2813 *
2814 * Callback to provide information about a signed mixer control.
2815 *
2816 * Returns 0 for success.
2817 */
2818int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2820{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002821 struct soc_mixer_control *mc =
2822 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002823 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002824 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002825
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002826 if (!mc->platform_max)
2827 mc->platform_max = mc->max;
2828 platform_max = mc->platform_max;
2829
Mark Browne13ac2e2008-05-28 17:58:05 +01002830 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2831 uinfo->count = 2;
2832 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002833 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2837
2838/**
2839 * snd_soc_get_volsw_s8 - signed mixer get callback
2840 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002841 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002842 *
2843 * Callback to get the value of a signed mixer control.
2844 *
2845 * Returns 0 for success.
2846 */
2847int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_value *ucontrol)
2849{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002850 struct soc_mixer_control *mc =
2851 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002852 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002853 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002854 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002855 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 int ret;
2857
2858 ret = snd_soc_component_read(component, reg, &val);
2859 if (ret)
2860 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002861
2862 ucontrol->value.integer.value[0] =
2863 ((signed char)(val & 0xff))-min;
2864 ucontrol->value.integer.value[1] =
2865 ((signed char)((val >> 8) & 0xff))-min;
2866 return 0;
2867}
2868EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2869
2870/**
2871 * snd_soc_put_volsw_sgn - signed mixer put callback
2872 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002873 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002874 *
2875 * Callback to set the value of a signed mixer control.
2876 *
2877 * Returns 0 for success.
2878 */
2879int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002882 struct soc_mixer_control *mc =
2883 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002884 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002885 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002886 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002887 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002888
2889 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2890 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2891
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002892 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002893}
2894EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2895
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002896/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002897 * snd_soc_info_volsw_range - single mixer info callback with range.
2898 * @kcontrol: mixer control
2899 * @uinfo: control element information
2900 *
2901 * Callback to provide information, within a range, about a single
2902 * mixer control.
2903 *
2904 * returns 0 for success.
2905 */
2906int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_info *uinfo)
2908{
2909 struct soc_mixer_control *mc =
2910 (struct soc_mixer_control *)kcontrol->private_value;
2911 int platform_max;
2912 int min = mc->min;
2913
2914 if (!mc->platform_max)
2915 mc->platform_max = mc->max;
2916 platform_max = mc->platform_max;
2917
2918 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002919 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002920 uinfo->value.integer.min = 0;
2921 uinfo->value.integer.max = platform_max - min;
2922
2923 return 0;
2924}
2925EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2926
2927/**
2928 * snd_soc_put_volsw_range - single mixer put value callback with range.
2929 * @kcontrol: mixer control
2930 * @ucontrol: control element information
2931 *
2932 * Callback to set the value, within a range, for a single mixer control.
2933 *
2934 * Returns 0 for success.
2935 */
2936int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
2938{
2939 struct soc_mixer_control *mc =
2940 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002941 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002942 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002943 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002944 unsigned int shift = mc->shift;
2945 int min = mc->min;
2946 int max = mc->max;
2947 unsigned int mask = (1 << fls(max)) - 1;
2948 unsigned int invert = mc->invert;
2949 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002950 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002951
2952 val = ((ucontrol->value.integer.value[0] + min) & mask);
2953 if (invert)
2954 val = max - val;
2955 val_mask = mask << shift;
2956 val = val << shift;
2957
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002958 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002959 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002960 return ret;
2961
2962 if (snd_soc_volsw_is_stereo(mc)) {
2963 val = ((ucontrol->value.integer.value[1] + min) & mask);
2964 if (invert)
2965 val = max - val;
2966 val_mask = mask << shift;
2967 val = val << shift;
2968
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002969 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2970 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002971 }
2972
2973 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002974}
2975EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2976
2977/**
2978 * snd_soc_get_volsw_range - single mixer get callback with range
2979 * @kcontrol: mixer control
2980 * @ucontrol: control element information
2981 *
2982 * Callback to get the value, within a range, of a single mixer control.
2983 *
2984 * Returns 0 for success.
2985 */
2986int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2987 struct snd_ctl_elem_value *ucontrol)
2988{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002989 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002990 struct soc_mixer_control *mc =
2991 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002992 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002993 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994 unsigned int shift = mc->shift;
2995 int min = mc->min;
2996 int max = mc->max;
2997 unsigned int mask = (1 << fls(max)) - 1;
2998 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002999 unsigned int val;
3000 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003001
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003002 ret = snd_soc_component_read(component, reg, &val);
3003 if (ret)
3004 return ret;
3005
3006 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003007 if (invert)
3008 ucontrol->value.integer.value[0] =
3009 max - ucontrol->value.integer.value[0];
3010 ucontrol->value.integer.value[0] =
3011 ucontrol->value.integer.value[0] - min;
3012
Mark Brown9bde4f02012-12-19 16:05:00 +00003013 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003014 ret = snd_soc_component_read(component, rreg, &val);
3015 if (ret)
3016 return ret;
3017
3018 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003019 if (invert)
3020 ucontrol->value.integer.value[1] =
3021 max - ucontrol->value.integer.value[1];
3022 ucontrol->value.integer.value[1] =
3023 ucontrol->value.integer.value[1] - min;
3024 }
3025
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003026 return 0;
3027}
3028EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3029
3030/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003031 * snd_soc_limit_volume - Set new limit to an existing volume control.
3032 *
3033 * @codec: where to look for the control
3034 * @name: Name of the control
3035 * @max: new maximum limit
3036 *
3037 * Return 0 for success, else error.
3038 */
3039int snd_soc_limit_volume(struct snd_soc_codec *codec,
3040 const char *name, int max)
3041{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003042 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003043 struct snd_kcontrol *kctl;
3044 struct soc_mixer_control *mc;
3045 int found = 0;
3046 int ret = -EINVAL;
3047
3048 /* Sanity check for name and max */
3049 if (unlikely(!name || max <= 0))
3050 return -EINVAL;
3051
3052 list_for_each_entry(kctl, &card->controls, list) {
3053 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3054 found = 1;
3055 break;
3056 }
3057 }
3058 if (found) {
3059 mc = (struct soc_mixer_control *)kctl->private_value;
3060 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003061 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003062 ret = 0;
3063 }
3064 }
3065 return ret;
3066}
3067EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3068
Mark Brown71d08512011-10-10 18:31:26 +01003069int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_info *uinfo)
3071{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003072 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003073 struct soc_bytes *params = (void *)kcontrol->private_value;
3074
3075 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003076 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003077
3078 return 0;
3079}
3080EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3081
3082int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3084{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003085 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003086 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003087 int ret;
3088
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003089 if (component->regmap)
3090 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003091 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003092 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003093 else
3094 ret = -EINVAL;
3095
Mark Brownf831b052012-02-17 16:20:33 -08003096 /* Hide any masked bytes to ensure consistent data reporting */
3097 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003098 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003099 case 1:
3100 ucontrol->value.bytes.data[0] &= ~params->mask;
3101 break;
3102 case 2:
3103 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003104 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003105 break;
3106 case 4:
3107 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003108 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003109 break;
3110 default:
3111 return -EINVAL;
3112 }
3113 }
3114
Mark Brown71d08512011-10-10 18:31:26 +01003115 return ret;
3116}
3117EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3118
3119int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003122 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003123 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003124 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003125 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003126 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003127
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003128 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003129 return -EINVAL;
3130
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003131 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003132
Mark Brownb5a8fe42013-01-20 21:42:22 +09003133 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3134 if (!data)
3135 return -ENOMEM;
3136
Mark Brownf831b052012-02-17 16:20:33 -08003137 /*
3138 * If we've got a mask then we need to preserve the register
3139 * bits. We shouldn't modify the incoming data so take a
3140 * copy.
3141 */
3142 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003143 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003144 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003145 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003146
3147 val &= params->mask;
3148
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003149 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003150 case 1:
3151 ((u8 *)data)[0] &= ~params->mask;
3152 ((u8 *)data)[0] |= val;
3153 break;
3154 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003155 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003156 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003157 &mask, &mask);
3158 if (ret != 0)
3159 goto out;
3160
3161 ((u16 *)data)[0] &= mask;
3162
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003163 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003164 &val, &val);
3165 if (ret != 0)
3166 goto out;
3167
3168 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003169 break;
3170 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003171 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003172 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003173 &mask, &mask);
3174 if (ret != 0)
3175 goto out;
3176
3177 ((u32 *)data)[0] &= mask;
3178
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003179 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003180 &val, &val);
3181 if (ret != 0)
3182 goto out;
3183
3184 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003185 break;
3186 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003187 ret = -EINVAL;
3188 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003189 }
3190 }
3191
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003192 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003193 data, len);
3194
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003195out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003196 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003197
3198 return ret;
3199}
3200EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3201
Vinod Kould9881202014-05-02 10:58:11 +05303202int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_info *ucontrol)
3204{
3205 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3206
3207 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3208 ucontrol->count = params->max;
3209
3210 return 0;
3211}
3212EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3213
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303214int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3215 unsigned int size, unsigned int __user *tlv)
3216{
3217 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3218 unsigned int count = size < params->max ? size : params->max;
3219 int ret = -ENXIO;
3220
3221 switch (op_flag) {
3222 case SNDRV_CTL_TLV_OP_READ:
3223 if (params->get)
3224 ret = params->get(tlv, count);
3225 break;
3226 case SNDRV_CTL_TLV_OP_WRITE:
3227 if (params->put)
3228 ret = params->put(tlv, count);
3229 break;
3230 }
3231 return ret;
3232}
3233EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3234
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003235/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003236 * snd_soc_info_xr_sx - signed multi register info callback
3237 * @kcontrol: mreg control
3238 * @uinfo: control element information
3239 *
3240 * Callback to provide information of a control that can
3241 * span multiple codec registers which together
3242 * forms a single signed value in a MSB/LSB manner.
3243 *
3244 * Returns 0 for success.
3245 */
3246int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_info *uinfo)
3248{
3249 struct soc_mreg_control *mc =
3250 (struct soc_mreg_control *)kcontrol->private_value;
3251 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3252 uinfo->count = 1;
3253 uinfo->value.integer.min = mc->min;
3254 uinfo->value.integer.max = mc->max;
3255
3256 return 0;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3259
3260/**
3261 * snd_soc_get_xr_sx - signed multi register get callback
3262 * @kcontrol: mreg control
3263 * @ucontrol: control element information
3264 *
3265 * Callback to get the value of a control that can span
3266 * multiple codec registers which together forms a single
3267 * signed value in a MSB/LSB manner. The control supports
3268 * specifying total no of bits used to allow for bitfields
3269 * across the multiple codec registers.
3270 *
3271 * Returns 0 for success.
3272 */
3273int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3275{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003276 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003277 struct soc_mreg_control *mc =
3278 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003279 unsigned int regbase = mc->regbase;
3280 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003281 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003282 unsigned int regwmask = (1<<regwshift)-1;
3283 unsigned int invert = mc->invert;
3284 unsigned long mask = (1UL<<mc->nbits)-1;
3285 long min = mc->min;
3286 long max = mc->max;
3287 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003288 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003289 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003290 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003291
3292 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003293 ret = snd_soc_component_read(component, regbase+i, &regval);
3294 if (ret)
3295 return ret;
3296 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003297 }
3298 val &= mask;
3299 if (min < 0 && val > max)
3300 val |= ~mask;
3301 if (invert)
3302 val = max - val;
3303 ucontrol->value.integer.value[0] = val;
3304
3305 return 0;
3306}
3307EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3308
3309/**
3310 * snd_soc_put_xr_sx - signed multi register get callback
3311 * @kcontrol: mreg control
3312 * @ucontrol: control element information
3313 *
3314 * Callback to set the value of a control that can span
3315 * multiple codec registers which together forms a single
3316 * signed value in a MSB/LSB manner. The control supports
3317 * specifying total no of bits used to allow for bitfields
3318 * across the multiple codec registers.
3319 *
3320 * Returns 0 for success.
3321 */
3322int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3323 struct snd_ctl_elem_value *ucontrol)
3324{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003325 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003326 struct soc_mreg_control *mc =
3327 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003328 unsigned int regbase = mc->regbase;
3329 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003330 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003331 unsigned int regwmask = (1<<regwshift)-1;
3332 unsigned int invert = mc->invert;
3333 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003334 long max = mc->max;
3335 long val = ucontrol->value.integer.value[0];
3336 unsigned int i, regval, regmask;
3337 int err;
3338
3339 if (invert)
3340 val = max - val;
3341 val &= mask;
3342 for (i = 0; i < regcount; i++) {
3343 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3344 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003345 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003346 regmask, regval);
3347 if (err < 0)
3348 return err;
3349 }
3350
3351 return 0;
3352}
3353EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3354
3355/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003356 * snd_soc_get_strobe - strobe get callback
3357 * @kcontrol: mixer control
3358 * @ucontrol: control element information
3359 *
3360 * Callback get the value of a strobe mixer control.
3361 *
3362 * Returns 0 for success.
3363 */
3364int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3366{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003367 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003368 struct soc_mixer_control *mc =
3369 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003370 unsigned int reg = mc->reg;
3371 unsigned int shift = mc->shift;
3372 unsigned int mask = 1 << shift;
3373 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003374 unsigned int val;
3375 int ret;
3376
3377 ret = snd_soc_component_read(component, reg, &val);
3378 if (ret)
3379 return ret;
3380
3381 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003382
3383 if (shift != 0 && val != 0)
3384 val = val >> shift;
3385 ucontrol->value.enumerated.item[0] = val ^ invert;
3386
3387 return 0;
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3390
3391/**
3392 * snd_soc_put_strobe - strobe put callback
3393 * @kcontrol: mixer control
3394 * @ucontrol: control element information
3395 *
3396 * Callback strobe a register bit to high then low (or the inverse)
3397 * in one pass of a single mixer enum control.
3398 *
3399 * Returns 1 for success.
3400 */
3401int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3403{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003404 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003405 struct soc_mixer_control *mc =
3406 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003407 unsigned int reg = mc->reg;
3408 unsigned int shift = mc->shift;
3409 unsigned int mask = 1 << shift;
3410 unsigned int invert = mc->invert != 0;
3411 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3412 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3413 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3414 int err;
3415
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003416 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003417 if (err < 0)
3418 return err;
3419
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003420 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003421}
3422EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3423
3424/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003425 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3426 * @dai: DAI
3427 * @clk_id: DAI specific clock ID
3428 * @freq: new clock frequency in Hz
3429 * @dir: new clock direction - input/output.
3430 *
3431 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3432 */
3433int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3434 unsigned int freq, int dir)
3435{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003436 if (dai->driver && dai->driver->ops->set_sysclk)
3437 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003438 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003439 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003440 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003441 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003442 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003443}
3444EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3445
3446/**
Mark Brownec4ee522011-03-07 20:58:11 +00003447 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3448 * @codec: CODEC
3449 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003450 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003451 * @freq: new clock frequency in Hz
3452 * @dir: new clock direction - input/output.
3453 *
3454 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3455 */
3456int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003457 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003458{
3459 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003460 return codec->driver->set_sysclk(codec, clk_id, source,
3461 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003462 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003463 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003464}
3465EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3466
3467/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3469 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003470 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003471 * @div: new clock divisor.
3472 *
3473 * Configures the clock dividers. This is used to derive the best DAI bit and
3474 * frame clocks from the system or master clock. It's best to set the DAI bit
3475 * and frame clocks as low as possible to save system power.
3476 */
3477int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3478 int div_id, int div)
3479{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480 if (dai->driver && dai->driver->ops->set_clkdiv)
3481 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 else
3483 return -EINVAL;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3486
3487/**
3488 * snd_soc_dai_set_pll - configure DAI PLL.
3489 * @dai: DAI
3490 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003491 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 * @freq_in: PLL input clock frequency in Hz
3493 * @freq_out: requested PLL output clock frequency in Hz
3494 *
3495 * Configures and enables PLL to generate output clock based on input clock.
3496 */
Mark Brown85488032009-09-05 18:52:16 +01003497int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3498 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003499{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 if (dai->driver && dai->driver->ops->set_pll)
3501 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003502 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003503 else if (dai->codec && dai->codec->driver->set_pll)
3504 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3505 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506 else
3507 return -EINVAL;
3508}
3509EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3510
Mark Brownec4ee522011-03-07 20:58:11 +00003511/*
3512 * snd_soc_codec_set_pll - configure codec PLL.
3513 * @codec: CODEC
3514 * @pll_id: DAI specific PLL ID
3515 * @source: DAI specific source for the PLL
3516 * @freq_in: PLL input clock frequency in Hz
3517 * @freq_out: requested PLL output clock frequency in Hz
3518 *
3519 * Configures and enables PLL to generate output clock based on input clock.
3520 */
3521int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3522 unsigned int freq_in, unsigned int freq_out)
3523{
3524 if (codec->driver->set_pll)
3525 return codec->driver->set_pll(codec, pll_id, source,
3526 freq_in, freq_out);
3527 else
3528 return -EINVAL;
3529}
3530EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3531
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003532/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003533 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3534 * @dai: DAI
3535 * @ratio Ratio of BCLK to Sample rate.
3536 *
3537 * Configures the DAI for a preset BCLK to sample rate ratio.
3538 */
3539int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3540{
3541 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3542 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3543 else
3544 return -EINVAL;
3545}
3546EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3547
3548/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003549 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3550 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003551 * @fmt: SND_SOC_DAIFMT_ format value.
3552 *
3553 * Configures the DAI hardware format and clocking.
3554 */
3555int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3556{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003557 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003558 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003559 if (dai->driver->ops->set_fmt == NULL)
3560 return -ENOTSUPP;
3561 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562}
3563EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3564
3565/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003566 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003567 * @slots: Number of slots in use.
3568 * @tx_mask: bitmask representing active TX slots.
3569 * @rx_mask: bitmask representing active RX slots.
3570 *
3571 * Generates the TDM tx and rx slot default masks for DAI.
3572 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003573static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003574 unsigned int *tx_mask,
3575 unsigned int *rx_mask)
3576{
3577 if (*tx_mask || *rx_mask)
3578 return 0;
3579
3580 if (!slots)
3581 return -EINVAL;
3582
3583 *tx_mask = (1 << slots) - 1;
3584 *rx_mask = (1 << slots) - 1;
3585
3586 return 0;
3587}
3588
3589/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003590 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3591 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003592 * @tx_mask: bitmask representing active TX slots.
3593 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003594 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003595 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003596 *
3597 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3598 * specific.
3599 */
3600int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003601 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003602{
Xiubo Lie5c21512014-03-21 14:17:12 +08003603 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3604 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003605 &tx_mask, &rx_mask);
3606 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003607 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003608
Benoit Cousson88bd8702014-07-08 23:19:34 +02003609 dai->tx_mask = tx_mask;
3610 dai->rx_mask = rx_mask;
3611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003612 if (dai->driver && dai->driver->ops->set_tdm_slot)
3613 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003614 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003615 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003616 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003617}
3618EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3619
3620/**
Barry Song472df3c2009-09-12 01:16:29 +08003621 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3622 * @dai: DAI
3623 * @tx_num: how many TX channels
3624 * @tx_slot: pointer to an array which imply the TX slot number channel
3625 * 0~num-1 uses
3626 * @rx_num: how many RX channels
3627 * @rx_slot: pointer to an array which imply the RX slot number channel
3628 * 0~num-1 uses
3629 *
3630 * configure the relationship between channel number and TDM slot number.
3631 */
3632int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3633 unsigned int tx_num, unsigned int *tx_slot,
3634 unsigned int rx_num, unsigned int *rx_slot)
3635{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003636 if (dai->driver && dai->driver->ops->set_channel_map)
3637 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003638 rx_num, rx_slot);
3639 else
3640 return -EINVAL;
3641}
3642EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3643
3644/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003645 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3646 * @dai: DAI
3647 * @tristate: tristate enable
3648 *
3649 * Tristates the DAI so that others can use it.
3650 */
3651int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3652{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003653 if (dai->driver && dai->driver->ops->set_tristate)
3654 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003655 else
3656 return -EINVAL;
3657}
3658EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3659
3660/**
3661 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3662 * @dai: DAI
3663 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003664 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665 *
3666 * Mutes the DAI DAC.
3667 */
Mark Brownda183962013-02-06 15:44:07 +00003668int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3669 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003670{
Mark Brownda183962013-02-06 15:44:07 +00003671 if (!dai->driver)
3672 return -ENOTSUPP;
3673
3674 if (dai->driver->ops->mute_stream)
3675 return dai->driver->ops->mute_stream(dai, mute, direction);
3676 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3677 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003679 else
Mark Brown04570c62012-04-13 19:16:03 +01003680 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003681}
3682EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3683
Benoit Cousson88bd8702014-07-08 23:19:34 +02003684static int snd_soc_init_multicodec(struct snd_soc_card *card,
3685 struct snd_soc_dai_link *dai_link)
3686{
3687 /* Legacy codec/codec_dai link is a single entry in multicodec */
3688 if (dai_link->codec_name || dai_link->codec_of_node ||
3689 dai_link->codec_dai_name) {
3690 dai_link->num_codecs = 1;
3691
3692 dai_link->codecs = devm_kzalloc(card->dev,
3693 sizeof(struct snd_soc_dai_link_component),
3694 GFP_KERNEL);
3695 if (!dai_link->codecs)
3696 return -ENOMEM;
3697
3698 dai_link->codecs[0].name = dai_link->codec_name;
3699 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3700 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3701 }
3702
3703 if (!dai_link->codecs) {
3704 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3705 return -EINVAL;
3706 }
3707
3708 return 0;
3709}
3710
Mark Brownc5af3a22008-11-28 13:29:45 +00003711/**
3712 * snd_soc_register_card - Register a card with the ASoC core
3713 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003714 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003715 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003716 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303717int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003718{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003719 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003720
Mark Brownc5af3a22008-11-28 13:29:45 +00003721 if (!card->name || !card->dev)
3722 return -EINVAL;
3723
Stephen Warren5a504962011-12-21 10:40:59 -07003724 for (i = 0; i < card->num_links; i++) {
3725 struct snd_soc_dai_link *link = &card->dai_link[i];
3726
Benoit Cousson88bd8702014-07-08 23:19:34 +02003727 ret = snd_soc_init_multicodec(card, link);
3728 if (ret) {
3729 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3730 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003731 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003732
3733 for (j = 0; j < link->num_codecs; j++) {
3734 /*
3735 * Codec must be specified by 1 of name or OF node,
3736 * not both or neither.
3737 */
3738 if (!!link->codecs[j].name ==
3739 !!link->codecs[j].of_node) {
3740 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3741 link->name);
3742 return -EINVAL;
3743 }
3744 /* Codec DAI name must be specified */
3745 if (!link->codecs[j].dai_name) {
3746 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3747 link->name);
3748 return -EINVAL;
3749 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003750 }
Stephen Warren5a504962011-12-21 10:40:59 -07003751
3752 /*
3753 * Platform may be specified by either name or OF node, but
3754 * can be left unspecified, and a dummy platform will be used.
3755 */
3756 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003757 dev_err(card->dev,
3758 "ASoC: Both platform name/of_node are set for %s\n",
3759 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003760 return -EINVAL;
3761 }
3762
3763 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003764 * CPU device may be specified by either name or OF node, but
3765 * can be left unspecified, and will be matched based on DAI
3766 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003767 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003768 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003769 dev_err(card->dev,
3770 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3771 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003772 return -EINVAL;
3773 }
3774 /*
3775 * At least one of CPU DAI name or CPU device name/node must be
3776 * specified
3777 */
3778 if (!link->cpu_dai_name &&
3779 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003780 dev_err(card->dev,
3781 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3782 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003783 return -EINVAL;
3784 }
3785 }
3786
Mark Browned77cc12011-05-03 18:25:34 +01003787 dev_set_drvdata(card->dev, card);
3788
Stephen Warren111c6412011-01-28 14:26:35 -07003789 snd_soc_initialize_card_lists(card);
3790
Vinod Koul150dd2f2011-01-13 22:48:32 +05303791 soc_init_card_debugfs(card);
3792
Mark Brown181a6892012-03-14 20:18:49 +00003793 card->rtd = devm_kzalloc(card->dev,
3794 sizeof(struct snd_soc_pcm_runtime) *
3795 (card->num_links + card->num_aux_devs),
3796 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797 if (card->rtd == NULL)
3798 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003799 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003800 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003801
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003802 for (i = 0; i < card->num_links; i++) {
3803 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003804 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003805 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3806 sizeof(struct snd_soc_dai *) *
3807 (card->rtd[i].dai_link->num_codecs),
3808 GFP_KERNEL);
3809 if (card->rtd[i].codec_dais == NULL)
3810 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003811 }
3812
3813 for (i = 0; i < card->num_aux_devs; i++)
3814 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815
Mark Browndb432b42011-10-03 21:06:40 +01003816 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003817 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003818 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003819 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003820
Mark Brownb19e6e72012-03-14 21:18:39 +00003821 ret = snd_soc_instantiate_card(card);
3822 if (ret != 0)
3823 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003824
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003825 /* deactivate pins to sleep state */
3826 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003827 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3828 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3829 int j;
3830
3831 for (j = 0; j < rtd->num_codecs; j++) {
3832 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3833 if (!codec_dai->active)
3834 pinctrl_pm_select_sleep_state(codec_dai->dev);
3835 }
3836
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003837 if (!cpu_dai->active)
3838 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3839 }
3840
Mark Brownb19e6e72012-03-14 21:18:39 +00003841 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003842}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303843EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003844
3845/**
3846 * snd_soc_unregister_card - Unregister a card with the ASoC core
3847 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003848 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003849 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003850 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303851int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003852{
Vinod Koulb0e26482011-01-13 22:48:02 +05303853 if (card->instantiated)
3854 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003855 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003856
3857 return 0;
3858}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303859EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003860
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003861/*
3862 * Simplify DAI link configuration by removing ".-1" from device names
3863 * and sanitizing names.
3864 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003865static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003866{
3867 char *found, name[NAME_SIZE];
3868 int id1, id2;
3869
3870 if (dev_name(dev) == NULL)
3871 return NULL;
3872
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003873 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003874
3875 /* are we a "%s.%d" name (platform and SPI components) */
3876 found = strstr(name, dev->driver->name);
3877 if (found) {
3878 /* get ID */
3879 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3880
3881 /* discard ID from name if ID == -1 */
3882 if (*id == -1)
3883 found[strlen(dev->driver->name)] = '\0';
3884 }
3885
3886 } else {
3887 /* I2C component devices are named "bus-addr" */
3888 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3889 char tmp[NAME_SIZE];
3890
3891 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003892 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003893
3894 /* sanitize component name for DAI link creation */
3895 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003896 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897 } else
3898 *id = 0;
3899 }
3900
3901 return kstrdup(name, GFP_KERNEL);
3902}
3903
3904/*
3905 * Simplify DAI link naming for single devices with multiple DAIs by removing
3906 * any ".-1" and using the DAI name (instead of device name).
3907 */
3908static inline char *fmt_multiple_name(struct device *dev,
3909 struct snd_soc_dai_driver *dai_drv)
3910{
3911 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003912 dev_err(dev,
3913 "ASoC: error - multiple DAI %s registered with no name\n",
3914 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915 return NULL;
3916 }
3917
3918 return kstrdup(dai_drv->name, GFP_KERNEL);
3919}
3920
Mark Brown91151712008-11-30 23:31:24 +00003921/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003922 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003923 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003924 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003925 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003926static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003927{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003928 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003929
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003930 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003931 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3932 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003933 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003934 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003935 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003936 }
Mark Brown91151712008-11-30 23:31:24 +00003937}
Mark Brown91151712008-11-30 23:31:24 +00003938
3939/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003940 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003941 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003942 * @component: The component the DAIs are registered for
3943 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003944 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003945 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3946 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003947 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003948static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003949 struct snd_soc_dai_driver *dai_drv, size_t count,
3950 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003951{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003952 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003954 unsigned int i;
3955 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003957 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003958
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003959 component->dai_drv = dai_drv;
3960 component->num_dai = count;
3961
Mark Brown91151712008-11-30 23:31:24 +00003962 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003963
3964 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003965 if (dai == NULL) {
3966 ret = -ENOMEM;
3967 goto err;
3968 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003969
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003970 /*
3971 * Back in the old days when we still had component-less DAIs,
3972 * instead of having a static name, component-less DAIs would
3973 * inherit the name of the parent device so it is possible to
3974 * register multiple instances of the DAI. We still need to keep
3975 * the same naming style even though those DAIs are not
3976 * component-less anymore.
3977 */
3978 if (count == 1 && legacy_dai_naming) {
3979 dai->name = fmt_single_name(dev, &dai->id);
3980 } else {
3981 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3982 if (dai_drv[i].id)
3983 dai->id = dai_drv[i].id;
3984 else
3985 dai->id = i;
3986 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003987 if (dai->name == NULL) {
3988 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003989 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003990 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003991 }
3992
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003993 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003995 dai->driver = &dai_drv[i];
3996 if (!dai->driver->ops)
3997 dai->driver->ops = &null_dai_ops;
3998
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003999 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004000
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004001 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004002 }
4003
4004 return 0;
4005
4006err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004007 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004008
4009 return ret;
4010}
Mark Brown91151712008-11-30 23:31:24 +00004011
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004012static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4013 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004014{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004015 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004016
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004017 component->driver->seq_notifier(component, type, subseq);
4018}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004019
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004020static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4021 int event)
4022{
4023 struct snd_soc_component *component = dapm->component;
4024
4025 return component->driver->stream_event(component, event);
4026}
4027
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004028static int snd_soc_component_initialize(struct snd_soc_component *component,
4029 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004030{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004031 struct snd_soc_dapm_context *dapm;
4032
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004033 component->name = fmt_single_name(dev, &component->id);
4034 if (!component->name) {
4035 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004036 return -ENOMEM;
4037 }
4038
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004039 component->dev = dev;
4040 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004041
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004042 if (!component->dapm_ptr)
4043 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004044
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004045 dapm = component->dapm_ptr;
4046 dapm->dev = dev;
4047 dapm->component = component;
4048 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004049 if (driver->seq_notifier)
4050 dapm->seq_notifier = snd_soc_component_seq_notifier;
4051 if (driver->stream_event)
4052 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004053
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004054 INIT_LIST_HEAD(&component->dai_list);
4055 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004056
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004057 return 0;
4058}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004059
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004060static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4061{
4062 list_add(&component->list, &component_list);
4063}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004064
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004065static void snd_soc_component_add(struct snd_soc_component *component)
4066{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004067 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004068 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004069 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004070}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004072static void snd_soc_component_cleanup(struct snd_soc_component *component)
4073{
4074 snd_soc_unregister_dais(component);
4075 kfree(component->name);
4076}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004077
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004078static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4079{
4080 list_del(&component->list);
4081}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004082
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004083static void snd_soc_component_del(struct snd_soc_component *component)
4084{
4085 mutex_lock(&client_mutex);
4086 snd_soc_component_del_unlocked(component);
4087 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004088}
4089
4090int snd_soc_register_component(struct device *dev,
4091 const struct snd_soc_component_driver *cmpnt_drv,
4092 struct snd_soc_dai_driver *dai_drv,
4093 int num_dai)
4094{
4095 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004096 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004098 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004099 if (!cmpnt) {
4100 dev_err(dev, "ASoC: Failed to allocate memory\n");
4101 return -ENOMEM;
4102 }
4103
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004104 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4105 if (ret)
4106 goto err_free;
4107
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004108 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004109 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004110
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004111 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4112 if (ret < 0) {
4113 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4114 goto err_cleanup;
4115 }
4116
4117 snd_soc_component_add(cmpnt);
4118
4119 return 0;
4120
4121err_cleanup:
4122 snd_soc_component_cleanup(cmpnt);
4123err_free:
4124 kfree(cmpnt);
4125 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004126}
4127EXPORT_SYMBOL_GPL(snd_soc_register_component);
4128
4129/**
4130 * snd_soc_unregister_component - Unregister a component from the ASoC core
4131 *
4132 */
4133void snd_soc_unregister_component(struct device *dev)
4134{
4135 struct snd_soc_component *cmpnt;
4136
4137 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004138 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004139 goto found;
4140 }
4141 return;
4142
4143found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004144 snd_soc_component_del(cmpnt);
4145 snd_soc_component_cleanup(cmpnt);
4146 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004147}
4148EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4149
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004150static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4151{
4152 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4153
4154 return platform->driver->probe(platform);
4155}
4156
4157static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4158{
4159 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4160
4161 platform->driver->remove(platform);
4162}
4163
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004164static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4165 unsigned int reg, unsigned int val)
4166{
4167 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4168
4169 return platform->driver->write(platform, reg, val);
4170}
4171
4172static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4173 unsigned int reg, unsigned int *val)
4174{
4175 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4176
4177 *val = platform->driver->read(platform, reg);
4178
4179 return 0;
4180}
4181
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004182/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004183 * snd_soc_add_platform - Add a platform to the ASoC core
4184 * @dev: The parent device for the platform
4185 * @platform: The platform to add
4186 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004187 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004188int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004189 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004190{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004191 int ret;
4192
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004193 ret = snd_soc_component_initialize(&platform->component,
4194 &platform_drv->component_driver, dev);
4195 if (ret)
4196 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004197
4198 platform->dev = dev;
4199 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004200 if (platform_drv->controls) {
4201 platform->component.controls = platform_drv->controls;
4202 platform->component.num_controls = platform_drv->num_controls;
4203 }
4204 if (platform_drv->dapm_widgets) {
4205 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4206 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4207 platform->component.steal_sibling_dai_widgets = true;
4208 }
4209 if (platform_drv->dapm_routes) {
4210 platform->component.dapm_routes = platform_drv->dapm_routes;
4211 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4212 }
4213
4214 if (platform_drv->probe)
4215 platform->component.probe = snd_soc_platform_drv_probe;
4216 if (platform_drv->remove)
4217 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004218 if (platform_drv->write)
4219 platform->component.write = snd_soc_platform_drv_write;
4220 if (platform_drv->read)
4221 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004222
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004223#ifdef CONFIG_DEBUG_FS
4224 platform->component.debugfs_prefix = "platform";
4225#endif
4226
Mark Brown12a48a8c2008-12-03 19:40:30 +00004227 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004228 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004229 list_add(&platform->list, &platform_list);
4230 mutex_unlock(&client_mutex);
4231
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004232 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4233 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004234
4235 return 0;
4236}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004237EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4238
4239/**
4240 * snd_soc_register_platform - Register a platform with the ASoC core
4241 *
4242 * @platform: platform to register
4243 */
4244int snd_soc_register_platform(struct device *dev,
4245 const struct snd_soc_platform_driver *platform_drv)
4246{
4247 struct snd_soc_platform *platform;
4248 int ret;
4249
4250 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4251
4252 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4253 if (platform == NULL)
4254 return -ENOMEM;
4255
4256 ret = snd_soc_add_platform(dev, platform, platform_drv);
4257 if (ret)
4258 kfree(platform);
4259
4260 return ret;
4261}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004262EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4263
4264/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004265 * snd_soc_remove_platform - Remove a platform from the ASoC core
4266 * @platform: the platform to remove
4267 */
4268void snd_soc_remove_platform(struct snd_soc_platform *platform)
4269{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004270
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004271 mutex_lock(&client_mutex);
4272 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004273 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004274 mutex_unlock(&client_mutex);
4275
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004276 snd_soc_component_cleanup(&platform->component);
4277
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004278 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004279 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004280}
4281EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4282
4283struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4284{
4285 struct snd_soc_platform *platform;
4286
4287 list_for_each_entry(platform, &platform_list, list) {
4288 if (dev == platform->dev)
4289 return platform;
4290 }
4291
4292 return NULL;
4293}
4294EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4295
4296/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004297 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4298 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004299 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004300 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004301void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004302{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004303 struct snd_soc_platform *platform;
4304
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004305 platform = snd_soc_lookup_platform(dev);
4306 if (!platform)
4307 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004308
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004309 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004310 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004311}
4312EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4313
Mark Brown151ab222009-05-09 16:22:58 +01004314static u64 codec_format_map[] = {
4315 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4316 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4317 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4318 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4319 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4320 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4321 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4322 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4323 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4324 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4325 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4326 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4327 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4328 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4329 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4330 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4331};
4332
4333/* Fix up the DAI formats for endianness: codecs don't actually see
4334 * the endianness of the data but we're using the CPU format
4335 * definitions which do need to include endianness so we ensure that
4336 * codec DAIs always have both big and little endian variants set.
4337 */
4338static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4339{
4340 int i;
4341
4342 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4343 if (stream->formats & codec_format_map[i])
4344 stream->formats |= codec_format_map[i];
4345}
4346
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004347static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4348{
4349 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4350
4351 return codec->driver->probe(codec);
4352}
4353
4354static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4355{
4356 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4357
4358 codec->driver->remove(codec);
4359}
4360
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004361static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4362 unsigned int reg, unsigned int val)
4363{
4364 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4365
4366 return codec->driver->write(codec, reg, val);
4367}
4368
4369static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4370 unsigned int reg, unsigned int *val)
4371{
4372 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4373
4374 *val = codec->driver->read(codec, reg);
4375
4376 return 0;
4377}
4378
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004379static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4380 enum snd_soc_bias_level level)
4381{
4382 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4383
4384 return codec->driver->set_bias_level(codec, level);
4385}
4386
Mark Brown0d0cf002008-12-10 14:32:45 +00004387/**
4388 * snd_soc_register_codec - Register a codec with the ASoC core
4389 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004390 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004391 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004392int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004393 const struct snd_soc_codec_driver *codec_drv,
4394 struct snd_soc_dai_driver *dai_drv,
4395 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004396{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004397 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004398 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004399 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004400 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004401
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004402 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004403
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004404 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4405 if (codec == NULL)
4406 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004407
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004408 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004409 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004410
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004411 ret = snd_soc_component_initialize(&codec->component,
4412 &codec_drv->component_driver, dev);
4413 if (ret)
4414 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004415
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004416 if (codec_drv->controls) {
4417 codec->component.controls = codec_drv->controls;
4418 codec->component.num_controls = codec_drv->num_controls;
4419 }
4420 if (codec_drv->dapm_widgets) {
4421 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4422 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4423 }
4424 if (codec_drv->dapm_routes) {
4425 codec->component.dapm_routes = codec_drv->dapm_routes;
4426 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4427 }
4428
4429 if (codec_drv->probe)
4430 codec->component.probe = snd_soc_codec_drv_probe;
4431 if (codec_drv->remove)
4432 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004433 if (codec_drv->write)
4434 codec->component.write = snd_soc_codec_drv_write;
4435 if (codec_drv->read)
4436 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004437 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004438 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004439 if (codec_drv->seq_notifier)
4440 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004441 if (codec_drv->set_bias_level)
4442 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004443 codec->dev = dev;
4444 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004445 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004446 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004447
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004448#ifdef CONFIG_DEBUG_FS
4449 codec->component.init_debugfs = soc_init_codec_debugfs;
4450 codec->component.debugfs_prefix = "codec";
4451#endif
4452
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004453 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004454 if (codec_drv->get_regmap)
4455 regmap = codec_drv->get_regmap(dev);
4456 else
4457 regmap = dev_get_regmap(dev, NULL);
4458
4459 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004460 ret = snd_soc_component_init_io(&codec->component,
4461 regmap);
4462 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004463 dev_err(codec->dev,
4464 "Failed to set cache I/O:%d\n",
4465 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004466 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004467 }
4468 }
4469 }
4470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004471 for (i = 0; i < num_dai; i++) {
4472 fixup_codec_formats(&dai_drv[i].playback);
4473 fixup_codec_formats(&dai_drv[i].capture);
4474 }
4475
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004476 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4477 if (ret < 0) {
4478 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4479 goto err_cleanup;
4480 }
4481
4482 list_for_each_entry(dai, &codec->component.dai_list, list)
4483 dai->codec = codec;
4484
Mark Brown054880f2012-04-09 17:29:19 +01004485 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004486 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004487 list_add(&codec->list, &codec_list);
4488 mutex_unlock(&client_mutex);
4489
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004490 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4491 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004492 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004493
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004494err_cleanup:
4495 snd_soc_component_cleanup(&codec->component);
4496err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004497 kfree(codec);
4498 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004499}
4500EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4501
4502/**
4503 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4504 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004505 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004506 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004507void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004508{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004509 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004510
4511 list_for_each_entry(codec, &codec_list, list) {
4512 if (dev == codec->dev)
4513 goto found;
4514 }
4515 return;
4516
4517found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004518
Mark Brown0d0cf002008-12-10 14:32:45 +00004519 mutex_lock(&client_mutex);
4520 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004521 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004522 mutex_unlock(&client_mutex);
4523
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004524 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4525 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004526
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004527 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004528 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004529 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004530}
4531EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4532
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004533/* Retrieve a card's name from device tree */
4534int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4535 const char *propname)
4536{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304537 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004538 int ret;
4539
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304540 if (!card->dev) {
4541 pr_err("card->dev is not set before calling %s\n", __func__);
4542 return -EINVAL;
4543 }
4544
4545 np = card->dev->of_node;
4546
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004547 ret = of_property_read_string_index(np, propname, 0, &card->name);
4548 /*
4549 * EINVAL means the property does not exist. This is fine providing
4550 * card->name was previously set, which is checked later in
4551 * snd_soc_register_card.
4552 */
4553 if (ret < 0 && ret != -EINVAL) {
4554 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004555 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004556 propname, ret);
4557 return ret;
4558 }
4559
4560 return 0;
4561}
4562EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4563
Xiubo Li9a6d4862014-02-08 15:59:52 +08004564static const struct snd_soc_dapm_widget simple_widgets[] = {
4565 SND_SOC_DAPM_MIC("Microphone", NULL),
4566 SND_SOC_DAPM_LINE("Line", NULL),
4567 SND_SOC_DAPM_HP("Headphone", NULL),
4568 SND_SOC_DAPM_SPK("Speaker", NULL),
4569};
4570
4571int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4572 const char *propname)
4573{
4574 struct device_node *np = card->dev->of_node;
4575 struct snd_soc_dapm_widget *widgets;
4576 const char *template, *wname;
4577 int i, j, num_widgets, ret;
4578
4579 num_widgets = of_property_count_strings(np, propname);
4580 if (num_widgets < 0) {
4581 dev_err(card->dev,
4582 "ASoC: Property '%s' does not exist\n", propname);
4583 return -EINVAL;
4584 }
4585 if (num_widgets & 1) {
4586 dev_err(card->dev,
4587 "ASoC: Property '%s' length is not even\n", propname);
4588 return -EINVAL;
4589 }
4590
4591 num_widgets /= 2;
4592 if (!num_widgets) {
4593 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4594 propname);
4595 return -EINVAL;
4596 }
4597
4598 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4599 GFP_KERNEL);
4600 if (!widgets) {
4601 dev_err(card->dev,
4602 "ASoC: Could not allocate memory for widgets\n");
4603 return -ENOMEM;
4604 }
4605
4606 for (i = 0; i < num_widgets; i++) {
4607 ret = of_property_read_string_index(np, propname,
4608 2 * i, &template);
4609 if (ret) {
4610 dev_err(card->dev,
4611 "ASoC: Property '%s' index %d read error:%d\n",
4612 propname, 2 * i, ret);
4613 return -EINVAL;
4614 }
4615
4616 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4617 if (!strncmp(template, simple_widgets[j].name,
4618 strlen(simple_widgets[j].name))) {
4619 widgets[i] = simple_widgets[j];
4620 break;
4621 }
4622 }
4623
4624 if (j >= ARRAY_SIZE(simple_widgets)) {
4625 dev_err(card->dev,
4626 "ASoC: DAPM widget '%s' is not supported\n",
4627 template);
4628 return -EINVAL;
4629 }
4630
4631 ret = of_property_read_string_index(np, propname,
4632 (2 * i) + 1,
4633 &wname);
4634 if (ret) {
4635 dev_err(card->dev,
4636 "ASoC: Property '%s' index %d read error:%d\n",
4637 propname, (2 * i) + 1, ret);
4638 return -EINVAL;
4639 }
4640
4641 widgets[i].name = wname;
4642 }
4643
4644 card->dapm_widgets = widgets;
4645 card->num_dapm_widgets = num_widgets;
4646
4647 return 0;
4648}
4649EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4650
Xiubo Li89c67852014-02-14 09:34:35 +08004651int snd_soc_of_parse_tdm_slot(struct device_node *np,
4652 unsigned int *slots,
4653 unsigned int *slot_width)
4654{
4655 u32 val;
4656 int ret;
4657
4658 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4659 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4660 if (ret)
4661 return ret;
4662
4663 if (slots)
4664 *slots = val;
4665 }
4666
4667 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4668 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4669 if (ret)
4670 return ret;
4671
4672 if (slot_width)
4673 *slot_width = val;
4674 }
4675
4676 return 0;
4677}
4678EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4679
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004680int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4681 const char *propname)
4682{
4683 struct device_node *np = card->dev->of_node;
4684 int num_routes;
4685 struct snd_soc_dapm_route *routes;
4686 int i, ret;
4687
4688 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004689 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004690 dev_err(card->dev,
4691 "ASoC: Property '%s' does not exist or its length is not even\n",
4692 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004693 return -EINVAL;
4694 }
4695 num_routes /= 2;
4696 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004697 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004698 propname);
4699 return -EINVAL;
4700 }
4701
4702 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4703 GFP_KERNEL);
4704 if (!routes) {
4705 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004706 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004707 return -EINVAL;
4708 }
4709
4710 for (i = 0; i < num_routes; i++) {
4711 ret = of_property_read_string_index(np, propname,
4712 2 * i, &routes[i].sink);
4713 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004714 dev_err(card->dev,
4715 "ASoC: Property '%s' index %d could not be read: %d\n",
4716 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004717 return -EINVAL;
4718 }
4719 ret = of_property_read_string_index(np, propname,
4720 (2 * i) + 1, &routes[i].source);
4721 if (ret) {
4722 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004723 "ASoC: Property '%s' index %d could not be read: %d\n",
4724 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004725 return -EINVAL;
4726 }
4727 }
4728
4729 card->num_dapm_routes = num_routes;
4730 card->dapm_routes = routes;
4731
4732 return 0;
4733}
4734EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4735
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004736unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004737 const char *prefix,
4738 struct device_node **bitclkmaster,
4739 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004740{
4741 int ret, i;
4742 char prop[128];
4743 unsigned int format = 0;
4744 int bit, frame;
4745 const char *str;
4746 struct {
4747 char *name;
4748 unsigned int val;
4749 } of_fmt_table[] = {
4750 { "i2s", SND_SOC_DAIFMT_I2S },
4751 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4752 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4753 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4754 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4755 { "ac97", SND_SOC_DAIFMT_AC97 },
4756 { "pdm", SND_SOC_DAIFMT_PDM},
4757 { "msb", SND_SOC_DAIFMT_MSB },
4758 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004759 };
4760
4761 if (!prefix)
4762 prefix = "";
4763
4764 /*
4765 * check "[prefix]format = xxx"
4766 * SND_SOC_DAIFMT_FORMAT_MASK area
4767 */
4768 snprintf(prop, sizeof(prop), "%sformat", prefix);
4769 ret = of_property_read_string(np, prop, &str);
4770 if (ret == 0) {
4771 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4772 if (strcmp(str, of_fmt_table[i].name) == 0) {
4773 format |= of_fmt_table[i].val;
4774 break;
4775 }
4776 }
4777 }
4778
4779 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004780 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004781 * SND_SOC_DAIFMT_CLOCK_MASK area
4782 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004783 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4784 if (of_get_property(np, prop, NULL))
4785 format |= SND_SOC_DAIFMT_CONT;
4786 else
4787 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004788
4789 /*
4790 * check "[prefix]bitclock-inversion"
4791 * check "[prefix]frame-inversion"
4792 * SND_SOC_DAIFMT_INV_MASK area
4793 */
4794 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4795 bit = !!of_get_property(np, prop, NULL);
4796
4797 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4798 frame = !!of_get_property(np, prop, NULL);
4799
4800 switch ((bit << 4) + frame) {
4801 case 0x11:
4802 format |= SND_SOC_DAIFMT_IB_IF;
4803 break;
4804 case 0x10:
4805 format |= SND_SOC_DAIFMT_IB_NF;
4806 break;
4807 case 0x01:
4808 format |= SND_SOC_DAIFMT_NB_IF;
4809 break;
4810 default:
4811 /* SND_SOC_DAIFMT_NB_NF is default */
4812 break;
4813 }
4814
4815 /*
4816 * check "[prefix]bitclock-master"
4817 * check "[prefix]frame-master"
4818 * SND_SOC_DAIFMT_MASTER_MASK area
4819 */
4820 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4821 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004822 if (bit && bitclkmaster)
4823 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004824
4825 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4826 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004827 if (frame && framemaster)
4828 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004829
4830 switch ((bit << 4) + frame) {
4831 case 0x11:
4832 format |= SND_SOC_DAIFMT_CBM_CFM;
4833 break;
4834 case 0x10:
4835 format |= SND_SOC_DAIFMT_CBM_CFS;
4836 break;
4837 case 0x01:
4838 format |= SND_SOC_DAIFMT_CBS_CFM;
4839 break;
4840 default:
4841 format |= SND_SOC_DAIFMT_CBS_CFS;
4842 break;
4843 }
4844
4845 return format;
4846}
4847EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4848
Kuninori Morimotocb470082013-09-10 17:39:56 -07004849int snd_soc_of_get_dai_name(struct device_node *of_node,
4850 const char **dai_name)
4851{
4852 struct snd_soc_component *pos;
4853 struct of_phandle_args args;
4854 int ret;
4855
4856 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4857 "#sound-dai-cells", 0, &args);
4858 if (ret)
4859 return ret;
4860
4861 ret = -EPROBE_DEFER;
4862
4863 mutex_lock(&client_mutex);
4864 list_for_each_entry(pos, &component_list, list) {
4865 if (pos->dev->of_node != args.np)
4866 continue;
4867
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004868 if (pos->driver->of_xlate_dai_name) {
4869 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4870 } else {
4871 int id = -1;
4872
4873 switch (args.args_count) {
4874 case 0:
4875 id = 0; /* same as dai_drv[0] */
4876 break;
4877 case 1:
4878 id = args.args[0];
4879 break;
4880 default:
4881 /* not supported */
4882 break;
4883 }
4884
4885 if (id < 0 || id >= pos->num_dai) {
4886 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004887 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004888 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004889
4890 ret = 0;
4891
4892 *dai_name = pos->dai_drv[id].name;
4893 if (!*dai_name)
4894 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004895 }
4896
Kuninori Morimotocb470082013-09-10 17:39:56 -07004897 break;
4898 }
4899 mutex_unlock(&client_mutex);
4900
4901 of_node_put(args.np);
4902
4903 return ret;
4904}
4905EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4906
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004907static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004908{
Mark Brown384c89e2008-12-03 17:34:03 +00004909#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004910 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4911 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004912 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004913 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004914 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004915
Mark Brown8a9dab12011-01-10 22:25:21 +00004916 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004917 &codec_list_fops))
4918 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4919
Mark Brown8a9dab12011-01-10 22:25:21 +00004920 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004921 &dai_list_fops))
4922 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004923
Mark Brown8a9dab12011-01-10 22:25:21 +00004924 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004925 &platform_list_fops))
4926 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004927#endif
4928
Mark Brownfb257892011-04-28 10:57:54 +01004929 snd_soc_util_init();
4930
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004931 return platform_driver_register(&soc_driver);
4932}
Mark Brown4abe8e12010-10-12 17:41:03 +01004933module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004934
Mark Brown7d8c16a2008-11-30 22:11:24 +00004935static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004936{
Mark Brownfb257892011-04-28 10:57:54 +01004937 snd_soc_util_exit();
4938
Mark Brown384c89e2008-12-03 17:34:03 +00004939#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004940 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004941#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004942 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004943}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004944module_exit(snd_soc_exit);
4945
4946/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004947MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004948MODULE_DESCRIPTION("ALSA SoC Core");
4949MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004950MODULE_ALIAS("platform:soc-audio");