blob: 419682693886cdcfa63f69a0d33ee1ba7a0952d7 [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
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200555 /* If the card is not initialized yet there is nothing to do */
556 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200557 return 0;
558
Andy Green6ed25972008-06-13 16:24:05 +0100559 /* Due to the resume being scheduled into a workqueue we could
560 * suspend before that's finished - wait for it to complete.
561 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 snd_power_lock(card->snd_card);
563 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
564 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100565
566 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100568
Mark Browna00f90f2010-12-02 16:24:24 +0000569 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100573 continue;
574
Benoit Cousson88bd8702014-07-08 23:19:34 +0200575 for (j = 0; j < card->rtd[i].num_codecs; j++) {
576 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
577 struct snd_soc_dai_driver *drv = dai->driver;
578
579 if (drv->ops->digital_mute && dai->playback_active)
580 drv->ops->digital_mute(dai, 1);
581 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582 }
583
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100584 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 for (i = 0; i < card->num_rtd; i++) {
586 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100587 continue;
588
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100590 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100591
Mark Brown87506542008-11-18 20:50:34 +0000592 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000593 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 for (i = 0; i < card->num_rtd; i++) {
596 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
597 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100600 continue;
601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
603 cpu_dai->driver->suspend(cpu_dai);
604 if (platform->driver->suspend && !platform->suspended) {
605 platform->driver->suspend(cpu_dai);
606 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100607 }
608 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 /* close any waiting streams and save state */
611 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200612 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700613 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 for (j = 0; j < card->rtd[i].num_codecs; j++) {
615 codec_dais[j]->codec->dapm.suspend_bias_level =
616 codec_dais[j]->codec->dapm.bias_level;
617 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621
622 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100623 continue;
624
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800625 snd_soc_dapm_stream_event(&card->rtd[i],
626 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 snd_soc_dapm_stream_event(&card->rtd[i],
630 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 }
633
Mark Browne2d32ff2012-08-31 17:38:32 -0700634 /* Recheck all analogue paths too */
635 dapm_mark_io_dirty(&card->dapm);
636 snd_soc_dapm_sync(&card->dapm);
637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200639 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* If there are paths active then the CODEC will be held with
641 * bias _ON and should not be suspended. */
642 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200643 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000645 /*
646 * If the CODEC is capable of idle
647 * bias off then being in STANDBY
648 * means it's doing something,
649 * otherwise fall through.
650 */
651 if (codec->dapm.idle_bias_off) {
652 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200653 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000654 break;
655 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100657 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900659 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200660 if (codec->component.regmap)
661 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800662 /* deactivate pins to sleep state */
663 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 break;
665 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200666 dev_dbg(codec->dev,
667 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 break;
669 }
670 }
671 }
672
673 for (i = 0; i < card->num_rtd; i++) {
674 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
675
676 if (card->rtd[i].dai_link->ignore_suspend)
677 continue;
678
679 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
680 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800681
682 /* deactivate pins to sleep state */
683 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684 }
685
Mark Brown87506542008-11-18 20:50:34 +0000686 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000687 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200688
689 return 0;
690}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000691EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
Andy Green6ed25972008-06-13 16:24:05 +0100693/* deferred resume work, so resume can complete before we finished
694 * setting our codec back up, which can be very slow on I2C
695 */
696static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 struct snd_soc_card *card =
699 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200700 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200701 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702
Andy Green6ed25972008-06-13 16:24:05 +0100703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
705 */
706
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000707 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100708
Mark Brown99497882010-05-07 20:24:05 +0100709 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100711
Mark Brown87506542008-11-18 20:50:34 +0000712 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000713 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 /* resume AC97 DAIs */
716 for (i = 0; i < card->num_rtd; i++) {
717 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100718
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100720 continue;
721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
723 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724 }
725
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200726 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 /* If the CODEC was idle over suspend then it will have been
728 * left with bias OFF or STANDBY and suspended so we must now
729 * resume. Otherwise the suspend was suppressed.
730 */
731 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200732 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 case SND_SOC_BIAS_STANDBY:
734 case SND_SOC_BIAS_OFF:
735 codec->driver->resume(codec);
736 codec->suspended = 0;
737 break;
738 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200739 dev_dbg(codec->dev,
740 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 break;
742 }
Mark Brown1547aba2010-05-07 21:11:40 +0100743 }
744 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100749 continue;
750
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800751 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000752 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000756 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758 }
759
Mark Brown3ff3f642008-05-19 12:32:25 +0200760 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100764 continue;
765
Benoit Cousson88bd8702014-07-08 23:19:34 +0200766 for (j = 0; j < card->rtd[i].num_codecs; j++) {
767 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
768 struct snd_soc_dai_driver *drv = dai->driver;
769
770 if (drv->ops->digital_mute && dai->playback_active)
771 drv->ops->digital_mute(dai, 0);
772 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773 }
774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 for (i = 0; i < card->num_rtd; i++) {
776 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
777 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100778
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100780 continue;
781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
783 cpu_dai->driver->resume(cpu_dai);
784 if (platform->driver->resume && platform->suspended) {
785 platform->driver->resume(cpu_dai);
786 platform->suspended = 0;
787 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788 }
789
Mark Brown87506542008-11-18 20:50:34 +0000790 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000791 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000793 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100794
795 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700797
798 /* Recheck all analogue paths too */
799 dapm_mark_io_dirty(&card->dapm);
800 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100801}
802
803/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000804int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100805{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600807 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200808
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200809 /* If the card is not initialized yet there is nothing to do */
810 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 return 0;
812
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800813 /* activate pins from sleep state */
814 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200815 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
816 struct snd_soc_dai **codec_dais = rtd->codec_dais;
817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
818 int j;
819
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800820 if (cpu_dai->active)
821 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200822
823 for (j = 0; j < rtd->num_codecs; j++) {
824 struct snd_soc_dai *codec_dai = codec_dais[j];
825 if (codec_dai->active)
826 pinctrl_pm_select_default_state(codec_dai->dev);
827 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800828 }
829
Mark Brown64ab9ba2009-03-31 11:27:03 +0100830 /* AC97 devices might have other drivers hanging off them so
831 * need to resume immediately. Other drivers don't have that
832 * problem and may take a substantial amount of time to resume
833 * due to I/O costs and anti-pop so handle them out of line.
834 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 for (i = 0; i < card->num_rtd; i++) {
836 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600837 ac97_control |= cpu_dai->driver->ac97_control;
838 }
839 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000840 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 soc_resume_deferred(&card->deferred_resume_work);
842 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000843 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600844 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000845 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100846 }
Andy Green6ed25972008-06-13 16:24:05 +0100847
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200848 return 0;
849}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000850EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000852#define snd_soc_suspend NULL
853#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854#endif
855
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100856static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800857};
858
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200859static struct snd_soc_component *soc_find_component(
860 const struct device_node *of_node, const char *name)
861{
862 struct snd_soc_component *component;
863
864 list_for_each_entry(component, &component_list, list) {
865 if (of_node) {
866 if (component->dev->of_node == of_node)
867 return component;
868 } else if (strcmp(component->name, name) == 0) {
869 return component;
870 }
871 }
872
873 return NULL;
874}
875
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200876static struct snd_soc_dai *snd_soc_find_dai(
877 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100878{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200879 struct snd_soc_component *component;
880 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100881
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200882 /* Find CPU DAI from registered DAIs*/
883 list_for_each_entry(component, &component_list, list) {
884 if (dlc->of_node && component->dev->of_node != dlc->of_node)
885 continue;
886 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
887 continue;
888 list_for_each_entry(dai, &component->dai_list, list) {
889 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100890 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200892 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100893 }
894 }
895
896 return NULL;
897}
898
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
902 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200904 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200905 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000906 struct snd_soc_platform *platform;
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
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200912 cpu_dai_component.name = dai_link->cpu_name;
913 cpu_dai_component.of_node = dai_link->cpu_of_node;
914 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
915 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000916 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000917 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000919 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000920 }
921
Benoit Cousson88bd8702014-07-08 23:19:34 +0200922 rtd->num_codecs = dai_link->num_codecs;
923
924 /* Find CODEC from registered CODECs */
925 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200926 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200927 if (!codec_dais[i]) {
928 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
929 codecs[i].dai_name);
930 return -EPROBE_DEFER;
931 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000932 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100933
Benoit Cousson88bd8702014-07-08 23:19:34 +0200934 /* Single codec links expect codec and codec_dai in runtime data */
935 rtd->codec_dai = codec_dais[0];
936 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100937
Mark Brown848dd8b2011-04-27 18:16:32 +0100938 /* if there's no platform we match on the empty platform */
939 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700940 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100941 platform_name = "snd-soc-dummy";
942
Mark Brownb19e6e72012-03-14 21:18:39 +0000943 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700945 if (dai_link->platform_of_node) {
946 if (platform->dev->of_node !=
947 dai_link->platform_of_node)
948 continue;
949 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200950 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700951 continue;
952 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700953
954 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000956 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000957 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000959 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000961
962 card->num_rtd++;
963
964 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965}
966
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200967static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600968{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200969 if (!component->probed)
970 return;
971
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200972 /* This is a HACK and will be removed soon */
973 if (component->codec)
974 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600975
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200976 if (component->remove)
977 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600978
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200979 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600980
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200981 soc_cleanup_component_debugfs(component);
982 component->probed = 0;
983 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200984}
985
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200986static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100987{
988 int err;
989
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200990 if (dai && dai->probed &&
991 dai->driver->remove_order == order) {
992 if (dai->driver->remove) {
993 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100994 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200995 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100996 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200997 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100998 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200999 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001000 }
1001}
1002
Stephen Warren62ae68f2012-06-08 12:34:23 -06001003static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004{
1005 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001006 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001007
1008 /* unregister the rtd device */
1009 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001010 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1011 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1012 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013 rtd->dev_registered = 0;
1014 }
1015
1016 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001017 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001018 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001020 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001021}
1022
Stephen Warren62ae68f2012-06-08 12:34:23 -06001023static void soc_remove_link_components(struct snd_soc_card *card, int num,
1024 int order)
1025{
1026 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1027 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001028 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001029 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001030 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001031
1032 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001033 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001034 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001035
1036 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001037 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001038 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001039 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001040 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001041 }
1042
1043 /* remove any CPU-side CODEC */
1044 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001045 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001046 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001047 }
1048}
1049
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001050static void soc_remove_dai_links(struct snd_soc_card *card)
1051{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001052 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001053
Liam Girdwood0168bf02011-06-07 16:08:05 +01001054 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1055 order++) {
1056 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001057 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001058 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001059
1060 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1061 order++) {
1062 for (dai = 0; dai < card->num_rtd; dai++)
1063 soc_remove_link_components(card, dai, order);
1064 }
1065
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001066 card->num_rtd = 0;
1067}
1068
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001069static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001070 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001071{
1072 int i;
1073
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001074 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001075 return;
1076
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001077 for (i = 0; i < card->num_configs; i++) {
1078 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001079 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001080 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001081 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001082 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001083 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001084 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001085 }
1086}
1087
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001088static int soc_probe_component(struct snd_soc_card *card,
1089 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001090{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001091 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1092 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001093 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001094 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001095
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001096 if (component->probed)
1097 return 0;
1098
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001099 component->card = card;
1100 dapm->card = card;
1101 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001102
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001103 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001104 return -ENODEV;
1105
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001106 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001107
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001108 if (component->dapm_widgets) {
1109 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1110 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001111
1112 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001113 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001114 "Failed to create new controls %d\n", ret);
1115 goto err_probe;
1116 }
1117 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001118
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001119 /*
1120 * This is rather ugly, but certain platforms expect that the DAPM
1121 * widgets for the DAIs for components with the same parent device are
1122 * created in the platforms DAPM context. Until that is fixed we need to
1123 * keep this.
1124 */
1125 if (component->steal_sibling_dai_widgets) {
1126 dai_component = NULL;
1127 list_for_each_entry(component2, &component_list, list) {
1128 if (component == component2)
1129 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001130
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001131 if (component2->dev == component->dev &&
1132 !list_empty(&component2->dai_list)) {
1133 dai_component = component2;
1134 break;
1135 }
1136 }
1137 } else {
1138 dai_component = component;
1139 list_for_each_entry(component2, &component_list, list) {
1140 if (component2->dev == component->dev &&
1141 component2->steal_sibling_dai_widgets) {
1142 dai_component = NULL;
1143 break;
1144 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001145 }
1146 }
Mark Brown888df392012-02-16 19:37:51 -08001147
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001148 if (dai_component) {
1149 list_for_each_entry(dai, &dai_component->dai_list, list) {
1150 snd_soc_dapm_new_dai_widgets(dapm, dai);
1151 if (ret != 0) {
1152 dev_err(component->dev,
1153 "Failed to create DAI widgets %d\n",
1154 ret);
1155 goto err_probe;
1156 }
1157 }
1158 }
Mark Brown33c5f962011-08-22 18:40:30 +01001159
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001160 if (component->probe) {
1161 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001162 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001163 dev_err(component->dev,
1164 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001165 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001166 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001167
1168 WARN(dapm->idle_bias_off &&
1169 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001170 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001171 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001172 }
1173
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001174 if (component->controls)
1175 snd_soc_add_component_controls(component, component->controls,
1176 component->num_controls);
1177 if (component->dapm_routes)
1178 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1179 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001180
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001181 component->probed = 1;
1182 list_add(&dapm->list, &card->dapm_list);
1183
1184 /* This is a HACK and will be removed soon */
1185 if (component->codec)
1186 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001187
Jarkko Nikula70d29332011-01-27 16:24:22 +02001188 return 0;
1189
1190err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001191 soc_cleanup_component_debugfs(component);
1192 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001193
1194 return ret;
1195}
1196
Mark Brown36ae1a92012-01-06 17:12:45 -08001197static void rtd_release(struct device *dev)
1198{
1199 kfree(dev);
1200}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001201
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001202static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1203 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001204{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205 int ret = 0;
1206
Jarkko Nikula589c3562010-12-06 16:27:07 +02001207 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001208 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1209 if (!rtd->dev)
1210 return -ENOMEM;
1211 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001212 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001213 rtd->dev->release = rtd_release;
1214 rtd->dev->init_name = name;
1215 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001216 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1218 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1219 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1220 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001221 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001222 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001223 /* calling put_device() here to free the rtd->dev */
1224 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001225 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001226 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001227 return ret;
1228 }
1229 rtd->dev_registered = 1;
1230
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001231 if (rtd->codec) {
1232 /* add DAPM sysfs entries for this codec */
1233 ret = snd_soc_dapm_sys_add(rtd->dev);
1234 if (ret < 0)
1235 dev_err(rtd->dev,
1236 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1237 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001238
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001239 /* add codec sysfs entries */
1240 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1241 if (ret < 0)
1242 dev_err(rtd->dev,
1243 "ASoC: failed to add codec sysfs files: %d\n",
1244 ret);
1245 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001246
1247 return 0;
1248}
1249
Stephen Warren62ae68f2012-06-08 12:34:23 -06001250static int soc_probe_link_components(struct snd_soc_card *card, int num,
1251 int order)
1252{
1253 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001254 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001255 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001256 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001257
1258 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001259 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001260 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001261 ret = soc_probe_component(card, component);
1262 if (ret < 0)
1263 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001264 }
1265
Benoit Cousson88bd8702014-07-08 23:19:34 +02001266 /* probe the CODEC-side components */
1267 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001268 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001269 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001270 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001271 if (ret < 0)
1272 return ret;
1273 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001274 }
1275
1276 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001277 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001278 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001279 if (ret < 0)
1280 return ret;
1281 }
1282
1283 return 0;
1284}
1285
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001286static int soc_probe_codec_dai(struct snd_soc_card *card,
1287 struct snd_soc_dai *codec_dai,
1288 int order)
1289{
1290 int ret;
1291
1292 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1293 if (codec_dai->driver->probe) {
1294 ret = codec_dai->driver->probe(codec_dai);
1295 if (ret < 0) {
1296 dev_err(codec_dai->dev,
1297 "ASoC: failed to probe CODEC DAI %s: %d\n",
1298 codec_dai->name, ret);
1299 return ret;
1300 }
1301 }
1302
1303 /* mark codec_dai as probed and add to card dai list */
1304 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001305 }
1306
1307 return 0;
1308}
1309
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001310static int soc_link_dai_widgets(struct snd_soc_card *card,
1311 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001312 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001313{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001314 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1315 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001316 struct snd_soc_dapm_widget *play_w, *capture_w;
1317 int ret;
1318
Benoit Cousson88bd8702014-07-08 23:19:34 +02001319 if (rtd->num_codecs > 1)
1320 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1321
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001322 /* link the DAI widgets */
1323 play_w = codec_dai->playback_widget;
1324 capture_w = cpu_dai->capture_widget;
1325 if (play_w && capture_w) {
1326 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1327 capture_w, play_w);
1328 if (ret != 0) {
1329 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1330 play_w->name, capture_w->name, ret);
1331 return ret;
1332 }
1333 }
1334
1335 play_w = cpu_dai->playback_widget;
1336 capture_w = codec_dai->capture_widget;
1337 if (play_w && capture_w) {
1338 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1339 capture_w, play_w);
1340 if (ret != 0) {
1341 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1342 play_w->name, capture_w->name, ret);
1343 return ret;
1344 }
1345 }
1346
1347 return 0;
1348}
1349
Stephen Warren62ae68f2012-06-08 12:34:23 -06001350static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001351{
1352 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1353 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001354 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001355 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001356 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001357
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001358 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001359 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001360
1361 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001362 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001364 for (i = 0; i < rtd->num_codecs; i++)
1365 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001366
1367 /* set default power off timeout */
1368 rtd->pmdown_time = pmdown_time;
1369
1370 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001371 if (!cpu_dai->probed &&
1372 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 if (cpu_dai->driver->probe) {
1374 ret = cpu_dai->driver->probe(cpu_dai);
1375 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001376 dev_err(cpu_dai->dev,
1377 "ASoC: failed to probe CPU DAI %s: %d\n",
1378 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001379 return ret;
1380 }
1381 }
1382 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 }
1384
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001386 for (i = 0; i < rtd->num_codecs; i++) {
1387 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1388 if (ret)
1389 return ret;
1390 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001391
Liam Girdwood0168bf02011-06-07 16:08:05 +01001392 /* complete DAI probe during last probe */
1393 if (order != SND_SOC_COMP_ORDER_LAST)
1394 return 0;
1395
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001396 /* do machine specific initialization */
1397 if (dai_link->init) {
1398 ret = dai_link->init(rtd);
1399 if (ret < 0) {
1400 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1401 dai_link->name, ret);
1402 return ret;
1403 }
1404 }
1405
1406 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001407 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001408 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001409
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001410#ifdef CONFIG_DEBUG_FS
1411 /* add DPCM sysfs entries */
1412 if (dai_link->dynamic) {
1413 ret = soc_dpcm_debugfs_add(rtd);
1414 if (ret < 0) {
1415 dev_err(rtd->dev,
1416 "ASoC: failed to add dpcm sysfs entries: %d\n",
1417 ret);
1418 return ret;
1419 }
1420 }
1421#endif
1422
Mark Brown36ae1a92012-01-06 17:12:45 -08001423 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001425 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1426 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001427
Namarta Kohli1245b702012-08-16 17:10:41 +05301428 if (cpu_dai->driver->compress_dai) {
1429 /*create compress_device"*/
1430 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001431 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001432 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301433 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001434 return ret;
1435 }
1436 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301437
1438 if (!dai_link->params) {
1439 /* create the pcm */
1440 ret = soc_new_pcm(rtd, num);
1441 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001442 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301443 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001444 return ret;
1445 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301446 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001447 INIT_DELAYED_WORK(&rtd->delayed_work,
1448 codec2codec_close_delayed_work);
1449
Namarta Kohli1245b702012-08-16 17:10:41 +05301450 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001451 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001452 if (ret)
1453 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001454 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001455 }
1456
1457 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001458 for (i = 0; i < rtd->num_codecs; i++) {
1459 if (rtd->codec_dais[i]->driver->ac97_control)
1460 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1461 rtd->cpu_dai->ac97_pdata);
1462 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463
1464 return 0;
1465}
1466
1467#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001468static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1469 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470{
1471 int ret;
1472
1473 /* Only instantiate AC97 if not already done by the adaptor
1474 * for the generic AC97 subsystem.
1475 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001476 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001477 /*
1478 * It is possible that the AC97 device is already registered to
1479 * the device subsystem. This happens when the device is created
1480 * via snd_ac97_mixer(). Currently only SoC codec that does so
1481 * is the generic AC97 glue but others migh emerge.
1482 *
1483 * In those cases we don't try to register the device again.
1484 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001485 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001486 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001488 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001490 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001491 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 return ret;
1493 }
1494
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001495 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 }
1497 return 0;
1498}
1499
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001500static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501{
1502 if (codec->ac97_registered) {
1503 soc_ac97_dev_unregister(codec);
1504 codec->ac97_registered = 0;
1505 }
1506}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001507
Benoit Cousson88bd8702014-07-08 23:19:34 +02001508static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1509{
1510 int i, ret;
1511
1512 for (i = 0; i < rtd->num_codecs; i++) {
1513 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1514
1515 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1516 if (ret) {
1517 while (--i >= 0)
1518 soc_unregister_ac97_codec(codec_dai->codec);
1519 return ret;
1520 }
1521 }
1522
1523 return 0;
1524}
1525
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001526static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1527{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001528 int i;
1529
1530 for (i = 0; i < rtd->num_codecs; i++)
1531 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001532}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001533#endif
1534
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001535static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001536{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001537 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001538 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001539 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001540
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001541 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1542 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001543 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001544 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001545
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001546 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001547 return -EPROBE_DEFER;
1548 }
1549
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001550 /*
1551 * Some places still reference rtd->codec, so we have to keep that
1552 * initialized if the component is a CODEC. Once all those references
1553 * have been removed, this code can be removed as well.
1554 */
1555 rtd->codec = rtd->component->codec;
1556
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001557 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001558}
1559
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001560static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1561{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001562 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001563 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001564 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001565
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001566 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001567 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001568 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001569
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001570 /* do machine specific initialization */
1571 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001572 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001573 if (ret < 0) {
1574 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1575 aux_dev->name, ret);
1576 return ret;
1577 }
1578 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001579
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001580 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001581}
1582
1583static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1584{
1585 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001586 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001587
1588 /* unregister the rtd device */
1589 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001590 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001591 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001592 rtd->dev_registered = 0;
1593 }
1594
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001595 if (component && component->probed)
1596 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001597}
1598
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001599static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001600{
1601 int ret;
1602
1603 if (codec->cache_init)
1604 return 0;
1605
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001606 ret = snd_soc_cache_init(codec);
1607 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001608 dev_err(codec->dev,
1609 "ASoC: Failed to set cache compression type: %d\n",
1610 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001611 return ret;
1612 }
1613 codec->cache_init = 1;
1614 return 0;
1615}
1616
Mark Brownb19e6e72012-03-14 21:18:39 +00001617static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001619 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001620 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001621 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622
Liam Girdwood01b9d992012-03-07 10:38:25 +00001623 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624
Mark Brownb19e6e72012-03-14 21:18:39 +00001625 /* bind DAIs */
1626 for (i = 0; i < card->num_links; i++) {
1627 ret = soc_bind_dai_link(card, i);
1628 if (ret != 0)
1629 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630 }
1631
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001632 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001633 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001634 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001635 if (ret != 0)
1636 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 }
1638
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001639 /* initialize the register cache for each available codec */
1640 list_for_each_entry(codec, &codec_list, list) {
1641 if (codec->cache_init)
1642 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001643 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001644 if (ret < 0)
1645 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001646 }
1647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001649 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650 card->owner, 0, &card->snd_card);
1651 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001652 dev_err(card->dev,
1653 "ASoC: can't create sound card for card %s: %d\n",
1654 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001655 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001657
Mark Browne37a4972011-03-02 18:21:57 +00001658 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1659 card->dapm.dev = card->dev;
1660 card->dapm.card = card;
1661 list_add(&card->dapm.list, &card->dapm_list);
1662
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001663#ifdef CONFIG_DEBUG_FS
1664 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1665#endif
1666
Mark Brown88ee1c62011-02-02 10:43:26 +00001667#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001668 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001669 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001670#endif
Andy Green6ed25972008-06-13 16:24:05 +01001671
Mark Brown9a841eb2011-04-12 17:51:37 -07001672 if (card->dapm_widgets)
1673 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1674 card->num_dapm_widgets);
1675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001676 /* initialise the sound card only once */
1677 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001678 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001679 if (ret < 0)
1680 goto card_probe_error;
1681 }
1682
Stephen Warren62ae68f2012-06-08 12:34:23 -06001683 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001684 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1685 order++) {
1686 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001687 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001688 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001689 dev_err(card->dev,
1690 "ASoC: failed to instantiate card %d\n",
1691 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001692 goto probe_dai_err;
1693 }
1694 }
1695 }
1696
1697 /* probe all DAI links on this card */
1698 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1699 order++) {
1700 for (i = 0; i < card->num_links; i++) {
1701 ret = soc_probe_link_dais(card, i, order);
1702 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001703 dev_err(card->dev,
1704 "ASoC: failed to instantiate card %d\n",
1705 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001706 goto probe_dai_err;
1707 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001708 }
1709 }
1710
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001711 for (i = 0; i < card->num_aux_devs; i++) {
1712 ret = soc_probe_aux_dev(card, i);
1713 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001714 dev_err(card->dev,
1715 "ASoC: failed to add auxiliary devices %d\n",
1716 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001717 goto probe_aux_dev_err;
1718 }
1719 }
1720
Mark Brown888df392012-02-16 19:37:51 -08001721 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001722 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001723
Mark Brownb7af1da2011-04-07 19:18:44 +09001724 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001725 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001726
Mark Brownb8ad29d2011-03-02 18:35:51 +00001727 if (card->dapm_routes)
1728 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1729 card->num_dapm_routes);
1730
Mark Brown75d9ac42011-09-27 16:41:01 +01001731 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001732 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001733 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001734 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001735
Mark Brownf04209a2012-04-09 16:29:21 +01001736 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001737 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1738 int j;
1739
1740 for (j = 0; j < rtd->num_codecs; j++) {
1741 struct snd_soc_dai *codec_dai = codec_dais[j];
1742
1743 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1744 if (ret != 0 && ret != -ENOTSUPP)
1745 dev_warn(codec_dai->dev,
1746 "ASoC: Failed to set DAI format: %d\n",
1747 ret);
1748 }
Mark Brownf04209a2012-04-09 16:29:21 +01001749 }
1750
1751 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001752 if (dai_fmt &&
1753 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001754 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1755 dai_fmt);
1756 if (ret != 0 && ret != -ENOTSUPP)
1757 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001758 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001759 ret);
1760 } else if (dai_fmt) {
1761 /* Flip the polarity for the "CPU" end */
1762 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1763 switch (dai_link->dai_fmt &
1764 SND_SOC_DAIFMT_MASTER_MASK) {
1765 case SND_SOC_DAIFMT_CBM_CFM:
1766 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1767 break;
1768 case SND_SOC_DAIFMT_CBM_CFS:
1769 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1770 break;
1771 case SND_SOC_DAIFMT_CBS_CFM:
1772 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1773 break;
1774 case SND_SOC_DAIFMT_CBS_CFS:
1775 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1776 break;
1777 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001778
1779 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001780 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001781 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001782 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001783 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001784 ret);
1785 }
1786 }
1787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001788 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001790 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1791 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001792 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1793 "%s", card->driver_name ? card->driver_name : card->name);
1794 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1795 switch (card->snd_card->driver[i]) {
1796 case '_':
1797 case '-':
1798 case '\0':
1799 break;
1800 default:
1801 if (!isalnum(card->snd_card->driver[i]))
1802 card->snd_card->driver[i] = '_';
1803 break;
1804 }
1805 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001806
Mark Brown28e9ad92011-03-02 18:36:34 +00001807 if (card->late_probe) {
1808 ret = card->late_probe(card);
1809 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001810 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001811 card->name, ret);
1812 goto probe_aux_dev_err;
1813 }
1814 }
1815
Stephen Warren16332812011-11-23 12:42:04 -07001816 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001817 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001818
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001819 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001820
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001821 ret = snd_card_register(card->snd_card);
1822 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001823 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1824 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001825 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001826 }
1827
1828#ifdef CONFIG_SND_SOC_AC97_BUS
1829 /* register any AC97 codecs */
1830 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001831 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1832 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001833 dev_err(card->dev,
1834 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001835 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001836 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001837 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001838 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001839 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001840#endif
1841
Mark Brown435c5e22008-12-04 15:32:53 +00001842 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001843 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001845
1846 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001847
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001848probe_aux_dev_err:
1849 for (i = 0; i < card->num_aux_devs; i++)
1850 soc_remove_aux_dev(card, i);
1851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001853 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001854
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001855card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001856 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001857 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001858
1859 snd_card_free(card->snd_card);
1860
Mark Brownb19e6e72012-03-14 21:18:39 +00001861base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001862 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001863
Mark Brownb19e6e72012-03-14 21:18:39 +00001864 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001865}
1866
1867/* probes a new socdev */
1868static int soc_probe(struct platform_device *pdev)
1869{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001871
Vinod Koul70a7ca32011-01-14 19:22:48 +05301872 /*
1873 * no card, so machine driver should be registering card
1874 * we should not be here in that case so ret error
1875 */
1876 if (!card)
1877 return -EINVAL;
1878
Mark Brownfe4085e2012-03-02 13:07:41 +00001879 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001880 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001881 card->name);
1882
Mark Brown435c5e22008-12-04 15:32:53 +00001883 /* Bodge while we unpick instantiation */
1884 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001885
Mark Brown28d528c2012-08-09 18:45:23 +01001886 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001887}
1888
Vinod Koulb0e26482011-01-13 22:48:02 +05301889static int soc_cleanup_card_resources(struct snd_soc_card *card)
1890{
Vinod Koulb0e26482011-01-13 22:48:02 +05301891 int i;
1892
1893 /* make sure any delayed work runs */
1894 for (i = 0; i < card->num_rtd; i++) {
1895 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001896 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301897 }
1898
1899 /* remove auxiliary devices */
1900 for (i = 0; i < card->num_aux_devs; i++)
1901 soc_remove_aux_dev(card, i);
1902
1903 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001904 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301905
1906 soc_cleanup_card_debugfs(card);
1907
1908 /* remove the card */
1909 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001910 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301911
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001912 snd_soc_dapm_free(&card->dapm);
1913
Vinod Koulb0e26482011-01-13 22:48:02 +05301914 snd_card_free(card->snd_card);
1915 return 0;
1916
1917}
1918
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001919/* removes a socdev */
1920static int soc_remove(struct platform_device *pdev)
1921{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001922 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001923
Mark Brownc5af3a22008-11-28 13:29:45 +00001924 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925 return 0;
1926}
1927
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001928int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001929{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001930 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001931 int i;
Mark Brown51737472009-06-22 13:16:51 +01001932
1933 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001934 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001935
1936 /* Flush out pmdown_time work - we actually do want to run it
1937 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001938 for (i = 0; i < card->num_rtd; i++) {
1939 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001940 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941 }
Mark Brown51737472009-06-22 13:16:51 +01001942
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001943 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001944
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001945 /* deactivate pins to sleep state */
1946 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001947 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1948 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1949 int j;
1950
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001951 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001952 for (j = 0; j < rtd->num_codecs; j++) {
1953 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1954 pinctrl_pm_select_sleep_state(codec_dai->dev);
1955 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001956 }
1957
Mark Brown416356f2009-06-30 19:05:15 +01001958 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001959}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001960EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001961
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001962const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301963 .suspend = snd_soc_suspend,
1964 .resume = snd_soc_resume,
1965 .freeze = snd_soc_suspend,
1966 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001967 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301968 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001969};
Stephen Warrendeb26072011-04-05 19:35:30 -06001970EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001971
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972/* ASoC platform driver */
1973static struct platform_driver soc_driver = {
1974 .driver = {
1975 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001976 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001977 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978 },
1979 .probe = soc_probe,
1980 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981};
1982
Mark Brown096e49d2009-07-05 15:12:22 +01001983/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984 * snd_soc_new_ac97_codec - initailise AC97 device
1985 * @codec: audio codec
1986 * @ops: AC97 bus operations
1987 * @num: AC97 codec number
1988 *
1989 * Initialises AC97 codec resources for use by ad-hoc devices only.
1990 */
1991int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1992 struct snd_ac97_bus_ops *ops, int num)
1993{
1994 mutex_lock(&codec->mutex);
1995
1996 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1997 if (codec->ac97 == NULL) {
1998 mutex_unlock(&codec->mutex);
1999 return -ENOMEM;
2000 }
2001
2002 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2003 if (codec->ac97->bus == NULL) {
2004 kfree(codec->ac97);
2005 codec->ac97 = NULL;
2006 mutex_unlock(&codec->mutex);
2007 return -ENOMEM;
2008 }
2009
2010 codec->ac97->bus->ops = ops;
2011 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002012
2013 /*
2014 * Mark the AC97 device to be created by us. This way we ensure that the
2015 * device will be registered with the device subsystem later on.
2016 */
2017 codec->ac97_created = 1;
2018
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019 mutex_unlock(&codec->mutex);
2020 return 0;
2021}
2022EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2023
Markus Pargmann741a5092013-08-19 17:05:55 +02002024static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2025
2026static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2027{
2028 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2029
2030 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2031
2032 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2033
2034 udelay(10);
2035
2036 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2037
2038 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2039 msleep(2);
2040}
2041
2042static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2043{
2044 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2045
2046 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2047
2048 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2049 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2050 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2051
2052 udelay(10);
2053
2054 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2055
2056 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2057 msleep(2);
2058}
2059
2060static int snd_soc_ac97_parse_pinctl(struct device *dev,
2061 struct snd_ac97_reset_cfg *cfg)
2062{
2063 struct pinctrl *p;
2064 struct pinctrl_state *state;
2065 int gpio;
2066 int ret;
2067
2068 p = devm_pinctrl_get(dev);
2069 if (IS_ERR(p)) {
2070 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002071 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002072 }
2073 cfg->pctl = p;
2074
2075 state = pinctrl_lookup_state(p, "ac97-reset");
2076 if (IS_ERR(state)) {
2077 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002078 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002079 }
2080 cfg->pstate_reset = state;
2081
2082 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2083 if (IS_ERR(state)) {
2084 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002085 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002086 }
2087 cfg->pstate_warm_reset = state;
2088
2089 state = pinctrl_lookup_state(p, "ac97-running");
2090 if (IS_ERR(state)) {
2091 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002092 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002093 }
2094 cfg->pstate_run = state;
2095
2096 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2097 if (gpio < 0) {
2098 dev_err(dev, "Can't find ac97-sync gpio\n");
2099 return gpio;
2100 }
2101 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2102 if (ret) {
2103 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2104 return ret;
2105 }
2106 cfg->gpio_sync = gpio;
2107
2108 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2109 if (gpio < 0) {
2110 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2111 return gpio;
2112 }
2113 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2114 if (ret) {
2115 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2116 return ret;
2117 }
2118 cfg->gpio_sdata = gpio;
2119
2120 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2121 if (gpio < 0) {
2122 dev_err(dev, "Can't find ac97-reset gpio\n");
2123 return gpio;
2124 }
2125 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2126 if (ret) {
2127 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2128 return ret;
2129 }
2130 cfg->gpio_reset = gpio;
2131
2132 return 0;
2133}
2134
Mark Brownb047e1c2013-06-26 12:45:59 +01002135struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002136EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002137
2138int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2139{
2140 if (ops == soc_ac97_ops)
2141 return 0;
2142
2143 if (soc_ac97_ops && ops)
2144 return -EBUSY;
2145
2146 soc_ac97_ops = ops;
2147
2148 return 0;
2149}
2150EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2151
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002152/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002153 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2154 *
2155 * This function sets the reset and warm_reset properties of ops and parses
2156 * the device node of pdev to get pinctrl states and gpio numbers to use.
2157 */
2158int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2159 struct platform_device *pdev)
2160{
2161 struct device *dev = &pdev->dev;
2162 struct snd_ac97_reset_cfg cfg;
2163 int ret;
2164
2165 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2166 if (ret)
2167 return ret;
2168
2169 ret = snd_soc_set_ac97_ops(ops);
2170 if (ret)
2171 return ret;
2172
2173 ops->warm_reset = snd_soc_ac97_warm_reset;
2174 ops->reset = snd_soc_ac97_reset;
2175
2176 snd_ac97_rst_cfg = cfg;
2177 return 0;
2178}
2179EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2180
2181/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182 * snd_soc_free_ac97_codec - free AC97 codec device
2183 * @codec: audio codec
2184 *
2185 * Frees AC97 codec device resources.
2186 */
2187void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2188{
2189 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002190#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002191 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002192#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193 kfree(codec->ac97->bus);
2194 kfree(codec->ac97);
2195 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002196 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 mutex_unlock(&codec->mutex);
2198}
2199EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2200
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202 * snd_soc_cnew - create new control
2203 * @_template: control template
2204 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002205 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002206 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002207 *
2208 * Create a new mixer control from a template control.
2209 *
2210 * Returns 0 for success, else error.
2211 */
2212struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002213 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002214 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215{
2216 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002217 struct snd_kcontrol *kcontrol;
2218 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219
2220 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221 template.index = 0;
2222
Mark Brownefb7ac32011-03-08 17:23:24 +00002223 if (!long_name)
2224 long_name = template.name;
2225
2226 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002227 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002228 if (!name)
2229 return NULL;
2230
Mark Brownefb7ac32011-03-08 17:23:24 +00002231 template.name = name;
2232 } else {
2233 template.name = long_name;
2234 }
2235
2236 kcontrol = snd_ctl_new1(&template, data);
2237
2238 kfree(name);
2239
2240 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241}
2242EXPORT_SYMBOL_GPL(snd_soc_cnew);
2243
Liam Girdwood022658b2012-02-03 17:43:09 +00002244static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2245 const struct snd_kcontrol_new *controls, int num_controls,
2246 const char *prefix, void *data)
2247{
2248 int err, i;
2249
2250 for (i = 0; i < num_controls; i++) {
2251 const struct snd_kcontrol_new *control = &controls[i];
2252 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2253 control->name, prefix));
2254 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002255 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2256 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002257 return err;
2258 }
2259 }
2260
2261 return 0;
2262}
2263
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002264struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2265 const char *name)
2266{
2267 struct snd_card *card = soc_card->snd_card;
2268 struct snd_kcontrol *kctl;
2269
2270 if (unlikely(!name))
2271 return NULL;
2272
2273 list_for_each_entry(kctl, &card->controls, list)
2274 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2275 return kctl;
2276 return NULL;
2277}
2278EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2279
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002281 * snd_soc_add_component_controls - Add an array of controls to a component.
2282 *
2283 * @component: Component to add controls to
2284 * @controls: Array of controls to add
2285 * @num_controls: Number of elements in the array
2286 *
2287 * Return: 0 for success, else error.
2288 */
2289int snd_soc_add_component_controls(struct snd_soc_component *component,
2290 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2291{
2292 struct snd_card *card = component->card->snd_card;
2293
2294 return snd_soc_add_controls(card, component->dev, controls,
2295 num_controls, component->name_prefix, component);
2296}
2297EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2298
2299/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002300 * snd_soc_add_codec_controls - add an array of controls to a codec.
2301 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002302 * duplicating this code.
2303 *
2304 * @codec: codec to add controls to
2305 * @controls: array of controls to add
2306 * @num_controls: number of elements in the array
2307 *
2308 * Return 0 for success, else error.
2309 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002310int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002311 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002312{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002313 return snd_soc_add_component_controls(&codec->component, controls,
2314 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002315}
Liam Girdwood022658b2012-02-03 17:43:09 +00002316EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002317
2318/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002319 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002320 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002321 *
2322 * @platform: platform to add controls to
2323 * @controls: array of controls to add
2324 * @num_controls: number of elements in the array
2325 *
2326 * Return 0 for success, else error.
2327 */
2328int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002329 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002330{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002331 return snd_soc_add_component_controls(&platform->component, controls,
2332 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002333}
2334EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2335
2336/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002337 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2338 * Convenience function to add a list of controls.
2339 *
2340 * @soc_card: SoC card to add controls to
2341 * @controls: array of controls to add
2342 * @num_controls: number of elements in the array
2343 *
2344 * Return 0 for success, else error.
2345 */
2346int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2347 const struct snd_kcontrol_new *controls, int num_controls)
2348{
2349 struct snd_card *card = soc_card->snd_card;
2350
2351 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2352 NULL, soc_card);
2353}
2354EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2355
2356/**
2357 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2358 * Convienience function to add a list of controls.
2359 *
2360 * @dai: DAI 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_dai_controls(struct snd_soc_dai *dai,
2367 const struct snd_kcontrol_new *controls, int num_controls)
2368{
2369 struct snd_card *card = dai->card->snd_card;
2370
2371 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2372 NULL, dai);
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2375
2376/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002377 * snd_soc_info_enum_double - enumerated double mixer info callback
2378 * @kcontrol: mixer control
2379 * @uinfo: control element information
2380 *
2381 * Callback to provide information about a double enumerated
2382 * mixer control.
2383 *
2384 * Returns 0 for success.
2385 */
2386int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_info *uinfo)
2388{
2389 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2390
2391 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2392 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002393 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002395 if (uinfo->value.enumerated.item >= e->items)
2396 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002397 strlcpy(uinfo->value.enumerated.name,
2398 e->texts[uinfo->value.enumerated.item],
2399 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 return 0;
2401}
2402EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2403
2404/**
2405 * snd_soc_get_enum_double - enumerated double mixer get callback
2406 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002407 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408 *
2409 * Callback to get the value of a double enumerated mixer.
2410 *
2411 * Returns 0 for success.
2412 */
2413int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2415{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002416 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002418 unsigned int val, item;
2419 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002420 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002421
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002422 ret = snd_soc_component_read(component, e->reg, &reg_val);
2423 if (ret)
2424 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002425 val = (reg_val >> e->shift_l) & e->mask;
2426 item = snd_soc_enum_val_to_item(e, val);
2427 ucontrol->value.enumerated.item[0] = item;
2428 if (e->shift_l != e->shift_r) {
2429 val = (reg_val >> e->shift_l) & e->mask;
2430 item = snd_soc_enum_val_to_item(e, val);
2431 ucontrol->value.enumerated.item[1] = item;
2432 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433
2434 return 0;
2435}
2436EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2437
2438/**
2439 * snd_soc_put_enum_double - enumerated double mixer put callback
2440 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002441 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442 *
2443 * Callback to set the value of a double enumerated mixer.
2444 *
2445 * Returns 0 for success.
2446 */
2447int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
2449{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002450 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002452 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002453 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002454 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002456 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002458 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002459 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002461 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002463 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002464 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 }
2466
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002467 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468}
2469EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2470
2471/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002472 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002473 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002474 * @reg: Register to read
2475 * @mask: Mask to use after shifting the register value
2476 * @shift: Right shift of register value
2477 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002478 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002479 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002480 * This functions reads a codec register. The register value is shifted right
2481 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2482 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002483 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002484 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002485 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002486static int snd_soc_read_signed(struct snd_soc_component *component,
2487 unsigned int reg, unsigned int mask, unsigned int shift,
2488 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002489{
Markus Pargmannf227b882014-01-16 16:02:10 +01002490 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002491 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002492
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002493 ret = snd_soc_component_read(component, reg, &val);
2494 if (ret < 0)
2495 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002496
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002497 val = (val >> shift) & mask;
2498
2499 if (!sign_bit) {
2500 *signed_val = val;
2501 return 0;
2502 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002503
2504 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002505 if (!(val & BIT(sign_bit))) {
2506 *signed_val = val;
2507 return 0;
2508 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002509
2510 ret = val;
2511
2512 /*
2513 * The register most probably does not contain a full-sized int.
2514 * Instead we have an arbitrary number of bits in a signed
2515 * representation which has to be translated into a full-sized int.
2516 * This is done by filling up all bits above the sign-bit.
2517 */
2518 ret |= ~((int)(BIT(sign_bit) - 1));
2519
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002520 *signed_val = ret;
2521
2522 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002523}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002524
2525/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 * snd_soc_info_volsw - single mixer info callback
2527 * @kcontrol: mixer control
2528 * @uinfo: control element information
2529 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002530 * Callback to provide information about a single mixer control, or a double
2531 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002532 *
2533 * Returns 0 for success.
2534 */
2535int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_info *uinfo)
2537{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002538 struct soc_mixer_control *mc =
2539 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002540 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002541
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002542 if (!mc->platform_max)
2543 mc->platform_max = mc->max;
2544 platform_max = mc->platform_max;
2545
2546 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002547 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2548 else
2549 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2550
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002551 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002553 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002554 return 0;
2555}
2556EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2557
2558/**
2559 * snd_soc_get_volsw - single mixer get callback
2560 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002561 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002562 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002563 * Callback to get the value of a single mixer control, or a double mixer
2564 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002565 *
2566 * Returns 0 for success.
2567 */
2568int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2570{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002571 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002572 struct soc_mixer_control *mc =
2573 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002574 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002575 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002576 unsigned int shift = mc->shift;
2577 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002578 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002579 int min = mc->min;
2580 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002581 unsigned int mask = (1 << fls(max)) - 1;
2582 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002583 int val;
2584 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585
Markus Pargmannf227b882014-01-16 16:02:10 +01002586 if (sign_bit)
2587 mask = BIT(sign_bit + 1) - 1;
2588
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002589 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2590 if (ret)
2591 return ret;
2592
2593 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002594 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002595 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002596 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002597
2598 if (snd_soc_volsw_is_stereo(mc)) {
2599 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002600 ret = snd_soc_read_signed(component, reg, mask, rshift,
2601 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002602 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002603 ret = snd_soc_read_signed(component, reg2, mask, shift,
2604 sign_bit, &val);
2605 if (ret)
2606 return ret;
2607
2608 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002609 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002611 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612 }
2613
2614 return 0;
2615}
2616EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2617
2618/**
2619 * snd_soc_put_volsw - single mixer put callback
2620 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002621 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002623 * Callback to set the value of a single mixer control, or a double mixer
2624 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002625 *
2626 * Returns 0 for success.
2627 */
2628int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2630{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002631 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002632 struct soc_mixer_control *mc =
2633 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002635 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002636 unsigned int shift = mc->shift;
2637 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002638 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002639 int min = mc->min;
2640 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002641 unsigned int mask = (1 << fls(max)) - 1;
2642 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002643 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002644 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002645 unsigned int val2 = 0;
2646 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002647
Markus Pargmannf227b882014-01-16 16:02:10 +01002648 if (sign_bit)
2649 mask = BIT(sign_bit + 1) - 1;
2650
2651 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002653 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654 val_mask = mask << shift;
2655 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002656 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002657 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002659 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002660 if (reg == reg2) {
2661 val_mask |= mask << rshift;
2662 val |= val2 << rshift;
2663 } else {
2664 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002665 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002666 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002667 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002668 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002669 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002670 return err;
2671
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002672 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002673 err = snd_soc_component_update_bits(component, reg2, val_mask,
2674 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002675
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002676 return err;
2677}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002678EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002679
Mark Browne13ac2e2008-05-28 17:58:05 +01002680/**
Brian Austin1d99f242012-03-30 10:43:55 -05002681 * snd_soc_get_volsw_sx - single mixer get callback
2682 * @kcontrol: mixer control
2683 * @ucontrol: control element information
2684 *
2685 * Callback to get the value of a single mixer control, or a double mixer
2686 * control that spans 2 registers.
2687 *
2688 * Returns 0 for success.
2689 */
2690int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2692{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002693 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002696 unsigned int reg = mc->reg;
2697 unsigned int reg2 = mc->rreg;
2698 unsigned int shift = mc->shift;
2699 unsigned int rshift = mc->rshift;
2700 int max = mc->max;
2701 int min = mc->min;
2702 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002703 unsigned int val;
2704 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002705
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002706 ret = snd_soc_component_read(component, reg, &val);
2707 if (ret < 0)
2708 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002709
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002710 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2711
2712 if (snd_soc_volsw_is_stereo(mc)) {
2713 ret = snd_soc_component_read(component, reg2, &val);
2714 if (ret < 0)
2715 return ret;
2716
2717 val = ((val >> rshift) - min) & mask;
2718 ucontrol->value.integer.value[1] = val;
2719 }
Brian Austin1d99f242012-03-30 10:43:55 -05002720
2721 return 0;
2722}
2723EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2724
2725/**
2726 * snd_soc_put_volsw_sx - double mixer set callback
2727 * @kcontrol: mixer control
2728 * @uinfo: control element information
2729 *
2730 * Callback to set the value of a double mixer control that spans 2 registers.
2731 *
2732 * Returns 0 for success.
2733 */
2734int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2736{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002737 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002738 struct soc_mixer_control *mc =
2739 (struct soc_mixer_control *)kcontrol->private_value;
2740
2741 unsigned int reg = mc->reg;
2742 unsigned int reg2 = mc->rreg;
2743 unsigned int shift = mc->shift;
2744 unsigned int rshift = mc->rshift;
2745 int max = mc->max;
2746 int min = mc->min;
2747 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002748 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002749 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002750
2751 val_mask = mask << shift;
2752 val = (ucontrol->value.integer.value[0] + min) & mask;
2753 val = val << shift;
2754
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002755 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302756 if (err < 0)
2757 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002758
2759 if (snd_soc_volsw_is_stereo(mc)) {
2760 val_mask = mask << rshift;
2761 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2762 val2 = val2 << rshift;
2763
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002764 err = snd_soc_component_update_bits(component, reg2, val_mask,
2765 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002766 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002767 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002768}
2769EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2770
2771/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002772 * snd_soc_info_volsw_s8 - signed mixer info callback
2773 * @kcontrol: mixer control
2774 * @uinfo: control element information
2775 *
2776 * Callback to provide information about a signed mixer control.
2777 *
2778 * Returns 0 for success.
2779 */
2780int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_info *uinfo)
2782{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002783 struct soc_mixer_control *mc =
2784 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002785 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002786 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002787
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002788 if (!mc->platform_max)
2789 mc->platform_max = mc->max;
2790 platform_max = mc->platform_max;
2791
Mark Browne13ac2e2008-05-28 17:58:05 +01002792 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2793 uinfo->count = 2;
2794 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002795 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002796 return 0;
2797}
2798EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2799
2800/**
2801 * snd_soc_get_volsw_s8 - signed mixer get callback
2802 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002803 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002804 *
2805 * Callback to get the value of a signed mixer control.
2806 *
2807 * Returns 0 for success.
2808 */
2809int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_value *ucontrol)
2811{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002812 struct soc_mixer_control *mc =
2813 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002814 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002815 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002816 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002817 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002818 int ret;
2819
2820 ret = snd_soc_component_read(component, reg, &val);
2821 if (ret)
2822 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002823
2824 ucontrol->value.integer.value[0] =
2825 ((signed char)(val & 0xff))-min;
2826 ucontrol->value.integer.value[1] =
2827 ((signed char)((val >> 8) & 0xff))-min;
2828 return 0;
2829}
2830EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2831
2832/**
2833 * snd_soc_put_volsw_sgn - signed mixer put callback
2834 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002835 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002836 *
2837 * Callback to set the value of a signed mixer control.
2838 *
2839 * Returns 0 for success.
2840 */
2841int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
2843{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002844 struct soc_mixer_control *mc =
2845 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002846 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002847 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002848 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002849 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002850
2851 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2852 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2853
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002854 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002855}
2856EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2857
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002858/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002859 * snd_soc_info_volsw_range - single mixer info callback with range.
2860 * @kcontrol: mixer control
2861 * @uinfo: control element information
2862 *
2863 * Callback to provide information, within a range, about a single
2864 * mixer control.
2865 *
2866 * returns 0 for success.
2867 */
2868int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_info *uinfo)
2870{
2871 struct soc_mixer_control *mc =
2872 (struct soc_mixer_control *)kcontrol->private_value;
2873 int platform_max;
2874 int min = mc->min;
2875
2876 if (!mc->platform_max)
2877 mc->platform_max = mc->max;
2878 platform_max = mc->platform_max;
2879
2880 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002881 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002882 uinfo->value.integer.min = 0;
2883 uinfo->value.integer.max = platform_max - min;
2884
2885 return 0;
2886}
2887EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2888
2889/**
2890 * snd_soc_put_volsw_range - single mixer put value callback with range.
2891 * @kcontrol: mixer control
2892 * @ucontrol: control element information
2893 *
2894 * Callback to set the value, within a range, for a single mixer control.
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
2901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002903 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002904 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002905 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002906 unsigned int shift = mc->shift;
2907 int min = mc->min;
2908 int max = mc->max;
2909 unsigned int mask = (1 << fls(max)) - 1;
2910 unsigned int invert = mc->invert;
2911 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002912 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002913
2914 val = ((ucontrol->value.integer.value[0] + min) & mask);
2915 if (invert)
2916 val = max - val;
2917 val_mask = mask << shift;
2918 val = val << shift;
2919
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002920 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002921 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002922 return ret;
2923
2924 if (snd_soc_volsw_is_stereo(mc)) {
2925 val = ((ucontrol->value.integer.value[1] + min) & mask);
2926 if (invert)
2927 val = max - val;
2928 val_mask = mask << shift;
2929 val = val << shift;
2930
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002931 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2932 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002933 }
2934
2935 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002936}
2937EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2938
2939/**
2940 * snd_soc_get_volsw_range - single mixer get callback with range
2941 * @kcontrol: mixer control
2942 * @ucontrol: control element information
2943 *
2944 * Callback to get the value, within a range, of a single mixer control.
2945 *
2946 * Returns 0 for success.
2947 */
2948int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2950{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002951 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002952 struct soc_mixer_control *mc =
2953 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002954 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002955 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002956 unsigned int shift = mc->shift;
2957 int min = mc->min;
2958 int max = mc->max;
2959 unsigned int mask = (1 << fls(max)) - 1;
2960 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002961 unsigned int val;
2962 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002963
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002964 ret = snd_soc_component_read(component, reg, &val);
2965 if (ret)
2966 return ret;
2967
2968 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002969 if (invert)
2970 ucontrol->value.integer.value[0] =
2971 max - ucontrol->value.integer.value[0];
2972 ucontrol->value.integer.value[0] =
2973 ucontrol->value.integer.value[0] - min;
2974
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002976 ret = snd_soc_component_read(component, rreg, &val);
2977 if (ret)
2978 return ret;
2979
2980 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002981 if (invert)
2982 ucontrol->value.integer.value[1] =
2983 max - ucontrol->value.integer.value[1];
2984 ucontrol->value.integer.value[1] =
2985 ucontrol->value.integer.value[1] - min;
2986 }
2987
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002988 return 0;
2989}
2990EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2991
2992/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002993 * snd_soc_limit_volume - Set new limit to an existing volume control.
2994 *
2995 * @codec: where to look for the control
2996 * @name: Name of the control
2997 * @max: new maximum limit
2998 *
2999 * Return 0 for success, else error.
3000 */
3001int snd_soc_limit_volume(struct snd_soc_codec *codec,
3002 const char *name, int max)
3003{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003004 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003005 struct snd_kcontrol *kctl;
3006 struct soc_mixer_control *mc;
3007 int found = 0;
3008 int ret = -EINVAL;
3009
3010 /* Sanity check for name and max */
3011 if (unlikely(!name || max <= 0))
3012 return -EINVAL;
3013
3014 list_for_each_entry(kctl, &card->controls, list) {
3015 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3016 found = 1;
3017 break;
3018 }
3019 }
3020 if (found) {
3021 mc = (struct soc_mixer_control *)kctl->private_value;
3022 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003023 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003024 ret = 0;
3025 }
3026 }
3027 return ret;
3028}
3029EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3030
Mark Brown71d08512011-10-10 18:31:26 +01003031int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
3033{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003034 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003035 struct soc_bytes *params = (void *)kcontrol->private_value;
3036
3037 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003038 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003039
3040 return 0;
3041}
3042EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3043
3044int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3046{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003047 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003048 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003049 int ret;
3050
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003051 if (component->regmap)
3052 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003053 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003054 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003055 else
3056 ret = -EINVAL;
3057
Mark Brownf831b052012-02-17 16:20:33 -08003058 /* Hide any masked bytes to ensure consistent data reporting */
3059 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003060 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003061 case 1:
3062 ucontrol->value.bytes.data[0] &= ~params->mask;
3063 break;
3064 case 2:
3065 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003066 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003067 break;
3068 case 4:
3069 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003070 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003071 break;
3072 default:
3073 return -EINVAL;
3074 }
3075 }
3076
Mark Brown71d08512011-10-10 18:31:26 +01003077 return ret;
3078}
3079EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3080
3081int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3082 struct snd_ctl_elem_value *ucontrol)
3083{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003084 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003085 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003086 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003087 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003088 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003089
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003090 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003091 return -EINVAL;
3092
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003093 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003094
Mark Brownb5a8fe42013-01-20 21:42:22 +09003095 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3096 if (!data)
3097 return -ENOMEM;
3098
Mark Brownf831b052012-02-17 16:20:33 -08003099 /*
3100 * If we've got a mask then we need to preserve the register
3101 * bits. We shouldn't modify the incoming data so take a
3102 * copy.
3103 */
3104 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003105 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003106 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003107 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003108
3109 val &= params->mask;
3110
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003111 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003112 case 1:
3113 ((u8 *)data)[0] &= ~params->mask;
3114 ((u8 *)data)[0] |= val;
3115 break;
3116 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003117 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003118 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003119 &mask, &mask);
3120 if (ret != 0)
3121 goto out;
3122
3123 ((u16 *)data)[0] &= mask;
3124
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003125 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003126 &val, &val);
3127 if (ret != 0)
3128 goto out;
3129
3130 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003131 break;
3132 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003133 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003134 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003135 &mask, &mask);
3136 if (ret != 0)
3137 goto out;
3138
3139 ((u32 *)data)[0] &= mask;
3140
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003141 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003142 &val, &val);
3143 if (ret != 0)
3144 goto out;
3145
3146 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003147 break;
3148 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003149 ret = -EINVAL;
3150 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003151 }
3152 }
3153
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003154 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003155 data, len);
3156
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003157out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003158 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003159
3160 return ret;
3161}
3162EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3163
Vinod Kould9881202014-05-02 10:58:11 +05303164int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3165 struct snd_ctl_elem_info *ucontrol)
3166{
3167 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3168
3169 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3170 ucontrol->count = params->max;
3171
3172 return 0;
3173}
3174EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3175
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303176int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3177 unsigned int size, unsigned int __user *tlv)
3178{
3179 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3180 unsigned int count = size < params->max ? size : params->max;
3181 int ret = -ENXIO;
3182
3183 switch (op_flag) {
3184 case SNDRV_CTL_TLV_OP_READ:
3185 if (params->get)
3186 ret = params->get(tlv, count);
3187 break;
3188 case SNDRV_CTL_TLV_OP_WRITE:
3189 if (params->put)
3190 ret = params->put(tlv, count);
3191 break;
3192 }
3193 return ret;
3194}
3195EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3196
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003197/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003198 * snd_soc_info_xr_sx - signed multi register info callback
3199 * @kcontrol: mreg control
3200 * @uinfo: control element information
3201 *
3202 * Callback to provide information of a control that can
3203 * span multiple codec registers which together
3204 * forms a single signed value in a MSB/LSB manner.
3205 *
3206 * Returns 0 for success.
3207 */
3208int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_info *uinfo)
3210{
3211 struct soc_mreg_control *mc =
3212 (struct soc_mreg_control *)kcontrol->private_value;
3213 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3214 uinfo->count = 1;
3215 uinfo->value.integer.min = mc->min;
3216 uinfo->value.integer.max = mc->max;
3217
3218 return 0;
3219}
3220EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3221
3222/**
3223 * snd_soc_get_xr_sx - signed multi register get callback
3224 * @kcontrol: mreg control
3225 * @ucontrol: control element information
3226 *
3227 * Callback to get the value of a control that can span
3228 * multiple codec registers which together forms a single
3229 * signed value in a MSB/LSB manner. The control supports
3230 * specifying total no of bits used to allow for bitfields
3231 * across the multiple codec registers.
3232 *
3233 * Returns 0 for success.
3234 */
3235int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
3237{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003238 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003239 struct soc_mreg_control *mc =
3240 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003241 unsigned int regbase = mc->regbase;
3242 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003243 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003244 unsigned int regwmask = (1<<regwshift)-1;
3245 unsigned int invert = mc->invert;
3246 unsigned long mask = (1UL<<mc->nbits)-1;
3247 long min = mc->min;
3248 long max = mc->max;
3249 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003250 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003251 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003252 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003253
3254 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003255 ret = snd_soc_component_read(component, regbase+i, &regval);
3256 if (ret)
3257 return ret;
3258 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003259 }
3260 val &= mask;
3261 if (min < 0 && val > max)
3262 val |= ~mask;
3263 if (invert)
3264 val = max - val;
3265 ucontrol->value.integer.value[0] = val;
3266
3267 return 0;
3268}
3269EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3270
3271/**
3272 * snd_soc_put_xr_sx - signed multi register get callback
3273 * @kcontrol: mreg control
3274 * @ucontrol: control element information
3275 *
3276 * Callback to set the value of a control that can span
3277 * multiple codec registers which together forms a single
3278 * signed value in a MSB/LSB manner. The control supports
3279 * specifying total no of bits used to allow for bitfields
3280 * across the multiple codec registers.
3281 *
3282 * Returns 0 for success.
3283 */
3284int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3285 struct snd_ctl_elem_value *ucontrol)
3286{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003287 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003288 struct soc_mreg_control *mc =
3289 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003290 unsigned int regbase = mc->regbase;
3291 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003292 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003293 unsigned int regwmask = (1<<regwshift)-1;
3294 unsigned int invert = mc->invert;
3295 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003296 long max = mc->max;
3297 long val = ucontrol->value.integer.value[0];
3298 unsigned int i, regval, regmask;
3299 int err;
3300
3301 if (invert)
3302 val = max - val;
3303 val &= mask;
3304 for (i = 0; i < regcount; i++) {
3305 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3306 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003307 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003308 regmask, regval);
3309 if (err < 0)
3310 return err;
3311 }
3312
3313 return 0;
3314}
3315EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3316
3317/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003318 * snd_soc_get_strobe - strobe get callback
3319 * @kcontrol: mixer control
3320 * @ucontrol: control element information
3321 *
3322 * Callback get the value of a strobe mixer control.
3323 *
3324 * Returns 0 for success.
3325 */
3326int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_value *ucontrol)
3328{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003329 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003330 struct soc_mixer_control *mc =
3331 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003332 unsigned int reg = mc->reg;
3333 unsigned int shift = mc->shift;
3334 unsigned int mask = 1 << shift;
3335 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003336 unsigned int val;
3337 int ret;
3338
3339 ret = snd_soc_component_read(component, reg, &val);
3340 if (ret)
3341 return ret;
3342
3343 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003344
3345 if (shift != 0 && val != 0)
3346 val = val >> shift;
3347 ucontrol->value.enumerated.item[0] = val ^ invert;
3348
3349 return 0;
3350}
3351EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3352
3353/**
3354 * snd_soc_put_strobe - strobe put callback
3355 * @kcontrol: mixer control
3356 * @ucontrol: control element information
3357 *
3358 * Callback strobe a register bit to high then low (or the inverse)
3359 * in one pass of a single mixer enum control.
3360 *
3361 * Returns 1 for success.
3362 */
3363int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3365{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003366 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003367 struct soc_mixer_control *mc =
3368 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003369 unsigned int reg = mc->reg;
3370 unsigned int shift = mc->shift;
3371 unsigned int mask = 1 << shift;
3372 unsigned int invert = mc->invert != 0;
3373 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3374 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3375 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3376 int err;
3377
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003378 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003379 if (err < 0)
3380 return err;
3381
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003382 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003383}
3384EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3385
3386/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003387 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3388 * @dai: DAI
3389 * @clk_id: DAI specific clock ID
3390 * @freq: new clock frequency in Hz
3391 * @dir: new clock direction - input/output.
3392 *
3393 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3394 */
3395int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3396 unsigned int freq, int dir)
3397{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398 if (dai->driver && dai->driver->ops->set_sysclk)
3399 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003400 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003401 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003402 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003403 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003404 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003405}
3406EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3407
3408/**
Mark Brownec4ee522011-03-07 20:58:11 +00003409 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3410 * @codec: CODEC
3411 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003412 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003413 * @freq: new clock frequency in Hz
3414 * @dir: new clock direction - input/output.
3415 *
3416 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3417 */
3418int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003419 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003420{
3421 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003422 return codec->driver->set_sysclk(codec, clk_id, source,
3423 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003424 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003425 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003426}
3427EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3428
3429/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003430 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3431 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003432 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003433 * @div: new clock divisor.
3434 *
3435 * Configures the clock dividers. This is used to derive the best DAI bit and
3436 * frame clocks from the system or master clock. It's best to set the DAI bit
3437 * and frame clocks as low as possible to save system power.
3438 */
3439int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3440 int div_id, int div)
3441{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003442 if (dai->driver && dai->driver->ops->set_clkdiv)
3443 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003444 else
3445 return -EINVAL;
3446}
3447EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3448
3449/**
3450 * snd_soc_dai_set_pll - configure DAI PLL.
3451 * @dai: DAI
3452 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003453 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003454 * @freq_in: PLL input clock frequency in Hz
3455 * @freq_out: requested PLL output clock frequency in Hz
3456 *
3457 * Configures and enables PLL to generate output clock based on input clock.
3458 */
Mark Brown85488032009-09-05 18:52:16 +01003459int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3460 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003461{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003462 if (dai->driver && dai->driver->ops->set_pll)
3463 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003464 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003465 else if (dai->codec && dai->codec->driver->set_pll)
3466 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3467 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 else
3469 return -EINVAL;
3470}
3471EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3472
Mark Brownec4ee522011-03-07 20:58:11 +00003473/*
3474 * snd_soc_codec_set_pll - configure codec PLL.
3475 * @codec: CODEC
3476 * @pll_id: DAI specific PLL ID
3477 * @source: DAI specific source for the PLL
3478 * @freq_in: PLL input clock frequency in Hz
3479 * @freq_out: requested PLL output clock frequency in Hz
3480 *
3481 * Configures and enables PLL to generate output clock based on input clock.
3482 */
3483int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3484 unsigned int freq_in, unsigned int freq_out)
3485{
3486 if (codec->driver->set_pll)
3487 return codec->driver->set_pll(codec, pll_id, source,
3488 freq_in, freq_out);
3489 else
3490 return -EINVAL;
3491}
3492EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3493
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003494/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003495 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3496 * @dai: DAI
3497 * @ratio Ratio of BCLK to Sample rate.
3498 *
3499 * Configures the DAI for a preset BCLK to sample rate ratio.
3500 */
3501int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3502{
3503 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3504 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3505 else
3506 return -EINVAL;
3507}
3508EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3509
3510/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003511 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3512 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003513 * @fmt: SND_SOC_DAIFMT_ format value.
3514 *
3515 * Configures the DAI hardware format and clocking.
3516 */
3517int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3518{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003519 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003520 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003521 if (dai->driver->ops->set_fmt == NULL)
3522 return -ENOTSUPP;
3523 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003524}
3525EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3526
3527/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003528 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003529 * @slots: Number of slots in use.
3530 * @tx_mask: bitmask representing active TX slots.
3531 * @rx_mask: bitmask representing active RX slots.
3532 *
3533 * Generates the TDM tx and rx slot default masks for DAI.
3534 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003535static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003536 unsigned int *tx_mask,
3537 unsigned int *rx_mask)
3538{
3539 if (*tx_mask || *rx_mask)
3540 return 0;
3541
3542 if (!slots)
3543 return -EINVAL;
3544
3545 *tx_mask = (1 << slots) - 1;
3546 *rx_mask = (1 << slots) - 1;
3547
3548 return 0;
3549}
3550
3551/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003552 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3553 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003554 * @tx_mask: bitmask representing active TX slots.
3555 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003557 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003558 *
3559 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3560 * specific.
3561 */
3562int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003563 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003564{
Xiubo Lie5c21512014-03-21 14:17:12 +08003565 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3566 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003567 &tx_mask, &rx_mask);
3568 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003569 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003570
Benoit Cousson88bd8702014-07-08 23:19:34 +02003571 dai->tx_mask = tx_mask;
3572 dai->rx_mask = rx_mask;
3573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003574 if (dai->driver && dai->driver->ops->set_tdm_slot)
3575 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003576 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003577 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003578 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003579}
3580EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3581
3582/**
Barry Song472df3c2009-09-12 01:16:29 +08003583 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3584 * @dai: DAI
3585 * @tx_num: how many TX channels
3586 * @tx_slot: pointer to an array which imply the TX slot number channel
3587 * 0~num-1 uses
3588 * @rx_num: how many RX channels
3589 * @rx_slot: pointer to an array which imply the RX slot number channel
3590 * 0~num-1 uses
3591 *
3592 * configure the relationship between channel number and TDM slot number.
3593 */
3594int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3595 unsigned int tx_num, unsigned int *tx_slot,
3596 unsigned int rx_num, unsigned int *rx_slot)
3597{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003598 if (dai->driver && dai->driver->ops->set_channel_map)
3599 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003600 rx_num, rx_slot);
3601 else
3602 return -EINVAL;
3603}
3604EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3605
3606/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003607 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3608 * @dai: DAI
3609 * @tristate: tristate enable
3610 *
3611 * Tristates the DAI so that others can use it.
3612 */
3613int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3614{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003615 if (dai->driver && dai->driver->ops->set_tristate)
3616 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003617 else
3618 return -EINVAL;
3619}
3620EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3621
3622/**
3623 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3624 * @dai: DAI
3625 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003626 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003627 *
3628 * Mutes the DAI DAC.
3629 */
Mark Brownda183962013-02-06 15:44:07 +00003630int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3631 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003632{
Mark Brownda183962013-02-06 15:44:07 +00003633 if (!dai->driver)
3634 return -ENOTSUPP;
3635
3636 if (dai->driver->ops->mute_stream)
3637 return dai->driver->ops->mute_stream(dai, mute, direction);
3638 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3639 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003640 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003641 else
Mark Brown04570c62012-04-13 19:16:03 +01003642 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003643}
3644EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3645
Benoit Cousson88bd8702014-07-08 23:19:34 +02003646static int snd_soc_init_multicodec(struct snd_soc_card *card,
3647 struct snd_soc_dai_link *dai_link)
3648{
3649 /* Legacy codec/codec_dai link is a single entry in multicodec */
3650 if (dai_link->codec_name || dai_link->codec_of_node ||
3651 dai_link->codec_dai_name) {
3652 dai_link->num_codecs = 1;
3653
3654 dai_link->codecs = devm_kzalloc(card->dev,
3655 sizeof(struct snd_soc_dai_link_component),
3656 GFP_KERNEL);
3657 if (!dai_link->codecs)
3658 return -ENOMEM;
3659
3660 dai_link->codecs[0].name = dai_link->codec_name;
3661 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3662 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3663 }
3664
3665 if (!dai_link->codecs) {
3666 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3667 return -EINVAL;
3668 }
3669
3670 return 0;
3671}
3672
Mark Brownc5af3a22008-11-28 13:29:45 +00003673/**
3674 * snd_soc_register_card - Register a card with the ASoC core
3675 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003676 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003677 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003678 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303679int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003680{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003681 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682
Mark Brownc5af3a22008-11-28 13:29:45 +00003683 if (!card->name || !card->dev)
3684 return -EINVAL;
3685
Stephen Warren5a504962011-12-21 10:40:59 -07003686 for (i = 0; i < card->num_links; i++) {
3687 struct snd_soc_dai_link *link = &card->dai_link[i];
3688
Benoit Cousson88bd8702014-07-08 23:19:34 +02003689 ret = snd_soc_init_multicodec(card, link);
3690 if (ret) {
3691 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3692 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003693 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003694
3695 for (j = 0; j < link->num_codecs; j++) {
3696 /*
3697 * Codec must be specified by 1 of name or OF node,
3698 * not both or neither.
3699 */
3700 if (!!link->codecs[j].name ==
3701 !!link->codecs[j].of_node) {
3702 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3703 link->name);
3704 return -EINVAL;
3705 }
3706 /* Codec DAI name must be specified */
3707 if (!link->codecs[j].dai_name) {
3708 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3709 link->name);
3710 return -EINVAL;
3711 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003712 }
Stephen Warren5a504962011-12-21 10:40:59 -07003713
3714 /*
3715 * Platform may be specified by either name or OF node, but
3716 * can be left unspecified, and a dummy platform will be used.
3717 */
3718 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003719 dev_err(card->dev,
3720 "ASoC: Both platform name/of_node are set for %s\n",
3721 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003722 return -EINVAL;
3723 }
3724
3725 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003726 * CPU device may be specified by either name or OF node, but
3727 * can be left unspecified, and will be matched based on DAI
3728 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003729 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003730 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003731 dev_err(card->dev,
3732 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3733 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003734 return -EINVAL;
3735 }
3736 /*
3737 * At least one of CPU DAI name or CPU device name/node must be
3738 * specified
3739 */
3740 if (!link->cpu_dai_name &&
3741 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003742 dev_err(card->dev,
3743 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3744 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003745 return -EINVAL;
3746 }
3747 }
3748
Mark Browned77cc12011-05-03 18:25:34 +01003749 dev_set_drvdata(card->dev, card);
3750
Stephen Warren111c6412011-01-28 14:26:35 -07003751 snd_soc_initialize_card_lists(card);
3752
Vinod Koul150dd2f2011-01-13 22:48:32 +05303753 soc_init_card_debugfs(card);
3754
Mark Brown181a6892012-03-14 20:18:49 +00003755 card->rtd = devm_kzalloc(card->dev,
3756 sizeof(struct snd_soc_pcm_runtime) *
3757 (card->num_links + card->num_aux_devs),
3758 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003759 if (card->rtd == NULL)
3760 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003761 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003762 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003763
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003764 for (i = 0; i < card->num_links; i++) {
3765 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003766 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003767 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3768 sizeof(struct snd_soc_dai *) *
3769 (card->rtd[i].dai_link->num_codecs),
3770 GFP_KERNEL);
3771 if (card->rtd[i].codec_dais == NULL)
3772 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003773 }
3774
3775 for (i = 0; i < card->num_aux_devs; i++)
3776 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003777
Mark Browndb432b42011-10-03 21:06:40 +01003778 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003779 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003780 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003781 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003782
Mark Brownb19e6e72012-03-14 21:18:39 +00003783 ret = snd_soc_instantiate_card(card);
3784 if (ret != 0)
3785 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003786
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003787 /* deactivate pins to sleep state */
3788 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003789 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3790 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3791 int j;
3792
3793 for (j = 0; j < rtd->num_codecs; j++) {
3794 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3795 if (!codec_dai->active)
3796 pinctrl_pm_select_sleep_state(codec_dai->dev);
3797 }
3798
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003799 if (!cpu_dai->active)
3800 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3801 }
3802
Mark Brownb19e6e72012-03-14 21:18:39 +00003803 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003804}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303805EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003806
3807/**
3808 * snd_soc_unregister_card - Unregister a card with the ASoC core
3809 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003810 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003811 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003812 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303813int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003814{
Vinod Koulb0e26482011-01-13 22:48:02 +05303815 if (card->instantiated)
3816 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003817 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003818
3819 return 0;
3820}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303821EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003822
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003823/*
3824 * Simplify DAI link configuration by removing ".-1" from device names
3825 * and sanitizing names.
3826 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003827static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003828{
3829 char *found, name[NAME_SIZE];
3830 int id1, id2;
3831
3832 if (dev_name(dev) == NULL)
3833 return NULL;
3834
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003835 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003836
3837 /* are we a "%s.%d" name (platform and SPI components) */
3838 found = strstr(name, dev->driver->name);
3839 if (found) {
3840 /* get ID */
3841 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3842
3843 /* discard ID from name if ID == -1 */
3844 if (*id == -1)
3845 found[strlen(dev->driver->name)] = '\0';
3846 }
3847
3848 } else {
3849 /* I2C component devices are named "bus-addr" */
3850 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3851 char tmp[NAME_SIZE];
3852
3853 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003854 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003855
3856 /* sanitize component name for DAI link creation */
3857 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003858 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859 } else
3860 *id = 0;
3861 }
3862
3863 return kstrdup(name, GFP_KERNEL);
3864}
3865
3866/*
3867 * Simplify DAI link naming for single devices with multiple DAIs by removing
3868 * any ".-1" and using the DAI name (instead of device name).
3869 */
3870static inline char *fmt_multiple_name(struct device *dev,
3871 struct snd_soc_dai_driver *dai_drv)
3872{
3873 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003874 dev_err(dev,
3875 "ASoC: error - multiple DAI %s registered with no name\n",
3876 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003877 return NULL;
3878 }
3879
3880 return kstrdup(dai_drv->name, GFP_KERNEL);
3881}
3882
Mark Brown91151712008-11-30 23:31:24 +00003883/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003884 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003885 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003886 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003887 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003888static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003889{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003890 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003891
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003892 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003893 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3894 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003895 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003896 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003898 }
Mark Brown91151712008-11-30 23:31:24 +00003899}
Mark Brown91151712008-11-30 23:31:24 +00003900
3901/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003902 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003903 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003904 * @component: The component the DAIs are registered for
3905 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003906 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003907 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3908 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003909 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003910static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003911 struct snd_soc_dai_driver *dai_drv, size_t count,
3912 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003913{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003914 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003916 unsigned int i;
3917 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003918
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003919 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003920
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003921 component->dai_drv = dai_drv;
3922 component->num_dai = count;
3923
Mark Brown91151712008-11-30 23:31:24 +00003924 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925
3926 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003927 if (dai == NULL) {
3928 ret = -ENOMEM;
3929 goto err;
3930 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003931
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003932 /*
3933 * Back in the old days when we still had component-less DAIs,
3934 * instead of having a static name, component-less DAIs would
3935 * inherit the name of the parent device so it is possible to
3936 * register multiple instances of the DAI. We still need to keep
3937 * the same naming style even though those DAIs are not
3938 * component-less anymore.
3939 */
3940 if (count == 1 && legacy_dai_naming) {
3941 dai->name = fmt_single_name(dev, &dai->id);
3942 } else {
3943 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3944 if (dai_drv[i].id)
3945 dai->id = dai_drv[i].id;
3946 else
3947 dai->id = i;
3948 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003949 if (dai->name == NULL) {
3950 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003951 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003952 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 }
3954
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003955 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 dai->driver = &dai_drv[i];
3958 if (!dai->driver->ops)
3959 dai->driver->ops = &null_dai_ops;
3960
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003961 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003962
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003963 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003964 }
3965
3966 return 0;
3967
3968err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003969 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003970
3971 return ret;
3972}
Mark Brown91151712008-11-30 23:31:24 +00003973
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003974static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3975 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003976{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003977 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003978
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003979 component->driver->seq_notifier(component, type, subseq);
3980}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003981
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003982static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3983 int event)
3984{
3985 struct snd_soc_component *component = dapm->component;
3986
3987 return component->driver->stream_event(component, event);
3988}
3989
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003990static int snd_soc_component_initialize(struct snd_soc_component *component,
3991 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003992{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003993 struct snd_soc_dapm_context *dapm;
3994
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003995 component->name = fmt_single_name(dev, &component->id);
3996 if (!component->name) {
3997 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003998 return -ENOMEM;
3999 }
4000
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004001 component->dev = dev;
4002 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004003 component->probe = component->driver->probe;
4004 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004005
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004006 if (!component->dapm_ptr)
4007 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004008
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004009 dapm = component->dapm_ptr;
4010 dapm->dev = dev;
4011 dapm->component = component;
4012 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004013 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004014 if (driver->seq_notifier)
4015 dapm->seq_notifier = snd_soc_component_seq_notifier;
4016 if (driver->stream_event)
4017 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004018
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004019 component->controls = driver->controls;
4020 component->num_controls = driver->num_controls;
4021 component->dapm_widgets = driver->dapm_widgets;
4022 component->num_dapm_widgets = driver->num_dapm_widgets;
4023 component->dapm_routes = driver->dapm_routes;
4024 component->num_dapm_routes = driver->num_dapm_routes;
4025
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004026 INIT_LIST_HEAD(&component->dai_list);
4027 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004028
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004029 return 0;
4030}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004031
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004032static void snd_soc_component_init_regmap(struct snd_soc_component *component)
4033{
4034 if (!component->regmap)
4035 component->regmap = dev_get_regmap(component->dev, NULL);
4036 if (component->regmap) {
4037 int val_bytes = regmap_get_val_bytes(component->regmap);
4038 /* Errors are legitimate for non-integer byte multiples */
4039 if (val_bytes > 0)
4040 component->val_bytes = val_bytes;
4041 }
4042}
4043
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004044static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4045{
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004046 if (!component->write && !component->read)
4047 snd_soc_component_init_regmap(component);
4048
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004049 list_add(&component->list, &component_list);
4050}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004051
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004052static void snd_soc_component_add(struct snd_soc_component *component)
4053{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004054 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004055 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004056 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004057}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004058
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004059static void snd_soc_component_cleanup(struct snd_soc_component *component)
4060{
4061 snd_soc_unregister_dais(component);
4062 kfree(component->name);
4063}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004064
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004065static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4066{
4067 list_del(&component->list);
4068}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004069
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004070static void snd_soc_component_del(struct snd_soc_component *component)
4071{
4072 mutex_lock(&client_mutex);
4073 snd_soc_component_del_unlocked(component);
4074 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004075}
4076
4077int snd_soc_register_component(struct device *dev,
4078 const struct snd_soc_component_driver *cmpnt_drv,
4079 struct snd_soc_dai_driver *dai_drv,
4080 int num_dai)
4081{
4082 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004083 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004084
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004085 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004086 if (!cmpnt) {
4087 dev_err(dev, "ASoC: Failed to allocate memory\n");
4088 return -ENOMEM;
4089 }
4090
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004091 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4092 if (ret)
4093 goto err_free;
4094
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004095 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004096 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004098 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4099 if (ret < 0) {
4100 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4101 goto err_cleanup;
4102 }
4103
4104 snd_soc_component_add(cmpnt);
4105
4106 return 0;
4107
4108err_cleanup:
4109 snd_soc_component_cleanup(cmpnt);
4110err_free:
4111 kfree(cmpnt);
4112 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004113}
4114EXPORT_SYMBOL_GPL(snd_soc_register_component);
4115
4116/**
4117 * snd_soc_unregister_component - Unregister a component from the ASoC core
4118 *
4119 */
4120void snd_soc_unregister_component(struct device *dev)
4121{
4122 struct snd_soc_component *cmpnt;
4123
4124 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004125 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004126 goto found;
4127 }
4128 return;
4129
4130found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004131 snd_soc_component_del(cmpnt);
4132 snd_soc_component_cleanup(cmpnt);
4133 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004134}
4135EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4136
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004137static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4138{
4139 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4140
4141 return platform->driver->probe(platform);
4142}
4143
4144static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4145{
4146 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4147
4148 platform->driver->remove(platform);
4149}
4150
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004151/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004152 * snd_soc_add_platform - Add a platform to the ASoC core
4153 * @dev: The parent device for the platform
4154 * @platform: The platform to add
4155 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004156 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004157int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004158 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004159{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004160 int ret;
4161
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004162 ret = snd_soc_component_initialize(&platform->component,
4163 &platform_drv->component_driver, dev);
4164 if (ret)
4165 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004166
4167 platform->dev = dev;
4168 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004169 if (platform_drv->controls) {
4170 platform->component.controls = platform_drv->controls;
4171 platform->component.num_controls = platform_drv->num_controls;
4172 }
4173 if (platform_drv->dapm_widgets) {
4174 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4175 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4176 platform->component.steal_sibling_dai_widgets = true;
4177 }
4178 if (platform_drv->dapm_routes) {
4179 platform->component.dapm_routes = platform_drv->dapm_routes;
4180 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4181 }
4182
4183 if (platform_drv->probe)
4184 platform->component.probe = snd_soc_platform_drv_probe;
4185 if (platform_drv->remove)
4186 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004187
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004188#ifdef CONFIG_DEBUG_FS
4189 platform->component.debugfs_prefix = "platform";
4190#endif
4191
Mark Brown12a48a8c2008-12-03 19:40:30 +00004192 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004193 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004194 list_add(&platform->list, &platform_list);
4195 mutex_unlock(&client_mutex);
4196
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004197 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4198 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004199
4200 return 0;
4201}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004202EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4203
4204/**
4205 * snd_soc_register_platform - Register a platform with the ASoC core
4206 *
4207 * @platform: platform to register
4208 */
4209int snd_soc_register_platform(struct device *dev,
4210 const struct snd_soc_platform_driver *platform_drv)
4211{
4212 struct snd_soc_platform *platform;
4213 int ret;
4214
4215 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4216
4217 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4218 if (platform == NULL)
4219 return -ENOMEM;
4220
4221 ret = snd_soc_add_platform(dev, platform, platform_drv);
4222 if (ret)
4223 kfree(platform);
4224
4225 return ret;
4226}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004227EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4228
4229/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004230 * snd_soc_remove_platform - Remove a platform from the ASoC core
4231 * @platform: the platform to remove
4232 */
4233void snd_soc_remove_platform(struct snd_soc_platform *platform)
4234{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004235
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004236 mutex_lock(&client_mutex);
4237 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004238 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004239 mutex_unlock(&client_mutex);
4240
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004241 snd_soc_component_cleanup(&platform->component);
4242
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004243 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004244 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004245}
4246EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4247
4248struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4249{
4250 struct snd_soc_platform *platform;
4251
4252 list_for_each_entry(platform, &platform_list, list) {
4253 if (dev == platform->dev)
4254 return platform;
4255 }
4256
4257 return NULL;
4258}
4259EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4260
4261/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004262 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4263 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004264 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004265 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004266void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004267{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004268 struct snd_soc_platform *platform;
4269
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004270 platform = snd_soc_lookup_platform(dev);
4271 if (!platform)
4272 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004273
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004274 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004275 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004276}
4277EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4278
Mark Brown151ab222009-05-09 16:22:58 +01004279static u64 codec_format_map[] = {
4280 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4281 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4282 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4283 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4284 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4285 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4286 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4287 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4288 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4289 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4290 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4291 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4292 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4293 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4294 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4295 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4296};
4297
4298/* Fix up the DAI formats for endianness: codecs don't actually see
4299 * the endianness of the data but we're using the CPU format
4300 * definitions which do need to include endianness so we ensure that
4301 * codec DAIs always have both big and little endian variants set.
4302 */
4303static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4304{
4305 int i;
4306
4307 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4308 if (stream->formats & codec_format_map[i])
4309 stream->formats |= codec_format_map[i];
4310}
4311
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004312static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4313{
4314 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4315
4316 return codec->driver->probe(codec);
4317}
4318
4319static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4320{
4321 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4322
4323 codec->driver->remove(codec);
4324}
4325
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004326static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4327 unsigned int reg, unsigned int val)
4328{
4329 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4330
4331 return codec->driver->write(codec, reg, val);
4332}
4333
4334static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4335 unsigned int reg, unsigned int *val)
4336{
4337 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4338
4339 *val = codec->driver->read(codec, reg);
4340
4341 return 0;
4342}
4343
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004344static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4345 enum snd_soc_bias_level level)
4346{
4347 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4348
4349 return codec->driver->set_bias_level(codec, level);
4350}
4351
Mark Brown0d0cf002008-12-10 14:32:45 +00004352/**
4353 * snd_soc_register_codec - Register a codec with the ASoC core
4354 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004355 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004356 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004357int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004358 const struct snd_soc_codec_driver *codec_drv,
4359 struct snd_soc_dai_driver *dai_drv,
4360 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004361{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004362 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004363 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004364 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004365
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004366 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004367
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004368 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4369 if (codec == NULL)
4370 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004371
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004372 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004373 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004374
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004375 ret = snd_soc_component_initialize(&codec->component,
4376 &codec_drv->component_driver, dev);
4377 if (ret)
4378 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004379
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004380 if (codec_drv->controls) {
4381 codec->component.controls = codec_drv->controls;
4382 codec->component.num_controls = codec_drv->num_controls;
4383 }
4384 if (codec_drv->dapm_widgets) {
4385 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4386 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4387 }
4388 if (codec_drv->dapm_routes) {
4389 codec->component.dapm_routes = codec_drv->dapm_routes;
4390 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4391 }
4392
4393 if (codec_drv->probe)
4394 codec->component.probe = snd_soc_codec_drv_probe;
4395 if (codec_drv->remove)
4396 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004397 if (codec_drv->write)
4398 codec->component.write = snd_soc_codec_drv_write;
4399 if (codec_drv->read)
4400 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004401 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004402 codec->dapm.codec = codec;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004403 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004404 if (codec_drv->seq_notifier)
4405 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004406 if (codec_drv->set_bias_level)
4407 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004408 codec->dev = dev;
4409 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004410 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004411 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004412
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004413#ifdef CONFIG_DEBUG_FS
4414 codec->component.init_debugfs = soc_init_codec_debugfs;
4415 codec->component.debugfs_prefix = "codec";
4416#endif
4417
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004418 if (codec_drv->get_regmap)
4419 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08004420
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004421 for (i = 0; i < num_dai; i++) {
4422 fixup_codec_formats(&dai_drv[i].playback);
4423 fixup_codec_formats(&dai_drv[i].capture);
4424 }
4425
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004426 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4427 if (ret < 0) {
4428 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4429 goto err_cleanup;
4430 }
4431
4432 list_for_each_entry(dai, &codec->component.dai_list, list)
4433 dai->codec = codec;
4434
Mark Brown054880f2012-04-09 17:29:19 +01004435 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004436 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004437 list_add(&codec->list, &codec_list);
4438 mutex_unlock(&client_mutex);
4439
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004440 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4441 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004442 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004443
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004444err_cleanup:
4445 snd_soc_component_cleanup(&codec->component);
4446err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004447 kfree(codec);
4448 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004449}
4450EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4451
4452/**
4453 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4454 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004455 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004456 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004457void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004458{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004459 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004460
4461 list_for_each_entry(codec, &codec_list, list) {
4462 if (dev == codec->dev)
4463 goto found;
4464 }
4465 return;
4466
4467found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004468
Mark Brown0d0cf002008-12-10 14:32:45 +00004469 mutex_lock(&client_mutex);
4470 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004471 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004472 mutex_unlock(&client_mutex);
4473
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004474 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4475 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004476
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004477 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004478 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004479 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004480}
4481EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4482
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004483/* Retrieve a card's name from device tree */
4484int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4485 const char *propname)
4486{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304487 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004488 int ret;
4489
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304490 if (!card->dev) {
4491 pr_err("card->dev is not set before calling %s\n", __func__);
4492 return -EINVAL;
4493 }
4494
4495 np = card->dev->of_node;
4496
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004497 ret = of_property_read_string_index(np, propname, 0, &card->name);
4498 /*
4499 * EINVAL means the property does not exist. This is fine providing
4500 * card->name was previously set, which is checked later in
4501 * snd_soc_register_card.
4502 */
4503 if (ret < 0 && ret != -EINVAL) {
4504 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004505 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004506 propname, ret);
4507 return ret;
4508 }
4509
4510 return 0;
4511}
4512EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4513
Xiubo Li9a6d4862014-02-08 15:59:52 +08004514static const struct snd_soc_dapm_widget simple_widgets[] = {
4515 SND_SOC_DAPM_MIC("Microphone", NULL),
4516 SND_SOC_DAPM_LINE("Line", NULL),
4517 SND_SOC_DAPM_HP("Headphone", NULL),
4518 SND_SOC_DAPM_SPK("Speaker", NULL),
4519};
4520
4521int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4522 const char *propname)
4523{
4524 struct device_node *np = card->dev->of_node;
4525 struct snd_soc_dapm_widget *widgets;
4526 const char *template, *wname;
4527 int i, j, num_widgets, ret;
4528
4529 num_widgets = of_property_count_strings(np, propname);
4530 if (num_widgets < 0) {
4531 dev_err(card->dev,
4532 "ASoC: Property '%s' does not exist\n", propname);
4533 return -EINVAL;
4534 }
4535 if (num_widgets & 1) {
4536 dev_err(card->dev,
4537 "ASoC: Property '%s' length is not even\n", propname);
4538 return -EINVAL;
4539 }
4540
4541 num_widgets /= 2;
4542 if (!num_widgets) {
4543 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4544 propname);
4545 return -EINVAL;
4546 }
4547
4548 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4549 GFP_KERNEL);
4550 if (!widgets) {
4551 dev_err(card->dev,
4552 "ASoC: Could not allocate memory for widgets\n");
4553 return -ENOMEM;
4554 }
4555
4556 for (i = 0; i < num_widgets; i++) {
4557 ret = of_property_read_string_index(np, propname,
4558 2 * i, &template);
4559 if (ret) {
4560 dev_err(card->dev,
4561 "ASoC: Property '%s' index %d read error:%d\n",
4562 propname, 2 * i, ret);
4563 return -EINVAL;
4564 }
4565
4566 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4567 if (!strncmp(template, simple_widgets[j].name,
4568 strlen(simple_widgets[j].name))) {
4569 widgets[i] = simple_widgets[j];
4570 break;
4571 }
4572 }
4573
4574 if (j >= ARRAY_SIZE(simple_widgets)) {
4575 dev_err(card->dev,
4576 "ASoC: DAPM widget '%s' is not supported\n",
4577 template);
4578 return -EINVAL;
4579 }
4580
4581 ret = of_property_read_string_index(np, propname,
4582 (2 * i) + 1,
4583 &wname);
4584 if (ret) {
4585 dev_err(card->dev,
4586 "ASoC: Property '%s' index %d read error:%d\n",
4587 propname, (2 * i) + 1, ret);
4588 return -EINVAL;
4589 }
4590
4591 widgets[i].name = wname;
4592 }
4593
4594 card->dapm_widgets = widgets;
4595 card->num_dapm_widgets = num_widgets;
4596
4597 return 0;
4598}
4599EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4600
Xiubo Li89c67852014-02-14 09:34:35 +08004601int snd_soc_of_parse_tdm_slot(struct device_node *np,
4602 unsigned int *slots,
4603 unsigned int *slot_width)
4604{
4605 u32 val;
4606 int ret;
4607
4608 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4609 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4610 if (ret)
4611 return ret;
4612
4613 if (slots)
4614 *slots = val;
4615 }
4616
4617 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4618 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4619 if (ret)
4620 return ret;
4621
4622 if (slot_width)
4623 *slot_width = val;
4624 }
4625
4626 return 0;
4627}
4628EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4629
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004630int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4631 const char *propname)
4632{
4633 struct device_node *np = card->dev->of_node;
4634 int num_routes;
4635 struct snd_soc_dapm_route *routes;
4636 int i, ret;
4637
4638 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004639 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004640 dev_err(card->dev,
4641 "ASoC: Property '%s' does not exist or its length is not even\n",
4642 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004643 return -EINVAL;
4644 }
4645 num_routes /= 2;
4646 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004647 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004648 propname);
4649 return -EINVAL;
4650 }
4651
4652 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4653 GFP_KERNEL);
4654 if (!routes) {
4655 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004656 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004657 return -EINVAL;
4658 }
4659
4660 for (i = 0; i < num_routes; i++) {
4661 ret = of_property_read_string_index(np, propname,
4662 2 * i, &routes[i].sink);
4663 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004664 dev_err(card->dev,
4665 "ASoC: Property '%s' index %d could not be read: %d\n",
4666 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004667 return -EINVAL;
4668 }
4669 ret = of_property_read_string_index(np, propname,
4670 (2 * i) + 1, &routes[i].source);
4671 if (ret) {
4672 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004673 "ASoC: Property '%s' index %d could not be read: %d\n",
4674 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004675 return -EINVAL;
4676 }
4677 }
4678
4679 card->num_dapm_routes = num_routes;
4680 card->dapm_routes = routes;
4681
4682 return 0;
4683}
4684EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4685
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004686unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004687 const char *prefix,
4688 struct device_node **bitclkmaster,
4689 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004690{
4691 int ret, i;
4692 char prop[128];
4693 unsigned int format = 0;
4694 int bit, frame;
4695 const char *str;
4696 struct {
4697 char *name;
4698 unsigned int val;
4699 } of_fmt_table[] = {
4700 { "i2s", SND_SOC_DAIFMT_I2S },
4701 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4702 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4703 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4704 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4705 { "ac97", SND_SOC_DAIFMT_AC97 },
4706 { "pdm", SND_SOC_DAIFMT_PDM},
4707 { "msb", SND_SOC_DAIFMT_MSB },
4708 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004709 };
4710
4711 if (!prefix)
4712 prefix = "";
4713
4714 /*
4715 * check "[prefix]format = xxx"
4716 * SND_SOC_DAIFMT_FORMAT_MASK area
4717 */
4718 snprintf(prop, sizeof(prop), "%sformat", prefix);
4719 ret = of_property_read_string(np, prop, &str);
4720 if (ret == 0) {
4721 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4722 if (strcmp(str, of_fmt_table[i].name) == 0) {
4723 format |= of_fmt_table[i].val;
4724 break;
4725 }
4726 }
4727 }
4728
4729 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004730 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004731 * SND_SOC_DAIFMT_CLOCK_MASK area
4732 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004733 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4734 if (of_get_property(np, prop, NULL))
4735 format |= SND_SOC_DAIFMT_CONT;
4736 else
4737 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004738
4739 /*
4740 * check "[prefix]bitclock-inversion"
4741 * check "[prefix]frame-inversion"
4742 * SND_SOC_DAIFMT_INV_MASK area
4743 */
4744 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4745 bit = !!of_get_property(np, prop, NULL);
4746
4747 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4748 frame = !!of_get_property(np, prop, NULL);
4749
4750 switch ((bit << 4) + frame) {
4751 case 0x11:
4752 format |= SND_SOC_DAIFMT_IB_IF;
4753 break;
4754 case 0x10:
4755 format |= SND_SOC_DAIFMT_IB_NF;
4756 break;
4757 case 0x01:
4758 format |= SND_SOC_DAIFMT_NB_IF;
4759 break;
4760 default:
4761 /* SND_SOC_DAIFMT_NB_NF is default */
4762 break;
4763 }
4764
4765 /*
4766 * check "[prefix]bitclock-master"
4767 * check "[prefix]frame-master"
4768 * SND_SOC_DAIFMT_MASTER_MASK area
4769 */
4770 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4771 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004772 if (bit && bitclkmaster)
4773 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004774
4775 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4776 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004777 if (frame && framemaster)
4778 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004779
4780 switch ((bit << 4) + frame) {
4781 case 0x11:
4782 format |= SND_SOC_DAIFMT_CBM_CFM;
4783 break;
4784 case 0x10:
4785 format |= SND_SOC_DAIFMT_CBM_CFS;
4786 break;
4787 case 0x01:
4788 format |= SND_SOC_DAIFMT_CBS_CFM;
4789 break;
4790 default:
4791 format |= SND_SOC_DAIFMT_CBS_CFS;
4792 break;
4793 }
4794
4795 return format;
4796}
4797EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4798
Kuninori Morimotocb470082013-09-10 17:39:56 -07004799int snd_soc_of_get_dai_name(struct device_node *of_node,
4800 const char **dai_name)
4801{
4802 struct snd_soc_component *pos;
4803 struct of_phandle_args args;
4804 int ret;
4805
4806 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4807 "#sound-dai-cells", 0, &args);
4808 if (ret)
4809 return ret;
4810
4811 ret = -EPROBE_DEFER;
4812
4813 mutex_lock(&client_mutex);
4814 list_for_each_entry(pos, &component_list, list) {
4815 if (pos->dev->of_node != args.np)
4816 continue;
4817
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004818 if (pos->driver->of_xlate_dai_name) {
4819 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4820 } else {
4821 int id = -1;
4822
4823 switch (args.args_count) {
4824 case 0:
4825 id = 0; /* same as dai_drv[0] */
4826 break;
4827 case 1:
4828 id = args.args[0];
4829 break;
4830 default:
4831 /* not supported */
4832 break;
4833 }
4834
4835 if (id < 0 || id >= pos->num_dai) {
4836 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004837 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004838 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004839
4840 ret = 0;
4841
4842 *dai_name = pos->dai_drv[id].name;
4843 if (!*dai_name)
4844 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004845 }
4846
Kuninori Morimotocb470082013-09-10 17:39:56 -07004847 break;
4848 }
4849 mutex_unlock(&client_mutex);
4850
4851 of_node_put(args.np);
4852
4853 return ret;
4854}
4855EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4856
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004857static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004858{
Mark Brown384c89e2008-12-03 17:34:03 +00004859#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004860 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4861 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004862 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004863 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004864 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004865
Mark Brown8a9dab12011-01-10 22:25:21 +00004866 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004867 &codec_list_fops))
4868 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4869
Mark Brown8a9dab12011-01-10 22:25:21 +00004870 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004871 &dai_list_fops))
4872 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004873
Mark Brown8a9dab12011-01-10 22:25:21 +00004874 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004875 &platform_list_fops))
4876 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004877#endif
4878
Mark Brownfb257892011-04-28 10:57:54 +01004879 snd_soc_util_init();
4880
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004881 return platform_driver_register(&soc_driver);
4882}
Mark Brown4abe8e12010-10-12 17:41:03 +01004883module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004884
Mark Brown7d8c16a2008-11-30 22:11:24 +00004885static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004886{
Mark Brownfb257892011-04-28 10:57:54 +01004887 snd_soc_util_exit();
4888
Mark Brown384c89e2008-12-03 17:34:03 +00004889#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004890 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004891#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004892 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004893}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004894module_exit(snd_soc_exit);
4895
4896/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004897MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004898MODULE_DESCRIPTION("ALSA SoC Core");
4899MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004900MODULE_ALIAS("platform:soc-audio");