blob: 4393bc33d3afce323267af4799d1bf91bd3c262d [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200314 debugfs_create_bool("cache_only", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000315 &codec->cache_only);
316
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200318 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000319 codec, &codec_reg_fops);
320 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200321 dev_warn(codec->dev,
322 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000323}
324
Mark Brownc3c5a192010-09-15 18:15:14 +0100325static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100330 struct snd_soc_codec *codec;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(codec, &codec_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200337 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100345
346 if (ret >= 0)
347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
348
349 kfree(buf);
350
351 return ret;
352}
353
354static const struct file_operations codec_list_fops = {
355 .read = codec_list_read_file,
356 .llseek = default_llseek,/* read accesses f_pos */
357};
358
Mark Brownf3208782010-09-15 18:19:07 +0100359static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
360 size_t count, loff_t *ppos)
361{
362 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100364 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100365 struct snd_soc_dai *dai;
366
367 if (!buf)
368 return -ENOMEM;
369
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100370 list_for_each_entry(component, &component_list, list) {
371 list_for_each_entry(dai, &component->dai_list, list) {
372 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
373 dai->name);
374 if (len >= 0)
375 ret += len;
376 if (ret > PAGE_SIZE) {
377 ret = PAGE_SIZE;
378 break;
379 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 }
381 }
Mark Brownf3208782010-09-15 18:19:07 +0100382
Mark Brown2b194f9d2010-10-13 10:52:16 +0100383 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100384
385 kfree(buf);
386
387 return ret;
388}
389
390static const struct file_operations dai_list_fops = {
391 .read = dai_list_read_file,
392 .llseek = default_llseek,/* read accesses f_pos */
393};
394
Mark Brown19c7ac22010-09-15 18:22:40 +0100395static ssize_t platform_list_read_file(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398{
399 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100401 struct snd_soc_platform *platform;
402
403 if (!buf)
404 return -ENOMEM;
405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 list_for_each_entry(platform, &platform_list, list) {
407 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200408 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 if (len >= 0)
410 ret += len;
411 if (ret > PAGE_SIZE) {
412 ret = PAGE_SIZE;
413 break;
414 }
415 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
Mark Brown2b194f9d2010-10-13 10:52:16 +0100417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100418
419 kfree(buf);
420
421 return ret;
422}
423
424static const struct file_operations platform_list_fops = {
425 .read = platform_list_read_file,
426 .llseek = default_llseek,/* read accesses f_pos */
427};
428
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429static void soc_init_card_debugfs(struct snd_soc_card *card)
430{
431 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000432 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200433 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100435 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200436 return;
437 }
438
439 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
440 card->debugfs_card_root,
441 &card->pop_time);
442 if (!card->debugfs_pop_time)
443 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000444 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200445}
446
447static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449 debugfs_remove_recursive(card->debugfs_card_root);
450}
451
Mark Brown2624d5f2009-11-03 21:56:13 +0000452#else
453
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200454#define soc_init_codec_debugfs NULL
455
456static inline void soc_init_component_debugfs(
457 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000458{
459}
460
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200461static inline void soc_cleanup_component_debugfs(
462 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463{
464}
465
Axel Linb95fccb2010-11-09 17:06:44 +0800466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100475struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
476 const char *dai_link, int stream)
477{
478 int i;
479
480 for (i = 0; i < card->num_links; i++) {
481 if (card->rtd[i].dai_link->no_pcm &&
482 !strcmp(card->rtd[i].dai_link->name, dai_link))
483 return card->rtd[i].pcm->streams[stream].substream;
484 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000485 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100486 return NULL;
487}
488EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
489
490struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
491 const char *dai_link)
492{
493 int i;
494
495 for (i = 0; i < card->num_links; i++) {
496 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
497 return &card->rtd[i];
498 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000499 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100500 return NULL;
501}
502EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
503
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504#ifdef CONFIG_SND_SOC_AC97_BUS
505/* unregister ac97 codec */
506static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
507{
508 if (codec->ac97->dev.bus)
509 device_unregister(&codec->ac97->dev);
510 return 0;
511}
512
513/* stop no dev release warning */
514static void soc_ac97_device_release(struct device *dev){}
515
516/* register ac97 codec to bus */
517static int soc_ac97_dev_register(struct snd_soc_codec *codec)
518{
519 int err;
520
521 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200522 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 codec->ac97->dev.release = soc_ac97_device_release;
524
Kay Sieversbb072bf2008-11-02 03:50:35 +0100525 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200526 codec->component.card->snd_card->number, 0,
527 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 err = device_register(&codec->ac97->dev);
529 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000530 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 codec->ac97->dev.bus = NULL;
532 return err;
533 }
534 return 0;
535}
536#endif
537
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100538static void codec2codec_close_delayed_work(struct work_struct *work)
539{
540 /* Currently nothing to do for c2c links
541 * Since c2c links are internal nodes in the DAPM graph and
542 * don't interface with the outside world or application layer
543 * we don't have to do any special handling on close.
544 */
545}
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200553 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 continue;
576
Benoit Cousson88bd8702014-07-08 23:19:34 +0200577 for (j = 0; j < card->rtd[i].num_codecs; j++) {
578 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
579 struct snd_soc_dai_driver *drv = dai->driver;
580
581 if (drv->ops->digital_mute && dai->playback_active)
582 drv->ops->digital_mute(dai, 1);
583 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200584 }
585
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100586 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100589 continue;
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100593
Mark Brown87506542008-11-18 20:50:34 +0000594 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000595 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 for (i = 0; i < card->num_rtd; i++) {
598 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
599 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
605 cpu_dai->driver->suspend(cpu_dai);
606 if (platform->driver->suspend && !platform->suspended) {
607 platform->driver->suspend(cpu_dai);
608 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100609 }
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 /* close any waiting streams and save state */
613 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200614 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200616 for (j = 0; j < card->rtd[i].num_codecs; j++) {
617 codec_dais[j]->codec->dapm.suspend_bias_level =
618 codec_dais[j]->codec->dapm.bias_level;
619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623
624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 }
635
Mark Browne2d32ff2012-08-31 17:38:32 -0700636 /* Recheck all analogue paths too */
637 dapm_mark_io_dirty(&card->dapm);
638 snd_soc_dapm_sync(&card->dapm);
639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200641 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 /* If there are paths active then the CODEC will be held with
643 * bias _ON and should not be suspended. */
644 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200645 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000647 /*
648 * If the CODEC is capable of idle
649 * bias off then being in STANDBY
650 * means it's doing something,
651 * otherwise fall through.
652 */
653 if (codec->dapm.idle_bias_off) {
654 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200655 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000656 break;
657 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100659 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900661 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200662 if (codec->component.regmap)
663 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800664 /* deactivate pins to sleep state */
665 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200668 dev_dbg(codec->dev,
669 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 }
672 }
673 }
674
675 for (i = 0; i < card->num_rtd; i++) {
676 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
677
678 if (card->rtd[i].dai_link->ignore_suspend)
679 continue;
680
681 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
682 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683
684 /* deactivate pins to sleep state */
685 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 }
687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
691 return 0;
692}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000693EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Andy Green6ed25972008-06-13 16:24:05 +0100695/* deferred resume work, so resume can complete before we finished
696 * setting our codec back up, which can be very slow on I2C
697 */
698static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 struct snd_soc_card *card =
701 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200703 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Andy Green6ed25972008-06-13 16:24:05 +0100705 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
706 * so userspace apps are blocked from touching us
707 */
708
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000709 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100710
Mark Brown99497882010-05-07 20:24:05 +0100711 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 /* resume AC97 DAIs */
718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100722 continue;
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
725 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 /* If the CODEC was idle over suspend then it will have been
730 * left with bias OFF or STANDBY and suspended so we must now
731 * resume. Otherwise the suspend was suppressed.
732 */
733 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200734 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 case SND_SOC_BIAS_STANDBY:
736 case SND_SOC_BIAS_OFF:
737 codec->driver->resume(codec);
738 codec->suspended = 0;
739 break;
740 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200741 dev_dbg(codec->dev,
742 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 break;
744 }
Mark Brown1547aba2010-05-07 21:11:40 +0100745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100751 continue;
752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Mark Brown3ff3f642008-05-19 12:32:25 +0200762 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100766 continue;
767
Benoit Cousson88bd8702014-07-08 23:19:34 +0200768 for (j = 0; j < card->rtd[i].num_codecs; j++) {
769 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
770 struct snd_soc_dai_driver *drv = dai->driver;
771
772 if (drv->ops->digital_mute && dai->playback_active)
773 drv->ops->digital_mute(dai, 0);
774 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 }
776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 for (i = 0; i < card->num_rtd; i++) {
778 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
779 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100782 continue;
783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000793 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000795 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100796
797 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700799
800 /* Recheck all analogue paths too */
801 dapm_mark_io_dirty(&card->dapm);
802 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100803}
804
805/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100807{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200810
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800811 /* If the initialization of this soc device failed, there is no codec
812 * associated with it. Just bail out in this case.
813 */
814 if (list_empty(&card->codec_dev_list))
815 return 0;
816
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800817 /* activate pins from sleep state */
818 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
820 struct snd_soc_dai **codec_dais = rtd->codec_dais;
821 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
822 int j;
823
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800824 if (cpu_dai->active)
825 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200826
827 for (j = 0; j < rtd->num_codecs; j++) {
828 struct snd_soc_dai *codec_dai = codec_dais[j];
829 if (codec_dai->active)
830 pinctrl_pm_select_default_state(codec_dai->dev);
831 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800832 }
833
Mark Brown64ab9ba2009-03-31 11:27:03 +0100834 /* AC97 devices might have other drivers hanging off them so
835 * need to resume immediately. Other drivers don't have that
836 * problem and may take a substantial amount of time to resume
837 * due to I/O costs and anti-pop so handle them out of line.
838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839 for (i = 0; i < card->num_rtd; i++) {
840 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600841 ac97_control |= cpu_dai->driver->ac97_control;
842 }
843 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600845 soc_resume_deferred(&card->deferred_resume_work);
846 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000847 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600848 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000849 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100850 }
Andy Green6ed25972008-06-13 16:24:05 +0100851
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852 return 0;
853}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000854EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000856#define snd_soc_suspend NULL
857#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858#endif
859
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100860static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800861};
862
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200863static struct snd_soc_component *soc_find_component(
864 const struct device_node *of_node, const char *name)
865{
866 struct snd_soc_component *component;
867
868 list_for_each_entry(component, &component_list, list) {
869 if (of_node) {
870 if (component->dev->of_node == of_node)
871 return component;
872 } else if (strcmp(component->name, name) == 0) {
873 return component;
874 }
875 }
876
877 return NULL;
878}
879
Benoit Cousson88bd8702014-07-08 23:19:34 +0200880static struct snd_soc_codec *soc_find_codec(
881 const struct device_node *codec_of_node,
882 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883{
884 struct snd_soc_codec *codec;
885
886 list_for_each_entry(codec, &codec_list, list) {
887 if (codec_of_node) {
888 if (codec->dev->of_node != codec_of_node)
889 continue;
890 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200891 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892 continue;
893 }
894
895 return codec;
896 }
897
898 return NULL;
899}
900
901static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
902 const char *codec_dai_name)
903{
904 struct snd_soc_dai *codec_dai;
905
906 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
907 if (!strcmp(codec_dai->name, codec_dai_name)) {
908 return codec_dai;
909 }
910 }
911
912 return NULL;
913}
914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
918 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100919 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
921 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000922 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100923 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000927 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000928
Mark Brownb19e6e72012-03-14 21:18:39 +0000929 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100930 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600931 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100932 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600933 continue;
934 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100935 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600936 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100937 list_for_each_entry(cpu_dai, &component->dai_list, list) {
938 if (dai_link->cpu_dai_name &&
939 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
940 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700941
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100942 rtd->cpu_dai = cpu_dai;
943 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000945
946 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000947 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000949 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000950 }
951
Benoit Cousson88bd8702014-07-08 23:19:34 +0200952 rtd->num_codecs = dai_link->num_codecs;
953
954 /* Find CODEC from registered CODECs */
955 for (i = 0; i < rtd->num_codecs; i++) {
956 struct snd_soc_codec *codec;
957 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
958 if (!codec) {
959 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
960 codecs[i].name);
961 return -EPROBE_DEFER;
962 }
963
964 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
965 if (!codec_dais[i]) {
966 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
967 codecs[i].dai_name);
968 return -EPROBE_DEFER;
969 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000970 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100971
Benoit Cousson88bd8702014-07-08 23:19:34 +0200972 /* Single codec links expect codec and codec_dai in runtime data */
973 rtd->codec_dai = codec_dais[0];
974 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100975
Mark Brown848dd8b2011-04-27 18:16:32 +0100976 /* if there's no platform we match on the empty platform */
977 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700978 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100979 platform_name = "snd-soc-dummy";
980
Mark Brownb19e6e72012-03-14 21:18:39 +0000981 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700983 if (dai_link->platform_of_node) {
984 if (platform->dev->of_node !=
985 dai_link->platform_of_node)
986 continue;
987 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200988 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700989 continue;
990 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700991
992 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000993 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000994 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000995 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000997 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000999
1000 card->num_rtd++;
1001
1002 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003}
1004
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001005static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -06001006{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001007 /* This is a HACK and will be removed soon */
1008 if (component->codec)
1009 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -06001010
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001011 if (component->remove)
1012 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -06001013
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001014 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -06001015
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001016 soc_cleanup_component_debugfs(component);
1017 component->probed = 0;
1018 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001019}
1020
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001021static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1022{
1023 int err;
1024
1025 if (codec_dai && codec_dai->probed &&
1026 codec_dai->driver->remove_order == order) {
1027 if (codec_dai->driver->remove) {
1028 err = codec_dai->driver->remove(codec_dai);
1029 if (err < 0)
1030 dev_err(codec_dai->dev,
1031 "ASoC: failed to remove %s: %d\n",
1032 codec_dai->name, err);
1033 }
1034 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001035 }
1036}
1037
Stephen Warren62ae68f2012-06-08 12:34:23 -06001038static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039{
1040 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1042 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043
1044 /* unregister the rtd device */
1045 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001046 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1047 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1048 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 rtd->dev_registered = 0;
1050 }
1051
1052 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001053 for (i = 0; i < rtd->num_codecs; i++)
1054 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001057 if (cpu_dai && cpu_dai->probed &&
1058 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059 if (cpu_dai->driver->remove) {
1060 err = cpu_dai->driver->remove(cpu_dai);
1061 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001062 dev_err(cpu_dai->dev,
1063 "ASoC: failed to remove %s: %d\n",
1064 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 }
1066 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001067 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001068 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001069 }
1070}
1071
Stephen Warren62ae68f2012-06-08 12:34:23 -06001072static void soc_remove_link_components(struct snd_soc_card *card, int num,
1073 int order)
1074{
1075 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1076 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001077 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001078 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001079 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001080
1081 /* remove the platform */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001082 if (platform && platform->component.probed &&
1083 platform->component.driver->remove_order == order)
1084 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001085
1086 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001087 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001088 component = rtd->codec_dais[i]->component;
1089 if (component->probed &&
1090 component->driver->remove_order == order)
1091 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001092 }
1093
1094 /* remove any CPU-side CODEC */
1095 if (cpu_dai) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001096 if (cpu_dai->component->probed &&
1097 cpu_dai->component->driver->remove_order == order)
1098 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001099 }
1100}
1101
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001102static void soc_remove_dai_links(struct snd_soc_card *card)
1103{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001104 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001105
Liam Girdwood0168bf02011-06-07 16:08:05 +01001106 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1107 order++) {
1108 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001109 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001110 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001111
1112 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1113 order++) {
1114 for (dai = 0; dai < card->num_rtd; dai++)
1115 soc_remove_link_components(card, dai, order);
1116 }
1117
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001118 card->num_rtd = 0;
1119}
1120
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001121static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001122 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001123{
1124 int i;
1125
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001126 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001127 return;
1128
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001129 for (i = 0; i < card->num_configs; i++) {
1130 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001131 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001132 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001133 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001134 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001135 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001136 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001137 }
1138}
1139
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001140static int soc_probe_component(struct snd_soc_card *card,
1141 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001143 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1144 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001145 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001147
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001148 component->card = card;
1149 dapm->card = card;
1150 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001151
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001152 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001153 return -ENODEV;
1154
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001155 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001156
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001157 if (component->dapm_widgets) {
1158 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1159 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001160
1161 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001162 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001163 "Failed to create new controls %d\n", ret);
1164 goto err_probe;
1165 }
1166 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001167
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001168 /*
1169 * This is rather ugly, but certain platforms expect that the DAPM
1170 * widgets for the DAIs for components with the same parent device are
1171 * created in the platforms DAPM context. Until that is fixed we need to
1172 * keep this.
1173 */
1174 if (component->steal_sibling_dai_widgets) {
1175 dai_component = NULL;
1176 list_for_each_entry(component2, &component_list, list) {
1177 if (component == component2)
1178 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001179
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001180 if (component2->dev == component->dev &&
1181 !list_empty(&component2->dai_list)) {
1182 dai_component = component2;
1183 break;
1184 }
1185 }
1186 } else {
1187 dai_component = component;
1188 list_for_each_entry(component2, &component_list, list) {
1189 if (component2->dev == component->dev &&
1190 component2->steal_sibling_dai_widgets) {
1191 dai_component = NULL;
1192 break;
1193 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001194 }
1195 }
Mark Brown888df392012-02-16 19:37:51 -08001196
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001197 if (dai_component) {
1198 list_for_each_entry(dai, &dai_component->dai_list, list) {
1199 snd_soc_dapm_new_dai_widgets(dapm, dai);
1200 if (ret != 0) {
1201 dev_err(component->dev,
1202 "Failed to create DAI widgets %d\n",
1203 ret);
1204 goto err_probe;
1205 }
1206 }
1207 }
Mark Brown33c5f962011-08-22 18:40:30 +01001208
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001209 if (component->probe) {
1210 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001211 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001212 dev_err(component->dev,
1213 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001214 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001215 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001216
1217 WARN(dapm->idle_bias_off &&
1218 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001219 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001220 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001221 }
1222
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001223 if (component->controls)
1224 snd_soc_add_component_controls(component, component->controls,
1225 component->num_controls);
1226 if (component->dapm_routes)
1227 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1228 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001229
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001230 component->probed = 1;
1231 list_add(&dapm->list, &card->dapm_list);
1232
1233 /* This is a HACK and will be removed soon */
1234 if (component->codec)
1235 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001236
Jarkko Nikula70d29332011-01-27 16:24:22 +02001237 return 0;
1238
1239err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001240 soc_cleanup_component_debugfs(component);
1241 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001242
1243 return ret;
1244}
1245
Mark Brown36ae1a92012-01-06 17:12:45 -08001246static void rtd_release(struct device *dev)
1247{
1248 kfree(dev);
1249}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001250
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001251static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1252 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001253{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001254 int ret = 0;
1255
Jarkko Nikula589c3562010-12-06 16:27:07 +02001256 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001257 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1258 if (!rtd->dev)
1259 return -ENOMEM;
1260 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001261 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001262 rtd->dev->release = rtd_release;
1263 rtd->dev->init_name = name;
1264 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001265 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001266 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1267 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1268 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1269 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001270 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001271 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001272 /* calling put_device() here to free the rtd->dev */
1273 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001274 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001275 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276 return ret;
1277 }
1278 rtd->dev_registered = 1;
1279
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001280 if (rtd->codec) {
1281 /* add DAPM sysfs entries for this codec */
1282 ret = snd_soc_dapm_sys_add(rtd->dev);
1283 if (ret < 0)
1284 dev_err(rtd->dev,
1285 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1286 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001288 /* add codec sysfs entries */
1289 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1290 if (ret < 0)
1291 dev_err(rtd->dev,
1292 "ASoC: failed to add codec sysfs files: %d\n",
1293 ret);
1294 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295
1296 return 0;
1297}
1298
Stephen Warren62ae68f2012-06-08 12:34:23 -06001299static int soc_probe_link_components(struct snd_soc_card *card, int num,
1300 int order)
1301{
1302 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001303 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001304 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001305 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001306
1307 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001308 component = rtd->cpu_dai->component;
1309 if (!component->probed &&
1310 component->driver->probe_order == order) {
1311 ret = soc_probe_component(card, component);
1312 if (ret < 0)
1313 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001314 }
1315
Benoit Cousson88bd8702014-07-08 23:19:34 +02001316 /* probe the CODEC-side components */
1317 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001318 component = rtd->codec_dais[i]->component;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001319 if (!component->probed &&
1320 component->driver->probe_order == order) {
1321 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001322 if (ret < 0)
1323 return ret;
1324 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001325 }
1326
1327 /* probe the platform */
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001328 if (!platform->component.probed &&
1329 platform->component.driver->probe_order == order) {
1330 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001331 if (ret < 0)
1332 return ret;
1333 }
1334
1335 return 0;
1336}
1337
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001338static int soc_probe_codec_dai(struct snd_soc_card *card,
1339 struct snd_soc_dai *codec_dai,
1340 int order)
1341{
1342 int ret;
1343
1344 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1345 if (codec_dai->driver->probe) {
1346 ret = codec_dai->driver->probe(codec_dai);
1347 if (ret < 0) {
1348 dev_err(codec_dai->dev,
1349 "ASoC: failed to probe CODEC DAI %s: %d\n",
1350 codec_dai->name, ret);
1351 return ret;
1352 }
1353 }
1354
1355 /* mark codec_dai as probed and add to card dai list */
1356 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001357 }
1358
1359 return 0;
1360}
1361
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001362static int soc_link_dai_widgets(struct snd_soc_card *card,
1363 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001364 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001365{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001366 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1367 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001368 struct snd_soc_dapm_widget *play_w, *capture_w;
1369 int ret;
1370
Benoit Cousson88bd8702014-07-08 23:19:34 +02001371 if (rtd->num_codecs > 1)
1372 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1373
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001374 /* link the DAI widgets */
1375 play_w = codec_dai->playback_widget;
1376 capture_w = cpu_dai->capture_widget;
1377 if (play_w && capture_w) {
1378 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1379 capture_w, play_w);
1380 if (ret != 0) {
1381 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1382 play_w->name, capture_w->name, ret);
1383 return ret;
1384 }
1385 }
1386
1387 play_w = cpu_dai->playback_widget;
1388 capture_w = codec_dai->capture_widget;
1389 if (play_w && capture_w) {
1390 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1391 capture_w, play_w);
1392 if (ret != 0) {
1393 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1394 play_w->name, capture_w->name, ret);
1395 return ret;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
Stephen Warren62ae68f2012-06-08 12:34:23 -06001402static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001403{
1404 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1405 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001407 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001408 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001410 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001411 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412
1413 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001414 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001415 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001416 for (i = 0; i < rtd->num_codecs; i++)
1417 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001418
1419 /* set default power off timeout */
1420 rtd->pmdown_time = pmdown_time;
1421
1422 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001423 if (!cpu_dai->probed &&
1424 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001425 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001426 if (!try_module_get(cpu_dai->dev->driver->owner))
1427 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001428 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001429
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001430 if (cpu_dai->driver->probe) {
1431 ret = cpu_dai->driver->probe(cpu_dai);
1432 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001433 dev_err(cpu_dai->dev,
1434 "ASoC: failed to probe CPU DAI %s: %d\n",
1435 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001436 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437 return ret;
1438 }
1439 }
1440 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001441 }
1442
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001444 for (i = 0; i < rtd->num_codecs; i++) {
1445 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1446 if (ret)
1447 return ret;
1448 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Liam Girdwood0168bf02011-06-07 16:08:05 +01001450 /* complete DAI probe during last probe */
1451 if (order != SND_SOC_COMP_ORDER_LAST)
1452 return 0;
1453
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001454 /* do machine specific initialization */
1455 if (dai_link->init) {
1456 ret = dai_link->init(rtd);
1457 if (ret < 0) {
1458 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1459 dai_link->name, ret);
1460 return ret;
1461 }
1462 }
1463
1464 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001465 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001467
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001468#ifdef CONFIG_DEBUG_FS
1469 /* add DPCM sysfs entries */
1470 if (dai_link->dynamic) {
1471 ret = soc_dpcm_debugfs_add(rtd);
1472 if (ret < 0) {
1473 dev_err(rtd->dev,
1474 "ASoC: failed to add dpcm sysfs entries: %d\n",
1475 ret);
1476 return ret;
1477 }
1478 }
1479#endif
1480
Mark Brown36ae1a92012-01-06 17:12:45 -08001481 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001483 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1484 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
Namarta Kohli1245b702012-08-16 17:10:41 +05301486 if (cpu_dai->driver->compress_dai) {
1487 /*create compress_device"*/
1488 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001489 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001490 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301491 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001492 return ret;
1493 }
1494 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301495
1496 if (!dai_link->params) {
1497 /* create the pcm */
1498 ret = soc_new_pcm(rtd, num);
1499 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001500 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301501 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001502 return ret;
1503 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301504 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001505 INIT_DELAYED_WORK(&rtd->delayed_work,
1506 codec2codec_close_delayed_work);
1507
Namarta Kohli1245b702012-08-16 17:10:41 +05301508 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001509 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001510 if (ret)
1511 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001512 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001513 }
1514
1515 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001516 for (i = 0; i < rtd->num_codecs; i++) {
1517 if (rtd->codec_dais[i]->driver->ac97_control)
1518 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1519 rtd->cpu_dai->ac97_pdata);
1520 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001521
1522 return 0;
1523}
1524
1525#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001526static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1527 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001528{
1529 int ret;
1530
1531 /* Only instantiate AC97 if not already done by the adaptor
1532 * for the generic AC97 subsystem.
1533 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001534 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001535 /*
1536 * It is possible that the AC97 device is already registered to
1537 * the device subsystem. This happens when the device is created
1538 * via snd_ac97_mixer(). Currently only SoC codec that does so
1539 * is the generic AC97 glue but others migh emerge.
1540 *
1541 * In those cases we don't try to register the device again.
1542 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001543 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001544 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001546 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001548 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001549 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001550 return ret;
1551 }
1552
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001553 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554 }
1555 return 0;
1556}
1557
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001558static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001559{
1560 if (codec->ac97_registered) {
1561 soc_ac97_dev_unregister(codec);
1562 codec->ac97_registered = 0;
1563 }
1564}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001565
Benoit Cousson88bd8702014-07-08 23:19:34 +02001566static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1567{
1568 int i, ret;
1569
1570 for (i = 0; i < rtd->num_codecs; i++) {
1571 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1572
1573 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1574 if (ret) {
1575 while (--i >= 0)
1576 soc_unregister_ac97_codec(codec_dai->codec);
1577 return ret;
1578 }
1579 }
1580
1581 return 0;
1582}
1583
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001584static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1585{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001586 int i;
1587
1588 for (i = 0; i < rtd->num_codecs; i++)
1589 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001590}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591#endif
1592
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001593static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001594{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001595 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001596 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001597 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001598
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001599 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1600 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001601 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001602 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001603
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001604 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001605 return -EPROBE_DEFER;
1606 }
1607
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001608 /*
1609 * Some places still reference rtd->codec, so we have to keep that
1610 * initialized if the component is a CODEC. Once all those references
1611 * have been removed, this code can be removed as well.
1612 */
1613 rtd->codec = rtd->component->codec;
1614
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001615 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001616}
1617
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001618static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1619{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001620 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001621 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001622 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001623
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001624 if (rtd->component->probed) {
1625 dev_err(rtd->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001626 return -EBUSY;
1627 }
1628
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001629 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001630 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001631 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001632
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001633 /* do machine specific initialization */
1634 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001635 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001636 if (ret < 0) {
1637 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1638 aux_dev->name, ret);
1639 return ret;
1640 }
1641 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001642
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001643 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001644}
1645
1646static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1647{
1648 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001649 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001650
1651 /* unregister the rtd device */
1652 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001653 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001654 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001655 rtd->dev_registered = 0;
1656 }
1657
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001658 if (component && component->probed)
1659 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001660}
1661
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001662static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001663{
1664 int ret;
1665
1666 if (codec->cache_init)
1667 return 0;
1668
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001669 ret = snd_soc_cache_init(codec);
1670 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001671 dev_err(codec->dev,
1672 "ASoC: Failed to set cache compression type: %d\n",
1673 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001674 return ret;
1675 }
1676 codec->cache_init = 1;
1677 return 0;
1678}
1679
Mark Brownb19e6e72012-03-14 21:18:39 +00001680static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001681{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001682 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001683 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001684 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685
Liam Girdwood01b9d992012-03-07 10:38:25 +00001686 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001687
Mark Brownb19e6e72012-03-14 21:18:39 +00001688 /* bind DAIs */
1689 for (i = 0; i < card->num_links; i++) {
1690 ret = soc_bind_dai_link(card, i);
1691 if (ret != 0)
1692 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001693 }
1694
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001695 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001696 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001697 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001698 if (ret != 0)
1699 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 }
1701
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001702 /* initialize the register cache for each available codec */
1703 list_for_each_entry(codec, &codec_list, list) {
1704 if (codec->cache_init)
1705 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001706 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001707 if (ret < 0)
1708 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001709 }
1710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001712 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713 card->owner, 0, &card->snd_card);
1714 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001715 dev_err(card->dev,
1716 "ASoC: can't create sound card for card %s: %d\n",
1717 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001718 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001719 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720
Mark Browne37a4972011-03-02 18:21:57 +00001721 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1722 card->dapm.dev = card->dev;
1723 card->dapm.card = card;
1724 list_add(&card->dapm.list, &card->dapm_list);
1725
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001726#ifdef CONFIG_DEBUG_FS
1727 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1728#endif
1729
Mark Brown88ee1c62011-02-02 10:43:26 +00001730#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001731 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001732 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001733#endif
Andy Green6ed25972008-06-13 16:24:05 +01001734
Mark Brown9a841eb2011-04-12 17:51:37 -07001735 if (card->dapm_widgets)
1736 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1737 card->num_dapm_widgets);
1738
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001739 /* initialise the sound card only once */
1740 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001741 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 if (ret < 0)
1743 goto card_probe_error;
1744 }
1745
Stephen Warren62ae68f2012-06-08 12:34:23 -06001746 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001747 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1748 order++) {
1749 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001750 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001751 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001752 dev_err(card->dev,
1753 "ASoC: failed to instantiate card %d\n",
1754 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001755 goto probe_dai_err;
1756 }
1757 }
1758 }
1759
1760 /* probe all DAI links on this card */
1761 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1762 order++) {
1763 for (i = 0; i < card->num_links; i++) {
1764 ret = soc_probe_link_dais(card, i, order);
1765 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001766 dev_err(card->dev,
1767 "ASoC: failed to instantiate card %d\n",
1768 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001769 goto probe_dai_err;
1770 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 }
1772 }
1773
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001774 for (i = 0; i < card->num_aux_devs; i++) {
1775 ret = soc_probe_aux_dev(card, i);
1776 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001777 dev_err(card->dev,
1778 "ASoC: failed to add auxiliary devices %d\n",
1779 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001780 goto probe_aux_dev_err;
1781 }
1782 }
1783
Mark Brown888df392012-02-16 19:37:51 -08001784 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001785 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001786
Mark Brownb7af1da2011-04-07 19:18:44 +09001787 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001788 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001789
Mark Brownb8ad29d2011-03-02 18:35:51 +00001790 if (card->dapm_routes)
1791 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1792 card->num_dapm_routes);
1793
Mark Brown75d9ac42011-09-27 16:41:01 +01001794 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001795 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001796 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001797 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001798
Mark Brownf04209a2012-04-09 16:29:21 +01001799 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001800 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1801 int j;
1802
1803 for (j = 0; j < rtd->num_codecs; j++) {
1804 struct snd_soc_dai *codec_dai = codec_dais[j];
1805
1806 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1807 if (ret != 0 && ret != -ENOTSUPP)
1808 dev_warn(codec_dai->dev,
1809 "ASoC: Failed to set DAI format: %d\n",
1810 ret);
1811 }
Mark Brownf04209a2012-04-09 16:29:21 +01001812 }
1813
1814 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001815 if (dai_fmt &&
1816 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001817 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1818 dai_fmt);
1819 if (ret != 0 && ret != -ENOTSUPP)
1820 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001822 ret);
1823 } else if (dai_fmt) {
1824 /* Flip the polarity for the "CPU" end */
1825 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1826 switch (dai_link->dai_fmt &
1827 SND_SOC_DAIFMT_MASTER_MASK) {
1828 case SND_SOC_DAIFMT_CBM_CFM:
1829 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1830 break;
1831 case SND_SOC_DAIFMT_CBM_CFS:
1832 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1833 break;
1834 case SND_SOC_DAIFMT_CBS_CFM:
1835 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1836 break;
1837 case SND_SOC_DAIFMT_CBS_CFS:
1838 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1839 break;
1840 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001841
1842 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001843 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001844 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001845 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001846 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001847 ret);
1848 }
1849 }
1850
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001851 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001853 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1854 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001855 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1856 "%s", card->driver_name ? card->driver_name : card->name);
1857 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1858 switch (card->snd_card->driver[i]) {
1859 case '_':
1860 case '-':
1861 case '\0':
1862 break;
1863 default:
1864 if (!isalnum(card->snd_card->driver[i]))
1865 card->snd_card->driver[i] = '_';
1866 break;
1867 }
1868 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001869
Mark Brown28e9ad92011-03-02 18:36:34 +00001870 if (card->late_probe) {
1871 ret = card->late_probe(card);
1872 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001873 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001874 card->name, ret);
1875 goto probe_aux_dev_err;
1876 }
1877 }
1878
Stephen Warren16332812011-11-23 12:42:04 -07001879 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001880 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001881
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001882 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001883
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001884 ret = snd_card_register(card->snd_card);
1885 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001886 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1887 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001888 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001889 }
1890
1891#ifdef CONFIG_SND_SOC_AC97_BUS
1892 /* register any AC97 codecs */
1893 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001894 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1895 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001896 dev_err(card->dev,
1897 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001898 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001899 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001900 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001901 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001902 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001903#endif
1904
Mark Brown435c5e22008-12-04 15:32:53 +00001905 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001906 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001907 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001908
1909 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001910
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001911probe_aux_dev_err:
1912 for (i = 0; i < card->num_aux_devs; i++)
1913 soc_remove_aux_dev(card, i);
1914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001915probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001916 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001917
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001918card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001919 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001920 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001921
1922 snd_card_free(card->snd_card);
1923
Mark Brownb19e6e72012-03-14 21:18:39 +00001924base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001926
Mark Brownb19e6e72012-03-14 21:18:39 +00001927 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001928}
1929
1930/* probes a new socdev */
1931static int soc_probe(struct platform_device *pdev)
1932{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001933 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001934
Vinod Koul70a7ca32011-01-14 19:22:48 +05301935 /*
1936 * no card, so machine driver should be registering card
1937 * we should not be here in that case so ret error
1938 */
1939 if (!card)
1940 return -EINVAL;
1941
Mark Brownfe4085e2012-03-02 13:07:41 +00001942 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001943 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001944 card->name);
1945
Mark Brown435c5e22008-12-04 15:32:53 +00001946 /* Bodge while we unpick instantiation */
1947 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948
Mark Brown28d528c2012-08-09 18:45:23 +01001949 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950}
1951
Vinod Koulb0e26482011-01-13 22:48:02 +05301952static int soc_cleanup_card_resources(struct snd_soc_card *card)
1953{
Vinod Koulb0e26482011-01-13 22:48:02 +05301954 int i;
1955
1956 /* make sure any delayed work runs */
1957 for (i = 0; i < card->num_rtd; i++) {
1958 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001959 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301960 }
1961
1962 /* remove auxiliary devices */
1963 for (i = 0; i < card->num_aux_devs; i++)
1964 soc_remove_aux_dev(card, i);
1965
1966 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001967 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301968
1969 soc_cleanup_card_debugfs(card);
1970
1971 /* remove the card */
1972 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001973 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301974
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001975 snd_soc_dapm_free(&card->dapm);
1976
Vinod Koulb0e26482011-01-13 22:48:02 +05301977 snd_card_free(card->snd_card);
1978 return 0;
1979
1980}
1981
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982/* removes a socdev */
1983static int soc_remove(struct platform_device *pdev)
1984{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986
Mark Brownc5af3a22008-11-28 13:29:45 +00001987 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 return 0;
1989}
1990
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001991int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001992{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001993 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001994 int i;
Mark Brown51737472009-06-22 13:16:51 +01001995
1996 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001997 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001998
1999 /* Flush out pmdown_time work - we actually do want to run it
2000 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002001 for (i = 0; i < card->num_rtd; i++) {
2002 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002003 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002004 }
Mark Brown51737472009-06-22 13:16:51 +01002005
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002006 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002007
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002008 /* deactivate pins to sleep state */
2009 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002010 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2012 int j;
2013
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002014 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002015 for (j = 0; j < rtd->num_codecs; j++) {
2016 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2017 pinctrl_pm_select_sleep_state(codec_dai->dev);
2018 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002019 }
2020
Mark Brown416356f2009-06-30 19:05:15 +01002021 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002022}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002023EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002024
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002025const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302026 .suspend = snd_soc_suspend,
2027 .resume = snd_soc_resume,
2028 .freeze = snd_soc_suspend,
2029 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002030 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302031 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002032};
Stephen Warrendeb26072011-04-05 19:35:30 -06002033EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002034
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035/* ASoC platform driver */
2036static struct platform_driver soc_driver = {
2037 .driver = {
2038 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002039 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002040 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 },
2042 .probe = soc_probe,
2043 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002044};
2045
Mark Brown096e49d2009-07-05 15:12:22 +01002046/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002047 * snd_soc_new_ac97_codec - initailise AC97 device
2048 * @codec: audio codec
2049 * @ops: AC97 bus operations
2050 * @num: AC97 codec number
2051 *
2052 * Initialises AC97 codec resources for use by ad-hoc devices only.
2053 */
2054int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2055 struct snd_ac97_bus_ops *ops, int num)
2056{
2057 mutex_lock(&codec->mutex);
2058
2059 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2060 if (codec->ac97 == NULL) {
2061 mutex_unlock(&codec->mutex);
2062 return -ENOMEM;
2063 }
2064
2065 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2066 if (codec->ac97->bus == NULL) {
2067 kfree(codec->ac97);
2068 codec->ac97 = NULL;
2069 mutex_unlock(&codec->mutex);
2070 return -ENOMEM;
2071 }
2072
2073 codec->ac97->bus->ops = ops;
2074 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002075
2076 /*
2077 * Mark the AC97 device to be created by us. This way we ensure that the
2078 * device will be registered with the device subsystem later on.
2079 */
2080 codec->ac97_created = 1;
2081
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 mutex_unlock(&codec->mutex);
2083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2086
Markus Pargmann741a5092013-08-19 17:05:55 +02002087static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2088
2089static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2090{
2091 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2092
2093 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2094
2095 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2096
2097 udelay(10);
2098
2099 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2100
2101 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2102 msleep(2);
2103}
2104
2105static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2106{
2107 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2108
2109 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2110
2111 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2112 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2113 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2114
2115 udelay(10);
2116
2117 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2118
2119 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2120 msleep(2);
2121}
2122
2123static int snd_soc_ac97_parse_pinctl(struct device *dev,
2124 struct snd_ac97_reset_cfg *cfg)
2125{
2126 struct pinctrl *p;
2127 struct pinctrl_state *state;
2128 int gpio;
2129 int ret;
2130
2131 p = devm_pinctrl_get(dev);
2132 if (IS_ERR(p)) {
2133 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002134 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002135 }
2136 cfg->pctl = p;
2137
2138 state = pinctrl_lookup_state(p, "ac97-reset");
2139 if (IS_ERR(state)) {
2140 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002141 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002142 }
2143 cfg->pstate_reset = state;
2144
2145 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2146 if (IS_ERR(state)) {
2147 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002148 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002149 }
2150 cfg->pstate_warm_reset = state;
2151
2152 state = pinctrl_lookup_state(p, "ac97-running");
2153 if (IS_ERR(state)) {
2154 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002155 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002156 }
2157 cfg->pstate_run = state;
2158
2159 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2160 if (gpio < 0) {
2161 dev_err(dev, "Can't find ac97-sync gpio\n");
2162 return gpio;
2163 }
2164 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2165 if (ret) {
2166 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2167 return ret;
2168 }
2169 cfg->gpio_sync = gpio;
2170
2171 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2172 if (gpio < 0) {
2173 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2174 return gpio;
2175 }
2176 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2177 if (ret) {
2178 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2179 return ret;
2180 }
2181 cfg->gpio_sdata = gpio;
2182
2183 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2184 if (gpio < 0) {
2185 dev_err(dev, "Can't find ac97-reset gpio\n");
2186 return gpio;
2187 }
2188 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2189 if (ret) {
2190 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2191 return ret;
2192 }
2193 cfg->gpio_reset = gpio;
2194
2195 return 0;
2196}
2197
Mark Brownb047e1c2013-06-26 12:45:59 +01002198struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002199EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002200
2201int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2202{
2203 if (ops == soc_ac97_ops)
2204 return 0;
2205
2206 if (soc_ac97_ops && ops)
2207 return -EBUSY;
2208
2209 soc_ac97_ops = ops;
2210
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2214
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002216 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2217 *
2218 * This function sets the reset and warm_reset properties of ops and parses
2219 * the device node of pdev to get pinctrl states and gpio numbers to use.
2220 */
2221int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2222 struct platform_device *pdev)
2223{
2224 struct device *dev = &pdev->dev;
2225 struct snd_ac97_reset_cfg cfg;
2226 int ret;
2227
2228 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2229 if (ret)
2230 return ret;
2231
2232 ret = snd_soc_set_ac97_ops(ops);
2233 if (ret)
2234 return ret;
2235
2236 ops->warm_reset = snd_soc_ac97_warm_reset;
2237 ops->reset = snd_soc_ac97_reset;
2238
2239 snd_ac97_rst_cfg = cfg;
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2243
2244/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 * snd_soc_free_ac97_codec - free AC97 codec device
2246 * @codec: audio codec
2247 *
2248 * Frees AC97 codec device resources.
2249 */
2250void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2251{
2252 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002253#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002254 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002255#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 kfree(codec->ac97->bus);
2257 kfree(codec->ac97);
2258 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002259 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260 mutex_unlock(&codec->mutex);
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2263
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265 * snd_soc_cnew - create new control
2266 * @_template: control template
2267 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002268 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002269 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002270 *
2271 * Create a new mixer control from a template control.
2272 *
2273 * Returns 0 for success, else error.
2274 */
2275struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002276 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002277 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278{
2279 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002280 struct snd_kcontrol *kcontrol;
2281 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282
2283 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284 template.index = 0;
2285
Mark Brownefb7ac32011-03-08 17:23:24 +00002286 if (!long_name)
2287 long_name = template.name;
2288
2289 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002290 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002291 if (!name)
2292 return NULL;
2293
Mark Brownefb7ac32011-03-08 17:23:24 +00002294 template.name = name;
2295 } else {
2296 template.name = long_name;
2297 }
2298
2299 kcontrol = snd_ctl_new1(&template, data);
2300
2301 kfree(name);
2302
2303 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304}
2305EXPORT_SYMBOL_GPL(snd_soc_cnew);
2306
Liam Girdwood022658b2012-02-03 17:43:09 +00002307static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2308 const struct snd_kcontrol_new *controls, int num_controls,
2309 const char *prefix, void *data)
2310{
2311 int err, i;
2312
2313 for (i = 0; i < num_controls; i++) {
2314 const struct snd_kcontrol_new *control = &controls[i];
2315 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2316 control->name, prefix));
2317 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002318 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2319 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002320 return err;
2321 }
2322 }
2323
2324 return 0;
2325}
2326
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002327struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2328 const char *name)
2329{
2330 struct snd_card *card = soc_card->snd_card;
2331 struct snd_kcontrol *kctl;
2332
2333 if (unlikely(!name))
2334 return NULL;
2335
2336 list_for_each_entry(kctl, &card->controls, list)
2337 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2338 return kctl;
2339 return NULL;
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2342
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002344 * snd_soc_add_component_controls - Add an array of controls to a component.
2345 *
2346 * @component: Component to add controls to
2347 * @controls: Array of controls to add
2348 * @num_controls: Number of elements in the array
2349 *
2350 * Return: 0 for success, else error.
2351 */
2352int snd_soc_add_component_controls(struct snd_soc_component *component,
2353 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2354{
2355 struct snd_card *card = component->card->snd_card;
2356
2357 return snd_soc_add_controls(card, component->dev, controls,
2358 num_controls, component->name_prefix, component);
2359}
2360EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2361
2362/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002363 * snd_soc_add_codec_controls - add an array of controls to a codec.
2364 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002365 * duplicating this code.
2366 *
2367 * @codec: codec to add controls to
2368 * @controls: array of controls to add
2369 * @num_controls: number of elements in the array
2370 *
2371 * Return 0 for success, else error.
2372 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002373int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002374 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002375{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002376 return snd_soc_add_component_controls(&codec->component, controls,
2377 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002378}
Liam Girdwood022658b2012-02-03 17:43:09 +00002379EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002380
2381/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002382 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002383 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002384 *
2385 * @platform: platform to add controls to
2386 * @controls: array of controls to add
2387 * @num_controls: number of elements in the array
2388 *
2389 * Return 0 for success, else error.
2390 */
2391int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002392 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002393{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002394 return snd_soc_add_component_controls(&platform->component, controls,
2395 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002396}
2397EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2398
2399/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002400 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2401 * Convenience function to add a list of controls.
2402 *
2403 * @soc_card: SoC card to add controls to
2404 * @controls: array of controls to add
2405 * @num_controls: number of elements in the array
2406 *
2407 * Return 0 for success, else error.
2408 */
2409int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2410 const struct snd_kcontrol_new *controls, int num_controls)
2411{
2412 struct snd_card *card = soc_card->snd_card;
2413
2414 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2415 NULL, soc_card);
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2418
2419/**
2420 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2421 * Convienience function to add a list of controls.
2422 *
2423 * @dai: DAI to add controls to
2424 * @controls: array of controls to add
2425 * @num_controls: number of elements in the array
2426 *
2427 * Return 0 for success, else error.
2428 */
2429int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2430 const struct snd_kcontrol_new *controls, int num_controls)
2431{
2432 struct snd_card *card = dai->card->snd_card;
2433
2434 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2435 NULL, dai);
2436}
2437EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2438
2439/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440 * snd_soc_info_enum_double - enumerated double mixer info callback
2441 * @kcontrol: mixer control
2442 * @uinfo: control element information
2443 *
2444 * Callback to provide information about a double enumerated
2445 * mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_info *uinfo)
2451{
2452 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2453
2454 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2455 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002456 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002458 if (uinfo->value.enumerated.item >= e->items)
2459 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002460 strlcpy(uinfo->value.enumerated.name,
2461 e->texts[uinfo->value.enumerated.item],
2462 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463 return 0;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2466
2467/**
2468 * snd_soc_get_enum_double - enumerated double mixer get callback
2469 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002470 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 *
2472 * Callback to get the value of a double enumerated mixer.
2473 *
2474 * Returns 0 for success.
2475 */
2476int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002479 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002480 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002481 unsigned int val, item;
2482 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002483 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002484
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002485 ret = snd_soc_component_read(component, e->reg, &reg_val);
2486 if (ret)
2487 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002488 val = (reg_val >> e->shift_l) & e->mask;
2489 item = snd_soc_enum_val_to_item(e, val);
2490 ucontrol->value.enumerated.item[0] = item;
2491 if (e->shift_l != e->shift_r) {
2492 val = (reg_val >> e->shift_l) & e->mask;
2493 item = snd_soc_enum_val_to_item(e, val);
2494 ucontrol->value.enumerated.item[1] = item;
2495 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002496
2497 return 0;
2498}
2499EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2500
2501/**
2502 * snd_soc_put_enum_double - enumerated double mixer put callback
2503 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002504 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 *
2506 * Callback to set the value of a double enumerated mixer.
2507 *
2508 * Returns 0 for success.
2509 */
2510int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
2512{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002513 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002514 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002515 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002516 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002517 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002519 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002520 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002521 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002522 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002524 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002525 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002526 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002527 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528 }
2529
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002530 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531}
2532EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2533
2534/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002535 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002536 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002537 * @reg: Register to read
2538 * @mask: Mask to use after shifting the register value
2539 * @shift: Right shift of register value
2540 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002541 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002542 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002543 * This functions reads a codec register. The register value is shifted right
2544 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2545 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002546 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002547 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002548 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002549static int snd_soc_read_signed(struct snd_soc_component *component,
2550 unsigned int reg, unsigned int mask, unsigned int shift,
2551 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002552{
Markus Pargmannf227b882014-01-16 16:02:10 +01002553 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002554 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002555
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002556 ret = snd_soc_component_read(component, reg, &val);
2557 if (ret < 0)
2558 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002559
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002560 val = (val >> shift) & mask;
2561
2562 if (!sign_bit) {
2563 *signed_val = val;
2564 return 0;
2565 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002566
2567 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002568 if (!(val & BIT(sign_bit))) {
2569 *signed_val = val;
2570 return 0;
2571 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002572
2573 ret = val;
2574
2575 /*
2576 * The register most probably does not contain a full-sized int.
2577 * Instead we have an arbitrary number of bits in a signed
2578 * representation which has to be translated into a full-sized int.
2579 * This is done by filling up all bits above the sign-bit.
2580 */
2581 ret |= ~((int)(BIT(sign_bit) - 1));
2582
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002583 *signed_val = ret;
2584
2585 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002586}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002587
2588/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589 * snd_soc_info_volsw - single mixer info callback
2590 * @kcontrol: mixer control
2591 * @uinfo: control element information
2592 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002593 * Callback to provide information about a single mixer control, or a double
2594 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002595 *
2596 * Returns 0 for success.
2597 */
2598int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_info *uinfo)
2600{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002603 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002605 if (!mc->platform_max)
2606 mc->platform_max = mc->max;
2607 platform_max = mc->platform_max;
2608
2609 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002610 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2611 else
2612 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2613
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002614 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002615 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002616 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617 return 0;
2618}
2619EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2620
2621/**
2622 * snd_soc_get_volsw - single mixer get callback
2623 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002624 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002625 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002626 * Callback to get the value of a single mixer control, or a double mixer
2627 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628 *
2629 * Returns 0 for success.
2630 */
2631int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2633{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002634 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002635 struct soc_mixer_control *mc =
2636 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002637 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002638 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002639 unsigned int shift = mc->shift;
2640 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002641 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002642 int min = mc->min;
2643 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002644 unsigned int mask = (1 << fls(max)) - 1;
2645 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002646 int val;
2647 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
Markus Pargmannf227b882014-01-16 16:02:10 +01002649 if (sign_bit)
2650 mask = BIT(sign_bit + 1) - 1;
2651
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002652 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2653 if (ret)
2654 return ret;
2655
2656 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002657 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002659 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002660
2661 if (snd_soc_volsw_is_stereo(mc)) {
2662 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002663 ret = snd_soc_read_signed(component, reg, mask, rshift,
2664 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002665 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002666 ret = snd_soc_read_signed(component, reg2, mask, shift,
2667 sign_bit, &val);
2668 if (ret)
2669 return ret;
2670
2671 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002672 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002673 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002674 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 }
2676
2677 return 0;
2678}
2679EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2680
2681/**
2682 * snd_soc_put_volsw - single mixer put callback
2683 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002684 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002686 * Callback to set the value of a single mixer control, or a double mixer
2687 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002694 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002695 struct soc_mixer_control *mc =
2696 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002697 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002698 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002699 unsigned int shift = mc->shift;
2700 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002701 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002702 int min = mc->min;
2703 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002704 unsigned int mask = (1 << fls(max)) - 1;
2705 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002706 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002707 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 unsigned int val2 = 0;
2709 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710
Markus Pargmannf227b882014-01-16 16:02:10 +01002711 if (sign_bit)
2712 mask = BIT(sign_bit + 1) - 1;
2713
2714 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002716 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717 val_mask = mask << shift;
2718 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002719 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002720 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002722 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002723 if (reg == reg2) {
2724 val_mask |= mask << rshift;
2725 val |= val2 << rshift;
2726 } else {
2727 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002728 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002729 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002730 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002732 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002733 return err;
2734
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002735 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002736 err = snd_soc_component_update_bits(component, reg2, val_mask,
2737 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002738
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002739 return err;
2740}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002741EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002742
Mark Browne13ac2e2008-05-28 17:58:05 +01002743/**
Brian Austin1d99f242012-03-30 10:43:55 -05002744 * snd_soc_get_volsw_sx - single mixer get callback
2745 * @kcontrol: mixer control
2746 * @ucontrol: control element information
2747 *
2748 * Callback to get the value of a single mixer control, or a double mixer
2749 * control that spans 2 registers.
2750 *
2751 * Returns 0 for success.
2752 */
2753int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_value *ucontrol)
2755{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002756 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002757 struct soc_mixer_control *mc =
2758 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002759 unsigned int reg = mc->reg;
2760 unsigned int reg2 = mc->rreg;
2761 unsigned int shift = mc->shift;
2762 unsigned int rshift = mc->rshift;
2763 int max = mc->max;
2764 int min = mc->min;
2765 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002766 unsigned int val;
2767 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002768
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002769 ret = snd_soc_component_read(component, reg, &val);
2770 if (ret < 0)
2771 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002772
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002773 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2774
2775 if (snd_soc_volsw_is_stereo(mc)) {
2776 ret = snd_soc_component_read(component, reg2, &val);
2777 if (ret < 0)
2778 return ret;
2779
2780 val = ((val >> rshift) - min) & mask;
2781 ucontrol->value.integer.value[1] = val;
2782 }
Brian Austin1d99f242012-03-30 10:43:55 -05002783
2784 return 0;
2785}
2786EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2787
2788/**
2789 * snd_soc_put_volsw_sx - double mixer set callback
2790 * @kcontrol: mixer control
2791 * @uinfo: control element information
2792 *
2793 * Callback to set the value of a double mixer control that spans 2 registers.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002800 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002801 struct soc_mixer_control *mc =
2802 (struct soc_mixer_control *)kcontrol->private_value;
2803
2804 unsigned int reg = mc->reg;
2805 unsigned int reg2 = mc->rreg;
2806 unsigned int shift = mc->shift;
2807 unsigned int rshift = mc->rshift;
2808 int max = mc->max;
2809 int min = mc->min;
2810 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002811 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002812 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002813
2814 val_mask = mask << shift;
2815 val = (ucontrol->value.integer.value[0] + min) & mask;
2816 val = val << shift;
2817
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002818 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302819 if (err < 0)
2820 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002821
2822 if (snd_soc_volsw_is_stereo(mc)) {
2823 val_mask = mask << rshift;
2824 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2825 val2 = val2 << rshift;
2826
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002827 err = snd_soc_component_update_bits(component, reg2, val_mask,
2828 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002829 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002830 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002831}
2832EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2833
2834/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002835 * snd_soc_info_volsw_s8 - signed mixer info callback
2836 * @kcontrol: mixer control
2837 * @uinfo: control element information
2838 *
2839 * Callback to provide information about a signed mixer control.
2840 *
2841 * Returns 0 for success.
2842 */
2843int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_info *uinfo)
2845{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002846 struct soc_mixer_control *mc =
2847 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002848 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002849 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002850
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002851 if (!mc->platform_max)
2852 mc->platform_max = mc->max;
2853 platform_max = mc->platform_max;
2854
Mark Browne13ac2e2008-05-28 17:58:05 +01002855 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2856 uinfo->count = 2;
2857 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002858 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002859 return 0;
2860}
2861EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2862
2863/**
2864 * snd_soc_get_volsw_s8 - signed mixer get callback
2865 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002866 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002867 *
2868 * Callback to get the value of a signed mixer control.
2869 *
2870 * Returns 0 for success.
2871 */
2872int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002875 struct soc_mixer_control *mc =
2876 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002877 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002878 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002879 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002880 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002881 int ret;
2882
2883 ret = snd_soc_component_read(component, reg, &val);
2884 if (ret)
2885 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002886
2887 ucontrol->value.integer.value[0] =
2888 ((signed char)(val & 0xff))-min;
2889 ucontrol->value.integer.value[1] =
2890 ((signed char)((val >> 8) & 0xff))-min;
2891 return 0;
2892}
2893EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2894
2895/**
2896 * snd_soc_put_volsw_sgn - signed mixer put callback
2897 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002898 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002899 *
2900 * Callback to set the value of a signed mixer control.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002907 struct soc_mixer_control *mc =
2908 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002909 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002910 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002911 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002912 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002913
2914 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2915 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2916
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002917 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002918}
2919EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2920
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002921/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002922 * snd_soc_info_volsw_range - single mixer info callback with range.
2923 * @kcontrol: mixer control
2924 * @uinfo: control element information
2925 *
2926 * Callback to provide information, within a range, about a single
2927 * mixer control.
2928 *
2929 * returns 0 for success.
2930 */
2931int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
2933{
2934 struct soc_mixer_control *mc =
2935 (struct soc_mixer_control *)kcontrol->private_value;
2936 int platform_max;
2937 int min = mc->min;
2938
2939 if (!mc->platform_max)
2940 mc->platform_max = mc->max;
2941 platform_max = mc->platform_max;
2942
2943 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002944 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002945 uinfo->value.integer.min = 0;
2946 uinfo->value.integer.max = platform_max - min;
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2951
2952/**
2953 * snd_soc_put_volsw_range - single mixer put value callback with range.
2954 * @kcontrol: mixer control
2955 * @ucontrol: control element information
2956 *
2957 * Callback to set the value, within a range, for a single mixer control.
2958 *
2959 * Returns 0 for success.
2960 */
2961int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2963{
2964 struct soc_mixer_control *mc =
2965 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002966 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002967 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002968 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002969 unsigned int shift = mc->shift;
2970 int min = mc->min;
2971 int max = mc->max;
2972 unsigned int mask = (1 << fls(max)) - 1;
2973 unsigned int invert = mc->invert;
2974 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002976
2977 val = ((ucontrol->value.integer.value[0] + min) & mask);
2978 if (invert)
2979 val = max - val;
2980 val_mask = mask << shift;
2981 val = val << shift;
2982
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002983 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002984 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002985 return ret;
2986
2987 if (snd_soc_volsw_is_stereo(mc)) {
2988 val = ((ucontrol->value.integer.value[1] + min) & mask);
2989 if (invert)
2990 val = max - val;
2991 val_mask = mask << shift;
2992 val = val << shift;
2993
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002994 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2995 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002996 }
2997
2998 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002999}
3000EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3001
3002/**
3003 * snd_soc_get_volsw_range - single mixer get callback with range
3004 * @kcontrol: mixer control
3005 * @ucontrol: control element information
3006 *
3007 * Callback to get the value, within a range, of a single mixer control.
3008 *
3009 * Returns 0 for success.
3010 */
3011int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_value *ucontrol)
3013{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003014 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003015 struct soc_mixer_control *mc =
3016 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003017 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003018 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003019 unsigned int shift = mc->shift;
3020 int min = mc->min;
3021 int max = mc->max;
3022 unsigned int mask = (1 << fls(max)) - 1;
3023 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003024 unsigned int val;
3025 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003026
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003027 ret = snd_soc_component_read(component, reg, &val);
3028 if (ret)
3029 return ret;
3030
3031 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003032 if (invert)
3033 ucontrol->value.integer.value[0] =
3034 max - ucontrol->value.integer.value[0];
3035 ucontrol->value.integer.value[0] =
3036 ucontrol->value.integer.value[0] - min;
3037
Mark Brown9bde4f02012-12-19 16:05:00 +00003038 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003039 ret = snd_soc_component_read(component, rreg, &val);
3040 if (ret)
3041 return ret;
3042
3043 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003044 if (invert)
3045 ucontrol->value.integer.value[1] =
3046 max - ucontrol->value.integer.value[1];
3047 ucontrol->value.integer.value[1] =
3048 ucontrol->value.integer.value[1] - min;
3049 }
3050
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003051 return 0;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3054
3055/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003056 * snd_soc_limit_volume - Set new limit to an existing volume control.
3057 *
3058 * @codec: where to look for the control
3059 * @name: Name of the control
3060 * @max: new maximum limit
3061 *
3062 * Return 0 for success, else error.
3063 */
3064int snd_soc_limit_volume(struct snd_soc_codec *codec,
3065 const char *name, int max)
3066{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003067 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003068 struct snd_kcontrol *kctl;
3069 struct soc_mixer_control *mc;
3070 int found = 0;
3071 int ret = -EINVAL;
3072
3073 /* Sanity check for name and max */
3074 if (unlikely(!name || max <= 0))
3075 return -EINVAL;
3076
3077 list_for_each_entry(kctl, &card->controls, list) {
3078 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3079 found = 1;
3080 break;
3081 }
3082 }
3083 if (found) {
3084 mc = (struct soc_mixer_control *)kctl->private_value;
3085 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003086 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003087 ret = 0;
3088 }
3089 }
3090 return ret;
3091}
3092EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3093
Mark Brown71d08512011-10-10 18:31:26 +01003094int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_info *uinfo)
3096{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003097 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003098 struct soc_bytes *params = (void *)kcontrol->private_value;
3099
3100 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003101 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003102
3103 return 0;
3104}
3105EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3106
3107int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
3109{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003110 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003111 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003112 int ret;
3113
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003114 if (component->regmap)
3115 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003116 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003117 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003118 else
3119 ret = -EINVAL;
3120
Mark Brownf831b052012-02-17 16:20:33 -08003121 /* Hide any masked bytes to ensure consistent data reporting */
3122 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003123 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003124 case 1:
3125 ucontrol->value.bytes.data[0] &= ~params->mask;
3126 break;
3127 case 2:
3128 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003129 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003130 break;
3131 case 4:
3132 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003133 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003134 break;
3135 default:
3136 return -EINVAL;
3137 }
3138 }
3139
Mark Brown71d08512011-10-10 18:31:26 +01003140 return ret;
3141}
3142EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3143
3144int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3145 struct snd_ctl_elem_value *ucontrol)
3146{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003147 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003148 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003149 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003150 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003151 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003152
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003153 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003154 return -EINVAL;
3155
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003156 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003157
Mark Brownb5a8fe42013-01-20 21:42:22 +09003158 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3159 if (!data)
3160 return -ENOMEM;
3161
Mark Brownf831b052012-02-17 16:20:33 -08003162 /*
3163 * If we've got a mask then we need to preserve the register
3164 * bits. We shouldn't modify the incoming data so take a
3165 * copy.
3166 */
3167 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003168 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003169 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003170 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003171
3172 val &= params->mask;
3173
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003174 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003175 case 1:
3176 ((u8 *)data)[0] &= ~params->mask;
3177 ((u8 *)data)[0] |= val;
3178 break;
3179 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003180 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003181 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003182 &mask, &mask);
3183 if (ret != 0)
3184 goto out;
3185
3186 ((u16 *)data)[0] &= mask;
3187
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003188 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003189 &val, &val);
3190 if (ret != 0)
3191 goto out;
3192
3193 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003194 break;
3195 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003196 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003197 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003198 &mask, &mask);
3199 if (ret != 0)
3200 goto out;
3201
3202 ((u32 *)data)[0] &= mask;
3203
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003204 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003205 &val, &val);
3206 if (ret != 0)
3207 goto out;
3208
3209 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003210 break;
3211 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003212 ret = -EINVAL;
3213 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003214 }
3215 }
3216
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003217 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003218 data, len);
3219
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003220out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003221 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003222
3223 return ret;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3226
Vinod Kould9881202014-05-02 10:58:11 +05303227int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_info *ucontrol)
3229{
3230 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3231
3232 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3233 ucontrol->count = params->max;
3234
3235 return 0;
3236}
3237EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3238
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303239int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3240 unsigned int size, unsigned int __user *tlv)
3241{
3242 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3243 unsigned int count = size < params->max ? size : params->max;
3244 int ret = -ENXIO;
3245
3246 switch (op_flag) {
3247 case SNDRV_CTL_TLV_OP_READ:
3248 if (params->get)
3249 ret = params->get(tlv, count);
3250 break;
3251 case SNDRV_CTL_TLV_OP_WRITE:
3252 if (params->put)
3253 ret = params->put(tlv, count);
3254 break;
3255 }
3256 return ret;
3257}
3258EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3259
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003260/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003261 * snd_soc_info_xr_sx - signed multi register info callback
3262 * @kcontrol: mreg control
3263 * @uinfo: control element information
3264 *
3265 * Callback to provide information of a control that can
3266 * span multiple codec registers which together
3267 * forms a single signed value in a MSB/LSB manner.
3268 *
3269 * Returns 0 for success.
3270 */
3271int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3272 struct snd_ctl_elem_info *uinfo)
3273{
3274 struct soc_mreg_control *mc =
3275 (struct soc_mreg_control *)kcontrol->private_value;
3276 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3277 uinfo->count = 1;
3278 uinfo->value.integer.min = mc->min;
3279 uinfo->value.integer.max = mc->max;
3280
3281 return 0;
3282}
3283EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3284
3285/**
3286 * snd_soc_get_xr_sx - signed multi register get callback
3287 * @kcontrol: mreg control
3288 * @ucontrol: control element information
3289 *
3290 * Callback to get the value of a control that can span
3291 * multiple codec registers which together forms a single
3292 * signed value in a MSB/LSB manner. The control supports
3293 * specifying total no of bits used to allow for bitfields
3294 * across the multiple codec registers.
3295 *
3296 * Returns 0 for success.
3297 */
3298int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_value *ucontrol)
3300{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003301 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003302 struct soc_mreg_control *mc =
3303 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003304 unsigned int regbase = mc->regbase;
3305 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003306 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003307 unsigned int regwmask = (1<<regwshift)-1;
3308 unsigned int invert = mc->invert;
3309 unsigned long mask = (1UL<<mc->nbits)-1;
3310 long min = mc->min;
3311 long max = mc->max;
3312 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003313 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003314 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003315 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003316
3317 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003318 ret = snd_soc_component_read(component, regbase+i, &regval);
3319 if (ret)
3320 return ret;
3321 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003322 }
3323 val &= mask;
3324 if (min < 0 && val > max)
3325 val |= ~mask;
3326 if (invert)
3327 val = max - val;
3328 ucontrol->value.integer.value[0] = val;
3329
3330 return 0;
3331}
3332EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3333
3334/**
3335 * snd_soc_put_xr_sx - signed multi register get callback
3336 * @kcontrol: mreg control
3337 * @ucontrol: control element information
3338 *
3339 * Callback to set the value of a control that can span
3340 * multiple codec registers which together forms a single
3341 * signed value in a MSB/LSB manner. The control supports
3342 * specifying total no of bits used to allow for bitfields
3343 * across the multiple codec registers.
3344 *
3345 * Returns 0 for success.
3346 */
3347int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3348 struct snd_ctl_elem_value *ucontrol)
3349{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003350 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003351 struct soc_mreg_control *mc =
3352 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003353 unsigned int regbase = mc->regbase;
3354 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003355 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003356 unsigned int regwmask = (1<<regwshift)-1;
3357 unsigned int invert = mc->invert;
3358 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003359 long max = mc->max;
3360 long val = ucontrol->value.integer.value[0];
3361 unsigned int i, regval, regmask;
3362 int err;
3363
3364 if (invert)
3365 val = max - val;
3366 val &= mask;
3367 for (i = 0; i < regcount; i++) {
3368 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3369 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003370 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003371 regmask, regval);
3372 if (err < 0)
3373 return err;
3374 }
3375
3376 return 0;
3377}
3378EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3379
3380/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003381 * snd_soc_get_strobe - strobe get callback
3382 * @kcontrol: mixer control
3383 * @ucontrol: control element information
3384 *
3385 * Callback get the value of a strobe mixer control.
3386 *
3387 * Returns 0 for success.
3388 */
3389int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3391{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003392 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003393 struct soc_mixer_control *mc =
3394 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003395 unsigned int reg = mc->reg;
3396 unsigned int shift = mc->shift;
3397 unsigned int mask = 1 << shift;
3398 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003399 unsigned int val;
3400 int ret;
3401
3402 ret = snd_soc_component_read(component, reg, &val);
3403 if (ret)
3404 return ret;
3405
3406 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003407
3408 if (shift != 0 && val != 0)
3409 val = val >> shift;
3410 ucontrol->value.enumerated.item[0] = val ^ invert;
3411
3412 return 0;
3413}
3414EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3415
3416/**
3417 * snd_soc_put_strobe - strobe put callback
3418 * @kcontrol: mixer control
3419 * @ucontrol: control element information
3420 *
3421 * Callback strobe a register bit to high then low (or the inverse)
3422 * in one pass of a single mixer enum control.
3423 *
3424 * Returns 1 for success.
3425 */
3426int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3428{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003429 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003430 struct soc_mixer_control *mc =
3431 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003432 unsigned int reg = mc->reg;
3433 unsigned int shift = mc->shift;
3434 unsigned int mask = 1 << shift;
3435 unsigned int invert = mc->invert != 0;
3436 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3437 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3438 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3439 int err;
3440
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003441 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003442 if (err < 0)
3443 return err;
3444
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003445 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003446}
3447EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3448
3449/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003450 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3451 * @dai: DAI
3452 * @clk_id: DAI specific clock ID
3453 * @freq: new clock frequency in Hz
3454 * @dir: new clock direction - input/output.
3455 *
3456 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3457 */
3458int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3459 unsigned int freq, int dir)
3460{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003461 if (dai->driver && dai->driver->ops->set_sysclk)
3462 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003463 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003464 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003465 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003466 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003467 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468}
3469EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3470
3471/**
Mark Brownec4ee522011-03-07 20:58:11 +00003472 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3473 * @codec: CODEC
3474 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003475 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003476 * @freq: new clock frequency in Hz
3477 * @dir: new clock direction - input/output.
3478 *
3479 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3480 */
3481int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003482 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003483{
3484 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003485 return codec->driver->set_sysclk(codec, clk_id, source,
3486 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003487 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003488 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003489}
3490EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3491
3492/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003493 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3494 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003495 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496 * @div: new clock divisor.
3497 *
3498 * Configures the clock dividers. This is used to derive the best DAI bit and
3499 * frame clocks from the system or master clock. It's best to set the DAI bit
3500 * and frame clocks as low as possible to save system power.
3501 */
3502int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3503 int div_id, int div)
3504{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003505 if (dai->driver && dai->driver->ops->set_clkdiv)
3506 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003507 else
3508 return -EINVAL;
3509}
3510EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3511
3512/**
3513 * snd_soc_dai_set_pll - configure DAI PLL.
3514 * @dai: DAI
3515 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003516 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003517 * @freq_in: PLL input clock frequency in Hz
3518 * @freq_out: requested PLL output clock frequency in Hz
3519 *
3520 * Configures and enables PLL to generate output clock based on input clock.
3521 */
Mark Brown85488032009-09-05 18:52:16 +01003522int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3523 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003524{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003525 if (dai->driver && dai->driver->ops->set_pll)
3526 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003527 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003528 else if (dai->codec && dai->codec->driver->set_pll)
3529 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3530 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003531 else
3532 return -EINVAL;
3533}
3534EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3535
Mark Brownec4ee522011-03-07 20:58:11 +00003536/*
3537 * snd_soc_codec_set_pll - configure codec PLL.
3538 * @codec: CODEC
3539 * @pll_id: DAI specific PLL ID
3540 * @source: DAI specific source for the PLL
3541 * @freq_in: PLL input clock frequency in Hz
3542 * @freq_out: requested PLL output clock frequency in Hz
3543 *
3544 * Configures and enables PLL to generate output clock based on input clock.
3545 */
3546int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3547 unsigned int freq_in, unsigned int freq_out)
3548{
3549 if (codec->driver->set_pll)
3550 return codec->driver->set_pll(codec, pll_id, source,
3551 freq_in, freq_out);
3552 else
3553 return -EINVAL;
3554}
3555EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3556
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003557/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003558 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3559 * @dai: DAI
3560 * @ratio Ratio of BCLK to Sample rate.
3561 *
3562 * Configures the DAI for a preset BCLK to sample rate ratio.
3563 */
3564int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3565{
3566 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3567 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3568 else
3569 return -EINVAL;
3570}
3571EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3572
3573/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003574 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3575 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003576 * @fmt: SND_SOC_DAIFMT_ format value.
3577 *
3578 * Configures the DAI hardware format and clocking.
3579 */
3580int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3581{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003582 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003583 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003584 if (dai->driver->ops->set_fmt == NULL)
3585 return -ENOTSUPP;
3586 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003587}
3588EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3589
3590/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003591 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003592 * @slots: Number of slots in use.
3593 * @tx_mask: bitmask representing active TX slots.
3594 * @rx_mask: bitmask representing active RX slots.
3595 *
3596 * Generates the TDM tx and rx slot default masks for DAI.
3597 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003598static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003599 unsigned int *tx_mask,
3600 unsigned int *rx_mask)
3601{
3602 if (*tx_mask || *rx_mask)
3603 return 0;
3604
3605 if (!slots)
3606 return -EINVAL;
3607
3608 *tx_mask = (1 << slots) - 1;
3609 *rx_mask = (1 << slots) - 1;
3610
3611 return 0;
3612}
3613
3614/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003615 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3616 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003617 * @tx_mask: bitmask representing active TX slots.
3618 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003619 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003620 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003621 *
3622 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3623 * specific.
3624 */
3625int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003626 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003627{
Xiubo Lie5c21512014-03-21 14:17:12 +08003628 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3629 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003630 &tx_mask, &rx_mask);
3631 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003632 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003633
Benoit Cousson88bd8702014-07-08 23:19:34 +02003634 dai->tx_mask = tx_mask;
3635 dai->rx_mask = rx_mask;
3636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003637 if (dai->driver && dai->driver->ops->set_tdm_slot)
3638 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003639 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003640 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003641 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003642}
3643EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3644
3645/**
Barry Song472df3c2009-09-12 01:16:29 +08003646 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3647 * @dai: DAI
3648 * @tx_num: how many TX channels
3649 * @tx_slot: pointer to an array which imply the TX slot number channel
3650 * 0~num-1 uses
3651 * @rx_num: how many RX channels
3652 * @rx_slot: pointer to an array which imply the RX slot number channel
3653 * 0~num-1 uses
3654 *
3655 * configure the relationship between channel number and TDM slot number.
3656 */
3657int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3658 unsigned int tx_num, unsigned int *tx_slot,
3659 unsigned int rx_num, unsigned int *rx_slot)
3660{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003661 if (dai->driver && dai->driver->ops->set_channel_map)
3662 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003663 rx_num, rx_slot);
3664 else
3665 return -EINVAL;
3666}
3667EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3668
3669/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003670 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3671 * @dai: DAI
3672 * @tristate: tristate enable
3673 *
3674 * Tristates the DAI so that others can use it.
3675 */
3676int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3677{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678 if (dai->driver && dai->driver->ops->set_tristate)
3679 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003680 else
3681 return -EINVAL;
3682}
3683EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3684
3685/**
3686 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3687 * @dai: DAI
3688 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003689 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003690 *
3691 * Mutes the DAI DAC.
3692 */
Mark Brownda183962013-02-06 15:44:07 +00003693int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3694 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003695{
Mark Brownda183962013-02-06 15:44:07 +00003696 if (!dai->driver)
3697 return -ENOTSUPP;
3698
3699 if (dai->driver->ops->mute_stream)
3700 return dai->driver->ops->mute_stream(dai, mute, direction);
3701 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3702 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003703 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003704 else
Mark Brown04570c62012-04-13 19:16:03 +01003705 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003706}
3707EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3708
Benoit Cousson88bd8702014-07-08 23:19:34 +02003709static int snd_soc_init_multicodec(struct snd_soc_card *card,
3710 struct snd_soc_dai_link *dai_link)
3711{
3712 /* Legacy codec/codec_dai link is a single entry in multicodec */
3713 if (dai_link->codec_name || dai_link->codec_of_node ||
3714 dai_link->codec_dai_name) {
3715 dai_link->num_codecs = 1;
3716
3717 dai_link->codecs = devm_kzalloc(card->dev,
3718 sizeof(struct snd_soc_dai_link_component),
3719 GFP_KERNEL);
3720 if (!dai_link->codecs)
3721 return -ENOMEM;
3722
3723 dai_link->codecs[0].name = dai_link->codec_name;
3724 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3725 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3726 }
3727
3728 if (!dai_link->codecs) {
3729 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3730 return -EINVAL;
3731 }
3732
3733 return 0;
3734}
3735
Mark Brownc5af3a22008-11-28 13:29:45 +00003736/**
3737 * snd_soc_register_card - Register a card with the ASoC core
3738 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003739 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003740 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003741 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303742int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003743{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003744 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003745
Mark Brownc5af3a22008-11-28 13:29:45 +00003746 if (!card->name || !card->dev)
3747 return -EINVAL;
3748
Stephen Warren5a504962011-12-21 10:40:59 -07003749 for (i = 0; i < card->num_links; i++) {
3750 struct snd_soc_dai_link *link = &card->dai_link[i];
3751
Benoit Cousson88bd8702014-07-08 23:19:34 +02003752 ret = snd_soc_init_multicodec(card, link);
3753 if (ret) {
3754 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3755 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003756 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003757
3758 for (j = 0; j < link->num_codecs; j++) {
3759 /*
3760 * Codec must be specified by 1 of name or OF node,
3761 * not both or neither.
3762 */
3763 if (!!link->codecs[j].name ==
3764 !!link->codecs[j].of_node) {
3765 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3766 link->name);
3767 return -EINVAL;
3768 }
3769 /* Codec DAI name must be specified */
3770 if (!link->codecs[j].dai_name) {
3771 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3772 link->name);
3773 return -EINVAL;
3774 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003775 }
Stephen Warren5a504962011-12-21 10:40:59 -07003776
3777 /*
3778 * Platform may be specified by either name or OF node, but
3779 * can be left unspecified, and a dummy platform will be used.
3780 */
3781 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003782 dev_err(card->dev,
3783 "ASoC: Both platform name/of_node are set for %s\n",
3784 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003785 return -EINVAL;
3786 }
3787
3788 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003789 * CPU device may be specified by either name or OF node, but
3790 * can be left unspecified, and will be matched based on DAI
3791 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003792 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003793 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003794 dev_err(card->dev,
3795 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3796 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003797 return -EINVAL;
3798 }
3799 /*
3800 * At least one of CPU DAI name or CPU device name/node must be
3801 * specified
3802 */
3803 if (!link->cpu_dai_name &&
3804 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003805 dev_err(card->dev,
3806 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3807 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003808 return -EINVAL;
3809 }
3810 }
3811
Mark Browned77cc12011-05-03 18:25:34 +01003812 dev_set_drvdata(card->dev, card);
3813
Stephen Warren111c6412011-01-28 14:26:35 -07003814 snd_soc_initialize_card_lists(card);
3815
Vinod Koul150dd2f2011-01-13 22:48:32 +05303816 soc_init_card_debugfs(card);
3817
Mark Brown181a6892012-03-14 20:18:49 +00003818 card->rtd = devm_kzalloc(card->dev,
3819 sizeof(struct snd_soc_pcm_runtime) *
3820 (card->num_links + card->num_aux_devs),
3821 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822 if (card->rtd == NULL)
3823 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003824 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003825 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003827 for (i = 0; i < card->num_links; i++) {
3828 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003829 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003830 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3831 sizeof(struct snd_soc_dai *) *
3832 (card->rtd[i].dai_link->num_codecs),
3833 GFP_KERNEL);
3834 if (card->rtd[i].codec_dais == NULL)
3835 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003836 }
3837
3838 for (i = 0; i < card->num_aux_devs; i++)
3839 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003840
Mark Browndb432b42011-10-03 21:06:40 +01003841 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003842 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003843 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003844 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003845
Mark Brownb19e6e72012-03-14 21:18:39 +00003846 ret = snd_soc_instantiate_card(card);
3847 if (ret != 0)
3848 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003849
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003850 /* deactivate pins to sleep state */
3851 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003852 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3853 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3854 int j;
3855
3856 for (j = 0; j < rtd->num_codecs; j++) {
3857 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3858 if (!codec_dai->active)
3859 pinctrl_pm_select_sleep_state(codec_dai->dev);
3860 }
3861
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003862 if (!cpu_dai->active)
3863 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3864 }
3865
Mark Brownb19e6e72012-03-14 21:18:39 +00003866 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003867}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303868EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003869
3870/**
3871 * snd_soc_unregister_card - Unregister a card with the ASoC core
3872 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003873 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003874 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003875 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303876int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003877{
Vinod Koulb0e26482011-01-13 22:48:02 +05303878 if (card->instantiated)
3879 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003880 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003881
3882 return 0;
3883}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303884EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003885
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886/*
3887 * Simplify DAI link configuration by removing ".-1" from device names
3888 * and sanitizing names.
3889 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003890static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003891{
3892 char *found, name[NAME_SIZE];
3893 int id1, id2;
3894
3895 if (dev_name(dev) == NULL)
3896 return NULL;
3897
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003898 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899
3900 /* are we a "%s.%d" name (platform and SPI components) */
3901 found = strstr(name, dev->driver->name);
3902 if (found) {
3903 /* get ID */
3904 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3905
3906 /* discard ID from name if ID == -1 */
3907 if (*id == -1)
3908 found[strlen(dev->driver->name)] = '\0';
3909 }
3910
3911 } else {
3912 /* I2C component devices are named "bus-addr" */
3913 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3914 char tmp[NAME_SIZE];
3915
3916 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003917 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003918
3919 /* sanitize component name for DAI link creation */
3920 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003921 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003922 } else
3923 *id = 0;
3924 }
3925
3926 return kstrdup(name, GFP_KERNEL);
3927}
3928
3929/*
3930 * Simplify DAI link naming for single devices with multiple DAIs by removing
3931 * any ".-1" and using the DAI name (instead of device name).
3932 */
3933static inline char *fmt_multiple_name(struct device *dev,
3934 struct snd_soc_dai_driver *dai_drv)
3935{
3936 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003937 dev_err(dev,
3938 "ASoC: error - multiple DAI %s registered with no name\n",
3939 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003940 return NULL;
3941 }
3942
3943 return kstrdup(dai_drv->name, GFP_KERNEL);
3944}
3945
Mark Brown91151712008-11-30 23:31:24 +00003946/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003947 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003948 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003949 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003950 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003951static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003952{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003953 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003954
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003955 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003956 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3957 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003958 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003959 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003961 }
Mark Brown91151712008-11-30 23:31:24 +00003962}
Mark Brown91151712008-11-30 23:31:24 +00003963
3964/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003965 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003966 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003967 * @component: The component the DAIs are registered for
3968 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003969 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003970 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3971 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003972 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003973static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003974 struct snd_soc_dai_driver *dai_drv, size_t count,
3975 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003976{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003977 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003978 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003979 unsigned int i;
3980 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003981
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003982 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003983
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003984 component->dai_drv = dai_drv;
3985 component->num_dai = count;
3986
Mark Brown91151712008-11-30 23:31:24 +00003987 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003988
3989 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003990 if (dai == NULL) {
3991 ret = -ENOMEM;
3992 goto err;
3993 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003995 /*
3996 * Back in the old days when we still had component-less DAIs,
3997 * instead of having a static name, component-less DAIs would
3998 * inherit the name of the parent device so it is possible to
3999 * register multiple instances of the DAI. We still need to keep
4000 * the same naming style even though those DAIs are not
4001 * component-less anymore.
4002 */
4003 if (count == 1 && legacy_dai_naming) {
4004 dai->name = fmt_single_name(dev, &dai->id);
4005 } else {
4006 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4007 if (dai_drv[i].id)
4008 dai->id = dai_drv[i].id;
4009 else
4010 dai->id = i;
4011 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004012 if (dai->name == NULL) {
4013 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004014 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004015 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004016 }
4017
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004018 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004019 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004020 dai->driver = &dai_drv[i];
4021 if (!dai->driver->ops)
4022 dai->driver->ops = &null_dai_ops;
4023
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004024 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004025
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004026 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004027 }
4028
4029 return 0;
4030
4031err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004032 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004033
4034 return ret;
4035}
Mark Brown91151712008-11-30 23:31:24 +00004036
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004037static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4038 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004039{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004040 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004041
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004042 component->driver->seq_notifier(component, type, subseq);
4043}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004044
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004045static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4046 int event)
4047{
4048 struct snd_soc_component *component = dapm->component;
4049
4050 return component->driver->stream_event(component, event);
4051}
4052
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004053static int snd_soc_component_initialize(struct snd_soc_component *component,
4054 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004055{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004056 struct snd_soc_dapm_context *dapm;
4057
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004058 component->name = fmt_single_name(dev, &component->id);
4059 if (!component->name) {
4060 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004061 return -ENOMEM;
4062 }
4063
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004064 component->dev = dev;
4065 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004066 component->probe = component->driver->probe;
4067 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004068
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004069 if (!component->dapm_ptr)
4070 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004072 dapm = component->dapm_ptr;
4073 dapm->dev = dev;
4074 dapm->component = component;
4075 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004076 if (driver->seq_notifier)
4077 dapm->seq_notifier = snd_soc_component_seq_notifier;
4078 if (driver->stream_event)
4079 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004080
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004081 component->controls = driver->controls;
4082 component->num_controls = driver->num_controls;
4083 component->dapm_widgets = driver->dapm_widgets;
4084 component->num_dapm_widgets = driver->num_dapm_widgets;
4085 component->dapm_routes = driver->dapm_routes;
4086 component->num_dapm_routes = driver->num_dapm_routes;
4087
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004088 INIT_LIST_HEAD(&component->dai_list);
4089 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004090
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004091 return 0;
4092}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004093
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004094static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4095{
4096 list_add(&component->list, &component_list);
4097}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004098
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004099static void snd_soc_component_add(struct snd_soc_component *component)
4100{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004101 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004102 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004103 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004104}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004105
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004106static void snd_soc_component_cleanup(struct snd_soc_component *component)
4107{
4108 snd_soc_unregister_dais(component);
4109 kfree(component->name);
4110}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004111
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004112static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4113{
4114 list_del(&component->list);
4115}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004116
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004117static void snd_soc_component_del(struct snd_soc_component *component)
4118{
4119 mutex_lock(&client_mutex);
4120 snd_soc_component_del_unlocked(component);
4121 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004122}
4123
4124int snd_soc_register_component(struct device *dev,
4125 const struct snd_soc_component_driver *cmpnt_drv,
4126 struct snd_soc_dai_driver *dai_drv,
4127 int num_dai)
4128{
4129 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004130 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004131
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004132 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004133 if (!cmpnt) {
4134 dev_err(dev, "ASoC: Failed to allocate memory\n");
4135 return -ENOMEM;
4136 }
4137
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004138 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4139 if (ret)
4140 goto err_free;
4141
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004142 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004143 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004144
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004145 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4146 if (ret < 0) {
4147 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4148 goto err_cleanup;
4149 }
4150
4151 snd_soc_component_add(cmpnt);
4152
4153 return 0;
4154
4155err_cleanup:
4156 snd_soc_component_cleanup(cmpnt);
4157err_free:
4158 kfree(cmpnt);
4159 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004160}
4161EXPORT_SYMBOL_GPL(snd_soc_register_component);
4162
4163/**
4164 * snd_soc_unregister_component - Unregister a component from the ASoC core
4165 *
4166 */
4167void snd_soc_unregister_component(struct device *dev)
4168{
4169 struct snd_soc_component *cmpnt;
4170
4171 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004172 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004173 goto found;
4174 }
4175 return;
4176
4177found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004178 snd_soc_component_del(cmpnt);
4179 snd_soc_component_cleanup(cmpnt);
4180 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004181}
4182EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4183
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004184static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4185{
4186 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4187
4188 return platform->driver->probe(platform);
4189}
4190
4191static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4192{
4193 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4194
4195 platform->driver->remove(platform);
4196}
4197
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004198static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4199 unsigned int reg, unsigned int val)
4200{
4201 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4202
4203 return platform->driver->write(platform, reg, val);
4204}
4205
4206static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4207 unsigned int reg, unsigned int *val)
4208{
4209 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4210
4211 *val = platform->driver->read(platform, reg);
4212
4213 return 0;
4214}
4215
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004216/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004217 * snd_soc_add_platform - Add a platform to the ASoC core
4218 * @dev: The parent device for the platform
4219 * @platform: The platform to add
4220 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004221 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004222int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004223 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004224{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004225 int ret;
4226
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004227 ret = snd_soc_component_initialize(&platform->component,
4228 &platform_drv->component_driver, dev);
4229 if (ret)
4230 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004231
4232 platform->dev = dev;
4233 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004234 if (platform_drv->controls) {
4235 platform->component.controls = platform_drv->controls;
4236 platform->component.num_controls = platform_drv->num_controls;
4237 }
4238 if (platform_drv->dapm_widgets) {
4239 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4240 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4241 platform->component.steal_sibling_dai_widgets = true;
4242 }
4243 if (platform_drv->dapm_routes) {
4244 platform->component.dapm_routes = platform_drv->dapm_routes;
4245 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4246 }
4247
4248 if (platform_drv->probe)
4249 platform->component.probe = snd_soc_platform_drv_probe;
4250 if (platform_drv->remove)
4251 platform->component.remove = snd_soc_platform_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004252 if (platform_drv->write)
4253 platform->component.write = snd_soc_platform_drv_write;
4254 if (platform_drv->read)
4255 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004256
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004257#ifdef CONFIG_DEBUG_FS
4258 platform->component.debugfs_prefix = "platform";
4259#endif
4260
Mark Brown12a48a8c2008-12-03 19:40:30 +00004261 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004262 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004263 list_add(&platform->list, &platform_list);
4264 mutex_unlock(&client_mutex);
4265
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004266 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4267 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004268
4269 return 0;
4270}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004271EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4272
4273/**
4274 * snd_soc_register_platform - Register a platform with the ASoC core
4275 *
4276 * @platform: platform to register
4277 */
4278int snd_soc_register_platform(struct device *dev,
4279 const struct snd_soc_platform_driver *platform_drv)
4280{
4281 struct snd_soc_platform *platform;
4282 int ret;
4283
4284 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4285
4286 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4287 if (platform == NULL)
4288 return -ENOMEM;
4289
4290 ret = snd_soc_add_platform(dev, platform, platform_drv);
4291 if (ret)
4292 kfree(platform);
4293
4294 return ret;
4295}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004296EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4297
4298/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004299 * snd_soc_remove_platform - Remove a platform from the ASoC core
4300 * @platform: the platform to remove
4301 */
4302void snd_soc_remove_platform(struct snd_soc_platform *platform)
4303{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004304
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004305 mutex_lock(&client_mutex);
4306 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004307 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004308 mutex_unlock(&client_mutex);
4309
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004310 snd_soc_component_cleanup(&platform->component);
4311
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004312 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004313 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004314}
4315EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4316
4317struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4318{
4319 struct snd_soc_platform *platform;
4320
4321 list_for_each_entry(platform, &platform_list, list) {
4322 if (dev == platform->dev)
4323 return platform;
4324 }
4325
4326 return NULL;
4327}
4328EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4329
4330/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004331 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4332 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004333 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004334 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004335void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004336{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004337 struct snd_soc_platform *platform;
4338
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004339 platform = snd_soc_lookup_platform(dev);
4340 if (!platform)
4341 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004342
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004343 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004344 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004345}
4346EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4347
Mark Brown151ab222009-05-09 16:22:58 +01004348static u64 codec_format_map[] = {
4349 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4350 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4351 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4352 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4353 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4354 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4355 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4356 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4357 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4358 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4359 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4360 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4361 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4362 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4363 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4364 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4365};
4366
4367/* Fix up the DAI formats for endianness: codecs don't actually see
4368 * the endianness of the data but we're using the CPU format
4369 * definitions which do need to include endianness so we ensure that
4370 * codec DAIs always have both big and little endian variants set.
4371 */
4372static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4373{
4374 int i;
4375
4376 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4377 if (stream->formats & codec_format_map[i])
4378 stream->formats |= codec_format_map[i];
4379}
4380
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004381static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4382{
4383 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4384
4385 return codec->driver->probe(codec);
4386}
4387
4388static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4389{
4390 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4391
4392 codec->driver->remove(codec);
4393}
4394
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004395static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4396 unsigned int reg, unsigned int val)
4397{
4398 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4399
4400 return codec->driver->write(codec, reg, val);
4401}
4402
4403static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4404 unsigned int reg, unsigned int *val)
4405{
4406 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4407
4408 *val = codec->driver->read(codec, reg);
4409
4410 return 0;
4411}
4412
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004413static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4414 enum snd_soc_bias_level level)
4415{
4416 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4417
4418 return codec->driver->set_bias_level(codec, level);
4419}
4420
Mark Brown0d0cf002008-12-10 14:32:45 +00004421/**
4422 * snd_soc_register_codec - Register a codec with the ASoC core
4423 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004424 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004425 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004426int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004427 const struct snd_soc_codec_driver *codec_drv,
4428 struct snd_soc_dai_driver *dai_drv,
4429 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004430{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004431 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004432 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004433 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004434 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004435
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004436 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004437
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004438 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4439 if (codec == NULL)
4440 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004441
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004442 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004443 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004444
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004445 ret = snd_soc_component_initialize(&codec->component,
4446 &codec_drv->component_driver, dev);
4447 if (ret)
4448 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004449
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004450 if (codec_drv->controls) {
4451 codec->component.controls = codec_drv->controls;
4452 codec->component.num_controls = codec_drv->num_controls;
4453 }
4454 if (codec_drv->dapm_widgets) {
4455 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4456 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4457 }
4458 if (codec_drv->dapm_routes) {
4459 codec->component.dapm_routes = codec_drv->dapm_routes;
4460 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4461 }
4462
4463 if (codec_drv->probe)
4464 codec->component.probe = snd_soc_codec_drv_probe;
4465 if (codec_drv->remove)
4466 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004467 if (codec_drv->write)
4468 codec->component.write = snd_soc_codec_drv_write;
4469 if (codec_drv->read)
4470 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004471 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004472 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004473 if (codec_drv->seq_notifier)
4474 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004475 if (codec_drv->set_bias_level)
4476 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004477 codec->dev = dev;
4478 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004479 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004480 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004481
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004482#ifdef CONFIG_DEBUG_FS
4483 codec->component.init_debugfs = soc_init_codec_debugfs;
4484 codec->component.debugfs_prefix = "codec";
4485#endif
4486
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004487 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004488 if (codec_drv->get_regmap)
4489 regmap = codec_drv->get_regmap(dev);
4490 else
4491 regmap = dev_get_regmap(dev, NULL);
4492
4493 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004494 ret = snd_soc_component_init_io(&codec->component,
4495 regmap);
4496 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004497 dev_err(codec->dev,
4498 "Failed to set cache I/O:%d\n",
4499 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004500 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004501 }
4502 }
4503 }
4504
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004505 for (i = 0; i < num_dai; i++) {
4506 fixup_codec_formats(&dai_drv[i].playback);
4507 fixup_codec_formats(&dai_drv[i].capture);
4508 }
4509
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004510 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4511 if (ret < 0) {
4512 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4513 goto err_cleanup;
4514 }
4515
4516 list_for_each_entry(dai, &codec->component.dai_list, list)
4517 dai->codec = codec;
4518
Mark Brown054880f2012-04-09 17:29:19 +01004519 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004520 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004521 list_add(&codec->list, &codec_list);
4522 mutex_unlock(&client_mutex);
4523
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004524 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4525 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004526 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004527
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004528err_cleanup:
4529 snd_soc_component_cleanup(&codec->component);
4530err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004531 kfree(codec);
4532 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004533}
4534EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4535
4536/**
4537 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4538 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004539 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004540 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004541void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004542{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004543 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004544
4545 list_for_each_entry(codec, &codec_list, list) {
4546 if (dev == codec->dev)
4547 goto found;
4548 }
4549 return;
4550
4551found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004552
Mark Brown0d0cf002008-12-10 14:32:45 +00004553 mutex_lock(&client_mutex);
4554 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004555 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004556 mutex_unlock(&client_mutex);
4557
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004558 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4559 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004560
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004561 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004562 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004563 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004564}
4565EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4566
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004567/* Retrieve a card's name from device tree */
4568int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4569 const char *propname)
4570{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304571 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004572 int ret;
4573
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304574 if (!card->dev) {
4575 pr_err("card->dev is not set before calling %s\n", __func__);
4576 return -EINVAL;
4577 }
4578
4579 np = card->dev->of_node;
4580
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004581 ret = of_property_read_string_index(np, propname, 0, &card->name);
4582 /*
4583 * EINVAL means the property does not exist. This is fine providing
4584 * card->name was previously set, which is checked later in
4585 * snd_soc_register_card.
4586 */
4587 if (ret < 0 && ret != -EINVAL) {
4588 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004589 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004590 propname, ret);
4591 return ret;
4592 }
4593
4594 return 0;
4595}
4596EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4597
Xiubo Li9a6d4862014-02-08 15:59:52 +08004598static const struct snd_soc_dapm_widget simple_widgets[] = {
4599 SND_SOC_DAPM_MIC("Microphone", NULL),
4600 SND_SOC_DAPM_LINE("Line", NULL),
4601 SND_SOC_DAPM_HP("Headphone", NULL),
4602 SND_SOC_DAPM_SPK("Speaker", NULL),
4603};
4604
4605int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4606 const char *propname)
4607{
4608 struct device_node *np = card->dev->of_node;
4609 struct snd_soc_dapm_widget *widgets;
4610 const char *template, *wname;
4611 int i, j, num_widgets, ret;
4612
4613 num_widgets = of_property_count_strings(np, propname);
4614 if (num_widgets < 0) {
4615 dev_err(card->dev,
4616 "ASoC: Property '%s' does not exist\n", propname);
4617 return -EINVAL;
4618 }
4619 if (num_widgets & 1) {
4620 dev_err(card->dev,
4621 "ASoC: Property '%s' length is not even\n", propname);
4622 return -EINVAL;
4623 }
4624
4625 num_widgets /= 2;
4626 if (!num_widgets) {
4627 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4628 propname);
4629 return -EINVAL;
4630 }
4631
4632 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4633 GFP_KERNEL);
4634 if (!widgets) {
4635 dev_err(card->dev,
4636 "ASoC: Could not allocate memory for widgets\n");
4637 return -ENOMEM;
4638 }
4639
4640 for (i = 0; i < num_widgets; i++) {
4641 ret = of_property_read_string_index(np, propname,
4642 2 * i, &template);
4643 if (ret) {
4644 dev_err(card->dev,
4645 "ASoC: Property '%s' index %d read error:%d\n",
4646 propname, 2 * i, ret);
4647 return -EINVAL;
4648 }
4649
4650 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4651 if (!strncmp(template, simple_widgets[j].name,
4652 strlen(simple_widgets[j].name))) {
4653 widgets[i] = simple_widgets[j];
4654 break;
4655 }
4656 }
4657
4658 if (j >= ARRAY_SIZE(simple_widgets)) {
4659 dev_err(card->dev,
4660 "ASoC: DAPM widget '%s' is not supported\n",
4661 template);
4662 return -EINVAL;
4663 }
4664
4665 ret = of_property_read_string_index(np, propname,
4666 (2 * i) + 1,
4667 &wname);
4668 if (ret) {
4669 dev_err(card->dev,
4670 "ASoC: Property '%s' index %d read error:%d\n",
4671 propname, (2 * i) + 1, ret);
4672 return -EINVAL;
4673 }
4674
4675 widgets[i].name = wname;
4676 }
4677
4678 card->dapm_widgets = widgets;
4679 card->num_dapm_widgets = num_widgets;
4680
4681 return 0;
4682}
4683EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4684
Xiubo Li89c67852014-02-14 09:34:35 +08004685int snd_soc_of_parse_tdm_slot(struct device_node *np,
4686 unsigned int *slots,
4687 unsigned int *slot_width)
4688{
4689 u32 val;
4690 int ret;
4691
4692 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4693 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4694 if (ret)
4695 return ret;
4696
4697 if (slots)
4698 *slots = val;
4699 }
4700
4701 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4702 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4703 if (ret)
4704 return ret;
4705
4706 if (slot_width)
4707 *slot_width = val;
4708 }
4709
4710 return 0;
4711}
4712EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4713
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004714int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4715 const char *propname)
4716{
4717 struct device_node *np = card->dev->of_node;
4718 int num_routes;
4719 struct snd_soc_dapm_route *routes;
4720 int i, ret;
4721
4722 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004723 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004724 dev_err(card->dev,
4725 "ASoC: Property '%s' does not exist or its length is not even\n",
4726 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004727 return -EINVAL;
4728 }
4729 num_routes /= 2;
4730 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004731 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004732 propname);
4733 return -EINVAL;
4734 }
4735
4736 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4737 GFP_KERNEL);
4738 if (!routes) {
4739 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004740 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004741 return -EINVAL;
4742 }
4743
4744 for (i = 0; i < num_routes; i++) {
4745 ret = of_property_read_string_index(np, propname,
4746 2 * i, &routes[i].sink);
4747 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004748 dev_err(card->dev,
4749 "ASoC: Property '%s' index %d could not be read: %d\n",
4750 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004751 return -EINVAL;
4752 }
4753 ret = of_property_read_string_index(np, propname,
4754 (2 * i) + 1, &routes[i].source);
4755 if (ret) {
4756 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004757 "ASoC: Property '%s' index %d could not be read: %d\n",
4758 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004759 return -EINVAL;
4760 }
4761 }
4762
4763 card->num_dapm_routes = num_routes;
4764 card->dapm_routes = routes;
4765
4766 return 0;
4767}
4768EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4769
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004770unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004771 const char *prefix,
4772 struct device_node **bitclkmaster,
4773 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004774{
4775 int ret, i;
4776 char prop[128];
4777 unsigned int format = 0;
4778 int bit, frame;
4779 const char *str;
4780 struct {
4781 char *name;
4782 unsigned int val;
4783 } of_fmt_table[] = {
4784 { "i2s", SND_SOC_DAIFMT_I2S },
4785 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4786 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4787 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4788 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4789 { "ac97", SND_SOC_DAIFMT_AC97 },
4790 { "pdm", SND_SOC_DAIFMT_PDM},
4791 { "msb", SND_SOC_DAIFMT_MSB },
4792 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004793 };
4794
4795 if (!prefix)
4796 prefix = "";
4797
4798 /*
4799 * check "[prefix]format = xxx"
4800 * SND_SOC_DAIFMT_FORMAT_MASK area
4801 */
4802 snprintf(prop, sizeof(prop), "%sformat", prefix);
4803 ret = of_property_read_string(np, prop, &str);
4804 if (ret == 0) {
4805 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4806 if (strcmp(str, of_fmt_table[i].name) == 0) {
4807 format |= of_fmt_table[i].val;
4808 break;
4809 }
4810 }
4811 }
4812
4813 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004814 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004815 * SND_SOC_DAIFMT_CLOCK_MASK area
4816 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004817 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4818 if (of_get_property(np, prop, NULL))
4819 format |= SND_SOC_DAIFMT_CONT;
4820 else
4821 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004822
4823 /*
4824 * check "[prefix]bitclock-inversion"
4825 * check "[prefix]frame-inversion"
4826 * SND_SOC_DAIFMT_INV_MASK area
4827 */
4828 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4829 bit = !!of_get_property(np, prop, NULL);
4830
4831 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4832 frame = !!of_get_property(np, prop, NULL);
4833
4834 switch ((bit << 4) + frame) {
4835 case 0x11:
4836 format |= SND_SOC_DAIFMT_IB_IF;
4837 break;
4838 case 0x10:
4839 format |= SND_SOC_DAIFMT_IB_NF;
4840 break;
4841 case 0x01:
4842 format |= SND_SOC_DAIFMT_NB_IF;
4843 break;
4844 default:
4845 /* SND_SOC_DAIFMT_NB_NF is default */
4846 break;
4847 }
4848
4849 /*
4850 * check "[prefix]bitclock-master"
4851 * check "[prefix]frame-master"
4852 * SND_SOC_DAIFMT_MASTER_MASK area
4853 */
4854 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4855 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004856 if (bit && bitclkmaster)
4857 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004858
4859 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4860 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004861 if (frame && framemaster)
4862 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004863
4864 switch ((bit << 4) + frame) {
4865 case 0x11:
4866 format |= SND_SOC_DAIFMT_CBM_CFM;
4867 break;
4868 case 0x10:
4869 format |= SND_SOC_DAIFMT_CBM_CFS;
4870 break;
4871 case 0x01:
4872 format |= SND_SOC_DAIFMT_CBS_CFM;
4873 break;
4874 default:
4875 format |= SND_SOC_DAIFMT_CBS_CFS;
4876 break;
4877 }
4878
4879 return format;
4880}
4881EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4882
Kuninori Morimotocb470082013-09-10 17:39:56 -07004883int snd_soc_of_get_dai_name(struct device_node *of_node,
4884 const char **dai_name)
4885{
4886 struct snd_soc_component *pos;
4887 struct of_phandle_args args;
4888 int ret;
4889
4890 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4891 "#sound-dai-cells", 0, &args);
4892 if (ret)
4893 return ret;
4894
4895 ret = -EPROBE_DEFER;
4896
4897 mutex_lock(&client_mutex);
4898 list_for_each_entry(pos, &component_list, list) {
4899 if (pos->dev->of_node != args.np)
4900 continue;
4901
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004902 if (pos->driver->of_xlate_dai_name) {
4903 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4904 } else {
4905 int id = -1;
4906
4907 switch (args.args_count) {
4908 case 0:
4909 id = 0; /* same as dai_drv[0] */
4910 break;
4911 case 1:
4912 id = args.args[0];
4913 break;
4914 default:
4915 /* not supported */
4916 break;
4917 }
4918
4919 if (id < 0 || id >= pos->num_dai) {
4920 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004921 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004922 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004923
4924 ret = 0;
4925
4926 *dai_name = pos->dai_drv[id].name;
4927 if (!*dai_name)
4928 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004929 }
4930
Kuninori Morimotocb470082013-09-10 17:39:56 -07004931 break;
4932 }
4933 mutex_unlock(&client_mutex);
4934
4935 of_node_put(args.np);
4936
4937 return ret;
4938}
4939EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4940
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004941static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004942{
Mark Brown384c89e2008-12-03 17:34:03 +00004943#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004944 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4945 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004946 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004947 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004948 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004949
Mark Brown8a9dab12011-01-10 22:25:21 +00004950 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004951 &codec_list_fops))
4952 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4953
Mark Brown8a9dab12011-01-10 22:25:21 +00004954 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004955 &dai_list_fops))
4956 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004957
Mark Brown8a9dab12011-01-10 22:25:21 +00004958 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004959 &platform_list_fops))
4960 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004961#endif
4962
Mark Brownfb257892011-04-28 10:57:54 +01004963 snd_soc_util_init();
4964
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004965 return platform_driver_register(&soc_driver);
4966}
Mark Brown4abe8e12010-10-12 17:41:03 +01004967module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004968
Mark Brown7d8c16a2008-11-30 22:11:24 +00004969static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004970{
Mark Brownfb257892011-04-28 10:57:54 +01004971 snd_soc_util_exit();
4972
Mark Brown384c89e2008-12-03 17:34:03 +00004973#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004974 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004975#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004976 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004977}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004978module_exit(snd_soc_exit);
4979
4980/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004981MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004982MODULE_DESCRIPTION("ALSA SoC Core");
4983MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004984MODULE_ALIAS("platform:soc-audio");