blob: 140f43f9163570c8c7efc1186a40903800019181 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000315 &codec->cache_only);
316
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200318 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000323}
324
Mark Brownc3c5a192010-09-15 18:15:14 +0100325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200337 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
Mark Brownf3208782010-09-15 18:19:07 +0100359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100364 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 }
381 }
Mark Brownf3208782010-09-15 18:19:07 +0100382
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
Mark Brown19c7ac22010-09-15 18:22:40 +0100395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200408 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
Mark Brown2b194f9d2010-10-13 10:52:16 +0100417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000432 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200433 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100435 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000444 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#else
453
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200454#define soc_init_codec_debugfs NULL
455
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000458{
459}
460
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463{
464}
465
Axel Linb95fccb2010-11-09 17:06:44 +0800466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200522 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 codec->ac97->dev.release = soc_ac97_device_release;
524
Kay Sieversbb072bf2008-11-02 03:50:35 +0100525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 continue;
576
Benoit Cousson88bd8702014-07-08 23:19:34 +0200577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100586 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100589 continue;
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 /* close any waiting streams and save state */
613 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623
624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 }
635
Mark Browne2d32ff2012-08-31 17:38:32 -0700636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200645 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200655 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000656 break;
657 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100659 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900661 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 }
672 }
673 }
674
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
677
678 if (card->rtd[i].dai_link->ignore_suspend)
679 continue;
680
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 }
687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
691 return 0;
692}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000693EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Andy Green6ed25972008-06-13 16:24:05 +0100695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Andy Green6ed25972008-06-13 16:24:05 +0100705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100710
Mark Brown99497882010-05-07 20:24:05 +0100711 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100722 continue;
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 break;
744 }
Mark Brown1547aba2010-05-07 21:11:40 +0100745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100751 continue;
752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Mark Brown3ff3f642008-05-19 12:32:25 +0200762 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100766 continue;
767
Benoit Cousson88bd8702014-07-08 23:19:34 +0200768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 }
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100782 continue;
783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000793 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000795 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100796
797 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100803}
804
805/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100807{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200810
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800832 }
833
Mark Brown64ab9ba2009-03-31 11:27:03 +0100834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600848 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100850 }
Andy Green6ed25972008-06-13 16:24:05 +0100851
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 return 0;
853}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000854EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858#endif
859
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100860static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800861};
862
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200863static struct snd_soc_component *soc_find_component(
864 const struct device_node *of_node, const char *name)
865{
866 struct snd_soc_component *component;
867
868 list_for_each_entry(component, &component_list, list) {
869 if (of_node) {
870 if (component->dev->of_node == of_node)
871 return component;
872 } else if (strcmp(component->name, name) == 0) {
873 return component;
874 }
875 }
876
877 return NULL;
878}
879
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200880static struct snd_soc_dai *snd_soc_find_dai(
881 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100882{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200883 struct snd_soc_component *component;
884 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100885
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200886 /* Find CPU DAI from registered DAIs*/
887 list_for_each_entry(component, &component_list, list) {
888 if (dlc->of_node && component->dev->of_node != dlc->of_node)
889 continue;
890 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
891 continue;
892 list_for_each_entry(dai, &component->dai_list, list) {
893 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100894 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100895
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200896 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100897 }
898 }
899
900 return NULL;
901}
902
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
906 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200907 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200908 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200909 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000910 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100911 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200912 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200913
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000914 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000915
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200916 cpu_dai_component.name = dai_link->cpu_name;
917 cpu_dai_component.of_node = dai_link->cpu_of_node;
918 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
919 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000920 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000921 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000923 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000924 }
925
Benoit Cousson88bd8702014-07-08 23:19:34 +0200926 rtd->num_codecs = dai_link->num_codecs;
927
928 /* Find CODEC from registered CODECs */
929 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200930 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200931 if (!codec_dais[i]) {
932 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
933 codecs[i].dai_name);
934 return -EPROBE_DEFER;
935 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000936 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100937
Benoit Cousson88bd8702014-07-08 23:19:34 +0200938 /* Single codec links expect codec and codec_dai in runtime data */
939 rtd->codec_dai = codec_dais[0];
940 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100941
Mark Brown848dd8b2011-04-27 18:16:32 +0100942 /* if there's no platform we match on the empty platform */
943 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700944 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100945 platform_name = "snd-soc-dummy";
946
Mark Brownb19e6e72012-03-14 21:18:39 +0000947 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700949 if (dai_link->platform_of_node) {
950 if (platform->dev->of_node !=
951 dai_link->platform_of_node)
952 continue;
953 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200954 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700955 continue;
956 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700957
958 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000960 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000961 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000963 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000965
966 card->num_rtd++;
967
968 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969}
970
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200971static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600972{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200973 if (!component->probed)
974 return;
975
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200976 /* This is a HACK and will be removed soon */
977 if (component->codec)
978 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600979
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200980 if (component->remove)
981 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600982
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200983 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600984
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200985 soc_cleanup_component_debugfs(component);
986 component->probed = 0;
987 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200988}
989
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200990static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100991{
992 int err;
993
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200994 if (dai && dai->probed &&
995 dai->driver->remove_order == order) {
996 if (dai->driver->remove) {
997 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100998 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200999 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001000 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001001 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001002 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001003 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001004 }
1005}
1006
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008{
1009 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001010 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011
1012 /* unregister the rtd device */
1013 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001014 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1015 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1016 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017 rtd->dev_registered = 0;
1018 }
1019
1020 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001021 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001022 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001024 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025}
1026
Stephen Warren62ae68f2012-06-08 12:34:23 -06001027static void soc_remove_link_components(struct snd_soc_card *card, int num,
1028 int order)
1029{
1030 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1031 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001032 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001033 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001034 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001035
1036 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001037 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001038 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039
1040 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001041 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001042 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001043 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001044 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045 }
1046
1047 /* remove any CPU-side CODEC */
1048 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001049 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001050 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001051 }
1052}
1053
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001054static void soc_remove_dai_links(struct snd_soc_card *card)
1055{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001056 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001057
Liam Girdwood0168bf02011-06-07 16:08:05 +01001058 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1059 order++) {
1060 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001061 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001062 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001063
1064 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1065 order++) {
1066 for (dai = 0; dai < card->num_rtd; dai++)
1067 soc_remove_link_components(card, dai, order);
1068 }
1069
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001070 card->num_rtd = 0;
1071}
1072
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001073static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001074 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001075{
1076 int i;
1077
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001078 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001079 return;
1080
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001081 for (i = 0; i < card->num_configs; i++) {
1082 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001083 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001084 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001085 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001086 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001087 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001088 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001089 }
1090}
1091
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001092static int soc_probe_component(struct snd_soc_card *card,
1093 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001094{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001095 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1096 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001097 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001098 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001099
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001100 if (component->probed)
1101 return 0;
1102
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001103 component->card = card;
1104 dapm->card = card;
1105 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001106
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001107 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001108 return -ENODEV;
1109
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001110 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001111
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001112 if (component->dapm_widgets) {
1113 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1114 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001115
1116 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001117 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001118 "Failed to create new controls %d\n", ret);
1119 goto err_probe;
1120 }
1121 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001122
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001123 /*
1124 * This is rather ugly, but certain platforms expect that the DAPM
1125 * widgets for the DAIs for components with the same parent device are
1126 * created in the platforms DAPM context. Until that is fixed we need to
1127 * keep this.
1128 */
1129 if (component->steal_sibling_dai_widgets) {
1130 dai_component = NULL;
1131 list_for_each_entry(component2, &component_list, list) {
1132 if (component == component2)
1133 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001134
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001135 if (component2->dev == component->dev &&
1136 !list_empty(&component2->dai_list)) {
1137 dai_component = component2;
1138 break;
1139 }
1140 }
1141 } else {
1142 dai_component = component;
1143 list_for_each_entry(component2, &component_list, list) {
1144 if (component2->dev == component->dev &&
1145 component2->steal_sibling_dai_widgets) {
1146 dai_component = NULL;
1147 break;
1148 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001149 }
1150 }
Mark Brown888df392012-02-16 19:37:51 -08001151
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001152 if (dai_component) {
1153 list_for_each_entry(dai, &dai_component->dai_list, list) {
1154 snd_soc_dapm_new_dai_widgets(dapm, dai);
1155 if (ret != 0) {
1156 dev_err(component->dev,
1157 "Failed to create DAI widgets %d\n",
1158 ret);
1159 goto err_probe;
1160 }
1161 }
1162 }
Mark Brown33c5f962011-08-22 18:40:30 +01001163
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001164 if (component->probe) {
1165 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001166 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001167 dev_err(component->dev,
1168 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001169 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001170 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001171
1172 WARN(dapm->idle_bias_off &&
1173 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001174 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001175 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001176 }
1177
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001178 if (component->controls)
1179 snd_soc_add_component_controls(component, component->controls,
1180 component->num_controls);
1181 if (component->dapm_routes)
1182 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1183 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001184
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001185 component->probed = 1;
1186 list_add(&dapm->list, &card->dapm_list);
1187
1188 /* This is a HACK and will be removed soon */
1189 if (component->codec)
1190 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001191
Jarkko Nikula70d29332011-01-27 16:24:22 +02001192 return 0;
1193
1194err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001195 soc_cleanup_component_debugfs(component);
1196 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001197
1198 return ret;
1199}
1200
Mark Brown36ae1a92012-01-06 17:12:45 -08001201static void rtd_release(struct device *dev)
1202{
1203 kfree(dev);
1204}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001205
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001206static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1207 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001208{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001209 int ret = 0;
1210
Jarkko Nikula589c3562010-12-06 16:27:07 +02001211 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001212 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1213 if (!rtd->dev)
1214 return -ENOMEM;
1215 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001216 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001217 rtd->dev->release = rtd_release;
1218 rtd->dev->init_name = name;
1219 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001220 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001221 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1222 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1223 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1224 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001225 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001226 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001227 /* calling put_device() here to free the rtd->dev */
1228 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001229 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001230 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001231 return ret;
1232 }
1233 rtd->dev_registered = 1;
1234
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001235 if (rtd->codec) {
1236 /* add DAPM sysfs entries for this codec */
1237 ret = snd_soc_dapm_sys_add(rtd->dev);
1238 if (ret < 0)
1239 dev_err(rtd->dev,
1240 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1241 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001242
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001243 /* add codec sysfs entries */
1244 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1245 if (ret < 0)
1246 dev_err(rtd->dev,
1247 "ASoC: failed to add codec sysfs files: %d\n",
1248 ret);
1249 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001250
1251 return 0;
1252}
1253
Stephen Warren62ae68f2012-06-08 12:34:23 -06001254static int soc_probe_link_components(struct snd_soc_card *card, int num,
1255 int order)
1256{
1257 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001258 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001259 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001260 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001261
1262 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001263 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001264 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001265 ret = soc_probe_component(card, component);
1266 if (ret < 0)
1267 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001268 }
1269
Benoit Cousson88bd8702014-07-08 23:19:34 +02001270 /* probe the CODEC-side components */
1271 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001272 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001273 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001274 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001275 if (ret < 0)
1276 return ret;
1277 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001278 }
1279
1280 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001281 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001282 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001283 if (ret < 0)
1284 return ret;
1285 }
1286
1287 return 0;
1288}
1289
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001290static int soc_probe_codec_dai(struct snd_soc_card *card,
1291 struct snd_soc_dai *codec_dai,
1292 int order)
1293{
1294 int ret;
1295
1296 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1297 if (codec_dai->driver->probe) {
1298 ret = codec_dai->driver->probe(codec_dai);
1299 if (ret < 0) {
1300 dev_err(codec_dai->dev,
1301 "ASoC: failed to probe CODEC DAI %s: %d\n",
1302 codec_dai->name, ret);
1303 return ret;
1304 }
1305 }
1306
1307 /* mark codec_dai as probed and add to card dai list */
1308 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001309 }
1310
1311 return 0;
1312}
1313
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001314static int soc_link_dai_widgets(struct snd_soc_card *card,
1315 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001316 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001317{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001318 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1319 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001320 struct snd_soc_dapm_widget *play_w, *capture_w;
1321 int ret;
1322
Benoit Cousson88bd8702014-07-08 23:19:34 +02001323 if (rtd->num_codecs > 1)
1324 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1325
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001326 /* link the DAI widgets */
1327 play_w = codec_dai->playback_widget;
1328 capture_w = cpu_dai->capture_widget;
1329 if (play_w && capture_w) {
1330 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1331 capture_w, play_w);
1332 if (ret != 0) {
1333 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1334 play_w->name, capture_w->name, ret);
1335 return ret;
1336 }
1337 }
1338
1339 play_w = cpu_dai->playback_widget;
1340 capture_w = codec_dai->capture_widget;
1341 if (play_w && capture_w) {
1342 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1343 capture_w, play_w);
1344 if (ret != 0) {
1345 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1346 play_w->name, capture_w->name, ret);
1347 return ret;
1348 }
1349 }
1350
1351 return 0;
1352}
1353
Stephen Warren62ae68f2012-06-08 12:34:23 -06001354static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001355{
1356 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1357 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001358 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001359 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001360 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001361
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001362 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001363 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001364
1365 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001366 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001367 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001368 for (i = 0; i < rtd->num_codecs; i++)
1369 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370
1371 /* set default power off timeout */
1372 rtd->pmdown_time = pmdown_time;
1373
1374 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001375 if (!cpu_dai->probed &&
1376 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377 if (cpu_dai->driver->probe) {
1378 ret = cpu_dai->driver->probe(cpu_dai);
1379 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001380 dev_err(cpu_dai->dev,
1381 "ASoC: failed to probe CPU DAI %s: %d\n",
1382 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 return ret;
1384 }
1385 }
1386 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001387 }
1388
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001390 for (i = 0; i < rtd->num_codecs; i++) {
1391 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1392 if (ret)
1393 return ret;
1394 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001395
Liam Girdwood0168bf02011-06-07 16:08:05 +01001396 /* complete DAI probe during last probe */
1397 if (order != SND_SOC_COMP_ORDER_LAST)
1398 return 0;
1399
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001400 /* do machine specific initialization */
1401 if (dai_link->init) {
1402 ret = dai_link->init(rtd);
1403 if (ret < 0) {
1404 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1405 dai_link->name, ret);
1406 return ret;
1407 }
1408 }
1409
1410 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001411 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001413
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001414#ifdef CONFIG_DEBUG_FS
1415 /* add DPCM sysfs entries */
1416 if (dai_link->dynamic) {
1417 ret = soc_dpcm_debugfs_add(rtd);
1418 if (ret < 0) {
1419 dev_err(rtd->dev,
1420 "ASoC: failed to add dpcm sysfs entries: %d\n",
1421 ret);
1422 return ret;
1423 }
1424 }
1425#endif
1426
Mark Brown36ae1a92012-01-06 17:12:45 -08001427 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001429 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1430 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001431
Namarta Kohli1245b702012-08-16 17:10:41 +05301432 if (cpu_dai->driver->compress_dai) {
1433 /*create compress_device"*/
1434 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001435 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001436 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301437 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001438 return ret;
1439 }
1440 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301441
1442 if (!dai_link->params) {
1443 /* create the pcm */
1444 ret = soc_new_pcm(rtd, num);
1445 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001446 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301447 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001448 return ret;
1449 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301450 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001451 INIT_DELAYED_WORK(&rtd->delayed_work,
1452 codec2codec_close_delayed_work);
1453
Namarta Kohli1245b702012-08-16 17:10:41 +05301454 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001455 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001456 if (ret)
1457 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001458 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001459 }
1460
1461 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001462 for (i = 0; i < rtd->num_codecs; i++) {
1463 if (rtd->codec_dais[i]->driver->ac97_control)
1464 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1465 rtd->cpu_dai->ac97_pdata);
1466 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001467
1468 return 0;
1469}
1470
1471#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001472static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1473 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474{
1475 int ret;
1476
1477 /* Only instantiate AC97 if not already done by the adaptor
1478 * for the generic AC97 subsystem.
1479 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001480 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001481 /*
1482 * It is possible that the AC97 device is already registered to
1483 * the device subsystem. This happens when the device is created
1484 * via snd_ac97_mixer(). Currently only SoC codec that does so
1485 * is the generic AC97 glue but others migh emerge.
1486 *
1487 * In those cases we don't try to register the device again.
1488 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001489 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001490 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001492 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001494 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001495 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 return ret;
1497 }
1498
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001499 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 }
1501 return 0;
1502}
1503
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001504static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001505{
1506 if (codec->ac97_registered) {
1507 soc_ac97_dev_unregister(codec);
1508 codec->ac97_registered = 0;
1509 }
1510}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001511
Benoit Cousson88bd8702014-07-08 23:19:34 +02001512static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1513{
1514 int i, ret;
1515
1516 for (i = 0; i < rtd->num_codecs; i++) {
1517 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1518
1519 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1520 if (ret) {
1521 while (--i >= 0)
1522 soc_unregister_ac97_codec(codec_dai->codec);
1523 return ret;
1524 }
1525 }
1526
1527 return 0;
1528}
1529
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001530static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1531{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001532 int i;
1533
1534 for (i = 0; i < rtd->num_codecs; i++)
1535 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001536}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537#endif
1538
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001539static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001540{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001541 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001542 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001543 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001544
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001545 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1546 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001547 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001548 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001549
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001550 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001551 return -EPROBE_DEFER;
1552 }
1553
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001554 /*
1555 * Some places still reference rtd->codec, so we have to keep that
1556 * initialized if the component is a CODEC. Once all those references
1557 * have been removed, this code can be removed as well.
1558 */
1559 rtd->codec = rtd->component->codec;
1560
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001561 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001562}
1563
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001564static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1565{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001566 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001567 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001568 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001569
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001570 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001571 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001572 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001573
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001574 /* do machine specific initialization */
1575 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001576 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001577 if (ret < 0) {
1578 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1579 aux_dev->name, ret);
1580 return ret;
1581 }
1582 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001583
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001584 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001585}
1586
1587static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1588{
1589 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001590 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001591
1592 /* unregister the rtd device */
1593 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001594 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001595 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001596 rtd->dev_registered = 0;
1597 }
1598
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001599 if (component && component->probed)
1600 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001601}
1602
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001603static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001604{
1605 int ret;
1606
1607 if (codec->cache_init)
1608 return 0;
1609
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001610 ret = snd_soc_cache_init(codec);
1611 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001612 dev_err(codec->dev,
1613 "ASoC: Failed to set cache compression type: %d\n",
1614 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001615 return ret;
1616 }
1617 codec->cache_init = 1;
1618 return 0;
1619}
1620
Mark Brownb19e6e72012-03-14 21:18:39 +00001621static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001623 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001624 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001625 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626
Liam Girdwood01b9d992012-03-07 10:38:25 +00001627 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628
Mark Brownb19e6e72012-03-14 21:18:39 +00001629 /* bind DAIs */
1630 for (i = 0; i < card->num_links; i++) {
1631 ret = soc_bind_dai_link(card, i);
1632 if (ret != 0)
1633 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 }
1635
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001636 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001637 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001638 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001639 if (ret != 0)
1640 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641 }
1642
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001643 /* initialize the register cache for each available codec */
1644 list_for_each_entry(codec, &codec_list, list) {
1645 if (codec->cache_init)
1646 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001647 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001648 if (ret < 0)
1649 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001650 }
1651
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001653 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 card->owner, 0, &card->snd_card);
1655 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001656 dev_err(card->dev,
1657 "ASoC: can't create sound card for card %s: %d\n",
1658 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001659 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661
Mark Browne37a4972011-03-02 18:21:57 +00001662 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1663 card->dapm.dev = card->dev;
1664 card->dapm.card = card;
1665 list_add(&card->dapm.list, &card->dapm_list);
1666
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001667#ifdef CONFIG_DEBUG_FS
1668 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1669#endif
1670
Mark Brown88ee1c62011-02-02 10:43:26 +00001671#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001672 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001673 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001674#endif
Andy Green6ed25972008-06-13 16:24:05 +01001675
Mark Brown9a841eb2011-04-12 17:51:37 -07001676 if (card->dapm_widgets)
1677 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1678 card->num_dapm_widgets);
1679
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001680 /* initialise the sound card only once */
1681 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001682 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001683 if (ret < 0)
1684 goto card_probe_error;
1685 }
1686
Stephen Warren62ae68f2012-06-08 12:34:23 -06001687 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001688 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1689 order++) {
1690 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001691 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001692 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001693 dev_err(card->dev,
1694 "ASoC: failed to instantiate card %d\n",
1695 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001696 goto probe_dai_err;
1697 }
1698 }
1699 }
1700
1701 /* probe all DAI links on this card */
1702 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1703 order++) {
1704 for (i = 0; i < card->num_links; i++) {
1705 ret = soc_probe_link_dais(card, i, order);
1706 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001707 dev_err(card->dev,
1708 "ASoC: failed to instantiate card %d\n",
1709 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001710 goto probe_dai_err;
1711 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001712 }
1713 }
1714
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001715 for (i = 0; i < card->num_aux_devs; i++) {
1716 ret = soc_probe_aux_dev(card, i);
1717 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001718 dev_err(card->dev,
1719 "ASoC: failed to add auxiliary devices %d\n",
1720 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001721 goto probe_aux_dev_err;
1722 }
1723 }
1724
Mark Brown888df392012-02-16 19:37:51 -08001725 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001726 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001727
Mark Brownb7af1da2011-04-07 19:18:44 +09001728 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001729 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001730
Mark Brownb8ad29d2011-03-02 18:35:51 +00001731 if (card->dapm_routes)
1732 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1733 card->num_dapm_routes);
1734
Mark Brown75d9ac42011-09-27 16:41:01 +01001735 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001736 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001737 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001738 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001739
Mark Brownf04209a2012-04-09 16:29:21 +01001740 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001741 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1742 int j;
1743
1744 for (j = 0; j < rtd->num_codecs; j++) {
1745 struct snd_soc_dai *codec_dai = codec_dais[j];
1746
1747 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1748 if (ret != 0 && ret != -ENOTSUPP)
1749 dev_warn(codec_dai->dev,
1750 "ASoC: Failed to set DAI format: %d\n",
1751 ret);
1752 }
Mark Brownf04209a2012-04-09 16:29:21 +01001753 }
1754
1755 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001756 if (dai_fmt &&
1757 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001758 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1759 dai_fmt);
1760 if (ret != 0 && ret != -ENOTSUPP)
1761 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001762 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001763 ret);
1764 } else if (dai_fmt) {
1765 /* Flip the polarity for the "CPU" end */
1766 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1767 switch (dai_link->dai_fmt &
1768 SND_SOC_DAIFMT_MASTER_MASK) {
1769 case SND_SOC_DAIFMT_CBM_CFM:
1770 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1771 break;
1772 case SND_SOC_DAIFMT_CBM_CFS:
1773 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1774 break;
1775 case SND_SOC_DAIFMT_CBS_CFM:
1776 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1777 break;
1778 case SND_SOC_DAIFMT_CBS_CFS:
1779 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1780 break;
1781 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001782
1783 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001784 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001785 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001786 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001787 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001788 ret);
1789 }
1790 }
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001793 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001794 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1795 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001796 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1797 "%s", card->driver_name ? card->driver_name : card->name);
1798 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1799 switch (card->snd_card->driver[i]) {
1800 case '_':
1801 case '-':
1802 case '\0':
1803 break;
1804 default:
1805 if (!isalnum(card->snd_card->driver[i]))
1806 card->snd_card->driver[i] = '_';
1807 break;
1808 }
1809 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810
Mark Brown28e9ad92011-03-02 18:36:34 +00001811 if (card->late_probe) {
1812 ret = card->late_probe(card);
1813 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001814 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001815 card->name, ret);
1816 goto probe_aux_dev_err;
1817 }
1818 }
1819
Stephen Warren16332812011-11-23 12:42:04 -07001820 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001821 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001822
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001823 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001824
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001825 ret = snd_card_register(card->snd_card);
1826 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001827 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1828 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001829 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830 }
1831
1832#ifdef CONFIG_SND_SOC_AC97_BUS
1833 /* register any AC97 codecs */
1834 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001835 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1836 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001837 dev_err(card->dev,
1838 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001839 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001840 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001841 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001842 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001843 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001844#endif
1845
Mark Brown435c5e22008-12-04 15:32:53 +00001846 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001847 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001848 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001849
1850 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001852probe_aux_dev_err:
1853 for (i = 0; i < card->num_aux_devs; i++)
1854 soc_remove_aux_dev(card, i);
1855
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001857 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001858
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001860 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001861 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001862
1863 snd_card_free(card->snd_card);
1864
Mark Brownb19e6e72012-03-14 21:18:39 +00001865base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001866 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001867
Mark Brownb19e6e72012-03-14 21:18:39 +00001868 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001869}
1870
1871/* probes a new socdev */
1872static int soc_probe(struct platform_device *pdev)
1873{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001874 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001875
Vinod Koul70a7ca32011-01-14 19:22:48 +05301876 /*
1877 * no card, so machine driver should be registering card
1878 * we should not be here in that case so ret error
1879 */
1880 if (!card)
1881 return -EINVAL;
1882
Mark Brownfe4085e2012-03-02 13:07:41 +00001883 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001884 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001885 card->name);
1886
Mark Brown435c5e22008-12-04 15:32:53 +00001887 /* Bodge while we unpick instantiation */
1888 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001889
Mark Brown28d528c2012-08-09 18:45:23 +01001890 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001891}
1892
Vinod Koulb0e26482011-01-13 22:48:02 +05301893static int soc_cleanup_card_resources(struct snd_soc_card *card)
1894{
Vinod Koulb0e26482011-01-13 22:48:02 +05301895 int i;
1896
1897 /* make sure any delayed work runs */
1898 for (i = 0; i < card->num_rtd; i++) {
1899 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001900 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301901 }
1902
1903 /* remove auxiliary devices */
1904 for (i = 0; i < card->num_aux_devs; i++)
1905 soc_remove_aux_dev(card, i);
1906
1907 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001908 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301909
1910 soc_cleanup_card_debugfs(card);
1911
1912 /* remove the card */
1913 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001914 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301915
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001916 snd_soc_dapm_free(&card->dapm);
1917
Vinod Koulb0e26482011-01-13 22:48:02 +05301918 snd_card_free(card->snd_card);
1919 return 0;
1920
1921}
1922
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001923/* removes a socdev */
1924static int soc_remove(struct platform_device *pdev)
1925{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001926 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927
Mark Brownc5af3a22008-11-28 13:29:45 +00001928 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929 return 0;
1930}
1931
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001932int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001933{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001934 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001935 int i;
Mark Brown51737472009-06-22 13:16:51 +01001936
1937 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001938 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001939
1940 /* Flush out pmdown_time work - we actually do want to run it
1941 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942 for (i = 0; i < card->num_rtd; i++) {
1943 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001944 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001945 }
Mark Brown51737472009-06-22 13:16:51 +01001946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001947 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001948
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001949 /* deactivate pins to sleep state */
1950 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001951 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1952 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1953 int j;
1954
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001955 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001956 for (j = 0; j < rtd->num_codecs; j++) {
1957 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1958 pinctrl_pm_select_sleep_state(codec_dai->dev);
1959 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001960 }
1961
Mark Brown416356f2009-06-30 19:05:15 +01001962 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001963}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001964EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001965
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001966const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301967 .suspend = snd_soc_suspend,
1968 .resume = snd_soc_resume,
1969 .freeze = snd_soc_suspend,
1970 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001971 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301972 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001973};
Stephen Warrendeb26072011-04-05 19:35:30 -06001974EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001975
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976/* ASoC platform driver */
1977static struct platform_driver soc_driver = {
1978 .driver = {
1979 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001980 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001981 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 },
1983 .probe = soc_probe,
1984 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985};
1986
Mark Brown096e49d2009-07-05 15:12:22 +01001987/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 * snd_soc_new_ac97_codec - initailise AC97 device
1989 * @codec: audio codec
1990 * @ops: AC97 bus operations
1991 * @num: AC97 codec number
1992 *
1993 * Initialises AC97 codec resources for use by ad-hoc devices only.
1994 */
1995int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1996 struct snd_ac97_bus_ops *ops, int num)
1997{
1998 mutex_lock(&codec->mutex);
1999
2000 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2001 if (codec->ac97 == NULL) {
2002 mutex_unlock(&codec->mutex);
2003 return -ENOMEM;
2004 }
2005
2006 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2007 if (codec->ac97->bus == NULL) {
2008 kfree(codec->ac97);
2009 codec->ac97 = NULL;
2010 mutex_unlock(&codec->mutex);
2011 return -ENOMEM;
2012 }
2013
2014 codec->ac97->bus->ops = ops;
2015 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002016
2017 /*
2018 * Mark the AC97 device to be created by us. This way we ensure that the
2019 * device will be registered with the device subsystem later on.
2020 */
2021 codec->ac97_created = 1;
2022
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002023 mutex_unlock(&codec->mutex);
2024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2027
Markus Pargmann741a5092013-08-19 17:05:55 +02002028static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2029
2030static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2031{
2032 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2033
2034 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2035
2036 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2037
2038 udelay(10);
2039
2040 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2041
2042 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2043 msleep(2);
2044}
2045
2046static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2047{
2048 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2049
2050 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2051
2052 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2053 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2054 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2055
2056 udelay(10);
2057
2058 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2059
2060 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2061 msleep(2);
2062}
2063
2064static int snd_soc_ac97_parse_pinctl(struct device *dev,
2065 struct snd_ac97_reset_cfg *cfg)
2066{
2067 struct pinctrl *p;
2068 struct pinctrl_state *state;
2069 int gpio;
2070 int ret;
2071
2072 p = devm_pinctrl_get(dev);
2073 if (IS_ERR(p)) {
2074 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002075 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002076 }
2077 cfg->pctl = p;
2078
2079 state = pinctrl_lookup_state(p, "ac97-reset");
2080 if (IS_ERR(state)) {
2081 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002082 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002083 }
2084 cfg->pstate_reset = state;
2085
2086 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2087 if (IS_ERR(state)) {
2088 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002089 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002090 }
2091 cfg->pstate_warm_reset = state;
2092
2093 state = pinctrl_lookup_state(p, "ac97-running");
2094 if (IS_ERR(state)) {
2095 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002096 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002097 }
2098 cfg->pstate_run = state;
2099
2100 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2101 if (gpio < 0) {
2102 dev_err(dev, "Can't find ac97-sync gpio\n");
2103 return gpio;
2104 }
2105 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2106 if (ret) {
2107 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2108 return ret;
2109 }
2110 cfg->gpio_sync = gpio;
2111
2112 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2113 if (gpio < 0) {
2114 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2115 return gpio;
2116 }
2117 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2118 if (ret) {
2119 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2120 return ret;
2121 }
2122 cfg->gpio_sdata = gpio;
2123
2124 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2125 if (gpio < 0) {
2126 dev_err(dev, "Can't find ac97-reset gpio\n");
2127 return gpio;
2128 }
2129 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2130 if (ret) {
2131 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2132 return ret;
2133 }
2134 cfg->gpio_reset = gpio;
2135
2136 return 0;
2137}
2138
Mark Brownb047e1c2013-06-26 12:45:59 +01002139struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002140EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002141
2142int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2143{
2144 if (ops == soc_ac97_ops)
2145 return 0;
2146
2147 if (soc_ac97_ops && ops)
2148 return -EBUSY;
2149
2150 soc_ac97_ops = ops;
2151
2152 return 0;
2153}
2154EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2155
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002156/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002157 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2158 *
2159 * This function sets the reset and warm_reset properties of ops and parses
2160 * the device node of pdev to get pinctrl states and gpio numbers to use.
2161 */
2162int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2163 struct platform_device *pdev)
2164{
2165 struct device *dev = &pdev->dev;
2166 struct snd_ac97_reset_cfg cfg;
2167 int ret;
2168
2169 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2170 if (ret)
2171 return ret;
2172
2173 ret = snd_soc_set_ac97_ops(ops);
2174 if (ret)
2175 return ret;
2176
2177 ops->warm_reset = snd_soc_ac97_warm_reset;
2178 ops->reset = snd_soc_ac97_reset;
2179
2180 snd_ac97_rst_cfg = cfg;
2181 return 0;
2182}
2183EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2184
2185/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002186 * snd_soc_free_ac97_codec - free AC97 codec device
2187 * @codec: audio codec
2188 *
2189 * Frees AC97 codec device resources.
2190 */
2191void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2192{
2193 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002194#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002195 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002196#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 kfree(codec->ac97->bus);
2198 kfree(codec->ac97);
2199 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002200 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002201 mutex_unlock(&codec->mutex);
2202}
2203EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2204
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 * snd_soc_cnew - create new control
2207 * @_template: control template
2208 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002209 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002210 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211 *
2212 * Create a new mixer control from a template control.
2213 *
2214 * Returns 0 for success, else error.
2215 */
2216struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002217 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002218 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219{
2220 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 struct snd_kcontrol *kcontrol;
2222 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002223
2224 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002225 template.index = 0;
2226
Mark Brownefb7ac32011-03-08 17:23:24 +00002227 if (!long_name)
2228 long_name = template.name;
2229
2230 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002231 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002232 if (!name)
2233 return NULL;
2234
Mark Brownefb7ac32011-03-08 17:23:24 +00002235 template.name = name;
2236 } else {
2237 template.name = long_name;
2238 }
2239
2240 kcontrol = snd_ctl_new1(&template, data);
2241
2242 kfree(name);
2243
2244 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245}
2246EXPORT_SYMBOL_GPL(snd_soc_cnew);
2247
Liam Girdwood022658b2012-02-03 17:43:09 +00002248static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2249 const struct snd_kcontrol_new *controls, int num_controls,
2250 const char *prefix, void *data)
2251{
2252 int err, i;
2253
2254 for (i = 0; i < num_controls; i++) {
2255 const struct snd_kcontrol_new *control = &controls[i];
2256 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2257 control->name, prefix));
2258 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002259 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2260 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002261 return err;
2262 }
2263 }
2264
2265 return 0;
2266}
2267
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002268struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2269 const char *name)
2270{
2271 struct snd_card *card = soc_card->snd_card;
2272 struct snd_kcontrol *kctl;
2273
2274 if (unlikely(!name))
2275 return NULL;
2276
2277 list_for_each_entry(kctl, &card->controls, list)
2278 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2279 return kctl;
2280 return NULL;
2281}
2282EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2283
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002285 * snd_soc_add_component_controls - Add an array of controls to a component.
2286 *
2287 * @component: Component to add controls to
2288 * @controls: Array of controls to add
2289 * @num_controls: Number of elements in the array
2290 *
2291 * Return: 0 for success, else error.
2292 */
2293int snd_soc_add_component_controls(struct snd_soc_component *component,
2294 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2295{
2296 struct snd_card *card = component->card->snd_card;
2297
2298 return snd_soc_add_controls(card, component->dev, controls,
2299 num_controls, component->name_prefix, component);
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2302
2303/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002304 * snd_soc_add_codec_controls - add an array of controls to a codec.
2305 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002306 * duplicating this code.
2307 *
2308 * @codec: codec to add controls to
2309 * @controls: array of controls to add
2310 * @num_controls: number of elements in the array
2311 *
2312 * Return 0 for success, else error.
2313 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002314int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002315 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002316{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002317 return snd_soc_add_component_controls(&codec->component, controls,
2318 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002319}
Liam Girdwood022658b2012-02-03 17:43:09 +00002320EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002321
2322/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002323 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002324 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002325 *
2326 * @platform: platform to add controls to
2327 * @controls: array of controls to add
2328 * @num_controls: number of elements in the array
2329 *
2330 * Return 0 for success, else error.
2331 */
2332int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002333 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002334{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002335 return snd_soc_add_component_controls(&platform->component, controls,
2336 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002337}
2338EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2339
2340/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002341 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2342 * Convenience function to add a list of controls.
2343 *
2344 * @soc_card: SoC card to add controls to
2345 * @controls: array of controls to add
2346 * @num_controls: number of elements in the array
2347 *
2348 * Return 0 for success, else error.
2349 */
2350int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2351 const struct snd_kcontrol_new *controls, int num_controls)
2352{
2353 struct snd_card *card = soc_card->snd_card;
2354
2355 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2356 NULL, soc_card);
2357}
2358EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2359
2360/**
2361 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2362 * Convienience function to add a list of controls.
2363 *
2364 * @dai: DAI to add controls to
2365 * @controls: array of controls to add
2366 * @num_controls: number of elements in the array
2367 *
2368 * Return 0 for success, else error.
2369 */
2370int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2371 const struct snd_kcontrol_new *controls, int num_controls)
2372{
2373 struct snd_card *card = dai->card->snd_card;
2374
2375 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2376 NULL, dai);
2377}
2378EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2379
2380/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381 * snd_soc_info_enum_double - enumerated double mixer info callback
2382 * @kcontrol: mixer control
2383 * @uinfo: control element information
2384 *
2385 * Callback to provide information about a double enumerated
2386 * mixer control.
2387 *
2388 * Returns 0 for success.
2389 */
2390int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_info *uinfo)
2392{
2393 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2394
2395 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2396 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002397 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002399 if (uinfo->value.enumerated.item >= e->items)
2400 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002401 strlcpy(uinfo->value.enumerated.name,
2402 e->texts[uinfo->value.enumerated.item],
2403 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404 return 0;
2405}
2406EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2407
2408/**
2409 * snd_soc_get_enum_double - enumerated double mixer get callback
2410 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002411 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412 *
2413 * Callback to get the value of a double enumerated mixer.
2414 *
2415 * Returns 0 for success.
2416 */
2417int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2418 struct snd_ctl_elem_value *ucontrol)
2419{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002420 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002421 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002422 unsigned int val, item;
2423 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002424 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002425
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002426 ret = snd_soc_component_read(component, e->reg, &reg_val);
2427 if (ret)
2428 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002429 val = (reg_val >> e->shift_l) & e->mask;
2430 item = snd_soc_enum_val_to_item(e, val);
2431 ucontrol->value.enumerated.item[0] = item;
2432 if (e->shift_l != e->shift_r) {
2433 val = (reg_val >> e->shift_l) & e->mask;
2434 item = snd_soc_enum_val_to_item(e, val);
2435 ucontrol->value.enumerated.item[1] = item;
2436 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437
2438 return 0;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2441
2442/**
2443 * snd_soc_put_enum_double - enumerated double mixer put callback
2444 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002445 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 *
2447 * Callback to set the value of a double enumerated mixer.
2448 *
2449 * Returns 0 for success.
2450 */
2451int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2453{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002454 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002456 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002457 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002458 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002460 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002462 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002463 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002465 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002467 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002468 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469 }
2470
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002471 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472}
2473EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2474
2475/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002476 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002477 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002478 * @reg: Register to read
2479 * @mask: Mask to use after shifting the register value
2480 * @shift: Right shift of register value
2481 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002482 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002483 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002484 * This functions reads a codec register. The register value is shifted right
2485 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2486 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002487 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002488 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002489 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002490static int snd_soc_read_signed(struct snd_soc_component *component,
2491 unsigned int reg, unsigned int mask, unsigned int shift,
2492 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002493{
Markus Pargmannf227b882014-01-16 16:02:10 +01002494 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002495 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002496
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002497 ret = snd_soc_component_read(component, reg, &val);
2498 if (ret < 0)
2499 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002500
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002501 val = (val >> shift) & mask;
2502
2503 if (!sign_bit) {
2504 *signed_val = val;
2505 return 0;
2506 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002507
2508 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002509 if (!(val & BIT(sign_bit))) {
2510 *signed_val = val;
2511 return 0;
2512 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002513
2514 ret = val;
2515
2516 /*
2517 * The register most probably does not contain a full-sized int.
2518 * Instead we have an arbitrary number of bits in a signed
2519 * representation which has to be translated into a full-sized int.
2520 * This is done by filling up all bits above the sign-bit.
2521 */
2522 ret |= ~((int)(BIT(sign_bit) - 1));
2523
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002524 *signed_val = ret;
2525
2526 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002527}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002528
2529/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530 * snd_soc_info_volsw - single mixer info callback
2531 * @kcontrol: mixer control
2532 * @uinfo: control element information
2533 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002534 * Callback to provide information about a single mixer control, or a double
2535 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 *
2537 * Returns 0 for success.
2538 */
2539int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_info *uinfo)
2541{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002544 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002545
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002546 if (!mc->platform_max)
2547 mc->platform_max = mc->max;
2548 platform_max = mc->platform_max;
2549
2550 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002551 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2552 else
2553 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2554
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002555 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002556 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002557 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002558 return 0;
2559}
2560EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2561
2562/**
2563 * snd_soc_get_volsw - single mixer get callback
2564 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002565 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002567 * Callback to get the value of a single mixer control, or a double mixer
2568 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 *
2570 * Returns 0 for success.
2571 */
2572int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_value *ucontrol)
2574{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002575 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002576 struct soc_mixer_control *mc =
2577 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002578 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002579 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002580 unsigned int shift = mc->shift;
2581 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002582 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002583 int min = mc->min;
2584 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002585 unsigned int mask = (1 << fls(max)) - 1;
2586 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002587 int val;
2588 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589
Markus Pargmannf227b882014-01-16 16:02:10 +01002590 if (sign_bit)
2591 mask = BIT(sign_bit + 1) - 1;
2592
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002593 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2594 if (ret)
2595 return ret;
2596
2597 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002598 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002600 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002601
2602 if (snd_soc_volsw_is_stereo(mc)) {
2603 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002604 ret = snd_soc_read_signed(component, reg, mask, rshift,
2605 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002606 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002607 ret = snd_soc_read_signed(component, reg2, mask, shift,
2608 sign_bit, &val);
2609 if (ret)
2610 return ret;
2611
2612 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002613 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002615 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002616 }
2617
2618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2621
2622/**
2623 * snd_soc_put_volsw - single mixer put callback
2624 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002625 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002627 * Callback to set the value of a single mixer control, or a double mixer
2628 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 *
2630 * Returns 0 for success.
2631 */
2632int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2633 struct snd_ctl_elem_value *ucontrol)
2634{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002635 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002636 struct soc_mixer_control *mc =
2637 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002638 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002639 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002640 unsigned int shift = mc->shift;
2641 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002642 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002643 int min = mc->min;
2644 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002645 unsigned int mask = (1 << fls(max)) - 1;
2646 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002647 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002648 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002649 unsigned int val2 = 0;
2650 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651
Markus Pargmannf227b882014-01-16 16:02:10 +01002652 if (sign_bit)
2653 mask = BIT(sign_bit + 1) - 1;
2654
2655 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002656 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002657 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 val_mask = mask << shift;
2659 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002660 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002661 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002662 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002663 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002664 if (reg == reg2) {
2665 val_mask |= mask << rshift;
2666 val |= val2 << rshift;
2667 } else {
2668 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002669 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002670 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002671 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002672 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002673 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674 return err;
2675
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002677 err = snd_soc_component_update_bits(component, reg2, val_mask,
2678 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002679
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002680 return err;
2681}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002682EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683
Mark Browne13ac2e2008-05-28 17:58:05 +01002684/**
Brian Austin1d99f242012-03-30 10:43:55 -05002685 * snd_soc_get_volsw_sx - single mixer get callback
2686 * @kcontrol: mixer control
2687 * @ucontrol: control element information
2688 *
2689 * Callback to get the value of a single mixer control, or a double mixer
2690 * control that spans 2 registers.
2691 *
2692 * Returns 0 for success.
2693 */
2694int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002697 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002700 unsigned int reg = mc->reg;
2701 unsigned int reg2 = mc->rreg;
2702 unsigned int shift = mc->shift;
2703 unsigned int rshift = mc->rshift;
2704 int max = mc->max;
2705 int min = mc->min;
2706 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002707 unsigned int val;
2708 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002709
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002710 ret = snd_soc_component_read(component, reg, &val);
2711 if (ret < 0)
2712 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002713
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002714 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2715
2716 if (snd_soc_volsw_is_stereo(mc)) {
2717 ret = snd_soc_component_read(component, reg2, &val);
2718 if (ret < 0)
2719 return ret;
2720
2721 val = ((val >> rshift) - min) & mask;
2722 ucontrol->value.integer.value[1] = val;
2723 }
Brian Austin1d99f242012-03-30 10:43:55 -05002724
2725 return 0;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2728
2729/**
2730 * snd_soc_put_volsw_sx - double mixer set callback
2731 * @kcontrol: mixer control
2732 * @uinfo: control element information
2733 *
2734 * Callback to set the value of a double mixer control that spans 2 registers.
2735 *
2736 * Returns 0 for success.
2737 */
2738int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
2740{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002741 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002742 struct soc_mixer_control *mc =
2743 (struct soc_mixer_control *)kcontrol->private_value;
2744
2745 unsigned int reg = mc->reg;
2746 unsigned int reg2 = mc->rreg;
2747 unsigned int shift = mc->shift;
2748 unsigned int rshift = mc->rshift;
2749 int max = mc->max;
2750 int min = mc->min;
2751 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002752 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002753 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002754
2755 val_mask = mask << shift;
2756 val = (ucontrol->value.integer.value[0] + min) & mask;
2757 val = val << shift;
2758
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002759 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302760 if (err < 0)
2761 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002762
2763 if (snd_soc_volsw_is_stereo(mc)) {
2764 val_mask = mask << rshift;
2765 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2766 val2 = val2 << rshift;
2767
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002768 err = snd_soc_component_update_bits(component, reg2, val_mask,
2769 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002770 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002771 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002772}
2773EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2774
2775/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002776 * snd_soc_info_volsw_s8 - signed mixer info callback
2777 * @kcontrol: mixer control
2778 * @uinfo: control element information
2779 *
2780 * Callback to provide information about a signed mixer control.
2781 *
2782 * Returns 0 for success.
2783 */
2784int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_info *uinfo)
2786{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002787 struct soc_mixer_control *mc =
2788 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002789 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002790 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002791
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002792 if (!mc->platform_max)
2793 mc->platform_max = mc->max;
2794 platform_max = mc->platform_max;
2795
Mark Browne13ac2e2008-05-28 17:58:05 +01002796 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2797 uinfo->count = 2;
2798 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002799 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002800 return 0;
2801}
2802EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2803
2804/**
2805 * snd_soc_get_volsw_s8 - signed mixer get callback
2806 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002807 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002808 *
2809 * Callback to get the value of a signed mixer control.
2810 *
2811 * Returns 0 for success.
2812 */
2813int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_value *ucontrol)
2815{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002816 struct soc_mixer_control *mc =
2817 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002818 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002819 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002820 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002821 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002822 int ret;
2823
2824 ret = snd_soc_component_read(component, reg, &val);
2825 if (ret)
2826 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002827
2828 ucontrol->value.integer.value[0] =
2829 ((signed char)(val & 0xff))-min;
2830 ucontrol->value.integer.value[1] =
2831 ((signed char)((val >> 8) & 0xff))-min;
2832 return 0;
2833}
2834EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2835
2836/**
2837 * snd_soc_put_volsw_sgn - signed mixer put callback
2838 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002839 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002840 *
2841 * Callback to set the value of a signed mixer control.
2842 *
2843 * Returns 0 for success.
2844 */
2845int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2847{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002848 struct soc_mixer_control *mc =
2849 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002850 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002851 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002852 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002853 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002854
2855 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2856 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2857
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002858 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002859}
2860EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2861
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002862/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002863 * snd_soc_info_volsw_range - single mixer info callback with range.
2864 * @kcontrol: mixer control
2865 * @uinfo: control element information
2866 *
2867 * Callback to provide information, within a range, about a single
2868 * mixer control.
2869 *
2870 * returns 0 for success.
2871 */
2872int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_info *uinfo)
2874{
2875 struct soc_mixer_control *mc =
2876 (struct soc_mixer_control *)kcontrol->private_value;
2877 int platform_max;
2878 int min = mc->min;
2879
2880 if (!mc->platform_max)
2881 mc->platform_max = mc->max;
2882 platform_max = mc->platform_max;
2883
2884 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002885 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002886 uinfo->value.integer.min = 0;
2887 uinfo->value.integer.max = platform_max - min;
2888
2889 return 0;
2890}
2891EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2892
2893/**
2894 * snd_soc_put_volsw_range - single mixer put value callback with range.
2895 * @kcontrol: mixer control
2896 * @ucontrol: control element information
2897 *
2898 * Callback to set the value, within a range, for a single mixer control.
2899 *
2900 * Returns 0 for success.
2901 */
2902int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2904{
2905 struct soc_mixer_control *mc =
2906 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002907 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002908 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002909 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002910 unsigned int shift = mc->shift;
2911 int min = mc->min;
2912 int max = mc->max;
2913 unsigned int mask = (1 << fls(max)) - 1;
2914 unsigned int invert = mc->invert;
2915 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002916 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002917
2918 val = ((ucontrol->value.integer.value[0] + min) & mask);
2919 if (invert)
2920 val = max - val;
2921 val_mask = mask << shift;
2922 val = val << shift;
2923
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002924 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002925 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002926 return ret;
2927
2928 if (snd_soc_volsw_is_stereo(mc)) {
2929 val = ((ucontrol->value.integer.value[1] + min) & mask);
2930 if (invert)
2931 val = max - val;
2932 val_mask = mask << shift;
2933 val = val << shift;
2934
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002935 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2936 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002937 }
2938
2939 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002940}
2941EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2942
2943/**
2944 * snd_soc_get_volsw_range - single mixer get callback with range
2945 * @kcontrol: mixer control
2946 * @ucontrol: control element information
2947 *
2948 * Callback to get the value, within a range, of a single mixer control.
2949 *
2950 * Returns 0 for success.
2951 */
2952int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2954{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002955 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002956 struct soc_mixer_control *mc =
2957 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002958 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002959 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002960 unsigned int shift = mc->shift;
2961 int min = mc->min;
2962 int max = mc->max;
2963 unsigned int mask = (1 << fls(max)) - 1;
2964 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002965 unsigned int val;
2966 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002967
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002968 ret = snd_soc_component_read(component, reg, &val);
2969 if (ret)
2970 return ret;
2971
2972 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002973 if (invert)
2974 ucontrol->value.integer.value[0] =
2975 max - ucontrol->value.integer.value[0];
2976 ucontrol->value.integer.value[0] =
2977 ucontrol->value.integer.value[0] - min;
2978
Mark Brown9bde4f02012-12-19 16:05:00 +00002979 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002980 ret = snd_soc_component_read(component, rreg, &val);
2981 if (ret)
2982 return ret;
2983
2984 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002985 if (invert)
2986 ucontrol->value.integer.value[1] =
2987 max - ucontrol->value.integer.value[1];
2988 ucontrol->value.integer.value[1] =
2989 ucontrol->value.integer.value[1] - min;
2990 }
2991
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2995
2996/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002997 * snd_soc_limit_volume - Set new limit to an existing volume control.
2998 *
2999 * @codec: where to look for the control
3000 * @name: Name of the control
3001 * @max: new maximum limit
3002 *
3003 * Return 0 for success, else error.
3004 */
3005int snd_soc_limit_volume(struct snd_soc_codec *codec,
3006 const char *name, int max)
3007{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003008 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003009 struct snd_kcontrol *kctl;
3010 struct soc_mixer_control *mc;
3011 int found = 0;
3012 int ret = -EINVAL;
3013
3014 /* Sanity check for name and max */
3015 if (unlikely(!name || max <= 0))
3016 return -EINVAL;
3017
3018 list_for_each_entry(kctl, &card->controls, list) {
3019 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3020 found = 1;
3021 break;
3022 }
3023 }
3024 if (found) {
3025 mc = (struct soc_mixer_control *)kctl->private_value;
3026 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003027 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003028 ret = 0;
3029 }
3030 }
3031 return ret;
3032}
3033EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3034
Mark Brown71d08512011-10-10 18:31:26 +01003035int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3036 struct snd_ctl_elem_info *uinfo)
3037{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003038 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003039 struct soc_bytes *params = (void *)kcontrol->private_value;
3040
3041 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003042 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003043
3044 return 0;
3045}
3046EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3047
3048int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3049 struct snd_ctl_elem_value *ucontrol)
3050{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003051 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003052 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003053 int ret;
3054
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003055 if (component->regmap)
3056 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003057 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003058 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003059 else
3060 ret = -EINVAL;
3061
Mark Brownf831b052012-02-17 16:20:33 -08003062 /* Hide any masked bytes to ensure consistent data reporting */
3063 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003064 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003065 case 1:
3066 ucontrol->value.bytes.data[0] &= ~params->mask;
3067 break;
3068 case 2:
3069 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003070 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003071 break;
3072 case 4:
3073 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003074 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003075 break;
3076 default:
3077 return -EINVAL;
3078 }
3079 }
3080
Mark Brown71d08512011-10-10 18:31:26 +01003081 return ret;
3082}
3083EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3084
3085int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3086 struct snd_ctl_elem_value *ucontrol)
3087{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003088 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003089 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003090 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003091 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003092 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003093
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003094 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003095 return -EINVAL;
3096
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003097 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003098
Mark Brownb5a8fe42013-01-20 21:42:22 +09003099 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3100 if (!data)
3101 return -ENOMEM;
3102
Mark Brownf831b052012-02-17 16:20:33 -08003103 /*
3104 * If we've got a mask then we need to preserve the register
3105 * bits. We shouldn't modify the incoming data so take a
3106 * copy.
3107 */
3108 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003109 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003110 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003111 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003112
3113 val &= params->mask;
3114
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003115 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003116 case 1:
3117 ((u8 *)data)[0] &= ~params->mask;
3118 ((u8 *)data)[0] |= val;
3119 break;
3120 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003121 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003122 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003123 &mask, &mask);
3124 if (ret != 0)
3125 goto out;
3126
3127 ((u16 *)data)[0] &= mask;
3128
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003129 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003130 &val, &val);
3131 if (ret != 0)
3132 goto out;
3133
3134 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003135 break;
3136 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003137 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003138 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003139 &mask, &mask);
3140 if (ret != 0)
3141 goto out;
3142
3143 ((u32 *)data)[0] &= mask;
3144
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003145 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003146 &val, &val);
3147 if (ret != 0)
3148 goto out;
3149
3150 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003151 break;
3152 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003153 ret = -EINVAL;
3154 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003155 }
3156 }
3157
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003158 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003159 data, len);
3160
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003161out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003162 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003163
3164 return ret;
3165}
3166EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3167
Vinod Kould9881202014-05-02 10:58:11 +05303168int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_info *ucontrol)
3170{
3171 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3172
3173 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3174 ucontrol->count = params->max;
3175
3176 return 0;
3177}
3178EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3179
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303180int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3181 unsigned int size, unsigned int __user *tlv)
3182{
3183 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3184 unsigned int count = size < params->max ? size : params->max;
3185 int ret = -ENXIO;
3186
3187 switch (op_flag) {
3188 case SNDRV_CTL_TLV_OP_READ:
3189 if (params->get)
3190 ret = params->get(tlv, count);
3191 break;
3192 case SNDRV_CTL_TLV_OP_WRITE:
3193 if (params->put)
3194 ret = params->put(tlv, count);
3195 break;
3196 }
3197 return ret;
3198}
3199EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3200
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003201/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003202 * snd_soc_info_xr_sx - signed multi register info callback
3203 * @kcontrol: mreg control
3204 * @uinfo: control element information
3205 *
3206 * Callback to provide information of a control that can
3207 * span multiple codec registers which together
3208 * forms a single signed value in a MSB/LSB manner.
3209 *
3210 * Returns 0 for success.
3211 */
3212int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_info *uinfo)
3214{
3215 struct soc_mreg_control *mc =
3216 (struct soc_mreg_control *)kcontrol->private_value;
3217 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3218 uinfo->count = 1;
3219 uinfo->value.integer.min = mc->min;
3220 uinfo->value.integer.max = mc->max;
3221
3222 return 0;
3223}
3224EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3225
3226/**
3227 * snd_soc_get_xr_sx - signed multi register get callback
3228 * @kcontrol: mreg control
3229 * @ucontrol: control element information
3230 *
3231 * Callback to get the value of a control that can span
3232 * multiple codec registers which together forms a single
3233 * signed value in a MSB/LSB manner. The control supports
3234 * specifying total no of bits used to allow for bitfields
3235 * across the multiple codec registers.
3236 *
3237 * Returns 0 for success.
3238 */
3239int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3240 struct snd_ctl_elem_value *ucontrol)
3241{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003242 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003243 struct soc_mreg_control *mc =
3244 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003245 unsigned int regbase = mc->regbase;
3246 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003247 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003248 unsigned int regwmask = (1<<regwshift)-1;
3249 unsigned int invert = mc->invert;
3250 unsigned long mask = (1UL<<mc->nbits)-1;
3251 long min = mc->min;
3252 long max = mc->max;
3253 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003254 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003255 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003256 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003257
3258 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003259 ret = snd_soc_component_read(component, regbase+i, &regval);
3260 if (ret)
3261 return ret;
3262 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003263 }
3264 val &= mask;
3265 if (min < 0 && val > max)
3266 val |= ~mask;
3267 if (invert)
3268 val = max - val;
3269 ucontrol->value.integer.value[0] = val;
3270
3271 return 0;
3272}
3273EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3274
3275/**
3276 * snd_soc_put_xr_sx - signed multi register get callback
3277 * @kcontrol: mreg control
3278 * @ucontrol: control element information
3279 *
3280 * Callback to set the value of a control that can span
3281 * multiple codec registers which together forms a single
3282 * signed value in a MSB/LSB manner. The control supports
3283 * specifying total no of bits used to allow for bitfields
3284 * across the multiple codec registers.
3285 *
3286 * Returns 0 for success.
3287 */
3288int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3289 struct snd_ctl_elem_value *ucontrol)
3290{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003291 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003292 struct soc_mreg_control *mc =
3293 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003294 unsigned int regbase = mc->regbase;
3295 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003296 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003297 unsigned int regwmask = (1<<regwshift)-1;
3298 unsigned int invert = mc->invert;
3299 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003300 long max = mc->max;
3301 long val = ucontrol->value.integer.value[0];
3302 unsigned int i, regval, regmask;
3303 int err;
3304
3305 if (invert)
3306 val = max - val;
3307 val &= mask;
3308 for (i = 0; i < regcount; i++) {
3309 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3310 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003311 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003312 regmask, regval);
3313 if (err < 0)
3314 return err;
3315 }
3316
3317 return 0;
3318}
3319EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3320
3321/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003322 * snd_soc_get_strobe - strobe get callback
3323 * @kcontrol: mixer control
3324 * @ucontrol: control element information
3325 *
3326 * Callback get the value of a strobe mixer control.
3327 *
3328 * Returns 0 for success.
3329 */
3330int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003333 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003334 struct soc_mixer_control *mc =
3335 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003336 unsigned int reg = mc->reg;
3337 unsigned int shift = mc->shift;
3338 unsigned int mask = 1 << shift;
3339 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003340 unsigned int val;
3341 int ret;
3342
3343 ret = snd_soc_component_read(component, reg, &val);
3344 if (ret)
3345 return ret;
3346
3347 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003348
3349 if (shift != 0 && val != 0)
3350 val = val >> shift;
3351 ucontrol->value.enumerated.item[0] = val ^ invert;
3352
3353 return 0;
3354}
3355EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3356
3357/**
3358 * snd_soc_put_strobe - strobe put callback
3359 * @kcontrol: mixer control
3360 * @ucontrol: control element information
3361 *
3362 * Callback strobe a register bit to high then low (or the inverse)
3363 * in one pass of a single mixer enum control.
3364 *
3365 * Returns 1 for success.
3366 */
3367int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_value *ucontrol)
3369{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003370 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003371 struct soc_mixer_control *mc =
3372 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003373 unsigned int reg = mc->reg;
3374 unsigned int shift = mc->shift;
3375 unsigned int mask = 1 << shift;
3376 unsigned int invert = mc->invert != 0;
3377 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3378 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3379 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3380 int err;
3381
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003382 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003383 if (err < 0)
3384 return err;
3385
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003386 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003387}
3388EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3389
3390/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003391 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3392 * @dai: DAI
3393 * @clk_id: DAI specific clock ID
3394 * @freq: new clock frequency in Hz
3395 * @dir: new clock direction - input/output.
3396 *
3397 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3398 */
3399int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3400 unsigned int freq, int dir)
3401{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003402 if (dai->driver && dai->driver->ops->set_sysclk)
3403 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003404 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003405 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003406 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003407 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003408 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409}
3410EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3411
3412/**
Mark Brownec4ee522011-03-07 20:58:11 +00003413 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3414 * @codec: CODEC
3415 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003416 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003417 * @freq: new clock frequency in Hz
3418 * @dir: new clock direction - input/output.
3419 *
3420 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3421 */
3422int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003423 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003424{
3425 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003426 return codec->driver->set_sysclk(codec, clk_id, source,
3427 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003428 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003429 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003430}
3431EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3432
3433/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003434 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3435 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003436 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003437 * @div: new clock divisor.
3438 *
3439 * Configures the clock dividers. This is used to derive the best DAI bit and
3440 * frame clocks from the system or master clock. It's best to set the DAI bit
3441 * and frame clocks as low as possible to save system power.
3442 */
3443int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3444 int div_id, int div)
3445{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003446 if (dai->driver && dai->driver->ops->set_clkdiv)
3447 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003448 else
3449 return -EINVAL;
3450}
3451EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3452
3453/**
3454 * snd_soc_dai_set_pll - configure DAI PLL.
3455 * @dai: DAI
3456 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003457 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003458 * @freq_in: PLL input clock frequency in Hz
3459 * @freq_out: requested PLL output clock frequency in Hz
3460 *
3461 * Configures and enables PLL to generate output clock based on input clock.
3462 */
Mark Brown85488032009-09-05 18:52:16 +01003463int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3464 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003465{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466 if (dai->driver && dai->driver->ops->set_pll)
3467 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003468 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003469 else if (dai->codec && dai->codec->driver->set_pll)
3470 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3471 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472 else
3473 return -EINVAL;
3474}
3475EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3476
Mark Brownec4ee522011-03-07 20:58:11 +00003477/*
3478 * snd_soc_codec_set_pll - configure codec PLL.
3479 * @codec: CODEC
3480 * @pll_id: DAI specific PLL ID
3481 * @source: DAI specific source for the PLL
3482 * @freq_in: PLL input clock frequency in Hz
3483 * @freq_out: requested PLL output clock frequency in Hz
3484 *
3485 * Configures and enables PLL to generate output clock based on input clock.
3486 */
3487int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3488 unsigned int freq_in, unsigned int freq_out)
3489{
3490 if (codec->driver->set_pll)
3491 return codec->driver->set_pll(codec, pll_id, source,
3492 freq_in, freq_out);
3493 else
3494 return -EINVAL;
3495}
3496EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3497
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003498/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003499 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3500 * @dai: DAI
3501 * @ratio Ratio of BCLK to Sample rate.
3502 *
3503 * Configures the DAI for a preset BCLK to sample rate ratio.
3504 */
3505int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3506{
3507 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3508 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3509 else
3510 return -EINVAL;
3511}
3512EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3513
3514/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003515 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3516 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003517 * @fmt: SND_SOC_DAIFMT_ format value.
3518 *
3519 * Configures the DAI hardware format and clocking.
3520 */
3521int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3522{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003523 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003524 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003525 if (dai->driver->ops->set_fmt == NULL)
3526 return -ENOTSUPP;
3527 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003528}
3529EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3530
3531/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003532 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003533 * @slots: Number of slots in use.
3534 * @tx_mask: bitmask representing active TX slots.
3535 * @rx_mask: bitmask representing active RX slots.
3536 *
3537 * Generates the TDM tx and rx slot default masks for DAI.
3538 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003539static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003540 unsigned int *tx_mask,
3541 unsigned int *rx_mask)
3542{
3543 if (*tx_mask || *rx_mask)
3544 return 0;
3545
3546 if (!slots)
3547 return -EINVAL;
3548
3549 *tx_mask = (1 << slots) - 1;
3550 *rx_mask = (1 << slots) - 1;
3551
3552 return 0;
3553}
3554
3555/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3557 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003558 * @tx_mask: bitmask representing active TX slots.
3559 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003560 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003561 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562 *
3563 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3564 * specific.
3565 */
3566int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003567 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003568{
Xiubo Lie5c21512014-03-21 14:17:12 +08003569 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3570 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003571 &tx_mask, &rx_mask);
3572 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003573 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003574
Benoit Cousson88bd8702014-07-08 23:19:34 +02003575 dai->tx_mask = tx_mask;
3576 dai->rx_mask = rx_mask;
3577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578 if (dai->driver && dai->driver->ops->set_tdm_slot)
3579 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003580 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003581 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003582 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003583}
3584EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3585
3586/**
Barry Song472df3c2009-09-12 01:16:29 +08003587 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3588 * @dai: DAI
3589 * @tx_num: how many TX channels
3590 * @tx_slot: pointer to an array which imply the TX slot number channel
3591 * 0~num-1 uses
3592 * @rx_num: how many RX channels
3593 * @rx_slot: pointer to an array which imply the RX slot number channel
3594 * 0~num-1 uses
3595 *
3596 * configure the relationship between channel number and TDM slot number.
3597 */
3598int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3599 unsigned int tx_num, unsigned int *tx_slot,
3600 unsigned int rx_num, unsigned int *rx_slot)
3601{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003602 if (dai->driver && dai->driver->ops->set_channel_map)
3603 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003604 rx_num, rx_slot);
3605 else
3606 return -EINVAL;
3607}
3608EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3609
3610/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003611 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3612 * @dai: DAI
3613 * @tristate: tristate enable
3614 *
3615 * Tristates the DAI so that others can use it.
3616 */
3617int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3618{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003619 if (dai->driver && dai->driver->ops->set_tristate)
3620 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003621 else
3622 return -EINVAL;
3623}
3624EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3625
3626/**
3627 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3628 * @dai: DAI
3629 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003630 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003631 *
3632 * Mutes the DAI DAC.
3633 */
Mark Brownda183962013-02-06 15:44:07 +00003634int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3635 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003636{
Mark Brownda183962013-02-06 15:44:07 +00003637 if (!dai->driver)
3638 return -ENOTSUPP;
3639
3640 if (dai->driver->ops->mute_stream)
3641 return dai->driver->ops->mute_stream(dai, mute, direction);
3642 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3643 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003644 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003645 else
Mark Brown04570c62012-04-13 19:16:03 +01003646 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003647}
3648EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3649
Benoit Cousson88bd8702014-07-08 23:19:34 +02003650static int snd_soc_init_multicodec(struct snd_soc_card *card,
3651 struct snd_soc_dai_link *dai_link)
3652{
3653 /* Legacy codec/codec_dai link is a single entry in multicodec */
3654 if (dai_link->codec_name || dai_link->codec_of_node ||
3655 dai_link->codec_dai_name) {
3656 dai_link->num_codecs = 1;
3657
3658 dai_link->codecs = devm_kzalloc(card->dev,
3659 sizeof(struct snd_soc_dai_link_component),
3660 GFP_KERNEL);
3661 if (!dai_link->codecs)
3662 return -ENOMEM;
3663
3664 dai_link->codecs[0].name = dai_link->codec_name;
3665 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3666 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3667 }
3668
3669 if (!dai_link->codecs) {
3670 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3671 return -EINVAL;
3672 }
3673
3674 return 0;
3675}
3676
Mark Brownc5af3a22008-11-28 13:29:45 +00003677/**
3678 * snd_soc_register_card - Register a card with the ASoC core
3679 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003680 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003681 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003682 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303683int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003684{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003685 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686
Mark Brownc5af3a22008-11-28 13:29:45 +00003687 if (!card->name || !card->dev)
3688 return -EINVAL;
3689
Stephen Warren5a504962011-12-21 10:40:59 -07003690 for (i = 0; i < card->num_links; i++) {
3691 struct snd_soc_dai_link *link = &card->dai_link[i];
3692
Benoit Cousson88bd8702014-07-08 23:19:34 +02003693 ret = snd_soc_init_multicodec(card, link);
3694 if (ret) {
3695 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3696 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003697 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003698
3699 for (j = 0; j < link->num_codecs; j++) {
3700 /*
3701 * Codec must be specified by 1 of name or OF node,
3702 * not both or neither.
3703 */
3704 if (!!link->codecs[j].name ==
3705 !!link->codecs[j].of_node) {
3706 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3707 link->name);
3708 return -EINVAL;
3709 }
3710 /* Codec DAI name must be specified */
3711 if (!link->codecs[j].dai_name) {
3712 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3713 link->name);
3714 return -EINVAL;
3715 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003716 }
Stephen Warren5a504962011-12-21 10:40:59 -07003717
3718 /*
3719 * Platform may be specified by either name or OF node, but
3720 * can be left unspecified, and a dummy platform will be used.
3721 */
3722 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003723 dev_err(card->dev,
3724 "ASoC: Both platform name/of_node are set for %s\n",
3725 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003726 return -EINVAL;
3727 }
3728
3729 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003730 * CPU device may be specified by either name or OF node, but
3731 * can be left unspecified, and will be matched based on DAI
3732 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003733 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003734 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003735 dev_err(card->dev,
3736 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3737 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003738 return -EINVAL;
3739 }
3740 /*
3741 * At least one of CPU DAI name or CPU device name/node must be
3742 * specified
3743 */
3744 if (!link->cpu_dai_name &&
3745 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003746 dev_err(card->dev,
3747 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3748 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003749 return -EINVAL;
3750 }
3751 }
3752
Mark Browned77cc12011-05-03 18:25:34 +01003753 dev_set_drvdata(card->dev, card);
3754
Stephen Warren111c6412011-01-28 14:26:35 -07003755 snd_soc_initialize_card_lists(card);
3756
Vinod Koul150dd2f2011-01-13 22:48:32 +05303757 soc_init_card_debugfs(card);
3758
Mark Brown181a6892012-03-14 20:18:49 +00003759 card->rtd = devm_kzalloc(card->dev,
3760 sizeof(struct snd_soc_pcm_runtime) *
3761 (card->num_links + card->num_aux_devs),
3762 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003763 if (card->rtd == NULL)
3764 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003765 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003766 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003767
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003768 for (i = 0; i < card->num_links; i++) {
3769 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003770 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003771 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3772 sizeof(struct snd_soc_dai *) *
3773 (card->rtd[i].dai_link->num_codecs),
3774 GFP_KERNEL);
3775 if (card->rtd[i].codec_dais == NULL)
3776 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003777 }
3778
3779 for (i = 0; i < card->num_aux_devs; i++)
3780 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003781
Mark Browndb432b42011-10-03 21:06:40 +01003782 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003783 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003784 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003785 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003786
Mark Brownb19e6e72012-03-14 21:18:39 +00003787 ret = snd_soc_instantiate_card(card);
3788 if (ret != 0)
3789 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003790
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003791 /* deactivate pins to sleep state */
3792 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003793 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3794 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3795 int j;
3796
3797 for (j = 0; j < rtd->num_codecs; j++) {
3798 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3799 if (!codec_dai->active)
3800 pinctrl_pm_select_sleep_state(codec_dai->dev);
3801 }
3802
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003803 if (!cpu_dai->active)
3804 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3805 }
3806
Mark Brownb19e6e72012-03-14 21:18:39 +00003807 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003808}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303809EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003810
3811/**
3812 * snd_soc_unregister_card - Unregister a card with the ASoC core
3813 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003814 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003815 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003816 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303817int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003818{
Vinod Koulb0e26482011-01-13 22:48:02 +05303819 if (card->instantiated)
3820 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003821 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003822
3823 return 0;
3824}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303825EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003827/*
3828 * Simplify DAI link configuration by removing ".-1" from device names
3829 * and sanitizing names.
3830 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003831static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003832{
3833 char *found, name[NAME_SIZE];
3834 int id1, id2;
3835
3836 if (dev_name(dev) == NULL)
3837 return NULL;
3838
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003839 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003840
3841 /* are we a "%s.%d" name (platform and SPI components) */
3842 found = strstr(name, dev->driver->name);
3843 if (found) {
3844 /* get ID */
3845 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3846
3847 /* discard ID from name if ID == -1 */
3848 if (*id == -1)
3849 found[strlen(dev->driver->name)] = '\0';
3850 }
3851
3852 } else {
3853 /* I2C component devices are named "bus-addr" */
3854 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3855 char tmp[NAME_SIZE];
3856
3857 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003858 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859
3860 /* sanitize component name for DAI link creation */
3861 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003862 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003863 } else
3864 *id = 0;
3865 }
3866
3867 return kstrdup(name, GFP_KERNEL);
3868}
3869
3870/*
3871 * Simplify DAI link naming for single devices with multiple DAIs by removing
3872 * any ".-1" and using the DAI name (instead of device name).
3873 */
3874static inline char *fmt_multiple_name(struct device *dev,
3875 struct snd_soc_dai_driver *dai_drv)
3876{
3877 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003878 dev_err(dev,
3879 "ASoC: error - multiple DAI %s registered with no name\n",
3880 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881 return NULL;
3882 }
3883
3884 return kstrdup(dai_drv->name, GFP_KERNEL);
3885}
3886
Mark Brown91151712008-11-30 23:31:24 +00003887/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003888 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003889 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003890 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003891 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003892static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003893{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003894 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003895
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003896 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003897 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3898 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003899 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003900 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003901 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902 }
Mark Brown91151712008-11-30 23:31:24 +00003903}
Mark Brown91151712008-11-30 23:31:24 +00003904
3905/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003906 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003907 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003908 * @component: The component the DAIs are registered for
3909 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003910 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003911 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3912 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003913 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003914static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003915 struct snd_soc_dai_driver *dai_drv, size_t count,
3916 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003917{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003918 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003919 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003920 unsigned int i;
3921 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003922
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003923 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003924
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003925 component->dai_drv = dai_drv;
3926 component->num_dai = count;
3927
Mark Brown91151712008-11-30 23:31:24 +00003928 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003929
3930 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003931 if (dai == NULL) {
3932 ret = -ENOMEM;
3933 goto err;
3934 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003935
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003936 /*
3937 * Back in the old days when we still had component-less DAIs,
3938 * instead of having a static name, component-less DAIs would
3939 * inherit the name of the parent device so it is possible to
3940 * register multiple instances of the DAI. We still need to keep
3941 * the same naming style even though those DAIs are not
3942 * component-less anymore.
3943 */
3944 if (count == 1 && legacy_dai_naming) {
3945 dai->name = fmt_single_name(dev, &dai->id);
3946 } else {
3947 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3948 if (dai_drv[i].id)
3949 dai->id = dai_drv[i].id;
3950 else
3951 dai->id = i;
3952 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 if (dai->name == NULL) {
3954 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003955 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003956 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 }
3958
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003959 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003961 dai->driver = &dai_drv[i];
3962 if (!dai->driver->ops)
3963 dai->driver->ops = &null_dai_ops;
3964
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003965 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003966
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003967 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003968 }
3969
3970 return 0;
3971
3972err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003973 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003974
3975 return ret;
3976}
Mark Brown91151712008-11-30 23:31:24 +00003977
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003978static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3979 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003980{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003981 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003982
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003983 component->driver->seq_notifier(component, type, subseq);
3984}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003985
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003986static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3987 int event)
3988{
3989 struct snd_soc_component *component = dapm->component;
3990
3991 return component->driver->stream_event(component, event);
3992}
3993
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003994static int snd_soc_component_initialize(struct snd_soc_component *component,
3995 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003996{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003997 struct snd_soc_dapm_context *dapm;
3998
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003999 component->name = fmt_single_name(dev, &component->id);
4000 if (!component->name) {
4001 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004002 return -ENOMEM;
4003 }
4004
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004005 component->dev = dev;
4006 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004007 component->probe = component->driver->probe;
4008 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004009
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004010 if (!component->dapm_ptr)
4011 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004012
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004013 dapm = component->dapm_ptr;
4014 dapm->dev = dev;
4015 dapm->component = component;
4016 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004017 if (driver->seq_notifier)
4018 dapm->seq_notifier = snd_soc_component_seq_notifier;
4019 if (driver->stream_event)
4020 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004021
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004022 component->controls = driver->controls;
4023 component->num_controls = driver->num_controls;
4024 component->dapm_widgets = driver->dapm_widgets;
4025 component->num_dapm_widgets = driver->num_dapm_widgets;
4026 component->dapm_routes = driver->dapm_routes;
4027 component->num_dapm_routes = driver->num_dapm_routes;
4028
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004029 INIT_LIST_HEAD(&component->dai_list);
4030 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004031
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004032 return 0;
4033}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004034
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004035static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4036{
4037 list_add(&component->list, &component_list);
4038}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004039
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004040static void snd_soc_component_add(struct snd_soc_component *component)
4041{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004042 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004043 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004044 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004045}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004046
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004047static void snd_soc_component_cleanup(struct snd_soc_component *component)
4048{
4049 snd_soc_unregister_dais(component);
4050 kfree(component->name);
4051}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004052
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004053static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4054{
4055 list_del(&component->list);
4056}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004057
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004058static void snd_soc_component_del(struct snd_soc_component *component)
4059{
4060 mutex_lock(&client_mutex);
4061 snd_soc_component_del_unlocked(component);
4062 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004063}
4064
4065int snd_soc_register_component(struct device *dev,
4066 const struct snd_soc_component_driver *cmpnt_drv,
4067 struct snd_soc_dai_driver *dai_drv,
4068 int num_dai)
4069{
4070 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004071 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004072
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004073 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004074 if (!cmpnt) {
4075 dev_err(dev, "ASoC: Failed to allocate memory\n");
4076 return -ENOMEM;
4077 }
4078
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004079 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4080 if (ret)
4081 goto err_free;
4082
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004083 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004084 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004085
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004086 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4087 if (ret < 0) {
4088 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4089 goto err_cleanup;
4090 }
4091
4092 snd_soc_component_add(cmpnt);
4093
4094 return 0;
4095
4096err_cleanup:
4097 snd_soc_component_cleanup(cmpnt);
4098err_free:
4099 kfree(cmpnt);
4100 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004101}
4102EXPORT_SYMBOL_GPL(snd_soc_register_component);
4103
4104/**
4105 * snd_soc_unregister_component - Unregister a component from the ASoC core
4106 *
4107 */
4108void snd_soc_unregister_component(struct device *dev)
4109{
4110 struct snd_soc_component *cmpnt;
4111
4112 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004113 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004114 goto found;
4115 }
4116 return;
4117
4118found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004119 snd_soc_component_del(cmpnt);
4120 snd_soc_component_cleanup(cmpnt);
4121 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004122}
4123EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4124
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004125static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4126{
4127 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4128
4129 return platform->driver->probe(platform);
4130}
4131
4132static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4133{
4134 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4135
4136 platform->driver->remove(platform);
4137}
4138
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004139static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4140 unsigned int reg, unsigned int val)
4141{
4142 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4143
4144 return platform->driver->write(platform, reg, val);
4145}
4146
4147static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4148 unsigned int reg, unsigned int *val)
4149{
4150 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4151
4152 *val = platform->driver->read(platform, reg);
4153
4154 return 0;
4155}
4156
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004157/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004158 * snd_soc_add_platform - Add a platform to the ASoC core
4159 * @dev: The parent device for the platform
4160 * @platform: The platform to add
4161 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004162 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004163int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004164 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004165{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004166 int ret;
4167
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004168 ret = snd_soc_component_initialize(&platform->component,
4169 &platform_drv->component_driver, dev);
4170 if (ret)
4171 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004172
4173 platform->dev = dev;
4174 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004175 if (platform_drv->controls) {
4176 platform->component.controls = platform_drv->controls;
4177 platform->component.num_controls = platform_drv->num_controls;
4178 }
4179 if (platform_drv->dapm_widgets) {
4180 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4181 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4182 platform->component.steal_sibling_dai_widgets = true;
4183 }
4184 if (platform_drv->dapm_routes) {
4185 platform->component.dapm_routes = platform_drv->dapm_routes;
4186 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4187 }
4188
4189 if (platform_drv->probe)
4190 platform->component.probe = snd_soc_platform_drv_probe;
4191 if (platform_drv->remove)
4192 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004193 if (platform_drv->write)
4194 platform->component.write = snd_soc_platform_drv_write;
4195 if (platform_drv->read)
4196 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004197
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004198#ifdef CONFIG_DEBUG_FS
4199 platform->component.debugfs_prefix = "platform";
4200#endif
4201
Mark Brown12a48a8c2008-12-03 19:40:30 +00004202 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004203 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004204 list_add(&platform->list, &platform_list);
4205 mutex_unlock(&client_mutex);
4206
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004207 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4208 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004209
4210 return 0;
4211}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004212EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4213
4214/**
4215 * snd_soc_register_platform - Register a platform with the ASoC core
4216 *
4217 * @platform: platform to register
4218 */
4219int snd_soc_register_platform(struct device *dev,
4220 const struct snd_soc_platform_driver *platform_drv)
4221{
4222 struct snd_soc_platform *platform;
4223 int ret;
4224
4225 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4226
4227 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4228 if (platform == NULL)
4229 return -ENOMEM;
4230
4231 ret = snd_soc_add_platform(dev, platform, platform_drv);
4232 if (ret)
4233 kfree(platform);
4234
4235 return ret;
4236}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004237EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4238
4239/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004240 * snd_soc_remove_platform - Remove a platform from the ASoC core
4241 * @platform: the platform to remove
4242 */
4243void snd_soc_remove_platform(struct snd_soc_platform *platform)
4244{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004245
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004246 mutex_lock(&client_mutex);
4247 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004248 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004249 mutex_unlock(&client_mutex);
4250
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004251 snd_soc_component_cleanup(&platform->component);
4252
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004253 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004254 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004255}
4256EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4257
4258struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4259{
4260 struct snd_soc_platform *platform;
4261
4262 list_for_each_entry(platform, &platform_list, list) {
4263 if (dev == platform->dev)
4264 return platform;
4265 }
4266
4267 return NULL;
4268}
4269EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4270
4271/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004272 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4273 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004274 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004275 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004276void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004277{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004278 struct snd_soc_platform *platform;
4279
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004280 platform = snd_soc_lookup_platform(dev);
4281 if (!platform)
4282 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004283
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004284 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004285 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004286}
4287EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4288
Mark Brown151ab222009-05-09 16:22:58 +01004289static u64 codec_format_map[] = {
4290 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4291 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4292 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4293 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4294 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4295 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4296 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4297 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4298 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4299 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4300 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4301 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4302 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4303 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4304 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4305 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4306};
4307
4308/* Fix up the DAI formats for endianness: codecs don't actually see
4309 * the endianness of the data but we're using the CPU format
4310 * definitions which do need to include endianness so we ensure that
4311 * codec DAIs always have both big and little endian variants set.
4312 */
4313static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4314{
4315 int i;
4316
4317 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4318 if (stream->formats & codec_format_map[i])
4319 stream->formats |= codec_format_map[i];
4320}
4321
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004322static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4323{
4324 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4325
4326 return codec->driver->probe(codec);
4327}
4328
4329static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4330{
4331 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4332
4333 codec->driver->remove(codec);
4334}
4335
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004336static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4337 unsigned int reg, unsigned int val)
4338{
4339 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4340
4341 return codec->driver->write(codec, reg, val);
4342}
4343
4344static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4345 unsigned int reg, unsigned int *val)
4346{
4347 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4348
4349 *val = codec->driver->read(codec, reg);
4350
4351 return 0;
4352}
4353
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004354static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4355 enum snd_soc_bias_level level)
4356{
4357 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4358
4359 return codec->driver->set_bias_level(codec, level);
4360}
4361
Mark Brown0d0cf002008-12-10 14:32:45 +00004362/**
4363 * snd_soc_register_codec - Register a codec with the ASoC core
4364 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004365 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004366 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004367int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004368 const struct snd_soc_codec_driver *codec_drv,
4369 struct snd_soc_dai_driver *dai_drv,
4370 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004371{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004372 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004373 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004374 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004375 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004377 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004378
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004379 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4380 if (codec == NULL)
4381 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004382
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004383 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004384 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004385
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004386 ret = snd_soc_component_initialize(&codec->component,
4387 &codec_drv->component_driver, dev);
4388 if (ret)
4389 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004390
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004391 if (codec_drv->controls) {
4392 codec->component.controls = codec_drv->controls;
4393 codec->component.num_controls = codec_drv->num_controls;
4394 }
4395 if (codec_drv->dapm_widgets) {
4396 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4397 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4398 }
4399 if (codec_drv->dapm_routes) {
4400 codec->component.dapm_routes = codec_drv->dapm_routes;
4401 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4402 }
4403
4404 if (codec_drv->probe)
4405 codec->component.probe = snd_soc_codec_drv_probe;
4406 if (codec_drv->remove)
4407 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004408 if (codec_drv->write)
4409 codec->component.write = snd_soc_codec_drv_write;
4410 if (codec_drv->read)
4411 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004412 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004413 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004414 if (codec_drv->seq_notifier)
4415 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004416 if (codec_drv->set_bias_level)
4417 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004418 codec->dev = dev;
4419 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004420 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004421 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004422
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004423#ifdef CONFIG_DEBUG_FS
4424 codec->component.init_debugfs = soc_init_codec_debugfs;
4425 codec->component.debugfs_prefix = "codec";
4426#endif
4427
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004428 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004429 if (codec_drv->get_regmap)
4430 regmap = codec_drv->get_regmap(dev);
4431 else
4432 regmap = dev_get_regmap(dev, NULL);
4433
4434 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004435 ret = snd_soc_component_init_io(&codec->component,
4436 regmap);
4437 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004438 dev_err(codec->dev,
4439 "Failed to set cache I/O:%d\n",
4440 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004441 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004442 }
4443 }
4444 }
4445
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004446 for (i = 0; i < num_dai; i++) {
4447 fixup_codec_formats(&dai_drv[i].playback);
4448 fixup_codec_formats(&dai_drv[i].capture);
4449 }
4450
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004451 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4452 if (ret < 0) {
4453 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4454 goto err_cleanup;
4455 }
4456
4457 list_for_each_entry(dai, &codec->component.dai_list, list)
4458 dai->codec = codec;
4459
Mark Brown054880f2012-04-09 17:29:19 +01004460 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004461 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004462 list_add(&codec->list, &codec_list);
4463 mutex_unlock(&client_mutex);
4464
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004465 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4466 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004467 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004468
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004469err_cleanup:
4470 snd_soc_component_cleanup(&codec->component);
4471err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004472 kfree(codec);
4473 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004474}
4475EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4476
4477/**
4478 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4479 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004480 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004481 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004482void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004483{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004484 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004485
4486 list_for_each_entry(codec, &codec_list, list) {
4487 if (dev == codec->dev)
4488 goto found;
4489 }
4490 return;
4491
4492found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004493
Mark Brown0d0cf002008-12-10 14:32:45 +00004494 mutex_lock(&client_mutex);
4495 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004496 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004497 mutex_unlock(&client_mutex);
4498
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004499 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4500 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004501
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004502 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004503 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004504 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004505}
4506EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4507
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004508/* Retrieve a card's name from device tree */
4509int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4510 const char *propname)
4511{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304512 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004513 int ret;
4514
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304515 if (!card->dev) {
4516 pr_err("card->dev is not set before calling %s\n", __func__);
4517 return -EINVAL;
4518 }
4519
4520 np = card->dev->of_node;
4521
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004522 ret = of_property_read_string_index(np, propname, 0, &card->name);
4523 /*
4524 * EINVAL means the property does not exist. This is fine providing
4525 * card->name was previously set, which is checked later in
4526 * snd_soc_register_card.
4527 */
4528 if (ret < 0 && ret != -EINVAL) {
4529 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004530 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004531 propname, ret);
4532 return ret;
4533 }
4534
4535 return 0;
4536}
4537EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4538
Xiubo Li9a6d4862014-02-08 15:59:52 +08004539static const struct snd_soc_dapm_widget simple_widgets[] = {
4540 SND_SOC_DAPM_MIC("Microphone", NULL),
4541 SND_SOC_DAPM_LINE("Line", NULL),
4542 SND_SOC_DAPM_HP("Headphone", NULL),
4543 SND_SOC_DAPM_SPK("Speaker", NULL),
4544};
4545
4546int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4547 const char *propname)
4548{
4549 struct device_node *np = card->dev->of_node;
4550 struct snd_soc_dapm_widget *widgets;
4551 const char *template, *wname;
4552 int i, j, num_widgets, ret;
4553
4554 num_widgets = of_property_count_strings(np, propname);
4555 if (num_widgets < 0) {
4556 dev_err(card->dev,
4557 "ASoC: Property '%s' does not exist\n", propname);
4558 return -EINVAL;
4559 }
4560 if (num_widgets & 1) {
4561 dev_err(card->dev,
4562 "ASoC: Property '%s' length is not even\n", propname);
4563 return -EINVAL;
4564 }
4565
4566 num_widgets /= 2;
4567 if (!num_widgets) {
4568 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4569 propname);
4570 return -EINVAL;
4571 }
4572
4573 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4574 GFP_KERNEL);
4575 if (!widgets) {
4576 dev_err(card->dev,
4577 "ASoC: Could not allocate memory for widgets\n");
4578 return -ENOMEM;
4579 }
4580
4581 for (i = 0; i < num_widgets; i++) {
4582 ret = of_property_read_string_index(np, propname,
4583 2 * i, &template);
4584 if (ret) {
4585 dev_err(card->dev,
4586 "ASoC: Property '%s' index %d read error:%d\n",
4587 propname, 2 * i, ret);
4588 return -EINVAL;
4589 }
4590
4591 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4592 if (!strncmp(template, simple_widgets[j].name,
4593 strlen(simple_widgets[j].name))) {
4594 widgets[i] = simple_widgets[j];
4595 break;
4596 }
4597 }
4598
4599 if (j >= ARRAY_SIZE(simple_widgets)) {
4600 dev_err(card->dev,
4601 "ASoC: DAPM widget '%s' is not supported\n",
4602 template);
4603 return -EINVAL;
4604 }
4605
4606 ret = of_property_read_string_index(np, propname,
4607 (2 * i) + 1,
4608 &wname);
4609 if (ret) {
4610 dev_err(card->dev,
4611 "ASoC: Property '%s' index %d read error:%d\n",
4612 propname, (2 * i) + 1, ret);
4613 return -EINVAL;
4614 }
4615
4616 widgets[i].name = wname;
4617 }
4618
4619 card->dapm_widgets = widgets;
4620 card->num_dapm_widgets = num_widgets;
4621
4622 return 0;
4623}
4624EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4625
Xiubo Li89c67852014-02-14 09:34:35 +08004626int snd_soc_of_parse_tdm_slot(struct device_node *np,
4627 unsigned int *slots,
4628 unsigned int *slot_width)
4629{
4630 u32 val;
4631 int ret;
4632
4633 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4634 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4635 if (ret)
4636 return ret;
4637
4638 if (slots)
4639 *slots = val;
4640 }
4641
4642 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4643 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4644 if (ret)
4645 return ret;
4646
4647 if (slot_width)
4648 *slot_width = val;
4649 }
4650
4651 return 0;
4652}
4653EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4654
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004655int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4656 const char *propname)
4657{
4658 struct device_node *np = card->dev->of_node;
4659 int num_routes;
4660 struct snd_soc_dapm_route *routes;
4661 int i, ret;
4662
4663 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004664 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004665 dev_err(card->dev,
4666 "ASoC: Property '%s' does not exist or its length is not even\n",
4667 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004668 return -EINVAL;
4669 }
4670 num_routes /= 2;
4671 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004672 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004673 propname);
4674 return -EINVAL;
4675 }
4676
4677 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4678 GFP_KERNEL);
4679 if (!routes) {
4680 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004681 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004682 return -EINVAL;
4683 }
4684
4685 for (i = 0; i < num_routes; i++) {
4686 ret = of_property_read_string_index(np, propname,
4687 2 * i, &routes[i].sink);
4688 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004689 dev_err(card->dev,
4690 "ASoC: Property '%s' index %d could not be read: %d\n",
4691 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004692 return -EINVAL;
4693 }
4694 ret = of_property_read_string_index(np, propname,
4695 (2 * i) + 1, &routes[i].source);
4696 if (ret) {
4697 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004698 "ASoC: Property '%s' index %d could not be read: %d\n",
4699 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004700 return -EINVAL;
4701 }
4702 }
4703
4704 card->num_dapm_routes = num_routes;
4705 card->dapm_routes = routes;
4706
4707 return 0;
4708}
4709EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4710
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004711unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004712 const char *prefix,
4713 struct device_node **bitclkmaster,
4714 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004715{
4716 int ret, i;
4717 char prop[128];
4718 unsigned int format = 0;
4719 int bit, frame;
4720 const char *str;
4721 struct {
4722 char *name;
4723 unsigned int val;
4724 } of_fmt_table[] = {
4725 { "i2s", SND_SOC_DAIFMT_I2S },
4726 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4727 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4728 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4729 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4730 { "ac97", SND_SOC_DAIFMT_AC97 },
4731 { "pdm", SND_SOC_DAIFMT_PDM},
4732 { "msb", SND_SOC_DAIFMT_MSB },
4733 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004734 };
4735
4736 if (!prefix)
4737 prefix = "";
4738
4739 /*
4740 * check "[prefix]format = xxx"
4741 * SND_SOC_DAIFMT_FORMAT_MASK area
4742 */
4743 snprintf(prop, sizeof(prop), "%sformat", prefix);
4744 ret = of_property_read_string(np, prop, &str);
4745 if (ret == 0) {
4746 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4747 if (strcmp(str, of_fmt_table[i].name) == 0) {
4748 format |= of_fmt_table[i].val;
4749 break;
4750 }
4751 }
4752 }
4753
4754 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004755 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004756 * SND_SOC_DAIFMT_CLOCK_MASK area
4757 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004758 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4759 if (of_get_property(np, prop, NULL))
4760 format |= SND_SOC_DAIFMT_CONT;
4761 else
4762 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004763
4764 /*
4765 * check "[prefix]bitclock-inversion"
4766 * check "[prefix]frame-inversion"
4767 * SND_SOC_DAIFMT_INV_MASK area
4768 */
4769 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4770 bit = !!of_get_property(np, prop, NULL);
4771
4772 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4773 frame = !!of_get_property(np, prop, NULL);
4774
4775 switch ((bit << 4) + frame) {
4776 case 0x11:
4777 format |= SND_SOC_DAIFMT_IB_IF;
4778 break;
4779 case 0x10:
4780 format |= SND_SOC_DAIFMT_IB_NF;
4781 break;
4782 case 0x01:
4783 format |= SND_SOC_DAIFMT_NB_IF;
4784 break;
4785 default:
4786 /* SND_SOC_DAIFMT_NB_NF is default */
4787 break;
4788 }
4789
4790 /*
4791 * check "[prefix]bitclock-master"
4792 * check "[prefix]frame-master"
4793 * SND_SOC_DAIFMT_MASTER_MASK area
4794 */
4795 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4796 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004797 if (bit && bitclkmaster)
4798 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004799
4800 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4801 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004802 if (frame && framemaster)
4803 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004804
4805 switch ((bit << 4) + frame) {
4806 case 0x11:
4807 format |= SND_SOC_DAIFMT_CBM_CFM;
4808 break;
4809 case 0x10:
4810 format |= SND_SOC_DAIFMT_CBM_CFS;
4811 break;
4812 case 0x01:
4813 format |= SND_SOC_DAIFMT_CBS_CFM;
4814 break;
4815 default:
4816 format |= SND_SOC_DAIFMT_CBS_CFS;
4817 break;
4818 }
4819
4820 return format;
4821}
4822EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4823
Kuninori Morimotocb470082013-09-10 17:39:56 -07004824int snd_soc_of_get_dai_name(struct device_node *of_node,
4825 const char **dai_name)
4826{
4827 struct snd_soc_component *pos;
4828 struct of_phandle_args args;
4829 int ret;
4830
4831 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4832 "#sound-dai-cells", 0, &args);
4833 if (ret)
4834 return ret;
4835
4836 ret = -EPROBE_DEFER;
4837
4838 mutex_lock(&client_mutex);
4839 list_for_each_entry(pos, &component_list, list) {
4840 if (pos->dev->of_node != args.np)
4841 continue;
4842
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004843 if (pos->driver->of_xlate_dai_name) {
4844 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4845 } else {
4846 int id = -1;
4847
4848 switch (args.args_count) {
4849 case 0:
4850 id = 0; /* same as dai_drv[0] */
4851 break;
4852 case 1:
4853 id = args.args[0];
4854 break;
4855 default:
4856 /* not supported */
4857 break;
4858 }
4859
4860 if (id < 0 || id >= pos->num_dai) {
4861 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004862 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004863 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004864
4865 ret = 0;
4866
4867 *dai_name = pos->dai_drv[id].name;
4868 if (!*dai_name)
4869 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004870 }
4871
Kuninori Morimotocb470082013-09-10 17:39:56 -07004872 break;
4873 }
4874 mutex_unlock(&client_mutex);
4875
4876 of_node_put(args.np);
4877
4878 return ret;
4879}
4880EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4881
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004882static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004883{
Mark Brown384c89e2008-12-03 17:34:03 +00004884#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004885 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4886 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004887 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004888 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004889 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004890
Mark Brown8a9dab12011-01-10 22:25:21 +00004891 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004892 &codec_list_fops))
4893 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4894
Mark Brown8a9dab12011-01-10 22:25:21 +00004895 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004896 &dai_list_fops))
4897 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004898
Mark Brown8a9dab12011-01-10 22:25:21 +00004899 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004900 &platform_list_fops))
4901 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004902#endif
4903
Mark Brownfb257892011-04-28 10:57:54 +01004904 snd_soc_util_init();
4905
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004906 return platform_driver_register(&soc_driver);
4907}
Mark Brown4abe8e12010-10-12 17:41:03 +01004908module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004909
Mark Brown7d8c16a2008-11-30 22:11:24 +00004910static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004911{
Mark Brownfb257892011-04-28 10:57:54 +01004912 snd_soc_util_exit();
4913
Mark Brown384c89e2008-12-03 17:34:03 +00004914#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004915 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004916#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004917 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004918}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004919module_exit(snd_soc_exit);
4920
4921/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004922MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004923MODULE_DESCRIPTION("ALSA SoC Core");
4924MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004925MODULE_ALIAS("platform:soc-audio");